[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-15 Thread Fangrui Song via cfe-commits

https://github.com/MaskRay edited 
https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-15 Thread Fangrui Song via cfe-commits

https://github.com/MaskRay approved this pull request.


https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-15 Thread Fangrui Song via cfe-commits


@@ -0,0 +1,247 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=i686 --relocation-model=pic -enable-tlsdesc | 
FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-pc-linux-gnux32 --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X32
+; RUN: llc < %s -mtriple=x86_64 --relocation-model=pic -enable-tlsdesc | 
FileCheck %s --check-prefix=X64
+
+@x = thread_local global i32 0, align 4
+@y = internal thread_local global i32 0, align 4
+@z = internal thread_local global i32 1, align 4
+@t = external hidden thread_local global i32, align 4
+
+define ptr @f1() nounwind {
+; X86-LABEL: f1:
+; X86:   # %bb.0:
+; X86-NEXT:pushl %ebp
+; X86-NEXT:pushl %ebx
+; X86-NEXT:pushl %edi
+; X86-NEXT:pushl %esi
+; X86-NEXT:pushl %eax
+; X86-NEXT:calll .L0$pb
+; X86-NEXT:  .L0$pb:
+; X86-NEXT:popl %ebx
+; X86-NEXT:  .Ltmp0:
+; X86-NEXT:addl $_GLOBAL_OFFSET_TABLE_+(.Ltmp0-.L0$pb), %ebx
+; X86-NEXT:#APP
+; X86-NEXT:#NO_APP
+; X86-NEXT:movl %eax, (%esp) # 4-byte Spill
+; X86-NEXT:leal x@tlsdesc(%ebx), %eax
+; X86-NEXT:calll *x@tlscall(%eax)
+; X86-NEXT:addl %gs:0, %eax
+; X86-NEXT:movl (%esp), %ebx # 4-byte Reload
+; X86-NEXT:#APP
+; X86-NEXT:#NO_APP
+; X86-NEXT:addl $4, %esp
+; X86-NEXT:popl %esi
+; X86-NEXT:popl %edi
+; X86-NEXT:popl %ebx
+; X86-NEXT:popl %ebp
+; X86-NEXT:retl
+;
+; X32-LABEL: f1:
+; X32:   # %bb.0:
+; X32-NEXT:pushq %rax
+; X32-NEXT:#APP
+; X32-NEXT:#NO_APP
+; X32-NEXT:leal x@tlsdesc(%rip), %eax
+; X32-NEXT:callq *x@tlscall(%eax)
+; X32-NEXT:# kill: def $eax killed $eax def $rax
+; X32-NEXT:addl %fs:0, %eax
+; X32-NEXT:#APP
+; X32-NEXT:#NO_APP
+; X32-NEXT:popq %rcx
+; X32-NEXT:retq
+;
+; X64-LABEL: f1:
+; X64:   # %bb.0:
+; X64-NEXT:pushq %rax
+; X64-NEXT:#APP
+; X64-NEXT:#NO_APP
+; X64-NEXT:leaq x@tlsdesc(%rip), %rax
+; X64-NEXT:callq *x@tlscall(%rax)
+; X64-NEXT:addq %fs:0, %rax
+; X64-NEXT:#APP
+; X64-NEXT:#NO_APP
+; X64-NEXT:popq %rcx
+; X64-NEXT:retq
+  %a = call { i32, i32, i32, i32, i32, i32 } asm sideeffect "", 
"=r,=r,=r,=r,=r,=r,~{dirflag},~{fpsr},~{flags}"()
+  %b = call ptr @llvm.threadlocal.address.p0(ptr @x)
+  %a.0 = extractvalue { i32, i32, i32, i32, i32, i32 } %a, 0
+  %a.1 = extractvalue { i32, i32, i32, i32, i32, i32 } %a, 1
+  %a.2 = extractvalue { i32, i32, i32, i32, i32, i32 } %a, 2
+  %a.3 = extractvalue { i32, i32, i32, i32, i32, i32 } %a, 3
+  %a.4 = extractvalue { i32, i32, i32, i32, i32, i32 } %a, 4
+  %a.5 = extractvalue { i32, i32, i32, i32, i32, i32 } %a, 5
+  call void asm sideeffect "", "r,r,r,r,r,r,~{dirflag},~{fpsr},~{flags}"(i32 
%a.0, i32 %a.1, i32 %a.2, i32 %a.3, i32 %a.4, i32 %a.5)
+  ret ptr %b
+}
+
+define i32 @f2() nounwind {
+; X86-LABEL: f2:
+; X86:   # %bb.0:
+; X86-NEXT:pushl %ebx
+; X86-NEXT:calll .L1$pb
+; X86-NEXT:  .L1$pb:
+; X86-NEXT:popl %ebx
+; X86-NEXT:  .Ltmp1:
+; X86-NEXT:addl $_GLOBAL_OFFSET_TABLE_+(.Ltmp1-.L1$pb), %ebx
+; X86-NEXT:movl %gs:0, %ecx
+; X86-NEXT:leal x@tlsdesc(%ebx), %eax
+; X86-NEXT:calll *x@tlscall(%eax)
+; X86-NEXT:movl (%eax,%ecx), %eax
+; X86-NEXT:popl %ebx
+; X86-NEXT:retl
+;
+; X32-LABEL: f2:
+; X32:   # %bb.0:
+; X32-NEXT:pushq %rax
+; X32-NEXT:movl %fs:0, %ecx
+; X32-NEXT:leal x@tlsdesc(%rip), %eax
+; X32-NEXT:callq *x@tlscall(%eax)
+; X32-NEXT:movl (%eax,%ecx), %eax
+; X32-NEXT:popq %rcx
+; X32-NEXT:retq
+;
+; X64-LABEL: f2:
+; X64:   # %bb.0:
+; X64-NEXT:pushq %rax
+; X64-NEXT:movq %fs:0, %rcx
+; X64-NEXT:leaq x@tlsdesc(%rip), %rax
+; X64-NEXT:callq *x@tlscall(%rax)
+; X64-NEXT:movl (%rax,%rcx), %eax
+; X64-NEXT:popq %rcx
+; X64-NEXT:retq
+  %1 = tail call ptr @llvm.threadlocal.address.p0(ptr @x)
+  %2 = load i32, ptr %1
+  ret i32 %2
+}
+
+define ptr @f3() nounwind {
+; X86-LABEL: f3:
+; X86:   # %bb.0:
+; X86-NEXT:pushl %ebx
+; X86-NEXT:calll .L2$pb
+; X86-NEXT:  .L2$pb:
+; X86-NEXT:popl %ebx
+; X86-NEXT:  .Ltmp2:
+; X86-NEXT:addl $_GLOBAL_OFFSET_TABLE_+(.Ltmp2-.L2$pb), %ebx
+; X86-NEXT:leal x@tlsdesc(%ebx), %eax
+; X86-NEXT:calll *x@tlscall(%eax)
+; X86-NEXT:addl %gs:0, %eax
+; X86-NEXT:popl %ebx
+; X86-NEXT:retl
+;
+; X32-LABEL: f3:
+; X32:   # %bb.0:
+; X32-NEXT:pushq %rax
+; X32-NEXT:leal x@tlsdesc(%rip), %eax
+; X32-NEXT:callq *x@tlscall(%eax)
+; X32-NEXT:# kill: def $eax killed $eax def $rax
+; X32-NEXT:addl %fs:0, %eax
+; X32-NEXT:popq %rcx
+; X32-NEXT:retq
+;
+; X64-LABEL: f3:
+; X64:   # %bb.0:
+; X64-NEXT:pushq %rax
+; X64-NEXT:leaq x@tlsdesc(%rip), %rax
+; X64-NEXT:callq *x@tlscall(%rax)
+; X64-NEXT:addq %fs:0, %rax
+; X64-NEXT:popq %rcx
+; X64-NEXT:retq
+  %1 = tail call ptr @llvm.threadlocal.address

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-15 Thread Phoebe Wang via cfe-commits


@@ -0,0 +1,247 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=i686 --relocation-model=pic -enable-tlsdesc | 
FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-pc-linux-gnux32 --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X32
+; RUN: llc < %s -mtriple=x86_64 --relocation-model=pic -enable-tlsdesc | 
FileCheck %s --check-prefix=X64
+
+@x = thread_local global i32 0, align 4
+@y = internal thread_local global i32 0, align 4
+@z = internal thread_local global i32 1, align 4
+@t = external hidden thread_local global i32, align 4
+
+define ptr @f1() nounwind {
+; X86-LABEL: f1:
+; X86:   # %bb.0:
+; X86-NEXT:pushl %ebp
+; X86-NEXT:pushl %ebx
+; X86-NEXT:pushl %edi
+; X86-NEXT:pushl %esi
+; X86-NEXT:pushl %eax
+; X86-NEXT:calll .L0$pb
+; X86-NEXT:  .L0$pb:
+; X86-NEXT:popl %ebx
+; X86-NEXT:  .Ltmp0:
+; X86-NEXT:addl $_GLOBAL_OFFSET_TABLE_+(.Ltmp0-.L0$pb), %ebx
+; X86-NEXT:#APP
+; X86-NEXT:#NO_APP
+; X86-NEXT:movl %eax, (%esp) # 4-byte Spill
+; X86-NEXT:leal x@tlsdesc(%ebx), %eax
+; X86-NEXT:calll *x@tlscall(%eax)
+; X86-NEXT:addl %gs:0, %eax
+; X86-NEXT:movl (%esp), %ebx # 4-byte Reload
+; X86-NEXT:#APP
+; X86-NEXT:#NO_APP
+; X86-NEXT:addl $4, %esp
+; X86-NEXT:popl %esi
+; X86-NEXT:popl %edi
+; X86-NEXT:popl %ebx
+; X86-NEXT:popl %ebp
+; X86-NEXT:retl
+;
+; X32-LABEL: f1:
+; X32:   # %bb.0:
+; X32-NEXT:pushq %rax
+; X32-NEXT:#APP
+; X32-NEXT:#NO_APP
+; X32-NEXT:leal x@tlsdesc(%rip), %eax
+; X32-NEXT:callq *x@tlscall(%eax)
+; X32-NEXT:# kill: def $eax killed $eax def $rax
+; X32-NEXT:addl %fs:0, %eax
+; X32-NEXT:#APP
+; X32-NEXT:#NO_APP
+; X32-NEXT:popq %rcx
+; X32-NEXT:retq
+;
+; X64-LABEL: f1:
+; X64:   # %bb.0:
+; X64-NEXT:pushq %rax
+; X64-NEXT:#APP
+; X64-NEXT:#NO_APP
+; X64-NEXT:leaq x@tlsdesc(%rip), %rax
+; X64-NEXT:callq *x@tlscall(%rax)
+; X64-NEXT:addq %fs:0, %rax
+; X64-NEXT:#APP
+; X64-NEXT:#NO_APP
+; X64-NEXT:popq %rcx
+; X64-NEXT:retq
+  %a = call { i32, i32, i32, i32, i32, i32 } asm sideeffect "", 
"=r,=r,=r,=r,=r,=r,~{dirflag},~{fpsr},~{flags}"()
+  %b = call ptr @llvm.threadlocal.address.p0(ptr @x)
+  %a.0 = extractvalue { i32, i32, i32, i32, i32, i32 } %a, 0
+  %a.1 = extractvalue { i32, i32, i32, i32, i32, i32 } %a, 1
+  %a.2 = extractvalue { i32, i32, i32, i32, i32, i32 } %a, 2
+  %a.3 = extractvalue { i32, i32, i32, i32, i32, i32 } %a, 3
+  %a.4 = extractvalue { i32, i32, i32, i32, i32, i32 } %a, 4
+  %a.5 = extractvalue { i32, i32, i32, i32, i32, i32 } %a, 5
+  call void asm sideeffect "", "r,r,r,r,r,r,~{dirflag},~{fpsr},~{flags}"(i32 
%a.0, i32 %a.1, i32 %a.2, i32 %a.3, i32 %a.4, i32 %a.5)
+  ret ptr %b
+}
+
+define i32 @f2() nounwind {
+; X86-LABEL: f2:
+; X86:   # %bb.0:
+; X86-NEXT:pushl %ebx
+; X86-NEXT:calll .L1$pb
+; X86-NEXT:  .L1$pb:
+; X86-NEXT:popl %ebx
+; X86-NEXT:  .Ltmp1:
+; X86-NEXT:addl $_GLOBAL_OFFSET_TABLE_+(.Ltmp1-.L1$pb), %ebx
+; X86-NEXT:movl %gs:0, %ecx
+; X86-NEXT:leal x@tlsdesc(%ebx), %eax
+; X86-NEXT:calll *x@tlscall(%eax)
+; X86-NEXT:movl (%eax,%ecx), %eax
+; X86-NEXT:popl %ebx
+; X86-NEXT:retl
+;
+; X32-LABEL: f2:
+; X32:   # %bb.0:
+; X32-NEXT:pushq %rax
+; X32-NEXT:movl %fs:0, %ecx
+; X32-NEXT:leal x@tlsdesc(%rip), %eax
+; X32-NEXT:callq *x@tlscall(%eax)
+; X32-NEXT:movl (%eax,%ecx), %eax
+; X32-NEXT:popq %rcx
+; X32-NEXT:retq
+;
+; X64-LABEL: f2:
+; X64:   # %bb.0:
+; X64-NEXT:pushq %rax
+; X64-NEXT:movq %fs:0, %rcx
+; X64-NEXT:leaq x@tlsdesc(%rip), %rax
+; X64-NEXT:callq *x@tlscall(%rax)
+; X64-NEXT:movl (%rax,%rcx), %eax
+; X64-NEXT:popq %rcx
+; X64-NEXT:retq
+  %1 = tail call ptr @llvm.threadlocal.address.p0(ptr @x)
+  %2 = load i32, ptr %1
+  ret i32 %2
+}
+
+define ptr @f3() nounwind {
+; X86-LABEL: f3:
+; X86:   # %bb.0:
+; X86-NEXT:pushl %ebx
+; X86-NEXT:calll .L2$pb
+; X86-NEXT:  .L2$pb:
+; X86-NEXT:popl %ebx
+; X86-NEXT:  .Ltmp2:
+; X86-NEXT:addl $_GLOBAL_OFFSET_TABLE_+(.Ltmp2-.L2$pb), %ebx
+; X86-NEXT:leal x@tlsdesc(%ebx), %eax
+; X86-NEXT:calll *x@tlscall(%eax)
+; X86-NEXT:addl %gs:0, %eax
+; X86-NEXT:popl %ebx
+; X86-NEXT:retl
+;
+; X32-LABEL: f3:
+; X32:   # %bb.0:
+; X32-NEXT:pushq %rax
+; X32-NEXT:leal x@tlsdesc(%rip), %eax
+; X32-NEXT:callq *x@tlscall(%eax)
+; X32-NEXT:# kill: def $eax killed $eax def $rax
+; X32-NEXT:addl %fs:0, %eax
+; X32-NEXT:popq %rcx
+; X32-NEXT:retq
+;
+; X64-LABEL: f3:
+; X64:   # %bb.0:
+; X64-NEXT:pushq %rax
+; X64-NEXT:leaq x@tlsdesc(%rip), %rax
+; X64-NEXT:callq *x@tlscall(%rax)
+; X64-NEXT:addq %fs:0, %rax
+; X64-NEXT:popq %rcx
+; X64-NEXT:retq
+  %1 = tail call ptr @llvm.threadlocal.address

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-15 Thread Phoebe Wang via cfe-commits

https://github.com/phoebewang updated 
https://github.com/llvm/llvm-project/pull/83136

>From cdc9ee6c322af0ceed162f3f714bcd0a22e020c3 Mon Sep 17 00:00:00 2001
From: Phoebe Wang 
Date: Tue, 27 Feb 2024 22:16:38 +0800
Subject: [PATCH 1/8] [X86] Add Support for X86 TLSDESC Relocations

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp|   3 +-
 clang/test/Driver/tls-dialect.c   |   2 +-
 .../lib/Target/X86/MCTargetDesc/X86BaseInfo.h |  14 ++
 llvm/lib/Target/X86/X86AsmPrinter.cpp |   2 +
 llvm/lib/Target/X86/X86ISelLowering.cpp   |  98 +++
 llvm/lib/Target/X86/X86MCInstLower.cpp|  30 +++-
 llvm/test/CodeGen/X86/tls-desc.ll | 165 ++
 7 files changed, 273 insertions(+), 41 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/tls-desc.ll

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index faceee85a2f8dc..c66e3ee12e50c4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";
   } else {
 Unsupported = true;
   }
diff --git a/clang/test/Driver/tls-dialect.c b/clang/test/Driver/tls-dialect.c
index f73915b28ec2a3..a808dd81531ce7 100644
--- a/clang/test/Driver/tls-dialect.c
+++ b/clang/test/Driver/tls-dialect.c
@@ -2,6 +2,7 @@
 // RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
 // RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck 
--check-prefix=NODESC %s
 // RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
+// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=DESC %s
 
 /// Android supports TLSDESC by default on RISC-V
 /// TLSDESC is not on by default in Linux, even on RISC-V, and is covered above
@@ -18,7 +19,6 @@
 
 /// Unsupported argument
 // RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
-// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
 
 // DESC:   "-cc1" {{.*}}"-enable-tlsdesc"
 // NODESC-NOT: "-enable-tlsdesc"
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
index 4442b80861b61a..1877550f8c40bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
@@ -431,6 +431,20 @@ enum TOF {
   /// See 'ELF Handling for Thread-Local Storage' for more details.
   ///SYMBOL_LABEL @TLSLDM
   MO_TLSLDM,
+  /// MO_TLSCALL - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor function for the symbol. Used in both
+  /// the IA32 and x86-64 local dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSCALL
+  MO_TLSCALL,
+  /// MO_TLSDESC - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor argument for the symbol. When this
+  /// argument is passed to a call getting from index@TLSCALL, the function 
will
+  /// return the offset for the symbol. Used in both the IA32 and x86-64 local
+  /// dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSDESC
+  MO_TLSDESC,
   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
   /// the offset of the GOT entry with the thread-pointer offset for the
   /// symbol. Used in the x86-64 initial exec TLS access model.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 3395a13545e454..d8e111db1cec42 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,6 +271,8 @@ void X86AsmPrinter::PrintSymbolOperand(const MachineOperand 
&MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
+  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
+  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a86f13135173b0..88314bcf510e9a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) con

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-15 Thread Phoebe Wang via cfe-commits

https://github.com/phoebewang updated 
https://github.com/llvm/llvm-project/pull/83136

>From cdc9ee6c322af0ceed162f3f714bcd0a22e020c3 Mon Sep 17 00:00:00 2001
From: Phoebe Wang 
Date: Tue, 27 Feb 2024 22:16:38 +0800
Subject: [PATCH 1/8] [X86] Add Support for X86 TLSDESC Relocations

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp|   3 +-
 clang/test/Driver/tls-dialect.c   |   2 +-
 .../lib/Target/X86/MCTargetDesc/X86BaseInfo.h |  14 ++
 llvm/lib/Target/X86/X86AsmPrinter.cpp |   2 +
 llvm/lib/Target/X86/X86ISelLowering.cpp   |  98 +++
 llvm/lib/Target/X86/X86MCInstLower.cpp|  30 +++-
 llvm/test/CodeGen/X86/tls-desc.ll | 165 ++
 7 files changed, 273 insertions(+), 41 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/tls-desc.ll

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index faceee85a2f8dc..c66e3ee12e50c4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";
   } else {
 Unsupported = true;
   }
diff --git a/clang/test/Driver/tls-dialect.c b/clang/test/Driver/tls-dialect.c
index f73915b28ec2a3..a808dd81531ce7 100644
--- a/clang/test/Driver/tls-dialect.c
+++ b/clang/test/Driver/tls-dialect.c
@@ -2,6 +2,7 @@
 // RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
 // RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck 
--check-prefix=NODESC %s
 // RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
+// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=DESC %s
 
 /// Android supports TLSDESC by default on RISC-V
 /// TLSDESC is not on by default in Linux, even on RISC-V, and is covered above
@@ -18,7 +19,6 @@
 
 /// Unsupported argument
 // RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
-// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
 
 // DESC:   "-cc1" {{.*}}"-enable-tlsdesc"
 // NODESC-NOT: "-enable-tlsdesc"
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
index 4442b80861b61a..1877550f8c40bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
@@ -431,6 +431,20 @@ enum TOF {
   /// See 'ELF Handling for Thread-Local Storage' for more details.
   ///SYMBOL_LABEL @TLSLDM
   MO_TLSLDM,
+  /// MO_TLSCALL - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor function for the symbol. Used in both
+  /// the IA32 and x86-64 local dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSCALL
+  MO_TLSCALL,
+  /// MO_TLSDESC - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor argument for the symbol. When this
+  /// argument is passed to a call getting from index@TLSCALL, the function 
will
+  /// return the offset for the symbol. Used in both the IA32 and x86-64 local
+  /// dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSDESC
+  MO_TLSDESC,
   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
   /// the offset of the GOT entry with the thread-pointer offset for the
   /// symbol. Used in the x86-64 initial exec TLS access model.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 3395a13545e454..d8e111db1cec42 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,6 +271,8 @@ void X86AsmPrinter::PrintSymbolOperand(const MachineOperand 
&MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
+  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
+  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a86f13135173b0..88314bcf510e9a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) con

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-15 Thread Phoebe Wang via cfe-commits

https://github.com/phoebewang closed 
https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-27 Thread Phoebe Wang via cfe-commits

https://github.com/phoebewang created 
https://github.com/llvm/llvm-project/pull/83136

None

>From 421a5e4c0a6d7beda71118a36650e72c3d6f2377 Mon Sep 17 00:00:00 2001
From: Phoebe Wang 
Date: Tue, 27 Feb 2024 22:16:38 +0800
Subject: [PATCH] [X86] Add Support for X86 TLSDESC Relocations

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp|   3 +-
 clang/test/Driver/tls-dialect.c   |   2 +-
 .../lib/Target/X86/MCTargetDesc/X86BaseInfo.h |  14 ++
 llvm/lib/Target/X86/X86AsmPrinter.cpp |   2 +
 llvm/lib/Target/X86/X86ISelLowering.cpp   |  98 +++
 llvm/lib/Target/X86/X86MCInstLower.cpp|  30 +++-
 llvm/test/CodeGen/X86/tls-desc.ll | 165 ++
 7 files changed, 273 insertions(+), 41 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/tls-desc.ll

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index faceee85a2f8dc..c66e3ee12e50c4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";
   } else {
 Unsupported = true;
   }
diff --git a/clang/test/Driver/tls-dialect.c b/clang/test/Driver/tls-dialect.c
index f73915b28ec2a3..a808dd81531ce7 100644
--- a/clang/test/Driver/tls-dialect.c
+++ b/clang/test/Driver/tls-dialect.c
@@ -2,6 +2,7 @@
 // RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
 // RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck 
--check-prefix=NODESC %s
 // RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
+// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=DESC %s
 
 /// Android supports TLSDESC by default on RISC-V
 /// TLSDESC is not on by default in Linux, even on RISC-V, and is covered above
@@ -18,7 +19,6 @@
 
 /// Unsupported argument
 // RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
-// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
 
 // DESC:   "-cc1" {{.*}}"-enable-tlsdesc"
 // NODESC-NOT: "-enable-tlsdesc"
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
index 4442b80861b61a..1877550f8c40bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
@@ -431,6 +431,20 @@ enum TOF {
   /// See 'ELF Handling for Thread-Local Storage' for more details.
   ///SYMBOL_LABEL @TLSLDM
   MO_TLSLDM,
+  /// MO_TLSCALL - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor function for the symbol. Used in both
+  /// the IA32 and x86-64 local dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSCALL
+  MO_TLSCALL,
+  /// MO_TLSDESC - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor argument for the symbol. When this
+  /// argument is passed to a call getting from index@TLSCALL, the function 
will
+  /// return the offset for the symbol. Used in both the IA32 and x86-64 local
+  /// dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSDESC
+  MO_TLSDESC,
   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
   /// the offset of the GOT entry with the thread-pointer offset for the
   /// symbol. Used in the x86-64 initial exec TLS access model.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 3395a13545e454..d8e111db1cec42 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,6 +271,8 @@ void X86AsmPrinter::PrintSymbolOperand(const MachineOperand 
&MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
+  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
+  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a86f13135173b0..88314bcf510e9a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) c

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-27 Thread via cfe-commits

llvmbot wrote:



@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-clang-driver

Author: Phoebe Wang (phoebewang)


Changes



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


7 Files Affected:

- (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+2-1) 
- (modified) clang/test/Driver/tls-dialect.c (+1-1) 
- (modified) llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h (+14) 
- (modified) llvm/lib/Target/X86/X86AsmPrinter.cpp (+2) 
- (modified) llvm/lib/Target/X86/X86ISelLowering.cpp (+61-37) 
- (modified) llvm/lib/Target/X86/X86MCInstLower.cpp (+28-2) 
- (added) llvm/test/CodeGen/X86/tls-desc.ll (+165) 


``diff
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index faceee85a2f8dc..c66e3ee12e50c4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";
   } else {
 Unsupported = true;
   }
diff --git a/clang/test/Driver/tls-dialect.c b/clang/test/Driver/tls-dialect.c
index f73915b28ec2a3..a808dd81531ce7 100644
--- a/clang/test/Driver/tls-dialect.c
+++ b/clang/test/Driver/tls-dialect.c
@@ -2,6 +2,7 @@
 // RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
 // RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck 
--check-prefix=NODESC %s
 // RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
+// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=DESC %s
 
 /// Android supports TLSDESC by default on RISC-V
 /// TLSDESC is not on by default in Linux, even on RISC-V, and is covered above
@@ -18,7 +19,6 @@
 
 /// Unsupported argument
 // RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
-// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
 
 // DESC:   "-cc1" {{.*}}"-enable-tlsdesc"
 // NODESC-NOT: "-enable-tlsdesc"
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
index 4442b80861b61a..1877550f8c40bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
@@ -431,6 +431,20 @@ enum TOF {
   /// See 'ELF Handling for Thread-Local Storage' for more details.
   ///SYMBOL_LABEL @TLSLDM
   MO_TLSLDM,
+  /// MO_TLSCALL - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor function for the symbol. Used in both
+  /// the IA32 and x86-64 local dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSCALL
+  MO_TLSCALL,
+  /// MO_TLSDESC - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor argument for the symbol. When this
+  /// argument is passed to a call getting from index@TLSCALL, the function 
will
+  /// return the offset for the symbol. Used in both the IA32 and x86-64 local
+  /// dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSDESC
+  MO_TLSDESC,
   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
   /// the offset of the GOT entry with the thread-pointer offset for the
   /// symbol. Used in the x86-64 initial exec TLS access model.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 3395a13545e454..d8e111db1cec42 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,6 +271,8 @@ void X86AsmPrinter::PrintSymbolOperand(const MachineOperand 
&MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
+  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
+  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a86f13135173b0..88314bcf510e9a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) const {
   return LowerGlobalOrExternal(Op, DAG, /*ForCall=*/false);
 }
 
-static SDValue
-GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
-

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-27 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Phoebe Wang (phoebewang)


Changes



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


7 Files Affected:

- (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+2-1) 
- (modified) clang/test/Driver/tls-dialect.c (+1-1) 
- (modified) llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h (+14) 
- (modified) llvm/lib/Target/X86/X86AsmPrinter.cpp (+2) 
- (modified) llvm/lib/Target/X86/X86ISelLowering.cpp (+61-37) 
- (modified) llvm/lib/Target/X86/X86MCInstLower.cpp (+28-2) 
- (added) llvm/test/CodeGen/X86/tls-desc.ll (+165) 


``diff
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index faceee85a2f8dc..c66e3ee12e50c4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";
   } else {
 Unsupported = true;
   }
diff --git a/clang/test/Driver/tls-dialect.c b/clang/test/Driver/tls-dialect.c
index f73915b28ec2a3..a808dd81531ce7 100644
--- a/clang/test/Driver/tls-dialect.c
+++ b/clang/test/Driver/tls-dialect.c
@@ -2,6 +2,7 @@
 // RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
 // RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck 
--check-prefix=NODESC %s
 // RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
+// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=DESC %s
 
 /// Android supports TLSDESC by default on RISC-V
 /// TLSDESC is not on by default in Linux, even on RISC-V, and is covered above
@@ -18,7 +19,6 @@
 
 /// Unsupported argument
 // RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
-// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
 
 // DESC:   "-cc1" {{.*}}"-enable-tlsdesc"
 // NODESC-NOT: "-enable-tlsdesc"
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
index 4442b80861b61a..1877550f8c40bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
@@ -431,6 +431,20 @@ enum TOF {
   /// See 'ELF Handling for Thread-Local Storage' for more details.
   ///SYMBOL_LABEL @TLSLDM
   MO_TLSLDM,
+  /// MO_TLSCALL - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor function for the symbol. Used in both
+  /// the IA32 and x86-64 local dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSCALL
+  MO_TLSCALL,
+  /// MO_TLSDESC - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor argument for the symbol. When this
+  /// argument is passed to a call getting from index@TLSCALL, the function 
will
+  /// return the offset for the symbol. Used in both the IA32 and x86-64 local
+  /// dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSDESC
+  MO_TLSDESC,
   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
   /// the offset of the GOT entry with the thread-pointer offset for the
   /// symbol. Used in the x86-64 initial exec TLS access model.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 3395a13545e454..d8e111db1cec42 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,6 +271,8 @@ void X86AsmPrinter::PrintSymbolOperand(const MachineOperand 
&MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
+  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
+  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a86f13135173b0..88314bcf510e9a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) const {
   return LowerGlobalOrExternal(Op, DAG, /*ForCall=*/false);
 }
 
-static SDValue
-GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
-   SDValue *InGlue, const EVT Pt

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-27 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 5ec535b1bda4c87aac951e65cc5b65c910e92579 
421a5e4c0a6d7beda71118a36650e72c3d6f2377 -- 
clang/lib/Driver/ToolChains/CommonArgs.cpp clang/test/Driver/tls-dialect.c 
llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
llvm/lib/Target/X86/X86AsmPrinter.cpp llvm/lib/Target/X86/X86ISelLowering.cpp 
llvm/lib/Target/X86/X86MCInstLower.cpp
``





View the diff from clang-format here.


``diff
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index d8e111db1c..a69c4bcb71 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,8 +271,12 @@ void X86AsmPrinter::PrintSymbolOperand(const 
MachineOperand &MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
-  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
-  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
+  case X86II::MO_TLSDESC:
+O << "@TLSDESC";
+break;
+  case X86II::MO_TLSCALL:
+O << "@TLSCALL";
+break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;

``




https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-27 Thread Phoebe Wang via cfe-commits

https://github.com/phoebewang updated 
https://github.com/llvm/llvm-project/pull/83136

>From cdc9ee6c322af0ceed162f3f714bcd0a22e020c3 Mon Sep 17 00:00:00 2001
From: Phoebe Wang 
Date: Tue, 27 Feb 2024 22:16:38 +0800
Subject: [PATCH] [X86] Add Support for X86 TLSDESC Relocations

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp|   3 +-
 clang/test/Driver/tls-dialect.c   |   2 +-
 .../lib/Target/X86/MCTargetDesc/X86BaseInfo.h |  14 ++
 llvm/lib/Target/X86/X86AsmPrinter.cpp |   2 +
 llvm/lib/Target/X86/X86ISelLowering.cpp   |  98 +++
 llvm/lib/Target/X86/X86MCInstLower.cpp|  30 +++-
 llvm/test/CodeGen/X86/tls-desc.ll | 165 ++
 7 files changed, 273 insertions(+), 41 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/tls-desc.ll

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index faceee85a2f8dc..c66e3ee12e50c4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";
   } else {
 Unsupported = true;
   }
diff --git a/clang/test/Driver/tls-dialect.c b/clang/test/Driver/tls-dialect.c
index f73915b28ec2a3..a808dd81531ce7 100644
--- a/clang/test/Driver/tls-dialect.c
+++ b/clang/test/Driver/tls-dialect.c
@@ -2,6 +2,7 @@
 // RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
 // RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck 
--check-prefix=NODESC %s
 // RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
+// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=DESC %s
 
 /// Android supports TLSDESC by default on RISC-V
 /// TLSDESC is not on by default in Linux, even on RISC-V, and is covered above
@@ -18,7 +19,6 @@
 
 /// Unsupported argument
 // RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
-// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
 
 // DESC:   "-cc1" {{.*}}"-enable-tlsdesc"
 // NODESC-NOT: "-enable-tlsdesc"
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
index 4442b80861b61a..1877550f8c40bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
@@ -431,6 +431,20 @@ enum TOF {
   /// See 'ELF Handling for Thread-Local Storage' for more details.
   ///SYMBOL_LABEL @TLSLDM
   MO_TLSLDM,
+  /// MO_TLSCALL - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor function for the symbol. Used in both
+  /// the IA32 and x86-64 local dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSCALL
+  MO_TLSCALL,
+  /// MO_TLSDESC - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor argument for the symbol. When this
+  /// argument is passed to a call getting from index@TLSCALL, the function 
will
+  /// return the offset for the symbol. Used in both the IA32 and x86-64 local
+  /// dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSDESC
+  MO_TLSDESC,
   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
   /// the offset of the GOT entry with the thread-pointer offset for the
   /// symbol. Used in the x86-64 initial exec TLS access model.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 3395a13545e454..d8e111db1cec42 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,6 +271,8 @@ void X86AsmPrinter::PrintSymbolOperand(const MachineOperand 
&MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
+  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
+  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a86f13135173b0..88314bcf510e9a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) const {

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-27 Thread Fangrui Song via cfe-commits


@@ -0,0 +1,165 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=i686-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X86

MaskRay wrote:

`-unknown-unknown` can be removed

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-27 Thread Fangrui Song via cfe-commits


@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) const {
   return LowerGlobalOrExternal(Op, DAG, /*ForCall=*/false);
 }
 
-static SDValue
-GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
-   SDValue *InGlue, const EVT PtrVT, unsigned ReturnReg,
-   unsigned char OperandFlags, bool LocalDynamic = false) {
+static SDValue GetTLSADDR(SelectionDAG &DAG, SDValue Chain,

MaskRay wrote:

While changing signatures, rename this to `getTLSADDR` (lowerCase)?

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-27 Thread Fangrui Song via cfe-commits


@@ -0,0 +1,165 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=i686-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-pc-linux-gnux32 --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X32
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X64
+
+@x = thread_local global i32 0, align 4
+@y = internal thread_local global i32 0, align 4

MaskRay wrote:

GCC's x86-64 port assumes that FLAGS_REG and RAX are changed while all other 
registers are preserved.

Ideally, show that some registers are not clobbered.

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-28 Thread Phoebe Wang via cfe-commits

https://github.com/phoebewang updated 
https://github.com/llvm/llvm-project/pull/83136

>From cdc9ee6c322af0ceed162f3f714bcd0a22e020c3 Mon Sep 17 00:00:00 2001
From: Phoebe Wang 
Date: Tue, 27 Feb 2024 22:16:38 +0800
Subject: [PATCH 1/2] [X86] Add Support for X86 TLSDESC Relocations

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp|   3 +-
 clang/test/Driver/tls-dialect.c   |   2 +-
 .../lib/Target/X86/MCTargetDesc/X86BaseInfo.h |  14 ++
 llvm/lib/Target/X86/X86AsmPrinter.cpp |   2 +
 llvm/lib/Target/X86/X86ISelLowering.cpp   |  98 +++
 llvm/lib/Target/X86/X86MCInstLower.cpp|  30 +++-
 llvm/test/CodeGen/X86/tls-desc.ll | 165 ++
 7 files changed, 273 insertions(+), 41 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/tls-desc.ll

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index faceee85a2f8dc..c66e3ee12e50c4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";
   } else {
 Unsupported = true;
   }
diff --git a/clang/test/Driver/tls-dialect.c b/clang/test/Driver/tls-dialect.c
index f73915b28ec2a3..a808dd81531ce7 100644
--- a/clang/test/Driver/tls-dialect.c
+++ b/clang/test/Driver/tls-dialect.c
@@ -2,6 +2,7 @@
 // RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
 // RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck 
--check-prefix=NODESC %s
 // RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
+// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=DESC %s
 
 /// Android supports TLSDESC by default on RISC-V
 /// TLSDESC is not on by default in Linux, even on RISC-V, and is covered above
@@ -18,7 +19,6 @@
 
 /// Unsupported argument
 // RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
-// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
 
 // DESC:   "-cc1" {{.*}}"-enable-tlsdesc"
 // NODESC-NOT: "-enable-tlsdesc"
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
index 4442b80861b61a..1877550f8c40bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
@@ -431,6 +431,20 @@ enum TOF {
   /// See 'ELF Handling for Thread-Local Storage' for more details.
   ///SYMBOL_LABEL @TLSLDM
   MO_TLSLDM,
+  /// MO_TLSCALL - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor function for the symbol. Used in both
+  /// the IA32 and x86-64 local dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSCALL
+  MO_TLSCALL,
+  /// MO_TLSDESC - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor argument for the symbol. When this
+  /// argument is passed to a call getting from index@TLSCALL, the function 
will
+  /// return the offset for the symbol. Used in both the IA32 and x86-64 local
+  /// dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSDESC
+  MO_TLSDESC,
   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
   /// the offset of the GOT entry with the thread-pointer offset for the
   /// symbol. Used in the x86-64 initial exec TLS access model.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 3395a13545e454..d8e111db1cec42 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,6 +271,8 @@ void X86AsmPrinter::PrintSymbolOperand(const MachineOperand 
&MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
+  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
+  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a86f13135173b0..88314bcf510e9a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) con

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-28 Thread Phoebe Wang via cfe-commits


@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) const {
   return LowerGlobalOrExternal(Op, DAG, /*ForCall=*/false);
 }
 
-static SDValue
-GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
-   SDValue *InGlue, const EVT PtrVT, unsigned ReturnReg,
-   unsigned char OperandFlags, bool LocalDynamic = false) {
+static SDValue GetTLSADDR(SelectionDAG &DAG, SDValue Chain,

phoebewang wrote:

Done.

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-28 Thread Phoebe Wang via cfe-commits


@@ -0,0 +1,165 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=i686-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X86

phoebewang wrote:

Done.

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-28 Thread Phoebe Wang via cfe-commits


@@ -0,0 +1,165 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=i686-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-pc-linux-gnux32 --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X32
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X64
+
+@x = thread_local global i32 0, align 4
+@y = internal thread_local global i32 0, align 4

phoebewang wrote:

Thanks for the info! In that way, we need to define a new pseudo instruction 
that only clobbers the two register.
I'll try to do it in the next reversion.

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-28 Thread Phoebe Wang via cfe-commits

https://github.com/phoebewang updated 
https://github.com/llvm/llvm-project/pull/83136

>From cdc9ee6c322af0ceed162f3f714bcd0a22e020c3 Mon Sep 17 00:00:00 2001
From: Phoebe Wang 
Date: Tue, 27 Feb 2024 22:16:38 +0800
Subject: [PATCH 1/3] [X86] Add Support for X86 TLSDESC Relocations

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp|   3 +-
 clang/test/Driver/tls-dialect.c   |   2 +-
 .../lib/Target/X86/MCTargetDesc/X86BaseInfo.h |  14 ++
 llvm/lib/Target/X86/X86AsmPrinter.cpp |   2 +
 llvm/lib/Target/X86/X86ISelLowering.cpp   |  98 +++
 llvm/lib/Target/X86/X86MCInstLower.cpp|  30 +++-
 llvm/test/CodeGen/X86/tls-desc.ll | 165 ++
 7 files changed, 273 insertions(+), 41 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/tls-desc.ll

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index faceee85a2f8dc..c66e3ee12e50c4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";
   } else {
 Unsupported = true;
   }
diff --git a/clang/test/Driver/tls-dialect.c b/clang/test/Driver/tls-dialect.c
index f73915b28ec2a3..a808dd81531ce7 100644
--- a/clang/test/Driver/tls-dialect.c
+++ b/clang/test/Driver/tls-dialect.c
@@ -2,6 +2,7 @@
 // RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
 // RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck 
--check-prefix=NODESC %s
 // RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
+// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=DESC %s
 
 /// Android supports TLSDESC by default on RISC-V
 /// TLSDESC is not on by default in Linux, even on RISC-V, and is covered above
@@ -18,7 +19,6 @@
 
 /// Unsupported argument
 // RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
-// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
 
 // DESC:   "-cc1" {{.*}}"-enable-tlsdesc"
 // NODESC-NOT: "-enable-tlsdesc"
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
index 4442b80861b61a..1877550f8c40bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
@@ -431,6 +431,20 @@ enum TOF {
   /// See 'ELF Handling for Thread-Local Storage' for more details.
   ///SYMBOL_LABEL @TLSLDM
   MO_TLSLDM,
+  /// MO_TLSCALL - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor function for the symbol. Used in both
+  /// the IA32 and x86-64 local dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSCALL
+  MO_TLSCALL,
+  /// MO_TLSDESC - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor argument for the symbol. When this
+  /// argument is passed to a call getting from index@TLSCALL, the function 
will
+  /// return the offset for the symbol. Used in both the IA32 and x86-64 local
+  /// dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSDESC
+  MO_TLSDESC,
   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
   /// the offset of the GOT entry with the thread-pointer offset for the
   /// symbol. Used in the x86-64 initial exec TLS access model.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 3395a13545e454..d8e111db1cec42 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,6 +271,8 @@ void X86AsmPrinter::PrintSymbolOperand(const MachineOperand 
&MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
+  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
+  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a86f13135173b0..88314bcf510e9a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) con

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-02-28 Thread Phoebe Wang via cfe-commits


@@ -0,0 +1,165 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=i686-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-pc-linux-gnux32 --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X32
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X64
+
+@x = thread_local global i32 0, align 4
+@y = internal thread_local global i32 0, align 4

phoebewang wrote:

Done.

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-03 Thread Phoebe Wang via cfe-commits

phoebewang wrote:

Ping~

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-04 Thread Fangrui Song via cfe-commits


@@ -0,0 +1,165 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=i686-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-pc-linux-gnux32 --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X32
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X64
+
+@x = thread_local global i32 0, align 4
+@y = internal thread_local global i32 0, align 4

MaskRay wrote:

Thanks for the update. We need at least 2 `internal thread_local` to trigger 
general dynamic => local dynamic optimization, and demonstrate that TLSDESC 
optimizes

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-04 Thread Fangrui Song via cfe-commits

https://github.com/MaskRay edited 
https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-04 Thread Fangrui Song via cfe-commits

https://github.com/MaskRay edited 
https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-04 Thread Phoebe Wang via cfe-commits

https://github.com/phoebewang updated 
https://github.com/llvm/llvm-project/pull/83136

>From cdc9ee6c322af0ceed162f3f714bcd0a22e020c3 Mon Sep 17 00:00:00 2001
From: Phoebe Wang 
Date: Tue, 27 Feb 2024 22:16:38 +0800
Subject: [PATCH 1/4] [X86] Add Support for X86 TLSDESC Relocations

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp|   3 +-
 clang/test/Driver/tls-dialect.c   |   2 +-
 .../lib/Target/X86/MCTargetDesc/X86BaseInfo.h |  14 ++
 llvm/lib/Target/X86/X86AsmPrinter.cpp |   2 +
 llvm/lib/Target/X86/X86ISelLowering.cpp   |  98 +++
 llvm/lib/Target/X86/X86MCInstLower.cpp|  30 +++-
 llvm/test/CodeGen/X86/tls-desc.ll | 165 ++
 7 files changed, 273 insertions(+), 41 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/tls-desc.ll

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index faceee85a2f8dc..c66e3ee12e50c4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";
   } else {
 Unsupported = true;
   }
diff --git a/clang/test/Driver/tls-dialect.c b/clang/test/Driver/tls-dialect.c
index f73915b28ec2a3..a808dd81531ce7 100644
--- a/clang/test/Driver/tls-dialect.c
+++ b/clang/test/Driver/tls-dialect.c
@@ -2,6 +2,7 @@
 // RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
 // RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck 
--check-prefix=NODESC %s
 // RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
+// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=DESC %s
 
 /// Android supports TLSDESC by default on RISC-V
 /// TLSDESC is not on by default in Linux, even on RISC-V, and is covered above
@@ -18,7 +19,6 @@
 
 /// Unsupported argument
 // RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
-// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
 
 // DESC:   "-cc1" {{.*}}"-enable-tlsdesc"
 // NODESC-NOT: "-enable-tlsdesc"
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
index 4442b80861b61a..1877550f8c40bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
@@ -431,6 +431,20 @@ enum TOF {
   /// See 'ELF Handling for Thread-Local Storage' for more details.
   ///SYMBOL_LABEL @TLSLDM
   MO_TLSLDM,
+  /// MO_TLSCALL - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor function for the symbol. Used in both
+  /// the IA32 and x86-64 local dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSCALL
+  MO_TLSCALL,
+  /// MO_TLSDESC - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor argument for the symbol. When this
+  /// argument is passed to a call getting from index@TLSCALL, the function 
will
+  /// return the offset for the symbol. Used in both the IA32 and x86-64 local
+  /// dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSDESC
+  MO_TLSDESC,
   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
   /// the offset of the GOT entry with the thread-pointer offset for the
   /// symbol. Used in the x86-64 initial exec TLS access model.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 3395a13545e454..d8e111db1cec42 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,6 +271,8 @@ void X86AsmPrinter::PrintSymbolOperand(const MachineOperand 
&MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
+  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
+  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a86f13135173b0..88314bcf510e9a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) con

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-05 Thread Roland McGrath via cfe-commits


@@ -0,0 +1,165 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=i686-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-pc-linux-gnux32 --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X32
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X64
+
+@x = thread_local global i32 0, align 4
+@y = internal thread_local global i32 0, align 4

frobtech wrote:

That example allows the variables to be optimized out entirely because there's 
no way they can ever be modified. https://godbolt.org/z/63oz4xrj1 shows an 
example where it's generating LD model accesses.

Note that in some cases I've seen GCC generate a local anchor symbol instead of 
using `_TLS_MODULE_BASE_`. That is less optimal than using `_TLS_MODULE_BASE_`, 
because multiple uses of `_TLS_MODULE_BASE_` across multiple TUs can be 
resolved by the linker to a single GOT slot (pair), whereas a local anchor 
symbol will always have its own GOT slot (pair) that won't be shared with any 
other TU.

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-05 Thread Phoebe Wang via cfe-commits


@@ -0,0 +1,165 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=i686-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-pc-linux-gnux32 --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X32
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X64
+
+@x = thread_local global i32 0, align 4
+@y = internal thread_local global i32 0, align 4

phoebewang wrote:

Thanks @frobtech for the explanation. IIUC, GCC will use `_TLS_MODULE_BASE_` if 
there are more than 1 local TLS variables, right?

I have no idea how to do it smartly in codegen in LLVM since we lower it 
statelessly. Maybe let's land this first? @MaskRay @frobtech 

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-05 Thread Phoebe Wang via cfe-commits

https://github.com/phoebewang deleted 
https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Simon Pilgrim via cfe-commits


@@ -18515,20 +18515,20 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) const {
   return LowerGlobalOrExternal(Op, DAG, /*ForCall=*/false);
 }
 
-static SDValue
-GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
-   SDValue *InGlue, const EVT PtrVT, unsigned ReturnReg,
-   unsigned char OperandFlags, bool LocalDynamic = false) {
+static SDValue getTLSADDR(SelectionDAG &DAG, SDValue Chain,
+  GlobalAddressSDNode *GA, SDValue *InGlue,
+  const EVT PtrVT, unsigned ReturnReg,
+  unsigned char OperandFlags, bool UseTLSDESC = false,

RKSimon wrote:

Do we need a default arg for UseTLSDESC - all the calls to getTLSADDR seem to 
set it?

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Phoebe Wang via cfe-commits

https://github.com/phoebewang updated 
https://github.com/llvm/llvm-project/pull/83136

>From cdc9ee6c322af0ceed162f3f714bcd0a22e020c3 Mon Sep 17 00:00:00 2001
From: Phoebe Wang 
Date: Tue, 27 Feb 2024 22:16:38 +0800
Subject: [PATCH 1/5] [X86] Add Support for X86 TLSDESC Relocations

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp|   3 +-
 clang/test/Driver/tls-dialect.c   |   2 +-
 .../lib/Target/X86/MCTargetDesc/X86BaseInfo.h |  14 ++
 llvm/lib/Target/X86/X86AsmPrinter.cpp |   2 +
 llvm/lib/Target/X86/X86ISelLowering.cpp   |  98 +++
 llvm/lib/Target/X86/X86MCInstLower.cpp|  30 +++-
 llvm/test/CodeGen/X86/tls-desc.ll | 165 ++
 7 files changed, 273 insertions(+), 41 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/tls-desc.ll

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index faceee85a2f8dc..c66e3ee12e50c4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";
   } else {
 Unsupported = true;
   }
diff --git a/clang/test/Driver/tls-dialect.c b/clang/test/Driver/tls-dialect.c
index f73915b28ec2a3..a808dd81531ce7 100644
--- a/clang/test/Driver/tls-dialect.c
+++ b/clang/test/Driver/tls-dialect.c
@@ -2,6 +2,7 @@
 // RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
 // RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck 
--check-prefix=NODESC %s
 // RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
+// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=DESC %s
 
 /// Android supports TLSDESC by default on RISC-V
 /// TLSDESC is not on by default in Linux, even on RISC-V, and is covered above
@@ -18,7 +19,6 @@
 
 /// Unsupported argument
 // RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
-// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
 
 // DESC:   "-cc1" {{.*}}"-enable-tlsdesc"
 // NODESC-NOT: "-enable-tlsdesc"
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
index 4442b80861b61a..1877550f8c40bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
@@ -431,6 +431,20 @@ enum TOF {
   /// See 'ELF Handling for Thread-Local Storage' for more details.
   ///SYMBOL_LABEL @TLSLDM
   MO_TLSLDM,
+  /// MO_TLSCALL - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor function for the symbol. Used in both
+  /// the IA32 and x86-64 local dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSCALL
+  MO_TLSCALL,
+  /// MO_TLSDESC - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor argument for the symbol. When this
+  /// argument is passed to a call getting from index@TLSCALL, the function 
will
+  /// return the offset for the symbol. Used in both the IA32 and x86-64 local
+  /// dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSDESC
+  MO_TLSDESC,
   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
   /// the offset of the GOT entry with the thread-pointer offset for the
   /// symbol. Used in the x86-64 initial exec TLS access model.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 3395a13545e454..d8e111db1cec42 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,6 +271,8 @@ void X86AsmPrinter::PrintSymbolOperand(const MachineOperand 
&MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
+  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
+  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a86f13135173b0..88314bcf510e9a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) con

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Phoebe Wang via cfe-commits

https://github.com/phoebewang updated 
https://github.com/llvm/llvm-project/pull/83136

>From cdc9ee6c322af0ceed162f3f714bcd0a22e020c3 Mon Sep 17 00:00:00 2001
From: Phoebe Wang 
Date: Tue, 27 Feb 2024 22:16:38 +0800
Subject: [PATCH 1/6] [X86] Add Support for X86 TLSDESC Relocations

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp|   3 +-
 clang/test/Driver/tls-dialect.c   |   2 +-
 .../lib/Target/X86/MCTargetDesc/X86BaseInfo.h |  14 ++
 llvm/lib/Target/X86/X86AsmPrinter.cpp |   2 +
 llvm/lib/Target/X86/X86ISelLowering.cpp   |  98 +++
 llvm/lib/Target/X86/X86MCInstLower.cpp|  30 +++-
 llvm/test/CodeGen/X86/tls-desc.ll | 165 ++
 7 files changed, 273 insertions(+), 41 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/tls-desc.ll

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index faceee85a2f8dc..c66e3ee12e50c4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";
   } else {
 Unsupported = true;
   }
diff --git a/clang/test/Driver/tls-dialect.c b/clang/test/Driver/tls-dialect.c
index f73915b28ec2a3..a808dd81531ce7 100644
--- a/clang/test/Driver/tls-dialect.c
+++ b/clang/test/Driver/tls-dialect.c
@@ -2,6 +2,7 @@
 // RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
 // RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck 
--check-prefix=NODESC %s
 // RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
+// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=DESC %s
 
 /// Android supports TLSDESC by default on RISC-V
 /// TLSDESC is not on by default in Linux, even on RISC-V, and is covered above
@@ -18,7 +19,6 @@
 
 /// Unsupported argument
 // RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
-// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
 
 // DESC:   "-cc1" {{.*}}"-enable-tlsdesc"
 // NODESC-NOT: "-enable-tlsdesc"
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
index 4442b80861b61a..1877550f8c40bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
@@ -431,6 +431,20 @@ enum TOF {
   /// See 'ELF Handling for Thread-Local Storage' for more details.
   ///SYMBOL_LABEL @TLSLDM
   MO_TLSLDM,
+  /// MO_TLSCALL - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor function for the symbol. Used in both
+  /// the IA32 and x86-64 local dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSCALL
+  MO_TLSCALL,
+  /// MO_TLSDESC - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor argument for the symbol. When this
+  /// argument is passed to a call getting from index@TLSCALL, the function 
will
+  /// return the offset for the symbol. Used in both the IA32 and x86-64 local
+  /// dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSDESC
+  MO_TLSDESC,
   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
   /// the offset of the GOT entry with the thread-pointer offset for the
   /// symbol. Used in the x86-64 initial exec TLS access model.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 3395a13545e454..d8e111db1cec42 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,6 +271,8 @@ void X86AsmPrinter::PrintSymbolOperand(const MachineOperand 
&MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
+  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
+  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a86f13135173b0..88314bcf510e9a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) con

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Phoebe Wang via cfe-commits

https://github.com/phoebewang updated 
https://github.com/llvm/llvm-project/pull/83136

>From cdc9ee6c322af0ceed162f3f714bcd0a22e020c3 Mon Sep 17 00:00:00 2001
From: Phoebe Wang 
Date: Tue, 27 Feb 2024 22:16:38 +0800
Subject: [PATCH 1/6] [X86] Add Support for X86 TLSDESC Relocations

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp|   3 +-
 clang/test/Driver/tls-dialect.c   |   2 +-
 .../lib/Target/X86/MCTargetDesc/X86BaseInfo.h |  14 ++
 llvm/lib/Target/X86/X86AsmPrinter.cpp |   2 +
 llvm/lib/Target/X86/X86ISelLowering.cpp   |  98 +++
 llvm/lib/Target/X86/X86MCInstLower.cpp|  30 +++-
 llvm/test/CodeGen/X86/tls-desc.ll | 165 ++
 7 files changed, 273 insertions(+), 41 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/tls-desc.ll

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index faceee85a2f8dc..c66e3ee12e50c4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";
   } else {
 Unsupported = true;
   }
diff --git a/clang/test/Driver/tls-dialect.c b/clang/test/Driver/tls-dialect.c
index f73915b28ec2a3..a808dd81531ce7 100644
--- a/clang/test/Driver/tls-dialect.c
+++ b/clang/test/Driver/tls-dialect.c
@@ -2,6 +2,7 @@
 // RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
 // RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck 
--check-prefix=NODESC %s
 // RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
+// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=DESC %s
 
 /// Android supports TLSDESC by default on RISC-V
 /// TLSDESC is not on by default in Linux, even on RISC-V, and is covered above
@@ -18,7 +19,6 @@
 
 /// Unsupported argument
 // RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
-// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
 
 // DESC:   "-cc1" {{.*}}"-enable-tlsdesc"
 // NODESC-NOT: "-enable-tlsdesc"
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
index 4442b80861b61a..1877550f8c40bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
@@ -431,6 +431,20 @@ enum TOF {
   /// See 'ELF Handling for Thread-Local Storage' for more details.
   ///SYMBOL_LABEL @TLSLDM
   MO_TLSLDM,
+  /// MO_TLSCALL - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor function for the symbol. Used in both
+  /// the IA32 and x86-64 local dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSCALL
+  MO_TLSCALL,
+  /// MO_TLSDESC - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor argument for the symbol. When this
+  /// argument is passed to a call getting from index@TLSCALL, the function 
will
+  /// return the offset for the symbol. Used in both the IA32 and x86-64 local
+  /// dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSDESC
+  MO_TLSDESC,
   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
   /// the offset of the GOT entry with the thread-pointer offset for the
   /// symbol. Used in the x86-64 initial exec TLS access model.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 3395a13545e454..d8e111db1cec42 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,6 +271,8 @@ void X86AsmPrinter::PrintSymbolOperand(const MachineOperand 
&MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
+  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
+  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a86f13135173b0..88314bcf510e9a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) con

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Phoebe Wang via cfe-commits


@@ -0,0 +1,165 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=i686-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-pc-linux-gnux32 --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X32
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X64
+
+@x = thread_local global i32 0, align 4
+@y = internal thread_local global i32 0, align 4

phoebewang wrote:

Done.

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Phoebe Wang via cfe-commits


@@ -18515,20 +18515,20 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) const {
   return LowerGlobalOrExternal(Op, DAG, /*ForCall=*/false);
 }
 
-static SDValue
-GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
-   SDValue *InGlue, const EVT PtrVT, unsigned ReturnReg,
-   unsigned char OperandFlags, bool LocalDynamic = false) {
+static SDValue getTLSADDR(SelectionDAG &DAG, SDValue Chain,
+  GlobalAddressSDNode *GA, SDValue *InGlue,
+  const EVT PtrVT, unsigned ReturnReg,
+  unsigned char OperandFlags, bool UseTLSDESC = false,

phoebewang wrote:

Done. We don't need this arg now.

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Paul Kirth via cfe-commits


@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";

ilovepi wrote:

Is it possible to make this option work uniformly with `trad` and `desc` too?  
The `gnu`/`gnu2` spelling is something we need to support for compatibility, 
but it would be good if we can get this to be consistent in clang. I'm hoping 
we can get GCC to accept the same set of options.

@MaskRay does that sound reasonable to you?

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Paul Kirth via cfe-commits

https://github.com/ilovepi commented:

I assume that's what you're doing in the place I asked for a comment is trying 
to avoid redundantly generating TLSDESC accesses for _TLS_MODULE_BASE_.  I see 
that we don't generate multiple  accesses for _TLS_MODULE_BASE_ in the test 
cases, but I thought that was only safe to do when an earlier access dominates 
a later access.  

Is my understanding here correct, or is there an interaction with  
https://github.com/llvm/llvm-project/blob/04a271ebe4c2421f34a4fbf34c328df9f111/llvm/lib/Target/X86/X86InstrInfo.cpp#L10245
 ?

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Paul Kirth via cfe-commits


@@ -18522,13 +18522,21 @@ GetTLSADDR(SelectionDAG &DAG, SDValue Chain, 
GlobalAddressSDNode *GA,
   MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
   SDLoc dl(GA);
-  SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
-   GA->getValueType(0),
-   GA->getOffset(),
-   OperandFlags);
+  SDValue TGA;
+  bool UseTLSDESC = DAG.getTarget().useTLSDESC();
+  if (LocalDynamic && UseTLSDESC) {
+TGA = DAG.getTargetExternalSymbol("_TLS_MODULE_BASE_", PtrVT, 
OperandFlags);
+auto UI = TGA->use_begin();
+if (UI != TGA->use_end())
+  return SDValue(*UI->use_begin()->use_begin(), 0);

ilovepi wrote:

Can we get a comment here about why we're returning this value?

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Paul Kirth via cfe-commits

https://github.com/ilovepi edited 
https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Roland McGrath via cfe-commits


@@ -0,0 +1,165 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=i686-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-pc-linux-gnux32 --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X32
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X64
+
+@x = thread_local global i32 0, align 4
+@y = internal thread_local global i32 0, align 4

frobtech wrote:

I think it's worthwhile to have test cases that are `dso_local` but not 
internal linkage as well, and mixing the two.  These can and should also use 
`_TLS_MODULE_BASE_` with the secondary `dtpoff` offset after the TLSDESC call 
returns. e.g.
```
extern __thread int x __attribute__((visibility("hidden")));
static __thread int y;
int foo() { return ++x + ++y; }
```

The ideal thing is probably *not* to use `_TLS_MODULE_BASE_` when its result is 
used only once in the function, regardless of the linkage details of the 
referenced TLS symbol.  That's because a second addition with the `dtpoff` 
value will always be needed for that. If it's a case where that addition is 
"free", e.g. because it can be rolled into the displacement of a load you're 
doing anyway, then using `_TLS_MODULE_BASE_` is ideal even if it's the only use 
in the function (because the GOT slot & relocs will be shared with any other 
such use in the whole link).  However, if it requires an extra instruction to 
do the add after the TLSDESC call returns, then it's probably better overall to 
have the separate reloc for just that access.

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Fangrui Song via cfe-commits


@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";

MaskRay wrote:

If GCC is happy to accept `trad` and `desc` aliases, then we can accept the 
aliases; otherwise, I do not want to introduce another way to express the same 
thing.

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Phoebe Wang via cfe-commits

phoebewang wrote:

> I assume that's what you're doing in the place I asked for a comment is 
> trying to avoid redundantly generating TLSDESC accesses for 
> _TLS_MODULE_BASE_. I see that we don't generate multiple accesses for 
> _TLS_MODULE_BASE_ in the test cases, but I thought that was only safe to do 
> when an earlier access dominates a later access.
> 
> Is my understanding here correct, or is there an interaction with
> 
> https://github.com/llvm/llvm-project/blob/04a271ebe4c2421f34a4fbf34c328df9f111/llvm/lib/Target/X86/X86InstrInfo.cpp#L10245
> 
> ?

Yes, you are correct.

I think SelectionDAG takes care of it. I checked it locally, it will generate 
two pairs of `_TLS_MODULE_BASE_` in `if ... else ...` branches respectively and 
one `_TLS_MODULE_BASE_` otherwise.

I admit I didn't know the code here when adding it to GetTLSADDR. But I think 
not to create the nodes at the beginning is slightly better than to remove it 
later if they have the same output. Not to mention dominator iteration is 
expensive.

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Phoebe Wang via cfe-commits

https://github.com/phoebewang updated 
https://github.com/llvm/llvm-project/pull/83136

>From cdc9ee6c322af0ceed162f3f714bcd0a22e020c3 Mon Sep 17 00:00:00 2001
From: Phoebe Wang 
Date: Tue, 27 Feb 2024 22:16:38 +0800
Subject: [PATCH 1/7] [X86] Add Support for X86 TLSDESC Relocations

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp|   3 +-
 clang/test/Driver/tls-dialect.c   |   2 +-
 .../lib/Target/X86/MCTargetDesc/X86BaseInfo.h |  14 ++
 llvm/lib/Target/X86/X86AsmPrinter.cpp |   2 +
 llvm/lib/Target/X86/X86ISelLowering.cpp   |  98 +++
 llvm/lib/Target/X86/X86MCInstLower.cpp|  30 +++-
 llvm/test/CodeGen/X86/tls-desc.ll | 165 ++
 7 files changed, 273 insertions(+), 41 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/tls-desc.ll

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index faceee85a2f8dc..c66e3ee12e50c4 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -740,7 +740,8 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
 SupportedArgument = V == "desc" || V == "trad";
 EnableTLSDESC = V == "desc";
   } else if (Triple.isX86()) {
-SupportedArgument = V == "gnu";
+SupportedArgument = V == "gnu" || V == "gnu2";
+EnableTLSDESC = V == "gnu2";
   } else {
 Unsupported = true;
   }
diff --git a/clang/test/Driver/tls-dialect.c b/clang/test/Driver/tls-dialect.c
index f73915b28ec2a3..a808dd81531ce7 100644
--- a/clang/test/Driver/tls-dialect.c
+++ b/clang/test/Driver/tls-dialect.c
@@ -2,6 +2,7 @@
 // RUN: %clang -### --target=riscv64-linux -mtls-dialect=trad %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
 // RUN: %clang -### --target=riscv64-linux %s 2>&1 | FileCheck 
--check-prefix=NODESC %s
 // RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu %s 2>&1 | 
FileCheck --check-prefix=NODESC %s
+// RUN: %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=DESC %s
 
 /// Android supports TLSDESC by default on RISC-V
 /// TLSDESC is not on by default in Linux, even on RISC-V, and is covered above
@@ -18,7 +19,6 @@
 
 /// Unsupported argument
 // RUN: not %clang -### --target=riscv64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
-// RUN: not %clang -### --target=x86_64-linux -mtls-dialect=gnu2 %s 2>&1 | 
FileCheck --check-prefix=UNSUPPORTED-ARG %s
 
 // DESC:   "-cc1" {{.*}}"-enable-tlsdesc"
 // NODESC-NOT: "-enable-tlsdesc"
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h 
b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
index 4442b80861b61a..1877550f8c40bb 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86BaseInfo.h
@@ -431,6 +431,20 @@ enum TOF {
   /// See 'ELF Handling for Thread-Local Storage' for more details.
   ///SYMBOL_LABEL @TLSLDM
   MO_TLSLDM,
+  /// MO_TLSCALL - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor function for the symbol. Used in both
+  /// the IA32 and x86-64 local dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSCALL
+  MO_TLSCALL,
+  /// MO_TLSDESC - On a symbol operand this indicates that the immediate is
+  /// the index of the TLS descriptor argument for the symbol. When this
+  /// argument is passed to a call getting from index@TLSCALL, the function 
will
+  /// return the offset for the symbol. Used in both the IA32 and x86-64 local
+  /// dynamic TLS access model.
+  /// See 'RFC-TLSDESC-x86' for more details.
+  ///SYMBOL_LABEL @TLSDESC
+  MO_TLSDESC,
   /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
   /// the offset of the GOT entry with the thread-pointer offset for the
   /// symbol. Used in the x86-64 initial exec TLS access model.
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp 
b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 3395a13545e454..d8e111db1cec42 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -271,6 +271,8 @@ void X86AsmPrinter::PrintSymbolOperand(const MachineOperand 
&MO,
   case X86II::MO_TLSGD: O << "@TLSGD"; break;
   case X86II::MO_TLSLD: O << "@TLSLD"; break;
   case X86II::MO_TLSLDM:O << "@TLSLDM";break;
+  case X86II::MO_TLSDESC:   O << "@TLSDESC";   break;
+  case X86II::MO_TLSCALL:   O << "@TLSCALL";   break;
   case X86II::MO_GOTTPOFF:  O << "@GOTTPOFF";  break;
   case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
   case X86II::MO_TPOFF: O << "@TPOFF"; break;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp 
b/llvm/lib/Target/X86/X86ISelLowering.cpp
index a86f13135173b0..88314bcf510e9a 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -18515,17 +18515,17 @@ X86TargetLowering::LowerGlobalAddress(SDValue Op, 
SelectionDAG &DAG) con

[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-06 Thread Phoebe Wang via cfe-commits


@@ -0,0 +1,165 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=i686-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-pc-linux-gnux32 --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X32
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown --relocation-model=pic 
-enable-tlsdesc | FileCheck %s --check-prefix=X64
+
+@x = thread_local global i32 0, align 4
+@y = internal thread_local global i32 0, align 4

phoebewang wrote:

Added in `f5`.

> The ideal thing is probably not to use `_TLS_MODULE_BASE_` when its result is 
> used only once in the function

I think it is demonstrated by `f3`?

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-07 Thread via cfe-commits


@@ -18543,7 +18552,19 @@ GetTLSADDR(SelectionDAG &DAG, SDValue Chain, 
GlobalAddressSDNode *GA,
   MFI.setHasCalls(true);
 
   SDValue Glue = Chain.getValue(1);
-  return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
+  SDValue Ret = DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
+
+  if (!UseTLSDESC)
+return Ret;
+
+  const X86Subtarget &Subtarget = DAG.getSubtarget();
+  unsigned Seg = Subtarget.is64Bit() ? X86AS::FS : X86AS::GS;
+
+  Value *Ptr = Constant::getNullValue(PointerType::get(*DAG.getContext(), 
Seg));
+  SDValue Offset =
+  DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), DAG.getIntPtrConstant(0, dl),

goldsteinn wrote:

Why add `0`?

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-07 Thread Phoebe Wang via cfe-commits


@@ -18522,13 +18522,21 @@ GetTLSADDR(SelectionDAG &DAG, SDValue Chain, 
GlobalAddressSDNode *GA,
   MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
   SDLoc dl(GA);
-  SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
-   GA->getValueType(0),
-   GA->getOffset(),
-   OperandFlags);
+  SDValue TGA;
+  bool UseTLSDESC = DAG.getTarget().useTLSDESC();
+  if (LocalDynamic && UseTLSDESC) {
+TGA = DAG.getTargetExternalSymbol("_TLS_MODULE_BASE_", PtrVT, 
OperandFlags);
+auto UI = TGA->use_begin();
+if (UI != TGA->use_end())
+  return SDValue(*UI->use_begin()->use_begin(), 0);

phoebewang wrote:

Done.

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-07 Thread Phoebe Wang via cfe-commits


@@ -18543,7 +18552,19 @@ GetTLSADDR(SelectionDAG &DAG, SDValue Chain, 
GlobalAddressSDNode *GA,
   MFI.setHasCalls(true);
 
   SDValue Glue = Chain.getValue(1);
-  return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
+  SDValue Ret = DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Glue);
+
+  if (!UseTLSDESC)
+return Ret;
+
+  const X86Subtarget &Subtarget = DAG.getSubtarget();
+  unsigned Seg = Subtarget.is64Bit() ? X86AS::FS : X86AS::GS;
+
+  Value *Ptr = Constant::getNullValue(PointerType::get(*DAG.getContext(), 
Seg));
+  SDValue Offset =
+  DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), DAG.getIntPtrConstant(0, dl),

phoebewang wrote:

Quote from https://www.fsfla.org/~lxoliva/writeups/TLS/RFC-TLSDESC-x86.txt

> An alternate design in which the function called through the TLS
descriptor returns not the TP offset, but rather the address of the
variable of interest, could refrain from adding %gs:0 to the value
returned by the call to compute the address of a symbol, and from
using the %gs: prefix when accessing the variable, but it would
require the use of a longer call instruction to enable proper
relaxation.  The call instruction would have to be 7, instead of 2
bytes long, such that the linker could relax it to `addl %gs:0, %eax'.
This would make code that accesses the variable 4 bytes longer on
average (5 bytes minus one used by the %gs prefix), whereas code that
computes its address would be shorter by only two bytes.  It's not
clear such a change would be profitable.

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [X86] Add Support for X86 TLSDESC Relocations (PR #83136)

2024-03-10 Thread Phoebe Wang via cfe-commits

phoebewang wrote:

Gentle ping~

https://github.com/llvm/llvm-project/pull/83136
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits