[llvm] de79836 - [FPEnv] Teach the IRBuilder about correct use of the strictfp attribute.

Kevin P. Neal via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 25 09:58:03 PDT 2019


Author: Kevin P. Neal
Date: 2019-10-25T12:57:52-04:00
New Revision: de79836312e02b3b4a6d322ced971afee6d4ba4d

URL: https://github.com/llvm/llvm-project/commit/de79836312e02b3b4a6d322ced971afee6d4ba4d
DIFF: https://github.com/llvm/llvm-project/commit/de79836312e02b3b4a6d322ced971afee6d4ba4d.diff

LOG: [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

Added: 
    

Modified: 
    llvm/include/llvm/IR/IRBuilder.h
    llvm/unittests/IR/IRBuilderTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index d1ddb75cde9b..05f311d8b72d 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -255,6 +255,21 @@ class IRBuilderBase {
     return DefaultConstrainedRounding;
   }
 
+  void setConstrainedFPFunctionAttr() {
+    assert(BB && "Must have a basic block to set any function attributes!");
+
+    Function *F = BB->getParent();
+    if (!F->hasFnAttribute(Attribute::StrictFP)) {
+      F->addFnAttr(Attribute::StrictFP);
+    }
+  }
+
+  void setConstrainedFPCallAttr(CallInst *I) {
+    if (!I->hasFnAttr(Attribute::StrictFP))
+      I->addAttribute(AttributeList::FunctionIndex, Attribute::StrictFP);
+    setConstrainedFPFunctionAttr();
+  }
+
   //===--------------------------------------------------------------------===//
   // RAII helpers.
   //===--------------------------------------------------------------------===//
@@ -1479,6 +1494,7 @@ class IRBuilder : public IRBuilderBase, public Inserter {
 
     CallInst *C = CreateIntrinsic(ID, {L->getType()},
                                   {L, R, RoundingV, ExceptV}, nullptr, Name);
+    setConstrainedFPCallAttr(C);
     setFPAttrs(C, FPMathTag, UseFMF);
     return C;
   }
@@ -2084,6 +2100,8 @@ class IRBuilder : public IRBuilderBase, public Inserter {
                           Name);
       break;
     }
+    setConstrainedFPCallAttr(C);
+
     if (isa<FPMathOperator>(C))
       setFPAttrs(C, FPMathTag, UseFMF);
     return C;
@@ -2240,6 +2258,8 @@ class IRBuilder : public IRBuilderBase, public Inserter {
                        ArrayRef<Value *> Args = None, const Twine &Name = "",
                        MDNode *FPMathTag = nullptr) {
     CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
+    if (IsFPConstrained)
+      setConstrainedFPCallAttr(CI);
     if (isa<FPMathOperator>(CI))
       setFPAttrs(CI, FPMathTag, FMF);
     return Insert(CI, Name);
@@ -2249,6 +2269,8 @@ class IRBuilder : public IRBuilderBase, public Inserter {
                        ArrayRef<OperandBundleDef> OpBundles,
                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
     CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles);
+    if (IsFPConstrained)
+      setConstrainedFPCallAttr(CI);
     if (isa<FPMathOperator>(CI))
       setFPAttrs(CI, FPMathTag, FMF);
     return Insert(CI, Name);

diff  --git a/llvm/unittests/IR/IRBuilderTest.cpp b/llvm/unittests/IR/IRBuilderTest.cpp
index 3c9dbc7f19d9..c74f63014d29 100644
--- a/llvm/unittests/IR/IRBuilderTest.cpp
+++ b/llvm/unittests/IR/IRBuilderTest.cpp
@@ -229,6 +229,15 @@ TEST_F(IRBuilderTest, ConstrainedFP) {
   II = cast<IntrinsicInst>(VDouble);
   EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fpext);
 
+  // Verify attributes on the call are created automatically.
+  AttributeSet CallAttrs = II->getAttributes().getFnAttributes();
+  EXPECT_EQ(CallAttrs.hasAttribute(Attribute::StrictFP), true);
+
+  // Verify attributes on the containing function are created automatically.
+  AttributeList Attrs = BB->getParent()->getAttributes();
+  AttributeSet FnAttrs = Attrs.getFnAttributes();
+  EXPECT_EQ(FnAttrs.hasAttribute(Attribute::StrictFP), true);
+
   // Verify the codepaths for setting and overriding the default metadata.
   V = Builder.CreateFAdd(V, V);
   ASSERT_TRUE(isa<ConstrainedFPIntrinsic>(V));


        


More information about the llvm-commits mailing list