freeze_spec_const_test.cpp 5.2 KB

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