Index: include/llvm/LTO/LTO.h =================================================================== --- include/llvm/LTO/LTO.h +++ include/llvm/LTO/LTO.h @@ -325,6 +325,7 @@ bool HasModule = false; std::unique_ptr CombinedModule; std::unique_ptr Mover; + StringSet<> AsmUndefinedRefs; } RegularLTO; struct ThinLTOState { Index: include/llvm/LTO/LTOBackend.h =================================================================== --- include/llvm/LTO/LTOBackend.h +++ include/llvm/LTO/LTOBackend.h @@ -18,6 +18,7 @@ #define LLVM_LTO_LTOBACKEND_H #include "llvm/ADT/MapVector.h" +#include "llvm/ADT/StringSet.h" #include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/ModuleSummaryIndex.h" #include "llvm/LTO/Config.h" @@ -36,7 +37,7 @@ /// Runs a regular LTO backend. Error backend(Config &C, AddOutputFn AddStream, unsigned ParallelCodeGenParallelismLevel, - std::unique_ptr M); + std::unique_ptr M, StringSet<> AsmUndefinedRefs); /// Runs a ThinLTO backend. Error thinBackend(Config &C, unsigned Task, AddOutputFn AddStream, Module &M, Index: lib/LTO/LTO.cpp =================================================================== --- lib/LTO/LTO.cpp +++ lib/LTO/LTO.cpp @@ -301,6 +301,19 @@ return addRegularLTO(std::move(Input), Res); } +static void handleUndefinedAsmRefs(const InputFile::Symbol &Sym, + GlobalValue *GV, + StringSet<> &AsmUndefinedRefs) { + // GV associated => not an assembly symbol, bail out. + if (GV) + return; + + // This is an undefined reference of a symbol used in asm. We put that in + // compiler.used, so that we can preserve it from being dropped from + // the output, without necessarily preventing its internalization. + AsmUndefinedRefs.insert(Sym.getName().str()); +} + // Add a regular LTO object to the link. Error LTO::addRegularLTO(std::unique_ptr Input, ArrayRef Res) { @@ -337,8 +350,10 @@ addSymbolToGlobalRes(Obj.get(), Used, Sym, Res, 0); GlobalValue *GV = Obj->getSymbolGV(Sym.I->getRawDataRefImpl()); - if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined) + if (Sym.getFlags() & object::BasicSymbolRef::SF_Undefined) { + handleUndefinedAsmRefs(Sym, GV, RegularLTO.AsmUndefinedRefs); continue; + } if (Res.Prevailing && GV) { Keep.push_back(GV); switch (GV->getLinkage()) { @@ -475,7 +490,8 @@ return Error(); } return backend(Conf, AddOutput, RegularLTO.ParallelCodeGenParallelismLevel, - std::move(RegularLTO.CombinedModule)); + std::move(RegularLTO.CombinedModule), + RegularLTO.AsmUndefinedRefs); } /// This class defines the interface to the ThinLTO backend. Index: lib/LTO/LTOBackend.cpp =================================================================== --- lib/LTO/LTOBackend.cpp +++ lib/LTO/LTOBackend.cpp @@ -25,6 +25,7 @@ #include "llvm/IR/PassManager.h" #include "llvm/IR/Verifier.h" #include "llvm/LTO/LTO.h" +#include "llvm/LTO/legacy/UpdateCompilerUsed.h" #include "llvm/MC/SubtargetFeature.h" #include "llvm/Passes/PassBuilder.h" #include "llvm/Support/Error.h" @@ -277,7 +278,7 @@ Error lto::backend(Config &C, AddOutputFn AddOutput, unsigned ParallelCodeGenParallelismLevel, - std::unique_ptr Mod) { + std::unique_ptr Mod, StringSet<> AsmUndefinedRefs) { Expected TOrErr = initAndLookupTarget(C, *Mod); if (!TOrErr) return TOrErr.takeError(); @@ -285,6 +286,10 @@ std::unique_ptr TM = createTargetMachine(C, Mod->getTargetTriple(), *TOrErr); + // Update llvm.compiler.used to that optimizations won't strip + // off asm undefined references. + updateCompilerUsed(*Mod, *TM, AsmUndefinedRefs); + if (!C.CodeGenOnly) if (!opt(C, TM.get(), 0, *Mod, /*IsThinLto=*/false)) return Error(); @@ -307,8 +312,21 @@ if (!TOrErr) return TOrErr.takeError(); + StringRef TheTriple = Mod.getTargetTriple(); + std::unique_ptr TM = - createTargetMachine(Conf, Mod.getTargetTriple(), *TOrErr); + createTargetMachine(Conf, TheTriple, *TOrErr); + + // Collect the list of undefined symbols used in asm and update + // llvm.compiler.used to prevent optimization to drop these from the output. + StringSet<> AsmUndefinedRefs; + object::IRObjectFile::CollectAsmUndefinedRefs( + Triple(TheTriple), Mod.getModuleInlineAsm(), + [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) { + if (Flags & object::BasicSymbolRef::SF_Undefined) + AsmUndefinedRefs.insert(Name); + }); + updateCompilerUsed(Mod, *TM, AsmUndefinedRefs); if (Conf.CodeGenOnly) { codegen(Conf, TM.get(), AddOutput, Task, Mod); Index: test/tools/gold/X86/asm_undefined2.ll =================================================================== --- /dev/null +++ test/tools/gold/X86/asm_undefined2.ll @@ -0,0 +1,20 @@ +; RUN: llvm-as %s -o %t.o +; RUN: %gold -shared -m elf_x86_64 -o %t2 -plugin %llvmshlibdir/LLVMgold.so \ +; RUN: %t.o --plugin-opt=save-temps -upatatino +; RUN: llvm-dis < %t2.0.5.precodegen.bc | FileCheck %s + +; Check that foo is properly appended to llvm.compiler.used +; CHECK: @llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (void ()* @foo to i8*)], section "llvm.metadata" + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +module asm ".global patatino" +module asm ".equ patatino, foo" + +declare void @patatino() + +define void @foo() { + call void @patatino() + ret void +} Index: test/tools/gold/X86/asm_undefined2_thin.ll =================================================================== --- /dev/null +++ test/tools/gold/X86/asm_undefined2_thin.ll @@ -0,0 +1,21 @@ +; RUN: opt -module-summary %s -o %t.o + +; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold.so \ +; RUN: --plugin-opt=save-temps \ +; RUN: --plugin-opt=thinlto -o %t2 %t.o +; RUN: llvm-dis < %t.o.5.precodegen.bc | FileCheck %s + +; CHECK: @llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (void ()* @foo to i8*)], section "llvm.metadata" + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +module asm ".global patatino" +module asm ".equ patatino, foo" + +declare void @patatino() + +define void @foo() { + call void @patatino() + ret void +}