Skip to content

Commit e68ceb9

Browse files
[SYCL] Fix the case of folded const expr when replacing a call to the fpbuiltin intrinsic (#17244)
This PR adds a sanity check to the FPBuiltinFnSelection pass to ensure that copyFastMathFlags is not called for a folded constant. This is to fix the case when ConstantFolder folds original arguments to a constant, meaning that we have no instruction anymore. From the LLVM IR perspective the reproducer is as simple as ``` define spir_kernel void @foo() { entry: %r = tail call float @llvm.fpbuiltin.fdiv.f32(float 1.000000e+00, float 2.000000e+00) ret void } ``` The corresponding SYCL code may look like, for instance: ``` #include <sycl/detail/core.hpp> using namespace sycl; int main() { const unsigned array_size = 4; range<1> numOfItems{array_size}; queue deviceQueue; float *a; deviceQueue.submit([&](handler& cgh) { cgh.parallel_for<class KernelFdiv>(numOfItems, [=](id<1> wiID) { a[0] = .5f / .9f; }); }); return 0; } ``` Having SYCL code, the crash may be reproduced either via LLVM IR ``` ./bin/clang++ -S -fsycl -emit-llvm -fno-offload-fp32-prec-div fpbuiltin-issue.cpp -o fpbuiltin-issue.ll ``` or directly using the LLVM SPIR-V backend ``` ./bin/clang++ -fsycl -fno-offload-fp32-prec-div fpbuiltin-issue.cpp -o fpbuiltin-issue-exe -fsycl-use-spirv-backend-for-spirv-gen ```
1 parent 710fe46 commit e68ceb9

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

llvm/lib/Transforms/Scalar/FPBuiltinFnSelection.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ static bool replaceWithLLVMIR(FPBuiltinIntrinsic &BuiltinCall) {
100100
break;
101101
}
102102
BuiltinCall.replaceAllUsesWith(Replacement);
103-
cast<Instruction>(Replacement)->copyFastMathFlags(&BuiltinCall);
103+
// ConstantFolder may fold original arguments to a constant, meaning we might
104+
// have no instruction anymore.
105+
if (auto *ReplacementI = dyn_cast<Instruction>(Replacement))
106+
ReplacementI->copyFastMathFlags(&BuiltinCall);
104107
LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Replaced call to `"
105108
<< BuiltinCall.getCalledFunction()->getName()
106109
<< "` with equivalent IR. \n `");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; RUN: llc -mtriple=spirv64-unknown-unknown -o - %s
2+
; Test that codegen doesn't crash for fpbuiltin intrinsic lowering.
3+
4+
define float @test() {
5+
entry:
6+
%r = tail call float @llvm.fpbuiltin.fdiv.f32(float 1.000000e+00, float 2.000000e+00)
7+
ret float %r
8+
}
9+
10+
define spir_kernel void @foo() {
11+
entry:
12+
%r = tail call float @test()
13+
ret void
14+
}

0 commit comments

Comments
 (0)