Skip to content

Commit 1e6b6bf

Browse files
committed
Add constrained fadd to FP operations
Constrained intrinsics are treated as regular intrinsicss from the perspective of FP operand bundle mechanism, meaning they can have FP operand bundles. The FP bundle API functions will report properties based on the operand bundles rather than metadata arguments. These hybrid entities are temporary objects that exist while both alternative mechanisms remain available. Nevertheless, they can be useful for development: * They can represent FP instructions with bundles. For example, while there is currently no intrinsic that directly represents 'fadd' instruction (and bundles cannot be attached to a plain 'fadd' since it is not a call), a corresponding constrained intrinsic call can represent the operation while still carrying operand bundles. * Constrained intrinsic with bundles can be assigned properties that otherwise cannot be expressed, such as denormal behavior. This change adds 'experimental.constrained.fadd' to the list of FP operations, enabling creation of tests for denormal behavior. It also modifies methods `getRoundingMode` and `getExceptionBehavior` so that they report information from metadata arguments if bundles are absent. This adjustment should help incorporating constrained intrinsics into algorithms based on operand bundles.
1 parent ad480e9 commit 1e6b6bf

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

llvm/include/llvm/IR/FloatingPointOps.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// - intrinsic function name,
1919
// - DAG node corresponding to the intrinsic.
2020

21+
FUNCTION(experimental_constrained_fadd, FADD)
2122
FUNCTION(nearbyint, FNEARBYINT)
2223
FUNCTION(trunc, FTRUNC)
2324

llvm/lib/IR/Instructions.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,11 @@ RoundingMode CallBase::getRoundingMode() const {
636636
if (RM)
637637
return *RM;
638638

639+
// If this is a constrained intrinsic, get rounding mode from its metadata
640+
// arguments.
641+
if (auto *CI = dyn_cast<ConstrainedFPIntrinsic>(this))
642+
return CI->getRoundingMode().value_or(RoundingMode::Dynamic);
643+
639644
// No FP bundle, try to guess from the current mode.
640645
if (getParent())
641646
if (auto *F = getFunction(); F)
@@ -658,6 +663,11 @@ fp::ExceptionBehavior CallBase::getExceptionBehavior() const {
658663
if (EB)
659664
return *EB;
660665

666+
// If this is a constrained intrinsic, get exception behavior from its
667+
// metadata arguments.
668+
if (auto *CI = dyn_cast<ConstrainedFPIntrinsic>(this))
669+
return CI->getExceptionBehavior().value_or(fp::ebStrict);
670+
661671
// No FP bundle, try to guess from the current mode.
662672
if (getParent())
663673
if (auto *F = getFunction(); F)

llvm/unittests/IR/IRBuilderTest.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,71 @@ TEST_F(IRBuilderTest, FPBundlesStrict) {
662662
}
663663
}
664664

665+
TEST_F(IRBuilderTest, FPBundlesConstrained) {
666+
F->addFnAttr(Attribute::StrictFP);
667+
668+
IRBuilder<> Builder(BB);
669+
LLVMContext &Context = Builder.getContext();
670+
Builder.setDefaultConstrainedExcept(fp::ebStrict);
671+
Builder.setDefaultConstrainedRounding(RoundingMode::TowardZero);
672+
Builder.setIsFPConstrained(true);
673+
674+
GlobalVariable *GVDouble = new GlobalVariable(
675+
*M, Type::getDoubleTy(Ctx), true, GlobalValue::ExternalLinkage, nullptr);
676+
Value *FnArg = Builder.CreateLoad(GVDouble->getValueType(), GVDouble);
677+
Function *Fn = Intrinsic::getOrInsertDeclaration(
678+
M.get(), Intrinsic::experimental_constrained_fadd,
679+
{Type::getDoubleTy(Ctx)});
680+
681+
const auto createConstrainedRounding = [&](RoundingMode RM) {
682+
std::optional<StringRef> RoundingStr = convertRoundingModeToStr(RM);
683+
assert(RoundingStr);
684+
auto *RoundingMDS = MDString::get(Context, *RoundingStr);
685+
return MetadataAsValue::get(Context, RoundingMDS);
686+
};
687+
const auto createConstrainedExcept = [&](fp::ExceptionBehavior EB) {
688+
std::optional<StringRef> ExceptStr = convertExceptionBehaviorToStr(EB);
689+
assert(ExceptStr);
690+
auto *ExceptMDS = MDString::get(Context, *ExceptStr);
691+
return MetadataAsValue::get(Context, ExceptMDS);
692+
};
693+
694+
// Constrained function call without bundles.
695+
{
696+
Value *V = Builder.CreateFAdd(FnArg, FnArg);
697+
auto *I = cast<IntrinsicInst>(V);
698+
EXPECT_FALSE(I->getOperandBundle(LLVMContext::OB_fp_control).has_value());
699+
EXPECT_FALSE(I->getOperandBundle(LLVMContext::OB_fp_except).has_value());
700+
EXPECT_EQ(Intrinsic::experimental_constrained_fadd, I->getIntrinsicID());
701+
EXPECT_EQ(RoundingMode::TowardZero, I->getRoundingMode());
702+
EXPECT_EQ(fp::ebStrict, I->getExceptionBehavior());
703+
MemoryEffects ME = I->getMemoryEffects();
704+
EXPECT_TRUE(ME.doesAccessInaccessibleMem());
705+
}
706+
707+
// Operand bundles have precedence over constrained intrinsic metadata
708+
// arguments.
709+
{
710+
SmallVector<OperandBundleDef, 1> Bundles;
711+
llvm::addFPRoundingBundle(Ctx, Bundles, RoundingMode::TowardNegative);
712+
llvm::addFPExceptionBundle(Ctx, Bundles, fp::ebIgnore);
713+
Value *V = Builder.CreateCall(
714+
Fn,
715+
{FnArg, FnArg, createConstrainedRounding(RoundingMode::TowardZero),
716+
createConstrainedExcept(fp::ebStrict)},
717+
Bundles);
718+
719+
auto *I = cast<IntrinsicInst>(V);
720+
EXPECT_TRUE(I->getOperandBundle(LLVMContext::OB_fp_control).has_value());
721+
EXPECT_TRUE(I->getOperandBundle(LLVMContext::OB_fp_except).has_value());
722+
EXPECT_EQ(Intrinsic::experimental_constrained_fadd, I->getIntrinsicID());
723+
EXPECT_EQ(RoundingMode::TowardNegative, I->getRoundingMode());
724+
EXPECT_EQ(fp::ebIgnore, I->getExceptionBehavior());
725+
MemoryEffects ME = I->getMemoryEffects();
726+
EXPECT_TRUE(ME.doesAccessInaccessibleMem());
727+
}
728+
}
729+
665730
TEST_F(IRBuilderTest, Lifetime) {
666731
IRBuilder<> Builder(BB);
667732
AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());

0 commit comments

Comments
 (0)