From cd94dfdbe83e7cb1148a1ee9d74c926532cf4499 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 8 May 2025 14:35:19 +0800 Subject: [PATCH] [IRMover] Fix symver Copy @@ When FunctionImport uses IRMover to copy a function from other thinLTO modules, it will copy the related .symver asm. However, like ".symver foo, foo@@foo_1.0" sets the default version, requiring the corresponding function to be defined. I believe that as a module using this function, it only need to know the version corresponding to the symbol name it uses, so the copied asm should be changed to ".symver foo, foo@foo_1.0". To solve this problem, the code in IRMover needs to be modified. Signed-off-by: Eric --- llvm/lib/Linker/IRMover.cpp | 15 ++++++++++++++- llvm/test/ThinLTO/X86/Inputs/import-symver-foo.ll | 2 ++ llvm/test/ThinLTO/X86/import-symver.ll | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp index ab960e021d4d..b520dc24b167 100644 --- a/llvm/lib/Linker/IRMover.cpp +++ b/llvm/lib/Linker/IRMover.cpp @@ -30,6 +30,11 @@ #include using namespace llvm; +static cl::opt ReplaceImportedSymverAsDeclare( + "replace-imported-symver-as-declare", + cl::desc("Replace imported symver as declare"), cl::init(true), + cl::Hidden); + //===----------------------------------------------------------------------===// // TypeMap implementation. //===----------------------------------------------------------------------===// @@ -1622,7 +1627,15 @@ Error IRLinker::run() { SmallString<256> S(".symver "); S += Name; S += ", "; - S += Alias; + auto Pos = Alias.find("@@"); + if (ReplaceImportedSymverAsDeclare && + (Pos != StringRef::npos && !Alias.startswith("@@@"))) { + S += Alias.substr(0, Pos); + S += '@'; + S += Alias.substr(Pos + 2); + } else { + S += Alias; + } DstM.appendModuleInlineAsm(S); } }); diff --git a/llvm/test/ThinLTO/X86/Inputs/import-symver-foo.ll b/llvm/test/ThinLTO/X86/Inputs/import-symver-foo.ll index 95545b433f98..498fede9e552 100644 --- a/llvm/test/ThinLTO/X86/Inputs/import-symver-foo.ll +++ b/llvm/test/ThinLTO/X86/Inputs/import-symver-foo.ll @@ -5,6 +5,8 @@ module asm ".symver bar, bar@BAR_1.2.3" declare dso_local i32 @bar() +module asm ".symver foo, foo@@FOO_1.2.3" + define dso_local i32 @foo() { entry: %call = tail call i32 @bar() diff --git a/llvm/test/ThinLTO/X86/import-symver.ll b/llvm/test/ThinLTO/X86/import-symver.ll index 556c4fd992f0..41cd98f8665b 100644 --- a/llvm/test/ThinLTO/X86/import-symver.ll +++ b/llvm/test/ThinLTO/X86/import-symver.ll @@ -8,6 +8,8 @@ ; RUN: llvm-lto -thinlto-action=import -exported-symbol=main -import-instr-limit=0 %t1.bc -thinlto-index=%t3.index.bc ; RUN: llvm-dis %t1.bc.thinlto.imported.bc -o - | FileCheck --check-prefix=NOIMPORT %s +; When @foo gets imported, the symver must be imported as @ rather than @@. +; IMPORT: module asm ".symver foo, foo@FOO_1.2.3" ; When @bar gets imported, the symver must be imported too. ; IMPORT: module asm ".symver bar, bar@BAR_1.2.3" ; IMPORT: declare dso_local i32 @bar() -- Gitee