[llvm] Add IRBuilder::CreateFMA (PR #131112)

Frederik Harwath via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 13 03:38:27 PDT 2025


https://github.com/frederik-h updated https://github.com/llvm/llvm-project/pull/131112

>From f1ce369163e842e4f44172bfdc9ad53d48d1f0b4 Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Thu, 13 Mar 2025 05:11:54 -0400
Subject: [PATCH 1/2] Add IRBuilder::CreateFMA

- Adjust some existing CreateIntrinsic uses appropriately
- Adjust IRBuilderTest.cpp and remove duplicate test case
---
 llvm/include/llvm/IR/IRBuilder.h    |  7 +++++++
 llvm/lib/IR/AutoUpgrade.cpp         |  4 ++--
 llvm/unittests/IR/IRBuilderTest.cpp | 12 ++----------
 3 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index 67e357c600d3b..13665dcd992a8 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -1065,6 +1065,13 @@ class IRBuilderBase {
                            {Src, Exp}, FMFSource, Name);
   }
 
+  /// Create call to the fma intrinsic.
+  Value *CreateFMA(Value *Factor1, Value *Factor2, Value *Summand,
+                   FMFSource FMFSource = {}, const Twine &Name = "") {
+    return CreateIntrinsic(Intrinsic::fma, {Factor1->getType()},
+                           {Factor1, Factor2, Summand}, FMFSource, Name);
+  }
+
   /// Create a call to the arithmetic_fence intrinsic.
   CallInst *CreateArithmeticFence(Value *Val, Type *DstType,
                                   const Twine &Name = "") {
diff --git a/llvm/lib/IR/AutoUpgrade.cpp b/llvm/lib/IR/AutoUpgrade.cpp
index cb4ecc60aa473..ce3b2c90a41a1 100644
--- a/llvm/lib/IR/AutoUpgrade.cpp
+++ b/llvm/lib/IR/AutoUpgrade.cpp
@@ -3755,7 +3755,7 @@ static Value *upgradeX86IntrinsicCall(StringRef Name, CallBase *CI, Function *F,
         IID = Intrinsic::x86_avx512_vfmadd_f32;
       Rep = Builder.CreateIntrinsic(IID, {}, Ops);
     } else {
-      Rep = Builder.CreateIntrinsic(Intrinsic::fma, A->getType(), {A, B, C});
+      Rep = Builder.CreateFMA(A, B, C);
     }
 
     Value *PassThru = IsMaskZ   ? Constant::getNullValue(Rep->getType())
@@ -3808,7 +3808,7 @@ static Value *upgradeX86IntrinsicCall(StringRef Name, CallBase *CI, Function *F,
 
       Rep = Builder.CreateIntrinsic(IID, {}, {A, B, C, CI->getArgOperand(4)});
     } else {
-      Rep = Builder.CreateIntrinsic(Intrinsic::fma, A->getType(), {A, B, C});
+      Rep = Builder.CreateFMA(A, B, C);
     }
 
     Value *PassThru = IsMaskZ   ? llvm::Constant::getNullValue(CI->getType())
diff --git a/llvm/unittests/IR/IRBuilderTest.cpp b/llvm/unittests/IR/IRBuilderTest.cpp
index 3a55d88f03d49..2170524d6eb3c 100644
--- a/llvm/unittests/IR/IRBuilderTest.cpp
+++ b/llvm/unittests/IR/IRBuilderTest.cpp
@@ -109,21 +109,13 @@ TEST_F(IRBuilderTest, Intrinsics) {
   EXPECT_TRUE(II->hasNoInfs());
   EXPECT_FALSE(II->hasNoNaNs());
 
-  Result = Builder.CreateIntrinsic(Intrinsic::fma, {V->getType()}, {V, V, V});
+  Result = Builder.CreateFMA(V, V, V);
   II = cast<IntrinsicInst>(Result);
   EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fma);
   EXPECT_FALSE(II->hasNoInfs());
   EXPECT_FALSE(II->hasNoNaNs());
 
-  Result =
-      Builder.CreateIntrinsic(Intrinsic::fma, {V->getType()}, {V, V, V}, I);
-  II = cast<IntrinsicInst>(Result);
-  EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fma);
-  EXPECT_TRUE(II->hasNoInfs());
-  EXPECT_FALSE(II->hasNoNaNs());
-
-  Result =
-      Builder.CreateIntrinsic(Intrinsic::fma, {V->getType()}, {V, V, V}, I);
+  Result = Builder.CreateFMA(V, V, V, I);
   II = cast<IntrinsicInst>(Result);
   EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fma);
   EXPECT_TRUE(II->hasNoInfs());

>From 1191eea7e92e4549c80385876c116de6a2b7975d Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Thu, 13 Mar 2025 06:34:59 -0400
Subject: [PATCH 2/2] CreateFMA: Add assert to document that
 experimental.constrained.fma is not supported

---
 llvm/include/llvm/IR/IRBuilder.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index 13665dcd992a8..389c7882502af 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -1068,6 +1068,7 @@ class IRBuilderBase {
   /// Create call to the fma intrinsic.
   Value *CreateFMA(Value *Factor1, Value *Factor2, Value *Summand,
                    FMFSource FMFSource = {}, const Twine &Name = "") {
+    assert(!IsFPConstrained && "TODO: Support experimental_constrained_fma");
     return CreateIntrinsic(Intrinsic::fma, {Factor1->getType()},
                            {Factor1, Factor2, Summand}, FMFSource, Name);
   }



More information about the llvm-commits mailing list