[clang] [llvm] [X86][AsmParser] Fix MS inline asm label mismatch with offset operator (PR #199552)
via cfe-commits
cfe-commits at lists.llvm.org
Tue May 26 03:16:06 PDT 2026
https://github.com/sonyps5201314 updated https://github.com/llvm/llvm-project/pull/199552
>From 767fb0f39213c9fa908699484ca9665583a33e92 Mon Sep 17 00:00:00 2001
From: sonyps5201314 <sonyps5201314 at gmail.com>
Date: Tue, 26 May 2026 00:55:21 +0800
Subject: [PATCH 1/2] [X86][AsmParser] Fix MS inline asm label mismatch with
offset operator
When using the 'offset' operator to reference an inline asm label in
MSVC-style inline assembly, the label reference was missing the
PrivateLabelPrefix (e.g., 'L' on COFF targets). This caused a symbol
name mismatch between the label definition and its reference:
- Label definition (via AOK_Label rewrite): L__MSASMLABEL_.0__name
- Offset reference (no rewrite): __MSASMLABEL_.0__name
This mismatch resulted in an 'undefined symbol' linker error when
using lld-link on Windows.
The fix prepends PrivateLabelPrefix to the internal label name when
parsing the offset operator, matching what AOK_Label does for label
definitions and non-offset references.
Fixes https://github.com/llvm/llvm-project/issues/132863
Assisted by: Claude Opus 4.6
---
clang/test/CodeGen/ms-inline-asm.c | 14 ++++++++++++++
llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp | 10 ++++++++--
2 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/clang/test/CodeGen/ms-inline-asm.c b/clang/test/CodeGen/ms-inline-asm.c
index c3eef9a23e166..2cf3cb3ce74a2 100644
--- a/clang/test/CodeGen/ms-inline-asm.c
+++ b/clang/test/CodeGen/ms-inline-asm.c
@@ -789,6 +789,20 @@ int test_indirect_field(LARGE_INTEGER LargeInteger) {
// CHECK-LABEL: define{{.*}} i32 @test_indirect_field(
// CHECK: call i32 asm sideeffect inteldialect "mov eax, $1",
+// Test that 'offset' references to inline asm labels use the same
+// PrivateLabelPrefix as the label definition.
+void label_offset(void) {
+ __asm {
+ label_offset_target:
+ mov eax, offset label_offset_target
+ }
+ // CHECK-LABEL: define{{.*}} void @label_offset(
+ // CHECK: call void asm sideeffect inteldialect
+ // CHECK-SAME: {{.*}}__MSASMLABEL_.${:uid}__label_offset_target:
+ // CHECK-SAME: mov eax, offset {{.*}}__MSASMLABEL_.${:uid}__label_offset_target
+ // CHECK-SAME: "~{eax},~{dirflag},~{fpsr},~{flags}"()
+}
+
// MS ASM containing labels must not be duplicated (PR23715).
// CHECK: attributes [[ATTR1]] = {
// CHECK-NOT: noduplicate
diff --git a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
index 1741efac3a8c7..942ee6bb07d28 100644
--- a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
+++ b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
@@ -2317,8 +2317,14 @@ bool X86AsmParser::ParseIntelInlineAsmIdentifier(
if (!IsParsingOffsetOperator)
InstInfo->AsmRewrites->emplace_back(AOK_Label, Loc, Identifier.size(),
InternalName);
- else
- Identifier = InternalName;
+ else {
+ // When parsing the offset operator, we need to prepend
+ // PrivateLabelPrefix to match the AOK_Label rewrite at label definition.
+ StringRef Prefix = getContext().getAsmInfo().getPrivateLabelPrefix();
+ SmallString<128> PrefixedName;
+ (Twine(Prefix) + InternalName).toVector(PrefixedName);
+ Identifier = getContext().allocateString(PrefixedName);
+ }
} else if (Info.isKind(InlineAsmIdentifierInfo::IK_EnumVal))
return false;
// Create the symbol reference.
>From cf82a818cf5fcf816e2e2d853ee0aa56beb703f5 Mon Sep 17 00:00:00 2001
From: sonyps5201314 <sonyps5201314 at gmail.com>
Date: Tue, 26 May 2026 17:59:40 +0800
Subject: [PATCH 2/2] [X86][CodeGen] Add IR test for MS inline asm label offset
reference
Add an LLVM IR-level test in llvm/test/CodeGen/X86/ that verifies
the label definition and offset reference use matching names with
the PrivateLabelPrefix ('L' on COFF) after expansion.
---
.../CodeGen/X86/ms-inline-asm-label-offset.ll | 32 +++++++++++++++++++
1 file changed, 32 insertions(+)
create mode 100644 llvm/test/CodeGen/X86/ms-inline-asm-label-offset.ll
diff --git a/llvm/test/CodeGen/X86/ms-inline-asm-label-offset.ll b/llvm/test/CodeGen/X86/ms-inline-asm-label-offset.ll
new file mode 100644
index 0000000000000..24659d6116e87
--- /dev/null
+++ b/llvm/test/CodeGen/X86/ms-inline-asm-label-offset.ll
@@ -0,0 +1,32 @@
+; RUN: llc < %s -mtriple=i386-pc-windows-msvc | FileCheck %s
+
+; Verify that the 'offset' reference to an inline asm label uses the same
+; PrivateLabelPrefix as the label definition. On Windows COFF the prefix is "L".
+;
+; Previously, the label definition got the "L" prefix via AOK_Label rewrite,
+; but the offset reference skipped the rewrite and used the internal name
+; without the prefix, causing an undefined symbol error at link time.
+;
+; C source:
+; int main() {
+; unsigned dwpXIP;
+; __asm {
+; XIP_label:
+; mov dwpXIP, offset XIP_label
+; }
+; return 0;
+; }
+
+define dso_local i32 @label_offset() {
+entry:
+ %result = alloca i32, align 4
+ call void asm sideeffect inteldialect "L__MSASMLABEL_.${:uid}__XIP_label:\0A\09mov dword ptr $0, offset L__MSASMLABEL_.${:uid}__XIP_label", "=*m,~{dirflag},~{fpsr},~{flags}"(ptr elementtype(i32) %result)
+ %0 = load i32, ptr %result, align 4
+ ret i32 %0
+}
+
+; The key check: both the label definition and offset reference must use
+; the same "L__MSASMLABEL_.0__XIP_label" name with the "L" prefix.
+; CHECK-LABEL: _label_offset:
+; CHECK: L__MSASMLABEL_.0__XIP_label:
+; CHECK-NEXT: mov{{.*}}L__MSASMLABEL_.0__XIP_label
More information about the cfe-commits
mailing list