Skip to content

Commit a62abcb

Browse files
authored
[OPT] Don't fold n % 1.0 (#6100)
If values are not whole number, then the result is not 0. For example, `(1.5 % 1.0) == 0.5`. Fixes #6097
1 parent 506fee4 commit a62abcb

2 files changed

Lines changed: 4 additions & 17 deletions

File tree

source/opt/folding_rules.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,7 +2466,7 @@ FoldingRule RedundantFDiv() {
24662466
}
24672467

24682468
FoldingRule RedundantFMod() {
2469-
return [](IRContext* context, Instruction* inst,
2469+
return [](IRContext*, Instruction* inst,
24702470
const std::vector<const analysis::Constant*>& constants) {
24712471
assert(inst->opcode() == spv::Op::OpFMod &&
24722472
"Wrong opcode. Should be OpFMod.");
@@ -2477,7 +2477,6 @@ FoldingRule RedundantFMod() {
24772477
}
24782478

24792479
FloatConstantKind kind0 = getFloatConstantKind(constants[0]);
2480-
FloatConstantKind kind1 = getFloatConstantKind(constants[1]);
24812480

24822481
if (kind0 == FloatConstantKind::Zero) {
24832482
inst->SetOpcode(spv::Op::OpCopyObject);
@@ -2486,19 +2485,6 @@ FoldingRule RedundantFMod() {
24862485
return true;
24872486
}
24882487

2489-
if (kind1 == FloatConstantKind::One) {
2490-
auto type = context->get_type_mgr()->GetType(inst->type_id());
2491-
std::vector<uint32_t> zero_words;
2492-
zero_words.resize(ElementWidth(type) / 32);
2493-
auto const_mgr = context->get_constant_mgr();
2494-
auto zero = const_mgr->GetConstant(type, std::move(zero_words));
2495-
auto zero_id = const_mgr->GetDefiningInstruction(zero)->result_id();
2496-
2497-
inst->SetOpcode(spv::Op::OpCopyObject);
2498-
inst->SetInOperands({{SPV_OPERAND_TYPE_ID, {zero_id}}});
2499-
return true;
2500-
}
2501-
25022488
return false;
25032489
};
25042490
}

test/opt/fold_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4609,7 +4609,8 @@ INSTANTIATE_TEST_SUITE_P(FloatRedundantFoldingTest, GeneralInstructionFoldingTes
46094609
"OpReturn\n" +
46104610
"OpFunctionEnd",
46114611
2, 3),
4612-
// Test case 9: Fold n % 1.0
4612+
// Test case 9: Don't fold n % 1.0
4613+
// If `n` is not a whole number, the answer is not 0.
46134614
InstructionFoldingCase<uint32_t>(
46144615
Header() + "%main = OpFunction %void None %void_func\n" +
46154616
"%main_lab = OpLabel\n" +
@@ -4618,7 +4619,7 @@ INSTANTIATE_TEST_SUITE_P(FloatRedundantFoldingTest, GeneralInstructionFoldingTes
46184619
"%2 = OpFMod %float %3 %float_1\n" +
46194620
"OpReturn\n" +
46204621
"OpFunctionEnd",
4621-
2, FLOAT_0_ID),
4622+
2, 0),
46224623
// Test case 10: Fold n * 0.0
46234624
InstructionFoldingCase<uint32_t>(
46244625
Header() + "%main = OpFunction %void None %void_func\n" +

0 commit comments

Comments
 (0)