Description: Patch from upstream to fix clang crash.
Origin: upstream, https://github.com/swiftlang/llvm-project/commit/91d7ca904
Bug: https://github.com/swiftlang/llvm-project/issues/8741
Forwarded: not-needed
Applied-Upstream: 6.1

--- swiftlang-6.0.3.orig/llvm-project/llvm/include/llvm/IR/DebugInfo.h
+++ swiftlang-6.0.3/llvm-project/llvm/include/llvm/IR/DebugInfo.h
@@ -238,6 +238,10 @@ bool calculateFragmentIntersect(
     uint64_t SliceSizeInBits, const DbgAssignIntrinsic *DAI,
     std::optional<DIExpression::FragmentInfo> &Result);
 
+/// Replace DIAssignID uses and attachments with IDs from \p Map.
+/// If an ID is unmapped a new ID is generated and added to \p Map.
+void remapAssignID(DenseMap<DIAssignID *, DIAssignID *> &Map, Instruction &I);
+
 /// Helper struct for trackAssignments, below. We don't use the similar
 /// DebugVariable class because trackAssignments doesn't (yet?) understand
 /// partial variables (fragment info) as input and want to make that clear and
--- swiftlang-6.0.3.orig/llvm-project/llvm/lib/IR/DebugInfo.cpp
+++ swiftlang-6.0.3/llvm-project/llvm/lib/IR/DebugInfo.cpp
@@ -1913,6 +1913,30 @@ bool at::calculateFragmentIntersect(
   return true;
 }
 
+/// Update inlined instructions' DIAssignID metadata. We need to do this
+/// otherwise a function inlined more than once into the same function
+/// will cause DIAssignID to be shared by many instructions.
+void at::remapAssignID(DenseMap<DIAssignID *, DIAssignID *> &Map,
+                       Instruction &I) {
+  auto GetNewID = [&Map](Metadata *Old) {
+    DIAssignID *OldID = cast<DIAssignID>(Old);
+    if (DIAssignID *NewID = Map.lookup(OldID))
+      return NewID;
+    DIAssignID *NewID = DIAssignID::getDistinct(OldID->getContext());
+    Map[OldID] = NewID;
+    return NewID;
+  };
+  // If we find a DIAssignID attachment or use, replace it with a new version.
+  //for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
+  //  if (DVR.isDbgAssign())
+  //    DVR.setAssignId(GetNewID(DVR.getAssignID()));
+  //}
+  if (auto *ID = I.getMetadata(LLVMContext::MD_DIAssignID))
+    I.setMetadata(LLVMContext::MD_DIAssignID, GetNewID(ID));
+  else if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(&I))
+    DAI->setAssignId(GetNewID(DAI->getAssignID()));
+}
+
 /// Collect constant properies (base, size, offset) of \p StoreDest.
 /// Return std::nullopt if any properties are not constants or the
 /// offset from the base pointer is negative.
--- swiftlang-6.0.3.orig/llvm-project/llvm/lib/Transforms/Utils/CodeExtractor.cpp
+++ swiftlang-6.0.3/llvm-project/llvm/lib/Transforms/Utils/CodeExtractor.cpp
@@ -1605,8 +1605,9 @@ static void fixupDebugInfoPostExtraction
     DII->eraseFromParent();
   DIB.finalizeSubprogram(NewSP);
 
-  // Fix up the scope information attached to the line locations in the new
-  // function.
+  // Fix up the scope information attached to the line locations and the
+  // debug assignment metadata in the new function.
+  DenseMap<DIAssignID *, DIAssignID *> AssignmentIDMap;
   for (Instruction &I : instructions(NewFunc)) {
     if (const DebugLoc &DL = I.getDebugLoc())
       I.setDebugLoc(
@@ -1619,6 +1620,7 @@ static void fixupDebugInfoPostExtraction
       return MD;
     };
     updateLoopMetadataDebugLocations(I, updateLoopInfoLoc);
+    at::remapAssignID(AssignmentIDMap, I);
     if (auto *ID = I.getMetadata(LLVMContext::MD_DIAssignID))
       I.setMetadata(LLVMContext::MD_DIAssignID, nullptr);
   }
--- swiftlang-6.0.3.orig/llvm-project/llvm/lib/Transforms/Utils/InlineFunction.cpp
+++ swiftlang-6.0.3/llvm-project/llvm/lib/Transforms/Utils/InlineFunction.cpp
@@ -1719,25 +1719,12 @@ static void trackInlinedStores(Function:
 /// otherwise a function inlined more than once into the same function
 /// will cause DIAssignID to be shared by many instructions.
 static void fixupAssignments(Function::iterator Start, Function::iterator End) {
-  // Map {Old, New} metadata. Not used directly - use GetNewID.
   DenseMap<DIAssignID *, DIAssignID *> Map;
-  auto GetNewID = [&Map](Metadata *Old) {
-    DIAssignID *OldID = cast<DIAssignID>(Old);
-    if (DIAssignID *NewID = Map.lookup(OldID))
-      return NewID;
-    DIAssignID *NewID = DIAssignID::getDistinct(OldID->getContext());
-    Map[OldID] = NewID;
-    return NewID;
-  };
   // Loop over all the inlined instructions. If we find a DIAssignID
   // attachment or use, replace it with a new version.
   for (auto BBI = Start; BBI != End; ++BBI) {
-    for (Instruction &I : *BBI) {
-      if (auto *ID = I.getMetadata(LLVMContext::MD_DIAssignID))
-        I.setMetadata(LLVMContext::MD_DIAssignID, GetNewID(ID));
-      else if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(&I))
-        DAI->setAssignId(GetNewID(DAI->getAssignID()));
-    }
+    for (Instruction &I : *BBI)
+      at::remapAssignID(Map, I);
   }
 }
 #undef DEBUG_TYPE
