Description: Clang 17 and newer crash on arm64
Forwarded: https://github.com/swiftlang/swift/issues/70345

--- a/swift/lib/IRGen/GenDecl.cpp
+++ b/swift/lib/IRGen/GenDecl.cpp
@@ -4810,8 +4810,11 @@
   assert(ObjCInterop);
 
   LinkEntity entity = LinkEntity::forObjCMetadataUpdateFunction(classDecl);
-  llvm::Function *&entry = GlobalFuncs[entity];
-  if (entry) {
+  auto found = GlobalFuncs.find(entity);
+  llvm::Function *entry = nullptr;
+
+  if (found != GlobalFuncs.end()) {
+    entry = found->second;
     if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
     return entry;
   }
@@ -4820,6 +4823,7 @@
   Signature signature(ObjCUpdateCallbackTy, llvm::AttributeList(), DefaultCC);
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -4853,8 +4857,11 @@
   IRGen.noteUseOfTypeMetadata(Nominal);
 
   LinkEntity entity = LinkEntity::forTypeMetadataAccessFunction(type);
-  llvm::Function *&entry = GlobalFuncs[entity];
-  if (entry) {
+  auto found = GlobalFuncs.find(entity);
+  llvm::Function *entry = nullptr;
+
+  if (found != GlobalFuncs.end()) {
+    entry = found->second;
     if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
     return entry;
   }
@@ -4865,6 +4872,7 @@
   Signature signature(fnType, llvm::AttributeList(), SwiftCC);
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -4880,16 +4888,19 @@
   auto fnType = llvm::FunctionType::get(OpaqueTypeDescriptorPtrTy, {}, false);
   Signature signature(fnType, llvm::AttributeList(), SwiftCC);
 
-  llvm::Function *&entry = GlobalFuncs[entity];
-  if (entry) {
-    if (forDefinition)
-      updateLinkageForDefinition(*this, entry, entity);
+  auto found = GlobalFuncs.find(entity);
+  llvm::Function *entry = nullptr;
+
+  if (found != GlobalFuncs.end()) {
+    entry = found->second;
+    if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
     return FunctionPointer::forDirect(FunctionPointer::Kind::Function, entry,
                                       nullptr, signature);
   }
 
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return FunctionPointer::forDirect(FunctionPointer::Kind::Function, entry,
                                     nullptr, signature);
 }
@@ -4908,8 +4919,11 @@
   auto type = nominal->getDeclaredType()->getCanonicalType();
   assert(type->hasUnboundGenericType());
   LinkEntity entity = LinkEntity::forTypeMetadataAccessFunction(type);
-  llvm::Function *&entry = GlobalFuncs[entity];
-  if (entry) {
+  auto found = GlobalFuncs.find(entity);
+  llvm::Function *entry = nullptr;
+
+  if (found != GlobalFuncs.end()) {
+    entry = found->second;
     if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
     return entry;
   }
@@ -4937,6 +4951,7 @@
   Signature signature(fnType, llvm::AttributeList(), SwiftCC);
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -4953,10 +4968,11 @@
 
   LinkEntity entity =
       LinkEntity::forPrespecializedTypeMetadataAccessFunction(theType);
-  llvm::Function *&entry = GlobalFuncs[entity];
-  if (entry) {
-    if (forDefinition)
-      updateLinkageForDefinition(*this, entry, entity);
+  auto found = GlobalFuncs.find(entity);
+  llvm::Function *entry = nullptr;
+  if (found != GlobalFuncs.end()) {
+    entry = found->second;
+    if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
     return entry;
   }
 
@@ -4969,6 +4985,7 @@
   Signature signature(functionType, llvm::AttributeList(), SwiftCC);
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -5401,8 +5418,10 @@
 IRGenModule::getAddrOfTypeMetadataInstantiationFunction(NominalTypeDecl *D,
                                               ForDefinition_t forDefinition) {
   LinkEntity entity = LinkEntity::forTypeMetadataInstantiationFunction(D);
-  llvm::Function *&entry = GlobalFuncs[entity];
-  if (entry) {
+  auto found = GlobalFuncs.find(entity);
+  llvm::Function *entry = nullptr;
+  if (found != GlobalFuncs.end()) {
+    entry = found->second;
     if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
     return entry;
   }
@@ -5442,6 +5461,7 @@
   Signature signature(fnType, llvm::AttributeList(), DefaultCC);
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -5449,8 +5469,10 @@
 IRGenModule::getAddrOfTypeMetadataCompletionFunction(NominalTypeDecl *D,
                                               ForDefinition_t forDefinition) {
   LinkEntity entity = LinkEntity::forTypeMetadataCompletionFunction(D);
-  llvm::Function *&entry = GlobalFuncs[entity];
-  if (entry) {
+  auto found = GlobalFuncs.find(entity);
+  llvm::Function *entry = nullptr;
+  if (found != GlobalFuncs.end()) {
+    entry = found->second;
     if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
     return entry;
   }
@@ -5468,6 +5490,7 @@
   Signature signature(fnType, llvm::AttributeList(), SwiftCC);
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -5635,8 +5658,10 @@
                                                 ForDefinition_t forDefinition) {
   LinkEntity entity = LinkEntity::forValueWitness(abstractType, index);
 
-  llvm::Function *&entry = GlobalFuncs[entity];
-  if (entry) {
+   auto found = GlobalFuncs.find(entity);
+  llvm::Function *entry = nullptr;
+  if (found != GlobalFuncs.end()) {
+    entry = found->second;
     if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
     return entry;
   }
@@ -5644,6 +5669,7 @@
   auto signature = getValueWitnessSignature(index);
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -6058,8 +6084,10 @@
 
   LinkEntity entity =
     LinkEntity::forGenericProtocolWitnessTableInstantiationFunction(conf);
-  llvm::Function *&entry = GlobalFuncs[entity];
-  if (entry) {
+  auto found = GlobalFuncs.find(entity);
+  llvm::Function *entry = nullptr;
+  if (found != GlobalFuncs.end()) {
+    entry = found->second;
     if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
     return entry;
   }
@@ -6070,6 +6098,7 @@
   Signature signature(fnType, llvm::AttributeList(), DefaultCC);
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -6081,8 +6110,10 @@
                                               ForDefinition_t forDefinition) {
   LinkEntity entity =
     LinkEntity::forProtocolWitnessTableLazyAccessFunction(conf, conformingType);
-  llvm::Function *&entry = GlobalFuncs[entity];
-  if (entry) {
+  auto found = GlobalFuncs.find(entity);
+  llvm::Function *entry = nullptr;
+  if (found != GlobalFuncs.end()) {
+    entry = found->second;
     if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
     return entry;
   }
@@ -6095,6 +6126,7 @@
   entry = createFunction(*this, link, signature);
   ApplyIRLinkage({link.getLinkage(), link.getVisibility(), link.getDLLStorage()})
       .to(entry, link.isForDefinition());
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -6162,8 +6194,10 @@
   LinkEntity entity =
     LinkEntity::forAssociatedTypeWitnessTableAccessFunction(conformance,
                                                             association);
-  llvm::Function *&entry = GlobalFuncs[entity];
-  if (entry) {
+  auto found = GlobalFuncs.find(entity);
+  llvm::Function *entry = nullptr;
+  if (found != GlobalFuncs.end()) {
+    entry = found->second;
     if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
     return entry;
   }
@@ -6171,6 +6205,7 @@
   auto signature = getAssociatedTypeWitnessTableAccessFunctionSignature();
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -6181,8 +6216,10 @@
 
   LinkEntity entity =
     LinkEntity::forDefaultAssociatedConformanceAccessor(requirement);
-  llvm::Function *&entry = GlobalFuncs[entity];
-  if (entry) {
+  auto found = GlobalFuncs.find(entity);
+  llvm::Function *entry = nullptr;
+  if (found != GlobalFuncs.end()) {
+    entry = found->second;
     if (forDefinition) updateLinkageForDefinition(*this, entry, entity);
     return entry;
   }
@@ -6190,6 +6227,7 @@
   auto signature = getAssociatedTypeWitnessTableAccessFunctionSignature();
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -6197,13 +6235,18 @@
 IRGenModule::getAddrOfContinuationPrototype(CanSILFunctionType fnType) {
   LinkEntity entity = LinkEntity::forCoroutineContinuationPrototype(fnType);
 
-  llvm::Function *&entry = GlobalFuncs[entity];
-  if (entry) return entry;
+  auto found = GlobalFuncs.find(entity);
+  llvm::Function *entry = nullptr;
+  if (found != GlobalFuncs.end()) {
+    entry = found->second;
+    return entry;
+  }
 
   GenericContextScope scope(*this, fnType->getInvocationGenericSignature());
   auto signature = Signature::forCoroutineContinuation(*this, fnType);
   LinkInfo link = LinkInfo::get(*this, entity, NotForDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
