spirv-fuzz: Fix def-use update in PermutePhiOperands (#4309)
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.
diff --git a/source/fuzz/transformation_permute_phi_operands.cpp b/source/fuzz/transformation_permute_phi_operands.cpp
index 6056331..7ee7a82 100644
--- a/source/fuzz/transformation_permute_phi_operands.cpp
+++ b/source/fuzz/transformation_permute_phi_operands.cpp
@@ -81,8 +81,7 @@
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 3df399a..0774dcf 100644
--- a/test/fuzz/transformation_permute_phi_operands_test.cpp
+++ b/test/fuzz/transformation_permute_phi_operands_test.cpp
@@ -60,6 +60,7 @@
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 @@
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 @@
OpBranch %17
%17 = OpLabel
%25 = OpPhi %6 %24 %21 %20 %16
+ %30 = OpIAdd %6 %25 %25
OpStore %8 %25
OpReturn
OpFunctionEnd