Skip to content

Commit 7ad1203

Browse files
authored
[SYCL] Preserve fpbuiltin-max-error information in fpmath metadata (#17393)
FPBuiltinFnSelection pass is modified to create fpmath metadata during replacement of llvm.fpbuiltin.* intrinsics with native LLVM instructions or LLVM std intrinsics. Signed-off-by: Sidorov, Dmitry <[email protected]>
1 parent 81e9be7 commit 7ad1203

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

llvm/lib/Transforms/Scalar/FPBuiltinFnSelection.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/IR/InstIterator.h"
2020
#include "llvm/IR/IntrinsicInst.h"
2121
#include "llvm/IR/IntrinsicsNVPTX.h"
22+
#include "llvm/IR/MDBuilder.h"
2223
#include "llvm/InitializePasses.h"
2324
#include "llvm/Support/FormatVariadic.h"
2425

@@ -102,8 +103,17 @@ static bool replaceWithLLVMIR(FPBuiltinIntrinsic &BuiltinCall) {
102103
BuiltinCall.replaceAllUsesWith(Replacement);
103104
// ConstantFolder may fold original arguments to a constant, meaning we might
104105
// have no instruction anymore.
105-
if (auto *ReplacementI = dyn_cast<Instruction>(Replacement))
106+
if (auto *ReplacementI = dyn_cast<Instruction>(Replacement)) {
106107
ReplacementI->copyFastMathFlags(&BuiltinCall);
108+
// Copy accuracy from fp-max-error attribute to fpmath metadata just in case
109+
// if the backend can do something useful with it.
110+
std::optional<float> Accuracy = BuiltinCall.getRequiredAccuracy();
111+
if (Accuracy.has_value()) {
112+
llvm::MDBuilder MDHelper(BuiltinCall.getContext());
113+
llvm::MDNode *Node = MDHelper.createFPMath(Accuracy.value());
114+
ReplacementI->setMetadata(LLVMContext::MD_fpmath, Node);
115+
}
116+
}
107117
LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Replaced call to `"
108118
<< BuiltinCall.getCalledFunction()->getName()
109119
<< "` with equivalent IR. \n `");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; RUN: opt -fpbuiltin-fn-selection -S < %s 2>&1 | FileCheck %s
2+
3+
; Check that "fpbuiltin-max-error" attribute value is copied to fpmath metadata
4+
5+
; CHECK: %[[#]] = call float @llvm.sqrt.f32(float %{{.*}}), !fpmath ![[#MD:]]
6+
; CHECK: ![[#MD]] = !{float 5.000000e-01}
7+
8+
define void @test_scalar_cr(float %f) {
9+
entry:
10+
%t1 = call float @llvm.fpbuiltin.sqrt.f32(float %f) #0
11+
ret void
12+
}
13+
14+
declare float @llvm.fpbuiltin.sqrt.f32(float)
15+
16+
attributes #0 = { "fpbuiltin-max-error"="0.5" }

0 commit comments

Comments
 (0)