Skip to content

Commit de79836

Browse files
committed
[FPEnv] Teach the IRBuilder about correct use of the strictfp attribute.
The IRBuilder needs to add the strictfp attribute to function definitions and calls when constrained floating point is enabled. Since so far all front ends have had to do is flip the constrained switch, I've made this patch always add the required attributes when said constrained switch is enabled. This continues to keep changes to front ends minimal. Differential Revision: D69312
1 parent a153233 commit de79836

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,21 @@ class IRBuilderBase {
255255
return DefaultConstrainedRounding;
256256
}
257257

258+
void setConstrainedFPFunctionAttr() {
259+
assert(BB && "Must have a basic block to set any function attributes!");
260+
261+
Function *F = BB->getParent();
262+
if (!F->hasFnAttribute(Attribute::StrictFP)) {
263+
F->addFnAttr(Attribute::StrictFP);
264+
}
265+
}
266+
267+
void setConstrainedFPCallAttr(CallInst *I) {
268+
if (!I->hasFnAttr(Attribute::StrictFP))
269+
I->addAttribute(AttributeList::FunctionIndex, Attribute::StrictFP);
270+
setConstrainedFPFunctionAttr();
271+
}
272+
258273
//===--------------------------------------------------------------------===//
259274
// RAII helpers.
260275
//===--------------------------------------------------------------------===//
@@ -1479,6 +1494,7 @@ class IRBuilder : public IRBuilderBase, public Inserter {
14791494

14801495
CallInst *C = CreateIntrinsic(ID, {L->getType()},
14811496
{L, R, RoundingV, ExceptV}, nullptr, Name);
1497+
setConstrainedFPCallAttr(C);
14821498
setFPAttrs(C, FPMathTag, UseFMF);
14831499
return C;
14841500
}
@@ -2084,6 +2100,8 @@ class IRBuilder : public IRBuilderBase, public Inserter {
20842100
Name);
20852101
break;
20862102
}
2103+
setConstrainedFPCallAttr(C);
2104+
20872105
if (isa<FPMathOperator>(C))
20882106
setFPAttrs(C, FPMathTag, UseFMF);
20892107
return C;
@@ -2240,6 +2258,8 @@ class IRBuilder : public IRBuilderBase, public Inserter {
22402258
ArrayRef<Value *> Args = None, const Twine &Name = "",
22412259
MDNode *FPMathTag = nullptr) {
22422260
CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
2261+
if (IsFPConstrained)
2262+
setConstrainedFPCallAttr(CI);
22432263
if (isa<FPMathOperator>(CI))
22442264
setFPAttrs(CI, FPMathTag, FMF);
22452265
return Insert(CI, Name);
@@ -2249,6 +2269,8 @@ class IRBuilder : public IRBuilderBase, public Inserter {
22492269
ArrayRef<OperandBundleDef> OpBundles,
22502270
const Twine &Name = "", MDNode *FPMathTag = nullptr) {
22512271
CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles);
2272+
if (IsFPConstrained)
2273+
setConstrainedFPCallAttr(CI);
22522274
if (isa<FPMathOperator>(CI))
22532275
setFPAttrs(CI, FPMathTag, FMF);
22542276
return Insert(CI, Name);

llvm/unittests/IR/IRBuilderTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ TEST_F(IRBuilderTest, ConstrainedFP) {
229229
II = cast<IntrinsicInst>(VDouble);
230230
EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fpext);
231231

232+
// Verify attributes on the call are created automatically.
233+
AttributeSet CallAttrs = II->getAttributes().getFnAttributes();
234+
EXPECT_EQ(CallAttrs.hasAttribute(Attribute::StrictFP), true);
235+
236+
// Verify attributes on the containing function are created automatically.
237+
AttributeList Attrs = BB->getParent()->getAttributes();
238+
AttributeSet FnAttrs = Attrs.getFnAttributes();
239+
EXPECT_EQ(FnAttrs.hasAttribute(Attribute::StrictFP), true);
240+
232241
// Verify the codepaths for setting and overriding the default metadata.
233242
V = Builder.CreateFAdd(V, V);
234243
ASSERT_TRUE(isa<ConstrainedFPIntrinsic>(V));

0 commit comments

Comments
 (0)