[llvm] [MC] Flush pending errors when layout stops early due to relaxOnce error (PR #226028)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 23 23:07:07 PDT 2026


https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/226028

When relaxOnce returns nonzero after an error has been reported,
layout() returns without flushing the errors recorded by the last
relaxation pass, and Finish() fails `assert(PendingErrors.empty())`.

Fixes #225036

>From ec595ae1aab048a3be265ba702bd3d7883e66932 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Wed, 23 Sep 2026 22:37:16 -0700
Subject: [PATCH] [MC] Flush pending errors when layout stops early due to
 relaxOnce error

When relaxOnce returns nonzero after an error has been reported,
layout() returns without flushing the errors recorded by the last
relaxation pass, and Finish() fails `assert(PendingErrors.empty())`.

Fixes #225036
---
 llvm/lib/MC/MCAssembler.cpp   | 7 +++++--
 llvm/test/MC/ELF/leb128-err.s | 5 +++++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp
index fcdcdbc96426f..db4aa48138e04 100644
--- a/llvm/lib/MC/MCAssembler.cpp
+++ b/llvm/lib/MC/MCAssembler.cpp
@@ -701,9 +701,12 @@ void MCAssembler::layout() {
   for (MCSection &Sec : *this)
     layoutSection(Sec);
   unsigned FirstStable = Sections.size();
-  while ((FirstStable = relaxOnce(FirstStable)) > 0)
-    if (getContext().hadError())
+  while ((FirstStable = relaxOnce(FirstStable)) > 0) {
+    if (getContext().hadError()) {
+      flushPendingErrors();
       return;
+    }
+  }
 
   // Some targets might want to adjust fragment offsets. If so, perform another
   // layout iteration.
diff --git a/llvm/test/MC/ELF/leb128-err.s b/llvm/test/MC/ELF/leb128-err.s
index e6a7329372a95..41200c2f864f5 100644
--- a/llvm/test/MC/ELF/leb128-err.s
+++ b/llvm/test/MC/ELF/leb128-err.s
@@ -23,3 +23,8 @@ w1:
 .section .nonalloc_y; ny:
 # CHECK: :[[#@LINE+1]]:12: error: .sleb128 expression is not absolute
 .sleb128 nx-ny
+
+## We also report errors from non-LEB128 fragments.
+.data
+# CHECK: :[[#@LINE+1]]:8: error: expected assembly-time absolute expression
+.space extern



More information about the llvm-commits mailing list