|
19 | 19 | #include "llvm/IR/InstIterator.h"
|
20 | 20 | #include "llvm/IR/IntrinsicInst.h"
|
21 | 21 | #include "llvm/IR/IntrinsicsNVPTX.h"
|
| 22 | +#include "llvm/IR/MDBuilder.h" |
22 | 23 | #include "llvm/InitializePasses.h"
|
23 | 24 | #include "llvm/Support/FormatVariadic.h"
|
24 | 25 |
|
@@ -102,8 +103,17 @@ static bool replaceWithLLVMIR(FPBuiltinIntrinsic &BuiltinCall) {
|
102 | 103 | BuiltinCall.replaceAllUsesWith(Replacement);
|
103 | 104 | // ConstantFolder may fold original arguments to a constant, meaning we might
|
104 | 105 | // have no instruction anymore.
|
105 |
| - if (auto *ReplacementI = dyn_cast<Instruction>(Replacement)) |
| 106 | + if (auto *ReplacementI = dyn_cast<Instruction>(Replacement)) { |
106 | 107 | 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 | + } |
107 | 117 | LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Replaced call to `"
|
108 | 118 | << BuiltinCall.getCalledFunction()->getName()
|
109 | 119 | << "` with equivalent IR. \n `");
|
|
0 commit comments