Skip to content

[X86] Asm modifier %a: add (%rip) for 64-bit static relocation model #139040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

MaskRay
Copy link
Member

@MaskRay MaskRay commented May 8, 2025

In GCC,

static int a;
int foo() {
  asm("# %a0" : : "i"(&a));
}

lowers to # a(%rip) regardless of the PIC mode. This PR follow suits
for ELF -fno-pic, matching ELF -fpic (asm-modifier-pic.ll) and Mach-O
(which defaults to PIC).

Close #139001

Created using spr 1.3.5-bogner
@llvmbot
Copy link
Member

llvmbot commented May 8, 2025

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-llvm-ir

Author: Fangrui Song (MaskRay)

Changes

In GCC,

static int a;
asm("# %0" : : "i"(&a));

lowers to # a(%rip) regardless of the PIC mode. This PR follow suits
for ELF -fno-pic, matching ELF -fpic (asm-modifier-pic.ll) and Mach-O
(which defaults to PIC).

Close #139001


Full diff: https://github.com/llvm/llvm-project/pull/139040.diff

3 Files Affected:

  • (modified) llvm/docs/LangRef.rst (+3)
  • (modified) llvm/lib/Target/X86/X86AsmPrinter.cpp (+1-1)
  • (modified) llvm/test/CodeGen/X86/asm-modifier.ll (+13-6)
diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index 568843a4486e5..7296bb84b7d95 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -5776,6 +5776,7 @@ and GCC likely indicates a bug in LLVM.
 
 Target-independent:
 
+- ``a``: Print a memory reference. Targets might customize the output.
 - ``c``: Print an immediate integer constant unadorned, without
   the target-specific immediate punctuation (e.g. no ``$`` prefix).
 - ``n``: Negate and print immediate integer constant unadorned, without the
@@ -5913,6 +5914,8 @@ target-independent modifiers.
 
 X86:
 
+- ``a``: Print a memory reference. This displays as ``sym(%rip)`` for x86-64.
+  i386 should only use this with the static relocation model.
 - ``c``: Print an unadorned integer or symbol name. (The latter is
   target-specific behavior for this typically target-independent modifier).
 - ``A``: Print a register name with a '``*``' before it.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 5f5bfc70e8a1a..754f3f017fd29 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -744,7 +744,7 @@ bool X86AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
         llvm_unreachable("unexpected operand type!");
       case MachineOperand::MO_GlobalAddress:
         PrintSymbolOperand(MO, O);
-        if (Subtarget->isPICStyleRIPRel())
+        if (Subtarget->is64Bit())
           O << "(%rip)";
         return false;
       case MachineOperand::MO_Register:
diff --git a/llvm/test/CodeGen/X86/asm-modifier.ll b/llvm/test/CodeGen/X86/asm-modifier.ll
index e49e7d6b01964..7fa1e34a288da 100644
--- a/llvm/test/CodeGen/X86/asm-modifier.ll
+++ b/llvm/test/CodeGen/X86/asm-modifier.ll
@@ -6,12 +6,19 @@
 @var = internal global i32 0, align 4
 
 define dso_local void @test_a() nounwind {
-; CHECK-LABEL: test_a:
-; CHECK:       # %bb.0:
-; CHECK-NEXT:    #APP
-; CHECK-NEXT:    #TEST 42 var#
-; CHECK-NEXT:    #NO_APP
-; CHECK-NEXT:    ret{{[l|q]}}
+; X86-LABEL: test_a:
+; X86:       # %bb.0:
+; X86-NEXT:    #APP
+; X86-NEXT:    #TEST 42 var#
+; X86-NEXT:    #NO_APP
+; X86-NEXT:    retl
+;
+; X64-LABEL: test_a:
+; X64:       # %bb.0:
+; X64-NEXT:    #APP
+; X64-NEXT:    #TEST 42 var(%rip)#
+; X64-NEXT:    #NO_APP
+; X64-NEXT:    retq
   tail call void asm sideeffect "#TEST ${0:a} ${1:a}#", "i,i"(i32 42, ptr @var)
   ret void
 }

Copy link
Contributor

@phoebewang phoebewang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@KanRobert
Copy link
Contributor

In GCC,

static int a;
asm("# %0" : : "i"(&a));

lowers to # a(%rip) regardless of the PIC mode. This PR follow suits for ELF -fno-pic, matching ELF -fpic (asm-modifier-pic.ll) and Mach-O (which defaults to PIC).

Close #139001

Maybe we can use a better example in the description? This example does not use "a" modifier.

Copy link
Contributor

@KanRobert KanRobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one suggestion

@MaskRay
Copy link
Member Author

MaskRay commented May 9, 2025

In GCC,

static int a;
asm("# %0" : : "i"(&a));

lowers to # a(%rip) regardless of the PIC mode. This PR follow suits for ELF -fno-pic, matching ELF -fpic (asm-modifier-pic.ll) and Mach-O (which defaults to PIC).
Close #139001

Maybe we can use a better example in the description? This example does not use "a" modifier.

Updated!

@MaskRay MaskRay merged commit d926ec3 into main May 9, 2025
13 of 15 checks passed
@MaskRay MaskRay deleted the users/MaskRay/spr/x86-asm-modifier-a-add-rip-for-64-bit-static-relocation-model branch May 9, 2025 03:54
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request May 9, 2025
…tion model

In GCC,

```
static int a;
int foo() {
  asm("# %a0" : : "i"(&a));
}
```

lowers to `# a(%rip)` regardless of the PIC mode. This PR follow suits
for ELF -fno-pic, matching ELF -fpic (asm-modifier-pic.ll) and Mach-O
(which defaults to PIC).

Close llvm/llvm-project#139001

Pull Request: llvm/llvm-project#139040
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

x86: inline asm: make %a compatible with gcc
4 participants