[llvm] [mlir] [MLIR][LLVM][Intrinsics] Add new MLIR and LLVM APIs to automatically resolve overload types (PR #168188)
Rajat Bajpai via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 19 22:13:59 PST 2025
================
@@ -898,6 +898,21 @@ llvm::CallInst *mlir::LLVM::detail::createIntrinsicCall(
return builder.CreateCall(fn, args);
}
+llvm::CallInst *mlir::LLVM::detail::createIntrinsicCall(
+ llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic,
+ llvm::Type *retTy, ArrayRef<llvm::Value *> args) {
+ llvm::Module *module = builder.GetInsertBlock()->getModule();
+
+ SmallVector<llvm::Type *> argTys;
----------------
rajatbajpai wrote:
Right, it is more of a use case than the motivation on the LLVM side. I did find one example in VPlanRecipes.cpp (shown below), but I don’t have enough familiarity with that part of the codebase to confidently refactor it. The modification worked correctly in local testing, but I’m not sure it’s a good idea to merge it alongside this change.
Additionally, the earlier CreateIntrinsic API was suboptimal, it computed the overloaded type, which was then decoded again inside getOrInsertDeclaration. The new API avoids this redundant computation.
> nit: why not just call builder.CreateIntrinsic() here?
That’s a good point. Technically, yes, we could call builder.CreateIntrinsic() here. However, I'm not sure if there were any other reasons of not using LLVM api here. Perhaps, @ftynse might have more insight as the original author of this code.
-----
```diff
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -30,6 +30,7 @@
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
+#include "llvm/IR/VectorTypeUtils.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
@@ -1763,11 +1764,8 @@ void VPWidenCallRecipe::print(raw_ostream &O, const Twine &Indent,
void VPWidenIntrinsicRecipe::execute(VPTransformState &State) {
assert(State.VF.isVector() && "not widening");
- SmallVector<Type *, 2> TysForDecl;
- // Add return type if intrinsic is overloaded on it.
- if (isVectorIntrinsicWithOverloadTypeAtArg(VectorIntrinsicID, -1, State.TTI))
- TysForDecl.push_back(VectorType::get(getResultType(), State.VF));
SmallVector<Value *, 4> Args;
+ SmallVector<Type *, 4> ArgTys;
for (const auto &I : enumerate(operands())) {
// Some intrinsics have a scalar argument - don't replace it with a
// vector.
@@ -1777,16 +1775,17 @@ void VPWidenIntrinsicRecipe::execute(VPTransformState &State) {
Arg = State.get(I.value(), VPLane(0));
else
Arg = State.get(I.value(), usesFirstLaneOnly(I.value()));
- if (isVectorIntrinsicWithOverloadTypeAtArg(VectorIntrinsicID, I.index(),
- State.TTI))
- TysForDecl.push_back(Arg->getType());
Args.push_back(Arg);
+ ArgTys.push_back(Arg->getType());
}
// Use vector version of the intrinsic.
Module *M = State.Builder.GetInsertBlock()->getModule();
+
+ Type *RetTy = toVectorizedTy(getResultType(), State.VF);
+
Function *VectorF =
- Intrinsic::getOrInsertDeclaration(M, VectorIntrinsicID, TysForDecl);
+ Intrinsic::getOrInsertDeclaration(M, VectorIntrinsicID, RetTy, ArgTys);
assert(VectorF &&
"Can't retrieve vector intrinsic or vector-predication intrinsics.");
```
https://github.com/llvm/llvm-project/pull/168188
More information about the llvm-commits
mailing list