| 1 |
// Copyright (c) 2017 Valve Corporation |
| 2 |
// Copyright (c) 2017 LunarG Inc. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
|
| 16 |
#include <string> |
| 17 |
|
| 18 |
#include "test/opt/pass_fixture.h" |
| 19 |
#include "test/opt/pass_utils.h" |
| 20 |
|
| 21 |
namespace spvtools { |
| 22 |
namespace opt { |
| 23 |
namespace { |
| 24 |
|
| 25 |
using LocalSingleStoreElimTest = PassTest<::testing::Test>; |
| 26 |
|
| 27 |
TEST_F(LocalSingleStoreElimTest, PositiveAndNegative) { |
| 28 |
// Single store to v is optimized. Multiple store to |
| 29 |
// f is not optimized. |
| 30 |
// |
| 31 |
// #version 140 |
| 32 |
// |
| 33 |
// in vec4 BaseColor; |
| 34 |
// in float fi; |
| 35 |
// |
| 36 |
// void main() |
| 37 |
// { |
| 38 |
// vec4 v = BaseColor; |
| 39 |
// float f = fi; |
| 40 |
// if (f < 0) |
| 41 |
// f = 0.0; |
| 42 |
// gl_FragColor = v + f; |
| 43 |
// } |
| 44 |
|
| 45 |
const std::string predefs = |
| 46 |
R"(OpCapability Shader |
| 47 |
%1 = OpExtInstImport "GLSL.std.450" |
| 48 |
OpMemoryModel Logical GLSL450 |
| 49 |
OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor |
| 50 |
OpExecutionMode %main OriginUpperLeft |
| 51 |
OpSource GLSL 140 |
| 52 |
OpName %main "main" |
| 53 |
OpName %v "v" |
| 54 |
OpName %BaseColor "BaseColor" |
| 55 |
OpName %f "f" |
| 56 |
OpName %fi "fi" |
| 57 |
OpName %gl_FragColor "gl_FragColor" |
| 58 |
%void = OpTypeVoid |
| 59 |
%9 = OpTypeFunction %void |
| 60 |
%float = OpTypeFloat 32 |
| 61 |
%v4float = OpTypeVector %float 4 |
| 62 |
%_ptr_Function_v4float = OpTypePointer Function %v4float |
| 63 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 64 |
%BaseColor = OpVariable %_ptr_Input_v4float Input |
| 65 |
%_ptr_Function_float = OpTypePointer Function %float |
| 66 |
%_ptr_Input_float = OpTypePointer Input %float |
| 67 |
%fi = OpVariable %_ptr_Input_float Input |
| 68 |
%float_0 = OpConstant %float 0 |
| 69 |
%bool = OpTypeBool |
| 70 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 71 |
%gl_FragColor = OpVariable %_ptr_Output_v4float Output |
| 72 |
)"; |
| 73 |
|
| 74 |
const std::string before = |
| 75 |
R"(%main = OpFunction %void None %9 |
| 76 |
%19 = OpLabel |
| 77 |
%v = OpVariable %_ptr_Function_v4float Function |
| 78 |
%f = OpVariable %_ptr_Function_float Function |
| 79 |
%20 = OpLoad %v4float %BaseColor |
| 80 |
OpStore %v %20 |
| 81 |
%21 = OpLoad %float %fi |
| 82 |
OpStore %f %21 |
| 83 |
%22 = OpLoad %float %f |
| 84 |
%23 = OpFOrdLessThan %bool %22 %float_0 |
| 85 |
OpSelectionMerge %24 None |
| 86 |
OpBranchConditional %23 %25 %24 |
| 87 |
%25 = OpLabel |
| 88 |
OpStore %f %float_0 |
| 89 |
OpBranch %24 |
| 90 |
%24 = OpLabel |
| 91 |
%26 = OpLoad %v4float %v |
| 92 |
%27 = OpLoad %float %f |
| 93 |
%28 = OpCompositeConstruct %v4float %27 %27 %27 %27 |
| 94 |
%29 = OpFAdd %v4float %26 %28 |
| 95 |
OpStore %gl_FragColor %29 |
| 96 |
OpReturn |
| 97 |
OpFunctionEnd |
| 98 |
)"; |
| 99 |
|
| 100 |
const std::string after = |
| 101 |
R"(%main = OpFunction %void None %9 |
| 102 |
%19 = OpLabel |
| 103 |
%v = OpVariable %_ptr_Function_v4float Function |
| 104 |
%f = OpVariable %_ptr_Function_float Function |
| 105 |
%20 = OpLoad %v4float %BaseColor |
| 106 |
OpStore %v %20 |
| 107 |
%21 = OpLoad %float %fi |
| 108 |
OpStore %f %21 |
| 109 |
%22 = OpLoad %float %f |
| 110 |
%23 = OpFOrdLessThan %bool %22 %float_0 |
| 111 |
OpSelectionMerge %24 None |
| 112 |
OpBranchConditional %23 %25 %24 |
| 113 |
%25 = OpLabel |
| 114 |
OpStore %f %float_0 |
| 115 |
OpBranch %24 |
| 116 |
%24 = OpLabel |
| 117 |
%27 = OpLoad %float %f |
| 118 |
%28 = OpCompositeConstruct %v4float %27 %27 %27 %27 |
| 119 |
%29 = OpFAdd %v4float %20 %28 |
| 120 |
OpStore %gl_FragColor %29 |
| 121 |
OpReturn |
| 122 |
OpFunctionEnd |
| 123 |
)"; |
| 124 |
|
| 125 |
SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before, |
| 126 |
predefs + after, true, true); |
| 127 |
} |
| 128 |
|
| 129 |
TEST_F(LocalSingleStoreElimTest, ThreeStores) { |
| 130 |
// Three stores to multiple loads of v is not optimized. |
| 131 |
|
| 132 |
const std::string predefs = |
| 133 |
R"(OpCapability Shader |
| 134 |
%1 = OpExtInstImport "GLSL.std.450" |
| 135 |
OpMemoryModel Logical GLSL450 |
| 136 |
OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor |
| 137 |
OpExecutionMode %main OriginUpperLeft |
| 138 |
OpSource GLSL 140 |
| 139 |
OpName %main "main" |
| 140 |
OpName %v "v" |
| 141 |
OpName %BaseColor "BaseColor" |
| 142 |
OpName %fi "fi" |
| 143 |
OpName %r "r" |
| 144 |
OpName %gl_FragColor "gl_FragColor" |
| 145 |
%void = OpTypeVoid |
| 146 |
%9 = OpTypeFunction %void |
| 147 |
%float = OpTypeFloat 32 |
| 148 |
%v4float = OpTypeVector %float 4 |
| 149 |
%_ptr_Function_v4float = OpTypePointer Function %v4float |
| 150 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 151 |
%BaseColor = OpVariable %_ptr_Input_v4float Input |
| 152 |
%_ptr_Input_float = OpTypePointer Input %float |
| 153 |
%fi = OpVariable %_ptr_Input_float Input |
| 154 |
%float_0 = OpConstant %float 0 |
| 155 |
%bool = OpTypeBool |
| 156 |
%float_1 = OpConstant %float 1 |
| 157 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 158 |
%gl_FragColor = OpVariable %_ptr_Output_v4float Output |
| 159 |
)"; |
| 160 |
|
| 161 |
const std::string before = |
| 162 |
R"(%main = OpFunction %void None %9 |
| 163 |
%19 = OpLabel |
| 164 |
%v = OpVariable %_ptr_Function_v4float Function |
| 165 |
%r = OpVariable %_ptr_Function_v4float Function |
| 166 |
%20 = OpLoad %v4float %BaseColor |
| 167 |
OpStore %v %20 |
| 168 |
%21 = OpLoad %float %fi |
| 169 |
%22 = OpFOrdLessThan %bool %21 %float_0 |
| 170 |
OpSelectionMerge %23 None |
| 171 |
OpBranchConditional %22 %24 %25 |
| 172 |
%24 = OpLabel |
| 173 |
%26 = OpLoad %v4float %v |
| 174 |
OpStore %v %26 |
| 175 |
OpStore %r %26 |
| 176 |
OpBranch %23 |
| 177 |
%25 = OpLabel |
| 178 |
%27 = OpLoad %v4float %v |
| 179 |
%28 = OpCompositeConstruct %v4float %float_1 %float_1 %float_1 %float_1 |
| 180 |
OpStore %v %28 |
| 181 |
%29 = OpFSub %v4float %28 %27 |
| 182 |
OpStore %r %29 |
| 183 |
OpBranch %23 |
| 184 |
%23 = OpLabel |
| 185 |
%30 = OpLoad %v4float %r |
| 186 |
OpStore %gl_FragColor %30 |
| 187 |
OpReturn |
| 188 |
OpFunctionEnd |
| 189 |
)"; |
| 190 |
|
| 191 |
SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before, |
| 192 |
predefs + before, true, true); |
| 193 |
} |
| 194 |
|
| 195 |
TEST_F(LocalSingleStoreElimTest, MultipleLoads) { |
| 196 |
// Single store to multiple loads of v is optimized. |
| 197 |
// |
| 198 |
// #version 140 |
| 199 |
// |
| 200 |
// in vec4 BaseColor; |
| 201 |
// in float fi; |
| 202 |
// |
| 203 |
// void main() |
| 204 |
// { |
| 205 |
// vec4 v = BaseColor; |
| 206 |
// float f = fi; |
| 207 |
// if (f < 0) |
| 208 |
// f = 0.0; |
| 209 |
// gl_FragColor = v + f; |
| 210 |
// } |
| 211 |
|
| 212 |
const std::string predefs = |
| 213 |
R"(OpCapability Shader |
| 214 |
%1 = OpExtInstImport "GLSL.std.450" |
| 215 |
OpMemoryModel Logical GLSL450 |
| 216 |
OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor |
| 217 |
OpExecutionMode %main OriginUpperLeft |
| 218 |
OpSource GLSL 140 |
| 219 |
OpName %main "main" |
| 220 |
OpName %v "v" |
| 221 |
OpName %BaseColor "BaseColor" |
| 222 |
OpName %fi "fi" |
| 223 |
OpName %r "r" |
| 224 |
OpName %gl_FragColor "gl_FragColor" |
| 225 |
%void = OpTypeVoid |
| 226 |
%9 = OpTypeFunction %void |
| 227 |
%float = OpTypeFloat 32 |
| 228 |
%v4float = OpTypeVector %float 4 |
| 229 |
%_ptr_Function_v4float = OpTypePointer Function %v4float |
| 230 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 231 |
%BaseColor = OpVariable %_ptr_Input_v4float Input |
| 232 |
%_ptr_Input_float = OpTypePointer Input %float |
| 233 |
%fi = OpVariable %_ptr_Input_float Input |
| 234 |
%float_0 = OpConstant %float 0 |
| 235 |
%bool = OpTypeBool |
| 236 |
%float_1 = OpConstant %float 1 |
| 237 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 238 |
%gl_FragColor = OpVariable %_ptr_Output_v4float Output |
| 239 |
)"; |
| 240 |
|
| 241 |
const std::string before = |
| 242 |
R"(%main = OpFunction %void None %9 |
| 243 |
%19 = OpLabel |
| 244 |
%v = OpVariable %_ptr_Function_v4float Function |
| 245 |
%r = OpVariable %_ptr_Function_v4float Function |
| 246 |
%20 = OpLoad %v4float %BaseColor |
| 247 |
OpStore %v %20 |
| 248 |
%21 = OpLoad %float %fi |
| 249 |
%22 = OpFOrdLessThan %bool %21 %float_0 |
| 250 |
OpSelectionMerge %23 None |
| 251 |
OpBranchConditional %22 %24 %25 |
| 252 |
%24 = OpLabel |
| 253 |
%26 = OpLoad %v4float %v |
| 254 |
OpStore %r %26 |
| 255 |
OpBranch %23 |
| 256 |
%25 = OpLabel |
| 257 |
%27 = OpLoad %v4float %v |
| 258 |
%28 = OpCompositeConstruct %v4float %float_1 %float_1 %float_1 %float_1 |
| 259 |
%29 = OpFSub %v4float %28 %27 |
| 260 |
OpStore %r %29 |
| 261 |
OpBranch %23 |
| 262 |
%23 = OpLabel |
| 263 |
%30 = OpLoad %v4float %r |
| 264 |
OpStore %gl_FragColor %30 |
| 265 |
OpReturn |
| 266 |
OpFunctionEnd |
| 267 |
)"; |
| 268 |
|
| 269 |
const std::string after = |
| 270 |
R"(%main = OpFunction %void None %9 |
| 271 |
%19 = OpLabel |
| 272 |
%v = OpVariable %_ptr_Function_v4float Function |
| 273 |
%r = OpVariable %_ptr_Function_v4float Function |
| 274 |
%20 = OpLoad %v4float %BaseColor |
| 275 |
OpStore %v %20 |
| 276 |
%21 = OpLoad %float %fi |
| 277 |
%22 = OpFOrdLessThan %bool %21 %float_0 |
| 278 |
OpSelectionMerge %23 None |
| 279 |
OpBranchConditional %22 %24 %25 |
| 280 |
%24 = OpLabel |
| 281 |
OpStore %r %20 |
| 282 |
OpBranch %23 |
| 283 |
%25 = OpLabel |
| 284 |
%28 = OpCompositeConstruct %v4float %float_1 %float_1 %float_1 %float_1 |
| 285 |
%29 = OpFSub %v4float %28 %20 |
| 286 |
OpStore %r %29 |
| 287 |
OpBranch %23 |
| 288 |
%23 = OpLabel |
| 289 |
%30 = OpLoad %v4float %r |
| 290 |
OpStore %gl_FragColor %30 |
| 291 |
OpReturn |
| 292 |
OpFunctionEnd |
| 293 |
)"; |
| 294 |
|
| 295 |
SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before, |
| 296 |
predefs + after, true, true); |
| 297 |
} |
| 298 |
|
| 299 |
TEST_F(LocalSingleStoreElimTest, NoStoreElimWithInterveningAccessChainLoad) { |
| 300 |
// Last load of v is eliminated, but access chain load and store of v isn't |
| 301 |
// |
| 302 |
// #version 140 |
| 303 |
// |
| 304 |
// in vec4 BaseColor; |
| 305 |
// |
| 306 |
// void main() |
| 307 |
// { |
| 308 |
// vec4 v = BaseColor; |
| 309 |
// float f = v[3]; |
| 310 |
// gl_FragColor = v * f; |
| 311 |
// } |
| 312 |
|
| 313 |
const std::string predefs = |
| 314 |
R"(OpCapability Shader |
| 315 |
%1 = OpExtInstImport "GLSL.std.450" |
| 316 |
OpMemoryModel Logical GLSL450 |
| 317 |
OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor |
| 318 |
OpExecutionMode %main OriginUpperLeft |
| 319 |
OpSource GLSL 140 |
| 320 |
OpName %main "main" |
| 321 |
OpName %v "v" |
| 322 |
OpName %BaseColor "BaseColor" |
| 323 |
OpName %f "f" |
| 324 |
OpName %gl_FragColor "gl_FragColor" |
| 325 |
%void = OpTypeVoid |
| 326 |
%8 = OpTypeFunction %void |
| 327 |
%float = OpTypeFloat 32 |
| 328 |
%v4float = OpTypeVector %float 4 |
| 329 |
%_ptr_Function_v4float = OpTypePointer Function %v4float |
| 330 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 331 |
%BaseColor = OpVariable %_ptr_Input_v4float Input |
| 332 |
%_ptr_Function_float = OpTypePointer Function %float |
| 333 |
%uint = OpTypeInt 32 0 |
| 334 |
%uint_3 = OpConstant %uint 3 |
| 335 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 336 |
%gl_FragColor = OpVariable %_ptr_Output_v4float Output |
| 337 |
)"; |
| 338 |
|
| 339 |
const std::string before = |
| 340 |
R"(%main = OpFunction %void None %8 |
| 341 |
%17 = OpLabel |
| 342 |
%v = OpVariable %_ptr_Function_v4float Function |
| 343 |
%f = OpVariable %_ptr_Function_float Function |
| 344 |
%18 = OpLoad %v4float %BaseColor |
| 345 |
OpStore %v %18 |
| 346 |
%19 = OpAccessChain %_ptr_Function_float %v %uint_3 |
| 347 |
%20 = OpLoad %float %19 |
| 348 |
OpStore %f %20 |
| 349 |
%21 = OpLoad %v4float %v |
| 350 |
%22 = OpLoad %float %f |
| 351 |
%23 = OpVectorTimesScalar %v4float %21 %22 |
| 352 |
OpStore %gl_FragColor %23 |
| 353 |
OpReturn |
| 354 |
OpFunctionEnd |
| 355 |
)"; |
| 356 |
|
| 357 |
const std::string after = |
| 358 |
R"(%main = OpFunction %void None %8 |
| 359 |
%17 = OpLabel |
| 360 |
%v = OpVariable %_ptr_Function_v4float Function |
| 361 |
%f = OpVariable %_ptr_Function_float Function |
| 362 |
%18 = OpLoad %v4float %BaseColor |
| 363 |
OpStore %v %18 |
| 364 |
%19 = OpAccessChain %_ptr_Function_float %v %uint_3 |
| 365 |
%20 = OpLoad %float %19 |
| 366 |
OpStore %f %20 |
| 367 |
%23 = OpVectorTimesScalar %v4float %18 %20 |
| 368 |
OpStore %gl_FragColor %23 |
| 369 |
OpReturn |
| 370 |
OpFunctionEnd |
| 371 |
)"; |
| 372 |
|
| 373 |
SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before, |
| 374 |
predefs + after, true, true); |
| 375 |
} |
| 376 |
|
| 377 |
TEST_F(LocalSingleStoreElimTest, NoReplaceOfDominatingPartialStore) { |
| 378 |
// Note: SPIR-V hand edited to initialize v to vec4(0.0) |
| 379 |
// |
| 380 |
// #version 140 |
| 381 |
// |
| 382 |
// in vec4 BaseColor; |
| 383 |
// |
| 384 |
// void main() |
| 385 |
// { |
| 386 |
// vec4 v; |
| 387 |
// float v[1] = 1.0; |
| 388 |
// gl_FragColor = v; |
| 389 |
// } |
| 390 |
|
| 391 |
const std::string assembly = |
| 392 |
R"(OpCapability Shader |
| 393 |
%1 = OpExtInstImport "GLSL.std.450" |
| 394 |
OpMemoryModel Logical GLSL450 |
| 395 |
OpEntryPoint Fragment %main "main" %gl_FragColor %BaseColor |
| 396 |
OpExecutionMode %main OriginUpperLeft |
| 397 |
OpSource GLSL 140 |
| 398 |
OpName %main "main" |
| 399 |
OpName %v "v" |
| 400 |
OpName %gl_FragColor "gl_FragColor" |
| 401 |
OpName %BaseColor "BaseColor" |
| 402 |
%void = OpTypeVoid |
| 403 |
%7 = OpTypeFunction %void |
| 404 |
%float = OpTypeFloat 32 |
| 405 |
%v4float = OpTypeVector %float 4 |
| 406 |
%_ptr_Function_v4float = OpTypePointer Function %v4float |
| 407 |
%float_0 = OpConstant %float 0 |
| 408 |
%12 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0 |
| 409 |
%float_1 = OpConstant %float 1 |
| 410 |
%uint = OpTypeInt 32 0 |
| 411 |
%uint_1 = OpConstant %uint 1 |
| 412 |
%_ptr_Function_float = OpTypePointer Function %float |
| 413 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 414 |
%gl_FragColor = OpVariable %_ptr_Output_v4float Output |
| 415 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 416 |
%BaseColor = OpVariable %_ptr_Input_v4float Input |
| 417 |
%main = OpFunction %void None %7 |
| 418 |
%19 = OpLabel |
| 419 |
%v = OpVariable %_ptr_Function_v4float Function %12 |
| 420 |
%20 = OpAccessChain %_ptr_Function_float %v %uint_1 |
| 421 |
OpStore %20 %float_1 |
| 422 |
%21 = OpLoad %v4float %v |
| 423 |
OpStore %gl_FragColor %21 |
| 424 |
OpReturn |
| 425 |
OpFunctionEnd |
| 426 |
)"; |
| 427 |
|
| 428 |
SinglePassRunAndCheck<LocalSingleStoreElimPass>(assembly, assembly, true, |
| 429 |
true); |
| 430 |
} |
| 431 |
|
| 432 |
TEST_F(LocalSingleStoreElimTest, ElimIfCopyObjectInFunction) { |
| 433 |
// Note: hand edited to insert OpCopyObject |
| 434 |
// |
| 435 |
// #version 140 |
| 436 |
// |
| 437 |
// in vec4 BaseColor; |
| 438 |
// in float fi; |
| 439 |
// |
| 440 |
// void main() |
| 441 |
// { |
| 442 |
// vec4 v = BaseColor; |
| 443 |
// float f = fi; |
| 444 |
// if (f < 0) |
| 445 |
// f = 0.0; |
| 446 |
// gl_FragColor = v + f; |
| 447 |
// } |
| 448 |
|
| 449 |
const std::string predefs = |
| 450 |
R"(OpCapability Shader |
| 451 |
%1 = OpExtInstImport "GLSL.std.450" |
| 452 |
OpMemoryModel Logical GLSL450 |
| 453 |
OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor |
| 454 |
OpExecutionMode %main OriginUpperLeft |
| 455 |
OpSource GLSL 140 |
| 456 |
OpName %main "main" |
| 457 |
OpName %v "v" |
| 458 |
OpName %BaseColor "BaseColor" |
| 459 |
OpName %f "f" |
| 460 |
OpName %fi "fi" |
| 461 |
OpName %gl_FragColor "gl_FragColor" |
| 462 |
%void = OpTypeVoid |
| 463 |
%9 = OpTypeFunction %void |
| 464 |
%float = OpTypeFloat 32 |
| 465 |
%v4float = OpTypeVector %float 4 |
| 466 |
%_ptr_Function_v4float = OpTypePointer Function %v4float |
| 467 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 468 |
%BaseColor = OpVariable %_ptr_Input_v4float Input |
| 469 |
%_ptr_Function_float = OpTypePointer Function %float |
| 470 |
%_ptr_Input_float = OpTypePointer Input %float |
| 471 |
%fi = OpVariable %_ptr_Input_float Input |
| 472 |
%float_0 = OpConstant %float 0 |
| 473 |
%bool = OpTypeBool |
| 474 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 475 |
%gl_FragColor = OpVariable %_ptr_Output_v4float Output |
| 476 |
)"; |
| 477 |
|
| 478 |
const std::string before = |
| 479 |
R"(%main = OpFunction %void None %9 |
| 480 |
%19 = OpLabel |
| 481 |
%v = OpVariable %_ptr_Function_v4float Function |
| 482 |
%f = OpVariable %_ptr_Function_float Function |
| 483 |
%20 = OpLoad %v4float %BaseColor |
| 484 |
OpStore %v %20 |
| 485 |
%21 = OpLoad %float %fi |
| 486 |
OpStore %f %21 |
| 487 |
%22 = OpLoad %float %f |
| 488 |
%23 = OpFOrdLessThan %bool %22 %float_0 |
| 489 |
OpSelectionMerge %24 None |
| 490 |
OpBranchConditional %23 %25 %24 |
| 491 |
%25 = OpLabel |
| 492 |
OpStore %f %float_0 |
| 493 |
OpBranch %24 |
| 494 |
%24 = OpLabel |
| 495 |
%26 = OpCopyObject %_ptr_Function_v4float %v |
| 496 |
%27 = OpLoad %v4float %26 |
| 497 |
%28 = OpLoad %float %f |
| 498 |
%29 = OpCompositeConstruct %v4float %28 %28 %28 %28 |
| 499 |
%30 = OpFAdd %v4float %27 %29 |
| 500 |
OpStore %gl_FragColor %30 |
| 501 |
OpReturn |
| 502 |
OpFunctionEnd |
| 503 |
)"; |
| 504 |
|
| 505 |
const std::string after = |
| 506 |
R"(%main = OpFunction %void None %9 |
| 507 |
%19 = OpLabel |
| 508 |
%v = OpVariable %_ptr_Function_v4float Function |
| 509 |
%f = OpVariable %_ptr_Function_float Function |
| 510 |
%20 = OpLoad %v4float %BaseColor |
| 511 |
OpStore %v %20 |
| 512 |
%21 = OpLoad %float %fi |
| 513 |
OpStore %f %21 |
| 514 |
%22 = OpLoad %float %f |
| 515 |
%23 = OpFOrdLessThan %bool %22 %float_0 |
| 516 |
OpSelectionMerge %24 None |
| 517 |
OpBranchConditional %23 %25 %24 |
| 518 |
%25 = OpLabel |
| 519 |
OpStore %f %float_0 |
| 520 |
OpBranch %24 |
| 521 |
%24 = OpLabel |
| 522 |
%26 = OpCopyObject %_ptr_Function_v4float %v |
| 523 |
%28 = OpLoad %float %f |
| 524 |
%29 = OpCompositeConstruct %v4float %28 %28 %28 %28 |
| 525 |
%30 = OpFAdd %v4float %20 %29 |
| 526 |
OpStore %gl_FragColor %30 |
| 527 |
OpReturn |
| 528 |
OpFunctionEnd |
| 529 |
)"; |
| 530 |
|
| 531 |
SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before, |
| 532 |
predefs + after, true, true); |
| 533 |
} |
| 534 |
|
| 535 |
TEST_F(LocalSingleStoreElimTest, NoOptIfStoreNotDominating) { |
| 536 |
// Single store to f not optimized because it does not dominate |
| 537 |
// the load. |
| 538 |
// |
| 539 |
// #version 140 |
| 540 |
// |
| 541 |
// in vec4 BaseColor; |
| 542 |
// in float fi; |
| 543 |
// |
| 544 |
// void main() |
| 545 |
// { |
| 546 |
// float f; |
| 547 |
// if (fi < 0) |
| 548 |
// f = 0.5; |
| 549 |
// if (fi < 0) |
| 550 |
// gl_FragColor = BaseColor * f; |
| 551 |
// else |
| 552 |
// gl_FragColor = BaseColor; |
| 553 |
// } |
| 554 |
|
| 555 |
const std::string assembly = |
| 556 |
R"(OpCapability Shader |
| 557 |
%1 = OpExtInstImport "GLSL.std.450" |
| 558 |
OpMemoryModel Logical GLSL450 |
| 559 |
OpEntryPoint Fragment %main "main" %fi %gl_FragColor %BaseColor |
| 560 |
OpExecutionMode %main OriginUpperLeft |
| 561 |
OpSource GLSL 140 |
| 562 |
OpName %main "main" |
| 563 |
OpName %fi "fi" |
| 564 |
OpName %f "f" |
| 565 |
OpName %gl_FragColor "gl_FragColor" |
| 566 |
OpName %BaseColor "BaseColor" |
| 567 |
%void = OpTypeVoid |
| 568 |
%8 = OpTypeFunction %void |
| 569 |
%float = OpTypeFloat 32 |
| 570 |
%_ptr_Input_float = OpTypePointer Input %float |
| 571 |
%fi = OpVariable %_ptr_Input_float Input |
| 572 |
%float_0 = OpConstant %float 0 |
| 573 |
%bool = OpTypeBool |
| 574 |
%_ptr_Function_float = OpTypePointer Function %float |
| 575 |
%float_0_5 = OpConstant %float 0.5 |
| 576 |
%v4float = OpTypeVector %float 4 |
| 577 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 578 |
%gl_FragColor = OpVariable %_ptr_Output_v4float Output |
| 579 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 580 |
%BaseColor = OpVariable %_ptr_Input_v4float Input |
| 581 |
%main = OpFunction %void None %8 |
| 582 |
%18 = OpLabel |
| 583 |
%f = OpVariable %_ptr_Function_float Function |
| 584 |
%19 = OpLoad %float %fi |
| 585 |
%20 = OpFOrdLessThan %bool %19 %float_0 |
| 586 |
OpSelectionMerge %21 None |
| 587 |
OpBranchConditional %20 %22 %21 |
| 588 |
%22 = OpLabel |
| 589 |
OpStore %f %float_0_5 |
| 590 |
OpBranch %21 |
| 591 |
%21 = OpLabel |
| 592 |
%23 = OpLoad %float %fi |
| 593 |
%24 = OpFOrdLessThan %bool %23 %float_0 |
| 594 |
OpSelectionMerge %25 None |
| 595 |
OpBranchConditional %24 %26 %27 |
| 596 |
%26 = OpLabel |
| 597 |
%28 = OpLoad %v4float %BaseColor |
| 598 |
%29 = OpLoad %float %f |
| 599 |
%30 = OpVectorTimesScalar %v4float %28 %29 |
| 600 |
OpStore %gl_FragColor %30 |
| 601 |
OpBranch %25 |
| 602 |
%27 = OpLabel |
| 603 |
%31 = OpLoad %v4float %BaseColor |
| 604 |
OpStore %gl_FragColor %31 |
| 605 |
OpBranch %25 |
| 606 |
%25 = OpLabel |
| 607 |
OpReturn |
| 608 |
OpFunctionEnd |
| 609 |
)"; |
| 610 |
|
| 611 |
SinglePassRunAndCheck<LocalSingleStoreElimPass>(assembly, assembly, true, |
| 612 |
true); |
| 613 |
} |
| 614 |
|
| 615 |
TEST_F(LocalSingleStoreElimTest, OptInitializedVariableLikeStore) { |
| 616 |
// Initialized variable f is optimized like it was a store. |
| 617 |
// Note: The SPIR-V was edited to turn the store to f to an |
| 618 |
// an initialization. |
| 619 |
// |
| 620 |
// #version 140 |
| 621 |
// |
| 622 |
// void main() |
| 623 |
// { |
| 624 |
// float f = 0.0; |
| 625 |
// gl_FragColor = vec4(f); |
| 626 |
// } |
| 627 |
|
| 628 |
const std::string predefs = |
| 629 |
R"(OpCapability Shader |
| 630 |
%1 = OpExtInstImport "GLSL.std.450" |
| 631 |
OpMemoryModel Logical GLSL450 |
| 632 |
OpEntryPoint Fragment %main "main" %gl_FragColor |
| 633 |
OpExecutionMode %main OriginUpperLeft |
| 634 |
OpSource GLSL 140 |
| 635 |
OpName %main "main" |
| 636 |
OpName %f "f" |
| 637 |
OpName %gl_FragColor "gl_FragColor" |
| 638 |
OpDecorate %gl_FragColor Location 0 |
| 639 |
%void = OpTypeVoid |
| 640 |
%6 = OpTypeFunction %void |
| 641 |
%float = OpTypeFloat 32 |
| 642 |
%_ptr_Function_float = OpTypePointer Function %float |
| 643 |
%float_0 = OpConstant %float 0 |
| 644 |
%v4float = OpTypeVector %float 4 |
| 645 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 646 |
%gl_FragColor = OpVariable %_ptr_Output_v4float Output |
| 647 |
)"; |
| 648 |
|
| 649 |
const std::string before = |
| 650 |
R"(%main = OpFunction %void None %6 |
| 651 |
%12 = OpLabel |
| 652 |
%f = OpVariable %_ptr_Function_float Function %float_0 |
| 653 |
%13 = OpLoad %float %f |
| 654 |
%14 = OpCompositeConstruct %v4float %13 %13 %13 %13 |
| 655 |
OpStore %gl_FragColor %14 |
| 656 |
OpReturn |
| 657 |
OpFunctionEnd |
| 658 |
)"; |
| 659 |
|
| 660 |
const std::string after = |
| 661 |
R"(%main = OpFunction %void None %6 |
| 662 |
%12 = OpLabel |
| 663 |
%f = OpVariable %_ptr_Function_float Function %float_0 |
| 664 |
%14 = OpCompositeConstruct %v4float %float_0 %float_0 %float_0 %float_0 |
| 665 |
OpStore %gl_FragColor %14 |
| 666 |
OpReturn |
| 667 |
OpFunctionEnd |
| 668 |
)"; |
| 669 |
|
| 670 |
SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before, |
| 671 |
predefs + after, true, true); |
| 672 |
} |
| 673 |
|
| 674 |
TEST_F(LocalSingleStoreElimTest, PointerVariable) { |
| 675 |
// Test that checks if a pointer variable is removed. |
| 676 |
|
| 677 |
const std::string before = |
| 678 |
R"(OpCapability Shader |
| 679 |
OpMemoryModel Logical GLSL450 |
| 680 |
OpEntryPoint Fragment %1 "main" %2 |
| 681 |
OpExecutionMode %1 OriginUpperLeft |
| 682 |
OpMemberDecorate %_struct_3 0 Offset 0 |
| 683 |
OpDecorate %_runtimearr__struct_3 ArrayStride 16 |
| 684 |
OpMemberDecorate %_struct_5 0 Offset 0 |
| 685 |
OpDecorate %_struct_5 BufferBlock |
| 686 |
OpMemberDecorate %_struct_6 0 Offset 0 |
| 687 |
OpDecorate %_struct_6 BufferBlock |
| 688 |
OpDecorate %2 Location 0 |
| 689 |
OpDecorate %7 DescriptorSet 0 |
| 690 |
OpDecorate %7 Binding 0 |
| 691 |
%void = OpTypeVoid |
| 692 |
%10 = OpTypeFunction %void |
| 693 |
%int = OpTypeInt 32 1 |
| 694 |
%uint = OpTypeInt 32 0 |
| 695 |
%float = OpTypeFloat 32 |
| 696 |
%v4float = OpTypeVector %float 4 |
| 697 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 698 |
%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float |
| 699 |
%_struct_3 = OpTypeStruct %v4float |
| 700 |
%_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3 |
| 701 |
%_struct_5 = OpTypeStruct %_runtimearr__struct_3 |
| 702 |
%_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5 |
| 703 |
%_struct_6 = OpTypeStruct %int |
| 704 |
%_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6 |
| 705 |
%_ptr_Function__ptr_Uniform__struct_5 = OpTypePointer Function %_ptr_Uniform__struct_5 |
| 706 |
%_ptr_Function__ptr_Uniform__struct_6 = OpTypePointer Function %_ptr_Uniform__struct_6 |
| 707 |
%int_0 = OpConstant %int 0 |
| 708 |
%uint_0 = OpConstant %uint 0 |
| 709 |
%2 = OpVariable %_ptr_Output_v4float Output |
| 710 |
%7 = OpVariable %_ptr_Uniform__struct_5 Uniform |
| 711 |
%1 = OpFunction %void None %10 |
| 712 |
%23 = OpLabel |
| 713 |
%24 = OpVariable %_ptr_Function__ptr_Uniform__struct_5 Function |
| 714 |
OpStore %24 %7 |
| 715 |
%26 = OpLoad %_ptr_Uniform__struct_5 %24 |
| 716 |
%27 = OpAccessChain %_ptr_Uniform_v4float %26 %int_0 %uint_0 %int_0 |
| 717 |
%28 = OpLoad %v4float %27 |
| 718 |
%29 = OpCopyObject %v4float %28 |
| 719 |
OpStore %2 %28 |
| 720 |
OpReturn |
| 721 |
OpFunctionEnd |
| 722 |
)"; |
| 723 |
|
| 724 |
const std::string after = |
| 725 |
R"(OpCapability Shader |
| 726 |
OpMemoryModel Logical GLSL450 |
| 727 |
OpEntryPoint Fragment %1 "main" %2 |
| 728 |
OpExecutionMode %1 OriginUpperLeft |
| 729 |
OpMemberDecorate %_struct_3 0 Offset 0 |
| 730 |
OpDecorate %_runtimearr__struct_3 ArrayStride 16 |
| 731 |
OpMemberDecorate %_struct_5 0 Offset 0 |
| 732 |
OpDecorate %_struct_5 BufferBlock |
| 733 |
OpMemberDecorate %_struct_6 0 Offset 0 |
| 734 |
OpDecorate %_struct_6 BufferBlock |
| 735 |
OpDecorate %2 Location 0 |
| 736 |
OpDecorate %7 DescriptorSet 0 |
| 737 |
OpDecorate %7 Binding 0 |
| 738 |
%void = OpTypeVoid |
| 739 |
%10 = OpTypeFunction %void |
| 740 |
%int = OpTypeInt 32 1 |
| 741 |
%uint = OpTypeInt 32 0 |
| 742 |
%float = OpTypeFloat 32 |
| 743 |
%v4float = OpTypeVector %float 4 |
| 744 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 745 |
%_ptr_Uniform_v4float = OpTypePointer Uniform %v4float |
| 746 |
%_struct_3 = OpTypeStruct %v4float |
| 747 |
%_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3 |
| 748 |
%_struct_5 = OpTypeStruct %_runtimearr__struct_3 |
| 749 |
%_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5 |
| 750 |
%_struct_6 = OpTypeStruct %int |
| 751 |
%_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6 |
| 752 |
%_ptr_Function__ptr_Uniform__struct_5 = OpTypePointer Function %_ptr_Uniform__struct_5 |
| 753 |
%_ptr_Function__ptr_Uniform__struct_6 = OpTypePointer Function %_ptr_Uniform__struct_6 |
| 754 |
%int_0 = OpConstant %int 0 |
| 755 |
%uint_0 = OpConstant %uint 0 |
| 756 |
%2 = OpVariable %_ptr_Output_v4float Output |
| 757 |
%7 = OpVariable %_ptr_Uniform__struct_5 Uniform |
| 758 |
%1 = OpFunction %void None %10 |
| 759 |
%23 = OpLabel |
| 760 |
%24 = OpVariable %_ptr_Function__ptr_Uniform__struct_5 Function |
| 761 |
OpStore %24 %7 |
| 762 |
%27 = OpAccessChain %_ptr_Uniform_v4float %7 %int_0 %uint_0 %int_0 |
| 763 |
%28 = OpLoad %v4float %27 |
| 764 |
%29 = OpCopyObject %v4float %28 |
| 765 |
OpStore %2 %28 |
| 766 |
OpReturn |
| 767 |
OpFunctionEnd |
| 768 |
)"; |
| 769 |
|
| 770 |
// Relax logical pointers to allow pointer allocations. |
| 771 |
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS); |
| 772 |
ValidatorOptions()->relax_logical_pointer = true; |
| 773 |
SinglePassRunAndCheck<LocalSingleStoreElimPass>(before, after, true, true); |
| 774 |
} |
| 775 |
|
| 776 |
// Test that that an unused OpAccessChain between a store and a use does does |
| 777 |
// not hinders the replacement of the use. We need to check this because |
| 778 |
// local-access-chain-convert does always remove the OpAccessChain instructions |
| 779 |
// that become dead. |
| 780 |
|
| 781 |
TEST_F(LocalSingleStoreElimTest, |
| 782 |
StoreElimWithUnusedInterveningAccessChainLoad) { |
| 783 |
// Last load of v is eliminated, but access chain load and store of v isn't |
| 784 |
// |
| 785 |
// #version 140 |
| 786 |
// |
| 787 |
// in vec4 BaseColor; |
| 788 |
// |
| 789 |
// void main() |
| 790 |
// { |
| 791 |
// vec4 v = BaseColor; |
| 792 |
// float f = v[3]; |
| 793 |
// gl_FragColor = v * f; |
| 794 |
// } |
| 795 |
|
| 796 |
const std::string predefs = |
| 797 |
R"(OpCapability Shader |
| 798 |
%1 = OpExtInstImport "GLSL.std.450" |
| 799 |
OpMemoryModel Logical GLSL450 |
| 800 |
OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor |
| 801 |
OpExecutionMode %main OriginUpperLeft |
| 802 |
OpSource GLSL 140 |
| 803 |
OpName %main "main" |
| 804 |
OpName %v "v" |
| 805 |
OpName %BaseColor "BaseColor" |
| 806 |
OpName %gl_FragColor "gl_FragColor" |
| 807 |
%void = OpTypeVoid |
| 808 |
%8 = OpTypeFunction %void |
| 809 |
%float = OpTypeFloat 32 |
| 810 |
%v4float = OpTypeVector %float 4 |
| 811 |
%_ptr_Function_v4float = OpTypePointer Function %v4float |
| 812 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 813 |
%BaseColor = OpVariable %_ptr_Input_v4float Input |
| 814 |
%_ptr_Function_float = OpTypePointer Function %float |
| 815 |
%uint = OpTypeInt 32 0 |
| 816 |
%uint_3 = OpConstant %uint 3 |
| 817 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 818 |
%gl_FragColor = OpVariable %_ptr_Output_v4float Output |
| 819 |
)"; |
| 820 |
|
| 821 |
const std::string before = |
| 822 |
R"(%main = OpFunction %void None %8 |
| 823 |
%17 = OpLabel |
| 824 |
%v = OpVariable %_ptr_Function_v4float Function |
| 825 |
%18 = OpLoad %v4float %BaseColor |
| 826 |
OpStore %v %18 |
| 827 |
%19 = OpAccessChain %_ptr_Function_float %v %uint_3 |
| 828 |
%21 = OpLoad %v4float %v |
| 829 |
OpStore %gl_FragColor %21 |
| 830 |
OpReturn |
| 831 |
OpFunctionEnd |
| 832 |
)"; |
| 833 |
|
| 834 |
const std::string after = |
| 835 |
R"(%main = OpFunction %void None %8 |
| 836 |
%17 = OpLabel |
| 837 |
%v = OpVariable %_ptr_Function_v4float Function |
| 838 |
%18 = OpLoad %v4float %BaseColor |
| 839 |
OpStore %v %18 |
| 840 |
%19 = OpAccessChain %_ptr_Function_float %v %uint_3 |
| 841 |
OpStore %gl_FragColor %18 |
| 842 |
OpReturn |
| 843 |
OpFunctionEnd |
| 844 |
)"; |
| 845 |
|
| 846 |
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS); |
| 847 |
SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before, |
| 848 |
predefs + after, true, true); |
| 849 |
} |
| 850 |
|
| 851 |
TEST_F(LocalSingleStoreElimTest, VariablePointerTest) { |
| 852 |
// Check that the load of the first variable is still used and that the load |
| 853 |
// of the third variable is propagated. The first load has to remain because |
| 854 |
// of the store to the variable pointer. |
| 855 |
const std::string text = R"( |
| 856 |
; CHECK: [[v1:%\w+]] = OpVariable |
| 857 |
; CHECK: [[v2:%\w+]] = OpVariable |
| 858 |
; CHECK: [[v3:%\w+]] = OpVariable |
| 859 |
; CHECK: [[ld1:%\w+]] = OpLoad %int [[v1]] |
| 860 |
; CHECK: OpIAdd %int [[ld1]] %int_0 |
| 861 |
OpCapability Shader |
| 862 |
OpCapability VariablePointers |
| 863 |
%1 = OpExtInstImport "GLSL.std.450" |
| 864 |
OpMemoryModel Logical GLSL450 |
| 865 |
OpEntryPoint GLCompute %2 "main" |
| 866 |
OpExecutionMode %2 LocalSize 1 1 1 |
| 867 |
OpSource GLSL 450 |
| 868 |
OpMemberDecorate %_struct_3 0 Offset 0 |
| 869 |
OpMemberDecorate %_struct_3 1 Offset 4 |
| 870 |
%void = OpTypeVoid |
| 871 |
%5 = OpTypeFunction %void |
| 872 |
%int = OpTypeInt 32 1 |
| 873 |
%bool = OpTypeBool |
| 874 |
%_struct_3 = OpTypeStruct %int %int |
| 875 |
%_ptr_Function__struct_3 = OpTypePointer Function %_struct_3 |
| 876 |
%_ptr_Function_int = OpTypePointer Function %int |
| 877 |
%true = OpConstantTrue %bool |
| 878 |
%int_0 = OpConstant %int 0 |
| 879 |
%int_1 = OpConstant %int 1 |
| 880 |
%13 = OpConstantNull %_struct_3 |
| 881 |
%2 = OpFunction %void None %5 |
| 882 |
%14 = OpLabel |
| 883 |
%15 = OpVariable %_ptr_Function_int Function |
| 884 |
%16 = OpVariable %_ptr_Function_int Function |
| 885 |
%17 = OpVariable %_ptr_Function_int Function |
| 886 |
OpStore %15 %int_1 |
| 887 |
OpStore %17 %int_0 |
| 888 |
OpSelectionMerge %18 None |
| 889 |
OpBranchConditional %true %19 %20 |
| 890 |
%19 = OpLabel |
| 891 |
OpBranch %18 |
| 892 |
%20 = OpLabel |
| 893 |
OpBranch %18 |
| 894 |
%18 = OpLabel |
| 895 |
%21 = OpPhi %_ptr_Function_int %15 %19 %16 %20 |
| 896 |
OpStore %21 %int_0 |
| 897 |
%22 = OpLoad %int %15 |
| 898 |
%23 = OpLoad %int %17 |
| 899 |
%24 = OpIAdd %int %22 %23 |
| 900 |
OpReturn |
| 901 |
OpFunctionEnd |
| 902 |
)"; |
| 903 |
SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false); |
| 904 |
} |
| 905 |
|
| 906 |
TEST_F(LocalSingleStoreElimTest, DebugDeclareTest) { |
| 907 |
// If OpenCL.DebugInfo.100 enabled, check that store/load is still |
| 908 |
// optimized, DebugValue placed after the store and the associated |
| 909 |
// DebugDeclare is removed. |
| 910 |
const std::string text = R"( |
| 911 |
OpCapability Shader |
| 912 |
%1 = OpExtInstImport "OpenCL.DebugInfo.100" |
| 913 |
OpMemoryModel Logical GLSL450 |
| 914 |
OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %g_sAniso %in_var_TEXCOORD2 %out_var_SV_Target0 |
| 915 |
OpExecutionMode %MainPs OriginUpperLeft |
| 916 |
%20 = OpString "foo.frag" |
| 917 |
%24 = OpString "PS_OUTPUT" |
| 918 |
%28 = OpString "float" |
| 919 |
%31 = OpString "vColor" |
| 920 |
%33 = OpString "PS_INPUT" |
| 921 |
%38 = OpString "vTextureCoords" |
| 922 |
%40 = OpString "@type.2d.image" |
| 923 |
%41 = OpString "type.2d.image" |
| 924 |
%43 = OpString "Texture2D.TemplateParam" |
| 925 |
%47 = OpString "src.MainPs" |
| 926 |
%51 = OpString "tc" |
| 927 |
%53 = OpString "ps_output" |
| 928 |
%56 = OpString "i" |
| 929 |
%58 = OpString "@type.sampler" |
| 930 |
%59 = OpString "type.sampler" |
| 931 |
%61 = OpString "g_sAniso" |
| 932 |
%63 = OpString "g_tColor" |
| 933 |
OpName %type_2d_image "type.2d.image" |
| 934 |
OpName %g_tColor "g_tColor" |
| 935 |
OpName %type_sampler "type.sampler" |
| 936 |
OpName %g_sAniso "g_sAniso" |
| 937 |
OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2" |
| 938 |
OpName %out_var_SV_Target0 "out.var.SV_Target0" |
| 939 |
OpName %MainPs "MainPs" |
| 940 |
OpName %PS_INPUT "PS_INPUT" |
| 941 |
OpMemberName %PS_INPUT 0 "vTextureCoords" |
| 942 |
OpName %param_var_i "param.var.i" |
| 943 |
OpName %PS_OUTPUT "PS_OUTPUT" |
| 944 |
OpMemberName %PS_OUTPUT 0 "vColor" |
| 945 |
OpName %type_sampled_image "type.sampled.image" |
| 946 |
OpDecorate %in_var_TEXCOORD2 Location 0 |
| 947 |
OpDecorate %out_var_SV_Target0 Location 0 |
| 948 |
OpDecorate %g_tColor DescriptorSet 0 |
| 949 |
OpDecorate %g_tColor Binding 0 |
| 950 |
OpDecorate %g_sAniso DescriptorSet 0 |
| 951 |
OpDecorate %g_sAniso Binding 1 |
| 952 |
%int = OpTypeInt 32 1 |
| 953 |
%int_0 = OpConstant %int 0 |
| 954 |
%uint = OpTypeInt 32 0 |
| 955 |
%uint_32 = OpConstant %uint 32 |
| 956 |
%float = OpTypeFloat 32 |
| 957 |
%type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown |
| 958 |
%_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image |
| 959 |
%type_sampler = OpTypeSampler |
| 960 |
%_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler |
| 961 |
%v2float = OpTypeVector %float 2 |
| 962 |
%_ptr_Input_v2float = OpTypePointer Input %v2float |
| 963 |
%v4float = OpTypeVector %float 4 |
| 964 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 965 |
%void = OpTypeVoid |
| 966 |
%uint_128 = OpConstant %uint 128 |
| 967 |
%uint_0 = OpConstant %uint 0 |
| 968 |
%uint_64 = OpConstant %uint 64 |
| 969 |
%65 = OpTypeFunction %void |
| 970 |
%PS_INPUT = OpTypeStruct %v2float |
| 971 |
%_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT |
| 972 |
%PS_OUTPUT = OpTypeStruct %v4float |
| 973 |
%75 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT |
| 974 |
%_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT |
| 975 |
%_ptr_Function_v2float = OpTypePointer Function %v2float |
| 976 |
%type_sampled_image = OpTypeSampledImage %type_2d_image |
| 977 |
%_ptr_Function_v4float = OpTypePointer Function %v4float |
| 978 |
%g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant |
| 979 |
%g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant |
| 980 |
%in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input |
| 981 |
%out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output |
| 982 |
%39 = OpExtInst %void %1 DebugInfoNone |
| 983 |
%55 = OpExtInst %void %1 DebugExpression |
| 984 |
%22 = OpExtInst %void %1 DebugSource %20 |
| 985 |
%23 = OpExtInst %void %1 DebugCompilationUnit 1 4 %22 HLSL |
| 986 |
%26 = OpExtInst %void %1 DebugTypeComposite %24 Structure %22 10 1 %23 %24 %uint_128 FlagIsProtected|FlagIsPrivate %27 |
| 987 |
%29 = OpExtInst %void %1 DebugTypeBasic %28 %uint_32 Float |
| 988 |
%30 = OpExtInst %void %1 DebugTypeVector %29 4 |
| 989 |
%27 = OpExtInst %void %1 DebugTypeMember %31 %30 %22 12 5 %26 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate |
| 990 |
%35 = OpExtInst %void %1 DebugTypeComposite %33 Structure %22 5 1 %23 %33 %uint_64 FlagIsProtected|FlagIsPrivate %36 |
| 991 |
%37 = OpExtInst %void %1 DebugTypeVector %29 2 |
| 992 |
%36 = OpExtInst %void %1 DebugTypeMember %38 %37 %22 7 5 %35 %uint_0 %uint_64 FlagIsProtected|FlagIsPrivate |
| 993 |
%42 = OpExtInst %void %1 DebugTypeComposite %40 Class %22 0 0 %23 %41 %39 FlagIsProtected|FlagIsPrivate |
| 994 |
%44 = OpExtInst %void %1 DebugTypeTemplateParameter %43 %29 %39 %22 0 0 |
| 995 |
%45 = OpExtInst %void %1 DebugTypeTemplate %42 %44 |
| 996 |
%46 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %26 %35 |
| 997 |
%48 = OpExtInst %void %1 DebugFunction %47 %46 %22 15 1 %23 %47 FlagIsProtected|FlagIsPrivate 16 %39 |
| 998 |
%50 = OpExtInst %void %1 DebugLexicalBlock %22 16 1 %48 |
| 999 |
%52 = OpExtInst %void %1 DebugLocalVariable %51 %37 %22 19 12 %50 FlagIsLocal |
| 1000 |
%54 = OpExtInst %void %1 DebugLocalVariable %53 %26 %22 17 15 %50 FlagIsLocal |
| 1001 |
%57 = OpExtInst %void %1 DebugLocalVariable %56 %35 %22 15 29 %48 FlagIsLocal 1 |
| 1002 |
%60 = OpExtInst %void %1 DebugTypeComposite %58 Structure %22 0 0 %23 %59 %39 FlagIsProtected|FlagIsPrivate |
| 1003 |
%62 = OpExtInst %void %1 DebugGlobalVariable %61 %60 %22 3 14 %23 %61 %g_sAniso FlagIsDefinition |
| 1004 |
%64 = OpExtInst %void %1 DebugGlobalVariable %63 %42 %22 1 11 %23 %63 %g_tColor FlagIsDefinition |
| 1005 |
%MainPs = OpFunction %void None %65 |
| 1006 |
%66 = OpLabel |
| 1007 |
%114 = OpExtInst %void %1 DebugScope %50 |
| 1008 |
%98 = OpVariable %_ptr_Function_PS_OUTPUT Function |
| 1009 |
%99 = OpVariable %_ptr_Function_v2float Function |
| 1010 |
%115 = OpExtInst %void %1 DebugNoScope |
| 1011 |
%100 = OpVariable %_ptr_Function_PS_OUTPUT Function |
| 1012 |
%param_var_i = OpVariable %_ptr_Function_PS_INPUT Function |
| 1013 |
%70 = OpLoad %v2float %in_var_TEXCOORD2 |
| 1014 |
%71 = OpCompositeConstruct %PS_INPUT %70 |
| 1015 |
OpStore %param_var_i %71 |
| 1016 |
%116 = OpExtInst %void %1 DebugScope %48 |
| 1017 |
%102 = OpExtInst %void %1 DebugDeclare %57 %param_var_i %55 |
| 1018 |
%117 = OpExtInst %void %1 DebugScope %50 |
| 1019 |
%103 = OpExtInst %void %1 DebugDeclare %54 %98 %55 |
| 1020 |
OpLine %20 19 17 |
| 1021 |
%104 = OpAccessChain %_ptr_Function_v2float %param_var_i %int_0 |
| 1022 |
%105 = OpLoad %v2float %104 |
| 1023 |
OpLine %20 19 12 |
| 1024 |
OpStore %99 %105 |
| 1025 |
%106 = OpExtInst %void %1 DebugDeclare %52 %99 %55 |
| 1026 |
;CHECK-NOT: %106 = OpExtInst %void %1 DebugDeclare %52 %99 %55 |
| 1027 |
;CHECK: %119 = OpExtInst %void %1 DebugValue %52 %105 %55 |
| 1028 |
OpLine %20 20 26 |
| 1029 |
%107 = OpLoad %type_2d_image %g_tColor |
| 1030 |
OpLine %20 20 46 |
| 1031 |
%108 = OpLoad %type_sampler %g_sAniso |
| 1032 |
OpLine %20 20 57 |
| 1033 |
%109 = OpLoad %v2float %99 |
| 1034 |
;CHECK-NOT: %109 = OpLoad %v2float %99 |
| 1035 |
OpLine %20 20 26 |
| 1036 |
%110 = OpSampledImage %type_sampled_image %107 %108 |
| 1037 |
%111 = OpImageSampleImplicitLod %v4float %110 %109 None |
| 1038 |
;CHECK-NOT: %111 = OpImageSampleImplicitLod %v4float %110 %109 None |
| 1039 |
;CHECK: %111 = OpImageSampleImplicitLod %v4float %110 %105 None |
| 1040 |
OpLine %20 20 5 |
| 1041 |
%112 = OpAccessChain %_ptr_Function_v4float %98 %int_0 |
| 1042 |
OpStore %112 %111 |
| 1043 |
OpLine %20 21 12 |
| 1044 |
%113 = OpLoad %PS_OUTPUT %98 |
| 1045 |
OpLine %20 21 5 |
| 1046 |
OpStore %100 %113 |
| 1047 |
%118 = OpExtInst %void %1 DebugNoScope |
| 1048 |
%73 = OpLoad %PS_OUTPUT %100 |
| 1049 |
%74 = OpCompositeExtract %v4float %73 0 |
| 1050 |
OpStore %out_var_SV_Target0 %74 |
| 1051 |
OpReturn |
| 1052 |
OpFunctionEnd |
| 1053 |
)"; |
| 1054 |
|
| 1055 |
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS); |
| 1056 |
SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false); |
| 1057 |
} |
| 1058 |
|
| 1059 |
TEST_F(LocalSingleStoreElimTest, DebugValueTest) { |
| 1060 |
// If OpenCL.DebugInfo.100 enabled, check that store/load is still |
| 1061 |
// optimized, DebugValue placed after the store and the associated |
| 1062 |
// DebugValue Deref is removed. |
| 1063 |
const std::string text = R"( |
| 1064 |
OpCapability Shader |
| 1065 |
%1 = OpExtInstImport "OpenCL.DebugInfo.100" |
| 1066 |
OpMemoryModel Logical GLSL450 |
| 1067 |
OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %g_sAniso %in_var_TEXCOORD2 %out_var_SV_Target0 |
| 1068 |
OpExecutionMode %MainPs OriginUpperLeft |
| 1069 |
%7 = OpString "foo.frag" |
| 1070 |
%8 = OpString "PS_OUTPUT" |
| 1071 |
%9 = OpString "float" |
| 1072 |
%10 = OpString "vColor" |
| 1073 |
%11 = OpString "PS_INPUT" |
| 1074 |
%12 = OpString "vTextureCoords" |
| 1075 |
%13 = OpString "@type.2d.image" |
| 1076 |
%14 = OpString "type.2d.image" |
| 1077 |
%15 = OpString "Texture2D.TemplateParam" |
| 1078 |
%16 = OpString "src.MainPs" |
| 1079 |
%17 = OpString "tc" |
| 1080 |
%18 = OpString "ps_output" |
| 1081 |
%19 = OpString "i" |
| 1082 |
%20 = OpString "@type.sampler" |
| 1083 |
%21 = OpString "type.sampler" |
| 1084 |
%22 = OpString "g_sAniso" |
| 1085 |
%23 = OpString "g_tColor" |
| 1086 |
OpName %type_2d_image "type.2d.image" |
| 1087 |
OpName %g_tColor "g_tColor" |
| 1088 |
OpName %type_sampler "type.sampler" |
| 1089 |
OpName %g_sAniso "g_sAniso" |
| 1090 |
OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2" |
| 1091 |
OpName %out_var_SV_Target0 "out.var.SV_Target0" |
| 1092 |
OpName %MainPs "MainPs" |
| 1093 |
OpName %PS_INPUT "PS_INPUT" |
| 1094 |
OpMemberName %PS_INPUT 0 "vTextureCoords" |
| 1095 |
OpName %param_var_i "param.var.i" |
| 1096 |
OpName %PS_OUTPUT "PS_OUTPUT" |
| 1097 |
OpMemberName %PS_OUTPUT 0 "vColor" |
| 1098 |
OpName %type_sampled_image "type.sampled.image" |
| 1099 |
OpDecorate %in_var_TEXCOORD2 Location 0 |
| 1100 |
OpDecorate %out_var_SV_Target0 Location 0 |
| 1101 |
OpDecorate %g_tColor DescriptorSet 0 |
| 1102 |
OpDecorate %g_tColor Binding 0 |
| 1103 |
OpDecorate %g_sAniso DescriptorSet 0 |
| 1104 |
OpDecorate %g_sAniso Binding 1 |
| 1105 |
%int = OpTypeInt 32 1 |
| 1106 |
%int_0 = OpConstant %int 0 |
| 1107 |
%uint = OpTypeInt 32 0 |
| 1108 |
%uint_32 = OpConstant %uint 32 |
| 1109 |
%float = OpTypeFloat 32 |
| 1110 |
%type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown |
| 1111 |
%_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image |
| 1112 |
%type_sampler = OpTypeSampler |
| 1113 |
%_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler |
| 1114 |
%v2float = OpTypeVector %float 2 |
| 1115 |
%_ptr_Input_v2float = OpTypePointer Input %v2float |
| 1116 |
%v4float = OpTypeVector %float 4 |
| 1117 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 1118 |
%void = OpTypeVoid |
| 1119 |
%uint_128 = OpConstant %uint 128 |
| 1120 |
%uint_0 = OpConstant %uint 0 |
| 1121 |
%uint_64 = OpConstant %uint 64 |
| 1122 |
%45 = OpTypeFunction %void |
| 1123 |
%PS_INPUT = OpTypeStruct %v2float |
| 1124 |
%_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT |
| 1125 |
%PS_OUTPUT = OpTypeStruct %v4float |
| 1126 |
%47 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT |
| 1127 |
%_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT |
| 1128 |
%_ptr_Function_v2float = OpTypePointer Function %v2float |
| 1129 |
%type_sampled_image = OpTypeSampledImage %type_2d_image |
| 1130 |
%_ptr_Function_v4float = OpTypePointer Function %v4float |
| 1131 |
%g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant |
| 1132 |
%g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant |
| 1133 |
%in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input |
| 1134 |
%out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output |
| 1135 |
%51 = OpExtInst %void %1 DebugInfoNone |
| 1136 |
%52 = OpExtInst %void %1 DebugExpression |
| 1137 |
%53 = OpExtInst %void %1 DebugOperation Deref |
| 1138 |
%54 = OpExtInst %void %1 DebugExpression %53 |
| 1139 |
%55 = OpExtInst %void %1 DebugSource %7 |
| 1140 |
%56 = OpExtInst %void %1 DebugCompilationUnit 1 4 %55 HLSL |
| 1141 |
%57 = OpExtInst %void %1 DebugTypeComposite %8 Structure %55 10 1 %56 %8 %uint_128 FlagIsProtected|FlagIsPrivate %58 |
| 1142 |
%59 = OpExtInst %void %1 DebugTypeBasic %9 %uint_32 Float |
| 1143 |
%60 = OpExtInst %void %1 DebugTypeVector %59 4 |
| 1144 |
%58 = OpExtInst %void %1 DebugTypeMember %10 %60 %55 12 5 %57 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate |
| 1145 |
%61 = OpExtInst %void %1 DebugTypeComposite %11 Structure %55 5 1 %56 %11 %uint_64 FlagIsProtected|FlagIsPrivate %62 |
| 1146 |
%63 = OpExtInst %void %1 DebugTypeVector %59 2 |
| 1147 |
%62 = OpExtInst %void %1 DebugTypeMember %12 %63 %55 7 5 %61 %uint_0 %uint_64 FlagIsProtected|FlagIsPrivate |
| 1148 |
%64 = OpExtInst %void %1 DebugTypeComposite %13 Class %55 0 0 %56 %14 %51 FlagIsProtected|FlagIsPrivate |
| 1149 |
%65 = OpExtInst %void %1 DebugTypeTemplateParameter %15 %59 %51 %55 0 0 |
| 1150 |
%66 = OpExtInst %void %1 DebugTypeTemplate %64 %65 |
| 1151 |
%67 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %57 %61 |
| 1152 |
%68 = OpExtInst %void %1 DebugFunction %16 %67 %55 15 1 %56 %16 FlagIsProtected|FlagIsPrivate 16 %51 |
| 1153 |
%69 = OpExtInst %void %1 DebugLexicalBlock %55 16 1 %68 |
| 1154 |
%70 = OpExtInst %void %1 DebugLocalVariable %17 %63 %55 19 12 %69 FlagIsLocal |
| 1155 |
%71 = OpExtInst %void %1 DebugLocalVariable %18 %57 %55 17 15 %69 FlagIsLocal |
| 1156 |
%72 = OpExtInst %void %1 DebugLocalVariable %19 %61 %55 15 29 %68 FlagIsLocal 1 |
| 1157 |
%73 = OpExtInst %void %1 DebugTypeComposite %20 Structure %55 0 0 %56 %21 %51 FlagIsProtected|FlagIsPrivate |
| 1158 |
%74 = OpExtInst %void %1 DebugGlobalVariable %22 %73 %55 3 14 %56 %22 %g_sAniso FlagIsDefinition |
| 1159 |
%75 = OpExtInst %void %1 DebugGlobalVariable %23 %64 %55 1 11 %56 %23 %g_tColor FlagIsDefinition |
| 1160 |
%MainPs = OpFunction %void None %45 |
| 1161 |
%76 = OpLabel |
| 1162 |
%101 = OpExtInst %void %1 DebugScope %69 |
| 1163 |
%78 = OpVariable %_ptr_Function_PS_OUTPUT Function |
| 1164 |
%79 = OpVariable %_ptr_Function_v2float Function |
| 1165 |
%102 = OpExtInst %void %1 DebugNoScope |
| 1166 |
%81 = OpVariable %_ptr_Function_PS_OUTPUT Function |
| 1167 |
%param_var_i = OpVariable %_ptr_Function_PS_INPUT Function |
| 1168 |
%82 = OpLoad %v2float %in_var_TEXCOORD2 |
| 1169 |
%83 = OpCompositeConstruct %PS_INPUT %82 |
| 1170 |
OpStore %param_var_i %83 |
| 1171 |
%103 = OpExtInst %void %1 DebugScope %68 |
| 1172 |
%85 = OpExtInst %void %1 DebugDeclare %72 %param_var_i %52 |
| 1173 |
%104 = OpExtInst %void %1 DebugScope %69 |
| 1174 |
%87 = OpExtInst %void %1 DebugDeclare %71 %78 %52 |
| 1175 |
OpLine %7 19 17 |
| 1176 |
%88 = OpAccessChain %_ptr_Function_v2float %param_var_i %int_0 |
| 1177 |
%89 = OpLoad %v2float %88 |
| 1178 |
OpLine %7 19 12 |
| 1179 |
OpStore %79 %89 |
| 1180 |
%90 = OpExtInst %void %1 DebugValue %70 %79 %54 |
| 1181 |
;CHECK-NOT: %90 = OpExtInst %void %1 DebugValue %70 %79 %54 |
| 1182 |
;CHECK: %106 = OpExtInst %void %1 DebugValue %70 %89 %52 |
| 1183 |
OpLine %7 20 26 |
| 1184 |
%91 = OpLoad %type_2d_image %g_tColor |
| 1185 |
OpLine %7 20 46 |
| 1186 |
%92 = OpLoad %type_sampler %g_sAniso |
| 1187 |
OpLine %7 20 57 |
| 1188 |
%93 = OpLoad %v2float %79 |
| 1189 |
;CHECK-NOT: %93 = OpLoad %v2float %79 |
| 1190 |
OpLine %7 20 26 |
| 1191 |
%94 = OpSampledImage %type_sampled_image %91 %92 |
| 1192 |
%95 = OpImageSampleImplicitLod %v4float %94 %93 None |
| 1193 |
;CHECK-NOT: %95 = OpImageSampleImplicitLod %v4float %94 %93 None |
| 1194 |
;CHECK: %95 = OpImageSampleImplicitLod %v4float %94 %89 None |
| 1195 |
OpLine %7 20 5 |
| 1196 |
%96 = OpAccessChain %_ptr_Function_v4float %78 %int_0 |
| 1197 |
OpStore %96 %95 |
| 1198 |
OpLine %7 21 12 |
| 1199 |
%97 = OpLoad %PS_OUTPUT %78 |
| 1200 |
OpLine %7 21 5 |
| 1201 |
OpStore %81 %97 |
| 1202 |
%105 = OpExtInst %void %1 DebugNoScope |
| 1203 |
%99 = OpLoad %PS_OUTPUT %81 |
| 1204 |
%100 = OpCompositeExtract %v4float %99 0 |
| 1205 |
OpStore %out_var_SV_Target0 %100 |
| 1206 |
OpReturn |
| 1207 |
OpFunctionEnd |
| 1208 |
)"; |
| 1209 |
|
| 1210 |
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS); |
| 1211 |
SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false); |
| 1212 |
} |
| 1213 |
|
| 1214 |
TEST_F(LocalSingleStoreElimTest, UseStoreLineInfoForDebugValueLine) { |
| 1215 |
// When the store is in the scope of OpenCL.DebugInfo.100 DebugDeclare, |
| 1216 |
// the OpLine of the added OpenCL.DebugInfo.100 DebugValue must be the |
| 1217 |
// same with the OpLine of the store. |
| 1218 |
const std::string text = R"( |
| 1219 |
OpCapability Shader |
| 1220 |
%1 = OpExtInstImport "OpenCL.DebugInfo.100" |
| 1221 |
OpMemoryModel Logical GLSL450 |
| 1222 |
OpEntryPoint Vertex %main "main" %in_var_POSITION %in_var_COLOR %gl_Position %out_var_COLOR |
| 1223 |
%7 = OpString "simple.hlsl" |
| 1224 |
%8 = OpString "float" |
| 1225 |
%9 = OpString "VS_OUTPUT" |
| 1226 |
%10 = OpString "color" |
| 1227 |
%11 = OpString "pos" |
| 1228 |
%12 = OpString "main" |
| 1229 |
%13 = OpString "" |
| 1230 |
%14 = OpString "vout" |
| 1231 |
OpName %in_var_POSITION "in.var.POSITION" |
| 1232 |
OpName %in_var_COLOR "in.var.COLOR" |
| 1233 |
OpName %out_var_COLOR "out.var.COLOR" |
| 1234 |
OpName %main "main" |
| 1235 |
OpName %VS_OUTPUT "VS_OUTPUT" |
| 1236 |
OpMemberName %VS_OUTPUT 0 "pos" |
| 1237 |
OpMemberName %VS_OUTPUT 1 "color" |
| 1238 |
OpDecorate %gl_Position BuiltIn Position |
| 1239 |
OpDecorate %in_var_POSITION Location 0 |
| 1240 |
OpDecorate %in_var_COLOR Location 1 |
| 1241 |
OpDecorate %out_var_COLOR Location 0 |
| 1242 |
%int = OpTypeInt 32 1 |
| 1243 |
%int_0 = OpConstant %int 0 |
| 1244 |
%int_1 = OpConstant %int 1 |
| 1245 |
%uint = OpTypeInt 32 0 |
| 1246 |
%uint_32 = OpConstant %uint 32 |
| 1247 |
%float = OpTypeFloat 32 |
| 1248 |
%v4float = OpTypeVector %float 4 |
| 1249 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 1250 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 1251 |
%void = OpTypeVoid |
| 1252 |
%uint_256 = OpConstant %uint 256 |
| 1253 |
%uint_128 = OpConstant %uint 128 |
| 1254 |
%uint_0 = OpConstant %uint 0 |
| 1255 |
%36 = OpTypeFunction %void |
| 1256 |
%_ptr_Function_v4float = OpTypePointer Function %v4float |
| 1257 |
%VS_OUTPUT = OpTypeStruct %v4float %v4float |
| 1258 |
%_ptr_Function_VS_OUTPUT = OpTypePointer Function %VS_OUTPUT |
| 1259 |
%in_var_POSITION = OpVariable %_ptr_Input_v4float Input |
| 1260 |
%in_var_COLOR = OpVariable %_ptr_Input_v4float Input |
| 1261 |
%gl_Position = OpVariable %_ptr_Output_v4float Output |
| 1262 |
%out_var_COLOR = OpVariable %_ptr_Output_v4float Output |
| 1263 |
%85 = OpExtInst %void %1 DebugOperation Deref |
| 1264 |
%81 = OpExtInst %void %1 DebugInfoNone |
| 1265 |
%52 = OpExtInst %void %1 DebugExpression |
| 1266 |
%40 = OpExtInst %void %1 DebugTypeBasic %8 %uint_32 Float |
| 1267 |
%41 = OpExtInst %void %1 DebugTypeVector %40 4 |
| 1268 |
%42 = OpExtInst %void %1 DebugSource %7 |
| 1269 |
%43 = OpExtInst %void %1 DebugCompilationUnit 1 4 %42 HLSL |
| 1270 |
%44 = OpExtInst %void %1 DebugTypeComposite %9 Structure %42 1 8 %43 %9 %uint_256 FlagIsProtected|FlagIsPrivate %45 %46 |
| 1271 |
%46 = OpExtInst %void %1 DebugTypeMember %10 %41 %42 3 10 %44 %uint_128 %uint_128 FlagIsProtected|FlagIsPrivate |
| 1272 |
%45 = OpExtInst %void %1 DebugTypeMember %11 %41 %42 2 10 %44 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate |
| 1273 |
%47 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %44 %41 %41 |
| 1274 |
%48 = OpExtInst %void %1 DebugFunction %12 %47 %42 6 1 %43 %13 FlagIsProtected|FlagIsPrivate 7 %81 |
| 1275 |
%49 = OpExtInst %void %1 DebugLexicalBlock %42 7 38 %48 |
| 1276 |
%50 = OpExtInst %void %1 DebugLocalVariable %14 %44 %42 8 13 %49 FlagIsLocal |
| 1277 |
%84 = OpExtInst %void %1 DebugExpression %85 |
| 1278 |
%main = OpFunction %void None %36 |
| 1279 |
%54 = OpLabel |
| 1280 |
%91 = OpExtInst %void %1 DebugScope %49 |
| 1281 |
OpLine %7 7 23 |
| 1282 |
%83 = OpVariable %_ptr_Function_v4float Function |
| 1283 |
OpLine %7 8 13 |
| 1284 |
%87 = OpExtInst %void %1 DebugValue %50 %83 %84 %int_1 |
| 1285 |
OpLine %7 7 23 |
| 1286 |
%82 = OpVariable %_ptr_Function_v4float Function |
| 1287 |
OpLine %7 8 13 |
| 1288 |
%86 = OpExtInst %void %1 DebugValue %50 %82 %84 %int_0 |
| 1289 |
OpNoLine |
| 1290 |
%92 = OpExtInst %void %1 DebugNoScope |
| 1291 |
%55 = OpLoad %v4float %in_var_POSITION |
| 1292 |
%56 = OpLoad %v4float %in_var_COLOR |
| 1293 |
;CHECK: [[pos:%\w+]] = OpLoad %v4float %in_var_POSITION |
| 1294 |
;CHECK: [[color:%\w+]] = OpLoad %v4float %in_var_COLOR |
| 1295 |
|
| 1296 |
%94 = OpExtInst %void %1 DebugScope %49 |
| 1297 |
OpLine %7 9 3 |
| 1298 |
OpStore %82 %55 |
| 1299 |
;CHECK: OpLine [[file:%\w+]] 9 3 |
| 1300 |
;CHECK: OpStore {{%\w+}} [[pos]] |
| 1301 |
;CHECK: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[vout:%\w+]] [[pos]] [[empty_expr:%\w+]] %int_0 |
| 1302 |
;CHECK: OpLine [[file]] 10 3 |
| 1303 |
;CHECK: OpStore {{%\w+}} [[color]] |
| 1304 |
;CHECK: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[vout]] [[color]] [[empty_expr]] %int_1 |
| 1305 |
|
| 1306 |
OpLine %7 10 3 |
| 1307 |
OpStore %83 %56 |
| 1308 |
OpLine %7 11 10 |
| 1309 |
%90 = OpCompositeConstruct %VS_OUTPUT %55 %56 |
| 1310 |
OpNoLine |
| 1311 |
%95 = OpExtInst %void %1 DebugNoScope |
| 1312 |
%58 = OpCompositeExtract %v4float %90 0 |
| 1313 |
OpStore %gl_Position %58 |
| 1314 |
%59 = OpCompositeExtract %v4float %90 1 |
| 1315 |
OpStore %out_var_COLOR %59 |
| 1316 |
OpReturn |
| 1317 |
OpFunctionEnd |
| 1318 |
)"; |
| 1319 |
|
| 1320 |
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS); |
| 1321 |
SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false); |
| 1322 |
} |
| 1323 |
|
| 1324 |
TEST_F(LocalSingleStoreElimTest, AddDebugValueforStoreOutOfDebugDeclareScope) { |
| 1325 |
const std::string text = R"( |
| 1326 |
OpCapability Shader |
| 1327 |
%1 = OpExtInstImport "OpenCL.DebugInfo.100" |
| 1328 |
OpMemoryModel Logical GLSL450 |
| 1329 |
OpEntryPoint Vertex %main "main" %in_var_POSITION %in_var_COLOR %gl_Position %out_var_COLOR |
| 1330 |
%7 = OpString "simple.hlsl" |
| 1331 |
%8 = OpString "float" |
| 1332 |
%9 = OpString "VS_OUTPUT" |
| 1333 |
%10 = OpString "color" |
| 1334 |
%11 = OpString "pos" |
| 1335 |
%12 = OpString "main" |
| 1336 |
%13 = OpString "" |
| 1337 |
%14 = OpString "vout" |
| 1338 |
OpName %in_var_POSITION "in.var.POSITION" |
| 1339 |
OpName %in_var_COLOR "in.var.COLOR" |
| 1340 |
OpName %out_var_COLOR "out.var.COLOR" |
| 1341 |
OpName %main "main" |
| 1342 |
OpName %VS_OUTPUT "VS_OUTPUT" |
| 1343 |
OpMemberName %VS_OUTPUT 0 "pos" |
| 1344 |
OpMemberName %VS_OUTPUT 1 "color" |
| 1345 |
OpDecorate %gl_Position BuiltIn Position |
| 1346 |
OpDecorate %in_var_POSITION Location 0 |
| 1347 |
OpDecorate %in_var_COLOR Location 1 |
| 1348 |
OpDecorate %out_var_COLOR Location 0 |
| 1349 |
%int = OpTypeInt 32 1 |
| 1350 |
%int_0 = OpConstant %int 0 |
| 1351 |
%int_1 = OpConstant %int 1 |
| 1352 |
%uint = OpTypeInt 32 0 |
| 1353 |
%uint_32 = OpConstant %uint 32 |
| 1354 |
%float = OpTypeFloat 32 |
| 1355 |
%v4float = OpTypeVector %float 4 |
| 1356 |
%_ptr_Input_v4float = OpTypePointer Input %v4float |
| 1357 |
%_ptr_Output_v4float = OpTypePointer Output %v4float |
| 1358 |
%void = OpTypeVoid |
| 1359 |
%uint_256 = OpConstant %uint 256 |
| 1360 |
%uint_128 = OpConstant %uint 128 |
| 1361 |
%uint_0 = OpConstant %uint 0 |
| 1362 |
%36 = OpTypeFunction %void |
| 1363 |
%_ptr_Function_v4float = OpTypePointer Function %v4float |
| 1364 |
%VS_OUTPUT = OpTypeStruct %v4float %v4float |
| 1365 |
%_ptr_Function_VS_OUTPUT = OpTypePointer Function %VS_OUTPUT |
| 1366 |
%in_var_POSITION = OpVariable %_ptr_Input_v4float Input |
| 1367 |
%in_var_COLOR = OpVariable %_ptr_Input_v4float Input |
| 1368 |
%gl_Position = OpVariable %_ptr_Output_v4float Output |
| 1369 |
%out_var_COLOR = OpVariable %_ptr_Output_v4float Output |
| 1370 |
%85 = OpExtInst %void %1 DebugOperation Deref |
| 1371 |
%81 = OpExtInst %void %1 DebugInfoNone |
| 1372 |
%52 = OpExtInst %void %1 DebugExpression |
| 1373 |
%40 = OpExtInst %void %1 DebugTypeBasic %8 %uint_32 Float |
| 1374 |
%41 = OpExtInst %void %1 DebugTypeVector %40 4 |
| 1375 |
%42 = OpExtInst %void %1 DebugSource %7 |
| 1376 |
%43 = OpExtInst %void %1 DebugCompilationUnit 1 4 %42 HLSL |
| 1377 |
%44 = OpExtInst %void %1 DebugTypeComposite %9 Structure %42 1 8 %43 %9 %uint_256 FlagIsProtected|FlagIsPrivate %45 %46 |
| 1378 |
%46 = OpExtInst %void %1 DebugTypeMember %10 %41 %42 3 10 %44 %uint_128 %uint_128 FlagIsProtected|FlagIsPrivate |
| 1379 |
%45 = OpExtInst %void %1 DebugTypeMember %11 %41 %42 2 10 %44 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate |
| 1380 |
%47 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %44 %41 %41 |
| 1381 |
%48 = OpExtInst %void %1 DebugFunction %12 %47 %42 6 1 %43 %13 FlagIsProtected|FlagIsPrivate 7 %81 |
| 1382 |
%49 = OpExtInst %void %1 DebugLexicalBlock %42 7 38 %48 |
| 1383 |
%50 = OpExtInst %void %1 DebugLocalVariable %14 %44 %42 8 13 %49 FlagIsLocal |
| 1384 |
%51 = OpExtInst %void %1 DebugLocalVariable %10 %41 %42 7 23 %48 FlagIsLocal 2 |
| 1385 |
%53 = OpExtInst %void %1 DebugLocalVariable %11 %41 %42 6 23 %48 FlagIsLocal 1 |
| 1386 |
;CHECK: [[dbg_color:%\w+]] = OpExtInst %void {{%\w+}} DebugLocalVariable {{%\w+}} {{%\w+}} {{%\w+}} 7 23 {{%\w+}} FlagIsLocal 2 |
| 1387 |
;CHECK: [[dbg_pos:%\w+]] = OpExtInst %void {{%\w+}} DebugLocalVariable {{%\w+}} {{%\w+}} {{%\w+}} 6 23 {{%\w+}} FlagIsLocal 1 |
| 1388 |
|
| 1389 |
%84 = OpExtInst %void %1 DebugExpression %85 |
| 1390 |
%main = OpFunction %void None %36 |
| 1391 |
%54 = OpLabel |
| 1392 |
%91 = OpExtInst %void %1 DebugScope %49 |
| 1393 |
OpLine %7 7 23 |
| 1394 |
%83 = OpVariable %_ptr_Function_v4float Function |
| 1395 |
OpLine %7 8 13 |
| 1396 |
%87 = OpExtInst %void %1 DebugValue %50 %83 %84 %int_1 |
| 1397 |
OpLine %7 7 23 |
| 1398 |
%82 = OpVariable %_ptr_Function_v4float Function |
| 1399 |
OpLine %7 8 13 |
| 1400 |
%86 = OpExtInst %void %1 DebugValue %50 %82 %84 %int_0 |
| 1401 |
OpNoLine |
| 1402 |
%92 = OpExtInst %void %1 DebugNoScope |
| 1403 |
%param_var_pos = OpVariable %_ptr_Function_v4float Function |
| 1404 |
%param_var_color = OpVariable %_ptr_Function_v4float Function |
| 1405 |
%55 = OpLoad %v4float %in_var_POSITION |
| 1406 |
OpStore %param_var_pos %55 |
| 1407 |
%56 = OpLoad %v4float %in_var_COLOR |
| 1408 |
;CHECK: DebugNoScope |
| 1409 |
;CHECK-NOT: OpLine |
| 1410 |
;CHECK: [[pos:%\w+]] = OpLoad %v4float %in_var_POSITION |
| 1411 |
;CHECK: [[color:%\w+]] = OpLoad %v4float %in_var_COLOR |
| 1412 |
|
| 1413 |
OpStore %param_var_color %56 |
| 1414 |
%93 = OpExtInst %void %1 DebugScope %48 |
| 1415 |
OpLine %7 6 23 |
| 1416 |
%73 = OpExtInst %void %1 DebugDeclare %53 %param_var_pos %52 |
| 1417 |
OpLine %7 7 23 |
| 1418 |
%74 = OpExtInst %void %1 DebugDeclare %51 %param_var_color %52 |
| 1419 |
;CHECK: OpLine [[file:%\w+]] 6 23 |
| 1420 |
;CHECK-NEXT: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[dbg_pos]] [[pos]] [[empty_expr:%\w+]] |
| 1421 |
;CHECK: OpLine [[file]] 7 23 |
| 1422 |
;CHECK-NEXT: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[dbg_color]] [[color]] [[empty_expr]] |
| 1423 |
|
| 1424 |
%94 = OpExtInst %void %1 DebugScope %49 |
| 1425 |
OpLine %7 9 3 |
| 1426 |
OpStore %82 %55 |
| 1427 |
OpLine %7 10 3 |
| 1428 |
OpStore %83 %56 |
| 1429 |
OpLine %7 11 10 |
| 1430 |
%90 = OpCompositeConstruct %VS_OUTPUT %55 %56 |
| 1431 |
OpNoLine |
| 1432 |
%95 = OpExtInst %void %1 DebugNoScope |
| 1433 |
%58 = OpCompositeExtract %v4float %90 0 |
| 1434 |
OpStore %gl_Position %58 |
| 1435 |
%59 = OpCompositeExtract %v4float %90 1 |
| 1436 |
OpStore %out_var_COLOR %59 |
| 1437 |
OpReturn |
| 1438 |
OpFunctionEnd |
| 1439 |
)"; |
| 1440 |
|
| 1441 |
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS); |
| 1442 |
SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false); |
| 1443 |
} |
| 1444 |
|
| 1445 |
// TODO(greg-lunarg): Add tests to verify handling of these cases: |
| 1446 |
// |
| 1447 |
// Other types |
| 1448 |
// Others? |
| 1449 |
|
| 1450 |
} // namespace |
| 1451 |
} // namespace opt |
| 1452 |
} // namespace spvtools |