Skip to content

Commit 10c733b

Browse files
committed
[LLVM] Change Intrinsic::getBaseName to return std::string
Change `Intrinsic::getName()` and `Intrinsic::getBaseName()` to return std::string. This is in preparation for reducing the size of the static intrinsic string table as described in https://discourse.llvm.org/t/rfc-compress-intrinsic-name-table/82412
1 parent 18952bd commit 10c733b

File tree

14 files changed

+136
-37
lines changed

14 files changed

+136
-37
lines changed

llvm/include/llvm-c/Core.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2833,11 +2833,17 @@ LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,
28332833
*/
28342834
const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength);
28352835

2836+
/**
2837+
* Retrieves the name of an intrinsic. The caller is responsible for freeing the
2838+
* returned string.
2839+
*
2840+
* @see llvm::Intrinsic::getName()
2841+
*/
2842+
char *LLVMIntrinsicCopyName(unsigned ID, size_t *NameLength);
2843+
28362844
/** Deprecated: Use LLVMIntrinsicCopyOverloadedName2 instead. */
2837-
const char *LLVMIntrinsicCopyOverloadedName(unsigned ID,
2838-
LLVMTypeRef *ParamTypes,
2839-
size_t ParamCount,
2840-
size_t *NameLength);
2845+
char *LLVMIntrinsicCopyOverloadedName(unsigned ID, LLVMTypeRef *ParamTypes,
2846+
size_t ParamCount, size_t *NameLength);
28412847

28422848
/**
28432849
* Copies the name of an overloaded intrinsic identified by a given list of
@@ -2850,10 +2856,9 @@ const char *LLVMIntrinsicCopyOverloadedName(unsigned ID,
28502856
*
28512857
* @see llvm::Intrinsic::getName()
28522858
*/
2853-
const char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod, unsigned ID,
2854-
LLVMTypeRef *ParamTypes,
2855-
size_t ParamCount,
2856-
size_t *NameLength);
2859+
char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod, unsigned ID,
2860+
LLVMTypeRef *ParamTypes,
2861+
size_t ParamCount, size_t *NameLength);
28572862

28582863
/**
28592864
* Obtain if the intrinsic identified by the given ID is overloaded.

llvm/include/llvm/IR/Intrinsics.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ namespace Intrinsic {
5252
/// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
5353
/// Note, this version is for intrinsics with no overloads. Use the other
5454
/// version of getName if overloads are required.
55-
StringRef getName(ID id);
55+
std::string getName(ID id);
5656

5757
/// Return the LLVM name for an intrinsic, without encoded types for
5858
/// overloading, such as "llvm.ssa.copy".
59-
StringRef getBaseName(ID id);
59+
std::string getBaseName(ID id);
6060

6161
/// Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx" or
6262
/// "llvm.ssa.copy.p0s_s.1". Note, this version of getName supports overloads.

llvm/lib/Analysis/IRSimilarityIdentifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void IRInstructionData::setCalleeName(bool MatchByName) {
147147
Intrinsic::getName(IntrinsicID, FT->params(), II->getModule(), FT);
148148
// If there is not an overloaded name, we only need to use this version.
149149
else
150-
CalleeName = Intrinsic::getName(IntrinsicID).str();
150+
CalleeName = Intrinsic::getName(IntrinsicID);
151151

152152
return;
153153
}

llvm/lib/CodeGen/ReplaceWithVeclib.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static bool replaceWithCallToVeclib(const TargetLibraryInfo &TLI,
130130
std::string ScalarName =
131131
Intrinsic::isOverloaded(IID)
132132
? Intrinsic::getName(IID, ScalarArgTypes, II->getModule())
133-
: Intrinsic::getName(IID).str();
133+
: Intrinsic::getName(IID);
134134

135135
// Try to find the mapping for the scalar version of this intrinsic and the
136136
// exact vector width of the call operands in the TargetLibraryInfo. First,

llvm/lib/CodeGen/SelectionDAG/SelectionDAGDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ std::string SDNode::getOperationName(const SelectionDAG *G) const {
161161
unsigned OpNo = getOpcode() == ISD::INTRINSIC_WO_CHAIN ? 0 : 1;
162162
unsigned IID = getOperand(OpNo)->getAsZExtVal();
163163
if (IID < Intrinsic::num_intrinsics)
164-
return Intrinsic::getBaseName((Intrinsic::ID)IID).str();
164+
return Intrinsic::getBaseName((Intrinsic::ID)IID);
165165
if (!G)
166166
return "Unknown intrinsic";
167167
if (const TargetIntrinsicInfo *TII = G->getTarget().getIntrinsicInfo())

llvm/lib/IR/Core.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@
3939
#include "llvm/Support/ManagedStatic.h"
4040
#include "llvm/Support/MathExtras.h"
4141
#include "llvm/Support/MemoryBuffer.h"
42+
#include "llvm/Support/Mutex.h"
4243
#include "llvm/Support/Threading.h"
4344
#include "llvm/Support/raw_ostream.h"
4445
#include <cassert>
4546
#include <cstdlib>
4647
#include <cstring>
4748
#include <system_error>
49+
#include <unordered_map>
4850

4951
using namespace llvm;
5052

@@ -2472,10 +2474,24 @@ LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod,
24722474
}
24732475

24742476
const char *LLVMIntrinsicGetName(unsigned ID, size_t *NameLength) {
2477+
static std::unordered_map<Intrinsic::ID, std::string> IntrinsicNamePool;
2478+
static sys::Mutex Mutex;
24752479
auto IID = llvm_map_to_intrinsic_id(ID);
2476-
auto Str = llvm::Intrinsic::getName(IID);
2480+
2481+
std::lock_guard<sys::Mutex> Guard(Mutex);
2482+
auto [Iter, Inserted] = IntrinsicNamePool.try_emplace(IID);
2483+
if (Inserted)
2484+
Iter->second = llvm::Intrinsic::getName(IID);
2485+
const std::string &Name = Iter->second;
2486+
*NameLength = Name.size();
2487+
return Name.data();
2488+
}
2489+
2490+
char *LLVMIntrinsicCopyName(unsigned ID, size_t *NameLength) {
2491+
auto IID = llvm_map_to_intrinsic_id(ID);
2492+
std::string Str = llvm::Intrinsic::getName(IID);
24772493
*NameLength = Str.size();
2478-
return Str.data();
2494+
return strdup(Str.c_str());
24792495
}
24802496

24812497
LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,
@@ -2485,21 +2501,18 @@ LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,
24852501
return wrap(llvm::Intrinsic::getType(*unwrap(Ctx), IID, Tys));
24862502
}
24872503

2488-
const char *LLVMIntrinsicCopyOverloadedName(unsigned ID,
2489-
LLVMTypeRef *ParamTypes,
2490-
size_t ParamCount,
2491-
size_t *NameLength) {
2504+
char *LLVMIntrinsicCopyOverloadedName(unsigned ID, LLVMTypeRef *ParamTypes,
2505+
size_t ParamCount, size_t *NameLength) {
24922506
auto IID = llvm_map_to_intrinsic_id(ID);
24932507
ArrayRef<Type*> Tys(unwrap(ParamTypes), ParamCount);
24942508
auto Str = llvm::Intrinsic::getNameNoUnnamedTypes(IID, Tys);
24952509
*NameLength = Str.length();
24962510
return strdup(Str.c_str());
24972511
}
24982512

2499-
const char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod, unsigned ID,
2500-
LLVMTypeRef *ParamTypes,
2501-
size_t ParamCount,
2502-
size_t *NameLength) {
2513+
char *LLVMIntrinsicCopyOverloadedName2(LLVMModuleRef Mod, unsigned ID,
2514+
LLVMTypeRef *ParamTypes,
2515+
size_t ParamCount, size_t *NameLength) {
25032516
auto IID = llvm_map_to_intrinsic_id(ID);
25042517
ArrayRef<Type *> Tys(unwrap(ParamTypes), ParamCount);
25052518
auto Str = llvm::Intrinsic::getName(IID, Tys, unwrap(Mod));

llvm/lib/IR/Intrinsics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ static constexpr const char *const IntrinsicNameTable[] = {
4444
#undef GET_INTRINSIC_NAME_TABLE
4545
};
4646

47-
StringRef Intrinsic::getBaseName(ID id) {
47+
std::string Intrinsic::getBaseName(ID id) {
4848
assert(id < num_intrinsics && "Invalid intrinsic ID!");
4949
return IntrinsicNameTable[id];
5050
}
5151

52-
StringRef Intrinsic::getName(ID id) {
52+
std::string Intrinsic::getName(ID id) {
5353
assert(id < num_intrinsics && "Invalid intrinsic ID!");
5454
assert(!Intrinsic::isOverloaded(id) &&
5555
"This version of getName does not support overloading");

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 = F.getParent()->getFunction(
175+
Intrinsic::getName(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 M.getFunction(Intrinsic::getName(IntrinsicId));
8382
}
8483

8584
} // end anonymous namespace

llvm/lib/Transforms/Scalar/LowerMatrixIntrinsics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,8 +2278,8 @@ class LowerMatrixIntrinsics {
22782278
return;
22792279
}
22802280
auto *II = cast<IntrinsicInst>(CI);
2281-
write(Intrinsic::getBaseName(II->getIntrinsicID())
2282-
.drop_front(StringRef("llvm.matrix.").size()));
2281+
std::string IntName = Intrinsic::getBaseName(II->getIntrinsicID());
2282+
write(StringRef(IntName).drop_front(StringRef("llvm.matrix.").size()));
22832283
write(".");
22842284
std::string Tmp;
22852285
raw_string_ostream SS(Tmp);

0 commit comments

Comments
 (0)