Index: llvm/lib/IR/Globals.cpp =================================================================== --- llvm/lib/IR/Globals.cpp +++ llvm/lib/IR/Globals.cpp @@ -65,6 +65,7 @@ void GlobalValue::copyAttributesFrom(const GlobalValue *Src) { setVisibility(Src->getVisibility()); setUnnamedAddr(Src->getUnnamedAddr()); + setThreadLocalMode(Src->getThreadLocalMode()); setDLLStorageClass(Src->getDLLStorageClass()); setDSOLocal(Src->isDSOLocal()); setPartition(Src->getPartition()); @@ -419,7 +420,6 @@ /// from the GlobalVariable Src to this one. void GlobalVariable::copyAttributesFrom(const GlobalVariable *Src) { GlobalObject::copyAttributesFrom(Src); - setThreadLocalMode(Src->getThreadLocalMode()); setExternallyInitialized(Src->isExternallyInitialized()); setAttributes(Src->getAttributes()); } Index: llvm/test/Linker/Inputs/alias-threadlocal-defs.ll =================================================================== --- /dev/null +++ llvm/test/Linker/Inputs/alias-threadlocal-defs.ll @@ -0,0 +1,2 @@ +@tlsvar1 = thread_local global i32 0, align 4 +@tlsvar2 = hidden thread_local alias i32, i32* @tlsvar1 Index: llvm/test/Linker/alias-threadlocal.ll =================================================================== --- /dev/null +++ llvm/test/Linker/alias-threadlocal.ll @@ -0,0 +1,9 @@ +; RUN: llvm-link %s %p/Inputs/alias-threadlocal-defs.ll -S -o - | FileCheck %s + +; PR46297 +; Verify that linking GlobalAliases preserves the thread_local attribute + +; CHECK: @tlsvar1 = thread_local global i32 0, align 4 +; CHECK: @tlsvar2 = hidden thread_local alias i32, i32* @tlsvar1 + +@tlsvar2 = external thread_local global i32, align 4