global_values_amount_test.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2017 Pierre Moreau
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string>
  15. #include "gmock/gmock.h"
  16. #include "test/link/linker_fixture.h"
  17. namespace spvtools {
  18. namespace {
  19. using ::testing::HasSubstr;
  20. const uint32_t binary_count = 2u;
  21. class EntryPointsAmountTest : public spvtest::LinkerTest {
  22. public:
  23. EntryPointsAmountTest() { binaries.reserve(binary_count + 1u); }
  24. void SetUp() override {
  25. const uint32_t global_variable_count_per_binary =
  26. (SPV_LIMIT_GLOBAL_VARIABLES_MAX - 1u) / binary_count;
  27. spvtest::Binary common_binary = {
  28. // clang-format off
  29. static_cast<uint32_t>(spv::MagicNumber),
  30. static_cast<uint32_t>(spv::Version),
  31. SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS, 0),
  32. 3u + global_variable_count_per_binary, // NOTE: Bound
  33. 0u, // NOTE: Schema; reserved
  34. static_cast<uint32_t>(spv::Op::OpCapability) | 2u << spv::WordCountShift,
  35. static_cast<uint32_t>(spv::Capability::Shader),
  36. static_cast<uint32_t>(spv::Op::OpMemoryModel) | 3u << spv::WordCountShift,
  37. static_cast<uint32_t>(spv::AddressingModel::Logical),
  38. static_cast<uint32_t>(spv::MemoryModel::Simple),
  39. static_cast<uint32_t>(spv::Op::OpTypeFloat) | 3u << spv::WordCountShift,
  40. 1u, // NOTE: Result ID
  41. 32u, // NOTE: Width
  42. static_cast<uint32_t>(spv::Op::OpTypePointer) | 4u << spv::WordCountShift,
  43. 2u, // NOTE: Result ID
  44. static_cast<uint32_t>(spv::StorageClass::Input),
  45. 1u // NOTE: Type ID
  46. // clang-format on
  47. };
  48. binaries.push_back({});
  49. spvtest::Binary& binary = binaries.back();
  50. binary.reserve(common_binary.size() + global_variable_count_per_binary * 4);
  51. binary.insert(binary.end(), common_binary.cbegin(), common_binary.cend());
  52. for (uint32_t i = 0u; i < global_variable_count_per_binary; ++i) {
  53. binary.push_back(static_cast<uint32_t>(spv::Op::OpVariable) |
  54. 4u << spv::WordCountShift);
  55. binary.push_back(2u); // NOTE: Type ID
  56. binary.push_back(3u + i); // NOTE: Result ID
  57. binary.push_back(static_cast<uint32_t>(spv::StorageClass::Input));
  58. }
  59. for (uint32_t i = 0u; i < binary_count - 1u; ++i) {
  60. binaries.push_back(binaries.back());
  61. }
  62. }
  63. void TearDown() override { binaries.clear(); }
  64. spvtest::Binaries binaries;
  65. };
  66. TEST_F(EntryPointsAmountTest, UnderLimit) {
  67. spvtest::Binary linked_binary;
  68. ASSERT_EQ(SPV_SUCCESS, Link(binaries, &linked_binary)) << GetErrorMessage();
  69. EXPECT_THAT(GetErrorMessage(), std::string());
  70. }
  71. TEST_F(EntryPointsAmountTest, OverLimit) {
  72. binaries.push_back({
  73. // clang-format off
  74. static_cast<uint32_t>(spv::MagicNumber),
  75. static_cast<uint32_t>(spv::Version),
  76. SPV_GENERATOR_WORD(SPV_GENERATOR_KHRONOS, 0),
  77. 5u, // NOTE: Bound
  78. 0u, // NOTE: Schema; reserved
  79. static_cast<uint32_t>(spv::Op::OpCapability) | 2u << spv::WordCountShift,
  80. static_cast<uint32_t>(spv::Capability::Shader),
  81. static_cast<uint32_t>(spv::Op::OpMemoryModel) | 3u << spv::WordCountShift,
  82. static_cast<uint32_t>(spv::AddressingModel::Logical),
  83. static_cast<uint32_t>(spv::MemoryModel::Simple),
  84. static_cast<uint32_t>(spv::Op::OpTypeFloat) | 3u << spv::WordCountShift,
  85. 1u, // NOTE: Result ID
  86. 32u, // NOTE: Width
  87. static_cast<uint32_t>(spv::Op::OpTypePointer) | 4u << spv::WordCountShift,
  88. 2u, // NOTE: Result ID
  89. static_cast<uint32_t>(spv::StorageClass::Input),
  90. 1u, // NOTE: Type ID
  91. static_cast<uint32_t>(spv::Op::OpVariable) | 4u << spv::WordCountShift,
  92. 2u, // NOTE: Type ID
  93. 3u, // NOTE: Result ID
  94. static_cast<uint32_t>(spv::StorageClass::Input),
  95. static_cast<uint32_t>(spv::Op::OpVariable) | 4u << spv::WordCountShift,
  96. 2u, // NOTE: Type ID
  97. 4u, // NOTE: Result ID
  98. static_cast<uint32_t>(spv::StorageClass::Input)
  99. // clang-format on
  100. });
  101. spvtest::Binary linked_binary;
  102. ASSERT_EQ(SPV_SUCCESS, Link(binaries, &linked_binary)) << GetErrorMessage();
  103. EXPECT_THAT(
  104. GetErrorMessage(),
  105. HasSubstr("The minimum limit of global values, 65535, was exceeded; "
  106. "65536 global values were found."));
  107. }
  108. } // namespace
  109. } // namespace spvtools