Skip to content

Commit

Permalink
spirv-fuzz: Fix def-use update in PermutePhiOperands (KhronosGroup#4309)
Browse files Browse the repository at this point in the history
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 KhronosGroup#4300.
  • Loading branch information
afd authored and sliu-UIUC committed Jun 1, 2021
1 parent 646d9da commit a2f2362
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
3 changes: 1 addition & 2 deletions source/fuzz/transformation_permute_phi_operands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions test/fuzz/transformation_permute_phi_operands_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a2f2362

Please sign in to comment.