| 1 |
// Copyright (c) 2019 Google LLC. |
| 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 |
|
| 15 |
#include <vector> |
| 16 |
|
| 17 |
#include "test/opt/pass_fixture.h" |
| 18 |
#include "test/opt/pass_utils.h" |
| 19 |
|
| 20 |
namespace spvtools { |
| 21 |
namespace opt { |
| 22 |
namespace { |
| 23 |
|
| 24 |
using DecomposeInitializedVariablesTest = PassTest<::testing::Test>; |
| 25 |
|
| 26 |
std::string single_entry_header = R"(OpCapability Shader |
| 27 |
OpCapability VulkanMemoryModel |
| 28 |
OpExtension "SPV_KHR_vulkan_memory_model" |
| 29 |
OpMemoryModel Logical Vulkan |
| 30 |
OpEntryPoint Vertex %1 "shader" |
| 31 |
%uint = OpTypeInt 32 0 |
| 32 |
%uint_1 = OpConstant %uint 1 |
| 33 |
%4 = OpConstantNull %uint |
| 34 |
%void = OpTypeVoid |
| 35 |
%6 = OpTypeFunction %void |
| 36 |
)"; |
| 37 |
|
| 38 |
std::string GetFunctionTest(std::string body) { |
| 39 |
auto result = single_entry_header; |
| 40 |
result += "%_ptr_Function_uint = OpTypePointer Function %uint\n"; |
| 41 |
result += "%1 = OpFunction %void None %6\n"; |
| 42 |
result += "%8 = OpLabel\n"; |
| 43 |
result += body + "\n"; |
| 44 |
result += "OpReturn\n"; |
| 45 |
result += "OpFunctionEnd\n"; |
| 46 |
return result; |
| 47 |
} |
| 48 |
|
| 49 |
TEST_F(DecomposeInitializedVariablesTest, FunctionChanged) { |
| 50 |
std::string input = "%9 = OpVariable %_ptr_Function_uint Function %uint_1"; |
| 51 |
std::string expected = R"(%9 = OpVariable %_ptr_Function_uint Function |
| 52 |
OpStore %9 %uint_1)"; |
| 53 |
|
| 54 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 55 |
GetFunctionTest(input), GetFunctionTest(expected), |
| 56 |
/* skip_nop = */ false); |
| 57 |
} |
| 58 |
|
| 59 |
TEST_F(DecomposeInitializedVariablesTest, FunctionUnchanged) { |
| 60 |
std::string input = "%9 = OpVariable %_ptr_Function_uint Function"; |
| 61 |
|
| 62 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 63 |
GetFunctionTest(input), GetFunctionTest(input), /* skip_nop = */ false); |
| 64 |
} |
| 65 |
|
| 66 |
TEST_F(DecomposeInitializedVariablesTest, FunctionMultipleVariables) { |
| 67 |
std::string input = R"(%9 = OpVariable %_ptr_Function_uint Function %uint_1 |
| 68 |
%10 = OpVariable %_ptr_Function_uint Function %4)"; |
| 69 |
std::string expected = R"(%9 = OpVariable %_ptr_Function_uint Function |
| 70 |
%10 = OpVariable %_ptr_Function_uint Function |
| 71 |
OpStore %9 %uint_1 |
| 72 |
OpStore %10 %4)"; |
| 73 |
|
| 74 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 75 |
GetFunctionTest(input), GetFunctionTest(expected), |
| 76 |
/* skip_nop = */ false); |
| 77 |
} |
| 78 |
|
| 79 |
std::string GetGlobalTest(std::string storage_class, bool initialized, |
| 80 |
bool decomposed) { |
| 81 |
auto result = single_entry_header; |
| 82 |
|
| 83 |
result += "%_ptr_" + storage_class + "_uint = OpTypePointer " + |
| 84 |
storage_class + " %uint\n"; |
| 85 |
if (initialized) { |
| 86 |
result += "%8 = OpVariable %_ptr_" + storage_class + "_uint " + |
| 87 |
storage_class + " %4\n"; |
| 88 |
} else { |
| 89 |
result += "%8 = OpVariable %_ptr_" + storage_class + "_uint " + |
| 90 |
storage_class + "\n"; |
| 91 |
} |
| 92 |
result += R"(%1 = OpFunction %void None %9 |
| 93 |
%9 = OpLabel |
| 94 |
)"; |
| 95 |
if (decomposed) result += "OpStore %8 %4\n"; |
| 96 |
result += R"(OpReturn |
| 97 |
OpFunctionEnd |
| 98 |
)"; |
| 99 |
return result; |
| 100 |
} |
| 101 |
|
| 102 |
TEST_F(DecomposeInitializedVariablesTest, PrivateChanged) { |
| 103 |
std::string input = GetGlobalTest("Private", true, false); |
| 104 |
std::string expected = GetGlobalTest("Private", false, true); |
| 105 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 106 |
input, expected, /* skip_nop = */ false); |
| 107 |
} |
| 108 |
|
| 109 |
TEST_F(DecomposeInitializedVariablesTest, PrivateUnchanged) { |
| 110 |
std::string input = GetGlobalTest("Private", false, false); |
| 111 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 112 |
input, input, /* skip_nop = */ false); |
| 113 |
} |
| 114 |
|
| 115 |
TEST_F(DecomposeInitializedVariablesTest, OutputChanged) { |
| 116 |
std::string input = GetGlobalTest("Output", true, false); |
| 117 |
std::string expected = GetGlobalTest("Output", false, true); |
| 118 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 119 |
input, expected, /* skip_nop = */ false); |
| 120 |
} |
| 121 |
|
| 122 |
TEST_F(DecomposeInitializedVariablesTest, OutputUnchanged) { |
| 123 |
std::string input = GetGlobalTest("Output", false, false); |
| 124 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 125 |
input, input, /* skip_nop = */ false); |
| 126 |
} |
| 127 |
|
| 128 |
std::string multiple_entry_header = R"(OpCapability Shader |
| 129 |
OpCapability VulkanMemoryModel |
| 130 |
OpExtension "SPV_KHR_vulkan_memory_model" |
| 131 |
OpMemoryModel Logical Vulkan |
| 132 |
OpEntryPoint Vertex %1 "vertex" |
| 133 |
OpEntryPoint Fragment %2 "fragment" |
| 134 |
%uint = OpTypeInt 32 0 |
| 135 |
%4 = OpConstantNull %uint |
| 136 |
%void = OpTypeVoid |
| 137 |
%6 = OpTypeFunction %void |
| 138 |
)"; |
| 139 |
|
| 140 |
std::string GetGlobalMultipleEntryTest(std::string storage_class, |
| 141 |
bool initialized, bool decomposed) { |
| 142 |
auto result = multiple_entry_header; |
| 143 |
result += "%_ptr_" + storage_class + "_uint = OpTypePointer " + |
| 144 |
storage_class + " %uint\n"; |
| 145 |
if (initialized) { |
| 146 |
result += "%8 = OpVariable %_ptr_" + storage_class + "_uint " + |
| 147 |
storage_class + " %4\n"; |
| 148 |
} else { |
| 149 |
result += "%8 = OpVariable %_ptr_" + storage_class + "_uint " + |
| 150 |
storage_class + "\n"; |
| 151 |
} |
| 152 |
result += R"(%1 = OpFunction %void None %9 |
| 153 |
%9 = OpLabel |
| 154 |
)"; |
| 155 |
if (decomposed) result += "OpStore %8 %4\n"; |
| 156 |
result += R"(OpReturn |
| 157 |
OpFunctionEnd |
| 158 |
%2 = OpFunction %void None %10 |
| 159 |
%10 = OpLabel |
| 160 |
)"; |
| 161 |
if (decomposed) result += "OpStore %8 %4\n"; |
| 162 |
result += R"(OpReturn |
| 163 |
OpFunctionEnd |
| 164 |
)"; |
| 165 |
|
| 166 |
return result; |
| 167 |
} |
| 168 |
|
| 169 |
TEST_F(DecomposeInitializedVariablesTest, PrivateMultipleEntryChanged) { |
| 170 |
std::string input = GetGlobalMultipleEntryTest("Private", true, false); |
| 171 |
std::string expected = GetGlobalMultipleEntryTest("Private", false, true); |
| 172 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 173 |
input, expected, /* skip_nop = */ false); |
| 174 |
} |
| 175 |
|
| 176 |
TEST_F(DecomposeInitializedVariablesTest, PrivateMultipleEntryUnchanged) { |
| 177 |
std::string input = GetGlobalMultipleEntryTest("Private", false, false); |
| 178 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 179 |
input, input, /* skip_nop = */ false); |
| 180 |
} |
| 181 |
|
| 182 |
TEST_F(DecomposeInitializedVariablesTest, OutputMultipleEntryChanged) { |
| 183 |
std::string input = GetGlobalMultipleEntryTest("Output", true, false); |
| 184 |
std::string expected = GetGlobalMultipleEntryTest("Output", false, true); |
| 185 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 186 |
input, expected, /* skip_nop = */ false); |
| 187 |
} |
| 188 |
|
| 189 |
TEST_F(DecomposeInitializedVariablesTest, OutputMultipleEntryUnchanged) { |
| 190 |
std::string input = GetGlobalMultipleEntryTest("Output", false, false); |
| 191 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 192 |
input, input, /* skip_nop = */ false); |
| 193 |
} |
| 194 |
|
| 195 |
std::string GetGlobalWithNonEntryPointTest(std::string storage_class, |
| 196 |
bool initialized, bool decomposed) { |
| 197 |
auto result = single_entry_header; |
| 198 |
result += "%_ptr_" + storage_class + "_uint = OpTypePointer " + |
| 199 |
storage_class + " %uint\n"; |
| 200 |
if (initialized) { |
| 201 |
result += "%8 = OpVariable %_ptr_" + storage_class + "_uint " + |
| 202 |
storage_class + " %4\n"; |
| 203 |
} else { |
| 204 |
result += "%8 = OpVariable %_ptr_" + storage_class + "_uint " + |
| 205 |
storage_class + "\n"; |
| 206 |
} |
| 207 |
result += R"(%1 = OpFunction %void None %9 |
| 208 |
%9 = OpLabel |
| 209 |
)"; |
| 210 |
if (decomposed) result += "OpStore %8 %4\n"; |
| 211 |
result += R"(OpReturn |
| 212 |
OpFunctionEnd |
| 213 |
%10 = OpFunction %void None %11 |
| 214 |
%11 = OpLabel |
| 215 |
OpReturn |
| 216 |
OpFunctionEnd |
| 217 |
)"; |
| 218 |
|
| 219 |
return result; |
| 220 |
} |
| 221 |
|
| 222 |
TEST_F(DecomposeInitializedVariablesTest, PrivateWithNonEntryPointChanged) { |
| 223 |
std::string input = GetGlobalWithNonEntryPointTest("Private", true, false); |
| 224 |
std::string expected = GetGlobalWithNonEntryPointTest("Private", false, true); |
| 225 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 226 |
input, expected, /* skip_nop = */ false); |
| 227 |
} |
| 228 |
|
| 229 |
TEST_F(DecomposeInitializedVariablesTest, PrivateWithNonEntryPointUnchanged) { |
| 230 |
std::string input = GetGlobalWithNonEntryPointTest("Private", false, false); |
| 231 |
// SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS); |
| 232 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 233 |
input, input, /* skip_nop = */ false); |
| 234 |
} |
| 235 |
|
| 236 |
TEST_F(DecomposeInitializedVariablesTest, OutputWithNonEntryPointChanged) { |
| 237 |
std::string input = GetGlobalWithNonEntryPointTest("Output", true, false); |
| 238 |
std::string expected = GetGlobalWithNonEntryPointTest("Output", false, true); |
| 239 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 240 |
input, expected, /* skip_nop = */ false); |
| 241 |
} |
| 242 |
|
| 243 |
TEST_F(DecomposeInitializedVariablesTest, OutputWithNonEntryPointUnchanged) { |
| 244 |
std::string input = GetGlobalWithNonEntryPointTest("Output", false, false); |
| 245 |
// SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS); |
| 246 |
SinglePassRunAndCheck<DecomposeInitializedVariablesPass>( |
| 247 |
input, input, /* skip_nop = */ false); |
| 248 |
} |
| 249 |
|
| 250 |
} // namespace |
| 251 |
} // namespace opt |
| 252 |
} // namespace spvtools |