From b12c84c74c1749e40b27c498829195b85bffa2db Mon Sep 17 00:00:00 2001 From: Ivan Radanov Ivanov Date: Fri, 12 Jan 2024 10:19:05 +0900 Subject: [PATCH] Util function for intrinsic creation --- enzyme/Enzyme/EnzymeLogic.cpp | 25 ++----------------------- enzyme/Enzyme/Utils.cpp | 28 ++++++++++++++++++++++++++++ enzyme/Enzyme/Utils.h | 6 ++++++ 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/enzyme/Enzyme/EnzymeLogic.cpp b/enzyme/Enzyme/EnzymeLogic.cpp index 31e4b45ca7b9..5000ed34bcaa 100644 --- a/enzyme/Enzyme/EnzymeLogic.cpp +++ b/enzyme/Enzyme/EnzymeLogic.cpp @@ -5094,29 +5094,8 @@ class TruncateGenerator : public llvm::InstVisitor { // TODO check that the intrinsic is overloaded CallInst *intr; -#if LLVM_VERSION_MAJOR >= 16 - Value *nres = intr = B.CreateIntrinsic(retTy, II.getIntrinsicID(), new_ops, - &II, II.getName()); -#else - // Older version do not automatically mangle the intrinsic for us - we need - // to provide the types to mangle with - SmallVector Table; - getIntrinsicInfoTableEntries(II.getIntrinsicID(), Table); - ArrayRef TableRef = Table; - SmallVector ArgTys; - Intrinsic::MatchIntrinsicTypesResult Res = - Intrinsic::matchIntrinsicSignature(II.getFunctionType(), TableRef, - ArgTys); - assert(Res != Intrinsic::MatchIntrinsicTypes_NoMatchRet && - "Intrinsic has incorrect return type!"); - assert(Res != Intrinsic::MatchIntrinsicTypes_NoMatchArg && - "Intrinsic has incorrect argument type!"); - for (unsigned i = 0; i < ArgTys.size(); i++) - if (ArgTys[i] == getFromType()) - ArgTys[i] = getToType(); - Value *nres = intr = B.CreateIntrinsic(II.getIntrinsicID(), ArgTys, new_ops, - &II, II.getName()); -#endif + Value *nres = intr = createIntrinsicCall(B, II.getIntrinsicID(), retTy, + new_ops, &II, II.getName()); if (II.getType() == getFromType()) nres = expand(B, nres); intr->copyIRFlags(newI); diff --git a/enzyme/Enzyme/Utils.cpp b/enzyme/Enzyme/Utils.cpp index 455346addf21..1ee2e48c4033 100644 --- a/enzyme/Enzyme/Utils.cpp +++ b/enzyme/Enzyme/Utils.cpp @@ -2825,3 +2825,31 @@ bool collectOffset(GEPOperator *gep, const DataLayout &DL, unsigned BitWidth, return true; #endif } + +llvm::CallInst *createIntrinsicCall(llvm::IRBuilderBase &B, + llvm::Intrinsic::ID ID, llvm::Type *RetTy, + llvm::ArrayRef Args, + llvm::Instruction *FMFSource, + const llvm::Twine &Name) { +#if LLVM_VERSION_MAJOR >= 16 + llvm::CallInst *nres = B.CreateIntrinsic(RetTy, ID, Args, FMFSource, Name); +#else + SmallVector Table; + Intrinsic::getIntrinsicInfoTableEntries(ID, Table); + ArrayRef TableRef(Table); + + SmallVector ArgTys; + ArgTys.reserve(Args.size()); + for (auto &I : Args) + ArgTys.push_back(I->getType()); + FunctionType *FTy = FunctionType::get(RetTy, ArgTys, false); + SmallVector OverloadTys; + Intrinsic::MatchIntrinsicTypesResult Res = + matchIntrinsicSignature(FTy, TableRef, OverloadTys); + (void)Res; + assert(Res == Intrinsic::MatchIntrinsicTypes_Match && TableRef.empty() && + "Wrong types for intrinsic!"); + llvm::CallInst *nres = B.CreateIntrinsic(ID, ArgTys, Args, FMFSource, Name); +#endif + return nres; +} diff --git a/enzyme/Enzyme/Utils.h b/enzyme/Enzyme/Utils.h index b7b7b5f31f29..32401d2f17e5 100644 --- a/enzyme/Enzyme/Utils.h +++ b/enzyme/Enzyme/Utils.h @@ -1807,4 +1807,10 @@ bool collectOffset(llvm::GEPOperator *gep, const llvm::DataLayout &DL, unsigned BitWidth, llvm::MapVector &VariableOffsets, llvm::APInt &ConstantOffset); + +llvm::CallInst *createIntrinsicCall(llvm::IRBuilderBase &B, + llvm::Intrinsic::ID ID, llvm::Type *RetTy, + llvm::ArrayRef Args, + llvm::Instruction *FMFSource = nullptr, + const llvm::Twine &Name = ""); #endif // ENZYME_UTILS_H