| 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_boolean_constant_with_constant_binary.h" |
| 16 |
|
| 17 |
#include "gtest/gtest.h" |
| 18 |
#include "source/fuzz/fuzzer_util.h" |
| 19 |
#include "source/fuzz/id_use_descriptor.h" |
| 20 |
#include "source/fuzz/instruction_descriptor.h" |
| 21 |
#include "test/fuzz/fuzz_test_util.h" |
| 22 |
|
| 23 |
namespace spvtools { |
| 24 |
namespace fuzz { |
| 25 |
namespace { |
| 26 |
|
| 27 |
TEST(TransformationReplaceBooleanConstantWithConstantBinaryTest, |
| 28 |
BasicReplacements) { |
| 29 |
// The test came from the following pseudo-GLSL, where int64 and uint64 denote |
| 30 |
// 64-bit integer types (they were replaced with int and uint during |
| 31 |
// translation to SPIR-V, and the generated SPIR-V has been doctored to |
| 32 |
// accommodate them). |
| 33 |
// |
| 34 |
// #version 450 |
| 35 |
// |
| 36 |
// void main() { |
| 37 |
// double d1, d2; |
| 38 |
// d1 = 1.0; |
| 39 |
// d2 = 2.0; |
| 40 |
// float f1, f2; |
| 41 |
// f1 = 4.0; |
| 42 |
// f2 = 8.0; |
| 43 |
// int i1, i2; |
| 44 |
// i1 = 100; |
| 45 |
// i2 = 200; |
| 46 |
// |
| 47 |
// uint u1, u2; |
| 48 |
// u1 = 300u; |
| 49 |
// u2 = 400u; |
| 50 |
// |
| 51 |
// int64 i64_1, i64_2; |
| 52 |
// i64_1 = 500; |
| 53 |
// i64_2 = 600; |
| 54 |
// |
| 55 |
// uint64 u64_1, u64_2; |
| 56 |
// u64_1 = 700u; |
| 57 |
// u64_2 = 800u; |
| 58 |
// |
| 59 |
// bool b, c, d, e; |
| 60 |
// b = true; |
| 61 |
// c = false; |
| 62 |
// d = true || c; |
| 63 |
// c = c && false; |
| 64 |
// } |
| 65 |
std::string shader = R"( |
| 66 |
OpCapability Shader |
| 67 |
OpCapability Float64 |
| 68 |
OpCapability Int64 |
| 69 |
%1 = OpExtInstImport "GLSL.std.450" |
| 70 |
OpMemoryModel Logical GLSL450 |
| 71 |
OpEntryPoint Fragment %4 "main" |
| 72 |
OpExecutionMode %4 OriginUpperLeft |
| 73 |
OpSource GLSL 450 |
| 74 |
OpName %4 "main" |
| 75 |
OpName %8 "d1" |
| 76 |
OpName %10 "d2" |
| 77 |
OpName %14 "f1" |
| 78 |
OpName %16 "f2" |
| 79 |
OpName %20 "i1" |
| 80 |
OpName %22 "i2" |
| 81 |
OpName %26 "u1" |
| 82 |
OpName %28 "u2" |
| 83 |
OpName %30 "i64_1" |
| 84 |
OpName %32 "i64_2" |
| 85 |
OpName %34 "u64_1" |
| 86 |
OpName %36 "u64_2" |
| 87 |
OpName %40 "b" |
| 88 |
OpName %42 "c" |
| 89 |
OpName %44 "d" |
| 90 |
%2 = OpTypeVoid |
| 91 |
%3 = OpTypeFunction %2 |
| 92 |
%6 = OpTypeFloat 64 |
| 93 |
%7 = OpTypePointer Function %6 |
| 94 |
%9 = OpConstant %6 1 |
| 95 |
%11 = OpConstant %6 2 |
| 96 |
%12 = OpTypeFloat 32 |
| 97 |
%13 = OpTypePointer Function %12 |
| 98 |
%15 = OpConstant %12 4 |
| 99 |
%17 = OpConstant %12 8 |
| 100 |
%18 = OpTypeInt 32 1 |
| 101 |
%60 = OpTypeInt 64 1 |
| 102 |
%61 = OpTypePointer Function %60 |
| 103 |
%19 = OpTypePointer Function %18 |
| 104 |
%21 = OpConstant %18 -100 |
| 105 |
%23 = OpConstant %18 200 |
| 106 |
%24 = OpTypeInt 32 0 |
| 107 |
%62 = OpTypeInt 64 0 |
| 108 |
%63 = OpTypePointer Function %62 |
| 109 |
%25 = OpTypePointer Function %24 |
| 110 |
%27 = OpConstant %24 300 |
| 111 |
%29 = OpConstant %24 400 |
| 112 |
%31 = OpConstant %60 -600 |
| 113 |
%33 = OpConstant %60 -500 |
| 114 |
%35 = OpConstant %62 700 |
| 115 |
%37 = OpConstant %62 800 |
| 116 |
%38 = OpTypeBool |
| 117 |
%39 = OpTypePointer Function %38 |
| 118 |
%41 = OpConstantTrue %38 |
| 119 |
%43 = OpConstantFalse %38 |
| 120 |
%4 = OpFunction %2 None %3 |
| 121 |
%5 = OpLabel |
| 122 |
%8 = OpVariable %7 Function |
| 123 |
%10 = OpVariable %7 Function |
| 124 |
%14 = OpVariable %13 Function |
| 125 |
%16 = OpVariable %13 Function |
| 126 |
%20 = OpVariable %19 Function |
| 127 |
%22 = OpVariable %19 Function |
| 128 |
%26 = OpVariable %25 Function |
| 129 |
%28 = OpVariable %25 Function |
| 130 |
%30 = OpVariable %61 Function |
| 131 |
%32 = OpVariable %61 Function |
| 132 |
%34 = OpVariable %63 Function |
| 133 |
%36 = OpVariable %63 Function |
| 134 |
%40 = OpVariable %39 Function |
| 135 |
%42 = OpVariable %39 Function |
| 136 |
%44 = OpVariable %39 Function |
| 137 |
OpStore %8 %9 |
| 138 |
OpStore %10 %11 |
| 139 |
OpStore %14 %15 |
| 140 |
OpStore %16 %17 |
| 141 |
OpStore %20 %21 |
| 142 |
OpStore %22 %23 |
| 143 |
OpStore %26 %27 |
| 144 |
OpStore %28 %29 |
| 145 |
OpStore %30 %31 |
| 146 |
OpStore %32 %33 |
| 147 |
OpStore %34 %35 |
| 148 |
OpStore %36 %37 |
| 149 |
OpStore %40 %41 |
| 150 |
OpStore %42 %43 |
| 151 |
%45 = OpLoad %38 %42 |
| 152 |
%46 = OpLogicalOr %38 %41 %45 |
| 153 |
OpStore %44 %46 |
| 154 |
%47 = OpLoad %38 %42 |
| 155 |
%48 = OpLogicalAnd %38 %47 %43 |
| 156 |
OpStore %42 %48 |
| 157 |
OpReturn |
| 158 |
OpFunctionEnd |
| 159 |
)"; |
| 160 |
|
| 161 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 162 |
const auto consumer = nullptr; |
| 163 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 164 |
spvtools::ValidatorOptions validator_options; |
| 165 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 166 |
kConsoleMessageConsumer)); |
| 167 |
TransformationContext transformation_context( |
| 168 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 169 |
std::vector<protobufs::IdUseDescriptor> uses_of_true = { |
| 170 |
MakeIdUseDescriptor( |
| 171 |
41, MakeInstructionDescriptor(44, spv::Op::OpStore, 12), 1), |
| 172 |
MakeIdUseDescriptor( |
| 173 |
41, MakeInstructionDescriptor(46, spv::Op::OpLogicalOr, 0), 0)}; |
| 174 |
|
| 175 |
std::vector<protobufs::IdUseDescriptor> uses_of_false = { |
| 176 |
MakeIdUseDescriptor( |
| 177 |
43, MakeInstructionDescriptor(44, spv::Op::OpStore, 13), 1), |
| 178 |
MakeIdUseDescriptor( |
| 179 |
43, MakeInstructionDescriptor(48, spv::Op::OpLogicalAnd, 0), 1)}; |
| 180 |
|
| 181 |
const uint32_t fresh_id = 100; |
| 182 |
|
| 183 |
std::vector<spv::Op> fp_gt_opcodes = { |
| 184 |
spv::Op::OpFOrdGreaterThan, spv::Op::OpFOrdGreaterThanEqual, |
| 185 |
spv::Op::OpFUnordGreaterThan, spv::Op::OpFUnordGreaterThanEqual}; |
| 186 |
|
| 187 |
std::vector<spv::Op> fp_lt_opcodes = { |
| 188 |
spv::Op::OpFOrdLessThan, spv::Op::OpFOrdLessThanEqual, |
| 189 |
spv::Op::OpFUnordLessThan, spv::Op::OpFUnordLessThanEqual}; |
| 190 |
|
| 191 |
std::vector<spv::Op> int_gt_opcodes = {spv::Op::OpSGreaterThan, |
| 192 |
spv::Op::OpSGreaterThanEqual}; |
| 193 |
|
| 194 |
std::vector<spv::Op> int_lt_opcodes = {spv::Op::OpSLessThan, |
| 195 |
spv::Op::OpSLessThanEqual}; |
| 196 |
|
| 197 |
std::vector<spv::Op> uint_gt_opcodes = {spv::Op::OpUGreaterThan, |
| 198 |
spv::Op::OpUGreaterThanEqual}; |
| 199 |
|
| 200 |
std::vector<spv::Op> uint_lt_opcodes = {spv::Op::OpULessThan, |
| 201 |
spv::Op::OpULessThanEqual}; |
| 202 |
|
| 203 |
#define CHECK_OPERATOR(USE_DESCRIPTOR, LHS_ID, RHS_ID, OPCODE, FRESH_ID) \ |
| 204 |
ASSERT_TRUE(TransformationReplaceBooleanConstantWithConstantBinary( \ |
| 205 |
USE_DESCRIPTOR, LHS_ID, RHS_ID, OPCODE, FRESH_ID) \ |
| 206 |
.IsApplicable(context.get(), transformation_context)); \ |
| 207 |
ASSERT_FALSE(TransformationReplaceBooleanConstantWithConstantBinary( \ |
| 208 |
USE_DESCRIPTOR, RHS_ID, LHS_ID, OPCODE, FRESH_ID) \ |
| 209 |
.IsApplicable(context.get(), transformation_context)); |
| 210 |
|
| 211 |
#define CHECK_TRANSFORMATION_APPLICABILITY(GT_OPCODES, LT_OPCODES, SMALL_ID, \ |
| 212 |
LARGE_ID) \ |
| 213 |
for (auto gt_opcode : GT_OPCODES) { \ |
| 214 |
for (auto& true_use : uses_of_true) { \ |
| 215 |
CHECK_OPERATOR(true_use, LARGE_ID, SMALL_ID, gt_opcode, fresh_id); \ |
| 216 |
} \ |
| 217 |
for (auto& false_use : uses_of_false) { \ |
| 218 |
CHECK_OPERATOR(false_use, SMALL_ID, LARGE_ID, gt_opcode, fresh_id); \ |
| 219 |
} \ |
| 220 |
} \ |
| 221 |
for (auto lt_opcode : LT_OPCODES) { \ |
| 222 |
for (auto& true_use : uses_of_true) { \ |
| 223 |
CHECK_OPERATOR(true_use, SMALL_ID, LARGE_ID, lt_opcode, fresh_id); \ |
| 224 |
} \ |
| 225 |
for (auto& false_use : uses_of_false) { \ |
| 226 |
CHECK_OPERATOR(false_use, LARGE_ID, SMALL_ID, lt_opcode, fresh_id); \ |
| 227 |
} \ |
| 228 |
} |
| 229 |
|
| 230 |
// Float |
| 231 |
{ CHECK_TRANSFORMATION_APPLICABILITY(fp_gt_opcodes, fp_lt_opcodes, 15, 17); } |
| 232 |
|
| 233 |
// Double |
| 234 |
{ CHECK_TRANSFORMATION_APPLICABILITY(fp_gt_opcodes, fp_lt_opcodes, 9, 11); } |
| 235 |
|
| 236 |
// Int32 |
| 237 |
{ |
| 238 |
CHECK_TRANSFORMATION_APPLICABILITY(int_gt_opcodes, int_lt_opcodes, 21, 23); |
| 239 |
} |
| 240 |
|
| 241 |
// Int64 |
| 242 |
{ |
| 243 |
CHECK_TRANSFORMATION_APPLICABILITY(int_gt_opcodes, int_lt_opcodes, 31, 33); |
| 244 |
} |
| 245 |
|
| 246 |
// Uint32 |
| 247 |
{ |
| 248 |
CHECK_TRANSFORMATION_APPLICABILITY(uint_gt_opcodes, uint_lt_opcodes, 27, |
| 249 |
29); |
| 250 |
} |
| 251 |
|
| 252 |
// Uint64 |
| 253 |
{ |
| 254 |
CHECK_TRANSFORMATION_APPLICABILITY(uint_gt_opcodes, uint_lt_opcodes, 35, |
| 255 |
37); |
| 256 |
} |
| 257 |
|
| 258 |
// Target id is not fresh |
| 259 |
ASSERT_FALSE(TransformationReplaceBooleanConstantWithConstantBinary( |
| 260 |
uses_of_true[0], 15, 17, spv::Op::OpFOrdLessThan, 15) |
| 261 |
.IsApplicable(context.get(), transformation_context)); |
| 262 |
|
| 263 |
// LHS id does not exist |
| 264 |
ASSERT_FALSE(TransformationReplaceBooleanConstantWithConstantBinary( |
| 265 |
uses_of_true[0], 300, 17, spv::Op::OpFOrdLessThan, 200) |
| 266 |
.IsApplicable(context.get(), transformation_context)); |
| 267 |
|
| 268 |
// RHS id does not exist |
| 269 |
ASSERT_FALSE(TransformationReplaceBooleanConstantWithConstantBinary( |
| 270 |
uses_of_true[0], 15, 300, spv::Op::OpFOrdLessThan, 200) |
| 271 |
.IsApplicable(context.get(), transformation_context)); |
| 272 |
|
| 273 |
// LHS and RHS ids do not match type |
| 274 |
ASSERT_FALSE(TransformationReplaceBooleanConstantWithConstantBinary( |
| 275 |
uses_of_true[0], 11, 17, spv::Op::OpFOrdLessThan, 200) |
| 276 |
.IsApplicable(context.get(), transformation_context)); |
| 277 |
|
| 278 |
// Opcode not appropriate |
| 279 |
ASSERT_FALSE(TransformationReplaceBooleanConstantWithConstantBinary( |
| 280 |
uses_of_true[0], 15, 17, spv::Op::OpFDiv, 200) |
| 281 |
.IsApplicable(context.get(), transformation_context)); |
| 282 |
|
| 283 |
auto replace_true_with_double_comparison = |
| 284 |
TransformationReplaceBooleanConstantWithConstantBinary( |
| 285 |
uses_of_true[0], 11, 9, spv::Op::OpFUnordGreaterThan, 100); |
| 286 |
auto replace_true_with_uint32_comparison = |
| 287 |
TransformationReplaceBooleanConstantWithConstantBinary( |
| 288 |
uses_of_true[1], 27, 29, spv::Op::OpULessThanEqual, 101); |
| 289 |
auto replace_false_with_float_comparison = |
| 290 |
TransformationReplaceBooleanConstantWithConstantBinary( |
| 291 |
uses_of_false[0], 17, 15, spv::Op::OpFOrdLessThan, 102); |
| 292 |
auto replace_false_with_sint64_comparison = |
| 293 |
TransformationReplaceBooleanConstantWithConstantBinary( |
| 294 |
uses_of_false[1], 33, 31, spv::Op::OpSLessThan, 103); |
| 295 |
|
| 296 |
ASSERT_TRUE(replace_true_with_double_comparison.IsApplicable( |
| 297 |
context.get(), transformation_context)); |
| 298 |
ApplyAndCheckFreshIds(replace_true_with_double_comparison, context.get(), |
| 299 |
&transformation_context); |
| 300 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 301 |
kConsoleMessageConsumer)); |
| 302 |
ASSERT_TRUE(replace_true_with_uint32_comparison.IsApplicable( |
| 303 |
context.get(), transformation_context)); |
| 304 |
ApplyAndCheckFreshIds(replace_true_with_uint32_comparison, context.get(), |
| 305 |
&transformation_context); |
| 306 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 307 |
kConsoleMessageConsumer)); |
| 308 |
ASSERT_TRUE(replace_false_with_float_comparison.IsApplicable( |
| 309 |
context.get(), transformation_context)); |
| 310 |
ApplyAndCheckFreshIds(replace_false_with_float_comparison, context.get(), |
| 311 |
&transformation_context); |
| 312 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 313 |
kConsoleMessageConsumer)); |
| 314 |
ASSERT_TRUE(replace_false_with_sint64_comparison.IsApplicable( |
| 315 |
context.get(), transformation_context)); |
| 316 |
ApplyAndCheckFreshIds(replace_false_with_sint64_comparison, context.get(), |
| 317 |
&transformation_context); |
| 318 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 319 |
kConsoleMessageConsumer)); |
| 320 |
|
| 321 |
std::string after = R"( |
| 322 |
OpCapability Shader |
| 323 |
OpCapability Float64 |
| 324 |
OpCapability Int64 |
| 325 |
%1 = OpExtInstImport "GLSL.std.450" |
| 326 |
OpMemoryModel Logical GLSL450 |
| 327 |
OpEntryPoint Fragment %4 "main" |
| 328 |
OpExecutionMode %4 OriginUpperLeft |
| 329 |
OpSource GLSL 450 |
| 330 |
OpName %4 "main" |
| 331 |
OpName %8 "d1" |
| 332 |
OpName %10 "d2" |
| 333 |
OpName %14 "f1" |
| 334 |
OpName %16 "f2" |
| 335 |
OpName %20 "i1" |
| 336 |
OpName %22 "i2" |
| 337 |
OpName %26 "u1" |
| 338 |
OpName %28 "u2" |
| 339 |
OpName %30 "i64_1" |
| 340 |
OpName %32 "i64_2" |
| 341 |
OpName %34 "u64_1" |
| 342 |
OpName %36 "u64_2" |
| 343 |
OpName %40 "b" |
| 344 |
OpName %42 "c" |
| 345 |
OpName %44 "d" |
| 346 |
%2 = OpTypeVoid |
| 347 |
%3 = OpTypeFunction %2 |
| 348 |
%6 = OpTypeFloat 64 |
| 349 |
%7 = OpTypePointer Function %6 |
| 350 |
%9 = OpConstant %6 1 |
| 351 |
%11 = OpConstant %6 2 |
| 352 |
%12 = OpTypeFloat 32 |
| 353 |
%13 = OpTypePointer Function %12 |
| 354 |
%15 = OpConstant %12 4 |
| 355 |
%17 = OpConstant %12 8 |
| 356 |
%18 = OpTypeInt 32 1 |
| 357 |
%60 = OpTypeInt 64 1 |
| 358 |
%61 = OpTypePointer Function %60 |
| 359 |
%19 = OpTypePointer Function %18 |
| 360 |
%21 = OpConstant %18 -100 |
| 361 |
%23 = OpConstant %18 200 |
| 362 |
%24 = OpTypeInt 32 0 |
| 363 |
%62 = OpTypeInt 64 0 |
| 364 |
%63 = OpTypePointer Function %62 |
| 365 |
%25 = OpTypePointer Function %24 |
| 366 |
%27 = OpConstant %24 300 |
| 367 |
%29 = OpConstant %24 400 |
| 368 |
%31 = OpConstant %60 -600 |
| 369 |
%33 = OpConstant %60 -500 |
| 370 |
%35 = OpConstant %62 700 |
| 371 |
%37 = OpConstant %62 800 |
| 372 |
%38 = OpTypeBool |
| 373 |
%39 = OpTypePointer Function %38 |
| 374 |
%41 = OpConstantTrue %38 |
| 375 |
%43 = OpConstantFalse %38 |
| 376 |
%4 = OpFunction %2 None %3 |
| 377 |
%5 = OpLabel |
| 378 |
%8 = OpVariable %7 Function |
| 379 |
%10 = OpVariable %7 Function |
| 380 |
%14 = OpVariable %13 Function |
| 381 |
%16 = OpVariable %13 Function |
| 382 |
%20 = OpVariable %19 Function |
| 383 |
%22 = OpVariable %19 Function |
| 384 |
%26 = OpVariable %25 Function |
| 385 |
%28 = OpVariable %25 Function |
| 386 |
%30 = OpVariable %61 Function |
| 387 |
%32 = OpVariable %61 Function |
| 388 |
%34 = OpVariable %63 Function |
| 389 |
%36 = OpVariable %63 Function |
| 390 |
%40 = OpVariable %39 Function |
| 391 |
%42 = OpVariable %39 Function |
| 392 |
%44 = OpVariable %39 Function |
| 393 |
OpStore %8 %9 |
| 394 |
OpStore %10 %11 |
| 395 |
OpStore %14 %15 |
| 396 |
OpStore %16 %17 |
| 397 |
OpStore %20 %21 |
| 398 |
OpStore %22 %23 |
| 399 |
OpStore %26 %27 |
| 400 |
OpStore %28 %29 |
| 401 |
OpStore %30 %31 |
| 402 |
OpStore %32 %33 |
| 403 |
OpStore %34 %35 |
| 404 |
OpStore %36 %37 |
| 405 |
%100 = OpFUnordGreaterThan %38 %11 %9 |
| 406 |
OpStore %40 %100 |
| 407 |
%102 = OpFOrdLessThan %38 %17 %15 |
| 408 |
OpStore %42 %102 |
| 409 |
%45 = OpLoad %38 %42 |
| 410 |
%101 = OpULessThanEqual %38 %27 %29 |
| 411 |
%46 = OpLogicalOr %38 %101 %45 |
| 412 |
OpStore %44 %46 |
| 413 |
%47 = OpLoad %38 %42 |
| 414 |
%103 = OpSLessThan %38 %33 %31 |
| 415 |
%48 = OpLogicalAnd %38 %47 %103 |
| 416 |
OpStore %42 %48 |
| 417 |
OpReturn |
| 418 |
OpFunctionEnd |
| 419 |
)"; |
| 420 |
ASSERT_TRUE(IsEqual(env, after, context.get())); |
| 421 |
|
| 422 |
if (std::numeric_limits<double>::has_quiet_NaN) { |
| 423 |
double quiet_nan_double = std::numeric_limits<double>::quiet_NaN(); |
| 424 |
uint32_t words[2]; |
| 425 |
memcpy(words, &quiet_nan_double, sizeof(double)); |
| 426 |
opt::Instruction::OperandList operands = { |
| 427 |
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {words[0]}}, |
| 428 |
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {words[1]}}}; |
| 429 |
context->module()->AddGlobalValue(MakeUnique<opt::Instruction>( |
| 430 |
context.get(), spv::Op::OpConstant, 6, 200, operands)); |
| 431 |
fuzzerutil::UpdateModuleIdBound(context.get(), 200); |
| 432 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed( |
| 433 |
context.get(), validator_options, kConsoleMessageConsumer)); |
| 434 |
// The transformation is not applicable because %200 is NaN. |
| 435 |
ASSERT_FALSE(TransformationReplaceBooleanConstantWithConstantBinary( |
| 436 |
uses_of_true[0], 11, 200, spv::Op::OpFOrdLessThan, 300) |
| 437 |
.IsApplicable(context.get(), transformation_context)); |
| 438 |
} |
| 439 |
if (std::numeric_limits<double>::has_infinity) { |
| 440 |
double positive_infinity_double = std::numeric_limits<double>::infinity(); |
| 441 |
uint32_t words[2]; |
| 442 |
memcpy(words, &positive_infinity_double, sizeof(double)); |
| 443 |
opt::Instruction::OperandList operands = { |
| 444 |
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {words[0]}}, |
| 445 |
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {words[1]}}}; |
| 446 |
context->module()->AddGlobalValue(MakeUnique<opt::Instruction>( |
| 447 |
context.get(), spv::Op::OpConstant, 6, 201, operands)); |
| 448 |
fuzzerutil::UpdateModuleIdBound(context.get(), 201); |
| 449 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed( |
| 450 |
context.get(), validator_options, kConsoleMessageConsumer)); |
| 451 |
// Even though the double constant %11 is less than the infinity %201, the |
| 452 |
// transformation is restricted to only apply to finite values. |
| 453 |
ASSERT_FALSE(TransformationReplaceBooleanConstantWithConstantBinary( |
| 454 |
uses_of_true[0], 11, 201, spv::Op::OpFOrdLessThan, 300) |
| 455 |
.IsApplicable(context.get(), transformation_context)); |
| 456 |
} |
| 457 |
if (std::numeric_limits<float>::has_infinity) { |
| 458 |
float positive_infinity_float = std::numeric_limits<float>::infinity(); |
| 459 |
float negative_infinity_float = -1 * positive_infinity_float; |
| 460 |
uint32_t words_positive_infinity[1]; |
| 461 |
uint32_t words_negative_infinity[1]; |
| 462 |
memcpy(words_positive_infinity, &positive_infinity_float, sizeof(float)); |
| 463 |
memcpy(words_negative_infinity, &negative_infinity_float, sizeof(float)); |
| 464 |
opt::Instruction::OperandList operands_positive_infinity = { |
| 465 |
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {words_positive_infinity[0]}}}; |
| 466 |
context->module()->AddGlobalValue( |
| 467 |
MakeUnique<opt::Instruction>(context.get(), spv::Op::OpConstant, 12, |
| 468 |
202, operands_positive_infinity)); |
| 469 |
fuzzerutil::UpdateModuleIdBound(context.get(), 202); |
| 470 |
opt::Instruction::OperandList operands = { |
| 471 |
{SPV_OPERAND_TYPE_LITERAL_INTEGER, {words_negative_infinity[0]}}}; |
| 472 |
context->module()->AddGlobalValue(MakeUnique<opt::Instruction>( |
| 473 |
context.get(), spv::Op::OpConstant, 12, 203, operands)); |
| 474 |
fuzzerutil::UpdateModuleIdBound(context.get(), 203); |
| 475 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed( |
| 476 |
context.get(), validator_options, kConsoleMessageConsumer)); |
| 477 |
// Even though the negative infinity at %203 is less than the positive |
| 478 |
// infinity %202, the transformation is restricted to only apply to finite |
| 479 |
// values. |
| 480 |
ASSERT_FALSE(TransformationReplaceBooleanConstantWithConstantBinary( |
| 481 |
uses_of_true[0], 203, 202, spv::Op::OpFOrdLessThan, 300) |
| 482 |
.IsApplicable(context.get(), transformation_context)); |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
TEST(TransformationReplaceBooleanConstantWithConstantBinaryTest, |
| 487 |
MergeInstructions) { |
| 488 |
// The test came from the following GLSL: |
| 489 |
// |
| 490 |
// void main() { |
| 491 |
// int x = 1; |
| 492 |
// int y = 2; |
| 493 |
// if (true) { |
| 494 |
// x = 2; |
| 495 |
// } |
| 496 |
// while(false) { |
| 497 |
// y = 2; |
| 498 |
// } |
| 499 |
// } |
| 500 |
|
| 501 |
std::string shader = R"( |
| 502 |
OpCapability Shader |
| 503 |
%1 = OpExtInstImport "GLSL.std.450" |
| 504 |
OpMemoryModel Logical GLSL450 |
| 505 |
OpEntryPoint Fragment %4 "main" |
| 506 |
OpExecutionMode %4 OriginUpperLeft |
| 507 |
OpSource GLSL 450 |
| 508 |
OpName %4 "main" |
| 509 |
OpName %8 "x" |
| 510 |
OpName %10 "y" |
| 511 |
%2 = OpTypeVoid |
| 512 |
%3 = OpTypeFunction %2 |
| 513 |
%6 = OpTypeInt 32 1 |
| 514 |
%7 = OpTypePointer Function %6 |
| 515 |
%9 = OpConstant %6 1 |
| 516 |
%11 = OpConstant %6 2 |
| 517 |
%12 = OpTypeBool |
| 518 |
%13 = OpConstantTrue %12 |
| 519 |
%21 = OpConstantFalse %12 |
| 520 |
%4 = OpFunction %2 None %3 |
| 521 |
%5 = OpLabel |
| 522 |
%8 = OpVariable %7 Function |
| 523 |
%10 = OpVariable %7 Function |
| 524 |
OpStore %8 %9 |
| 525 |
OpStore %10 %11 |
| 526 |
OpSelectionMerge %15 None |
| 527 |
OpBranchConditional %13 %14 %15 |
| 528 |
%14 = OpLabel |
| 529 |
OpStore %8 %11 |
| 530 |
OpBranch %15 |
| 531 |
%15 = OpLabel |
| 532 |
OpBranch %16 |
| 533 |
%16 = OpLabel |
| 534 |
OpLoopMerge %18 %19 None |
| 535 |
OpBranchConditional %21 %17 %18 |
| 536 |
%17 = OpLabel |
| 537 |
OpStore %10 %11 |
| 538 |
OpBranch %19 |
| 539 |
%19 = OpLabel |
| 540 |
OpBranch %16 |
| 541 |
%18 = OpLabel |
| 542 |
OpReturn |
| 543 |
OpFunctionEnd |
| 544 |
)"; |
| 545 |
|
| 546 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 547 |
const auto consumer = nullptr; |
| 548 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 549 |
spvtools::ValidatorOptions validator_options; |
| 550 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 551 |
kConsoleMessageConsumer)); |
| 552 |
TransformationContext transformation_context( |
| 553 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 554 |
auto use_of_true_in_if = MakeIdUseDescriptor( |
| 555 |
13, MakeInstructionDescriptor(10, spv::Op::OpBranchConditional, 0), 0); |
| 556 |
auto use_of_false_in_while = MakeIdUseDescriptor( |
| 557 |
21, MakeInstructionDescriptor(16, spv::Op::OpBranchConditional, 0), 0); |
| 558 |
|
| 559 |
auto replacement_1 = TransformationReplaceBooleanConstantWithConstantBinary( |
| 560 |
use_of_true_in_if, 9, 11, spv::Op::OpSLessThan, 100); |
| 561 |
auto replacement_2 = TransformationReplaceBooleanConstantWithConstantBinary( |
| 562 |
use_of_false_in_while, 9, 11, spv::Op::OpSGreaterThanEqual, 101); |
| 563 |
|
| 564 |
ASSERT_TRUE( |
| 565 |
replacement_1.IsApplicable(context.get(), transformation_context)); |
| 566 |
ApplyAndCheckFreshIds(replacement_1, context.get(), &transformation_context); |
| 567 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 568 |
kConsoleMessageConsumer)); |
| 569 |
|
| 570 |
ASSERT_TRUE( |
| 571 |
replacement_2.IsApplicable(context.get(), transformation_context)); |
| 572 |
ApplyAndCheckFreshIds(replacement_2, context.get(), &transformation_context); |
| 573 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 574 |
kConsoleMessageConsumer)); |
| 575 |
|
| 576 |
std::string after = R"( |
| 577 |
OpCapability Shader |
| 578 |
%1 = OpExtInstImport "GLSL.std.450" |
| 579 |
OpMemoryModel Logical GLSL450 |
| 580 |
OpEntryPoint Fragment %4 "main" |
| 581 |
OpExecutionMode %4 OriginUpperLeft |
| 582 |
OpSource GLSL 450 |
| 583 |
OpName %4 "main" |
| 584 |
OpName %8 "x" |
| 585 |
OpName %10 "y" |
| 586 |
%2 = OpTypeVoid |
| 587 |
%3 = OpTypeFunction %2 |
| 588 |
%6 = OpTypeInt 32 1 |
| 589 |
%7 = OpTypePointer Function %6 |
| 590 |
%9 = OpConstant %6 1 |
| 591 |
%11 = OpConstant %6 2 |
| 592 |
%12 = OpTypeBool |
| 593 |
%13 = OpConstantTrue %12 |
| 594 |
%21 = OpConstantFalse %12 |
| 595 |
%4 = OpFunction %2 None %3 |
| 596 |
%5 = OpLabel |
| 597 |
%8 = OpVariable %7 Function |
| 598 |
%10 = OpVariable %7 Function |
| 599 |
OpStore %8 %9 |
| 600 |
OpStore %10 %11 |
| 601 |
%100 = OpSLessThan %12 %9 %11 |
| 602 |
OpSelectionMerge %15 None |
| 603 |
OpBranchConditional %100 %14 %15 |
| 604 |
%14 = OpLabel |
| 605 |
OpStore %8 %11 |
| 606 |
OpBranch %15 |
| 607 |
%15 = OpLabel |
| 608 |
OpBranch %16 |
| 609 |
%16 = OpLabel |
| 610 |
%101 = OpSGreaterThanEqual %12 %9 %11 |
| 611 |
OpLoopMerge %18 %19 None |
| 612 |
OpBranchConditional %101 %17 %18 |
| 613 |
%17 = OpLabel |
| 614 |
OpStore %10 %11 |
| 615 |
OpBranch %19 |
| 616 |
%19 = OpLabel |
| 617 |
OpBranch %16 |
| 618 |
%18 = OpLabel |
| 619 |
OpReturn |
| 620 |
OpFunctionEnd |
| 621 |
)"; |
| 622 |
|
| 623 |
ASSERT_TRUE(IsEqual(env, after, context.get())); |
| 624 |
} |
| 625 |
|
| 626 |
TEST(TransformationReplaceBooleanConstantWithConstantBinaryTest, OpPhi) { |
| 627 |
// Hand-written SPIR-V to check applicability of the transformation on an |
| 628 |
// OpPhi argument. |
| 629 |
|
| 630 |
std::string reference_shader = R"( |
| 631 |
OpCapability Shader |
| 632 |
%1 = OpExtInstImport "GLSL.std.450" |
| 633 |
OpMemoryModel Logical GLSL450 |
| 634 |
OpEntryPoint Vertex %10 "main" |
| 635 |
|
| 636 |
; Types |
| 637 |
%2 = OpTypeVoid |
| 638 |
%3 = OpTypeFunction %2 |
| 639 |
%4 = OpTypeInt 32 0 |
| 640 |
%5 = OpTypeBool |
| 641 |
|
| 642 |
; Constants |
| 643 |
%6 = OpConstant %4 0 |
| 644 |
%7 = OpConstant %4 1 |
| 645 |
%8 = OpConstantTrue %5 |
| 646 |
%9 = OpConstantFalse %5 |
| 647 |
|
| 648 |
; main function |
| 649 |
%10 = OpFunction %2 None %3 |
| 650 |
%11 = OpLabel |
| 651 |
OpSelectionMerge %13 None |
| 652 |
OpBranchConditional %8 %12 %13 |
| 653 |
%12 = OpLabel |
| 654 |
OpBranch %13 |
| 655 |
%13 = OpLabel |
| 656 |
%14 = OpPhi %5 %8 %11 %9 %12 |
| 657 |
OpReturn |
| 658 |
OpFunctionEnd |
| 659 |
)"; |
| 660 |
|
| 661 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 662 |
const auto consumer = nullptr; |
| 663 |
const auto context = |
| 664 |
BuildModule(env, consumer, reference_shader, kFuzzAssembleOption); |
| 665 |
spvtools::ValidatorOptions validator_options; |
| 666 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 667 |
kConsoleMessageConsumer)); |
| 668 |
TransformationContext transformation_context( |
| 669 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 670 |
auto instruction_descriptor = |
| 671 |
MakeInstructionDescriptor(14, spv::Op::OpPhi, 0); |
| 672 |
auto id_use_descriptor = MakeIdUseDescriptor(8, instruction_descriptor, 0); |
| 673 |
auto transformation = TransformationReplaceBooleanConstantWithConstantBinary( |
| 674 |
id_use_descriptor, 6, 7, spv::Op::OpULessThan, 15); |
| 675 |
ASSERT_TRUE( |
| 676 |
transformation.IsApplicable(context.get(), transformation_context)); |
| 677 |
ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context); |
| 678 |
|
| 679 |
std::string variant_shader = R"( |
| 680 |
OpCapability Shader |
| 681 |
%1 = OpExtInstImport "GLSL.std.450" |
| 682 |
OpMemoryModel Logical GLSL450 |
| 683 |
OpEntryPoint Vertex %10 "main" |
| 684 |
|
| 685 |
; Types |
| 686 |
%2 = OpTypeVoid |
| 687 |
%3 = OpTypeFunction %2 |
| 688 |
%4 = OpTypeInt 32 0 |
| 689 |
%5 = OpTypeBool |
| 690 |
|
| 691 |
; Constants |
| 692 |
%6 = OpConstant %4 0 |
| 693 |
%7 = OpConstant %4 1 |
| 694 |
%8 = OpConstantTrue %5 |
| 695 |
%9 = OpConstantFalse %5 |
| 696 |
|
| 697 |
; main function |
| 698 |
%10 = OpFunction %2 None %3 |
| 699 |
%11 = OpLabel |
| 700 |
%15 = OpULessThan %5 %6 %7 |
| 701 |
OpSelectionMerge %13 None |
| 702 |
OpBranchConditional %8 %12 %13 |
| 703 |
%12 = OpLabel |
| 704 |
OpBranch %13 |
| 705 |
%13 = OpLabel |
| 706 |
%14 = OpPhi %5 %15 %11 %9 %12 |
| 707 |
OpReturn |
| 708 |
OpFunctionEnd |
| 709 |
)"; |
| 710 |
|
| 711 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 712 |
kConsoleMessageConsumer)); |
| 713 |
ASSERT_TRUE(IsEqual(env, variant_shader, context.get())); |
| 714 |
} |
| 715 |
|
| 716 |
TEST(TransformationReplaceBooleanConstantWithConstantBinaryTest, |
| 717 |
DoNotReplaceVariableInitializer) { |
| 718 |
std::string shader = R"( |
| 719 |
OpCapability Shader |
| 720 |
%1 = OpExtInstImport "GLSL.std.450" |
| 721 |
OpMemoryModel Logical GLSL450 |
| 722 |
OpEntryPoint Fragment %4 "main" |
| 723 |
OpExecutionMode %4 OriginUpperLeft |
| 724 |
OpSource ESSL 310 |
| 725 |
OpName %4 "main" |
| 726 |
%2 = OpTypeVoid |
| 727 |
%3 = OpTypeFunction %2 |
| 728 |
%6 = OpTypeBool |
| 729 |
%7 = OpTypePointer Function %6 |
| 730 |
%9 = OpConstantTrue %6 |
| 731 |
%10 = OpTypeInt 32 1 |
| 732 |
%13 = OpConstant %10 0 |
| 733 |
%15 = OpConstant %10 1 |
| 734 |
%4 = OpFunction %2 None %3 |
| 735 |
%5 = OpLabel |
| 736 |
%50 = OpVariable %7 Function %9 |
| 737 |
OpReturn |
| 738 |
OpFunctionEnd |
| 739 |
)"; |
| 740 |
|
| 741 |
const auto env = SPV_ENV_UNIVERSAL_1_3; |
| 742 |
const auto consumer = nullptr; |
| 743 |
const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption); |
| 744 |
spvtools::ValidatorOptions validator_options; |
| 745 |
ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options, |
| 746 |
kConsoleMessageConsumer)); |
| 747 |
TransformationContext transformation_context( |
| 748 |
MakeUnique<FactManager>(context.get()), validator_options); |
| 749 |
ASSERT_FALSE( |
| 750 |
TransformationReplaceBooleanConstantWithConstantBinary( |
| 751 |
MakeIdUseDescriptor( |
| 752 |
9, MakeInstructionDescriptor(50, spv::Op::OpVariable, 0), 1), |
| 753 |
13, 15, spv::Op::OpSLessThan, 100) |
| 754 |
.IsApplicable(context.get(), transformation_context)); |
| 755 |
} |
| 756 |
|
| 757 |
} // namespace |
| 758 |
} // namespace fuzz |
| 759 |
} // namespace spvtools |