[clang] [llvm] [X86][AsmParser] Fix MS inline asm label mismatch with offset operator (PR #199552)
via cfe-commits
cfe-commits at lists.llvm.org
Mon May 25 09:41:55 PDT 2026
https://github.com/sonyps5201314 created https://github.com/llvm/llvm-project/pull/199552
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.
for fix https://github.com/llvm/llvm-project/issues/132863
>From dce9fef3f39f3de1ef9b9b27e4270c5bf080d678 Mon Sep 17 00:00:00 2001
From: sonyps5201314 <sonyps5201314 at gmail.com>
Date: Tue, 26 May 2026 00:31:02 +0800
Subject: [PATCH] [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.
---
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.
More information about the cfe-commits
mailing list