freeze_spec_const_test.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright (c) 2016 Google Inc.
  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 <tuple>
  16. #include <utility>
  17. #include <vector>
  18. #include "test/opt/pass_fixture.h"
  19. #include "test/opt/pass_utils.h"
  20. namespace spvtools {
  21. namespace opt {
  22. namespace {
  23. struct FreezeSpecConstantValueTypeTestCase {
  24. const char* type_decl;
  25. const char* spec_const;
  26. const char* expected_frozen_const;
  27. };
  28. using FreezeSpecConstantValueTypeTest =
  29. PassTest<::testing::TestWithParam<FreezeSpecConstantValueTypeTestCase>>;
  30. TEST_P(FreezeSpecConstantValueTypeTest, PrimaryType) {
  31. auto& test_case = GetParam();
  32. std::vector<const char*> text = {"OpCapability Shader",
  33. "OpMemoryModel Logical GLSL450",
  34. test_case.type_decl, test_case.spec_const};
  35. std::vector<const char*> expected = {
  36. "OpCapability Shader", "OpMemoryModel Logical GLSL450",
  37. test_case.type_decl, test_case.expected_frozen_const};
  38. SinglePassRunAndCheck<FreezeSpecConstantValuePass>(
  39. JoinAllInsts(text), JoinAllInsts(expected), /* skip_nop = */ false);
  40. }
  41. // Test each primary type.
  42. INSTANTIATE_TEST_SUITE_P(
  43. PrimaryTypeSpecConst, FreezeSpecConstantValueTypeTest,
  44. ::testing::ValuesIn(std::vector<FreezeSpecConstantValueTypeTestCase>({
  45. // Type declaration, original spec constant definition, expected frozen
  46. // spec constants.
  47. {"%int = OpTypeInt 32 1", "%2 = OpSpecConstant %int 1",
  48. "%int_1 = OpConstant %int 1"},
  49. {"%uint = OpTypeInt 32 0", "%2 = OpSpecConstant %uint 1",
  50. "%uint_1 = OpConstant %uint 1"},
  51. {"%float = OpTypeFloat 32", "%2 = OpSpecConstant %float 3.1415",
  52. "%float_3_1415 = OpConstant %float 3.1415"},
  53. {"%double = OpTypeFloat 64", "%2 = OpSpecConstant %double 3.141592653",
  54. "%double_3_141592653 = OpConstant %double 3.141592653"},
  55. {"%bool = OpTypeBool", "%2 = OpSpecConstantTrue %bool",
  56. "%true = OpConstantTrue %bool"},
  57. {"%bool = OpTypeBool", "%2 = OpSpecConstantFalse %bool",
  58. "%false = OpConstantFalse %bool"},
  59. })));
  60. using FreezeSpecConstantValueRemoveDecorationTest = PassTest<::testing::Test>;
  61. TEST_F(FreezeSpecConstantValueRemoveDecorationTest,
  62. RemoveDecorationInstWithSpecId) {
  63. std::vector<const char*> text = {
  64. // clang-format off
  65. "OpCapability Shader",
  66. "OpCapability Float64",
  67. "%1 = OpExtInstImport \"GLSL.std.450\"",
  68. "OpMemoryModel Logical GLSL450",
  69. "OpEntryPoint Vertex %main \"main\"",
  70. "OpSource GLSL 450",
  71. "OpSourceExtension \"GL_GOOGLE_cpp_style_line_directive\"",
  72. "OpSourceExtension \"GL_GOOGLE_include_directive\"",
  73. "OpName %main \"main\"",
  74. "OpDecorate %3 SpecId 200",
  75. "OpDecorate %4 SpecId 201",
  76. "OpDecorate %5 SpecId 202",
  77. "OpDecorate %6 SpecId 203",
  78. "%void = OpTypeVoid",
  79. "%8 = OpTypeFunction %void",
  80. "%int = OpTypeInt 32 1",
  81. "%3 = OpSpecConstant %int 3",
  82. "%float = OpTypeFloat 32",
  83. "%4 = OpSpecConstant %float 3.1415",
  84. "%double = OpTypeFloat 64",
  85. "%5 = OpSpecConstant %double 3.14159265358979",
  86. "%bool = OpTypeBool",
  87. "%6 = OpSpecConstantTrue %bool",
  88. "%13 = OpSpecConstantFalse %bool",
  89. "%main = OpFunction %void None %8",
  90. "%14 = OpLabel",
  91. "OpReturn",
  92. "OpFunctionEnd",
  93. // clang-format on
  94. };
  95. std::string expected_disassembly = SelectiveJoin(text, [](const char* line) {
  96. return std::string(line).find("SpecId") != std::string::npos;
  97. });
  98. std::vector<std::pair<const char*, const char*>> replacement_pairs = {
  99. {"%3 = OpSpecConstant %int 3", "%int_3 = OpConstant %int 3"},
  100. {"%4 = OpSpecConstant %float 3.1415",
  101. "%float_3_1415 = OpConstant %float 3.1415"},
  102. {"%5 = OpSpecConstant %double 3.14159265358979",
  103. "%double_3_14159265358979 = OpConstant %double 3.14159265358979"},
  104. {"%6 = OpSpecConstantTrue ", "%true = OpConstantTrue "},
  105. {"%13 = OpSpecConstantFalse ", "%false = OpConstantFalse "},
  106. };
  107. for (auto& p : replacement_pairs) {
  108. EXPECT_TRUE(FindAndReplace(&expected_disassembly, p.first, p.second))
  109. << "text:\n"
  110. << expected_disassembly << "\n"
  111. << "find_str:\n"
  112. << p.first << "\n"
  113. << "replace_str:\n"
  114. << p.second << "\n";
  115. }
  116. SinglePassRunAndCheck<FreezeSpecConstantValuePass>(JoinAllInsts(text),
  117. expected_disassembly,
  118. /* skip_nop = */ true);
  119. }
  120. TEST_F(FreezeSpecConstantValueRemoveDecorationTest,
  121. RemoveDecorationForLocalSizeIdWithSpecId) {
  122. std::vector<const char*> text = {
  123. // clang-format off
  124. "OpCapability Shader",
  125. "%1 = OpExtInstImport \"GLSL.std.450\"",
  126. "OpMemoryModel Logical GLSL450",
  127. "OpEntryPoint GLCompute %2 \"main\"",
  128. "OpExecutionModeId %2 LocalSizeId %uint_32 %uint_1 %uint_1_0",
  129. "OpSource GLSL 450",
  130. "OpDecorate %3 SpecId 18",
  131. "OpDecorate %5 SpecId 19",
  132. "%void = OpTypeVoid",
  133. "%9 = OpTypeFunction %void",
  134. "%uint = OpTypeInt 32 0",
  135. "%uint_32 = OpSpecConstant %uint 32",
  136. "%uint_1 = OpConstant %uint 1",
  137. "%uint_1_0 = OpSpecConstant %uint 1",
  138. "%2 = OpFunction %void None %9",
  139. "%11 = OpLabel",
  140. "OpReturn",
  141. "OpFunctionEnd",
  142. // clang-format on
  143. };
  144. std::string expected_disassembly = SelectiveJoin(text, [](const char* line) {
  145. return std::string(line).find("SpecId") != std::string::npos;
  146. });
  147. std::vector<std::pair<const char*, const char*>> replacement_pairs = {
  148. {"%uint_32 = OpSpecConstant %uint 32", "%uint_32 = OpConstant %uint 32"},
  149. {"%uint_1_0 = OpSpecConstant %uint 1", "%uint_1_0 = OpConstant %uint 1"},
  150. };
  151. for (auto& p : replacement_pairs) {
  152. EXPECT_TRUE(FindAndReplace(&expected_disassembly, p.first, p.second))
  153. << "text:\n"
  154. << expected_disassembly << "\n"
  155. << "find_str:\n"
  156. << p.first << "\n"
  157. << "replace_str:\n"
  158. << p.second << "\n";
  159. }
  160. SinglePassRunAndCheck<FreezeSpecConstantValuePass>(JoinAllInsts(text),
  161. expected_disassembly,
  162. /* skip_nop = */ true);
  163. }
  164. } // namespace
  165. } // namespace opt
  166. } // namespace spvtools