[llvm] [MC] Increase asm-macro-max-nesting-depth default to 100 (PR #218455)

Matt Turner via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 24 22:14:58 PDT 2026


https://github.com/mattst88 updated https://github.com/llvm/llvm-project/pull/218455

>From e2741e816ec054b6d0fc2594525d41d90a3ab01c Mon Sep 17 00:00:00 2001
From: Matt Turner <mattst88 at gmail.com>
Date: Thu, 9 Jul 2026 22:00:52 -0400
Subject: [PATCH] [MC] Increase asm-macro-max-nesting-depth default to 100

The default of 20 rejects assembly GNU as accepts.  glibc's
sysdeps/unix/sysv/linux/alpha/rt_sigaction.S is one example:
SIGCONTEXT_REGS_I recurses once per register to emit a cfi_offset for
the whole struct sigcontext, and so nests 31 deep.

Raise the default to 100, the value of max_macro_nest in GNU as
(gas/as.c), matching it at the boundary too -- gas tests macro_nest >
max_macro_nest, so both assemblers now accept 100 levels and reject 101.
The limit still earns its keep: it turns runaway recursion into a
diagnostic rather than a stack overflow.

macro-max-depth.s covered only the explicit
-asm-macro-max-nesting-depth, at a fixed 41-deep input.  Give it a depth
parameter so one input covers both the new default (30 and 99 assemble,
100 does not) and the flag overriding it.

Assisted-by: Claude Code
---
 llvm/lib/MC/MCParser/MCAsmParser.cpp     |  5 ++-
 llvm/test/MC/AsmParser/macro-max-depth.s | 42 ++++++++++++++++--------
 2 files changed, 33 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/MC/MCParser/MCAsmParser.cpp b/llvm/lib/MC/MCParser/MCAsmParser.cpp
index 68bd0e4453d04..bbfb9bde178ce 100644
--- a/llvm/lib/MC/MCParser/MCAsmParser.cpp
+++ b/llvm/lib/MC/MCParser/MCAsmParser.cpp
@@ -23,8 +23,11 @@
 using namespace llvm;
 
 namespace llvm {
+// Matches max_macro_nest in GNU as (gas/as.c), so that the two assemblers
+// agree on which input is legal.  The previous 20 rejected assembly gas
+// accepts; see the test for the shape that hits it.
 cl::opt<unsigned> AsmMacroMaxNestingDepth(
-    "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden,
+    "asm-macro-max-nesting-depth", cl::init(100), cl::Hidden,
     cl::desc("The maximum nesting depth allowed for assembly macros."));
 }
 
diff --git a/llvm/test/MC/AsmParser/macro-max-depth.s b/llvm/test/MC/AsmParser/macro-max-depth.s
index 47fbf9a1930ba..4e2b28a58eecb 100644
--- a/llvm/test/MC/AsmParser/macro-max-depth.s
+++ b/llvm/test/MC/AsmParser/macro-max-depth.s
@@ -1,20 +1,36 @@
-// RUN: llvm-mc -triple x86_64-unknown-unknown -asm-macro-max-nesting-depth=42 %s | FileCheck %s -check-prefix=CHECK_PASS
-// RUN: not llvm-mc -triple x86_64-unknown-unknown %s 2> %t
-// RUN: FileCheck -check-prefix=CHECK_FAIL < %t %s
+/// The default nesting limit is pinned to max_macro_nest in GNU as (gas/as.c),
+/// including the boundary: gas tests macro_nest > max_macro_nest, so 100 levels
+/// assemble and 101 do not.
+///
+/// The limit has to clear what hand-written assembly actually reaches.  The
+/// shape below is glibc's, from sysdeps/unix/sysv/linux/alpha/rt_sigaction.S,
+/// where SIGCONTEXT_REGS_I recurses once per register to emit a cfi_offset and
+/// so nests 31 deep -- well over the 20 this used to default to.
 
-.macro rec head, tail:vararg
- .ifnb \tail
- rec \tail
+// RUN: llvm-mc -triple x86_64 -defsym DEPTH=30 %s \
+// RUN:   | FileCheck %s --check-prefix=PASS
+// RUN: llvm-mc -triple x86_64 -defsym DEPTH=99 %s \
+// RUN:   | FileCheck %s --check-prefix=PASS
+// RUN: not llvm-mc -triple x86_64 -defsym DEPTH=100 %s 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=TOODEEP-DEFAULT
+
+/// -asm-macro-max-nesting-depth still overrides the default.
+// RUN: llvm-mc -triple x86_64 -asm-macro-max-nesting-depth=42 \
+// RUN:   -defsym DEPTH=41 %s | FileCheck %s --check-prefix=PASS
+// RUN: not llvm-mc -triple x86_64 -asm-macro-max-nesting-depth=42 \
+// RUN:   -defsym DEPTH=42 %s 2>&1 | FileCheck %s --check-prefix=TOODEEP-FLAG
+
+.macro rec n
+ .if \n > 0
+ rec "(\n - 1)"
  .else
  .long 42
  .endif
 .endm
 
-.macro amplify macro, args:vararg
- \macro  \args \args \args \args
-.endm
-
-amplify rec 0 0 0 0 0 0 0 0 0 0
+rec DEPTH
 
-// CHECK_PASS: .long 42
-// CHECK_FAIL: error: macros cannot be nested more than {{[0-9]+}} levels deep. Use -asm-macro-max-nesting-depth to increase this limit.
+/// DEPTH=n nests n+1 deep, the outermost invocation included.
+// PASS: .long 42
+// TOODEEP-DEFAULT: error: macros cannot be nested more than 100 levels deep. Use -asm-macro-max-nesting-depth to increase this limit.
+// TOODEEP-FLAG: error: macros cannot be nested more than 42 levels deep. Use -asm-macro-max-nesting-depth to increase this limit.



More information about the llvm-commits mailing list