Skip to content

Commit fe43168

Browse files
committed
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
1 parent abef659 commit fe43168

File tree

10 files changed

+41
-26
lines changed

10 files changed

+41
-26
lines changed

llvm/include/llvm-c/Core.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,11 @@ const char *LLVMGetStringAttributeValue(LLVMAttributeRef A, unsigned *Length);
626626
LLVMBool LLVMIsEnumAttribute(LLVMAttributeRef A);
627627
LLVMBool LLVMIsStringAttribute(LLVMAttributeRef A);
628628

629+
/**
630+
* Obtain a Type from a context by its registered name.
631+
*/
632+
LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name);
633+
629634
/**
630635
* @}
631636
*/
@@ -867,9 +872,7 @@ LLVMValueRef LLVMGetInlineAsm(LLVMTypeRef Ty,
867872
*/
868873
LLVMContextRef LLVMGetModuleContext(LLVMModuleRef M);
869874

870-
/**
871-
* Obtain a Type from a module by its registered name.
872-
*/
875+
/** Deprecated: Use LLVMGetTypeByName2 instead. */
873876
LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name);
874877

875878
/**

llvm/include/llvm/IR/DerivedTypes.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ class StructType : public Type {
273273
return llvm::StructType::get(Ctx, StructFields);
274274
}
275275

276+
/// Return the type with the specified name, or null if there is none by that
277+
/// name.
278+
static StructType *getTypeByName(LLVMContext &C, StringRef Name);
279+
276280
bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
277281

278282
/// Return true if this type is uniqued by structural equivalence, false if it

llvm/include/llvm/IR/Module.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,6 @@ class Module {
329329
/// \see LLVMContext::getOperandBundleTagID
330330
void getOperandBundleTags(SmallVectorImpl<StringRef> &Result) const;
331331

332-
/// Return the type with the specified name, or null if there is none by that
333-
/// name.
334-
StructType *getTypeByName(StringRef Name) const;
335-
336332
std::vector<StructType *> getIdentifiedStructTypes() const;
337333

338334
/// @}

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ void OpenMPIRBuilder::initializeTypes(Module &M) {
13491349
VarName = FunctionType::get(ReturnType, {__VA_ARGS__}, IsVarArg); \
13501350
VarName##Ptr = PointerType::getUnqual(VarName);
13511351
#define OMP_STRUCT_TYPE(VarName, StructName, ...) \
1352-
T = M.getTypeByName(StructName); \
1352+
T = StructType::getTypeByName(Ctx, StructName); \
13531353
if (!T) \
13541354
T = StructType::create(Ctx, {__VA_ARGS__}, StructName); \
13551355
VarName = T; \

llvm/lib/IR/Core.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,11 @@ LLVMBool LLVMIsLiteralStruct(LLVMTypeRef StructTy) {
739739
}
740740

741741
LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name) {
742-
return wrap(unwrap(M)->getTypeByName(Name));
742+
return wrap(StructType::getTypeByName(unwrap(M)->getContext(), Name));
743+
}
744+
745+
LLVMTypeRef LLVMGetTypeByName2(LLVMContextRef C, const char *Name) {
746+
return wrap(StructType::getTypeByName(*unwrap(C), Name));
743747
}
744748

745749
/*--.. Operations on array, pointer, and vector types (sequence types) .....--*/

llvm/lib/IR/Type.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,6 @@ bool StructType::isLayoutIdentical(StructType *Other) const {
533533
return elements() == Other->elements();
534534
}
535535

536-
StructType *Module::getTypeByName(StringRef Name) const {
537-
return getContext().pImpl->NamedStructTypes.lookup(Name);
538-
}
539-
540536
Type *StructType::getTypeAtIndex(const Value *V) const {
541537
unsigned Idx = (unsigned)cast<Constant>(V)->getUniqueInteger().getZExtValue();
542538
assert(indexValid(Idx) && "Invalid structure index!");
@@ -557,6 +553,10 @@ bool StructType::indexValid(const Value *V) const {
557553
return CU && CU->getZExtValue() < getNumElements();
558554
}
559555

556+
StructType *StructType::getTypeByName(LLVMContext &C, StringRef Name) {
557+
return C.pImpl->NamedStructTypes.lookup(Name);
558+
}
559+
560560
//===----------------------------------------------------------------------===//
561561
// ArrayType Implementation
562562
//===----------------------------------------------------------------------===//

llvm/lib/Linker/IRMover.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,11 +796,11 @@ void IRLinker::computeTypeMapping() {
796796
}
797797

798798
auto STTypePrefix = getTypeNamePrefix(ST->getName());
799-
if (STTypePrefix.size()== ST->getName().size())
799+
if (STTypePrefix.size() == ST->getName().size())
800800
continue;
801801

802802
// Check to see if the destination module has a struct with the prefix name.
803-
StructType *DST = DstM.getTypeByName(STTypePrefix);
803+
StructType *DST = StructType::getTypeByName(ST->getContext(), STTypePrefix);
804804
if (!DST)
805805
continue;
806806

llvm/tools/llvm-c-test/echo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ struct TypeCloner {
110110
LLVMTypeRef S = nullptr;
111111
const char *Name = LLVMGetStructName(Src);
112112
if (Name) {
113-
S = LLVMGetTypeByName(M, Name);
113+
S = LLVMGetTypeByName2(Ctx, Name);
114114
if (S)
115115
return S;
116116
S = LLVMStructCreateNamed(Ctx, Name);

llvm/unittests/Analysis/TargetLibraryInfoTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class TargetLibraryInfoTest : public testing::Test {
6161
TEST_F(TargetLibraryInfoTest, InvalidProto) {
6262
parseAssembly("%foo = type { %foo }\n");
6363

64-
auto *StructTy = M->getTypeByName("foo");
64+
auto *StructTy = StructType::getTypeByName(Context, "foo");
6565
auto *InvalidFTy = FunctionType::get(StructTy, /*isVarArg=*/false);
6666

6767
for (unsigned FI = 0; FI != LibFunc::NumLibFuncs; ++FI) {

polly/lib/CodeGen/LoopGeneratorsKMP.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void ParallelLoopGeneratorKMP::createCallSpawnThreads(Value *SubFn,
2323
Value *Stride) {
2424
const std::string Name = "__kmpc_fork_call";
2525
Function *F = M->getFunction(Name);
26-
Type *KMPCMicroTy = M->getTypeByName("kmpc_micro");
26+
Type *KMPCMicroTy = StructType::getTypeByName(M->getContext(), "kmpc_micro");
2727

2828
if (!KMPCMicroTy) {
2929
// void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid, ...)
@@ -35,7 +35,8 @@ void ParallelLoopGeneratorKMP::createCallSpawnThreads(Value *SubFn,
3535

3636
// If F is not available, declare it.
3737
if (!F) {
38-
StructType *IdentTy = M->getTypeByName("struct.ident_t");
38+
StructType *IdentTy =
39+
StructType::getTypeByName(M->getContext(), "struct.ident_t");
3940

4041
GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
4142
Type *Params[] = {IdentTy->getPointerTo(), Builder.getInt32Ty(),
@@ -314,7 +315,8 @@ Value *ParallelLoopGeneratorKMP::createCallGlobalThreadNum() {
314315

315316
// If F is not available, declare it.
316317
if (!F) {
317-
StructType *IdentTy = M->getTypeByName("struct.ident_t");
318+
StructType *IdentTy =
319+
StructType::getTypeByName(M->getContext(), "struct.ident_t");
318320

319321
GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
320322
Type *Params[] = {IdentTy->getPointerTo()};
@@ -333,7 +335,8 @@ void ParallelLoopGeneratorKMP::createCallPushNumThreads(Value *GlobalThreadID,
333335

334336
// If F is not available, declare it.
335337
if (!F) {
336-
StructType *IdentTy = M->getTypeByName("struct.ident_t");
338+
StructType *IdentTy =
339+
StructType::getTypeByName(M->getContext(), "struct.ident_t");
337340

338341
GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
339342
Type *Params[] = {IdentTy->getPointerTo(), Builder.getInt32Ty(),
@@ -356,7 +359,8 @@ void ParallelLoopGeneratorKMP::createCallStaticInit(Value *GlobalThreadID,
356359
const std::string Name =
357360
is64BitArch() ? "__kmpc_for_static_init_8" : "__kmpc_for_static_init_4";
358361
Function *F = M->getFunction(Name);
359-
StructType *IdentTy = M->getTypeByName("struct.ident_t");
362+
StructType *IdentTy =
363+
StructType::getTypeByName(M->getContext(), "struct.ident_t");
360364

361365
// If F is not available, declare it.
362366
if (!F) {
@@ -395,7 +399,8 @@ void ParallelLoopGeneratorKMP::createCallStaticInit(Value *GlobalThreadID,
395399
void ParallelLoopGeneratorKMP::createCallStaticFini(Value *GlobalThreadID) {
396400
const std::string Name = "__kmpc_for_static_fini";
397401
Function *F = M->getFunction(Name);
398-
StructType *IdentTy = M->getTypeByName("struct.ident_t");
402+
StructType *IdentTy =
403+
StructType::getTypeByName(M->getContext(), "struct.ident_t");
399404

400405
// If F is not available, declare it.
401406
if (!F) {
@@ -417,7 +422,8 @@ void ParallelLoopGeneratorKMP::createCallDispatchInit(Value *GlobalThreadID,
417422
const std::string Name =
418423
is64BitArch() ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_4";
419424
Function *F = M->getFunction(Name);
420-
StructType *IdentTy = M->getTypeByName("struct.ident_t");
425+
StructType *IdentTy =
426+
StructType::getTypeByName(M->getContext(), "struct.ident_t");
421427

422428
// If F is not available, declare it.
423429
if (!F) {
@@ -457,7 +463,8 @@ Value *ParallelLoopGeneratorKMP::createCallDispatchNext(Value *GlobalThreadID,
457463
const std::string Name =
458464
is64BitArch() ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_4";
459465
Function *F = M->getFunction(Name);
460-
StructType *IdentTy = M->getTypeByName("struct.ident_t");
466+
StructType *IdentTy =
467+
StructType::getTypeByName(M->getContext(), "struct.ident_t");
461468

462469
// If F is not available, declare it.
463470
if (!F) {
@@ -488,7 +495,8 @@ GlobalVariable *ParallelLoopGeneratorKMP::createSourceLocation() {
488495

489496
if (SourceLocDummy == nullptr) {
490497
const std::string StructName = "struct.ident_t";
491-
StructType *IdentTy = M->getTypeByName(StructName);
498+
StructType *IdentTy =
499+
StructType::getTypeByName(M->getContext(), StructName);
492500

493501
// If the ident_t StructType is not available, declare it.
494502
// in LLVM-IR: ident_t = type { i32, i32, i32, i32, i8* }

0 commit comments

Comments
 (0)