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
4951using namespace llvm ;
5052
@@ -2472,10 +2474,24 @@ LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef Mod,
24722474}
24732475
24742476const 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
24812497LLVMTypeRef 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));
0 commit comments