[llvm] 68c976b - [X86] Fix referencing local tagged globals

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 17 14:07:53 PST 2023


Author: Arthur Eubanks
Date: 2023-12-17T13:49:50-08:00
New Revision: 68c976bf64f50fe9c16a335378a964c166851962

URL: https://github.com/llvm/llvm-project/commit/68c976bf64f50fe9c16a335378a964c166851962
DIFF: https://github.com/llvm/llvm-project/commit/68c976bf64f50fe9c16a335378a964c166851962.diff

LOG: [X86] Fix referencing local tagged globals

We should treat the medium code model like the small code model.
Classifying non-local references already properly handled this.

Added: 
    

Modified: 
    llvm/lib/Target/X86/X86Subtarget.cpp
    llvm/test/CodeGen/X86/tagged-globals-pic.ll
    llvm/test/CodeGen/X86/tagged-globals-static.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/X86/X86Subtarget.cpp b/llvm/lib/Target/X86/X86Subtarget.cpp
index d63f1ca1695b20..07f535685e8f97 100644
--- a/llvm/lib/Target/X86/X86Subtarget.cpp
+++ b/llvm/lib/Target/X86/X86Subtarget.cpp
@@ -69,11 +69,11 @@ X86Subtarget::classifyGlobalReference(const GlobalValue *GV) const {
 
 unsigned char
 X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
+  CodeModel::Model CM = TM.getCodeModel();
   // Tagged globals have non-zero upper bits, which makes direct references
-  // require a 64-bit immediate.  On the small code model this causes relocation
-  // errors, so we go through the GOT instead.
-  if (AllowTaggedGlobals && TM.getCodeModel() == CodeModel::Small && GV &&
-      !isa<Function>(GV))
+  // require a 64-bit immediate. With the small/medium code models this causes
+  // relocation errors, so we go through the GOT instead.
+  if (AllowTaggedGlobals && CM != CodeModel::Large && GV && !isa<Function>(GV))
     return X86II::MO_GOTPCREL_NORELAX;
 
   // If we're not PIC, it's not very interesting.
@@ -83,7 +83,6 @@ X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
   if (is64Bit()) {
     // 64-bit ELF PIC local references may use GOTOFF relocations.
     if (isTargetELF()) {
-      CodeModel::Model CM = TM.getCodeModel();
       assert(CM != CodeModel::Tiny &&
              "Tiny codesize model not supported on X86");
       // In the large code model, all text is far from any global data, so we

diff  --git a/llvm/test/CodeGen/X86/tagged-globals-pic.ll b/llvm/test/CodeGen/X86/tagged-globals-pic.ll
index 4f85b5ed99695d..156487ee163a2a 100644
--- a/llvm/test/CodeGen/X86/tagged-globals-pic.ll
+++ b/llvm/test/CodeGen/X86/tagged-globals-pic.ll
@@ -1,5 +1,7 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
-; RUN: llc --relocation-model=pic < %s | FileCheck %s
+; RUN: llc --relocation-model=pic -code-model=small < %s | FileCheck %s
+; RUN: llc --relocation-model=pic -code-model=medium < %s | FileCheck %s
+; RUN: llc --relocation-model=pic -code-model=large < %s | FileCheck %s --check-prefix=LARGE
 
 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"
@@ -12,6 +14,16 @@ define ptr @global_addr() #0 {
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    movq global at GOTPCREL_NORELAX(%rip), %rax
 ; CHECK-NEXT:    retq
+;
+; LARGE-LABEL: global_addr:
+; LARGE:       # %bb.0:
+; LARGE-NEXT:  .L0$pb:
+; LARGE-NEXT:    leaq .L0$pb(%rip), %rax
+; LARGE-NEXT:    movabsq $_GLOBAL_OFFSET_TABLE_-.L0$pb, %rcx
+; LARGE-NEXT:    addq %rax, %rcx
+; LARGE-NEXT:    movabsq $global at GOT, %rax
+; LARGE-NEXT:    movq (%rcx,%rax), %rax
+; LARGE-NEXT:    retq
   ret ptr @global
 }
 
@@ -21,6 +33,17 @@ define i32 @global_load() #0 {
 ; CHECK-NEXT:    movq global at GOTPCREL_NORELAX(%rip), %rax
 ; CHECK-NEXT:    movl (%rax), %eax
 ; CHECK-NEXT:    retq
+;
+; LARGE-LABEL: global_load:
+; LARGE:       # %bb.0:
+; LARGE-NEXT:  .L1$pb:
+; LARGE-NEXT:    leaq .L1$pb(%rip), %rax
+; LARGE-NEXT:    movabsq $_GLOBAL_OFFSET_TABLE_-.L1$pb, %rcx
+; LARGE-NEXT:    addq %rax, %rcx
+; LARGE-NEXT:    movabsq $global at GOT, %rax
+; LARGE-NEXT:    movq (%rcx,%rax), %rax
+; LARGE-NEXT:    movl (%rax), %eax
+; LARGE-NEXT:    retq
   %load = load i32, ptr @global
   ret i32 %load
 }
@@ -31,6 +54,17 @@ define void @global_store() #0 {
 ; CHECK-NEXT:    movq global at GOTPCREL_NORELAX(%rip), %rax
 ; CHECK-NEXT:    movl $0, (%rax)
 ; CHECK-NEXT:    retq
+;
+; LARGE-LABEL: global_store:
+; LARGE:       # %bb.0:
+; LARGE-NEXT:  .L2$pb:
+; LARGE-NEXT:    leaq .L2$pb(%rip), %rax
+; LARGE-NEXT:    movabsq $_GLOBAL_OFFSET_TABLE_-.L2$pb, %rcx
+; LARGE-NEXT:    addq %rax, %rcx
+; LARGE-NEXT:    movabsq $global at GOT, %rax
+; LARGE-NEXT:    movq (%rcx,%rax), %rax
+; LARGE-NEXT:    movl $0, (%rax)
+; LARGE-NEXT:    retq
   store i32 0, ptr @global
   ret void
 }
@@ -40,6 +74,16 @@ define ptr @func_addr() #0 {
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    movq func at GOTPCREL(%rip), %rax
 ; CHECK-NEXT:    retq
+;
+; LARGE-LABEL: func_addr:
+; LARGE:       # %bb.0:
+; LARGE-NEXT:  .L3$pb:
+; LARGE-NEXT:    leaq .L3$pb(%rip), %rax
+; LARGE-NEXT:    movabsq $_GLOBAL_OFFSET_TABLE_-.L3$pb, %rcx
+; LARGE-NEXT:    addq %rax, %rcx
+; LARGE-NEXT:    movabsq $func at GOT, %rax
+; LARGE-NEXT:    movq (%rcx,%rax), %rax
+; LARGE-NEXT:    retq
   ret ptr @func
 }
 

diff  --git a/llvm/test/CodeGen/X86/tagged-globals-static.ll b/llvm/test/CodeGen/X86/tagged-globals-static.ll
index bddbaa5592da58..0eb21267b06e03 100644
--- a/llvm/test/CodeGen/X86/tagged-globals-static.ll
+++ b/llvm/test/CodeGen/X86/tagged-globals-static.ll
@@ -1,5 +1,7 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
-; RUN: llc --relocation-model=static < %s | FileCheck %s
+; RUN: llc --relocation-model=static -code-model=small < %s | FileCheck %s
+; RUN: llc --relocation-model=static -code-model=medium < %s | FileCheck %s
+; RUN: llc --relocation-model=static -code-model=large < %s | FileCheck %s --check-prefix=LARGE
 
 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
 target triple = "x86_64-unknown-linux-gnu"
@@ -12,6 +14,11 @@ define ptr @global_addr() #0 {
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    movq global at GOTPCREL_NORELAX(%rip), %rax
 ; CHECK-NEXT:    retq
+;
+; LARGE-LABEL: global_addr:
+; LARGE:       # %bb.0:
+; LARGE-NEXT:    movabsq $global, %rax
+; LARGE-NEXT:    retq
   ret ptr @global
 }
 
@@ -21,6 +28,12 @@ define i32 @global_load() #0 {
 ; CHECK-NEXT:    movq global at GOTPCREL_NORELAX(%rip), %rax
 ; CHECK-NEXT:    movl (%rax), %eax
 ; CHECK-NEXT:    retq
+;
+; LARGE-LABEL: global_load:
+; LARGE:       # %bb.0:
+; LARGE-NEXT:    movabsq $global, %rax
+; LARGE-NEXT:    movl (%rax), %eax
+; LARGE-NEXT:    retq
   %load = load i32, ptr @global
   ret i32 %load
 }
@@ -31,6 +44,12 @@ define void @global_store() #0 {
 ; CHECK-NEXT:    movq global at GOTPCREL_NORELAX(%rip), %rax
 ; CHECK-NEXT:    movl $0, (%rax)
 ; CHECK-NEXT:    retq
+;
+; LARGE-LABEL: global_store:
+; LARGE:       # %bb.0:
+; LARGE-NEXT:    movabsq $global, %rax
+; LARGE-NEXT:    movl $0, (%rax)
+; LARGE-NEXT:    retq
   store i32 0, ptr @global
   ret void
 }
@@ -40,6 +59,11 @@ define ptr @func_addr() #0 {
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    movl $func, %eax
 ; CHECK-NEXT:    retq
+;
+; LARGE-LABEL: func_addr:
+; LARGE:       # %bb.0:
+; LARGE-NEXT:    movabsq $func, %rax
+; LARGE-NEXT:    retq
   ret ptr @func
 }
 


        


More information about the llvm-commits mailing list