Skip to content

Add IRBuilder::CreateFMA #131112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions llvm/include/llvm/IR/IRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,19 @@ 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 = "") {
if (IsFPConstrained) {
return CreateConstrainedFPIntrinsic(
Intrinsic::experimental_constrained_fma, {Factor1->getType()},
{Factor1, Factor2, Summand}, FMFSource, 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 = "") {
Expand Down Expand Up @@ -1723,6 +1736,17 @@ class IRBuilderBase {
return Accum;
}

/// This function is like @ref CreateIntrinsic for constrained fp
/// intrinsics. It sets the rounding mode and exception behavior of
/// the created intrinsic call according to \p Rounding and \p
/// Except and it sets \p FPMathTag as the 'fpmath' metadata, using
/// defaults if a value equals nullopt/null.
CallInst *CreateConstrainedFPIntrinsic(
Intrinsic::ID ID, ArrayRef<Type *> Types, ArrayRef<Value *> Args,
FMFSource FMFSource, const Twine &Name, MDNode *FPMathTag = nullptr,
std::optional<RoundingMode> Rounding = std::nullopt,
std::optional<fp::ExceptionBehavior> Except = std::nullopt);

CallInst *CreateConstrainedFPBinOp(
Intrinsic::ID ID, Value *L, Value *R, FMFSource FMFSource = {},
const Twine &Name = "", MDNode *FPMathTag = nullptr,
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/IR/AutoUpgrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
20 changes: 20 additions & 0 deletions llvm/lib/IR/IRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,26 @@ CallInst *IRBuilderBase::CreateConstrainedFPBinOp(
return C;
}

CallInst *IRBuilderBase::CreateConstrainedFPIntrinsic(
Intrinsic::ID ID, ArrayRef<Type *> Types, ArrayRef<Value *> Args,
FMFSource FMFSource, const Twine &Name, MDNode *FPMathTag,
std::optional<RoundingMode> Rounding,
std::optional<fp::ExceptionBehavior> Except) {
Value *RoundingV = getConstrainedFPRounding(Rounding);
Value *ExceptV = getConstrainedFPExcept(Except);

FastMathFlags UseFMF = FMFSource.get(FMF);

llvm::SmallVector<Value *, 5> ExtArgs(Args);
ExtArgs.push_back(RoundingV);
ExtArgs.push_back(ExceptV);

CallInst *C = CreateIntrinsic(ID, Types, ExtArgs, nullptr, Name);
setConstrainedFPCallAttr(C);
setFPAttrs(C, FPMathTag, UseFMF);
return C;
}

CallInst *IRBuilderBase::CreateConstrainedFPUnroundedBinOp(
Intrinsic::ID ID, Value *L, Value *R, FMFSource FMFSource,
const Twine &Name, MDNode *FPMathTag,
Expand Down
24 changes: 18 additions & 6 deletions llvm/unittests/IR/IRBuilderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,23 @@ 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);
Result = Builder.CreateFMA(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, FastMathFlags::getFast());
II = cast<IntrinsicInst>(Result);
EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fma);
EXPECT_TRUE(II->hasNoInfs());
EXPECT_FALSE(II->hasNoNaNs());
EXPECT_TRUE(II->hasNoNaNs());

Result = Builder.CreateUnaryIntrinsic(Intrinsic::roundeven, V);
II = cast<IntrinsicInst>(Result);
Expand Down Expand Up @@ -307,6 +305,11 @@ TEST_F(IRBuilderTest, ConstrainedFP) {
II = cast<IntrinsicInst>(V);
EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_frem);

V = Builder.CreateFMA(V, V, V);
ASSERT_TRUE(isa<IntrinsicInst>(V));
II = cast<IntrinsicInst>(V);
EXPECT_EQ(II->getIntrinsicID(), Intrinsic::experimental_constrained_fma);

VInt = Builder.CreateFPToUI(VDouble, Builder.getInt32Ty());
ASSERT_TRUE(isa<IntrinsicInst>(VInt));
II = cast<IntrinsicInst>(VInt);
Expand Down Expand Up @@ -398,6 +401,15 @@ TEST_F(IRBuilderTest, ConstrainedFP) {
EXPECT_EQ(fp::ebMayTrap, CII->getExceptionBehavior());
EXPECT_EQ(RoundingMode::TowardNegative, CII->getRoundingMode());

// Same as previous test for CreateConstrainedFPIntrinsic
Call = Builder.CreateConstrainedFPIntrinsic(
Intrinsic::experimental_constrained_fadd, {V->getType()}, {V, V}, nullptr,
"", nullptr, RoundingMode::TowardNegative, fp::ebMayTrap);
CII = cast<ConstrainedFPIntrinsic>(Call);
EXPECT_EQ(CII->getIntrinsicID(), Intrinsic::experimental_constrained_fadd);
EXPECT_EQ(fp::ebMayTrap, CII->getExceptionBehavior());
EXPECT_EQ(RoundingMode::TowardNegative, CII->getRoundingMode());

Builder.CreateRetVoid();
EXPECT_FALSE(verifyModule(*M));
}
Expand Down
Loading