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

GitOrigin-RevId: fe431683484a3041e024ab2373bb707b1ca8d1cf
diff --git a/lib/CodeGen/LoopGeneratorsKMP.cpp b/lib/CodeGen/LoopGeneratorsKMP.cpp
index a359a02..1fa3f89 100644
--- a/lib/CodeGen/LoopGeneratorsKMP.cpp
+++ b/lib/CodeGen/LoopGeneratorsKMP.cpp
@@ -23,7 +23,7 @@
                                                       Value *Stride) {
   const std::string Name = "__kmpc_fork_call";
   Function *F = M->getFunction(Name);
-  Type *KMPCMicroTy = M->getTypeByName("kmpc_micro");
+  Type *KMPCMicroTy = StructType::getTypeByName(M->getContext(), "kmpc_micro");
 
   if (!KMPCMicroTy) {
     // void (*kmpc_micro)(kmp_int32 *global_tid, kmp_int32 *bound_tid, ...)
@@ -35,7 +35,8 @@
 
   // If F is not available, declare it.
   if (!F) {
-    StructType *IdentTy = M->getTypeByName("struct.ident_t");
+    StructType *IdentTy =
+        StructType::getTypeByName(M->getContext(), "struct.ident_t");
 
     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
     Type *Params[] = {IdentTy->getPointerTo(), Builder.getInt32Ty(),
@@ -314,7 +315,8 @@
 
   // If F is not available, declare it.
   if (!F) {
-    StructType *IdentTy = M->getTypeByName("struct.ident_t");
+    StructType *IdentTy =
+        StructType::getTypeByName(M->getContext(), "struct.ident_t");
 
     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
     Type *Params[] = {IdentTy->getPointerTo()};
@@ -333,7 +335,8 @@
 
   // If F is not available, declare it.
   if (!F) {
-    StructType *IdentTy = M->getTypeByName("struct.ident_t");
+    StructType *IdentTy =
+        StructType::getTypeByName(M->getContext(), "struct.ident_t");
 
     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
     Type *Params[] = {IdentTy->getPointerTo(), Builder.getInt32Ty(),
@@ -356,7 +359,8 @@
   const std::string Name =
       is64BitArch() ? "__kmpc_for_static_init_8" : "__kmpc_for_static_init_4";
   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) {
@@ -395,7 +399,8 @@
 void ParallelLoopGeneratorKMP::createCallStaticFini(Value *GlobalThreadID) {
   const std::string Name = "__kmpc_for_static_fini";
   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) {
@@ -417,7 +422,8 @@
   const std::string Name =
       is64BitArch() ? "__kmpc_dispatch_init_8" : "__kmpc_dispatch_init_4";
   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) {
@@ -457,7 +463,8 @@
   const std::string Name =
       is64BitArch() ? "__kmpc_dispatch_next_8" : "__kmpc_dispatch_next_4";
   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) {
@@ -488,7 +495,8 @@
 
   if (SourceLocDummy == nullptr) {
     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.
     // in LLVM-IR: ident_t = type { i32, i32, i32, i32, i8* }