[llvm] [X86] Quote symbol names that collide with registers/keywords in Intel syntax (PR #186570)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 10 22:00:31 PDT 2026


https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/186570

>From c4ea209b5975a6078389a82c174ab9c43b6e6070 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sat, 14 Mar 2026 00:20:35 -0700
Subject: [PATCH 1/2] [X86] Quote symbol names that collide with
 registers/keywords in Intel syntax

When outputting Intel syntax assembly, symbol names that match register
names (e.g., `rsi`, `rax`) or keywords (`byte`, `ptr`, etc.) must be
quoted, otherwise the assembler parses them as registers/keywords
instead of symbol references.

```
% cat rsi.c
void rsi(void); void foo(void) { rsi(); }
% clang -c -c rsi.c -save-temps -masm=intel -fno-pic -o - | llvm-objdump -dr -
...
       4: ff d6                         callq   *%rsi
```
(incorrectly parsed as an indirect call)

Fix this by populating MCAsmInfo::ReservedIdentifiers with all X86
register names and Intel syntax keywords. isValidUnquotedName() checks
this set and forces quoting when a symbol name matches.
---
 llvm/include/llvm/MC/MCAsmInfo.h              |  6 ++
 llvm/lib/MC/MCAsmInfo.cpp                     |  2 +-
 .../X86/MCTargetDesc/X86MCTargetDesc.cpp      | 17 ++++
 .../test/MC/X86/intel-syntax-quote-reserved.s | 89 +++++++++++++++++++
 4 files changed, 113 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/MC/X86/intel-syntax-quote-reserved.s

diff --git a/llvm/include/llvm/MC/MCAsmInfo.h b/llvm/include/llvm/MC/MCAsmInfo.h
index 2649e080989c2..ca8e4e7d794d0 100644
--- a/llvm/include/llvm/MC/MCAsmInfo.h
+++ b/llvm/include/llvm/MC/MCAsmInfo.h
@@ -18,6 +18,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/MC/MCDirectives.h"
 #include "llvm/MC/MCTargetOptions.h"
 #include "llvm/Support/Compiler.h"
@@ -434,6 +435,11 @@ class LLVM_ABI MCAsmInfo {
   void initializeAtSpecifiers(ArrayRef<AtSpecifier>);
 
 public:
+  /// Identifiers that the assembler could misparse as registers, keywords, or
+  /// expression operators instead of symbol references. isValidUnquotedName()
+  /// checks this set to force quoting. Populated by targets.
+  StringSet<> ReservedIdentifiers;
+
   explicit MCAsmInfo();
   virtual ~MCAsmInfo();
 
diff --git a/llvm/lib/MC/MCAsmInfo.cpp b/llvm/lib/MC/MCAsmInfo.cpp
index 9f6bcd6ee9fca..e637ce3fa4e23 100644
--- a/llvm/lib/MC/MCAsmInfo.cpp
+++ b/llvm/lib/MC/MCAsmInfo.cpp
@@ -94,7 +94,7 @@ bool MCAsmInfo::isValidUnquotedName(StringRef Name) const {
       return false;
   }
 
-  return true;
+  return !ReservedIdentifiers.contains(Name.lower());
 }
 
 bool MCAsmInfo::shouldOmitSectionDirective(StringRef SectionName) const {
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp
index 0c874b7e6d674..36bf7e6b12e15 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp
@@ -464,6 +464,23 @@ static MCAsmInfo *createX86MCAsmInfo(const MCRegisterInfo &MRI,
       nullptr, MRI.getDwarfRegNum(InstPtr, true), stackGrowth);
   MAI->addInitialFrameState(Inst2);
 
+  // Populate reserved identifiers so that symbol names matching register names
+  // or Intel syntax keywords get quoted in assembly output.
+  for (unsigned i = 1, e = MRI.getNumRegs(); i < e; ++i)
+    if (const char *Name = MRI.getName(i))
+      if (Name[0])
+        MAI->ReservedIdentifiers.insert(StringRef(Name).lower());
+  // Size/type keywords.
+  for (StringRef KW : {"byte", "word", "dword", "fword", "qword", "mmword",
+                       "tbyte", "oword", "xmmword", "ymmword", "zmmword", "ptr",
+                       "offset", "flat", "near", "far", "short"})
+    MAI->ReservedIdentifiers.insert(KW);
+  // Operator keywords parsed by GAS in Intel mode (some are supported by
+  // X86AsmParser).
+  for (StringRef KW : {"and", "eq", "ge", "gt", "le", "lt", "mod", "ne", "not",
+                       "or", "shl", "shr", "xor"})
+    MAI->ReservedIdentifiers.insert(KW);
+
   return MAI;
 }
 
diff --git a/llvm/test/MC/X86/intel-syntax-quote-reserved.s b/llvm/test/MC/X86/intel-syntax-quote-reserved.s
new file mode 100644
index 0000000000000..de636fbf5539f
--- /dev/null
+++ b/llvm/test/MC/X86/intel-syntax-quote-reserved.s
@@ -0,0 +1,89 @@
+# RUN: llvm-mc -triple x86_64 -output-asm-variant=1 %s | FileCheck %s
+# RUN: llvm-mc -triple x86_64 -output-asm-variant=1 -filetype=obj %s | llvm-objdump -dr - | FileCheck %s --check-prefix=DIS
+
+## Round-trip: all calls should be direct (0xe8), not indirect (0xff).
+# DIS-NOT: callq *
+
+## Symbols whose names match registers or Intel syntax keywords must be quoted
+## in Intel syntax output. Without quoting, the assembler would misparse these
+## as registers, size specifiers, or expression operators.
+
+## Register names
+# CHECK: call "rsi"
+# CHECK: call "rax"
+# CHECK: call "EAX"
+# CHECK: call "Ah"
+callq rsi
+callq rax
+callq EAX
+callq Ah
+
+## Size/type keywords (gas treats these as size constants: byte=1, word=2, etc.)
+# CHECK: call "byte"
+# CHECK: call "word"
+# CHECK: call "dword"
+# CHECK: call "fword"
+# CHECK: call "qword"
+# CHECK: call "mmword"
+# CHECK: call "tbyte"
+# CHECK: call "oword"
+# CHECK: call "xmmword"
+# CHECK: call "ymmword"
+# CHECK: call "zmmword"
+callq byte
+callq word
+callq dword
+callq fword
+callq qword
+callq mmword
+callq tbyte
+callq oword
+callq xmmword
+callq ymmword
+callq zmmword
+
+## Other keywords
+# CHECK: call "ptr"
+# CHECK: call "offset"
+# CHECK: call "flat"
+# CHECK: call "near"
+# CHECK: call "far"
+# CHECK: call "short"
+callq ptr
+callq offset
+callq flat
+callq near
+callq far
+callq short
+
+## Expression operator keywords
+# CHECK: call "and"
+# CHECK: call "eq"
+# CHECK: call "ge"
+# CHECK: call "gt"
+# CHECK: call "le"
+# CHECK: call "lt"
+# CHECK: call "mod"
+# CHECK: call "ne"
+# CHECK: call "not"
+# CHECK: call "or"
+# CHECK: call "shl"
+# CHECK: call "shr"
+# CHECK: call "xor"
+callq and
+callq eq
+callq ge
+callq gt
+callq le
+callq lt
+callq mod
+callq ne
+callq not
+callq or
+callq shl
+callq shr
+callq xor
+
+## Normal symbols remain unquoted.
+# CHECK: call bar
+callq bar

>From cf7fefd28d98349aa610898bc83484269d295cfc Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sat, 14 Mar 2026 01:01:18 -0700
Subject: [PATCH 2/2] comment

---
 llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp
index 36bf7e6b12e15..b9f1dc692430c 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp
@@ -475,8 +475,7 @@ static MCAsmInfo *createX86MCAsmInfo(const MCRegisterInfo &MRI,
                        "tbyte", "oword", "xmmword", "ymmword", "zmmword", "ptr",
                        "offset", "flat", "near", "far", "short"})
     MAI->ReservedIdentifiers.insert(KW);
-  // Operator keywords parsed by GAS in Intel mode (some are supported by
-  // X86AsmParser).
+  // Operator keywords parsed by GAS/X86AsmParser in Intel mode.
   for (StringRef KW : {"and", "eq", "ge", "gt", "le", "lt", "mod", "ne", "not",
                        "or", "shl", "shr", "xor"})
     MAI->ReservedIdentifiers.insert(KW);



More information about the llvm-commits mailing list