[llvm] Fix emulated TLS alignment for large variables (PR #171037)

Medha Tiwari via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 7 05:09:51 PST 2025


https://github.com/medhatiwari created https://github.com/llvm/llvm-project/pull/171037

## Summary
Fix emulated TLS alignment for larger variables (>= 32 bytes) to use preferred alignment.


 Fixes #167219

>From 7781f9da813af8c3f59481191549190f62510d12 Mon Sep 17 00:00:00 2001
From: Medha Tiwari <medhatiwari at ibm.com>
Date: Sun, 7 Dec 2025 18:38:02 +0530
Subject: [PATCH] [CodeGen] Fix emulated TLS alignment for large variables
 Fixes #167219

---
 llvm/lib/CodeGen/LowerEmuTLS.cpp | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/llvm/lib/CodeGen/LowerEmuTLS.cpp b/llvm/lib/CodeGen/LowerEmuTLS.cpp
index c81f0184cf213..b85476b888c4e 100644
--- a/llvm/lib/CodeGen/LowerEmuTLS.cpp
+++ b/llvm/lib/CodeGen/LowerEmuTLS.cpp
@@ -150,6 +150,17 @@ bool addEmuTlsVar(Module &M, const GlobalVariable *GV) {
   Type *GVType = GV->getValueType();
   Align GVAlignment = DL.getValueOrABITypeAlignment(GV->getAlign(), GVType);
 
+  // For larger TLS variables that might be accessed via memcpy/memmove,
+  // use the preferred alignment to ensure the backend can use optimized
+  // vector instructions (e.g., ARM NEON vld1.64 requires 16-byte alignment).
+  // This fixes the mismatch where emulated TLS allocates with lower alignment
+  // than what the code generator assumes.
+  TypeSize TS = DL.getTypeStoreSize(GVType);
+  if (TS.isScalable() || TS.getFixedValue() >= 32) {
+    Align PreferredAlign = DL.getPreferredAlign(GV);
+    GVAlignment = std::max(GVAlignment, PreferredAlign);
+  }
+
   // Define "__emutls_t.*" if there is InitValue
   GlobalVariable *EmuTlsTmplVar = nullptr;
   if (InitValue) {



More information about the llvm-commits mailing list