From d8382ab75e1873d939e418af346af0fcd59562a7 Mon Sep 17 00:00:00 2001 From: Kuan-Wei Chiu Date: Thu, 11 Dec 2025 15:47:05 +0000 Subject: [PATCH] Adopt preserve_none attribute for MUST_TAIL optimization Despite using __attribute__((musttail)), the compiler may still emit full stack frames and spill callee-saved registers in complex handlers (e.g., do_lw), undermining the benefits of the threaded interpreter. Integrate Clang's __attribute__((preserve_none)) (available since Clang 19.1.0) into the MUST_TAIL macro. This attribute modifies the calling convention to avoid preserving registers, thereby minimizing stack setup and register pressure during instruction dispatch. This ensures more consistent tail call optimization and reduces overhead in the interpreter loop. Closes: https://github.com/sysprog21/rv32emu/issues/601 --- src/common.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/common.h b/src/common.h index bce9f0f24..33ca2f6f2 100644 --- a/src/common.h +++ b/src/common.h @@ -191,7 +191,11 @@ static inline uint8_t ilog2(uint32_t x) * even without optimizations enabled. */ #if defined(__has_attribute) && __has_attribute(musttail) +#if __has_attribute(preserve_none) +#define MUST_TAIL __attribute__((musttail)) __attribute__((preserve_none)) +#else #define MUST_TAIL __attribute__((musttail)) +#endif #else #define MUST_TAIL #endif