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
@@ -4763,8 +4763,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;
   }
@@ -4773,6 +4776,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;
 }
 
@@ -4806,8 +4810,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;
   }
@@ -4818,6 +4825,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;
 }
 
@@ -4833,16 +4841,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);
 }
@@ -4861,8 +4872,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;
   }
@@ -4890,6 +4904,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;
 }
 
@@ -4906,10 +4921,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;
   }
 
@@ -4922,6 +4938,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;
 }
 
@@ -5354,8 +5371,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;
   }
@@ -5395,6 +5414,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;
 }
 
@@ -5402,8 +5422,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;
   }
@@ -5421,6 +5443,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;
 }
 
@@ -5588,8 +5611,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;
   }
@@ -5597,6 +5622,7 @@
   auto signature = getValueWitnessSignature(index);
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -6005,8 +6031,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;
   }
@@ -6017,6 +6045,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;
 }
 
@@ -6028,8 +6057,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;
   }
@@ -6042,6 +6073,7 @@
   entry = createFunction(*this, link, signature);
   ApplyIRLinkage({link.getLinkage(), link.getVisibility(), link.getDLLStorage()})
       .to(entry, link.isForDefinition());
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -6109,8 +6141,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;
   }
@@ -6118,6 +6152,7 @@
   auto signature = getAssociatedTypeWitnessTableAccessFunctionSignature();
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -6128,8 +6163,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;
   }
@@ -6137,6 +6174,7 @@
   auto signature = getAssociatedTypeWitnessTableAccessFunctionSignature();
   LinkInfo link = LinkInfo::get(*this, entity, forDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
@@ -6144,12 +6182,17 @@
 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;
+  }
 
   auto signature = Signature::forCoroutineContinuation(*this, fnType);
   LinkInfo link = LinkInfo::get(*this, entity, NotForDefinition);
   entry = createFunction(*this, link, signature);
+  GlobalFuncs.insert({entity, entry});
   return entry;
 }
 
