Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[LoongArch] Emit error messages when using emulated TLS #92483

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

wangleiat
Copy link
Contributor

@wangleiat wangleiat commented May 17, 2024

No description provided.

Created using spr 1.3.5-bogner
@llvmbot
Copy link
Collaborator

llvmbot commented May 17, 2024

@llvm/pr-subscribers-backend-loongarch

Author: wanglei (wangleiat)

Changes

Some developers are currently porting OpenHOS to LoongArch, which
requires support for emulated TLS. We should support it like RISC-V.


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

2 Files Affected:

  • (modified) llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp (+3)
  • (added) llvm/test/CodeGen/LoongArch/emutls.ll (+133)
diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
index fe2c613b1b30f..0b22ba50ee30f 100644
--- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
@@ -934,6 +934,9 @@ LoongArchTargetLowering::lowerGlobalTLSAddress(SDValue Op,
   GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
   assert(N->getOffset() == 0 && "unexpected offset in global node");
 
+  if (DAG.getTarget().useEmulatedTLS())
+    return LowerToTLSEmulatedModel(N, DAG);
+
   bool IsDesc = DAG.getTarget().useTLSDESC();
 
   switch (getTargetMachine().getTLSModel(N->getGlobal())) {
diff --git a/llvm/test/CodeGen/LoongArch/emutls.ll b/llvm/test/CodeGen/LoongArch/emutls.ll
new file mode 100644
index 0000000000000..56ec8e3715f38
--- /dev/null
+++ b/llvm/test/CodeGen/LoongArch/emutls.ll
@@ -0,0 +1,133 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
+; RUN: llc --mtriple=loongarch32 -emulated-tls -relocation-model=pic < %s \
+; RUN:     | FileCheck -check-prefix=LA32 %s
+; RUN: llc --mtriple=loongarch64 -emulated-tls -relocation-model=pic < %s \
+; RUN:     | FileCheck -check-prefix=LA64 %s
+
+@external_x = external thread_local global i32, align 8
+@y = thread_local global i8 7, align 2
+@internal_z = internal thread_local global i64 9, align 16
+
+define ptr @get_external_x() nounwind {
+; LA32-LABEL: get_external_x:
+; LA32:       # %bb.0: # %entry
+; LA32-NEXT:    addi.w $sp, $sp, -16
+; LA32-NEXT:    st.w $ra, $sp, 12 # 4-byte Folded Spill
+; LA32-NEXT:    pcalau12i $a0, %got_pc_hi20(__emutls_v.external_x)
+; LA32-NEXT:    ld.w $a0, $a0, %got_pc_lo12(__emutls_v.external_x)
+; LA32-NEXT:    bl %plt(__emutls_get_address)
+; LA32-NEXT:    ld.w $ra, $sp, 12 # 4-byte Folded Reload
+; LA32-NEXT:    addi.w $sp, $sp, 16
+; LA32-NEXT:    ret
+;
+; LA64-LABEL: get_external_x:
+; LA64:       # %bb.0: # %entry
+; LA64-NEXT:    addi.d $sp, $sp, -16
+; LA64-NEXT:    st.d $ra, $sp, 8 # 8-byte Folded Spill
+; LA64-NEXT:    pcalau12i $a0, %got_pc_hi20(__emutls_v.external_x)
+; LA64-NEXT:    ld.d $a0, $a0, %got_pc_lo12(__emutls_v.external_x)
+; LA64-NEXT:    bl %plt(__emutls_get_address)
+; LA64-NEXT:    ld.d $ra, $sp, 8 # 8-byte Folded Reload
+; LA64-NEXT:    addi.d $sp, $sp, 16
+; LA64-NEXT:    ret
+entry:
+  ret ptr @external_x
+}
+
+define ptr @get_y() nounwind {
+; LA32-LABEL: get_y:
+; LA32:       # %bb.0: # %entry
+; LA32-NEXT:    addi.w $sp, $sp, -16
+; LA32-NEXT:    st.w $ra, $sp, 12 # 4-byte Folded Spill
+; LA32-NEXT:    pcalau12i $a0, %got_pc_hi20(__emutls_v.y)
+; LA32-NEXT:    ld.w $a0, $a0, %got_pc_lo12(__emutls_v.y)
+; LA32-NEXT:    bl %plt(__emutls_get_address)
+; LA32-NEXT:    ld.w $ra, $sp, 12 # 4-byte Folded Reload
+; LA32-NEXT:    addi.w $sp, $sp, 16
+; LA32-NEXT:    ret
+;
+; LA64-LABEL: get_y:
+; LA64:       # %bb.0: # %entry
+; LA64-NEXT:    addi.d $sp, $sp, -16
+; LA64-NEXT:    st.d $ra, $sp, 8 # 8-byte Folded Spill
+; LA64-NEXT:    pcalau12i $a0, %got_pc_hi20(__emutls_v.y)
+; LA64-NEXT:    ld.d $a0, $a0, %got_pc_lo12(__emutls_v.y)
+; LA64-NEXT:    bl %plt(__emutls_get_address)
+; LA64-NEXT:    ld.d $ra, $sp, 8 # 8-byte Folded Reload
+; LA64-NEXT:    addi.d $sp, $sp, 16
+; LA64-NEXT:    ret
+entry:
+  ret ptr @y
+}
+
+define ptr @get_internal_z() nounwind {
+; LA32-LABEL: get_internal_z:
+; LA32:       # %bb.0: # %entry
+; LA32-NEXT:    addi.w $sp, $sp, -16
+; LA32-NEXT:    st.w $ra, $sp, 12 # 4-byte Folded Spill
+; LA32-NEXT:    pcalau12i $a0, %pc_hi20(__emutls_v.internal_z)
+; LA32-NEXT:    addi.w $a0, $a0, %pc_lo12(__emutls_v.internal_z)
+; LA32-NEXT:    bl %plt(__emutls_get_address)
+; LA32-NEXT:    ld.w $ra, $sp, 12 # 4-byte Folded Reload
+; LA32-NEXT:    addi.w $sp, $sp, 16
+; LA32-NEXT:    ret
+;
+; LA64-LABEL: get_internal_z:
+; LA64:       # %bb.0: # %entry
+; LA64-NEXT:    addi.d $sp, $sp, -16
+; LA64-NEXT:    st.d $ra, $sp, 8 # 8-byte Folded Spill
+; LA64-NEXT:    pcalau12i $a0, %pc_hi20(__emutls_v.internal_z)
+; LA64-NEXT:    addi.d $a0, $a0, %pc_lo12(__emutls_v.internal_z)
+; LA64-NEXT:    bl %plt(__emutls_get_address)
+; LA64-NEXT:    ld.d $ra, $sp, 8 # 8-byte Folded Reload
+; LA64-NEXT:    addi.d $sp, $sp, 16
+; LA64-NEXT:    ret
+entry:
+  ret ptr @internal_z
+}
+
+; UTC_ARGS: --disable
+
+; LA32:        .data
+; LA32:        .globl __emutls_v.y
+; LA32:        .p2align 2
+; LA32-LABEL:  __emutls_v.y:
+; LA32-NEXT:     .word 1
+; LA32-NEXT:     .word 2
+; LA32-NEXT:     .word 0
+; LA32-NEXT:     .word __emutls_t.y
+; LA32:        .section .rodata,
+; LA32-LABEL:  __emutls_t.y:
+; LA32-NEXT:     .byte 7
+; LA32:        .data
+; LA32:        .p2align 2
+; LA32-LABEL:  __emutls_v.internal_z:
+; LA32-NEXT:     .word 8
+; LA32-NEXT:     .word 16
+; LA32-NEXT:     .word 0
+; LA32-NEXT:     .word __emutls_t.internal_z
+; LA32:        .section .rodata,
+; LA32-LABEL:  __emutls_t.internal_z:
+; LA32-NEXT:     .dword 9
+
+; LA64:        .data
+; LA64:        .globl __emutls_v.y
+; LA64:        .p2align 3
+; LA64-LABEL:  __emutls_v.y:
+; LA64-NEXT:     .dword 1
+; LA64-NEXT:     .dword 2
+; LA64-NEXT:     .dword 0
+; LA64-NEXT:     .dword __emutls_t.y
+; LA64:        .section .rodata,
+; LA64-LABEL:  __emutls_t.y:
+; LA64-NEXT:     .byte 7
+; LA64:        .data
+; LA64:        .p2align 3
+; LA64-LABEL:  __emutls_v.internal_z:
+; LA64-NEXT:     .dword 8
+; LA64-NEXT:     .dword 16
+; LA64-NEXT:     .dword 0
+; LA64-NEXT:     .dword __emutls_t.internal_z
+; LA64:        .section .rodata,
+; LA64-LABEL:  __emutls_t.internal_z:
+; LA64-NEXT:     .dword 9

@wangleiat
Copy link
Contributor Author

Hi @MaskRay
Previous discussion: https://reviews.llvm.org/D141183. Despite the numerous drawbacks of emulated TLS, there is still some demand for it. If you still insist that the LoongArch backend should not support emulated TLS, then we should at least provide users with clear warnings either in the frontend or during the downgrading process.
Perhaps like this:

if (DAG.getTarget().useEmulatedTLS())
  report_fatal_error("The emulated TLS is prohibited");

Otherwise, this issue will be reported as 'some symbols undefined' errors during the linking phase, which can be very confusing.

@wangleiat
Copy link
Contributor Author

@xen0n

@xen0n
Copy link
Contributor

xen0n commented May 23, 2024

Can we (and preferably also the OHOS porters) sort out and try to remove the emulated TLS usage in the OHOS codebase instead? At least we would know exactly why it is necessary if it eventually turns out to be the case.

@SixWeining
Copy link
Contributor

@yetist may be interested in this PR.

@SixWeining
Copy link
Contributor

Can we (and preferably also the OHOS porters) sort out and try to remove the emulated TLS usage in the OHOS codebase instead? At least we would know exactly why it is necessary if it eventually turns out to be the case.

I heard that currently some services in OHOS use coroutine which depends on emulated TLS, but I don't know the details. I also heard they plan to remove the dependency and switch to ELF TLS in future versions.

So I prefer reporting error in somewhere (ether in clang or backend) if emulated TLS is used on LoongArch.

Created using spr 1.3.5-bogner
@wangleiat wangleiat changed the title [LoongArch] Support emulated TLS [LoongArch] Emit error messages when using emulated TLS Jun 6, 2024
@wangleiat
Copy link
Contributor Author

Hi all:
How about emitting an error message like this?

Copy link
Contributor

@SixWeining SixWeining left a comment

Choose a reason for hiding this comment

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

LGTM

@MaskRay
Copy link
Member

MaskRay commented Jun 6, 2024

Instead of an error for each TLS instance, emit it in TargetMachine ?

Created using spr 1.3.5-bogner
@wangleiat
Copy link
Contributor Author

Instead of an error for each TLS instance, emit it in TargetMachine ?

I didn't find a way to emit an error message and return the error to the error handler. Either use report_fatal_error ?

Created using spr 1.3.5-bogner
@MaskRay
Copy link
Member

MaskRay commented Jun 7, 2024

Instead of an error for each TLS instance, emit it in TargetMachine ?

I didn't find a way to emit an error message and return the error to the error handler. Either use report_fatal_error ?

Seems fine. report_fatal_error with false GenCrashDiag?

@wangleiat
Copy link
Contributor Author

Instead of an error for each TLS instance, emit it in TargetMachine ?

I didn't find a way to emit an error message and return the error to the error handler. Either use report_fatal_error ?

Seems fine. report_fatal_error with false GenCrashDiag?

Does adding such test cases increase the testing time if useing report_fatal_error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants