Skip to content

Commit ff20688

Browse files
agrabezhigcbot
authored andcommitted
Process PHI Nodes with zeros in CustomUnsafeOptimization pass
Process PHI Nodes with floating point zeros in CustomUnsafeOptimization pass. Adding `FNeg` support to SetFastMathFlag pass.
1 parent 3452b57 commit ff20688

File tree

4 files changed

+84
-8
lines changed

4 files changed

+84
-8
lines changed

IGC/Compiler/CustomUnsafeOptPass.cpp

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,9 @@ char CustomUnsafeOptPass::ID = 0;
8282
STATISTIC(Stat_FcmpRemoved, "Number of insts removed in FCmp Opt");
8383
STATISTIC(Stat_FloatRemoved, "Number of insts removed in Float Opt");
8484

85-
static bool allowUnsafeMathOpt(CodeGenContext* ctx, llvm::BinaryOperator& op)
85+
static bool allowUnsafeMathOpt(CodeGenContext* ctx)
8686
{
87-
// always allow unsafe opt if instruction has the flag
88-
if (llvm::isa<llvm::FPMathOperator>(op) && op.getFastMathFlags().isFast())
89-
{
90-
return true;
91-
}
92-
93-
// then check compiler options in metadata
87+
// check compiler options in metadata
9488
if ((!ctx->m_checkFastFlagPerInstructionInCustomUnsafeOptPass &&
9589
ctx->getModuleMetaData()->compOpt.FastRelaxedMath) ||
9690
ctx->getModuleMetaData()->compOpt.UnsafeMathOptimizations)
@@ -106,6 +100,18 @@ static bool allowUnsafeMathOpt(CodeGenContext* ctx, llvm::BinaryOperator& op)
106100
return false;
107101
}
108102

103+
static bool allowUnsafeMathOpt(CodeGenContext* ctx, llvm::BinaryOperator& op)
104+
{
105+
// always allow unsafe opt if instruction has the flag
106+
if (llvm::isa<llvm::FPMathOperator>(op) && op.getFastMathFlags().isFast())
107+
{
108+
return true;
109+
}
110+
111+
// then check compiler options in metadata
112+
return allowUnsafeMathOpt(ctx);
113+
}
114+
109115
CustomUnsafeOptPass::CustomUnsafeOptPass()
110116
: FunctionPass(ID),
111117
m_disableReorderingOpt(0),
@@ -166,6 +172,31 @@ void CustomUnsafeOptPass::visitInstruction(Instruction& I)
166172
// nothing
167173
}
168174

175+
void CustomUnsafeOptPass::visitPHINode(llvm::PHINode& I)
176+
{
177+
/*
178+
From
179+
%zero = phi float [ -0.000000e+00, %if.then ], [ 0.000000e+00, %for.body ]
180+
%mul = fmul fast float %zero, %smth
181+
To
182+
%mul = fmul fast float 0.0, %smth
183+
*/
184+
if (I.getType()->isFloatingPointTy() && allowUnsafeMathOpt(m_ctx))
185+
{
186+
bool isZeroFP = llvm::all_of(I.incoming_values(), [](Use& use)
187+
{
188+
return isa<ConstantFP>(use.get()) && cast<ConstantFP>(use.get())->isZero();
189+
});
190+
191+
if (isZeroFP)
192+
{
193+
I.replaceAllUsesWith(ConstantFP::get(I.getType(), 0.0));
194+
I.eraseFromParent();
195+
m_isChanged = true;
196+
}
197+
}
198+
}
199+
169200
void CustomUnsafeOptPass::visitFPToSIInst(llvm::FPToSIInst& I)
170201
{
171202
/*

IGC/Compiler/CustomUnsafeOptPass.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ namespace IGC
4848
}
4949

5050
void visitInstruction(llvm::Instruction& I);
51+
void visitPHINode(llvm::PHINode& I);
5152
void visitBinaryOperator(llvm::BinaryOperator& I);
5253
void visitFCmpInst(llvm::FCmpInst& FC);
5354
void visitSelectInst(llvm::SelectInst& I);

IGC/Compiler/Optimizer/OpenCLPasses/SetFastMathFlags/SetFastMathFlags.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ bool SetFastMathFlags::setFlags(Function& F, FastMathFlags fmfs)
121121
{
122122
unsigned int op = i->getOpcode();
123123
switch (op) {
124+
case Instruction::FNeg:
124125
case Instruction::FAdd:
125126
case Instruction::FSub:
126127
case Instruction::FMul:
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2024 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
9+
; RUN: igc_opt %s -S -o - -igc-custom-unsafe-opt-pass | FileCheck %s
10+
11+
; tests CustomUnsafeOptPass::visitPHINode
12+
; Transformation:
13+
; From
14+
; %zero = phi float [ -0.000000e+00, %if.then ], [ 0.000000e+00, %for.body ]
15+
; %op = fmul fast float %zero, %smth
16+
; To
17+
; %op = fmul fast float 0.0, %smth
18+
19+
define float @test_phi(i1 %flag, float %in) {
20+
; CHECK-LABEL: @test_phi(
21+
; CHECK: if.end:
22+
; CHECK-NOT: %zero.phi = phi float
23+
; CHECK: ret float %in
24+
entry:
25+
br i1 %flag, label %if.then, label %if.end
26+
27+
if.then: ; preds = %entry
28+
br label %if.end
29+
30+
if.end: ; preds = %if.then, %entry
31+
%zero.phi = phi float [ -0.000000e+00, %if.then ], [ 0.000000e+00, %entry ]
32+
%mul = fmul fast float %zero.phi, 0x3FF3333340000000
33+
%add = fadd fast float %in, %mul
34+
ret float %add
35+
}
36+
37+
!IGCMetadata = !{!0}
38+
39+
!0 = !{!"ModuleMD", !1}
40+
!1 = !{!"compOpt", !2, !3, !4}
41+
!2 = !{!"FastRelaxedMath", i1 true}
42+
!3 = !{!"UnsafeMathOptimizations", i1 false}
43+
!4 = !{!"disableCustomUnsafeOpts", i1 false}

0 commit comments

Comments
 (0)