From 14712ad1536bc396f80b46561e05302a7715a8c9 Mon Sep 17 00:00:00 2001 From: "Alastair F. Donaldson" Date: Fri, 28 May 2021 00:18:21 +0100 Subject: [PATCH] spirv-fuzz: Fix def-use update in PermutePhiOperands The def-use manager was being incorrectly updated in TransformationPermutePhiOperands, and this was causing future transformations to go wrong during fuzzing. This change updates the def-use manager in a correct manner, and adds a test exposing the previous bug. Fixes #4300. --- .../transformation_permute_phi_operands.cpp | 3 +-- ...ansformation_permute_phi_operands_test.cpp | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/source/fuzz/transformation_permute_phi_operands.cpp b/source/fuzz/transformation_permute_phi_operands.cpp index 605633124a..7ee7a82fa7 100644 --- a/source/fuzz/transformation_permute_phi_operands.cpp +++ b/source/fuzz/transformation_permute_phi_operands.cpp @@ -81,8 +81,7 @@ void TransformationPermutePhiOperands::Apply( inst->SetInOperands(std::move(permuted_operands)); // Update the def-use manager. - ir_context->get_def_use_mgr()->ClearInst(inst); - ir_context->get_def_use_mgr()->AnalyzeInstDefUse(inst); + ir_context->UpdateDefUse(inst); } protobufs::Transformation TransformationPermutePhiOperands::ToMessage() const { diff --git a/test/fuzz/transformation_permute_phi_operands_test.cpp b/test/fuzz/transformation_permute_phi_operands_test.cpp index 3df399a55c..0774dcf3d0 100644 --- a/test/fuzz/transformation_permute_phi_operands_test.cpp +++ b/test/fuzz/transformation_permute_phi_operands_test.cpp @@ -60,6 +60,7 @@ TEST(TransformationPermutePhiOperandsTest, BasicTest) { OpBranch %17 %17 = OpLabel %25 = OpPhi %6 %20 %16 %24 %21 + %30 = OpIAdd %6 %25 %25 OpStore %8 %25 OpReturn OpFunctionEnd @@ -121,6 +122,30 @@ TEST(TransformationPermutePhiOperandsTest, BasicTest) { return true; }); } + bool found_use_in_store = false; + bool found_use_in_add_lhs = false; + bool found_use_in_add_rhs = false; + context->get_def_use_mgr()->ForEachUse( + 25, [&found_use_in_store, &found_use_in_add_lhs, &found_use_in_add_rhs]( + opt::Instruction* inst, uint32_t operand_index) { + if (inst->opcode() == SpvOpStore) { + ASSERT_FALSE(found_use_in_store); + found_use_in_store = true; + } else { + ASSERT_EQ(SpvOpIAdd, inst->opcode()); + if (operand_index == 2) { + ASSERT_FALSE(found_use_in_add_lhs); + found_use_in_add_lhs = true; + } else { + ASSERT_EQ(3, operand_index); + ASSERT_FALSE(found_use_in_add_rhs); + found_use_in_add_rhs = true; + } + } + }); + ASSERT_TRUE(found_use_in_store); + ASSERT_TRUE(found_use_in_add_lhs); + ASSERT_TRUE(found_use_in_add_rhs); std::string after_transformation = R"( OpCapability Shader @@ -159,6 +184,7 @@ TEST(TransformationPermutePhiOperandsTest, BasicTest) { OpBranch %17 %17 = OpLabel %25 = OpPhi %6 %24 %21 %20 %16 + %30 = OpIAdd %6 %25 %25 OpStore %8 %25 OpReturn OpFunctionEnd