Skip to content

Commit 8fb2cb3

Browse files
fda0igcbot
authored andcommitted
Disable optimizations for some math builtins
Don't apply fast math flags to selected math builtins. Disable module-wide unsafe math optimizations. Update CustomUnsafeOptPass tests to use fast math flag on arithmetic floating point operations. Fix regressions in CustomUnsafeOptPass by propagating fast math flags to newly created fsub and fdiv instructions. Keep handling xor optimizations by using an old method of looking up module metadata for FastRelaxedMath flag.
1 parent 997fe56 commit 8fb2cb3

File tree

21 files changed

+204
-284
lines changed

21 files changed

+204
-284
lines changed

IGC/Compiler/CustomUnsafeOptPass.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,6 @@ static bool allowUnsafeMathOpt(CodeGenContext* ctx, llvm::BinaryOperator& op)
9090
return true;
9191
}
9292

93-
// then checking compiler options in metadata
94-
if (ctx->getModuleMetaData()->compOpt.FastRelaxedMath)
95-
{
96-
return true;
97-
}
98-
9993
if (IGC_IS_FLAG_ENABLED(EnableFastMath))
10094
{
10195
return true;
@@ -1213,7 +1207,7 @@ bool CustomUnsafeOptPass::visitBinaryOperatorNegateMultiply(BinaryOperator& I)
12131207
// otherwise replace mul src0 with the negate
12141208
if (!replaced)
12151209
{
1216-
fmulInst->setOperand(0, BinaryOperator::CreateFSub(ConstantFP::get(fmulInst->getType(), 0), fmulInst->getOperand(0), "", fmulInst));
1210+
fmulInst->setOperand(0, copyIRFlags(BinaryOperator::CreateFSub(ConstantFP::get(fmulInst->getType(), 0), fmulInst->getOperand(0), "", fmulInst), &I));
12171211

12181212
// DIExpression in debug variable instructions must be extended with additional DWARF opcode:
12191213
// DW_OP_neg
@@ -1489,7 +1483,7 @@ bool CustomUnsafeOptPass::visitBinaryOperatorAddDiv(BinaryOperator& I)
14891483
{
14901484
if (faddInst->getOperand(i) == I.getOperand(1))
14911485
{
1492-
Value* div = BinaryOperator::CreateFDiv(faddInst->getOperand(1 - i), I.getOperand(1), "", faddInst);
1486+
Value* div = copyIRFlags(BinaryOperator::CreateFDiv(faddInst->getOperand(1 - i), I.getOperand(1), "", faddInst), &I);
14931487
const DebugLoc& DL = faddInst->getDebugLoc();
14941488
if (Instruction* divInst = dyn_cast<Instruction>(div))
14951489
divInst->setDebugLoc(DL);
@@ -1905,7 +1899,14 @@ void CustomUnsafeOptPass::visitBinaryOperator(BinaryOperator& I)
19051899
}
19061900
else if (I.getOpcode() == Instruction::Xor)
19071901
{
1908-
m_isChanged = visitBinaryOperatorXor(I);
1902+
m_isChanged |= visitBinaryOperatorXor(I);
1903+
}
1904+
}
1905+
else if (m_ctx->getModuleMetaData()->compOpt.FastRelaxedMath)
1906+
{
1907+
if (I.getOpcode() == Instruction::Xor)
1908+
{
1909+
m_isChanged |= visitBinaryOperatorXor(I);
19091910
}
19101911
}
19111912
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ bool SetFastMathFlags::setFlags(Function& F, FastMathFlags fmfs)
108108
{
109109
return false;
110110
}
111+
112+
StringRef fName = F.getName();
113+
if (fName.equals("__ocl_svml_cos") ||
114+
fName.equals("__ocl_svml_sin"))
115+
{
116+
return false;
117+
}
118+
111119
bool changed = false;
112120
for (inst_iterator i = inst_begin(&F), e = inst_end(&F); i != e; ++i)
113121
{

IGC/Compiler/tests/CustomUnsafeOptPass/exchange_cb.ll

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,18 @@
1616
define float @test1(float %x, float addrspace(65536)* %cb1, float addrspace(65536)* %cb2) #0 {
1717
entry:
1818
%0 = load float, float addrspace(65536)* %cb1
19-
%1 = fmul float %x, %0
19+
%1 = fmul fast float %x, %0
2020
%2 = load float, float addrspace(65536)* %cb2
21-
%3 = fmul float %x, %2
22-
%4 = fadd float %1, %3
21+
%3 = fmul fast float %x, %2
22+
%4 = fadd fast float %1, %3
2323
ret float %4
2424
}
2525

2626
; CHECK-LABEL: define float @test1
2727
; CHECK: [[CB1:%[A-z0-9]*]] = load float, float addrspace(65536)* %cb1
28-
; CHECK-NOT: fmul float %x, [[CB1]]
28+
; CHECK-NOT: fmul fast float %x, [[CB1]]
2929
; CHECK: [[CB2:%[A-z0-9]*]] = load float, float addrspace(65536)* %cb2
30-
; CHECK-NOT: fmul float %x, [[CB2]]
31-
; CHECK: [[ADD:%[A-z0-9]*]] = fadd float [[CB1]], [[CB2]]
32-
; CHECK: [[MUL:%[A-z0-9]*]] = fmul float %x, [[ADD]]
30+
; CHECK-NOT: fmul fast float %x, [[CB2]]
31+
; CHECK: [[ADD:%[A-z0-9]*]] = fadd fast float [[CB1]], [[CB2]]
32+
; CHECK: [[MUL:%[A-z0-9]*]] = fmul fast float %x, [[ADD]]
3333
; CHECK: ret float [[MUL]]
34-
35-
!IGCMetadata = !{!0}
36-
37-
!0 = !{!"ModuleMD", !1}
38-
!1 = !{!"compOpt", !2}
39-
!2 = !{!"FastRelaxedMath", i1 true}

IGC/Compiler/tests/CustomUnsafeOptPass/fadd.ll

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
; 0 + x = x
1313
define float @test1(float %x) #0 {
1414
entry:
15-
%0 = fadd float 0.000000e+00, %x
15+
%0 = fadd fast float 0.000000e+00, %x
1616
ret float %0
1717
}
1818

@@ -23,16 +23,10 @@ entry:
2323
; x + 0 = x
2424
define float @test2(float %x) #0 {
2525
entry:
26-
%0 = fadd float %x, 0.000000e+00
26+
%0 = fadd fast float %x, 0.000000e+00
2727
ret float %0
2828
}
2929

3030
; CHECK-LABEL: define float @test2
3131
; CHECK-NOT: fadd
3232
; CHECK: ret float %x
33-
34-
!IGCMetadata = !{!0}
35-
36-
!0 = !{!"ModuleMD", !1}
37-
!1 = !{!"compOpt", !2}
38-
!2 = !{!"FastRelaxedMath", i1 true}

IGC/Compiler/tests/CustomUnsafeOptPass/fadd_fdiv.ll

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,27 @@
1414
; (x + 2) / 2 => (x * 1/2) + 1
1515
define float @test1(float %x) #0 {
1616
entry:
17-
%0 = fadd float %x, 2.000000e+00
18-
%1 = fdiv float %0, 2.000000e+00
17+
%0 = fadd fast float %x, 2.000000e+00
18+
%1 = fdiv fast float %0, 2.000000e+00
1919
ret float %1
2020
}
2121

2222
; CHECK-LABEL: define float @test1
23-
; CHECK: %0 = fdiv float 1.000000e+00, 2.000000e+00
24-
; CHECK: %1 = fmul float %x, %0
25-
; CHECK: %2 = fadd float %1, 1.000000e+00
23+
; CHECK: %0 = fdiv fast float 1.000000e+00, 2.000000e+00
24+
; CHECK: %1 = fmul fast float %x, %0
25+
; CHECK: %2 = fadd fast float %1, 1.000000e+00
2626
; CHECK: ret float %2
2727

2828
; (x - 2) / 2 => (x * 1/2) - 1
2929
define float @test2(float %x) #0 {
3030
entry:
31-
%0 = fsub float %x, 2.000000e+00
32-
%1 = fdiv float %0, 2.000000e+00
31+
%0 = fsub fast float %x, 2.000000e+00
32+
%1 = fdiv fast float %0, 2.000000e+00
3333
ret float %1
3434
}
3535

3636
; CHECK-LABEL: define float @test2
37-
; CHECK: %0 = fdiv float 1.000000e+00, 2.000000e+00
38-
; CHECK: %1 = fmul float %x, %0
39-
; CHECK: %2 = fsub float %1, 1.000000e+00
37+
; CHECK: %0 = fdiv fast float 1.000000e+00, 2.000000e+00
38+
; CHECK: %1 = fmul fast float %x, %0
39+
; CHECK: %2 = fsub fast float %1, 1.000000e+00
4040
; CHECK: ret float %2
41-
42-
!IGCMetadata = !{!0}
43-
44-
!0 = !{!"ModuleMD", !1}
45-
!1 = !{!"compOpt", !2}
46-
!2 = !{!"FastRelaxedMath", i1 true}

IGC/Compiler/tests/CustomUnsafeOptPass/fadd_fsub.ll

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
; (1 - x) + x = 1
1515
define float @test1(float %x) #0 {
1616
entry:
17-
%0 = fsub float 1.000000e+00, %x
18-
%1 = fadd float %0, %x
17+
%0 = fsub fast float 1.000000e+00, %x
18+
%1 = fadd fast float %0, %x
1919
ret float %1
2020
}
2121

@@ -27,8 +27,8 @@ entry:
2727
; x - (x - 1) = 1
2828
define float @test2(float %x) #0 {
2929
entry:
30-
%0 = fsub float %x, 1.000000e+00
31-
%1 = fsub float %x, %0
30+
%0 = fsub fast float %x, 1.000000e+00
31+
%1 = fsub fast float %x, %0
3232
ret float %1
3333
}
3434

@@ -39,8 +39,8 @@ entry:
3939
; (1 + x) - x = 1
4040
define float @test3(float %x) #0 {
4141
entry:
42-
%0 = fadd float 1.000000e+00, %x
43-
%1 = fsub float %0, %x
42+
%0 = fadd fast float 1.000000e+00, %x
43+
%1 = fsub fast float %0, %x
4444
ret float %1
4545
}
4646

@@ -52,18 +52,12 @@ entry:
5252
; x - (1 + x) = -1
5353
define float @test4(float %x) #0 {
5454
entry:
55-
%0 = fadd float 1.000000e+00, %x
56-
%1 = fsub float %x, %0
55+
%0 = fadd fast float 1.000000e+00, %x
56+
%1 = fsub fast float %x, %0
5757
ret float %1
5858
}
5959

6060
; CHECK-LABEL: define float @test4
6161
; CHECK-NOT: fadd
62-
; CHECK: %0 = fsub float 0.000000e+00, 1.000000e+00
62+
; CHECK: %0 = fsub fast float 0.000000e+00, 1.000000e+00
6363
; CHECK: ret float %0
64-
65-
!IGCMetadata = !{!0}
66-
67-
!0 = !{!"ModuleMD", !1}
68-
!1 = !{!"compOpt", !2}
69-
!2 = !{!"FastRelaxedMath", i1 true}

IGC/Compiler/tests/CustomUnsafeOptPass/fdiv.ll

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
; 0/x => 0
1313
define float @div_to_zero(float %x) #0 {
1414
entry:
15-
%0 = fdiv float 0.000000e+00, %x
15+
%0 = fdiv fast float 0.000000e+00, %x
1616
ret float %0
1717
}
1818

@@ -23,7 +23,7 @@ entry:
2323
; x/1 => x
2424
define float @div_by_one(float %x) #0 {
2525
entry:
26-
%0 = fdiv float %x, 1.000000e+00
26+
%0 = fdiv fast float %x, 1.000000e+00
2727
ret float %0
2828
}
2929

@@ -34,18 +34,12 @@ entry:
3434
; x/3 => x * 1/3
3535
define float @div_to_mul(float %x) #0 {
3636
entry:
37-
%0 = fdiv float %x, 3.000000e+00
37+
%0 = fdiv fast float %x, 3.000000e+00
3838
ret float %0
3939
}
4040

4141
; CHECK-LABEL: define float @div_to_mul
42-
; CHECK-NOT: fdiv float %x, 3.000000e+00
43-
; CHECK: %0 = fdiv float 1.000000e+00, 3.000000e+00
44-
; CHECK: %1 = fmul float %x, %0
42+
; CHECK-NOT: fdiv{{.*}}float %x, 3.000000e+00
43+
; CHECK: %0 = fdiv fast float 1.000000e+00, 3.000000e+00
44+
; CHECK: %1 = fmul fast float %x, %0
4545
; CHECK: ret float %1
46-
47-
!IGCMetadata = !{!0}
48-
49-
!0 = !{!"ModuleMD", !1}
50-
!1 = !{!"compOpt", !2}
51-
!2 = !{!"FastRelaxedMath", i1 true}

IGC/Compiler/tests/CustomUnsafeOptPass/fdiv_fdiv.ll

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
; 1 / (1 / x) = x
1515
define float @test1(float %x) #0 {
1616
entry:
17-
%0 = fdiv float 1.000000e+00, %x
18-
%1 = fdiv float 1.000000e+00, %0
17+
%0 = fdiv fast float 1.000000e+00, %x
18+
%1 = fdiv fast float 1.000000e+00, %0
1919
ret float %1
2020
}
2121

@@ -26,18 +26,12 @@ entry:
2626
; 4 / (1 / x) = 4 * x
2727
define float @test2(float %x) #0 {
2828
entry:
29-
%0 = fdiv float 1.000000e+00, %x
30-
%1 = fdiv float 4.000000e+00, %0
29+
%0 = fdiv fast float 1.000000e+00, %x
30+
%1 = fdiv fast float 4.000000e+00, %0
3131
ret float %1
3232
}
3333

3434
; CHECK-LABEL: define float @test2
3535
; CHECK-NOT: fdiv
36-
; CHECK: %0 = fmul float 4.000000e+00, %x
36+
; CHECK: %0 = fmul fast float 4.000000e+00, %x
3737
; CHECK: ret float %0
38-
39-
!IGCMetadata = !{!0}
40-
41-
!0 = !{!"ModuleMD", !1}
42-
!1 = !{!"compOpt", !2}
43-
!2 = !{!"FastRelaxedMath", i1 true}

IGC/Compiler/tests/CustomUnsafeOptPass/fmul.ll

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
; 0 * x = 0
1313
define float @test1(float %x) #0 {
1414
entry:
15-
%0 = fmul float 0.000000e+00, %x
15+
%0 = fmul fast float 0.000000e+00, %x
1616
ret float %0
1717
}
1818

@@ -23,7 +23,7 @@ entry:
2323
; 1 * x = x
2424
define float @test2(float %x) #0 {
2525
entry:
26-
%0 = fmul float 1.000000e+00, %x
26+
%0 = fmul fast float 1.000000e+00, %x
2727
ret float %0
2828
}
2929

@@ -34,7 +34,7 @@ entry:
3434
; x * 1 = x
3535
define float @test3(float %x) #0 {
3636
entry:
37-
%0 = fmul float %x, 1.000000e+00
37+
%0 = fmul fast float %x, 1.000000e+00
3838
ret float %0
3939
}
4040

@@ -45,29 +45,23 @@ entry:
4545
; -1 * x = -x
4646
define float @test4(float %x) #0 {
4747
entry:
48-
%0 = fmul float -1.000000e+00, %x
48+
%0 = fmul fast float -1.000000e+00, %x
4949
ret float %0
5050
}
5151

5252
; CHECK-LABEL: define float @test4
5353
; CHECK-NOT: fmul
54-
; CHECK: %0 = fsub float 0.000000e+00, %x
54+
; CHECK: %0 = fsub fast float 0.000000e+00, %x
5555
; CHECK: ret float %0
5656

5757
; x * -1 = -x
5858
define float @test5(float %x) #0 {
5959
entry:
60-
%0 = fmul float %x, -1.000000e+00
60+
%0 = fmul fast float %x, -1.000000e+00
6161
ret float %0
6262
}
6363

6464
; CHECK-LABEL: define float @test5
6565
; CHECK-NOT: fmul
66-
; CHECK: %0 = fsub float 0.000000e+00, %x
66+
; CHECK: %0 = fsub fast float 0.000000e+00, %x
6767
; CHECK: ret float %0
68-
69-
!IGCMetadata = !{!0}
70-
71-
!0 = !{!"ModuleMD", !1}
72-
!1 = !{!"compOpt", !2}
73-
!2 = !{!"FastRelaxedMath", i1 true}

0 commit comments

Comments
 (0)