@@ -613,19 +613,30 @@ static SourceLanguage MapDWLangToCVLang(unsigned DWLang) {
613613void CodeViewDebug::beginModule (Module *M) {
614614 // If module doesn't have named metadata anchors or COFF debug section
615615 // is not available, skip any debug info related stuff.
616- if (!Asm->hasDebugInfo () ||
617- !Asm->getObjFileLowering ().getCOFFDebugSymbolsSection ()) {
616+ if (!Asm->getObjFileLowering ().getCOFFDebugSymbolsSection ()) {
618617 Asm = nullptr ;
619618 return ;
620619 }
621620
622621 TheCPU = mapArchToCVCPUType (M->getTargetTriple ().getArch ());
623622
624623 // Get the current source language.
625- const MDNode *Node = *M->debug_compile_units_begin ();
624+ const MDNode *Node;
625+ if (Asm->hasDebugInfo ()) {
626+ Node = *M->debug_compile_units_begin ();
627+ } else {
628+ // When emitting only compiler information, we may have only NoDebug CUs,
629+ // which would be skipped by debug_compile_units_begin.
630+ NamedMDNode *CUs = MMI->getModule ()->getNamedMetadata (" llvm.dbg.cu" );
631+ Node = *CUs->operands ().begin ();
632+ }
626633 const auto *CU = cast<DICompileUnit>(Node);
627634
628635 CurrentSourceLanguage = MapDWLangToCVLang (CU->getSourceLanguage ());
636+ NoDebug =
637+ !M->getCodeViewFlag () || CU->getEmissionKind () == DICompileUnit::NoDebug;
638+ if (NoDebug)
639+ return ;
629640
630641 collectGlobalVariableInfo ();
631642
@@ -636,7 +647,7 @@ void CodeViewDebug::beginModule(Module *M) {
636647}
637648
638649void CodeViewDebug::endModule () {
639- if (!Asm || !Asm-> hasDebugInfo () )
650+ if (!Asm)
640651 return ;
641652
642653 // The COFF .debug$S section consists of several subsections, each starting
@@ -652,6 +663,8 @@ void CodeViewDebug::endModule() {
652663 emitObjName ();
653664 emitCompilerInformation ();
654665 endCVSubsection (CompilerInfo);
666+ if (NoDebug)
667+ return ;
655668
656669 emitInlineeLinesSubsection ();
657670
@@ -1440,6 +1453,9 @@ void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
14401453}
14411454
14421455void CodeViewDebug::beginFunctionImpl (const MachineFunction *MF) {
1456+ if (NoDebug)
1457+ return ;
1458+
14431459 const TargetSubtargetInfo &TSI = MF->getSubtarget ();
14441460 const TargetRegisterInfo *TRI = TSI.getRegisterInfo ();
14451461 const MachineFrameInfo &MFI = MF->getFrameInfo ();
@@ -3031,6 +3047,9 @@ void CodeViewDebug::collectLexicalBlockInfo(
30313047}
30323048
30333049void CodeViewDebug::endFunctionImpl (const MachineFunction *MF) {
3050+ if (NoDebug)
3051+ return ;
3052+
30343053 const Function &GV = MF->getFunction ();
30353054 assert (FnDebugInfo.count (&GV));
30363055 assert (CurFn == FnDebugInfo[&GV].get ());
@@ -3089,6 +3108,8 @@ static bool isUsableDebugLoc(DebugLoc DL) {
30893108
30903109void CodeViewDebug::beginInstruction (const MachineInstr *MI) {
30913110 DebugHandlerBase::beginInstruction (MI);
3111+ if (NoDebug)
3112+ return ;
30923113
30933114 // Ignore DBG_VALUE and DBG_LABEL locations and function prologue.
30943115 if (!Asm || !CurFn || MI->isDebugInstr () ||
0 commit comments