aboutsummaryrefslogtreecommitdiff
path: root/test/opt/freeze_spec_const_test.cpp
blob: 6c0949397212430c718cff63ca96fd862bb2d249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2016 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "pass_fixture.h"
#include "pass_utils.h"

#include <algorithm>
#include <tuple>
#include <vector>

namespace {

using namespace spvtools;

struct FreezeSpecConstantValueTypeTestCase {
  const char* type_decl;
  const char* spec_const;
  const char* expected_frozen_const;
};

using FreezeSpecConstantValueTypeTest =
    PassTest<::testing::TestWithParam<FreezeSpecConstantValueTypeTestCase>>;

TEST_P(FreezeSpecConstantValueTypeTest, PrimaryType) {
  auto& test_case = GetParam();
  std::vector<const char*> text = {"OpCapability Shader",
                                   "OpMemoryModel Logical GLSL450",
                                   test_case.type_decl, test_case.spec_const};
  std::vector<const char*> expected = {
      "OpCapability Shader", "OpMemoryModel Logical GLSL450",
      test_case.type_decl, test_case.expected_frozen_const};
  SinglePassRunAndCheck<opt::FreezeSpecConstantValuePass>(
      JoinAllInsts(text), JoinAllInsts(expected), /* skip_nop = */ false);
}

// Test each primary type.
INSTANTIATE_TEST_CASE_P(
    PrimaryTypeSpecConst, FreezeSpecConstantValueTypeTest,
    ::testing::ValuesIn(std::vector<FreezeSpecConstantValueTypeTestCase>({
        // Type declaration, original spec constant definition, expected frozen
        // spec constants.
        {"%int = OpTypeInt 32 1", "%2 = OpSpecConstant %int 1",
         "%2 = OpConstant %int 1"},
        {"%uint = OpTypeInt 32 0", "%2 = OpSpecConstant %uint 1",
         "%2 = OpConstant %uint 1"},
        {"%float = OpTypeFloat 32", "%2 = OpSpecConstant %float 3.14",
         "%2 = OpConstant %float 3.14"},
        {"%double = OpTypeFloat 64", "%2 = OpSpecConstant %double 3.1415926",
         "%2 = OpConstant %double 3.1415926"},
        {"%bool = OpTypeBool", "%2 = OpSpecConstantTrue %bool",
         "%2 = OpConstantTrue %bool"},
        {"%bool = OpTypeBool", "%2 = OpSpecConstantFalse %bool",
         "%2 = OpConstantFalse %bool"},
    })));

using FreezeSpecConstantValueRemoveDecorationTest = PassTest<::testing::Test>;

TEST_F(FreezeSpecConstantValueRemoveDecorationTest,
       RemoveDecorationInstWithSpecId) {
  std::vector<const char*> text = {
      // clang-format off
               "OpCapability Shader",
               "OpCapability Float64",
          "%1 = OpExtInstImport \"GLSL.std.450\"",
               "OpMemoryModel Logical GLSL450",
               "OpEntryPoint Vertex %main \"main\"",
               "OpSource GLSL 450",
               "OpSourceExtension \"GL_GOOGLE_cpp_style_line_directive\"",
               "OpSourceExtension \"GL_GOOGLE_include_directive\"",
               "OpName %main \"main\"",
               "OpDecorate %3 SpecId 200",
               "OpDecorate %4 SpecId 201",
               "OpDecorate %5 SpecId 202",
               "OpDecorate %6 SpecId 203",
       "%void = OpTypeVoid",
          "%8 = OpTypeFunction %void",
        "%int = OpTypeInt 32 1",
          "%3 = OpSpecConstant %int 3",
      "%float = OpTypeFloat 32",
          "%4 = OpSpecConstant %float 3.14",
     "%double = OpTypeFloat 64",
          "%5 = OpSpecConstant %double 3.14159265358979",
       "%bool = OpTypeBool",
          "%6 = OpSpecConstantTrue %bool",
          "%13 = OpSpecConstantFalse %bool",
       "%main = OpFunction %void None %8",
         "%14 = OpLabel",
               "OpReturn",
               "OpFunctionEnd",
      // clang-format on
  };
  std::string expected_disassembly = SelectiveJoin(text, [](const char* line) {
    return std::string(line).find("SpecId") != std::string::npos;
  });
  std::vector<std::pair<const char*, const char*>> opcode_replacement_pairs = {
      {" OpSpecConstant ", " OpConstant "},
      {" OpSpecConstantTrue ", " OpConstantTrue "},
      {" OpSpecConstantFalse ", " OpConstantFalse "},
  };
  for (auto& p : opcode_replacement_pairs) {
    EXPECT_TRUE(FindAndReplace(&expected_disassembly, p.first, p.second))
        << "text:\n"
        << expected_disassembly << "\n"
        << "find_str:\n"
        << p.first << "\n"
        << "replace_str:\n"
        << p.second << "\n";
  }
  SinglePassRunAndCheck<opt::FreezeSpecConstantValuePass>(
      JoinAllInsts(text), expected_disassembly,
      /* skip_nop = */ true);
}
}  // anonymous namespace