Skip to content

Conversation

@dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Sep 24, 2023

This patch fixes crashes in #67236 when creating llvm.powi.* via IRBuilderBase::CreateBinaryIntrinsic.

@llvmbot
Copy link
Member

llvmbot commented Sep 24, 2023

@llvm/pr-subscribers-llvm-ir

Changes

This patch fixes crashes in #67236 when creating llvm.powi.* via IRBuilderBase::CreateBinaryIntrinsic.


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

2 Files Affected:

  • (modified) llvm/lib/IR/IRBuilder.cpp (+2-1)
  • (modified) llvm/unittests/IR/IRBuilderTest.cpp (+14-1)
diff --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp
index b321d8b325fe0be..0c2016662b4d72a 100644
--- a/llvm/lib/IR/IRBuilder.cpp
+++ b/llvm/lib/IR/IRBuilder.cpp
@@ -934,7 +934,8 @@ CallInst *IRBuilderBase::CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS,
                                                Instruction *FMFSource,
                                                const Twine &Name) {
   Module *M = BB->getModule();
-  Function *Fn = Intrinsic::getDeclaration(M, ID, { LHS->getType() });
+  Function *Fn =
+      Intrinsic::getDeclaration(M, ID, {LHS->getType(), RHS->getType()});
   return createCallHelper(Fn, {LHS, RHS}, Name, FMFSource);
 }
 
diff --git a/llvm/unittests/IR/IRBuilderTest.cpp b/llvm/unittests/IR/IRBuilderTest.cpp
index ef5f974419029d4..aedc60fbe8d2380 100644
--- a/llvm/unittests/IR/IRBuilderTest.cpp
+++ b/llvm/unittests/IR/IRBuilderTest.cpp
@@ -55,12 +55,13 @@ class IRBuilderTest : public testing::Test {
 
 TEST_F(IRBuilderTest, Intrinsics) {
   IRBuilder<> Builder(BB);
-  Value *V;
+  Value *V, *IV;
   Instruction *I;
   CallInst *Call;
   IntrinsicInst *II;
 
   V = Builder.CreateLoad(GV->getValueType(), GV);
+  IV = Builder.getInt32(2);
   I = cast<Instruction>(Builder.CreateFAdd(V, V));
   I->setHasNoInfs(true);
   I->setHasNoNaNs(false);
@@ -109,6 +110,18 @@ TEST_F(IRBuilderTest, Intrinsics) {
   EXPECT_TRUE(II->hasNoInfs());
   EXPECT_FALSE(II->hasNoNaNs());
 
+  Call = Builder.CreateBinaryIntrinsic(Intrinsic::powi, V, IV);
+  II = cast<IntrinsicInst>(Call);
+  EXPECT_EQ(II->getIntrinsicID(), Intrinsic::powi);
+  EXPECT_FALSE(II->hasNoInfs());
+  EXPECT_FALSE(II->hasNoNaNs());
+
+  Call = Builder.CreateBinaryIntrinsic(Intrinsic::powi, V, IV, I);
+  II = cast<IntrinsicInst>(Call);
+  EXPECT_EQ(II->getIntrinsicID(), Intrinsic::powi);
+  EXPECT_TRUE(II->hasNoInfs());
+  EXPECT_FALSE(II->hasNoNaNs());
+
   Call = Builder.CreateIntrinsic(Intrinsic::fma, {V->getType()}, {V, V, V});
   II = cast<IntrinsicInst>(Call);
   EXPECT_EQ(II->getIntrinsicID(), Intrinsic::fma);

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is completely incorrect. The passed types here are the mangling types of the intrinsic, not the operand types. CreateBinaryIntrinsic only supports intrinsics with two operands that are mangled on the single type, as the documentation says:

Create a call to intrinsic ID with 2 operands which is mangled on the first type.

Passing an additional type will result in incorrect intrinsic mangling.

@dtcxzyw
Copy link
Member Author

dtcxzyw commented Sep 24, 2023

Thank you for clarifying this.

@dtcxzyw dtcxzyw closed this Sep 24, 2023
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.

3 participants