Skip to content

Commit

Permalink
Util function for intrinsic creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanradanov committed Jan 12, 2024
1 parent 9b0e9a9 commit b8966bc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
25 changes: 2 additions & 23 deletions enzyme/Enzyme/EnzymeLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5094,29 +5094,8 @@ class TruncateGenerator : public llvm::InstVisitor<TruncateGenerator> {
// 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<Intrinsic::IITDescriptor, 8> Table;
getIntrinsicInfoTableEntries(II.getIntrinsicID(), Table);
ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
SmallVector<Type *, 2> 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);
Expand Down
32 changes: 32 additions & 0 deletions enzyme/Enzyme/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2825,3 +2825,35 @@ 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<llvm::Value *> 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<Intrinsic::IITDescriptor> Table;
Intrinsic::getIntrinsicInfoTableEntries(ID, Table);
ArrayRef<Intrinsic::IITDescriptor> TableRef(Table);

SmallVector<Type *> ArgTys;
ArgTys.reserve(Args.size());
for (auto &I : Args)
ArgTys.push_back(I->getType());
FunctionType *FTy = FunctionType::get(RetTy, ArgTys, false);
SmallVector<Type *> OverloadTys;
Intrinsic::MatchIntrinsicTypesResult Res =
matchIntrinsicSignature(FTy, TableRef, OverloadTys);
(void)Res;
assert(Res == Intrinsic::MatchIntrinsicTypes_Match && TableRef.empty() &&
"Wrong types for intrinsic!");
Function *Fn = Intrinsic::getDeclaration(B.GetInsertPoint()->getModule(), ID,
OverloadTys);
CallInst *nres = B.CreateCall(Fn, Args, {}, Name);
if (FMFSource)
nres->copyFastMathFlags(FMFSource);
#endif
return nres;
}
6 changes: 6 additions & 0 deletions enzyme/Enzyme/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1807,4 +1807,10 @@ bool collectOffset(llvm::GEPOperator *gep, const llvm::DataLayout &DL,
unsigned BitWidth,
llvm::MapVector<llvm::Value *, llvm::APInt> &VariableOffsets,
llvm::APInt &ConstantOffset);

llvm::CallInst *createIntrinsicCall(llvm::IRBuilderBase &B,
llvm::Intrinsic::ID ID, llvm::Type *RetTy,
llvm::ArrayRef<llvm::Value *> Args,
llvm::Instruction *FMFSource = nullptr,
const llvm::Twine &Name = "");
#endif // ENZYME_UTILS_H

0 comments on commit b8966bc

Please sign in to comment.