| 1 |
// Copyright (c) 2017 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 |
|
| 15 |
#include "spirv-tools/optimizer.hpp" |
| 16 |
|
| 17 |
#include <sstream> |
| 18 |
#include <string> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
#include "gmock/gmock.h" |
| 22 |
#include "spirv-tools/libspirv.hpp" |
| 23 |
#include "test/opt/pass_fixture.h" |
| 24 |
|
| 25 |
namespace spvtools { |
| 26 |
namespace opt { |
| 27 |
namespace { |
| 28 |
|
| 29 |
using ::testing::Eq; |
| 30 |
|
| 31 |
// Return a string that contains the minimum instructions needed to form |
| 32 |
// a valid module. Other instructions can be appended to this string. |
| 33 |
std::string Header() { |
| 34 |
return R"(OpCapability Shader |
| 35 |
OpCapability Linkage |
| 36 |
OpMemoryModel Logical GLSL450 |
| 37 |
)"; |
| 38 |
} |
| 39 |
|
| 40 |
TEST(Optimizer, CanRunNullPassWithDistinctInputOutputVectors) { |
| 41 |
SpirvTools tools(SPV_ENV_UNIVERSAL_1_0); |
| 42 |
std::vector<uint32_t> binary_in; |
| 43 |
tools.Assemble(Header() + "OpName %foo \"foo\"\n%foo = OpTypeVoid", |
| 44 |
&binary_in); |
| 45 |
|
| 46 |
Optimizer opt(SPV_ENV_UNIVERSAL_1_0); |
| 47 |
opt.RegisterPass(CreateNullPass()); |
| 48 |
std::vector<uint32_t> binary_out; |
| 49 |
opt.Run(binary_in.data(), binary_in.size(), &binary_out); |
| 50 |
|
| 51 |
std::string disassembly; |
| 52 |
tools.Disassemble(binary_out.data(), binary_out.size(), &disassembly); |
| 53 |
EXPECT_THAT(disassembly, |
| 54 |
Eq(Header() + "OpName %foo \"foo\"\n%foo = OpTypeVoid\n")); |
| 55 |
} |
| 56 |
|
| 57 |
TEST(Optimizer, CanRunTransformingPassWithDistinctInputOutputVectors) { |
| 58 |
SpirvTools tools(SPV_ENV_UNIVERSAL_1_0); |
| 59 |
std::vector<uint32_t> binary_in; |
| 60 |
tools.Assemble(Header() + "OpName %foo \"foo\"\n%foo = OpTypeVoid", |
| 61 |
&binary_in); |
| 62 |
|
| 63 |
Optimizer opt(SPV_ENV_UNIVERSAL_1_0); |
| 64 |
opt.RegisterPass(CreateStripDebugInfoPass()); |
| 65 |
std::vector<uint32_t> binary_out; |
| 66 |
opt.Run(binary_in.data(), binary_in.size(), &binary_out); |
| 67 |
|
| 68 |
std::string disassembly; |
| 69 |
tools.Disassemble(binary_out.data(), binary_out.size(), &disassembly); |
| 70 |
EXPECT_THAT(disassembly, Eq(Header() + "%void = OpTypeVoid\n")); |
| 71 |
} |
| 72 |
|
| 73 |
TEST(Optimizer, CanRunNullPassWithAliasedVectors) { |
| 74 |
SpirvTools tools(SPV_ENV_UNIVERSAL_1_0); |
| 75 |
std::vector<uint32_t> binary; |
| 76 |
tools.Assemble("OpName %foo \"foo\"\n%foo = OpTypeVoid", &binary); |
| 77 |
|
| 78 |
Optimizer opt(SPV_ENV_UNIVERSAL_1_0); |
| 79 |
opt.RegisterPass(CreateNullPass()); |
| 80 |
opt.Run(binary.data(), binary.size(), &binary); // This is the key. |
| 81 |
|
| 82 |
std::string disassembly; |
| 83 |
tools.Disassemble(binary.data(), binary.size(), &disassembly); |
| 84 |
EXPECT_THAT(disassembly, Eq("OpName %foo \"foo\"\n%foo = OpTypeVoid\n")); |
| 85 |
} |
| 86 |
|
| 87 |
TEST(Optimizer, CanRunNullPassWithAliasedVectorDataButDifferentSize) { |
| 88 |
SpirvTools tools(SPV_ENV_UNIVERSAL_1_0); |
| 89 |
std::vector<uint32_t> binary; |
| 90 |
tools.Assemble(Header() + "OpName %foo \"foo\"\n%foo = OpTypeVoid", &binary); |
| 91 |
|
| 92 |
Optimizer opt(SPV_ENV_UNIVERSAL_1_0); |
| 93 |
opt.RegisterPass(CreateNullPass()); |
| 94 |
auto orig_size = binary.size(); |
| 95 |
// Now change the size. Add a word that will be ignored |
| 96 |
// by the optimizer. |
| 97 |
binary.push_back(42); |
| 98 |
EXPECT_THAT(orig_size + 1, Eq(binary.size())); |
| 99 |
opt.Run(binary.data(), orig_size, &binary); // This is the key. |
| 100 |
// The binary vector should have been rewritten. |
| 101 |
EXPECT_THAT(binary.size(), Eq(orig_size)); |
| 102 |
|
| 103 |
std::string disassembly; |
| 104 |
tools.Disassemble(binary.data(), binary.size(), &disassembly); |
| 105 |
EXPECT_THAT(disassembly, |
| 106 |
Eq(Header() + "OpName %foo \"foo\"\n%foo = OpTypeVoid\n")); |
| 107 |
} |
| 108 |
|
| 109 |
TEST(Optimizer, CanRunTransformingPassWithAliasedVectors) { |
| 110 |
SpirvTools tools(SPV_ENV_UNIVERSAL_1_0); |
| 111 |
std::vector<uint32_t> binary; |
| 112 |
tools.Assemble(Header() + "OpName %foo \"foo\"\n%foo = OpTypeVoid", &binary); |
| 113 |
|
| 114 |
Optimizer opt(SPV_ENV_UNIVERSAL_1_0); |
| 115 |
opt.RegisterPass(CreateStripDebugInfoPass()); |
| 116 |
opt.Run(binary.data(), binary.size(), &binary); // This is the key |
| 117 |
|
| 118 |
std::string disassembly; |
| 119 |
tools.Disassemble(binary.data(), binary.size(), &disassembly); |
| 120 |
EXPECT_THAT(disassembly, Eq(Header() + "%void = OpTypeVoid\n")); |
| 121 |
} |
| 122 |
|
| 123 |
TEST(Optimizer, CanValidateFlags) { |
| 124 |
Optimizer opt(SPV_ENV_UNIVERSAL_1_0); |
| 125 |
EXPECT_FALSE(opt.FlagHasValidForm("bad-flag")); |
| 126 |
EXPECT_TRUE(opt.FlagHasValidForm("-O")); |
| 127 |
EXPECT_TRUE(opt.FlagHasValidForm("-Os")); |
| 128 |
EXPECT_FALSE(opt.FlagHasValidForm("-O2")); |
| 129 |
EXPECT_TRUE(opt.FlagHasValidForm("--this_flag")); |
| 130 |
} |
| 131 |
|
| 132 |
TEST(Optimizer, CanRegisterPassesFromFlags) { |
| 133 |
SpirvTools tools(SPV_ENV_UNIVERSAL_1_0); |
| 134 |
Optimizer opt(SPV_ENV_UNIVERSAL_1_0); |
| 135 |
|
| 136 |
spv_message_level_t msg_level; |
| 137 |
const char* msg_fname; |
| 138 |
spv_position_t msg_position; |
| 139 |
const char* msg; |
| 140 |
auto examine_message = [&msg_level, &msg_fname, &msg_position, &msg]( |
| 141 |
spv_message_level_t ml, const char* f, |
| 142 |
const spv_position_t& p, const char* m) { |
| 143 |
msg_level = ml; |
| 144 |
msg_fname = f; |
| 145 |
msg_position = p; |
| 146 |
msg = m; |
| 147 |
}; |
| 148 |
opt.SetMessageConsumer(examine_message); |
| 149 |
|
| 150 |
std::vector<std::string> pass_flags = { |
| 151 |
"--strip-debug", |
| 152 |
"--strip-nonsemantic", |
| 153 |
"--set-spec-const-default-value=23:42 21:12", |
| 154 |
"--if-conversion", |
| 155 |
"--freeze-spec-const", |
| 156 |
"--inline-entry-points-exhaustive", |
| 157 |
"--inline-entry-points-opaque", |
| 158 |
"--convert-local-access-chains", |
| 159 |
"--eliminate-dead-code-aggressive", |
| 160 |
"--eliminate-insert-extract", |
| 161 |
"--eliminate-local-single-block", |
| 162 |
"--eliminate-local-single-store", |
| 163 |
"--merge-blocks", |
| 164 |
"--merge-return", |
| 165 |
"--eliminate-dead-branches", |
| 166 |
"--eliminate-dead-functions", |
| 167 |
"--eliminate-local-multi-store", |
| 168 |
"--eliminate-dead-const", |
| 169 |
"--eliminate-dead-inserts", |
| 170 |
"--eliminate-dead-variables", |
| 171 |
"--fold-spec-const-op-composite", |
| 172 |
"--loop-unswitch", |
| 173 |
"--scalar-replacement=300", |
| 174 |
"--scalar-replacement", |
| 175 |
"--strength-reduction", |
| 176 |
"--unify-const", |
| 177 |
"--flatten-decorations", |
| 178 |
"--compact-ids", |
| 179 |
"--cfg-cleanup", |
| 180 |
"--local-redundancy-elimination", |
| 181 |
"--loop-invariant-code-motion", |
| 182 |
"--reduce-load-size", |
| 183 |
"--redundancy-elimination", |
| 184 |
"--private-to-local", |
| 185 |
"--remove-duplicates", |
| 186 |
"--workaround-1209", |
| 187 |
"--replace-invalid-opcode", |
| 188 |
"--simplify-instructions", |
| 189 |
"--ssa-rewrite", |
| 190 |
"--copy-propagate-arrays", |
| 191 |
"--loop-fission=20", |
| 192 |
"--loop-fusion=2", |
| 193 |
"--loop-unroll", |
| 194 |
"--vector-dce", |
| 195 |
"--loop-unroll-partial=3", |
| 196 |
"--loop-peeling", |
| 197 |
"--ccp", |
| 198 |
"-O", |
| 199 |
"-Os", |
| 200 |
"--legalize-hlsl"}; |
| 201 |
EXPECT_TRUE(opt.RegisterPassesFromFlags(pass_flags)); |
| 202 |
|
| 203 |
// Test some invalid flags. |
| 204 |
EXPECT_FALSE(opt.RegisterPassFromFlag("-O2")); |
| 205 |
EXPECT_EQ(msg_level, SPV_MSG_ERROR); |
| 206 |
|
| 207 |
EXPECT_FALSE(opt.RegisterPassFromFlag("-loop-unroll")); |
| 208 |
EXPECT_EQ(msg_level, SPV_MSG_ERROR); |
| 209 |
|
| 210 |
EXPECT_FALSE(opt.RegisterPassFromFlag("--set-spec-const-default-value")); |
| 211 |
EXPECT_EQ(msg_level, SPV_MSG_ERROR); |
| 212 |
|
| 213 |
EXPECT_FALSE(opt.RegisterPassFromFlag("--scalar-replacement=s")); |
| 214 |
EXPECT_EQ(msg_level, SPV_MSG_ERROR); |
| 215 |
|
| 216 |
EXPECT_FALSE(opt.RegisterPassFromFlag("--loop-fission=-4")); |
| 217 |
EXPECT_EQ(msg_level, SPV_MSG_ERROR); |
| 218 |
|
| 219 |
EXPECT_FALSE(opt.RegisterPassFromFlag("--loop-fusion=xx")); |
| 220 |
EXPECT_EQ(msg_level, SPV_MSG_ERROR); |
| 221 |
|
| 222 |
EXPECT_FALSE(opt.RegisterPassFromFlag("--loop-unroll-partial")); |
| 223 |
EXPECT_EQ(msg_level, SPV_MSG_ERROR); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
TEST(Optimizer, RemoveNop) { |
| 228 |
// Test that OpNops are removed even if no optimizations are run. |
| 229 |
const std::string before = R"(OpCapability Shader |
| 230 |
OpCapability Linkage |
| 231 |
OpMemoryModel Logical GLSL450 |
| 232 |
%void = OpTypeVoid |
| 233 |
%2 = OpTypeFunction %void |
| 234 |
%3 = OpFunction %void None %2 |
| 235 |
%4 = OpLabel |
| 236 |
OpNop |
| 237 |
OpReturn |
| 238 |
OpFunctionEnd |
| 239 |
)"; |
| 240 |
|
| 241 |
const std::string after = R"(OpCapability Shader |
| 242 |
OpCapability Linkage |
| 243 |
OpMemoryModel Logical GLSL450 |
| 244 |
%void = OpTypeVoid |
| 245 |
%2 = OpTypeFunction %void |
| 246 |
%3 = OpFunction %void None %2 |
| 247 |
%4 = OpLabel |
| 248 |
OpReturn |
| 249 |
OpFunctionEnd |
| 250 |
)"; |
| 251 |
|
| 252 |
std::vector<uint32_t> binary; |
| 253 |
{ |
| 254 |
SpirvTools tools(SPV_ENV_VULKAN_1_1); |
| 255 |
tools.Assemble(before, &binary); |
| 256 |
} |
| 257 |
|
| 258 |
Optimizer opt(SPV_ENV_VULKAN_1_1); |
| 259 |
|
| 260 |
std::vector<uint32_t> optimized; |
| 261 |
class ValidatorOptions validator_options; |
| 262 |
ASSERT_TRUE(opt.Run(binary.data(), binary.size(), &optimized, |
| 263 |
validator_options, true)) |
| 264 |
<< before << "\n"; |
| 265 |
std::string disassembly; |
| 266 |
{ |
| 267 |
SpirvTools tools(SPV_ENV_VULKAN_1_1); |
| 268 |
tools.Disassemble(optimized.data(), optimized.size(), &disassembly); |
| 269 |
} |
| 270 |
|
| 271 |
EXPECT_EQ(after, disassembly) |
| 272 |
<< "Was expecting the OpNop to have been removed."; |
| 273 |
} |
| 274 |
|
| 275 |
TEST(Optimizer, AvoidIntegrityCheckForExtraLineInfo) { |
| 276 |
// Test that it avoids the integrity check when no optimizations are run and |
| 277 |
// OpLines are propagated. |
| 278 |
const std::string before = R"(OpCapability Shader |
| 279 |
OpCapability Linkage |
| 280 |
OpMemoryModel Logical GLSL450 |
| 281 |
%1 = OpString "Test" |
| 282 |
%void = OpTypeVoid |
| 283 |
%3 = OpTypeFunction %void |
| 284 |
%uint = OpTypeInt 32 0 |
| 285 |
%_ptr_Function_uint = OpTypePointer Function %uint |
| 286 |
%6 = OpFunction %void None %3 |
| 287 |
%7 = OpLabel |
| 288 |
OpLine %1 10 0 |
| 289 |
%8 = OpVariable %_ptr_Function_uint Function |
| 290 |
OpLine %1 10 0 |
| 291 |
%9 = OpVariable %_ptr_Function_uint Function |
| 292 |
OpLine %1 20 0 |
| 293 |
OpReturn |
| 294 |
OpFunctionEnd |
| 295 |
)"; |
| 296 |
|
| 297 |
const std::string after = R"(OpCapability Shader |
| 298 |
OpCapability Linkage |
| 299 |
OpMemoryModel Logical GLSL450 |
| 300 |
%1 = OpString "Test" |
| 301 |
%void = OpTypeVoid |
| 302 |
%3 = OpTypeFunction %void |
| 303 |
%uint = OpTypeInt 32 0 |
| 304 |
%_ptr_Function_uint = OpTypePointer Function %uint |
| 305 |
%6 = OpFunction %void None %3 |
| 306 |
%7 = OpLabel |
| 307 |
OpLine %1 10 0 |
| 308 |
%8 = OpVariable %_ptr_Function_uint Function |
| 309 |
%9 = OpVariable %_ptr_Function_uint Function |
| 310 |
OpLine %1 20 0 |
| 311 |
OpReturn |
| 312 |
OpFunctionEnd |
| 313 |
)"; |
| 314 |
|
| 315 |
std::vector<uint32_t> binary; |
| 316 |
SpirvTools tools(SPV_ENV_VULKAN_1_1); |
| 317 |
tools.Assemble(before, &binary); |
| 318 |
|
| 319 |
Optimizer opt(SPV_ENV_VULKAN_1_1); |
| 320 |
|
| 321 |
std::vector<uint32_t> optimized; |
| 322 |
class ValidatorOptions validator_options; |
| 323 |
ASSERT_TRUE(opt.Run(binary.data(), binary.size(), &optimized, |
| 324 |
validator_options, true)) |
| 325 |
<< before << "\n"; |
| 326 |
|
| 327 |
std::string disassembly; |
| 328 |
tools.Disassemble(optimized.data(), optimized.size(), &disassembly); |
| 329 |
|
| 330 |
EXPECT_EQ(after, disassembly) |
| 331 |
<< "Was expecting the OpLine to have been propagated."; |
| 332 |
} |
| 333 |
|
| 334 |
TEST(Optimizer, AvoidIntegrityCheckForDebugScope) { |
| 335 |
// Test that it avoids the integrity check when the code contains DebugScope. |
| 336 |
const std::string before = R"(OpCapability Shader |
| 337 |
%1 = OpExtInstImport "OpenCL.DebugInfo.100" |
| 338 |
OpMemoryModel Logical GLSL450 |
| 339 |
OpEntryPoint Fragment %main "main" |
| 340 |
OpExecutionMode %main OriginUpperLeft |
| 341 |
%3 = OpString "simple_vs.hlsl" |
| 342 |
OpSource HLSL 600 %3 |
| 343 |
OpName %main "main" |
| 344 |
%void = OpTypeVoid |
| 345 |
%5 = OpTypeFunction %void |
| 346 |
%6 = OpExtInst %void %1 DebugSource %3 |
| 347 |
%7 = OpExtInst %void %1 DebugCompilationUnit 2 4 %6 HLSL |
| 348 |
%main = OpFunction %void None %5 |
| 349 |
%14 = OpLabel |
| 350 |
%26 = OpExtInst %void %1 DebugScope %7 |
| 351 |
OpReturn |
| 352 |
%27 = OpExtInst %void %1 DebugNoScope |
| 353 |
OpFunctionEnd |
| 354 |
)"; |
| 355 |
|
| 356 |
const std::string after = R"(OpCapability Shader |
| 357 |
%1 = OpExtInstImport "OpenCL.DebugInfo.100" |
| 358 |
OpMemoryModel Logical GLSL450 |
| 359 |
OpEntryPoint Fragment %main "main" |
| 360 |
OpExecutionMode %main OriginUpperLeft |
| 361 |
%3 = OpString "simple_vs.hlsl" |
| 362 |
OpSource HLSL 600 %3 |
| 363 |
OpName %main "main" |
| 364 |
%void = OpTypeVoid |
| 365 |
%5 = OpTypeFunction %void |
| 366 |
%6 = OpExtInst %void %1 DebugSource %3 |
| 367 |
%7 = OpExtInst %void %1 DebugCompilationUnit 2 4 %6 HLSL |
| 368 |
%main = OpFunction %void None %5 |
| 369 |
%8 = OpLabel |
| 370 |
%11 = OpExtInst %void %1 DebugScope %7 |
| 371 |
OpReturn |
| 372 |
%12 = OpExtInst %void %1 DebugNoScope |
| 373 |
OpFunctionEnd |
| 374 |
)"; |
| 375 |
|
| 376 |
std::vector<uint32_t> binary; |
| 377 |
SpirvTools tools(SPV_ENV_VULKAN_1_1); |
| 378 |
tools.Assemble(before, &binary); |
| 379 |
|
| 380 |
Optimizer opt(SPV_ENV_VULKAN_1_1); |
| 381 |
|
| 382 |
std::vector<uint32_t> optimized; |
| 383 |
ASSERT_TRUE(opt.Run(binary.data(), binary.size(), &optimized)) |
| 384 |
<< before << "\n"; |
| 385 |
|
| 386 |
std::string disassembly; |
| 387 |
tools.Disassemble(optimized.data(), optimized.size(), &disassembly); |
| 388 |
|
| 389 |
EXPECT_EQ(after, disassembly) |
| 390 |
<< "Was expecting the result id of DebugScope to have been changed."; |
| 391 |
} |
| 392 |
|
| 393 |
TEST(Optimizer, CheckDefaultPerformancePassesLargeStructScalarization) { |
| 394 |
std::string start = R"(OpCapability Shader |
| 395 |
%1 = OpExtInstImport "GLSL.std.450" |
| 396 |
OpMemoryModel Logical GLSL450 |
| 397 |
OpEntryPoint Vertex %4 "main" %46 %48 |
| 398 |
OpSource GLSL 430 |
| 399 |
OpName %4 "main" |
| 400 |
OpDecorate %44 Block |
| 401 |
OpMemberDecorate %44 0 BuiltIn Position |
| 402 |
OpMemberDecorate %44 1 BuiltIn PointSize |
| 403 |
OpMemberDecorate %44 2 BuiltIn ClipDistance |
| 404 |
OpDecorate %48 Location 0 |
| 405 |
%2 = OpTypeVoid |
| 406 |
%3 = OpTypeFunction %2 |
| 407 |
%6 = OpTypeFloat 32 |
| 408 |
%7 = OpTypeVector %6 4 |
| 409 |
%8 = OpTypePointer Function %7 |
| 410 |
%9 = OpTypeStruct %7)"; |
| 411 |
|
| 412 |
// add 200 float members to the struct |
| 413 |
for (int i = 0; i < 200; i++) { |
| 414 |
start += " %6"; |
| 415 |
} |
| 416 |
|
| 417 |
start += R"( |
| 418 |
%10 = OpTypeFunction %9 %8 |
| 419 |
%14 = OpTypeFunction %6 %9 |
| 420 |
%18 = OpTypePointer Function %9 |
| 421 |
%20 = OpTypeInt 32 1 |
| 422 |
%21 = OpConstant %20 0 |
| 423 |
%24 = OpConstant %20 1 |
| 424 |
%25 = OpTypeInt 32 0 |
| 425 |
%26 = OpConstant %25 1 |
| 426 |
%27 = OpTypePointer Function %6 |
| 427 |
%43 = OpTypeArray %6 %26 |
| 428 |
%44 = OpTypeStruct %7 %6 %43 |
| 429 |
%45 = OpTypePointer Output %44 |
| 430 |
%46 = OpVariable %45 Output |
| 431 |
%47 = OpTypePointer Input %7 |
| 432 |
%48 = OpVariable %47 Input |
| 433 |
%54 = OpTypePointer Output %7 |
| 434 |
%4 = OpFunction %2 None %3 |
| 435 |
%5 = OpLabel |
| 436 |
%49 = OpVariable %8 Function |
| 437 |
%50 = OpLoad %7 %48 |
| 438 |
OpStore %49 %50 |
| 439 |
%51 = OpFunctionCall %9 %12 %49 |
| 440 |
%52 = OpFunctionCall %6 %16 %51 |
| 441 |
%53 = OpCompositeConstruct %7 %52 %52 %52 %52 |
| 442 |
%55 = OpAccessChain %54 %46 %21 |
| 443 |
OpStore %55 %53 |
| 444 |
OpReturn |
| 445 |
OpFunctionEnd |
| 446 |
%12 = OpFunction %9 None %10 |
| 447 |
%11 = OpFunctionParameter %8 |
| 448 |
%13 = OpLabel |
| 449 |
%19 = OpVariable %18 Function |
| 450 |
%22 = OpLoad %7 %11 |
| 451 |
%23 = OpAccessChain %8 %19 %21 |
| 452 |
OpStore %23 %22 |
| 453 |
%28 = OpAccessChain %27 %11 %26 |
| 454 |
%29 = OpLoad %6 %28 |
| 455 |
%30 = OpConvertFToS %20 %29 |
| 456 |
%31 = OpAccessChain %27 %19 %21 %30 |
| 457 |
%32 = OpLoad %6 %31 |
| 458 |
%33 = OpAccessChain %27 %19 %24 |
| 459 |
OpStore %33 %32 |
| 460 |
%34 = OpLoad %9 %19 |
| 461 |
OpReturnValue %34 |
| 462 |
OpFunctionEnd |
| 463 |
%16 = OpFunction %6 None %14 |
| 464 |
%15 = OpFunctionParameter %9 |
| 465 |
%17 = OpLabel |
| 466 |
%37 = OpCompositeExtract %6 %15 1 |
| 467 |
%38 = OpConvertFToS %20 %37 |
| 468 |
%39 = OpCompositeExtract %7 %15 0 |
| 469 |
%40 = OpVectorExtractDynamic %6 %39 %38 |
| 470 |
OpReturnValue %40 |
| 471 |
OpFunctionEnd)"; |
| 472 |
|
| 473 |
std::vector<uint32_t> binary; |
| 474 |
SpirvTools tools(SPV_ENV_VULKAN_1_3); |
| 475 |
tools.Assemble(start, &binary, |
| 476 |
SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS); |
| 477 |
|
| 478 |
std::string test_disassembly; |
| 479 |
std::string default_disassembly; |
| 480 |
|
| 481 |
{ |
| 482 |
Optimizer opt(SPV_ENV_VULKAN_1_3); |
| 483 |
opt.RegisterPerformancePasses(); |
| 484 |
|
| 485 |
std::vector<uint32_t> optimized; |
| 486 |
ASSERT_TRUE(opt.Run(binary.data(), binary.size(), &optimized)) |
| 487 |
<< start << "\n"; |
| 488 |
|
| 489 |
tools.Disassemble(optimized.data(), optimized.size(), &default_disassembly, |
| 490 |
SPV_BINARY_TO_TEXT_OPTION_NO_HEADER); |
| 491 |
} |
| 492 |
|
| 493 |
{ |
| 494 |
// default passes should not benefit from additional scalar replacement |
| 495 |
Optimizer opt(SPV_ENV_VULKAN_1_3); |
| 496 |
opt.RegisterPerformancePasses() |
| 497 |
.RegisterPass(CreateScalarReplacementPass(201)) |
| 498 |
.RegisterPass(CreateAggressiveDCEPass()); |
| 499 |
|
| 500 |
std::vector<uint32_t> optimized; |
| 501 |
ASSERT_TRUE(opt.Run(binary.data(), binary.size(), &optimized)) |
| 502 |
<< start << "\n"; |
| 503 |
|
| 504 |
tools.Disassemble(optimized.data(), optimized.size(), &test_disassembly, |
| 505 |
SPV_BINARY_TO_TEXT_OPTION_NO_HEADER); |
| 506 |
} |
| 507 |
|
| 508 |
EXPECT_EQ(test_disassembly, default_disassembly); |
| 509 |
} |
| 510 |
|
| 511 |
TEST(Optimizer, KeepDebugBuildIdentifierAfterDCE) { |
| 512 |
// Test that DebugBuildIdentifier is not removed after DCE. |
| 513 |
const std::string before = R"( |
| 514 |
OpCapability Shader |
| 515 |
OpExtension "SPV_KHR_non_semantic_info" |
| 516 |
%1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100" |
| 517 |
OpMemoryModel Logical GLSL450 |
| 518 |
OpEntryPoint GLCompute %main "main" |
| 519 |
OpExecutionMode %main LocalSize 8 8 1 |
| 520 |
%4 = OpString "8937d8f571cf7b58d86d9d66196024f5d04e3186" |
| 521 |
%7 = OpString "" |
| 522 |
%9 = OpString "" |
| 523 |
OpSource Slang 1 |
| 524 |
%19 = OpString "" |
| 525 |
%24 = OpString "" |
| 526 |
%25 = OpString "" |
| 527 |
OpName %main "main" |
| 528 |
%void = OpTypeVoid |
| 529 |
%uint = OpTypeInt 32 0 |
| 530 |
%uint_0 = OpConstant %uint 0 |
| 531 |
%uint_11 = OpConstant %uint 11 |
| 532 |
%uint_5 = OpConstant %uint 5 |
| 533 |
%uint_100 = OpConstant %uint 100 |
| 534 |
%15 = OpTypeFunction %void |
| 535 |
%uint_6 = OpConstant %uint 6 |
| 536 |
%uint_7 = OpConstant %uint 7 |
| 537 |
%uint_1 = OpConstant %uint 1 |
| 538 |
%uint_2 = OpConstant %uint 2 |
| 539 |
%3 = OpExtInst %void %1 DebugBuildIdentifier %4 %uint_0 |
| 540 |
%8 = OpExtInst %void %1 DebugSource %9 %7 |
| 541 |
%13 = OpExtInst %void %1 DebugCompilationUnit %uint_100 %uint_5 %8 %uint_11 |
| 542 |
%17 = OpExtInst %void %1 DebugTypeFunction %uint_0 %void |
| 543 |
%18 = OpExtInst %void %1 DebugFunction %19 %17 %8 %uint_5 %uint_6 %13 %19 %uint_0 %uint_5 |
| 544 |
%23 = OpExtInst %void %1 DebugEntryPoint %18 %13 %24 %25 |
| 545 |
%main = OpFunction %void None %15 |
| 546 |
%16 = OpLabel |
| 547 |
%21 = OpExtInst %void %1 DebugFunctionDefinition %18 %main |
| 548 |
%32 = OpExtInst %void %1 DebugScope %18 |
| 549 |
%26 = OpExtInst %void %1 DebugLine %8 %uint_7 %uint_7 %uint_1 %uint_2 |
| 550 |
OpReturn |
| 551 |
%33 = OpExtInst %void %1 DebugNoScope |
| 552 |
OpFunctionEnd |
| 553 |
)"; |
| 554 |
|
| 555 |
std::vector<uint32_t> binary; |
| 556 |
SpirvTools tools(SPV_ENV_VULKAN_1_3); |
| 557 |
tools.Assemble(before, &binary, |
| 558 |
SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS); |
| 559 |
|
| 560 |
Optimizer opt(SPV_ENV_VULKAN_1_3); |
| 561 |
opt.RegisterPerformancePasses().RegisterPass(CreateAggressiveDCEPass()); |
| 562 |
|
| 563 |
std::vector<uint32_t> optimized; |
| 564 |
ASSERT_TRUE(opt.Run(binary.data(), binary.size(), &optimized)) |
| 565 |
<< before << "\n"; |
| 566 |
|
| 567 |
std::string after; |
| 568 |
tools.Disassemble(optimized.data(), optimized.size(), &after, |
| 569 |
SPV_BINARY_TO_TEXT_OPTION_NO_HEADER); |
| 570 |
|
| 571 |
// Test that the DebugBuildIdentifier is not removed after DCE. |
| 572 |
size_t dbi_pos = after.find("DebugBuildIdentifier"); |
| 573 |
EXPECT_NE(dbi_pos, std::string::npos) |
| 574 |
<< "Was expecting the DebugBuildIdentifier to have been kept."; |
| 575 |
std::string string_id; |
| 576 |
std::string flags_id; |
| 577 |
if (dbi_pos != std::string::npos) { |
| 578 |
std::stringstream ss(after.substr(dbi_pos)); |
| 579 |
std::string temp; |
| 580 |
char percent; |
| 581 |
ss >> temp; // Consume "DebugBuildIdentifier" |
| 582 |
ss >> percent >> string_id; |
| 583 |
ss >> percent >> flags_id; |
| 584 |
} |
| 585 |
|
| 586 |
EXPECT_FALSE(string_id.empty()) |
| 587 |
<< "Could not find string id for DebugBuildIdentifier."; |
| 588 |
EXPECT_FALSE(flags_id.empty()) |
| 589 |
<< "Could not find flags id for DebugBuildIdentifier."; |
| 590 |
|
| 591 |
bool found = |
| 592 |
(after.find("%" + string_id + " = OpString") != std::string::npos); |
| 593 |
EXPECT_TRUE(found) |
| 594 |
<< "Was expecting the DebugBuildIdentifier string to have been kept."; |
| 595 |
found = (after.find("%" + flags_id + " = OpConstant") != std::string::npos); |
| 596 |
EXPECT_TRUE(found) |
| 597 |
<< "Was expecting the DebugBuildIdentifier constant to have been kept."; |
| 598 |
} |
| 599 |
|
| 600 |
} // namespace |
| 601 |
} // namespace opt |
| 602 |
} // namespace spvtools |