Skip to content

Add constrained fadd to FP operations #136499

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

Open
wants to merge 1 commit into
base: users/spavloff/bundles2
Choose a base branch
from

Conversation

spavloff
Copy link
Collaborator

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.

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.
@spavloff spavloff self-assigned this Apr 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 20, 2025

@llvm/pr-subscribers-llvm-ir

Author: Serge Pavlov (spavloff)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/136499.diff

3 Files Affected:

  • (modified) llvm/include/llvm/IR/FloatingPointOps.def (+1)
  • (modified) llvm/lib/IR/Instructions.cpp (+10)
  • (modified) llvm/unittests/IR/IRBuilderTest.cpp (+65)
diff --git a/llvm/include/llvm/IR/FloatingPointOps.def b/llvm/include/llvm/IR/FloatingPointOps.def
index 8567b5dbac302..468227e648300 100644
--- a/llvm/include/llvm/IR/FloatingPointOps.def
+++ b/llvm/include/llvm/IR/FloatingPointOps.def
@@ -18,6 +18,7 @@
 // - intrinsic function name,
 // - DAG node corresponding to the intrinsic.
 
+FUNCTION(experimental_constrained_fadd,      FADD)
 FUNCTION(nearbyint,                          FNEARBYINT)
 FUNCTION(trunc,                              FTRUNC)
 
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index 0e362d79493c1..1073c2939a8a3 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -636,6 +636,11 @@ RoundingMode CallBase::getRoundingMode() const {
   if (RM)
     return *RM;
 
+  // If this is a constrained intrinsic, get rounding mode from its metadata
+  // arguments.
+  if (auto *CI = dyn_cast<ConstrainedFPIntrinsic>(this))
+    return CI->getRoundingMode().value_or(RoundingMode::Dynamic);
+
   // No FP bundle, try to guess from the current mode.
   if (getParent())
     if (auto *F = getFunction(); F)
@@ -658,6 +663,11 @@ fp::ExceptionBehavior CallBase::getExceptionBehavior() const {
   if (EB)
     return *EB;
 
+  // If this is a constrained intrinsic, get exception behavior from its
+  // metadata arguments.
+  if (auto *CI = dyn_cast<ConstrainedFPIntrinsic>(this))
+    return CI->getExceptionBehavior().value_or(fp::ebStrict);
+
   // No FP bundle, try to guess from the current mode.
   if (getParent())
     if (auto *F = getFunction(); F)
diff --git a/llvm/unittests/IR/IRBuilderTest.cpp b/llvm/unittests/IR/IRBuilderTest.cpp
index 48a76c9cd586d..660dc453a62fb 100644
--- a/llvm/unittests/IR/IRBuilderTest.cpp
+++ b/llvm/unittests/IR/IRBuilderTest.cpp
@@ -662,6 +662,71 @@ TEST_F(IRBuilderTest, FPBundlesStrict) {
   }
 }
 
+TEST_F(IRBuilderTest, FPBundlesConstrained) {
+  F->addFnAttr(Attribute::StrictFP);
+
+  IRBuilder<> Builder(BB);
+  LLVMContext &Context = Builder.getContext();
+  Builder.setDefaultConstrainedExcept(fp::ebStrict);
+  Builder.setDefaultConstrainedRounding(RoundingMode::TowardZero);
+  Builder.setIsFPConstrained(true);
+
+  GlobalVariable *GVDouble = new GlobalVariable(
+      *M, Type::getDoubleTy(Ctx), true, GlobalValue::ExternalLinkage, nullptr);
+  Value *FnArg = Builder.CreateLoad(GVDouble->getValueType(), GVDouble);
+  Function *Fn = Intrinsic::getOrInsertDeclaration(
+      M.get(), Intrinsic::experimental_constrained_fadd,
+      {Type::getDoubleTy(Ctx)});
+
+  const auto createConstrainedRounding = [&](RoundingMode RM) {
+    std::optional<StringRef> RoundingStr = convertRoundingModeToStr(RM);
+    assert(RoundingStr);
+    auto *RoundingMDS = MDString::get(Context, *RoundingStr);
+    return MetadataAsValue::get(Context, RoundingMDS);
+  };
+  const auto createConstrainedExcept = [&](fp::ExceptionBehavior EB) {
+    std::optional<StringRef> ExceptStr = convertExceptionBehaviorToStr(EB);
+    assert(ExceptStr);
+    auto *ExceptMDS = MDString::get(Context, *ExceptStr);
+    return MetadataAsValue::get(Context, ExceptMDS);
+  };
+
+  // Constrained function call without bundles.
+  {
+    Value *V = Builder.CreateFAdd(FnArg, FnArg);
+    auto *I = cast<IntrinsicInst>(V);
+    EXPECT_FALSE(I->getOperandBundle(LLVMContext::OB_fp_control).has_value());
+    EXPECT_FALSE(I->getOperandBundle(LLVMContext::OB_fp_except).has_value());
+    EXPECT_EQ(Intrinsic::experimental_constrained_fadd, I->getIntrinsicID());
+    EXPECT_EQ(RoundingMode::TowardZero, I->getRoundingMode());
+    EXPECT_EQ(fp::ebStrict, I->getExceptionBehavior());
+    MemoryEffects ME = I->getMemoryEffects();
+    EXPECT_TRUE(ME.doesAccessInaccessibleMem());
+  }
+
+  // Operand bundles have precedence over constrained intrinsic metadata
+  // arguments.
+  {
+    SmallVector<OperandBundleDef, 1> Bundles;
+    llvm::addFPRoundingBundle(Ctx, Bundles, RoundingMode::TowardNegative);
+    llvm::addFPExceptionBundle(Ctx, Bundles, fp::ebIgnore);
+    Value *V = Builder.CreateCall(
+        Fn,
+        {FnArg, FnArg, createConstrainedRounding(RoundingMode::TowardZero),
+         createConstrainedExcept(fp::ebStrict)},
+        Bundles);
+
+    auto *I = cast<IntrinsicInst>(V);
+    EXPECT_TRUE(I->getOperandBundle(LLVMContext::OB_fp_control).has_value());
+    EXPECT_TRUE(I->getOperandBundle(LLVMContext::OB_fp_except).has_value());
+    EXPECT_EQ(Intrinsic::experimental_constrained_fadd, I->getIntrinsicID());
+    EXPECT_EQ(RoundingMode::TowardNegative, I->getRoundingMode());
+    EXPECT_EQ(fp::ebIgnore, I->getExceptionBehavior());
+    MemoryEffects ME = I->getMemoryEffects();
+    EXPECT_TRUE(ME.doesAccessInaccessibleMem());
+  }
+}
+
 TEST_F(IRBuilderTest, Lifetime) {
   IRBuilder<> Builder(BB);
   AllocaInst *Var1 = Builder.CreateAlloca(Builder.getInt8Ty());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants