[llvm] [MC] Avoid DWARF emission after inline asm errors (PR #228707)

Mathew Joseph via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 3 08:10:44 PDT 2026


https://github.com/mathew1046 updated https://github.com/llvm/llvm-project/pull/228707

>From d173e3b1a056f787148a7d79a35b728c47b78f0e Mon Sep 17 00:00:00 2001
From: Mathew Joseph <mathewjosephparakka at gmail.com>
Date: Sat, 3 Oct 2026 18:18:52 +0530
Subject: [PATCH] [MC] Avoid DWARF emission after inline asm errors

Propagate deferred inline assembly errors to the MC context so streamer finalization does not emit incomplete DWARF line tables. This preserves the existing diagnostics for unassigned .file entries and prevents assertions in the DWARF file table emitter.

Fixes #228061
---
 llvm/include/llvm/MC/MCContext.h                      |  1 +
 llvm/lib/MC/MCObjectStreamer.cpp                      |  3 +++
 llvm/lib/MC/MCParser/AsmParser.cpp                    |  3 +++
 .../CodeGen/X86/inline-asm-file-directive-error.ll    | 11 +++++++++++
 4 files changed, 18 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/inline-asm-file-directive-error.ll

diff --git a/llvm/include/llvm/MC/MCContext.h b/llvm/include/llvm/MC/MCContext.h
index 7e76abe08f56f7..c392b82104e72a 100644
--- a/llvm/include/llvm/MC/MCContext.h
+++ b/llvm/include/llvm/MC/MCContext.h
@@ -838,6 +838,7 @@ class MCContext {
   }
 
   bool hadError() { return HadError; }
+  void setHadError() { HadError = true; }
   LLVM_ABI void diagnose(const SMDiagnostic &SMD);
   LLVM_ABI void reportError(SMLoc L, const Twine &Msg);
   LLVM_ABI void reportWarning(SMLoc L, const Twine &Msg);
diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp
index 7a64fbbd667129..da6b3ad617f916 100644
--- a/llvm/lib/MC/MCObjectStreamer.cpp
+++ b/llvm/lib/MC/MCObjectStreamer.cpp
@@ -801,6 +801,9 @@ void MCObjectStreamer::emitAddrsigSym(const MCSymbol *Sym) {
 }
 
 void MCObjectStreamer::finishImpl() {
+  if (getContext().hadError())
+    return;
+
   getContext().RemapDebugPaths();
 
   // If we are generating dwarf for assembly source files dump out the sections.
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index 588cc8e3ffde3f..f067e4d81761fe 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -1073,6 +1073,9 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
     Out.finish(Lexer.getLoc());
   }
 
+  if (HadError && NoFinalize)
+    getContext().setHadError();
+
   return HadError || getContext().hadError();
 }
 
diff --git a/llvm/test/CodeGen/X86/inline-asm-file-directive-error.ll b/llvm/test/CodeGen/X86/inline-asm-file-directive-error.ll
new file mode 100644
index 00000000000000..09283c2c3e4757
--- /dev/null
+++ b/llvm/test/CodeGen/X86/inline-asm-file-directive-error.ll
@@ -0,0 +1,11 @@
+; RUN: not llc -mtriple=x86_64-unknown-linux-gnu -filetype=obj %s -o %t.o 2>&1 | FileCheck %s
+
+; An inline assembly error must prevent deferred DWARF finalization.
+define void @f() {
+  call void asm sideeffect ".file 3 \22b\22", ""()
+  ret void
+}
+
+; CHECK: error: unassigned file number: 1 for .file directives
+; CHECK: error: unassigned file number: 2 for .file directives
+; CHECK-NOT: Assertion



More information about the llvm-commits mailing list