[llvm] [lli] Honor explicit emulated TLS option in ORC mode (PR #217554)

Jiaxun Yang via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 15 13:48:49 PDT 2026


https://github.com/FlyGoat updated https://github.com/llvm/llvm-project/pull/217554

>From 927a7791d4f0b0750adfbc5fe8eb218c2be93234 Mon Sep 17 00:00:00 2001
From: Jiaxun Yang <jiaxun.yang at flygoat.com>
Date: Thu, 20 Aug 2026 09:32:04 +0100
Subject: [PATCH 1/3] [lli] Honor explicit emulated TLS option in ORC mode

Forward an explicitly specified -emulated-tls value to the ORC JITTargetMachineBuilder. This preserves the builder default when the option is omitted while allowing callers to request native TLS lowering.
---
 llvm/test/ExecutionEngine/OrcLazy/emulated-tls.ll | 8 ++++++--
 llvm/tools/lli/lli.cpp                            | 4 ++++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/llvm/test/ExecutionEngine/OrcLazy/emulated-tls.ll b/llvm/test/ExecutionEngine/OrcLazy/emulated-tls.ll
index a70bc5f4ceb42..4c1b23201f9a0 100644
--- a/llvm/test/ExecutionEngine/OrcLazy/emulated-tls.ll
+++ b/llvm/test/ExecutionEngine/OrcLazy/emulated-tls.ll
@@ -2,7 +2,9 @@
 ; UNSUPPORTED: target=loongarch{{.*}}
 
 ; RUN: not lli -no-process-syms -lljit-platform=Inactive -emulated-tls \
-; RUN:   -jit-kind=orc-lazy %s 2>&1 | FileCheck %s
+; RUN:   -jit-kind=orc-lazy %s 2>&1 | FileCheck %s --check-prefix=EMULATED
+; RUN: not lli -no-process-syms -lljit-platform=Inactive -emulated-tls=false \
+; RUN:   -jit-kind=orc-lazy %s 2>&1 | FileCheck %s --check-prefix=NATIVE
 ;
 ; Test that emulated-tls does not generate any unexpected errors.
 ;
@@ -15,7 +17,9 @@
 ; tls lowering was applied, and (2) that thread locals defined in the JIT'd code
 ; were otherwise handled correctly.
 
-; CHECK: JIT session error: Symbols not found: [ {{[^,]*}}__emutls_get_address ]
+; EMULATED: JIT session error: Symbols not found: [ {{[^,]*}}__emutls_get_address ]
+; NATIVE-NOT: __emutls_get_address
+; NATIVE: JIT session error:
 
 @x = thread_local global i32 42, align 4
 
diff --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp
index 9cafbcf104921..6bee74935a115 100644
--- a/llvm/tools/lli/lli.cpp
+++ b/llvm/tools/lli/lli.cpp
@@ -971,6 +971,10 @@ static int runOrcJIT(const char *ProgName) {
       .setRelocationModel(codegen::getExplicitRelocModel())
       .setCodeModel(codegen::getExplicitCodeModel());
 
+  if (auto EmulatedTLS = codegen::getExplicitEmulatedTLS())
+    Builder.getJITTargetMachineBuilder()->getOptions().EmulatedTLS =
+        *EmulatedTLS;
+
   // Link process symbols unless NoProcessSymbols is set.
   Builder.setLinkProcessSymbolsByDefault(!NoProcessSymbols);
 

>From 6390bd8f6d644103aacb230bfbca53a64491de17 Mon Sep 17 00:00:00 2001
From: Jiaxun Yang <jiaxun.yang at flygoat.com>
Date: Tue, 15 Sep 2026 19:19:11 +0100
Subject: [PATCH 2/3] [RuntimeDyld] Report TLS allocation failures without
 aborting

Return a recoverable error when TLS allocation is unsupported or fails. Exercise the RuntimeDyld path explicitly in the ORC TLS test so hosts that default to JITLink also cover it.

Rename the test to tls.ll and record the planned replacement with successful TLS execution tests using the new ORC runtime.
---
 llvm/include/llvm/ExecutionEngine/RuntimeDyld.h        |  1 +
 llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp   |  5 ++++-
 .../OrcLazy/{emulated-tls.ll => tls.ll}                | 10 +++++++++-
 3 files changed, 14 insertions(+), 2 deletions(-)
 rename llvm/test/ExecutionEngine/OrcLazy/{emulated-tls.ll => tls.ll} (71%)

diff --git a/llvm/include/llvm/ExecutionEngine/RuntimeDyld.h b/llvm/include/llvm/ExecutionEngine/RuntimeDyld.h
index 57387f029b6cf..aec57b7795f4a 100644
--- a/llvm/include/llvm/ExecutionEngine/RuntimeDyld.h
+++ b/llvm/include/llvm/ExecutionEngine/RuntimeDyld.h
@@ -123,6 +123,7 @@ class RuntimeDyld {
 
     /// Allocate a memory block of (at least) the given size to be used for
     /// thread-local storage (TLS).
+    /// Return a null InitializationImage if allocation is unsupported or fails.
     virtual TLSSection allocateTLSSection(uintptr_t Size, unsigned Alignment,
                                           unsigned SectionID,
                                           StringRef SectionName);
diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
index 9f8c1dd87c76a..252506ef22853 100644
--- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
+++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyld.cpp
@@ -864,6 +864,9 @@ RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
     if (IsTLS) {
       auto TLSSection = MemMgr.allocateTLSSection(Allocate, Alignment.value(),
                                                   SectionID, Name);
+      if (!TLSSection.InitializationImage)
+        return make_error<RuntimeDyldError>(
+            "Unable to allocate TLS section memory");
       Addr = TLSSection.InitializationImage;
       LoadAddress = TLSSection.Offset;
     } else if (IsCode) {
@@ -1299,7 +1302,7 @@ RuntimeDyld::MemoryManager::allocateTLSSection(uintptr_t Size,
                                                unsigned Alignment,
                                                unsigned SectionID,
                                                StringRef SectionName) {
-  report_fatal_error("allocation of TLS not implemented");
+  return {};
 }
 
 void RuntimeDyld::MemoryManager::anchor() {}
diff --git a/llvm/test/ExecutionEngine/OrcLazy/emulated-tls.ll b/llvm/test/ExecutionEngine/OrcLazy/tls.ll
similarity index 71%
rename from llvm/test/ExecutionEngine/OrcLazy/emulated-tls.ll
rename to llvm/test/ExecutionEngine/OrcLazy/tls.ll
index 4c1b23201f9a0..785c1508f364d 100644
--- a/llvm/test/ExecutionEngine/OrcLazy/emulated-tls.ll
+++ b/llvm/test/ExecutionEngine/OrcLazy/tls.ll
@@ -5,8 +5,14 @@
 ; RUN:   -jit-kind=orc-lazy %s 2>&1 | FileCheck %s --check-prefix=EMULATED
 ; RUN: not lli -no-process-syms -lljit-platform=Inactive -emulated-tls=false \
 ; RUN:   -jit-kind=orc-lazy %s 2>&1 | FileCheck %s --check-prefix=NATIVE
+; RUN: %if system-linux %{ not lli -no-process-syms -lljit-platform=Inactive \
+; RUN:   -emulated-tls=false -jit-kind=orc-lazy -jit-linker=rtdyld %s 2>&1 \
+; RUN:   | FileCheck %s --check-prefix=RTDYLD %}
 ;
-; Test that emulated-tls does not generate any unexpected errors.
+; Test that emulated and native TLS lowering produce the expected errors.
+;
+; TODO: Replace this test with positive tests of successful TLS execution using
+; the new ORC runtime once it supports them.
 ;
 ; Unfortunately we cannot test successful execution of JIT'd code with
 ; emulated-tls as this would require the JIT itself, in this case lli, to be
@@ -20,6 +26,8 @@
 ; EMULATED: JIT session error: Symbols not found: [ {{[^,]*}}__emutls_get_address ]
 ; NATIVE-NOT: __emutls_get_address
 ; NATIVE: JIT session error:
+; NATIVE-NOT: __emutls_get_address
+; RTDYLD: JIT session error: Unable to allocate TLS section memory
 
 @x = thread_local global i32 42, align 4
 

>From c99dd3cd66926441fe10f591599b9d49734547c9 Mon Sep 17 00:00:00 2001
From: Jiaxun Yang <jiaxun.yang at flygoat.com>
Date: Tue, 15 Sep 2026 21:46:32 +0100
Subject: [PATCH 3/3] [lli] Use small code model for RuntimeDyld TLS test

AArch64 defaults to the large code model when using RuntimeDyld, but general-dynamic ELF TLS lowering does not support that model. Select the small code model explicitly so the test reaches the TLS allocation error it is intended to check.
---
 llvm/test/ExecutionEngine/OrcLazy/tls.ll | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/test/ExecutionEngine/OrcLazy/tls.ll b/llvm/test/ExecutionEngine/OrcLazy/tls.ll
index 785c1508f364d..272a6ecdaee75 100644
--- a/llvm/test/ExecutionEngine/OrcLazy/tls.ll
+++ b/llvm/test/ExecutionEngine/OrcLazy/tls.ll
@@ -6,10 +6,13 @@
 ; RUN: not lli -no-process-syms -lljit-platform=Inactive -emulated-tls=false \
 ; RUN:   -jit-kind=orc-lazy %s 2>&1 | FileCheck %s --check-prefix=NATIVE
 ; RUN: %if system-linux %{ not lli -no-process-syms -lljit-platform=Inactive \
-; RUN:   -emulated-tls=false -jit-kind=orc-lazy -jit-linker=rtdyld %s 2>&1 \
+; RUN:   -emulated-tls=false -jit-kind=orc-lazy -jit-linker=rtdyld \
+; RUN:   -code-model=small %s 2>&1 \
 ; RUN:   | FileCheck %s --check-prefix=RTDYLD %}
 ;
 ; Test that emulated and native TLS lowering produce the expected errors.
+; Use the small code model for RuntimeDyld: AArch64's large code model does not
+; support general-dynamic TLS lowering.
 ;
 ; TODO: Replace this test with positive tests of successful TLS execution using
 ; the new ORC runtime once it supports them.



More information about the llvm-commits mailing list