[lld] [llvm] [llvm][lld] Pre-commit tests for RISCV TLSDESC symbols (PR #85816)

Paul Kirth via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 19 16:15:40 PDT 2024


https://github.com/ilovepi updated https://github.com/llvm/llvm-project/pull/85816

>From 6a944f767aa6283f13f7e0379f71d58422240244 Mon Sep 17 00:00:00 2001
From: Paul Kirth <paulkirth at google.com>
Date: Tue, 19 Mar 2024 16:42:23 +0000
Subject: [PATCH 1/3] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.4
---
 lld/test/ELF/riscv-tlsdesc.s              | 37 +++++++++++++++++++++++
 llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll | 22 ++++++++++++++
 2 files changed, 59 insertions(+)
 create mode 100644 llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll

diff --git a/lld/test/ELF/riscv-tlsdesc.s b/lld/test/ELF/riscv-tlsdesc.s
index 1738f86256caa6..a893d098344d0a 100644
--- a/lld/test/ELF/riscv-tlsdesc.s
+++ b/lld/test/ELF/riscv-tlsdesc.s
@@ -29,6 +29,12 @@
 # RUN: ld.lld -e 0 -z now a.32.o c.32.so -o a.32.ie
 # RUN: llvm-objdump --no-show-raw-insn -M no-aliases -h -d a.32.ie | FileCheck %s --check-prefix=IE32
 
+# RUN: llvm-mc -triple=riscv64 --position-independent -filetype=obj d.s -o d.64.o
+# RUN: not ld.lld -shared -soname=libd.64.so -o libd.64.so d.64.o 2>&1 | FileCheck %s --check-prefix=BADTLSLABEL
+
+# RUN: llvm-mc -triple=riscv32 --position-independent -filetype=obj d.s -o d.32.o
+# RUN: not ld.lld -shared -soname=libd.32.so -o libd.32.so d.32.o 2>&1 | FileCheck %s --check-prefix=BADTLSLABEL
+
 # GD64-RELA:      .rela.dyn {
 # GD64-RELA-NEXT:   0x2408 R_RISCV_TLSDESC - 0x7FF
 # GD64-RELA-NEXT:   0x23E8 R_RISCV_TLSDESC a 0x0
@@ -150,6 +156,9 @@
 # IE32-NEXT:         lw      a0, 0x80(a0)
 # IE32-NEXT:         add     a0, a0, tp
 
+## FIXME This should not pass, but the code MC layer needs a fix to prevent this.
+# BADTLSLABEL: error: d.{{.*}}.o has an STT_TLS symbol but doesn't have an SHF_TLS section
+
 #--- a.s
 .macro load dst, src
 .ifdef ELF32
@@ -192,3 +201,31 @@ b:
 .tbss
 .globl c
 c: .zero 4
+
+#--- d.s
+  .text
+  .attribute	4, 16
+  .attribute	5, "rv64i2p1_m2p0_a2p1_c2p0"
+  .globl	bar
+  .p2align	1
+  .type	bar, at function
+bar:
+  addi	sp, sp, -16
+.Lpcrel_hi0:
+  auipc	a0, %got_pcrel_hi(_ZTH3foo)
+  ld	a0, %pcrel_lo(.Lpcrel_hi0)(a0)
+  beqz	a0, .Ltlsdesc_hi0
+  call	_ZTH3foo
+.Ltlsdesc_hi0:
+  auipc	a0, %tlsdesc_hi(foo)
+  ld	a1, %tlsdesc_load_lo(.Ltlsdesc_hi0)(a0)
+  addi	a0, a0, %tlsdesc_add_lo(.Ltlsdesc_hi0)
+  jalr	t0, 0(a1), %tlsdesc_call(.Ltlsdesc_hi0)
+  add	a1, a0, tp
+  lw	a0, 0(a1)
+  addiw	a0, a0, 1
+  sw	a0, 0(a1)
+  addi	sp, sp, 16
+  ret
+
+.weak _ZTH3foo
diff --git a/llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll b/llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll
new file mode 100644
index 00000000000000..4fb88b33aae516
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll
@@ -0,0 +1,22 @@
+; RUN: llc -mtriple=riscv64 -relocation-model=pic -enable-tlsdesc < %s \
+; RUN:     | llvm-mc -triple=riscv64 -filetype=obj -o %t.o --position-independent \
+; RUN:     | llvm-readelf --symbols %t.o \
+; RUN:     | FileCheck %s
+
+; RUN: llc -mtriple=riscv32 -relocation-model=pic -enable-tlsdesc < %s \
+; RUN:     | llvm-mc -triple=riscv32 -filetype=obj -o %t.o --position-independent \
+; RUN:     | llvm-readelf --symbols %t.o \
+; RUN:     | FileCheck %s
+
+; Check that TLS symbols are lowered correctly based on the specified
+; model. Make sure they're external to avoid them all being optimised to Local
+; Exec for the executable.
+
+ at unspecified = external thread_local global i32
+
+define ptr @f1() nounwind {
+entry:
+  ret ptr @unspecified
+  ; CHECK: Symbol table '.symtab' contains 7 entries:
+  ; CHECK: TLS {{.*}} unspecified
+}

>From daf3ea0978b84896490876da886fe6cff039ea28 Mon Sep 17 00:00:00 2001
From: Paul Kirth <paulkirth at google.com>
Date: Tue, 19 Mar 2024 20:08:39 +0000
Subject: [PATCH 2/3] Address comments, update names, and use stdout

Created using spr 1.3.4
---
 lld/test/ELF/riscv-tlsdesc.s              | 36 ++++++++---------------
 llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll |  8 ++---
 2 files changed, 16 insertions(+), 28 deletions(-)

diff --git a/lld/test/ELF/riscv-tlsdesc.s b/lld/test/ELF/riscv-tlsdesc.s
index a893d098344d0a..7b5737eeaf049e 100644
--- a/lld/test/ELF/riscv-tlsdesc.s
+++ b/lld/test/ELF/riscv-tlsdesc.s
@@ -30,10 +30,10 @@
 # RUN: llvm-objdump --no-show-raw-insn -M no-aliases -h -d a.32.ie | FileCheck %s --check-prefix=IE32
 
 # RUN: llvm-mc -triple=riscv64 --position-independent -filetype=obj d.s -o d.64.o
-# RUN: not ld.lld -shared -soname=libd.64.so -o libd.64.so d.64.o 2>&1 | FileCheck %s --check-prefix=BADTLSLABEL
+# RUN: not ld.lld -shared -soname=d.64.so -o d.64.so d.64.o 2>&1 | FileCheck %s --check-prefix=BADTLSLABEL
 
-# RUN: llvm-mc -triple=riscv32 --position-independent -filetype=obj d.s -o d.32.o
-# RUN: not ld.lld -shared -soname=libd.32.so -o libd.32.so d.32.o 2>&1 | FileCheck %s --check-prefix=BADTLSLABEL
+# RUN: llvm-mc -triple=riscv32 --position-independent -filetype=obj d.s -o d.32.o --defsym ELF32=1
+# RUN: not ld.lld -shared -soname=d.32.so -o d.32.so d.32.o 2>&1 | FileCheck %s --check-prefix=BADTLSLABEL
 
 # GD64-RELA:      .rela.dyn {
 # GD64-RELA-NEXT:   0x2408 R_RISCV_TLSDESC - 0x7FF
@@ -203,29 +203,17 @@ b:
 c: .zero 4
 
 #--- d.s
-  .text
-  .attribute	4, 16
-  .attribute	5, "rv64i2p1_m2p0_a2p1_c2p0"
-  .globl	bar
-  .p2align	1
-  .type	bar, at function
-bar:
-  addi	sp, sp, -16
-.Lpcrel_hi0:
-  auipc	a0, %got_pcrel_hi(_ZTH3foo)
-  ld	a0, %pcrel_lo(.Lpcrel_hi0)(a0)
-  beqz	a0, .Ltlsdesc_hi0
-  call	_ZTH3foo
+.macro load dst, src
+.ifdef ELF32
+lw \dst, \src
+.else
+ld \dst, \src
+.endif
+.endm
+
 .Ltlsdesc_hi0:
   auipc	a0, %tlsdesc_hi(foo)
-  ld	a1, %tlsdesc_load_lo(.Ltlsdesc_hi0)(a0)
+  load	a1, %tlsdesc_load_lo(.Ltlsdesc_hi0)(a0)
   addi	a0, a0, %tlsdesc_add_lo(.Ltlsdesc_hi0)
   jalr	t0, 0(a1), %tlsdesc_call(.Ltlsdesc_hi0)
   add	a1, a0, tp
-  lw	a0, 0(a1)
-  addiw	a0, a0, 1
-  sw	a0, 0(a1)
-  addi	sp, sp, 16
-  ret
-
-.weak _ZTH3foo
diff --git a/llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll b/llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll
index 4fb88b33aae516..a04e01b91c18dc 100644
--- a/llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll
+++ b/llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll
@@ -1,11 +1,11 @@
 ; RUN: llc -mtriple=riscv64 -relocation-model=pic -enable-tlsdesc < %s \
-; RUN:     | llvm-mc -triple=riscv64 -filetype=obj -o %t.o --position-independent \
-; RUN:     | llvm-readelf --symbols %t.o \
+; RUN:     | llvm-mc -triple=riscv64 -filetype=obj -o - --position-independent \
+; RUN:     | llvm-readelf --symbols - \
 ; RUN:     | FileCheck %s
 
 ; RUN: llc -mtriple=riscv32 -relocation-model=pic -enable-tlsdesc < %s \
-; RUN:     | llvm-mc -triple=riscv32 -filetype=obj -o %t.o --position-independent \
-; RUN:     | llvm-readelf --symbols %t.o \
+; RUN:     | llvm-mc -triple=riscv32 -filetype=obj -o - --position-independent \
+; RUN:     | llvm-readelf --symbols - \
 ; RUN:     | FileCheck %s
 
 ; Check that TLS symbols are lowered correctly based on the specified

>From 2ea78dc1da5c79a13f8a715dfcb70dc086040049 Mon Sep 17 00:00:00 2001
From: Paul Kirth <paulkirth at google.com>
Date: Tue, 19 Mar 2024 23:15:26 +0000
Subject: [PATCH 3/3] Remove `--position-independent`

Created using spr 1.3.4
---
 lld/test/ELF/riscv-tlsdesc.s              | 4 ++--
 llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lld/test/ELF/riscv-tlsdesc.s b/lld/test/ELF/riscv-tlsdesc.s
index 7b5737eeaf049e..c583e15cf30ce3 100644
--- a/lld/test/ELF/riscv-tlsdesc.s
+++ b/lld/test/ELF/riscv-tlsdesc.s
@@ -29,10 +29,10 @@
 # RUN: ld.lld -e 0 -z now a.32.o c.32.so -o a.32.ie
 # RUN: llvm-objdump --no-show-raw-insn -M no-aliases -h -d a.32.ie | FileCheck %s --check-prefix=IE32
 
-# RUN: llvm-mc -triple=riscv64 --position-independent -filetype=obj d.s -o d.64.o
+# RUN: llvm-mc -triple=riscv64 -filetype=obj d.s -o d.64.o
 # RUN: not ld.lld -shared -soname=d.64.so -o d.64.so d.64.o 2>&1 | FileCheck %s --check-prefix=BADTLSLABEL
 
-# RUN: llvm-mc -triple=riscv32 --position-independent -filetype=obj d.s -o d.32.o --defsym ELF32=1
+# RUN: llvm-mc -triple=riscv32 -filetype=obj d.s -o d.32.o --defsym ELF32=1
 # RUN: not ld.lld -shared -soname=d.32.so -o d.32.so d.32.o 2>&1 | FileCheck %s --check-prefix=BADTLSLABEL
 
 # GD64-RELA:      .rela.dyn {
diff --git a/llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll b/llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll
index a04e01b91c18dc..3e80fefb771fca 100644
--- a/llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll
+++ b/llvm/test/CodeGen/RISCV/tlsdesc-symbol.ll
@@ -1,10 +1,10 @@
 ; RUN: llc -mtriple=riscv64 -relocation-model=pic -enable-tlsdesc < %s \
-; RUN:     | llvm-mc -triple=riscv64 -filetype=obj -o - --position-independent \
+; RUN:     | llvm-mc -triple=riscv64 -filetype=obj -o - \
 ; RUN:     | llvm-readelf --symbols - \
 ; RUN:     | FileCheck %s
 
 ; RUN: llc -mtriple=riscv32 -relocation-model=pic -enable-tlsdesc < %s \
-; RUN:     | llvm-mc -triple=riscv32 -filetype=obj -o - --position-independent \
+; RUN:     | llvm-mc -triple=riscv32 -filetype=obj -o - \
 ; RUN:     | llvm-readelf --symbols - \
 ; RUN:     | FileCheck %s
 



More information about the llvm-commits mailing list