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

via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 3 05:56:01 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-x86

Author: Mathew Joseph (mathew1046)

<details>
<summary>Changes</summary>

## Summary

This prevents an assertion during DWARF finalization after an inline assembly error.

## Reproducer

```llvm
define void @<!-- -->f() {
  call void asm sideeffect ".file 3 \22b\22", ""()
  ret void
}
```

The inline assembly contains:

```asm
.file 3 "b"
```

without defining file numbers 1 and 2 first.

With an assertions-enabled LLVM build, `llc -filetype=obj` reports the expected `unassigned file number` diagnostics and then aborts with:

```text
Assertion `!MCDwarfFiles[i].Name.empty()` failed.
```

The failure occurs because inline assembly is parsed with `NoFinalize=true`. `AsmParser::Run()` reports the error through its local error state, but the error was not propagated to `MCContext`. Later, `MCStreamer::finish()` continued into `MCObjectStreamer::finishImpl()`, which attempted to emit a DWARF line table containing empty file-table entries.

## Fix

Propagate deferred inline assembly errors to `MCContext` and stop streamer finalization when the context has an error. This preserves the existing `unassigned file number` diagnostics and prevents incomplete DWARF emission.

## Testing

Added `llvm/test/CodeGen/X86/inline-asm-file-directive-error.ll`.

Validated with an assertions-enabled LLVM build:

```bash
ninja -C /home/mathew/llvm-project-build-assertions llc
/home/mathew/llvm-project-build-assertions/bin/llc -mtriple=x86_64-unknown-linux-gnu -filetype=obj llvm/test/CodeGen/X86/inline-asm-file-directive-error.ll -o /dev/null
```

The test now reports the original errors and exits nonzero without asserting. The same behavior was also checked with `-dwarf-version=5`, and valid inline assembly continues to compile successfully.

Fixes #<!-- -->228061

---
Full diff: https://github.com/llvm/llvm-project/pull/228707.diff


4 Files Affected:

- (modified) llvm/include/llvm/MC/MCContext.h (+1) 
- (modified) llvm/lib/MC/MCParser/AsmParser.cpp (+3) 
- (modified) llvm/lib/MC/MCStreamer.cpp (+3) 
- (added) llvm/test/CodeGen/X86/inline-asm-file-directive-error.ll (+11) 


``````````diff
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/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/lib/MC/MCStreamer.cpp b/llvm/lib/MC/MCStreamer.cpp
index 1d51fbc46a43c4..e4f77bc484edb8 100644
--- a/llvm/lib/MC/MCStreamer.cpp
+++ b/llvm/lib/MC/MCStreamer.cpp
@@ -1299,6 +1299,9 @@ void MCStreamer::emitWindowsUnwindTables() {}
 void MCStreamer::emitWindowsUnwindTables(WinEH::FrameInfo *Frame) {}
 
 void MCStreamer::finish(SMLoc EndLoc) {
+  if (getContext().hadError())
+    return;
+
   if (hasUnfinishedDwarfFrameInfo() ||
       (!WinFrameInfos.empty() && !WinFrameInfos.back()->End)) {
     getContext().reportError(EndLoc, "Unfinished frame!");
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..ed1d5b26950873
--- /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
\ No newline at end of file

``````````

</details>


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


More information about the llvm-commits mailing list