| 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 "source/fuzz/transformation_replace_constant_with_uniform.h" |
| 16 |
|
| 17 |
#include "gtest/gtest.h" |
| 18 |
#include "source/fuzz/fuzzer_util.h" |
| 19 |
#include "source/fuzz/instruction_descriptor.h" |
| 20 |
#include "source/fuzz/uniform_buffer_element_descriptor.h" |
| 21 |
#include "test/fuzz/fuzz_test_util.h" |
| 22 |
|
| 23 |
namespace spvtools { |
| 24 |
namespace fuzz { |
| 25 |
namespace { |
| 26 |
|
| 27 |
bool AddFactHelper( |
| 28 |
TransformationContext* transformation_context, uint32_t word, |
| 29 |
const protobufs::UniformBufferElementDescriptor& descriptor) { |
| 30 |
protobufs::FactConstantUniform constant_uniform_fact; |
| 31 |
constant_uniform_fact.add_constant_word(word); |
| 32 |
*constant_uniform_fact.mutable_uniform_buffer_element_descriptor() = |
| 33 |
descriptor; |
| 34 |
protobufs::Fact fact; |
| 35 |
*fact.mutable_constant_uniform_fact() = constant_uniform_fact; |
| 36 |
return transformation_context->GetFactManager()->MaybeAddFact(fact); |
| 37 |
} |
| 38 |
|
| 39 |
TEST(TransformationReplaceConstantWithUniformTest, BasicReplacements) { |
| 40 |
// This test came from the following GLSL: |
| 41 |
// |
| 42 |
// #version 450 |
| 43 |
// |
| 44 |
// uniform blockname { |
| 45 |
// int a; |
| 46 |
// int b; |
| 47 |
// int c; |
| 48 |
// }; |
| 49 |
// |
| 50 |
// void main() |
| 51 |
// { |
| 52 |
// int x; |
| 53 |
// x = 1; |
| 54 |
// x = x + 2; |
| 55 |
// x = 3 + x; |
| 56 |
// } |
| 57 |
|
| 58 |
std::string shader = R"( |
| 59 |
OpCapability Shader |
| 60 |
%1 = OpExtInstImport "GLSL.std.450" |
| 61 |
OpMemoryModel Logical GLSL450 |
| 62 |
OpEntryPoint Fragment %4 "main" |
| 63 |
OpExecutionMode %4 OriginUpperLeft |
| 64 |
OpSource GLSL 450 |
| 65 |
OpName %4 "main" |
| 66 |
OpName %8 "x" |
| 67 |
OpName %16 "blockname" |
| 68 |
OpMemberName %16 0 "a" |
| 69 |
OpMemberName %16 1 "b" |
| 70 |
OpMemberName %16 2 "c" |
| 71 |
OpName %18 "" |
| 72 |
OpMemberDecorate %16 0 Offset 0 |
| 73 |
OpMemberDecorate %16 1 Offset 4 |
| 74 |
OpMemberDecorate %16 2 Offset 8 |
| 75 |
OpDecorate %16 Block |
| 76 |
OpDecorate %18 DescriptorSet 0 |
| 77 |
OpDecorate %18 Binding 0 |
| 78 |
%2 = OpTypeVoid |
| 79 |
%3 = OpTypeFunction %2 |
| 80 |
%6 = OpTypeInt 32 1 |
| 81 |
%7 = OpTypePointer Function %6 |
| 82 |
%50 = OpConstant %6 0 |
| 83 |
%9 = OpConstant %6 1 |
| 84 |
%11 = OpConstant %6 2 |
| 85 |
%14 = OpConstant %6 3 |
| 86 |
%16 = OpTypeStruct %6 %6 %6 |
| 87 |
%17 = OpTypePointer Uniform %16 |
| 88 |
%51 = OpTypePointer Uniform %6 |
| 89 |
%18 = OpVariable %17 Uniform |
| 90 |
%4 = OpFunction %2 None %3 |
| 91 |
%5 = OpLabel |
| 92 |
%8 = OpVariable %7 Function |
| 93 |
OpStore %8 %9 |
| 94 |
%10 = OpLoad %6 %8 |
| 95 |
%12 = OpIAdd %6 %10 %11 |
| 96 |
OpStore %8 %12 |
| 97 |
%13 = OpLoad %6 %8 |
| 98 |
%15 = OpIAdd %6 %14 %13 |
| 99 |
OpStore %8 %15 |
| 100 |
OpReturn |
| 101 |
OpFunctionEnd |
| 102 |
)"; |
| 103 |
|
| 104 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 105 |
const auto consumer = nullptr; |
| 106 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 107 |
spvtools::ValidatorOptions validator_options; |
| 108 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 109 |
kConsoleMessageConsumer)); |
| 110 |
TransformationContext transformation_context( |
| 111 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 112 |
protobufs::UniformBufferElementDescriptor blockname_a = |
| 113 |
MakeUniformBufferElementDescriptor(0, 0, {0}); |
| 114 |
protobufs::UniformBufferElementDescriptor blockname_b = |
| 115 |
MakeUniformBufferElementDescriptor(0, 0, {1}); |
| 116 |
protobufs::UniformBufferElementDescriptor blockname_c = |
| 117 |
MakeUniformBufferElementDescriptor(0, 0, {2}); |
| 118 |
|
| 119 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 1, blockname_a)); |
| 120 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 2, blockname_b)); |
| 121 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 3, blockname_c)); |
| 122 |
|
| 123 |
// The constant ids are 9, 11 and 14, for 1, 2 and 3 respectively. |
| 124 |
protobufs::IdUseDescriptor use_of_9_in_store = MakeIdUseDescriptor( |
| 125 |
9, MakeInstructionDescriptor(8, spv::Op::OpStore, 0), 1); |
| 126 |
protobufs::IdUseDescriptor use_of_11_in_add = MakeIdUseDescriptor( |
| 127 |
11, MakeInstructionDescriptor(12, spv::Op::OpIAdd, 0), 1); |
| 128 |
protobufs::IdUseDescriptor use_of_14_in_add = MakeIdUseDescriptor( |
| 129 |
14, MakeInstructionDescriptor(15, spv::Op::OpIAdd, 0), 0); |
| 130 |
|
| 131 |
// These transformations work: they match the facts. |
| 132 |
auto transformation_use_of_9_in_store = |
| 133 |
TransformationReplaceConstantWithUniform(use_of_9_in_store, blockname_a, |
| 134 |
100, 101); |
| 135 |
ASSERT_TRUE(transformation_use_of_9_in_store.IsApplicable( |
| 136 |
context.get(), transformation_context)); |
| 137 |
auto transformation_use_of_11_in_add = |
| 138 |
TransformationReplaceConstantWithUniform(use_of_11_in_add, blockname_b, |
| 139 |
102, 103); |
| 140 |
ASSERT_TRUE(transformation_use_of_11_in_add.IsApplicable( |
| 141 |
context.get(), transformation_context)); |
| 142 |
auto transformation_use_of_14_in_add = |
| 143 |
TransformationReplaceConstantWithUniform(use_of_14_in_add, blockname_c, |
| 144 |
104, 105); |
| 145 |
ASSERT_TRUE(transformation_use_of_14_in_add.IsApplicable( |
| 146 |
context.get(), transformation_context)); |
| 147 |
|
| 148 |
// The transformations are not applicable if we change which uniforms are |
| 149 |
// applied to which constants. |
| 150 |
ASSERT_FALSE(TransformationReplaceConstantWithUniform(use_of_9_in_store, |
| 151 |
blockname_b, 101, 102) |
| 152 |
.IsApplicable(context.get(), transformation_context)); |
| 153 |
ASSERT_FALSE(TransformationReplaceConstantWithUniform(use_of_11_in_add, |
| 154 |
blockname_c, 101, 102) |
| 155 |
.IsApplicable(context.get(), transformation_context)); |
| 156 |
ASSERT_FALSE(TransformationReplaceConstantWithUniform(use_of_14_in_add, |
| 157 |
blockname_a, 101, 102) |
| 158 |
.IsApplicable(context.get(), transformation_context)); |
| 159 |
|
| 160 |
// The following transformations do not apply because the uniform descriptors |
| 161 |
// are not sensible. |
| 162 |
protobufs::UniformBufferElementDescriptor nonsense_uniform_descriptor1 = |
| 163 |
MakeUniformBufferElementDescriptor(1, 2, {0}); |
| 164 |
protobufs::UniformBufferElementDescriptor nonsense_uniform_descriptor2 = |
| 165 |
MakeUniformBufferElementDescriptor(0, 0, {5}); |
| 166 |
ASSERT_FALSE(TransformationReplaceConstantWithUniform( |
| 167 |
use_of_9_in_store, nonsense_uniform_descriptor1, 101, 102) |
| 168 |
.IsApplicable(context.get(), transformation_context)); |
| 169 |
ASSERT_FALSE(TransformationReplaceConstantWithUniform( |
| 170 |
use_of_9_in_store, nonsense_uniform_descriptor2, 101, 102) |
| 171 |
.IsApplicable(context.get(), transformation_context)); |
| 172 |
|
| 173 |
// The following transformation does not apply because the id descriptor is |
| 174 |
// not sensible. |
| 175 |
protobufs::IdUseDescriptor nonsense_id_use_descriptor = MakeIdUseDescriptor( |
| 176 |
9, MakeInstructionDescriptor(15, spv::Op::OpIAdd, 0), 0); |
| 177 |
ASSERT_FALSE(TransformationReplaceConstantWithUniform( |
| 178 |
nonsense_id_use_descriptor, blockname_a, 101, 102) |
| 179 |
.IsApplicable(context.get(), transformation_context)); |
| 180 |
|
| 181 |
// The following transformations do not apply because the ids are not fresh. |
| 182 |
ASSERT_FALSE(TransformationReplaceConstantWithUniform(use_of_11_in_add, |
| 183 |
blockname_b, 15, 103) |
| 184 |
.IsApplicable(context.get(), transformation_context)); |
| 185 |
ASSERT_FALSE(TransformationReplaceConstantWithUniform(use_of_11_in_add, |
| 186 |
blockname_b, 102, 15) |
| 187 |
.IsApplicable(context.get(), transformation_context)); |
| 188 |
|
| 189 |
// Apply the use of 9 in a store. |
| 190 |
ApplyAndCheckFreshIds(transformation_use_of_9_in_store, context.get(), |
| 191 |
&transformation_context); |
| 192 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 193 |
kConsoleMessageConsumer)); |
| 194 |
std::string after_replacing_use_of_9_in_store = R"( |
| 195 |
OpCapability Shader |
| 196 |
%1 = OpExtInstImport "GLSL.std.450" |
| 197 |
OpMemoryModel Logical GLSL450 |
| 198 |
OpEntryPoint Fragment %4 "main" |
| 199 |
OpExecutionMode %4 OriginUpperLeft |
| 200 |
OpSource GLSL 450 |
| 201 |
OpName %4 "main" |
| 202 |
OpName %8 "x" |
| 203 |
OpName %16 "blockname" |
| 204 |
OpMemberName %16 0 "a" |
| 205 |
OpMemberName %16 1 "b" |
| 206 |
OpMemberName %16 2 "c" |
| 207 |
OpName %18 "" |
| 208 |
OpMemberDecorate %16 0 Offset 0 |
| 209 |
OpMemberDecorate %16 1 Offset 4 |
| 210 |
OpMemberDecorate %16 2 Offset 8 |
| 211 |
OpDecorate %16 Block |
| 212 |
OpDecorate %18 DescriptorSet 0 |
| 213 |
OpDecorate %18 Binding 0 |
| 214 |
%2 = OpTypeVoid |
| 215 |
%3 = OpTypeFunction %2 |
| 216 |
%6 = OpTypeInt 32 1 |
| 217 |
%7 = OpTypePointer Function %6 |
| 218 |
%50 = OpConstant %6 0 |
| 219 |
%9 = OpConstant %6 1 |
| 220 |
%11 = OpConstant %6 2 |
| 221 |
%14 = OpConstant %6 3 |
| 222 |
%16 = OpTypeStruct %6 %6 %6 |
| 223 |
%17 = OpTypePointer Uniform %16 |
| 224 |
%51 = OpTypePointer Uniform %6 |
| 225 |
%18 = OpVariable %17 Uniform |
| 226 |
%4 = OpFunction %2 None %3 |
| 227 |
%5 = OpLabel |
| 228 |
%8 = OpVariable %7 Function |
| 229 |
%100 = OpAccessChain %51 %18 %50 |
| 230 |
%101 = OpLoad %6 %100 |
| 231 |
OpStore %8 %101 |
| 232 |
%10 = OpLoad %6 %8 |
| 233 |
%12 = OpIAdd %6 %10 %11 |
| 234 |
OpStore %8 %12 |
| 235 |
%13 = OpLoad %6 %8 |
| 236 |
%15 = OpIAdd %6 %14 %13 |
| 237 |
OpStore %8 %15 |
| 238 |
OpReturn |
| 239 |
OpFunctionEnd |
| 240 |
)"; |
| 241 |
ASSERT_TRUE(IsEqual(env, after_replacing_use_of_9_in_store, context.get())); |
| 242 |
|
| 243 |
ASSERT_TRUE(transformation_use_of_11_in_add.IsApplicable( |
| 244 |
context.get(), transformation_context)); |
| 245 |
// Apply the use of 11 in an add. |
| 246 |
ApplyAndCheckFreshIds(transformation_use_of_11_in_add, context.get(), |
| 247 |
&transformation_context); |
| 248 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 249 |
kConsoleMessageConsumer)); |
| 250 |
std::string after_replacing_use_of_11_in_add = R"( |
| 251 |
OpCapability Shader |
| 252 |
%1 = OpExtInstImport "GLSL.std.450" |
| 253 |
OpMemoryModel Logical GLSL450 |
| 254 |
OpEntryPoint Fragment %4 "main" |
| 255 |
OpExecutionMode %4 OriginUpperLeft |
| 256 |
OpSource GLSL 450 |
| 257 |
OpName %4 "main" |
| 258 |
OpName %8 "x" |
| 259 |
OpName %16 "blockname" |
| 260 |
OpMemberName %16 0 "a" |
| 261 |
OpMemberName %16 1 "b" |
| 262 |
OpMemberName %16 2 "c" |
| 263 |
OpName %18 "" |
| 264 |
OpMemberDecorate %16 0 Offset 0 |
| 265 |
OpMemberDecorate %16 1 Offset 4 |
| 266 |
OpMemberDecorate %16 2 Offset 8 |
| 267 |
OpDecorate %16 Block |
| 268 |
OpDecorate %18 DescriptorSet 0 |
| 269 |
OpDecorate %18 Binding 0 |
| 270 |
%2 = OpTypeVoid |
| 271 |
%3 = OpTypeFunction %2 |
| 272 |
%6 = OpTypeInt 32 1 |
| 273 |
%7 = OpTypePointer Function %6 |
| 274 |
%50 = OpConstant %6 0 |
| 275 |
%9 = OpConstant %6 1 |
| 276 |
%11 = OpConstant %6 2 |
| 277 |
%14 = OpConstant %6 3 |
| 278 |
%16 = OpTypeStruct %6 %6 %6 |
| 279 |
%17 = OpTypePointer Uniform %16 |
| 280 |
%51 = OpTypePointer Uniform %6 |
| 281 |
%18 = OpVariable %17 Uniform |
| 282 |
%4 = OpFunction %2 None %3 |
| 283 |
%5 = OpLabel |
| 284 |
%8 = OpVariable %7 Function |
| 285 |
%100 = OpAccessChain %51 %18 %50 |
| 286 |
%101 = OpLoad %6 %100 |
| 287 |
OpStore %8 %101 |
| 288 |
%10 = OpLoad %6 %8 |
| 289 |
%102 = OpAccessChain %51 %18 %9 |
| 290 |
%103 = OpLoad %6 %102 |
| 291 |
%12 = OpIAdd %6 %10 %103 |
| 292 |
OpStore %8 %12 |
| 293 |
%13 = OpLoad %6 %8 |
| 294 |
%15 = OpIAdd %6 %14 %13 |
| 295 |
OpStore %8 %15 |
| 296 |
OpReturn |
| 297 |
OpFunctionEnd |
| 298 |
)"; |
| 299 |
ASSERT_TRUE(IsEqual(env, after_replacing_use_of_11_in_add, context.get())); |
| 300 |
|
| 301 |
ASSERT_TRUE(transformation_use_of_14_in_add.IsApplicable( |
| 302 |
context.get(), transformation_context)); |
| 303 |
// Apply the use of 15 in an add. |
| 304 |
ApplyAndCheckFreshIds(transformation_use_of_14_in_add, context.get(), |
| 305 |
&transformation_context); |
| 306 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 307 |
kConsoleMessageConsumer)); |
| 308 |
std::string after_replacing_use_of_14_in_add = R"( |
| 309 |
OpCapability Shader |
| 310 |
%1 = OpExtInstImport "GLSL.std.450" |
| 311 |
OpMemoryModel Logical GLSL450 |
| 312 |
OpEntryPoint Fragment %4 "main" |
| 313 |
OpExecutionMode %4 OriginUpperLeft |
| 314 |
OpSource GLSL 450 |
| 315 |
OpName %4 "main" |
| 316 |
OpName %8 "x" |
| 317 |
OpName %16 "blockname" |
| 318 |
OpMemberName %16 0 "a" |
| 319 |
OpMemberName %16 1 "b" |
| 320 |
OpMemberName %16 2 "c" |
| 321 |
OpName %18 "" |
| 322 |
OpMemberDecorate %16 0 Offset 0 |
| 323 |
OpMemberDecorate %16 1 Offset 4 |
| 324 |
OpMemberDecorate %16 2 Offset 8 |
| 325 |
OpDecorate %16 Block |
| 326 |
OpDecorate %18 DescriptorSet 0 |
| 327 |
OpDecorate %18 Binding 0 |
| 328 |
%2 = OpTypeVoid |
| 329 |
%3 = OpTypeFunction %2 |
| 330 |
%6 = OpTypeInt 32 1 |
| 331 |
%7 = OpTypePointer Function %6 |
| 332 |
%50 = OpConstant %6 0 |
| 333 |
%9 = OpConstant %6 1 |
| 334 |
%11 = OpConstant %6 2 |
| 335 |
%14 = OpConstant %6 3 |
| 336 |
%16 = OpTypeStruct %6 %6 %6 |
| 337 |
%17 = OpTypePointer Uniform %16 |
| 338 |
%51 = OpTypePointer Uniform %6 |
| 339 |
%18 = OpVariable %17 Uniform |
| 340 |
%4 = OpFunction %2 None %3 |
| 341 |
%5 = OpLabel |
| 342 |
%8 = OpVariable %7 Function |
| 343 |
%100 = OpAccessChain %51 %18 %50 |
| 344 |
%101 = OpLoad %6 %100 |
| 345 |
OpStore %8 %101 |
| 346 |
%10 = OpLoad %6 %8 |
| 347 |
%102 = OpAccessChain %51 %18 %9 |
| 348 |
%103 = OpLoad %6 %102 |
| 349 |
%12 = OpIAdd %6 %10 %103 |
| 350 |
OpStore %8 %12 |
| 351 |
%13 = OpLoad %6 %8 |
| 352 |
%104 = OpAccessChain %51 %18 %11 |
| 353 |
%105 = OpLoad %6 %104 |
| 354 |
%15 = OpIAdd %6 %105 %13 |
| 355 |
OpStore %8 %15 |
| 356 |
OpReturn |
| 357 |
OpFunctionEnd |
| 358 |
)"; |
| 359 |
ASSERT_TRUE(IsEqual(env, after_replacing_use_of_14_in_add, context.get())); |
| 360 |
} |
| 361 |
|
| 362 |
TEST(TransformationReplaceConstantWithUniformTest, NestedStruct) { |
| 363 |
// This test came from the following GLSL: |
| 364 |
// |
| 365 |
// #version 450 |
| 366 |
// |
| 367 |
// struct U { |
| 368 |
// int x; // == 4 |
| 369 |
// }; |
| 370 |
// |
| 371 |
// struct T { |
| 372 |
// int x; // == 3 |
| 373 |
// U y; |
| 374 |
// }; |
| 375 |
// |
| 376 |
// struct S { |
| 377 |
// T x; |
| 378 |
// int y; // == 2 |
| 379 |
// }; |
| 380 |
// |
| 381 |
// uniform blockname { |
| 382 |
// int x; // == 1 |
| 383 |
// S y; |
| 384 |
// }; |
| 385 |
// |
| 386 |
// void foo(int a) { } |
| 387 |
// |
| 388 |
// void main() |
| 389 |
// { |
| 390 |
// int x; |
| 391 |
// x = 1; |
| 392 |
// x = x + 2; |
| 393 |
// x = 3 + x; |
| 394 |
// foo(4); |
| 395 |
// } |
| 396 |
|
| 397 |
std::string shader = R"( |
| 398 |
OpCapability Shader |
| 399 |
%1 = OpExtInstImport "GLSL.std.450" |
| 400 |
OpMemoryModel Logical GLSL450 |
| 401 |
OpEntryPoint Fragment %4 "main" |
| 402 |
OpExecutionMode %4 OriginUpperLeft |
| 403 |
OpSource GLSL 450 |
| 404 |
OpName %4 "main" |
| 405 |
OpName %10 "foo(i1;" |
| 406 |
OpName %9 "a" |
| 407 |
OpName %12 "x" |
| 408 |
OpName %21 "param" |
| 409 |
OpName %23 "U" |
| 410 |
OpMemberName %23 0 "x" |
| 411 |
OpName %24 "T" |
| 412 |
OpMemberName %24 0 "x" |
| 413 |
OpMemberName %24 1 "y" |
| 414 |
OpName %25 "S" |
| 415 |
OpMemberName %25 0 "x" |
| 416 |
OpMemberName %25 1 "y" |
| 417 |
OpName %26 "blockname" |
| 418 |
OpMemberName %26 0 "x" |
| 419 |
OpMemberName %26 1 "y" |
| 420 |
OpName %28 "" |
| 421 |
OpMemberDecorate %23 0 Offset 0 |
| 422 |
OpMemberDecorate %24 0 Offset 0 |
| 423 |
OpMemberDecorate %24 1 Offset 16 |
| 424 |
OpMemberDecorate %25 0 Offset 0 |
| 425 |
OpMemberDecorate %25 1 Offset 32 |
| 426 |
OpMemberDecorate %26 0 Offset 0 |
| 427 |
OpMemberDecorate %26 1 Offset 16 |
| 428 |
OpDecorate %26 Block |
| 429 |
OpDecorate %28 DescriptorSet 0 |
| 430 |
OpDecorate %28 Binding 0 |
| 431 |
%2 = OpTypeVoid |
| 432 |
%3 = OpTypeFunction %2 |
| 433 |
%6 = OpTypeInt 32 1 |
| 434 |
%7 = OpTypePointer Function %6 |
| 435 |
%8 = OpTypeFunction %2 %7 |
| 436 |
%50 = OpConstant %6 0 |
| 437 |
%13 = OpConstant %6 1 |
| 438 |
%15 = OpConstant %6 2 |
| 439 |
%17 = OpConstant %6 3 |
| 440 |
%20 = OpConstant %6 4 |
| 441 |
%23 = OpTypeStruct %6 |
| 442 |
%24 = OpTypeStruct %6 %23 |
| 443 |
%25 = OpTypeStruct %24 %6 |
| 444 |
%26 = OpTypeStruct %6 %25 |
| 445 |
%27 = OpTypePointer Uniform %26 |
| 446 |
%51 = OpTypePointer Uniform %6 |
| 447 |
%28 = OpVariable %27 Uniform |
| 448 |
%4 = OpFunction %2 None %3 |
| 449 |
%5 = OpLabel |
| 450 |
%12 = OpVariable %7 Function |
| 451 |
%21 = OpVariable %7 Function |
| 452 |
OpStore %12 %13 |
| 453 |
%14 = OpLoad %6 %12 |
| 454 |
%16 = OpIAdd %6 %14 %15 |
| 455 |
OpStore %12 %16 |
| 456 |
%18 = OpLoad %6 %12 |
| 457 |
%19 = OpIAdd %6 %17 %18 |
| 458 |
OpStore %12 %19 |
| 459 |
OpStore %21 %20 |
| 460 |
%22 = OpFunctionCall %2 %10 %21 |
| 461 |
OpReturn |
| 462 |
OpFunctionEnd |
| 463 |
%10 = OpFunction %2 None %8 |
| 464 |
%9 = OpFunctionParameter %7 |
| 465 |
%11 = OpLabel |
| 466 |
OpReturn |
| 467 |
OpFunctionEnd |
| 468 |
)"; |
| 469 |
|
| 470 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 471 |
const auto consumer = nullptr; |
| 472 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 473 |
spvtools::ValidatorOptions validator_options; |
| 474 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 475 |
kConsoleMessageConsumer)); |
| 476 |
TransformationContext transformation_context( |
| 477 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 478 |
protobufs::UniformBufferElementDescriptor blockname_1 = |
| 479 |
MakeUniformBufferElementDescriptor(0, 0, {0}); |
| 480 |
protobufs::UniformBufferElementDescriptor blockname_2 = |
| 481 |
MakeUniformBufferElementDescriptor(0, 0, {1, 1}); |
| 482 |
protobufs::UniformBufferElementDescriptor blockname_3 = |
| 483 |
MakeUniformBufferElementDescriptor(0, 0, {1, 0, 0}); |
| 484 |
protobufs::UniformBufferElementDescriptor blockname_4 = |
| 485 |
MakeUniformBufferElementDescriptor(0, 0, {1, 0, 1, 0}); |
| 486 |
|
| 487 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 1, blockname_1)); |
| 488 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 2, blockname_2)); |
| 489 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 3, blockname_3)); |
| 490 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 4, blockname_4)); |
| 491 |
|
| 492 |
// The constant ids are 13, 15, 17 and 20, for 1, 2, 3 and 4 respectively. |
| 493 |
protobufs::IdUseDescriptor use_of_13_in_store = MakeIdUseDescriptor( |
| 494 |
13, MakeInstructionDescriptor(21, spv::Op::OpStore, 0), 1); |
| 495 |
protobufs::IdUseDescriptor use_of_15_in_add = MakeIdUseDescriptor( |
| 496 |
15, MakeInstructionDescriptor(16, spv::Op::OpIAdd, 0), 1); |
| 497 |
protobufs::IdUseDescriptor use_of_17_in_add = MakeIdUseDescriptor( |
| 498 |
17, MakeInstructionDescriptor(19, spv::Op::OpIAdd, 0), 0); |
| 499 |
protobufs::IdUseDescriptor use_of_20_in_store = MakeIdUseDescriptor( |
| 500 |
20, MakeInstructionDescriptor(19, spv::Op::OpStore, 1), 1); |
| 501 |
|
| 502 |
// These transformations work: they match the facts. |
| 503 |
auto transformation_use_of_13_in_store = |
| 504 |
TransformationReplaceConstantWithUniform(use_of_13_in_store, blockname_1, |
| 505 |
100, 101); |
| 506 |
ASSERT_TRUE(transformation_use_of_13_in_store.IsApplicable( |
| 507 |
context.get(), transformation_context)); |
| 508 |
auto transformation_use_of_15_in_add = |
| 509 |
TransformationReplaceConstantWithUniform(use_of_15_in_add, blockname_2, |
| 510 |
102, 103); |
| 511 |
ASSERT_TRUE(transformation_use_of_15_in_add.IsApplicable( |
| 512 |
context.get(), transformation_context)); |
| 513 |
auto transformation_use_of_17_in_add = |
| 514 |
TransformationReplaceConstantWithUniform(use_of_17_in_add, blockname_3, |
| 515 |
104, 105); |
| 516 |
ASSERT_TRUE(transformation_use_of_17_in_add.IsApplicable( |
| 517 |
context.get(), transformation_context)); |
| 518 |
auto transformation_use_of_20_in_store = |
| 519 |
TransformationReplaceConstantWithUniform(use_of_20_in_store, blockname_4, |
| 520 |
106, 107); |
| 521 |
ASSERT_TRUE(transformation_use_of_20_in_store.IsApplicable( |
| 522 |
context.get(), transformation_context)); |
| 523 |
|
| 524 |
ASSERT_TRUE(transformation_use_of_13_in_store.IsApplicable( |
| 525 |
context.get(), transformation_context)); |
| 526 |
ASSERT_TRUE(transformation_use_of_15_in_add.IsApplicable( |
| 527 |
context.get(), transformation_context)); |
| 528 |
ASSERT_TRUE(transformation_use_of_17_in_add.IsApplicable( |
| 529 |
context.get(), transformation_context)); |
| 530 |
ASSERT_TRUE(transformation_use_of_20_in_store.IsApplicable( |
| 531 |
context.get(), transformation_context)); |
| 532 |
|
| 533 |
ApplyAndCheckFreshIds(transformation_use_of_13_in_store, context.get(), |
| 534 |
&transformation_context); |
| 535 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 536 |
kConsoleMessageConsumer)); |
| 537 |
ASSERT_FALSE(transformation_use_of_13_in_store.IsApplicable( |
| 538 |
context.get(), transformation_context)); |
| 539 |
ASSERT_TRUE(transformation_use_of_15_in_add.IsApplicable( |
| 540 |
context.get(), transformation_context)); |
| 541 |
ASSERT_TRUE(transformation_use_of_17_in_add.IsApplicable( |
| 542 |
context.get(), transformation_context)); |
| 543 |
ASSERT_TRUE(transformation_use_of_20_in_store.IsApplicable( |
| 544 |
context.get(), transformation_context)); |
| 545 |
|
| 546 |
ApplyAndCheckFreshIds(transformation_use_of_15_in_add, context.get(), |
| 547 |
&transformation_context); |
| 548 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 549 |
kConsoleMessageConsumer)); |
| 550 |
ASSERT_FALSE(transformation_use_of_13_in_store.IsApplicable( |
| 551 |
context.get(), transformation_context)); |
| 552 |
ASSERT_FALSE(transformation_use_of_15_in_add.IsApplicable( |
| 553 |
context.get(), transformation_context)); |
| 554 |
ASSERT_TRUE(transformation_use_of_17_in_add.IsApplicable( |
| 555 |
context.get(), transformation_context)); |
| 556 |
ASSERT_TRUE(transformation_use_of_20_in_store.IsApplicable( |
| 557 |
context.get(), transformation_context)); |
| 558 |
|
| 559 |
ApplyAndCheckFreshIds(transformation_use_of_17_in_add, context.get(), |
| 560 |
&transformation_context); |
| 561 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 562 |
kConsoleMessageConsumer)); |
| 563 |
ASSERT_FALSE(transformation_use_of_13_in_store.IsApplicable( |
| 564 |
context.get(), transformation_context)); |
| 565 |
ASSERT_FALSE(transformation_use_of_15_in_add.IsApplicable( |
| 566 |
context.get(), transformation_context)); |
| 567 |
ASSERT_FALSE(transformation_use_of_17_in_add.IsApplicable( |
| 568 |
context.get(), transformation_context)); |
| 569 |
ASSERT_TRUE(transformation_use_of_20_in_store.IsApplicable( |
| 570 |
context.get(), transformation_context)); |
| 571 |
|
| 572 |
ApplyAndCheckFreshIds(transformation_use_of_20_in_store, context.get(), |
| 573 |
&transformation_context); |
| 574 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 575 |
kConsoleMessageConsumer)); |
| 576 |
ASSERT_FALSE(transformation_use_of_13_in_store.IsApplicable( |
| 577 |
context.get(), transformation_context)); |
| 578 |
ASSERT_FALSE(transformation_use_of_15_in_add.IsApplicable( |
| 579 |
context.get(), transformation_context)); |
| 580 |
ASSERT_FALSE(transformation_use_of_17_in_add.IsApplicable( |
| 581 |
context.get(), transformation_context)); |
| 582 |
ASSERT_FALSE(transformation_use_of_20_in_store.IsApplicable( |
| 583 |
context.get(), transformation_context)); |
| 584 |
|
| 585 |
std::string after = R"( |
| 586 |
OpCapability Shader |
| 587 |
%1 = OpExtInstImport "GLSL.std.450" |
| 588 |
OpMemoryModel Logical GLSL450 |
| 589 |
OpEntryPoint Fragment %4 "main" |
| 590 |
OpExecutionMode %4 OriginUpperLeft |
| 591 |
OpSource GLSL 450 |
| 592 |
OpName %4 "main" |
| 593 |
OpName %10 "foo(i1;" |
| 594 |
OpName %9 "a" |
| 595 |
OpName %12 "x" |
| 596 |
OpName %21 "param" |
| 597 |
OpName %23 "U" |
| 598 |
OpMemberName %23 0 "x" |
| 599 |
OpName %24 "T" |
| 600 |
OpMemberName %24 0 "x" |
| 601 |
OpMemberName %24 1 "y" |
| 602 |
OpName %25 "S" |
| 603 |
OpMemberName %25 0 "x" |
| 604 |
OpMemberName %25 1 "y" |
| 605 |
OpName %26 "blockname" |
| 606 |
OpMemberName %26 0 "x" |
| 607 |
OpMemberName %26 1 "y" |
| 608 |
OpName %28 "" |
| 609 |
OpMemberDecorate %23 0 Offset 0 |
| 610 |
OpMemberDecorate %24 0 Offset 0 |
| 611 |
OpMemberDecorate %24 1 Offset 16 |
| 612 |
OpMemberDecorate %25 0 Offset 0 |
| 613 |
OpMemberDecorate %25 1 Offset 32 |
| 614 |
OpMemberDecorate %26 0 Offset 0 |
| 615 |
OpMemberDecorate %26 1 Offset 16 |
| 616 |
OpDecorate %26 Block |
| 617 |
OpDecorate %28 DescriptorSet 0 |
| 618 |
OpDecorate %28 Binding 0 |
| 619 |
%2 = OpTypeVoid |
| 620 |
%3 = OpTypeFunction %2 |
| 621 |
%6 = OpTypeInt 32 1 |
| 622 |
%7 = OpTypePointer Function %6 |
| 623 |
%8 = OpTypeFunction %2 %7 |
| 624 |
%50 = OpConstant %6 0 |
| 625 |
%13 = OpConstant %6 1 |
| 626 |
%15 = OpConstant %6 2 |
| 627 |
%17 = OpConstant %6 3 |
| 628 |
%20 = OpConstant %6 4 |
| 629 |
%23 = OpTypeStruct %6 |
| 630 |
%24 = OpTypeStruct %6 %23 |
| 631 |
%25 = OpTypeStruct %24 %6 |
| 632 |
%26 = OpTypeStruct %6 %25 |
| 633 |
%27 = OpTypePointer Uniform %26 |
| 634 |
%51 = OpTypePointer Uniform %6 |
| 635 |
%28 = OpVariable %27 Uniform |
| 636 |
%4 = OpFunction %2 None %3 |
| 637 |
%5 = OpLabel |
| 638 |
%12 = OpVariable %7 Function |
| 639 |
%21 = OpVariable %7 Function |
| 640 |
%100 = OpAccessChain %51 %28 %50 |
| 641 |
%101 = OpLoad %6 %100 |
| 642 |
OpStore %12 %101 |
| 643 |
%14 = OpLoad %6 %12 |
| 644 |
%102 = OpAccessChain %51 %28 %13 %13 |
| 645 |
%103 = OpLoad %6 %102 |
| 646 |
%16 = OpIAdd %6 %14 %103 |
| 647 |
OpStore %12 %16 |
| 648 |
%18 = OpLoad %6 %12 |
| 649 |
%104 = OpAccessChain %51 %28 %13 %50 %50 |
| 650 |
%105 = OpLoad %6 %104 |
| 651 |
%19 = OpIAdd %6 %105 %18 |
| 652 |
OpStore %12 %19 |
| 653 |
%106 = OpAccessChain %51 %28 %13 %50 %13 %50 |
| 654 |
%107 = OpLoad %6 %106 |
| 655 |
OpStore %21 %107 |
| 656 |
%22 = OpFunctionCall %2 %10 %21 |
| 657 |
OpReturn |
| 658 |
OpFunctionEnd |
| 659 |
%10 = OpFunction %2 None %8 |
| 660 |
%9 = OpFunctionParameter %7 |
| 661 |
%11 = OpLabel |
| 662 |
OpReturn |
| 663 |
OpFunctionEnd |
| 664 |
)"; |
| 665 |
ASSERT_TRUE(IsEqual(env, after, context.get())); |
| 666 |
} |
| 667 |
|
| 668 |
TEST(TransformationReplaceConstantWithUniformTest, NoUniformIntPointerPresent) { |
| 669 |
// This test came from the following GLSL: |
| 670 |
// |
| 671 |
// #version 450 |
| 672 |
// |
| 673 |
// uniform blockname { |
| 674 |
// int x; // == 0 |
| 675 |
// }; |
| 676 |
// |
| 677 |
// void main() |
| 678 |
// { |
| 679 |
// int a; |
| 680 |
// a = 0; |
| 681 |
// } |
| 682 |
|
| 683 |
std::string shader = R"( |
| 684 |
OpCapability Shader |
| 685 |
%1 = OpExtInstImport "GLSL.std.450" |
| 686 |
OpMemoryModel Logical GLSL450 |
| 687 |
OpEntryPoint Fragment %4 "main" |
| 688 |
OpExecutionMode %4 OriginUpperLeft |
| 689 |
OpSource GLSL 450 |
| 690 |
OpName %4 "main" |
| 691 |
OpName %8 "a" |
| 692 |
OpName %10 "blockname" |
| 693 |
OpMemberName %10 0 "x" |
| 694 |
OpName %12 "" |
| 695 |
OpMemberDecorate %10 0 Offset 0 |
| 696 |
OpDecorate %10 Block |
| 697 |
OpDecorate %12 DescriptorSet 0 |
| 698 |
OpDecorate %12 Binding 0 |
| 699 |
%2 = OpTypeVoid |
| 700 |
%3 = OpTypeFunction %2 |
| 701 |
%6 = OpTypeInt 32 1 |
| 702 |
%7 = OpTypePointer Function %6 |
| 703 |
%9 = OpConstant %6 0 |
| 704 |
%10 = OpTypeStruct %6 |
| 705 |
%11 = OpTypePointer Uniform %10 |
| 706 |
%12 = OpVariable %11 Uniform |
| 707 |
%4 = OpFunction %2 None %3 |
| 708 |
%5 = OpLabel |
| 709 |
%8 = OpVariable %7 Function |
| 710 |
OpStore %8 %9 |
| 711 |
OpReturn |
| 712 |
OpFunctionEnd |
| 713 |
)"; |
| 714 |
|
| 715 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 716 |
const auto consumer = nullptr; |
| 717 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 718 |
spvtools::ValidatorOptions validator_options; |
| 719 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 720 |
kConsoleMessageConsumer)); |
| 721 |
TransformationContext transformation_context( |
| 722 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 723 |
protobufs::UniformBufferElementDescriptor blockname_0 = |
| 724 |
MakeUniformBufferElementDescriptor(0, 0, {0}); |
| 725 |
|
| 726 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 0, blockname_0)); |
| 727 |
|
| 728 |
// The constant id is 9 for 0. |
| 729 |
protobufs::IdUseDescriptor use_of_9_in_store = MakeIdUseDescriptor( |
| 730 |
9, MakeInstructionDescriptor(8, spv::Op::OpStore, 0), 1); |
| 731 |
|
| 732 |
// This transformation is not available because no uniform pointer to integer |
| 733 |
// type is present: |
| 734 |
ASSERT_FALSE(TransformationReplaceConstantWithUniform(use_of_9_in_store, |
| 735 |
blockname_0, 100, 101) |
| 736 |
.IsApplicable(context.get(), transformation_context)); |
| 737 |
} |
| 738 |
|
| 739 |
TEST(TransformationReplaceConstantWithUniformTest, NoConstantPresentForIndex) { |
| 740 |
// This test came from the following GLSL: |
| 741 |
// |
| 742 |
// #version 450 |
| 743 |
// |
| 744 |
// uniform blockname { |
| 745 |
// int x; // == 0 |
| 746 |
// int y; // == 9 |
| 747 |
// }; |
| 748 |
// |
| 749 |
// void main() |
| 750 |
// { |
| 751 |
// int a; |
| 752 |
// a = 9; |
| 753 |
// } |
| 754 |
|
| 755 |
std::string shader = R"( |
| 756 |
OpCapability Shader |
| 757 |
%1 = OpExtInstImport "GLSL.std.450" |
| 758 |
OpMemoryModel Logical GLSL450 |
| 759 |
OpEntryPoint Fragment %4 "main" |
| 760 |
OpExecutionMode %4 OriginUpperLeft |
| 761 |
OpSource GLSL 450 |
| 762 |
OpName %4 "main" |
| 763 |
OpName %8 "a" |
| 764 |
OpName %10 "blockname" |
| 765 |
OpMemberName %10 0 "x" |
| 766 |
OpMemberName %10 1 "y" |
| 767 |
OpName %12 "" |
| 768 |
OpMemberDecorate %10 0 Offset 0 |
| 769 |
OpMemberDecorate %10 1 Offset 4 |
| 770 |
OpDecorate %10 Block |
| 771 |
OpDecorate %12 DescriptorSet 0 |
| 772 |
OpDecorate %12 Binding 0 |
| 773 |
%2 = OpTypeVoid |
| 774 |
%3 = OpTypeFunction %2 |
| 775 |
%6 = OpTypeInt 32 1 |
| 776 |
%7 = OpTypePointer Function %6 |
| 777 |
%9 = OpConstant %6 9 |
| 778 |
%10 = OpTypeStruct %6 %6 |
| 779 |
%11 = OpTypePointer Uniform %10 |
| 780 |
%50 = OpTypePointer Uniform %6 |
| 781 |
%12 = OpVariable %11 Uniform |
| 782 |
%4 = OpFunction %2 None %3 |
| 783 |
%5 = OpLabel |
| 784 |
%8 = OpVariable %7 Function |
| 785 |
OpStore %8 %9 |
| 786 |
OpReturn |
| 787 |
OpFunctionEnd |
| 788 |
)"; |
| 789 |
|
| 790 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 791 |
const auto consumer = nullptr; |
| 792 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 793 |
spvtools::ValidatorOptions validator_options; |
| 794 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 795 |
kConsoleMessageConsumer)); |
| 796 |
TransformationContext transformation_context( |
| 797 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 798 |
protobufs::UniformBufferElementDescriptor blockname_0 = |
| 799 |
MakeUniformBufferElementDescriptor(0, 0, {0}); |
| 800 |
protobufs::UniformBufferElementDescriptor blockname_9 = |
| 801 |
MakeUniformBufferElementDescriptor(0, 0, {1}); |
| 802 |
|
| 803 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 9, blockname_9)); |
| 804 |
|
| 805 |
// The constant id is 9 for 9. |
| 806 |
protobufs::IdUseDescriptor use_of_9_in_store = MakeIdUseDescriptor( |
| 807 |
9, MakeInstructionDescriptor(8, spv::Op::OpStore, 0), 1); |
| 808 |
|
| 809 |
// This transformation is not available because no constant is present for the |
| 810 |
// index 1 required to index into the uniform buffer: |
| 811 |
ASSERT_FALSE(TransformationReplaceConstantWithUniform(use_of_9_in_store, |
| 812 |
blockname_9, 100, 101) |
| 813 |
.IsApplicable(context.get(), transformation_context)); |
| 814 |
} |
| 815 |
|
| 816 |
TEST(TransformationReplaceConstantWithUniformTest, |
| 817 |
NoIntTypePresentToEnableIndexing) { |
| 818 |
// This test came from the following GLSL: |
| 819 |
// |
| 820 |
// #version 450 |
| 821 |
// |
| 822 |
// uniform blockname { |
| 823 |
// float f; // == 9 |
| 824 |
// }; |
| 825 |
// |
| 826 |
// void main() |
| 827 |
// { |
| 828 |
// float a; |
| 829 |
// a = 3.0; |
| 830 |
// } |
| 831 |
|
| 832 |
std::string shader = R"( |
| 833 |
OpCapability Shader |
| 834 |
%1 = OpExtInstImport "GLSL.std.450" |
| 835 |
OpMemoryModel Logical GLSL450 |
| 836 |
OpEntryPoint Fragment %4 "main" |
| 837 |
OpExecutionMode %4 OriginUpperLeft |
| 838 |
OpSource GLSL 450 |
| 839 |
OpName %4 "main" |
| 840 |
OpName %8 "a" |
| 841 |
OpName %10 "blockname" |
| 842 |
OpMemberName %10 0 "f" |
| 843 |
OpName %12 "" |
| 844 |
OpMemberDecorate %10 0 Offset 0 |
| 845 |
OpDecorate %10 Block |
| 846 |
OpDecorate %12 DescriptorSet 0 |
| 847 |
OpDecorate %12 Binding 0 |
| 848 |
%2 = OpTypeVoid |
| 849 |
%3 = OpTypeFunction %2 |
| 850 |
%6 = OpTypeFloat 32 |
| 851 |
%7 = OpTypePointer Function %6 |
| 852 |
%9 = OpConstant %6 3 |
| 853 |
%10 = OpTypeStruct %6 |
| 854 |
%11 = OpTypePointer Uniform %10 |
| 855 |
%12 = OpVariable %11 Uniform |
| 856 |
%4 = OpFunction %2 None %3 |
| 857 |
%5 = OpLabel |
| 858 |
%8 = OpVariable %7 Function |
| 859 |
OpStore %8 %9 |
| 860 |
OpReturn |
| 861 |
OpFunctionEnd |
| 862 |
)"; |
| 863 |
|
| 864 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 865 |
const auto consumer = nullptr; |
| 866 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 867 |
spvtools::ValidatorOptions validator_options; |
| 868 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 869 |
kConsoleMessageConsumer)); |
| 870 |
TransformationContext transformation_context( |
| 871 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 872 |
protobufs::UniformBufferElementDescriptor blockname_3 = |
| 873 |
MakeUniformBufferElementDescriptor(0, 0, {0}); |
| 874 |
|
| 875 |
uint32_t float_data[1]; |
| 876 |
float temp = 3.0; |
| 877 |
memcpy(&float_data[0], &temp, sizeof(float)); |
| 878 |
ASSERT_TRUE( |
| 879 |
AddFactHelper(&transformation_context, float_data[0], blockname_3)); |
| 880 |
|
| 881 |
// The constant id is 9 for 3.0. |
| 882 |
protobufs::IdUseDescriptor use_of_9_in_store = MakeIdUseDescriptor( |
| 883 |
9, MakeInstructionDescriptor(8, spv::Op::OpStore, 0), 1); |
| 884 |
|
| 885 |
// This transformation is not available because no integer type is present to |
| 886 |
// allow a constant index to be expressed: |
| 887 |
ASSERT_FALSE(TransformationReplaceConstantWithUniform(use_of_9_in_store, |
| 888 |
blockname_3, 100, 101) |
| 889 |
.IsApplicable(context.get(), transformation_context)); |
| 890 |
} |
| 891 |
|
| 892 |
TEST(TransformationReplaceConstantWithUniformTest, |
| 893 |
UniformFactsDoNotMatchConstants) { |
| 894 |
// This test came from the following GLSL: |
| 895 |
// |
| 896 |
// #version 450 |
| 897 |
// |
| 898 |
// uniform blockname { |
| 899 |
// int x; // == 9 |
| 900 |
// int y; // == 10 |
| 901 |
// }; |
| 902 |
// |
| 903 |
// void main() |
| 904 |
// { |
| 905 |
// int a; |
| 906 |
// int b; |
| 907 |
// a = 9; |
| 908 |
// b = 10; |
| 909 |
// } |
| 910 |
|
| 911 |
std::string shader = R"( |
| 912 |
OpCapability Shader |
| 913 |
%1 = OpExtInstImport "GLSL.std.450" |
| 914 |
OpMemoryModel Logical GLSL450 |
| 915 |
OpEntryPoint Fragment %4 "main" |
| 916 |
OpExecutionMode %4 OriginUpperLeft |
| 917 |
OpSource GLSL 450 |
| 918 |
OpName %4 "main" |
| 919 |
OpName %8 "a" |
| 920 |
OpName %10 "b" |
| 921 |
OpName %12 "blockname" |
| 922 |
OpMemberName %12 0 "x" |
| 923 |
OpMemberName %12 1 "y" |
| 924 |
OpName %14 "" |
| 925 |
OpMemberDecorate %12 0 Offset 0 |
| 926 |
OpMemberDecorate %12 1 Offset 4 |
| 927 |
OpDecorate %12 Block |
| 928 |
OpDecorate %14 DescriptorSet 0 |
| 929 |
OpDecorate %14 Binding 0 |
| 930 |
%2 = OpTypeVoid |
| 931 |
%3 = OpTypeFunction %2 |
| 932 |
%6 = OpTypeInt 32 1 |
| 933 |
%7 = OpTypePointer Function %6 |
| 934 |
%9 = OpConstant %6 9 |
| 935 |
%11 = OpConstant %6 10 |
| 936 |
%50 = OpConstant %6 0 |
| 937 |
%51 = OpConstant %6 1 |
| 938 |
%12 = OpTypeStruct %6 %6 |
| 939 |
%13 = OpTypePointer Uniform %12 |
| 940 |
%52 = OpTypePointer Uniform %6 |
| 941 |
%14 = OpVariable %13 Uniform |
| 942 |
%4 = OpFunction %2 None %3 |
| 943 |
%5 = OpLabel |
| 944 |
%8 = OpVariable %7 Function |
| 945 |
%10 = OpVariable %7 Function |
| 946 |
OpStore %8 %9 |
| 947 |
OpStore %10 %11 |
| 948 |
OpReturn |
| 949 |
OpFunctionEnd |
| 950 |
)"; |
| 951 |
|
| 952 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 953 |
const auto consumer = nullptr; |
| 954 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 955 |
spvtools::ValidatorOptions validator_options; |
| 956 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 957 |
kConsoleMessageConsumer)); |
| 958 |
TransformationContext transformation_context( |
| 959 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 960 |
protobufs::UniformBufferElementDescriptor blockname_9 = |
| 961 |
MakeUniformBufferElementDescriptor(0, 0, {0}); |
| 962 |
protobufs::UniformBufferElementDescriptor blockname_10 = |
| 963 |
MakeUniformBufferElementDescriptor(0, 0, {1}); |
| 964 |
|
| 965 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 9, blockname_9)); |
| 966 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 10, blockname_10)); |
| 967 |
|
| 968 |
// The constant ids for 9 and 10 are 9 and 11 respectively |
| 969 |
protobufs::IdUseDescriptor use_of_9_in_store = MakeIdUseDescriptor( |
| 970 |
9, MakeInstructionDescriptor(10, spv::Op::OpStore, 0), 1); |
| 971 |
protobufs::IdUseDescriptor use_of_11_in_store = MakeIdUseDescriptor( |
| 972 |
11, MakeInstructionDescriptor(10, spv::Op::OpStore, 1), 1); |
| 973 |
|
| 974 |
// These are right: |
| 975 |
ASSERT_TRUE(TransformationReplaceConstantWithUniform(use_of_9_in_store, |
| 976 |
blockname_9, 100, 101) |
| 977 |
.IsApplicable(context.get(), transformation_context)); |
| 978 |
ASSERT_TRUE(TransformationReplaceConstantWithUniform(use_of_11_in_store, |
| 979 |
blockname_10, 102, 103) |
| 980 |
.IsApplicable(context.get(), transformation_context)); |
| 981 |
|
| 982 |
// These are wrong because the constants do not match the facts about |
| 983 |
// uniforms. |
| 984 |
ASSERT_FALSE(TransformationReplaceConstantWithUniform(use_of_11_in_store, |
| 985 |
blockname_9, 100, 101) |
| 986 |
.IsApplicable(context.get(), transformation_context)); |
| 987 |
ASSERT_FALSE(TransformationReplaceConstantWithUniform(use_of_9_in_store, |
| 988 |
blockname_10, 102, 103) |
| 989 |
.IsApplicable(context.get(), transformation_context)); |
| 990 |
} |
| 991 |
|
| 992 |
TEST(TransformationReplaceConstantWithUniformTest, ComplexReplacements) { |
| 993 |
// The following GLSL was the basis for this test: |
| 994 |
|
| 995 |
// #version 450 |
| 996 |
// |
| 997 |
// struct T { |
| 998 |
// float a[5]; // [1.0, 1.5, 1.75, 1.875, 1.9375] |
| 999 |
// ivec4 b; // (1, 2, 3, 4) |
| 1000 |
// vec3 c; // (2.0, 2.5, 2.75) |
| 1001 |
// uint d; // 42u |
| 1002 |
// bool e; // Not used in test |
| 1003 |
// }; |
| 1004 |
// |
| 1005 |
// uniform block { |
| 1006 |
// T f; |
| 1007 |
// int g; // 22 |
| 1008 |
// uvec2 h; // (100u, 200u) |
| 1009 |
// }; |
| 1010 |
// |
| 1011 |
// void main() |
| 1012 |
// { |
| 1013 |
// T myT; |
| 1014 |
// |
| 1015 |
// myT.a[0] = 1.9375; |
| 1016 |
// myT.a[1] = 1.875; |
| 1017 |
// myT.a[2] = 1.75; |
| 1018 |
// myT.a[3] = 1.5; |
| 1019 |
// myT.a[4] = 1.0; |
| 1020 |
// |
| 1021 |
// myT.b.x = 4; |
| 1022 |
// myT.b.y = 3; |
| 1023 |
// myT.b.z = 2; |
| 1024 |
// myT.b.w = 1; |
| 1025 |
// |
| 1026 |
// myT.b.r = 22; |
| 1027 |
// |
| 1028 |
// myT.c[0] = 2.75; |
| 1029 |
// myT.c[0] = 2.5; |
| 1030 |
// myT.c[0] = 2.0; |
| 1031 |
// |
| 1032 |
// myT.d = 42u; |
| 1033 |
// myT.d = 100u; |
| 1034 |
// myT.d = 200u; |
| 1035 |
// |
| 1036 |
// myT.e = true; // No attempt to replace 'true' by a uniform value |
| 1037 |
// |
| 1038 |
// } |
| 1039 |
|
| 1040 |
std::string shader = R"( |
| 1041 |
OpCapability Shader |
| 1042 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1043 |
OpMemoryModel Logical GLSL450 |
| 1044 |
OpEntryPoint Fragment %4 "main" |
| 1045 |
OpExecutionMode %4 OriginUpperLeft |
| 1046 |
OpSource GLSL 450 |
| 1047 |
OpName %4 "main" |
| 1048 |
OpName %14 "T" |
| 1049 |
OpMemberName %14 0 "a" |
| 1050 |
OpMemberName %14 1 "b" |
| 1051 |
OpMemberName %14 2 "c" |
| 1052 |
OpMemberName %14 3 "d" |
| 1053 |
OpMemberName %14 4 "e" |
| 1054 |
OpName %16 "myT" |
| 1055 |
OpName %61 "T" |
| 1056 |
OpMemberName %61 0 "a" |
| 1057 |
OpMemberName %61 1 "b" |
| 1058 |
OpMemberName %61 2 "c" |
| 1059 |
OpMemberName %61 3 "d" |
| 1060 |
OpMemberName %61 4 "e" |
| 1061 |
OpName %63 "block" |
| 1062 |
OpMemberName %63 0 "f" |
| 1063 |
OpMemberName %63 1 "g" |
| 1064 |
OpMemberName %63 2 "h" |
| 1065 |
OpName %65 "" |
| 1066 |
OpDecorate %60 ArrayStride 16 |
| 1067 |
OpMemberDecorate %61 0 Offset 0 |
| 1068 |
OpMemberDecorate %61 1 Offset 80 |
| 1069 |
OpMemberDecorate %61 2 Offset 96 |
| 1070 |
OpMemberDecorate %61 3 Offset 108 |
| 1071 |
OpMemberDecorate %61 4 Offset 112 |
| 1072 |
OpMemberDecorate %63 0 Offset 0 |
| 1073 |
OpMemberDecorate %63 1 Offset 128 |
| 1074 |
OpMemberDecorate %63 2 Offset 136 |
| 1075 |
OpDecorate %63 Block |
| 1076 |
OpDecorate %65 DescriptorSet 0 |
| 1077 |
OpDecorate %65 Binding 0 |
| 1078 |
%2 = OpTypeVoid |
| 1079 |
%3 = OpTypeFunction %2 |
| 1080 |
%6 = OpTypeFloat 32 |
| 1081 |
%7 = OpTypeInt 32 0 |
| 1082 |
%8 = OpConstant %7 5 |
| 1083 |
%9 = OpTypeArray %6 %8 |
| 1084 |
%10 = OpTypeInt 32 1 |
| 1085 |
%11 = OpTypeVector %10 4 |
| 1086 |
%12 = OpTypeVector %6 3 |
| 1087 |
%13 = OpTypeBool |
| 1088 |
%14 = OpTypeStruct %9 %11 %12 %7 %13 |
| 1089 |
%15 = OpTypePointer Function %14 |
| 1090 |
%17 = OpConstant %10 0 |
| 1091 |
%18 = OpConstant %6 1.9375 |
| 1092 |
%19 = OpTypePointer Function %6 |
| 1093 |
%21 = OpConstant %10 1 |
| 1094 |
%22 = OpConstant %6 1.875 |
| 1095 |
%24 = OpConstant %10 2 |
| 1096 |
%25 = OpConstant %6 1.75 |
| 1097 |
%27 = OpConstant %10 3 |
| 1098 |
%28 = OpConstant %6 1.5 |
| 1099 |
%30 = OpConstant %10 4 |
| 1100 |
%31 = OpConstant %6 1 |
| 1101 |
%33 = OpConstant %7 0 |
| 1102 |
%34 = OpTypePointer Function %10 |
| 1103 |
%36 = OpConstant %7 1 |
| 1104 |
%38 = OpConstant %7 2 |
| 1105 |
%40 = OpConstant %7 3 |
| 1106 |
%42 = OpConstant %10 22 |
| 1107 |
%44 = OpConstant %6 2.75 |
| 1108 |
%46 = OpConstant %6 2.5 |
| 1109 |
%48 = OpConstant %6 2 |
| 1110 |
%50 = OpConstant %7 42 |
| 1111 |
%51 = OpTypePointer Function %7 |
| 1112 |
%53 = OpConstant %7 100 |
| 1113 |
%55 = OpConstant %7 200 |
| 1114 |
%57 = OpConstantTrue %13 |
| 1115 |
%58 = OpTypePointer Function %13 |
| 1116 |
%60 = OpTypeArray %6 %8 |
| 1117 |
%61 = OpTypeStruct %60 %11 %12 %7 %7 |
| 1118 |
%62 = OpTypeVector %7 2 |
| 1119 |
%63 = OpTypeStruct %61 %10 %62 |
| 1120 |
%64 = OpTypePointer Uniform %63 |
| 1121 |
%100 = OpTypePointer Uniform %10 |
| 1122 |
%101 = OpTypePointer Uniform %7 |
| 1123 |
%102 = OpTypePointer Uniform %6 |
| 1124 |
%103 = OpTypePointer Uniform %13 |
| 1125 |
%65 = OpVariable %64 Uniform |
| 1126 |
%4 = OpFunction %2 None %3 |
| 1127 |
%5 = OpLabel |
| 1128 |
%16 = OpVariable %15 Function |
| 1129 |
%20 = OpAccessChain %19 %16 %17 %17 |
| 1130 |
OpStore %20 %18 |
| 1131 |
%23 = OpAccessChain %19 %16 %17 %21 |
| 1132 |
OpStore %23 %22 |
| 1133 |
%26 = OpAccessChain %19 %16 %17 %24 |
| 1134 |
OpStore %26 %25 |
| 1135 |
%29 = OpAccessChain %19 %16 %17 %27 |
| 1136 |
OpStore %29 %28 |
| 1137 |
%32 = OpAccessChain %19 %16 %17 %30 |
| 1138 |
OpStore %32 %31 |
| 1139 |
%35 = OpAccessChain %34 %16 %21 %33 |
| 1140 |
OpStore %35 %30 |
| 1141 |
%37 = OpAccessChain %34 %16 %21 %36 |
| 1142 |
OpStore %37 %27 |
| 1143 |
%39 = OpAccessChain %34 %16 %21 %38 |
| 1144 |
OpStore %39 %24 |
| 1145 |
%41 = OpAccessChain %34 %16 %21 %40 |
| 1146 |
OpStore %41 %21 |
| 1147 |
%43 = OpAccessChain %34 %16 %21 %33 |
| 1148 |
OpStore %43 %42 |
| 1149 |
%45 = OpAccessChain %19 %16 %24 %33 |
| 1150 |
OpStore %45 %44 |
| 1151 |
%47 = OpAccessChain %19 %16 %24 %33 |
| 1152 |
OpStore %47 %46 |
| 1153 |
%49 = OpAccessChain %19 %16 %24 %33 |
| 1154 |
OpStore %49 %48 |
| 1155 |
%52 = OpAccessChain %51 %16 %27 |
| 1156 |
OpStore %52 %50 |
| 1157 |
%54 = OpAccessChain %51 %16 %27 |
| 1158 |
OpStore %54 %53 |
| 1159 |
%56 = OpAccessChain %51 %16 %27 |
| 1160 |
OpStore %56 %55 |
| 1161 |
%59 = OpAccessChain %58 %16 %30 |
| 1162 |
OpStore %59 %57 |
| 1163 |
OpReturn |
| 1164 |
OpFunctionEnd |
| 1165 |
)"; |
| 1166 |
|
| 1167 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 1168 |
const auto consumer = nullptr; |
| 1169 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1170 |
spvtools::ValidatorOptions validator_options; |
| 1171 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1172 |
kConsoleMessageConsumer)); |
| 1173 |
TransformationContext transformation_context( |
| 1174 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1175 |
const float float_array_values[5] = {1.0, 1.5, 1.75, 1.875, 1.9375}; |
| 1176 |
uint32_t float_array_data[5]; |
| 1177 |
memcpy(&float_array_data, &float_array_values, sizeof(float_array_values)); |
| 1178 |
|
| 1179 |
const float float_vector_values[3] = {2.0, 2.5, 2.75}; |
| 1180 |
uint32_t float_vector_data[3]; |
| 1181 |
memcpy(&float_vector_data, &float_vector_values, sizeof(float_vector_values)); |
| 1182 |
|
| 1183 |
protobufs::UniformBufferElementDescriptor uniform_f_a_0 = |
| 1184 |
MakeUniformBufferElementDescriptor(0, 0, {0, 0, 0}); |
| 1185 |
protobufs::UniformBufferElementDescriptor uniform_f_a_1 = |
| 1186 |
MakeUniformBufferElementDescriptor(0, 0, {0, 0, 1}); |
| 1187 |
protobufs::UniformBufferElementDescriptor uniform_f_a_2 = |
| 1188 |
MakeUniformBufferElementDescriptor(0, 0, {0, 0, 2}); |
| 1189 |
protobufs::UniformBufferElementDescriptor uniform_f_a_3 = |
| 1190 |
MakeUniformBufferElementDescriptor(0, 0, {0, 0, 3}); |
| 1191 |
protobufs::UniformBufferElementDescriptor uniform_f_a_4 = |
| 1192 |
MakeUniformBufferElementDescriptor(0, 0, {0, 0, 4}); |
| 1193 |
|
| 1194 |
protobufs::UniformBufferElementDescriptor uniform_f_b_x = |
| 1195 |
MakeUniformBufferElementDescriptor(0, 0, {0, 1, 0}); |
| 1196 |
protobufs::UniformBufferElementDescriptor uniform_f_b_y = |
| 1197 |
MakeUniformBufferElementDescriptor(0, 0, {0, 1, 1}); |
| 1198 |
protobufs::UniformBufferElementDescriptor uniform_f_b_z = |
| 1199 |
MakeUniformBufferElementDescriptor(0, 0, {0, 1, 2}); |
| 1200 |
protobufs::UniformBufferElementDescriptor uniform_f_b_w = |
| 1201 |
MakeUniformBufferElementDescriptor(0, 0, {0, 1, 3}); |
| 1202 |
|
| 1203 |
protobufs::UniformBufferElementDescriptor uniform_f_c_x = |
| 1204 |
MakeUniformBufferElementDescriptor(0, 0, {0, 2, 0}); |
| 1205 |
protobufs::UniformBufferElementDescriptor uniform_f_c_y = |
| 1206 |
MakeUniformBufferElementDescriptor(0, 0, {0, 2, 1}); |
| 1207 |
protobufs::UniformBufferElementDescriptor uniform_f_c_z = |
| 1208 |
MakeUniformBufferElementDescriptor(0, 0, {0, 2, 2}); |
| 1209 |
|
| 1210 |
protobufs::UniformBufferElementDescriptor uniform_f_d = |
| 1211 |
MakeUniformBufferElementDescriptor(0, 0, {0, 3}); |
| 1212 |
|
| 1213 |
protobufs::UniformBufferElementDescriptor uniform_g = |
| 1214 |
MakeUniformBufferElementDescriptor(0, 0, {1}); |
| 1215 |
|
| 1216 |
protobufs::UniformBufferElementDescriptor uniform_h_x = |
| 1217 |
MakeUniformBufferElementDescriptor(0, 0, {2, 0}); |
| 1218 |
protobufs::UniformBufferElementDescriptor uniform_h_y = |
| 1219 |
MakeUniformBufferElementDescriptor(0, 0, {2, 1}); |
| 1220 |
|
| 1221 |
ASSERT_TRUE(AddFactHelper(&transformation_context, float_array_data[0], |
| 1222 |
uniform_f_a_0)); |
| 1223 |
ASSERT_TRUE(AddFactHelper(&transformation_context, float_array_data[1], |
| 1224 |
uniform_f_a_1)); |
| 1225 |
ASSERT_TRUE(AddFactHelper(&transformation_context, float_array_data[2], |
| 1226 |
uniform_f_a_2)); |
| 1227 |
ASSERT_TRUE(AddFactHelper(&transformation_context, float_array_data[3], |
| 1228 |
uniform_f_a_3)); |
| 1229 |
ASSERT_TRUE(AddFactHelper(&transformation_context, float_array_data[4], |
| 1230 |
uniform_f_a_4)); |
| 1231 |
|
| 1232 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 1, uniform_f_b_x)); |
| 1233 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 2, uniform_f_b_y)); |
| 1234 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 3, uniform_f_b_z)); |
| 1235 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 4, uniform_f_b_w)); |
| 1236 |
|
| 1237 |
ASSERT_TRUE(AddFactHelper(&transformation_context, float_vector_data[0], |
| 1238 |
uniform_f_c_x)); |
| 1239 |
ASSERT_TRUE(AddFactHelper(&transformation_context, float_vector_data[1], |
| 1240 |
uniform_f_c_y)); |
| 1241 |
ASSERT_TRUE(AddFactHelper(&transformation_context, float_vector_data[2], |
| 1242 |
uniform_f_c_z)); |
| 1243 |
|
| 1244 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 42, uniform_f_d)); |
| 1245 |
|
| 1246 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 22, uniform_g)); |
| 1247 |
|
| 1248 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 100, uniform_h_x)); |
| 1249 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 200, uniform_h_y)); |
| 1250 |
|
| 1251 |
std::vector<TransformationReplaceConstantWithUniform> transformations; |
| 1252 |
|
| 1253 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1254 |
MakeIdUseDescriptor( |
| 1255 |
18, MakeInstructionDescriptor(20, spv::Op::OpStore, 0), 1), |
| 1256 |
uniform_f_a_4, 200, 201)); |
| 1257 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1258 |
MakeIdUseDescriptor( |
| 1259 |
22, MakeInstructionDescriptor(23, spv::Op::OpStore, 0), 1), |
| 1260 |
uniform_f_a_3, 202, 203)); |
| 1261 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1262 |
MakeIdUseDescriptor( |
| 1263 |
25, MakeInstructionDescriptor(26, spv::Op::OpStore, 0), 1), |
| 1264 |
uniform_f_a_2, 204, 205)); |
| 1265 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1266 |
MakeIdUseDescriptor( |
| 1267 |
28, MakeInstructionDescriptor(29, spv::Op::OpStore, 0), 1), |
| 1268 |
uniform_f_a_1, 206, 207)); |
| 1269 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1270 |
MakeIdUseDescriptor( |
| 1271 |
31, MakeInstructionDescriptor(32, spv::Op::OpStore, 0), 1), |
| 1272 |
uniform_f_a_0, 208, 209)); |
| 1273 |
|
| 1274 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1275 |
MakeIdUseDescriptor( |
| 1276 |
30, MakeInstructionDescriptor(35, spv::Op::OpStore, 0), 1), |
| 1277 |
uniform_f_b_w, 210, 211)); |
| 1278 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1279 |
MakeIdUseDescriptor( |
| 1280 |
27, MakeInstructionDescriptor(37, spv::Op::OpStore, 0), 1), |
| 1281 |
uniform_f_b_z, 212, 213)); |
| 1282 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1283 |
MakeIdUseDescriptor( |
| 1284 |
24, MakeInstructionDescriptor(39, spv::Op::OpStore, 0), 1), |
| 1285 |
uniform_f_b_y, 214, 215)); |
| 1286 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1287 |
MakeIdUseDescriptor( |
| 1288 |
21, MakeInstructionDescriptor(41, spv::Op::OpStore, 0), 1), |
| 1289 |
uniform_f_b_x, 216, 217)); |
| 1290 |
|
| 1291 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1292 |
MakeIdUseDescriptor( |
| 1293 |
44, MakeInstructionDescriptor(45, spv::Op::OpStore, 0), 1), |
| 1294 |
uniform_f_c_z, 220, 221)); |
| 1295 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1296 |
MakeIdUseDescriptor( |
| 1297 |
46, MakeInstructionDescriptor(47, spv::Op::OpStore, 0), 1), |
| 1298 |
uniform_f_c_y, 222, 223)); |
| 1299 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1300 |
MakeIdUseDescriptor( |
| 1301 |
48, MakeInstructionDescriptor(49, spv::Op::OpStore, 0), 1), |
| 1302 |
uniform_f_c_x, 224, 225)); |
| 1303 |
|
| 1304 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1305 |
MakeIdUseDescriptor( |
| 1306 |
50, MakeInstructionDescriptor(52, spv::Op::OpStore, 0), 1), |
| 1307 |
uniform_f_d, 226, 227)); |
| 1308 |
|
| 1309 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1310 |
MakeIdUseDescriptor( |
| 1311 |
53, MakeInstructionDescriptor(54, spv::Op::OpStore, 0), 1), |
| 1312 |
uniform_h_x, 228, 229)); |
| 1313 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1314 |
MakeIdUseDescriptor( |
| 1315 |
55, MakeInstructionDescriptor(56, spv::Op::OpStore, 0), 1), |
| 1316 |
uniform_h_y, 230, 231)); |
| 1317 |
|
| 1318 |
transformations.emplace_back(TransformationReplaceConstantWithUniform( |
| 1319 |
MakeIdUseDescriptor( |
| 1320 |
42, MakeInstructionDescriptor(43, spv::Op::OpStore, 0), 1), |
| 1321 |
uniform_g, 218, 219)); |
| 1322 |
|
| 1323 |
for (auto& transformation : transformations) { |
| 1324 |
ASSERT_TRUE( |
| 1325 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 1326 |
ApplyAndCheckFreshIds(transformation, context.get(), |
| 1327 |
&transformation_context); |
| 1328 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed( |
| 1329 |
context.get(), validator_options, kConsoleMessageConsumer)); |
| 1330 |
} |
| 1331 |
|
| 1332 |
std::string after = R"( |
| 1333 |
OpCapability Shader |
| 1334 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1335 |
OpMemoryModel Logical GLSL450 |
| 1336 |
OpEntryPoint Fragment %4 "main" |
| 1337 |
OpExecutionMode %4 OriginUpperLeft |
| 1338 |
OpSource GLSL 450 |
| 1339 |
OpName %4 "main" |
| 1340 |
OpName %14 "T" |
| 1341 |
OpMemberName %14 0 "a" |
| 1342 |
OpMemberName %14 1 "b" |
| 1343 |
OpMemberName %14 2 "c" |
| 1344 |
OpMemberName %14 3 "d" |
| 1345 |
OpMemberName %14 4 "e" |
| 1346 |
OpName %16 "myT" |
| 1347 |
OpName %61 "T" |
| 1348 |
OpMemberName %61 0 "a" |
| 1349 |
OpMemberName %61 1 "b" |
| 1350 |
OpMemberName %61 2 "c" |
| 1351 |
OpMemberName %61 3 "d" |
| 1352 |
OpMemberName %61 4 "e" |
| 1353 |
OpName %63 "block" |
| 1354 |
OpMemberName %63 0 "f" |
| 1355 |
OpMemberName %63 1 "g" |
| 1356 |
OpMemberName %63 2 "h" |
| 1357 |
OpName %65 "" |
| 1358 |
OpDecorate %60 ArrayStride 16 |
| 1359 |
OpMemberDecorate %61 0 Offset 0 |
| 1360 |
OpMemberDecorate %61 1 Offset 80 |
| 1361 |
OpMemberDecorate %61 2 Offset 96 |
| 1362 |
OpMemberDecorate %61 3 Offset 108 |
| 1363 |
OpMemberDecorate %61 4 Offset 112 |
| 1364 |
OpMemberDecorate %63 0 Offset 0 |
| 1365 |
OpMemberDecorate %63 1 Offset 128 |
| 1366 |
OpMemberDecorate %63 2 Offset 136 |
| 1367 |
OpDecorate %63 Block |
| 1368 |
OpDecorate %65 DescriptorSet 0 |
| 1369 |
OpDecorate %65 Binding 0 |
| 1370 |
%2 = OpTypeVoid |
| 1371 |
%3 = OpTypeFunction %2 |
| 1372 |
%6 = OpTypeFloat 32 |
| 1373 |
%7 = OpTypeInt 32 0 |
| 1374 |
%8 = OpConstant %7 5 |
| 1375 |
%9 = OpTypeArray %6 %8 |
| 1376 |
%10 = OpTypeInt 32 1 |
| 1377 |
%11 = OpTypeVector %10 4 |
| 1378 |
%12 = OpTypeVector %6 3 |
| 1379 |
%13 = OpTypeBool |
| 1380 |
%14 = OpTypeStruct %9 %11 %12 %7 %13 |
| 1381 |
%15 = OpTypePointer Function %14 |
| 1382 |
%17 = OpConstant %10 0 |
| 1383 |
%18 = OpConstant %6 1.9375 |
| 1384 |
%19 = OpTypePointer Function %6 |
| 1385 |
%21 = OpConstant %10 1 |
| 1386 |
%22 = OpConstant %6 1.875 |
| 1387 |
%24 = OpConstant %10 2 |
| 1388 |
%25 = OpConstant %6 1.75 |
| 1389 |
%27 = OpConstant %10 3 |
| 1390 |
%28 = OpConstant %6 1.5 |
| 1391 |
%30 = OpConstant %10 4 |
| 1392 |
%31 = OpConstant %6 1 |
| 1393 |
%33 = OpConstant %7 0 |
| 1394 |
%34 = OpTypePointer Function %10 |
| 1395 |
%36 = OpConstant %7 1 |
| 1396 |
%38 = OpConstant %7 2 |
| 1397 |
%40 = OpConstant %7 3 |
| 1398 |
%42 = OpConstant %10 22 |
| 1399 |
%44 = OpConstant %6 2.75 |
| 1400 |
%46 = OpConstant %6 2.5 |
| 1401 |
%48 = OpConstant %6 2 |
| 1402 |
%50 = OpConstant %7 42 |
| 1403 |
%51 = OpTypePointer Function %7 |
| 1404 |
%53 = OpConstant %7 100 |
| 1405 |
%55 = OpConstant %7 200 |
| 1406 |
%57 = OpConstantTrue %13 |
| 1407 |
%58 = OpTypePointer Function %13 |
| 1408 |
%60 = OpTypeArray %6 %8 |
| 1409 |
%61 = OpTypeStruct %60 %11 %12 %7 %7 |
| 1410 |
%62 = OpTypeVector %7 2 |
| 1411 |
%63 = OpTypeStruct %61 %10 %62 |
| 1412 |
%64 = OpTypePointer Uniform %63 |
| 1413 |
%100 = OpTypePointer Uniform %10 |
| 1414 |
%101 = OpTypePointer Uniform %7 |
| 1415 |
%102 = OpTypePointer Uniform %6 |
| 1416 |
%103 = OpTypePointer Uniform %13 |
| 1417 |
%65 = OpVariable %64 Uniform |
| 1418 |
%4 = OpFunction %2 None %3 |
| 1419 |
%5 = OpLabel |
| 1420 |
%16 = OpVariable %15 Function |
| 1421 |
%20 = OpAccessChain %19 %16 %17 %17 |
| 1422 |
%200 = OpAccessChain %102 %65 %17 %17 %30 |
| 1423 |
%201 = OpLoad %6 %200 |
| 1424 |
OpStore %20 %201 |
| 1425 |
%23 = OpAccessChain %19 %16 %17 %21 |
| 1426 |
%202 = OpAccessChain %102 %65 %17 %17 %27 |
| 1427 |
%203 = OpLoad %6 %202 |
| 1428 |
OpStore %23 %203 |
| 1429 |
%26 = OpAccessChain %19 %16 %17 %24 |
| 1430 |
%204 = OpAccessChain %102 %65 %17 %17 %24 |
| 1431 |
%205 = OpLoad %6 %204 |
| 1432 |
OpStore %26 %205 |
| 1433 |
%29 = OpAccessChain %19 %16 %17 %27 |
| 1434 |
%206 = OpAccessChain %102 %65 %17 %17 %21 |
| 1435 |
%207 = OpLoad %6 %206 |
| 1436 |
OpStore %29 %207 |
| 1437 |
%32 = OpAccessChain %19 %16 %17 %30 |
| 1438 |
%208 = OpAccessChain %102 %65 %17 %17 %17 |
| 1439 |
%209 = OpLoad %6 %208 |
| 1440 |
OpStore %32 %209 |
| 1441 |
%35 = OpAccessChain %34 %16 %21 %33 |
| 1442 |
%210 = OpAccessChain %100 %65 %17 %21 %27 |
| 1443 |
%211 = OpLoad %10 %210 |
| 1444 |
OpStore %35 %211 |
| 1445 |
%37 = OpAccessChain %34 %16 %21 %36 |
| 1446 |
%212 = OpAccessChain %100 %65 %17 %21 %24 |
| 1447 |
%213 = OpLoad %10 %212 |
| 1448 |
OpStore %37 %213 |
| 1449 |
%39 = OpAccessChain %34 %16 %21 %38 |
| 1450 |
%214 = OpAccessChain %100 %65 %17 %21 %21 |
| 1451 |
%215 = OpLoad %10 %214 |
| 1452 |
OpStore %39 %215 |
| 1453 |
%41 = OpAccessChain %34 %16 %21 %40 |
| 1454 |
%216 = OpAccessChain %100 %65 %17 %21 %17 |
| 1455 |
%217 = OpLoad %10 %216 |
| 1456 |
OpStore %41 %217 |
| 1457 |
%43 = OpAccessChain %34 %16 %21 %33 |
| 1458 |
%218 = OpAccessChain %100 %65 %21 |
| 1459 |
%219 = OpLoad %10 %218 |
| 1460 |
OpStore %43 %219 |
| 1461 |
%45 = OpAccessChain %19 %16 %24 %33 |
| 1462 |
%220 = OpAccessChain %102 %65 %17 %24 %24 |
| 1463 |
%221 = OpLoad %6 %220 |
| 1464 |
OpStore %45 %221 |
| 1465 |
%47 = OpAccessChain %19 %16 %24 %33 |
| 1466 |
%222 = OpAccessChain %102 %65 %17 %24 %21 |
| 1467 |
%223 = OpLoad %6 %222 |
| 1468 |
OpStore %47 %223 |
| 1469 |
%49 = OpAccessChain %19 %16 %24 %33 |
| 1470 |
%224 = OpAccessChain %102 %65 %17 %24 %17 |
| 1471 |
%225 = OpLoad %6 %224 |
| 1472 |
OpStore %49 %225 |
| 1473 |
%52 = OpAccessChain %51 %16 %27 |
| 1474 |
%226 = OpAccessChain %101 %65 %17 %27 |
| 1475 |
%227 = OpLoad %7 %226 |
| 1476 |
OpStore %52 %227 |
| 1477 |
%54 = OpAccessChain %51 %16 %27 |
| 1478 |
%228 = OpAccessChain %101 %65 %24 %17 |
| 1479 |
%229 = OpLoad %7 %228 |
| 1480 |
OpStore %54 %229 |
| 1481 |
%56 = OpAccessChain %51 %16 %27 |
| 1482 |
%230 = OpAccessChain %101 %65 %24 %21 |
| 1483 |
%231 = OpLoad %7 %230 |
| 1484 |
OpStore %56 %231 |
| 1485 |
%59 = OpAccessChain %58 %16 %30 |
| 1486 |
OpStore %59 %57 |
| 1487 |
OpReturn |
| 1488 |
OpFunctionEnd |
| 1489 |
)"; |
| 1490 |
|
| 1491 |
ASSERT_TRUE(IsEqual(env, after, context.get())); |
| 1492 |
} |
| 1493 |
|
| 1494 |
TEST(TransformationReplaceConstantWithUniformTest, |
| 1495 |
DoNotReplaceVariableInitializer) { |
| 1496 |
// If a local variable has a constant initializer, this cannot be replaced |
| 1497 |
// by a uniform. |
| 1498 |
|
| 1499 |
std::string shader = R"( |
| 1500 |
OpCapability Shader |
| 1501 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1502 |
OpMemoryModel Logical GLSL450 |
| 1503 |
OpEntryPoint Fragment %4 "main" |
| 1504 |
OpExecutionMode %4 OriginUpperLeft |
| 1505 |
OpSource GLSL 450 |
| 1506 |
OpMemberDecorate %16 0 Offset 0 |
| 1507 |
OpDecorate %16 Block |
| 1508 |
OpDecorate %18 DescriptorSet 0 |
| 1509 |
OpDecorate %18 Binding 0 |
| 1510 |
%2 = OpTypeVoid |
| 1511 |
%3 = OpTypeFunction %2 |
| 1512 |
%6 = OpTypeInt 32 1 |
| 1513 |
%7 = OpTypePointer Function %6 |
| 1514 |
%50 = OpConstant %6 0 |
| 1515 |
%16 = OpTypeStruct %6 |
| 1516 |
%17 = OpTypePointer Uniform %16 |
| 1517 |
%51 = OpTypePointer Uniform %6 |
| 1518 |
%18 = OpVariable %17 Uniform |
| 1519 |
%4 = OpFunction %2 None %3 |
| 1520 |
%5 = OpLabel |
| 1521 |
%8 = OpVariable %7 Function %50 |
| 1522 |
OpReturn |
| 1523 |
OpFunctionEnd |
| 1524 |
)"; |
| 1525 |
|
| 1526 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 1527 |
const auto consumer = nullptr; |
| 1528 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1529 |
spvtools::ValidatorOptions validator_options; |
| 1530 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1531 |
kConsoleMessageConsumer)); |
| 1532 |
TransformationContext transformation_context( |
| 1533 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1534 |
protobufs::UniformBufferElementDescriptor blockname_a = |
| 1535 |
MakeUniformBufferElementDescriptor(0, 0, {0}); |
| 1536 |
|
| 1537 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 0, blockname_a)); |
| 1538 |
|
| 1539 |
ASSERT_FALSE( |
| 1540 |
TransformationReplaceConstantWithUniform( |
| 1541 |
MakeIdUseDescriptor( |
| 1542 |
50, MakeInstructionDescriptor(8, spv::Op::OpVariable, 0), 1), |
| 1543 |
blockname_a, 100, 101) |
| 1544 |
.IsApplicable(context.get(), transformation_context)); |
| 1545 |
} |
| 1546 |
|
| 1547 |
TEST(TransformationReplaceConstantWithUniformTest, ReplaceOpPhiOperand) { |
| 1548 |
std::string shader = R"( |
| 1549 |
OpCapability Shader |
| 1550 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1551 |
OpMemoryModel Logical GLSL450 |
| 1552 |
OpEntryPoint Fragment %4 "main" |
| 1553 |
OpExecutionMode %4 OriginUpperLeft |
| 1554 |
OpSource ESSL 320 |
| 1555 |
OpDecorate %32 DescriptorSet 0 |
| 1556 |
OpDecorate %32 Binding 0 |
| 1557 |
%2 = OpTypeVoid |
| 1558 |
%3 = OpTypeFunction %2 |
| 1559 |
%6 = OpTypeInt 32 1 |
| 1560 |
%7 = OpConstant %6 2 |
| 1561 |
%13 = OpConstant %6 4 |
| 1562 |
%21 = OpConstant %6 1 |
| 1563 |
%34 = OpConstant %6 0 |
| 1564 |
%10 = OpTypeBool |
| 1565 |
%30 = OpTypeStruct %6 |
| 1566 |
%31 = OpTypePointer Uniform %30 |
| 1567 |
%32 = OpVariable %31 Uniform |
| 1568 |
%33 = OpTypePointer Uniform %6 |
| 1569 |
%4 = OpFunction %2 None %3 |
| 1570 |
%11 = OpLabel |
| 1571 |
OpBranch %5 |
| 1572 |
%5 = OpLabel |
| 1573 |
%23 = OpPhi %6 %7 %11 %20 %15 |
| 1574 |
%9 = OpSLessThan %10 %23 %13 |
| 1575 |
OpLoopMerge %8 %15 None |
| 1576 |
OpBranchConditional %9 %15 %8 |
| 1577 |
%15 = OpLabel |
| 1578 |
%20 = OpIAdd %6 %23 %21 |
| 1579 |
OpBranch %5 |
| 1580 |
%8 = OpLabel |
| 1581 |
OpReturn |
| 1582 |
OpFunctionEnd |
| 1583 |
)"; |
| 1584 |
|
| 1585 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 1586 |
const auto consumer = nullptr; |
| 1587 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 1588 |
spvtools::ValidatorOptions validator_options; |
| 1589 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 1590 |
kConsoleMessageConsumer)); |
| 1591 |
TransformationContext transformation_context( |
| 1592 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 1593 |
auto int_descriptor = MakeUniformBufferElementDescriptor(0, 0, {0}); |
| 1594 |
|
| 1595 |
ASSERT_TRUE(AddFactHelper(&transformation_context, 2, int_descriptor)); |
| 1596 |
|
| 1597 |
{ |
| 1598 |
TransformationReplaceConstantWithUniform transformation( |
| 1599 |
MakeIdUseDescriptor(7, MakeInstructionDescriptor(23, spv::Op::OpPhi, 0), |
| 1600 |
0), |
| 1601 |
int_descriptor, 50, 51); |
| 1602 |
ASSERT_TRUE( |
| 1603 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 1604 |
ApplyAndCheckFreshIds(transformation, context.get(), |
| 1605 |
&transformation_context); |
| 1606 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed( |
| 1607 |
context.get(), validator_options, kConsoleMessageConsumer)); |
| 1608 |
} |
| 1609 |
|
| 1610 |
std::string after_transformation = R"( |
| 1611 |
OpCapability Shader |
| 1612 |
%1 = OpExtInstImport "GLSL.std.450" |
| 1613 |
OpMemoryModel Logical GLSL450 |
| 1614 |
OpEntryPoint Fragment %4 "main" |
| 1615 |
OpExecutionMode %4 OriginUpperLeft |
| 1616 |
OpSource ESSL 320 |
| 1617 |
OpDecorate %32 DescriptorSet 0 |
| 1618 |
OpDecorate %32 Binding 0 |
| 1619 |
%2 = OpTypeVoid |
| 1620 |
%3 = OpTypeFunction %2 |
| 1621 |
%6 = OpTypeInt 32 1 |
| 1622 |
%7 = OpConstant %6 2 |
| 1623 |
%13 = OpConstant %6 4 |
| 1624 |
%21 = OpConstant %6 1 |
| 1625 |
%34 = OpConstant %6 0 |
| 1626 |
%10 = OpTypeBool |
| 1627 |
%30 = OpTypeStruct %6 |
| 1628 |
%31 = OpTypePointer Uniform %30 |
| 1629 |
%32 = OpVariable %31 Uniform |
| 1630 |
%33 = OpTypePointer Uniform %6 |
| 1631 |
%4 = OpFunction %2 None %3 |
| 1632 |
%11 = OpLabel |
| 1633 |
%50 = OpAccessChain %33 %32 %34 |
| 1634 |
%51 = OpLoad %6 %50 |
| 1635 |
OpBranch %5 |
| 1636 |
%5 = OpLabel |
| 1637 |
%23 = OpPhi %6 %51 %11 %20 %15 |
| 1638 |
%9 = OpSLessThan %10 %23 %13 |
| 1639 |
OpLoopMerge %8 %15 None |
| 1640 |
OpBranchConditional %9 %15 %8 |
| 1641 |
%15 = OpLabel |
| 1642 |
%20 = OpIAdd %6 %23 %21 |
| 1643 |
OpBranch %5 |
| 1644 |
%8 = OpLabel |
| 1645 |
OpReturn |
| 1646 |
OpFunctionEnd |
| 1647 |
)"; |
| 1648 |
|
| 1649 |
ASSERT_TRUE(IsEqual(env, after_transformation, context.get())); |
| 1650 |
} |
| 1651 |
|
| 1652 |
} // namespace |
| 1653 |
} // namespace fuzz |
| 1654 |
} // namespace spvtools |