Redo this. The compile unit iterator has the unhelpful behavior
of skipping entries that are there.
diff --git a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
index e64ad6a..5379f5f 100644
--- a/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
@@ -625,13 +625,15 @@
   if (Asm->hasDebugInfo()) {
     Node = *M->debug_compile_units_begin();
   } else {
-    auto DebugCompileUnits = MMI->getModule()->debug_compile_units();
-    if (DebugCompileUnits.empty())
-      return;
-
     // When emitting only compiler information, we may have only NoDebug CUs,
     // which would be skipped by debug_compile_units_begin.
-    Node = *DebugCompileUnits.begin();
+    NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
+    if (CUs->operands().empty()) {
+      Asm = nullptr;
+      return;
+    }
+
+    Node = *CUs->operands().begin();
   }
   const auto *CU = cast<DICompileUnit>(Node);
   DISourceLanguageName Lang = CU->getSourceLanguage();
@@ -903,18 +905,19 @@
   OS.AddComment("CPUType");
   OS.emitInt16(static_cast<uint64_t>(TheCPU));
 
-  StringRef CompilerVersion = "0";
-  auto CUs = MMI->getModule()->debug_compile_units();
-  if (!CUs.empty()) {
-    const MDNode *Node = *CUs.begin();
-    const auto *CU = cast<DICompileUnit>(Node);
+  NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
 
+  StringRef CompilerVersion = "0";
+  if (!CUs->operands().empty()) {
+    const MDNode *Node = *CUs->operands().begin();
+    const auto *CU = cast<DICompileUnit>(Node);
     CompilerVersion = CU->getProducer();
-    Version FrontVer = parseVersion(CompilerVersion);
-    OS.AddComment("Frontend version");
-    for (int N : FrontVer.Part) {
-      OS.emitInt16(N);
-    }
+  }
+
+  Version FrontVer = parseVersion(CompilerVersion);
+  OS.AddComment("Frontend version");
+  for (int N : FrontVer.Part) {
+    OS.emitInt16(N);
   }
 
   // Some Microsoft tools, like Binscope, expect a backend version number of at
@@ -954,11 +957,8 @@
   // not clear if the compiler path should refer to the executable for the
   // frontend or the backend. Leave it blank for now.
   TypeIndex BuildInfoArgs[BuildInfoRecord::MaxArgs] = {};
-  auto CUs = MMI->getModule()->debug_compile_units();
-  if (CUs.empty())
-    return;
-
-  const MDNode *Node = *CUs.begin(); // FIXME: Multiple CUs.
+  NamedMDNode *CUs = MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
+  const MDNode *Node = *CUs->operands().begin(); // FIXME: Multiple CUs.
   const auto *CU = cast<DICompileUnit>(Node);
   const DIFile *MainSourceFile = CU->getFile();
   BuildInfoArgs[BuildInfoRecord::CurrentDirectory] =
diff --git a/llvm/test/DebugInfo/X86/codeview-empty-dbg-cu-crash.ll b/llvm/test/DebugInfo/X86/codeview-empty-dbg-cu-crash.ll
index b40b48c..51435b1 100644
--- a/llvm/test/DebugInfo/X86/codeview-empty-dbg-cu-crash.ll
+++ b/llvm/test/DebugInfo/X86/codeview-empty-dbg-cu-crash.ll
@@ -19,6 +19,10 @@
 ; CHECK-NEXT: .short	4412                            # Record kind: S_COMPILE3
 ; CHECK-NEXT: .long	3                               # Flags and language
 ; CHECK-NEXT: .short	208                             # CPUType
+; CHECK-NEXT: .short	0                               # Frontend version
+; CHECK-NEXT: .short	0
+; CHECK-NEXT: .short	0
+; CHECK-NEXT: .short	0
 ; CHECK-NEXT: .short	22000                           # Backend version
 ; CHECK-NEXT: .short	0
 ; CHECK-NEXT: .short	0
@@ -28,8 +32,6 @@
 ; CHECK-NEXT: .Ltmp5:
 ; CHECK-NEXT: .Ltmp1:
 ; CHECK-NEXT: .p2align	2, 0x0
-; CHECK-NEXT: .cv_filechecksums                       # File index to string table offset subsection
-; CHECK-NEXT: .cv_stringtable                         # String table
 
 !llvm.dbg.cu = !{}
 !llvm.module.flags = !{!0}