forked from OSchip/llvm-project
Creating a named struct requires only a Context and a name, but looking up a struct by name requires a Module. The method on Module merely accesses the LLVMContextImpl and no data from the module itself, so this patch moves getTypeByName to a static method on StructType that takes a Context and a name.
There's a small number of users of this function, they are all updated. This updates the C API adding a new method LLVMGetTypeByName2 that takes a context and a name. Differential Revision: https://reviews.llvm.org/D78793
This commit is contained in:
parent
abef659a45
commit
fe43168348
|
@ -626,6 +626,11 @@ const char *LLVMGetStringAttributeValue(LLVMAttributeRef A, unsigned *Length);
|
||||||
LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A);
|
LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A);
|
||||||
LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A);
|
LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtain a Type from a context by its registered name.
|
||||||
|
*/
|
||||||
|
LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
*/
|
*/
|
||||||
|
@ -867,9 +872,7 @@ LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty,
|
||||||
*/
|
*/
|
||||||
LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M);
|
LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M);
|
||||||
|
|
||||||
/**
|
/** Deprecated: Use LLVMGetTypeByName2 instead. */
|
||||||
* Obtain a Type from a module by its registered name.
|
|
||||||
*/
|
|
||||||
LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name);
|
LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -273,6 +273,10 @@ public:
|
||||||
return llvm::StructType::get(Ctx, StructFields);
|
return llvm::StructType::get(Ctx, StructFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the type with the specified name, or null if there is none by that
|
||||||
|
/// name.
|
||||||
|
static StructType *getTypeByName(LLVMContext &C, StringRef Name);
|
||||||
|
|
||||||
bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
|
bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
|
||||||
|
|
||||||
/// Return true if this type is uniqued by structural equivalence, false if it
|
/// Return true if this type is uniqued by structural equivalence, false if it
|
||||||
|
|
|
@ -329,10 +329,6 @@ public:
|
||||||
/// \see LLVMContext::getOperandBundleTagID
|
/// \see LLVMContext::getOperandBundleTagID
|
||||||
void getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const;
|
void getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const;
|
||||||
|
|
||||||
/// Return the type with the specified name, or null if there is none by that
|
|
||||||
/// name.
|
|
||||||
StructType *getTypeByName(StringRef Name) const;
|
|
||||||
|
|
||||||
std::vector<StructType *> getIdentifiedStructTypes() const;
|
std::vector<StructType *> getIdentifiedStructTypes() const;
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
|
|
|
@ -1349,7 +1349,7 @@ void OpenMPIRBuilder::initializeTypes(Module &M) {
|
||||||
VarName = FunctionType::get(ReturnType, {__VA_ARGS__}, IsVarArg); \
|
VarName = FunctionType::get(ReturnType, {__VA_ARGS__}, IsVarArg); \
|
||||||
VarName##Ptr = PointerType::getUnqual(VarName);
|
VarName##Ptr = PointerType::getUnqual(VarName);
|
||||||
#define OMP_STRUCT_TYPE(VarName, StructName, ...) \
|
#define OMP_STRUCT_TYPE(VarName, StructName, ...) \
|
||||||
T = M.getTypeByName(StructName); \
|
T = StructType::getTypeByName(Ctx, StructName); \
|
||||||
if (!T) \
|
if (!T) \
|
||||||
T = StructType::create(Ctx, {__VA_ARGS__}, StructName); \
|
T = StructType::create(Ctx, {__VA_ARGS__}, StructName); \
|
||||||
VarName = T; \
|
VarName = T; \
|
||||||
|
|
|
@ -739,7 +739,11 @@ LLVMBool LLVMIsLiteralStruct(LLVMTypeRef StructTy) {
|
||||||
}
|
}
|
||||||
|
|
||||||
LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
|
LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
|
||||||
return wrap(unwrap(M)->getTypeByName(Name));
|
return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
|
||||||
|
}
|
||||||
|
|
||||||
|
LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name) {
|
||||||
|
return wrap(StructType::getTypeByName(*unwrap(C), Name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
|
/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/
|
||||||
|
|
|
@ -533,10 +533,6 @@ bool StructType::isLayoutIdentical(StructType *Other) const {
|
||||||
return elements() == Other->elements();
|
return elements() == Other->elements();
|
||||||
}
|
}
|
||||||
|
|
||||||
StructType *Module::getTypeByName(StringRef Name) const {
|
|
||||||
return getContext().pImpl->NamedStructTypes.lookup(Name);
|
|
||||||
}
|
|
||||||
|
|
||||||
Type *StructType::getTypeAtIndex(const Value *V) const {
|
Type *StructType::getTypeAtIndex(const Value *V) const {
|
||||||
unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
|
unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
|
||||||
assert(indexValid(Idx) && "Invalid structure index!");
|
assert(indexValid(Idx) && "Invalid structure index!");
|
||||||
|
@ -557,6 +553,10 @@ bool StructType::indexValid(const Value *V) const {
|
||||||
return CU && CU->getZExtValue() < getNumElements();
|
return CU && CU->getZExtValue() < getNumElements();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) {
|
||||||
|
return C.pImpl->NamedStructTypes.lookup(Name);
|
||||||
|
}
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
// ArrayType Implementation
|
// ArrayType Implementation
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
|
@ -796,11 +796,11 @@ void IRLinker::computeTypeMapping() {
|
||||||
}
|
}
|
||||||
|
|
||||||
auto STTypePrefix = getTypeNamePrefix(ST->getName());
|
auto STTypePrefix = getTypeNamePrefix(ST->getName());
|
||||||
if (STTypePrefix.size()== ST->getName().size())
|
if (STTypePrefix.size() == ST->getName().size())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Check to see if the destination module has a struct with the prefix name.
|
// Check to see if the destination module has a struct with the prefix name.
|
||||||
StructType *DST = DstM.getTypeByName(STTypePrefix);
|
StructType *DST = StructType::getTypeByName(ST->getContext(), STTypePrefix);
|
||||||
if (!DST)
|
if (!DST)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
|
@ -110,7 +110,7 @@ struct TypeCloner {
|
||||||
LLVMTypeRef S = nullptr;
|
LLVMTypeRef S = nullptr;
|
||||||
const char *Name = LLVMGetStructName(Src);
|
const char *Name = LLVMGetStructName(Src);
|
||||||
if (Name) {
|
if (Name) {
|
||||||
S = LLVMGetTypeByName(M, Name);
|
S = LLVMGetTypeByName2(Ctx, Name);
|
||||||
if (S)
|
if (S)
|
||||||
return S;
|
return S;
|
||||||
S = LLVMStructCreateNamed(Ctx, Name);
|
S = LLVMStructCreateNamed(Ctx, Name);
|
||||||
|
|
|
@ -61,7 +61,7 @@ protected:
|
||||||
TEST_F(TargetLibraryInfoTest, InvalidProto) {
|
TEST_F(TargetLibraryInfoTest, InvalidProto) {
|
||||||
parseAssembly("%foo = type { %foo }\n");
|
parseAssembly("%foo = type { %foo }\n");
|
||||||
|
|
||||||
auto *StructTy = M->getTypeByName("foo");
|
auto *StructTy = StructType::getTypeByName(Context, "foo");
|
||||||
auto *InvalidFTy = FunctionType::get(StructTy, /*isVarArg=*/false);
|
auto *InvalidFTy = FunctionType::get(StructTy, /*isVarArg=*/false);
|
||||||
|
|
||||||
for (unsigned FI = 0; FI != LibFunc::NumLibFuncs; ++FI) {
|
for (unsigned FI = 0; FI != LibFunc::NumLibFuncs; ++FI) {
|
||||||
|
|
|
@ -23,7 +23,7 @@ void ParallelLoopGeneratorKMP::createCallSpawnThreads(Value *SubFn,
|
||||||
Value *Stride) {
|
Value *Stride) {
|
||||||
const std::string Name = "__kmpc_fork_call";
|
const std::string Name = "__kmpc_fork_call";
|
||||||
Function *F = M->getFunction(Name);
|
Function *F = M->getFunction(Name);
|
||||||
Type *KMPCMicroTy = M->getTypeByName("kmpc_micro");
|
Type *KMPCMicroTy = StructType::getTypeByName(M->getContext(), "kmpc_micro");
|
||||||
|
|
||||||
if (!KMPCMicroTy) {
|
if (!KMPCMicroTy) {
|
||||||
// void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid, ...)
|
// void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid, ...)
|
||||||
|
@ -35,7 +35,8 @@ void ParallelLoopGeneratorKMP::createCallSpawnThreads(Value *SubFn,
|
||||||
|
|
||||||
// If F is not available, declare it.
|
// If F is not available, declare it.
|
||||||
if (!F) {
|
if (!F) {
|
||||||
StructType *IdentTy = M->getTypeByName("struct.ident_t");
|
StructType *IdentTy =
|
||||||
|
StructType::getTypeByName(M->getContext(), "struct.ident_t");
|
||||||
|
|
||||||
GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
|
GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
|
||||||
Type *Params[] = {IdentTy->getPointerTo(), Builder.getInt32Ty(),
|
Type *Params[] = {IdentTy->getPointerTo(), Builder.getInt32Ty(),
|
||||||
|
@ -314,7 +315,8 @@ Value *ParallelLoopGeneratorKMP::createCallGlobalThreadNum() {
|
||||||
|
|
||||||
// If F is not available, declare it.
|
// If F is not available, declare it.
|
||||||
if (!F) {
|
if (!F) {
|
||||||
StructType *IdentTy = M->getTypeByName("struct.ident_t");
|
StructType *IdentTy =
|
||||||
|
StructType::getTypeByName(M->getContext(), "struct.ident_t");
|
||||||
|
|
||||||
GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
|
GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
|
||||||
Type *Params[] = {IdentTy->getPointerTo()};
|
Type *Params[] = {IdentTy->getPointerTo()};
|
||||||
|
@ -333,7 +335,8 @@ void ParallelLoopGeneratorKMP::createCallPushNumThreads(Value *GlobalThreadID,
|
||||||
|
|
||||||
// If F is not available, declare it.
|
// If F is not available, declare it.
|
||||||
if (!F) {
|
if (!F) {
|
||||||
StructType *IdentTy = M->getTypeByName("struct.ident_t");
|
StructType *IdentTy =
|
||||||
|
StructType::getTypeByName(M->getContext(), "struct.ident_t");
|
||||||
|
|
||||||
GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
|
GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
|
||||||
Type *Params[] = {IdentTy->getPointerTo(), Builder.getInt32Ty(),
|
Type *Params[] = {IdentTy->getPointerTo(), Builder.getInt32Ty(),
|
||||||
|
@ -356,7 +359,8 @@ void ParallelLoopGeneratorKMP::createCallStaticInit(Value *GlobalThreadID,
|
||||||
const std::string Name =
|
const std::string Name =
|
||||||
is64BitArch() ? "__kmpc_for_static_init_8" : "__kmpc_for_static_init_4";
|
is64BitArch() ? "__kmpc_for_static_init_8" : "__kmpc_for_static_init_4";
|
||||||
Function *F = M->getFunction(Name);
|
Function *F = M->getFunction(Name);
|
||||||
StructType *IdentTy = M->getTypeByName("struct.ident_t");
|
StructType *IdentTy =
|
||||||
|
StructType::getTypeByName(M->getContext(), "struct.ident_t");
|
||||||
|
|
||||||
// If F is not available, declare it.
|
// If F is not available, declare it.
|
||||||
if (!F) {
|
if (!F) {
|
||||||
|
@ -395,7 +399,8 @@ void ParallelLoopGeneratorKMP::createCallStaticInit(Value *GlobalThreadID,
|
||||||
void ParallelLoopGeneratorKMP::createCallStaticFini(Value *GlobalThreadID) {
|
void ParallelLoopGeneratorKMP::createCallStaticFini(Value *GlobalThreadID) {
|
||||||
const std::string Name = "__kmpc_for_static_fini";
|
const std::string Name = "__kmpc_for_static_fini";
|
||||||
Function *F = M->getFunction(Name);
|
Function *F = M->getFunction(Name);
|
||||||
StructType *IdentTy = M->getTypeByName("struct.ident_t");
|
StructType *IdentTy =
|
||||||
|
StructType::getTypeByName(M->getContext(), "struct.ident_t");
|
||||||
|
|
||||||
// If F is not available, declare it.
|
// If F is not available, declare it.
|
||||||
if (!F) {
|
if (!F) {
|
||||||
|
@ -417,7 +422,8 @@ void ParallelLoopGeneratorKMP::createCallDispatchInit(Value *GlobalThreadID,
|
||||||
const std::string Name =
|
const std::string Name =
|
||||||
is64BitArch() ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_4";
|
is64BitArch() ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_4";
|
||||||
Function *F = M->getFunction(Name);
|
Function *F = M->getFunction(Name);
|
||||||
StructType *IdentTy = M->getTypeByName("struct.ident_t");
|
StructType *IdentTy =
|
||||||
|
StructType::getTypeByName(M->getContext(), "struct.ident_t");
|
||||||
|
|
||||||
// If F is not available, declare it.
|
// If F is not available, declare it.
|
||||||
if (!F) {
|
if (!F) {
|
||||||
|
@ -457,7 +463,8 @@ Value *ParallelLoopGeneratorKMP::createCallDispatchNext(Value *GlobalThreadID,
|
||||||
const std::string Name =
|
const std::string Name =
|
||||||
is64BitArch() ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_4";
|
is64BitArch() ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_4";
|
||||||
Function *F = M->getFunction(Name);
|
Function *F = M->getFunction(Name);
|
||||||
StructType *IdentTy = M->getTypeByName("struct.ident_t");
|
StructType *IdentTy =
|
||||||
|
StructType::getTypeByName(M->getContext(), "struct.ident_t");
|
||||||
|
|
||||||
// If F is not available, declare it.
|
// If F is not available, declare it.
|
||||||
if (!F) {
|
if (!F) {
|
||||||
|
@ -488,7 +495,8 @@ GlobalVariable *ParallelLoopGeneratorKMP::createSourceLocation() {
|
||||||
|
|
||||||
if (SourceLocDummy == nullptr) {
|
if (SourceLocDummy == nullptr) {
|
||||||
const std::string StructName = "struct.ident_t";
|
const std::string StructName = "struct.ident_t";
|
||||||
StructType *IdentTy = M->getTypeByName(StructName);
|
StructType *IdentTy =
|
||||||
|
StructType::getTypeByName(M->getContext(), StructName);
|
||||||
|
|
||||||
// If the ident_t StructType is not available, declare it.
|
// If the ident_t StructType is not available, declare it.
|
||||||
// in LLVM-IR: ident_t = type { i32, i32, i32, i32, i8* }
|
// in LLVM-IR: ident_t = type { i32, i32, i32, i32, i8* }
|
||||||
|
|
Loading…
Reference in New Issue