Skip to content

Commit 6924fc0

Browse files
authored
[LLVM] Add Intrinsic::getDeclarationIfExists (#112428)
Add `Intrinsic::getDeclarationIfExists` to lookup an existing declaration of an intrinsic in a `Module`.
1 parent cba7b36 commit 6924fc0

24 files changed

+89
-68
lines changed

llvm/include/llvm/IR/Intrinsics.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ namespace Intrinsic {
102102
inline Function *getDeclaration(Module *M, ID id, ArrayRef<Type *> Tys = {}) {
103103
return getOrInsertDeclaration(M, id, Tys);
104104
}
105+
106+
/// Look up the Function declaration of the intrinsic \p id in the Module
107+
/// \p M and return it if it exists. Otherwise, return nullptr. This version
108+
/// supports non-overloaded intrinsics.
109+
Function *getDeclarationIfExists(const Module *M, ID id);
110+
111+
/// This version supports overloaded intrinsics.
112+
Function *getDeclarationIfExists(Module *M, ID id, ArrayRef<Type *> Tys,
113+
FunctionType *FT = nullptr);
114+
105115
/// Looks up Name in NameTable via binary search. NameTable must be sorted
106116
/// and all entries must start with "llvm.". If NameTable contains an exact
107117
/// match for Name or a prefix of Name followed by a dot, its index in

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1613,7 +1613,7 @@ LazyValueInfoImpl &LazyValueInfo::getOrCreateImpl(const Module *M) {
16131613
assert(M && "getCache() called with a null Module");
16141614
const DataLayout &DL = M->getDataLayout();
16151615
Function *GuardDecl =
1616-
M->getFunction(Intrinsic::getName(Intrinsic::experimental_guard));
1616+
Intrinsic::getDeclarationIfExists(M, Intrinsic::experimental_guard);
16171617
PImpl = new LazyValueInfoImpl(AC, DL, GuardDecl);
16181618
}
16191619
return *static_cast<LazyValueInfoImpl *>(PImpl);

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11665,8 +11665,8 @@ bool ScalarEvolution::isBasicBlockEntryGuardedByCond(const BasicBlock *BB,
1166511665
}
1166611666

1166711667
// Check conditions due to any @llvm.experimental.guard intrinsics.
11668-
auto *GuardDecl = F.getParent()->getFunction(
11669-
Intrinsic::getName(Intrinsic::experimental_guard));
11668+
auto *GuardDecl = Intrinsic::getDeclarationIfExists(
11669+
F.getParent(), Intrinsic::experimental_guard);
1167011670
if (GuardDecl)
1167111671
for (const auto *GU : GuardDecl->users())
1167211672
if (const auto *Guard = dyn_cast<IntrinsicInst>(GU))
@@ -13615,8 +13615,8 @@ ScalarEvolution::ScalarEvolution(Function &F, TargetLibraryInfo &TLI,
1361513615
// ScalarEvolution to optimize based on those guards. For now we prefer to be
1361613616
// efficient in lieu of being smart in that rather obscure case.
1361713617

13618-
auto *GuardDecl = F.getParent()->getFunction(
13619-
Intrinsic::getName(Intrinsic::experimental_guard));
13618+
auto *GuardDecl = Intrinsic::getDeclarationIfExists(
13619+
F.getParent(), Intrinsic::experimental_guard);
1362013620
HasGuards = GuardDecl && !GuardDecl->use_empty();
1362113621
}
1362213622

@@ -15593,8 +15593,8 @@ ScalarEvolution::LoopGuards::collect(const Loop *L, ScalarEvolution &SE) {
1559315593
}
1559415594

1559515595
// Second, collect information from llvm.experimental.guards dominating the loop.
15596-
auto *GuardDecl = SE.F.getParent()->getFunction(
15597-
Intrinsic::getName(Intrinsic::experimental_guard));
15596+
auto *GuardDecl = Intrinsic::getDeclarationIfExists(
15597+
SE.F.getParent(), Intrinsic::experimental_guard);
1559815598
if (GuardDecl)
1559915599
for (const auto *GU : GuardDecl->users())
1560015600
if (const auto *Guard = dyn_cast<IntrinsicInst>(GU))

llvm/lib/IR/Intrinsics.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,16 @@ Function *Intrinsic::getOrInsertDeclaration(Module *M, ID id,
724724
.getCallee());
725725
}
726726

727+
Function *Intrinsic::getDeclarationIfExists(const Module *M, ID id) {
728+
return M->getFunction(getName(id));
729+
}
730+
731+
Function *Intrinsic::getDeclarationIfExists(Module *M, ID id,
732+
ArrayRef<Type *> Tys,
733+
FunctionType *FT) {
734+
return M->getFunction(getName(id, Tys, M, FT));
735+
}
736+
727737
// This defines the "Intrinsic::getIntrinsicForClangBuiltin()" method.
728738
#define GET_LLVM_INTRINSIC_FOR_CLANG_BUILTIN
729739
#include "llvm/IR/IntrinsicImpl.inc"

llvm/lib/LTO/LTO.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,13 +1120,13 @@ Error LTO::checkPartiallySplit() {
11201120
if (!ThinLTO.CombinedIndex.partiallySplitLTOUnits())
11211121
return Error::success();
11221122

1123-
Function *TypeTestFunc = RegularLTO.CombinedModule->getFunction(
1124-
Intrinsic::getName(Intrinsic::type_test));
1125-
Function *TypeCheckedLoadFunc = RegularLTO.CombinedModule->getFunction(
1126-
Intrinsic::getName(Intrinsic::type_checked_load));
1127-
Function *TypeCheckedLoadRelativeFunc =
1128-
RegularLTO.CombinedModule->getFunction(
1129-
Intrinsic::getName(Intrinsic::type_checked_load_relative));
1123+
const Module *Combined = RegularLTO.CombinedModule.get();
1124+
Function *TypeTestFunc =
1125+
Intrinsic::getDeclarationIfExists(Combined, Intrinsic::type_test);
1126+
Function *TypeCheckedLoadFunc =
1127+
Intrinsic::getDeclarationIfExists(Combined, Intrinsic::type_checked_load);
1128+
Function *TypeCheckedLoadRelativeFunc = Intrinsic::getDeclarationIfExists(
1129+
Combined, Intrinsic::type_checked_load_relative);
11301130

11311131
// First check if there are type tests / type checked loads in the
11321132
// merged regular LTO module IR.

llvm/lib/Target/AMDGPU/AMDGPULowerKernelArguments.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ class PreloadKernelArgInfo {
171171
// Try to allocate SGPRs to preload implicit kernel arguments.
172172
void tryAllocImplicitArgPreloadSGPRs(uint64_t ImplicitArgsBaseOffset,
173173
IRBuilder<> &Builder) {
174-
StringRef Name = Intrinsic::getName(Intrinsic::amdgcn_implicitarg_ptr);
175-
Function *ImplicitArgPtr = F.getParent()->getFunction(Name);
174+
Function *ImplicitArgPtr = Intrinsic::getDeclarationIfExists(
175+
F.getParent(), Intrinsic::amdgcn_implicitarg_ptr);
176176
if (!ImplicitArgPtr)
177177
return;
178178

llvm/lib/Target/AMDGPU/AMDGPULowerKernelAttributes.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ class AMDGPULowerKernelAttributes : public ModulePass {
7878
Function *getBasePtrIntrinsic(Module &M, bool IsV5OrAbove) {
7979
auto IntrinsicId = IsV5OrAbove ? Intrinsic::amdgcn_implicitarg_ptr
8080
: Intrinsic::amdgcn_dispatch_ptr;
81-
StringRef Name = Intrinsic::getName(IntrinsicId);
82-
return M.getFunction(Name);
81+
return Intrinsic::getDeclarationIfExists(&M, IntrinsicId);
8382
}
8483

8584
} // end anonymous namespace

llvm/lib/Target/AMDGPU/SIISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8786,7 +8786,7 @@ SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
87868786

87878787
const Module *M = MF.getFunction().getParent();
87888788
const GlobalValue *GV =
8789-
M->getNamedValue(Intrinsic::getName(Intrinsic::amdgcn_groupstaticsize));
8789+
Intrinsic::getDeclarationIfExists(M, Intrinsic::amdgcn_groupstaticsize);
87908790
SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
87918791
SIInstrInfo::MO_ABS32_LO);
87928792
return {DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, GA), 0};

llvm/lib/Transforms/IPO/ExpandVariadics.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,10 @@ class VariadicABIInfo {
145145
// function here in the meantime to decouple from that discussion.
146146
Function *getPreexistingDeclaration(Module *M, Intrinsic::ID Id,
147147
ArrayRef<Type *> Tys = {}) {
148+
if (Tys.empty())
149+
return Intrinsic::getDeclarationIfExists(M, Id);
148150
auto *FT = Intrinsic::getType(M->getContext(), Id, Tys);
149-
return M->getFunction(Tys.empty() ? Intrinsic::getName(Id)
150-
: Intrinsic::getName(Id, Tys, M, FT));
151+
return Intrinsic::getDeclarationIfExists(M, Id, Tys, FT);
151152
}
152153

153154
class ExpandVariadics : public ModulePass {

llvm/lib/Transforms/IPO/GlobalDCE.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ void GlobalDCEPass::ScanVTableLoad(Function *Caller, Metadata *TypeId,
186186
void GlobalDCEPass::ScanTypeCheckedLoadIntrinsics(Module &M) {
187187
LLVM_DEBUG(dbgs() << "Scanning type.checked.load intrinsics\n");
188188
Function *TypeCheckedLoadFunc =
189-
M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load));
190-
Function *TypeCheckedLoadRelativeFunc =
191-
M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load_relative));
189+
Intrinsic::getDeclarationIfExists(&M, Intrinsic::type_checked_load);
190+
Function *TypeCheckedLoadRelativeFunc = Intrinsic::getDeclarationIfExists(
191+
&M, Intrinsic::type_checked_load_relative);
192192

193193
auto scan = [&](Function *CheckedLoadFunc) {
194194
if (!CheckedLoadFunc)

0 commit comments

Comments
 (0)