[llvm] r281741 - [LTO] Prevent asm references to be dropped from the output.

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 16 09:05:26 PDT 2016


Author: davide
Date: Fri Sep 16 11:05:25 2016
New Revision: 281741

URL: http://llvm.org/viewvc/llvm-project?rev=281741&view=rev
Log:
[LTO] Prevent asm references to be dropped from the output.

Differential Revision:  https://reviews.llvm.org/D24617

Added:
    llvm/trunk/test/tools/gold/X86/asm_undefined2.ll
Modified:
    llvm/trunk/lib/LTO/LTOBackend.cpp

Modified: llvm/trunk/lib/LTO/LTOBackend.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOBackend.cpp?rev=281741&r1=281740&r2=281741&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/LTOBackend.cpp (original)
+++ llvm/trunk/lib/LTO/LTOBackend.cpp Fri Sep 16 11:05:25 2016
@@ -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"
@@ -275,6 +276,19 @@ Expected<const Target *> initAndLookupTa
 
 }
 
+static void handleAsmUndefinedRefs(Module &Mod, TargetMachine &TM) {
+  // 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(Mod.getTargetTriple()), Mod.getModuleInlineAsm(),
+      [&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) {
+        if (Flags & object::BasicSymbolRef::SF_Undefined)
+          AsmUndefinedRefs.insert(Name);
+      });
+  updateCompilerUsed(Mod, TM, AsmUndefinedRefs);
+}
+
 Error lto::backend(Config &C, AddOutputFn AddOutput,
                    unsigned ParallelCodeGenParallelismLevel,
                    std::unique_ptr<Module> Mod) {
@@ -285,6 +299,8 @@ Error lto::backend(Config &C, AddOutputF
   std::unique_ptr<TargetMachine> TM =
       createTargetMachine(C, Mod->getTargetTriple(), *TOrErr);
 
+  handleAsmUndefinedRefs(*Mod, *TM);
+
   if (!C.CodeGenOnly)
     if (!opt(C, TM.get(), 0, *Mod, /*IsThinLto=*/false))
       return Error();
@@ -310,6 +326,8 @@ Error lto::thinBackend(Config &Conf, uns
   std::unique_ptr<TargetMachine> TM =
       createTargetMachine(Conf, Mod.getTargetTriple(), *TOrErr);
 
+  handleAsmUndefinedRefs(Mod, *TM);
+
   if (Conf.CodeGenOnly) {
     codegen(Conf, TM.get(), AddOutput, Task, Mod);
     return Error();

Added: llvm/trunk/test/tools/gold/X86/asm_undefined2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/gold/X86/asm_undefined2.ll?rev=281741&view=auto
==============================================================================
--- llvm/trunk/test/tools/gold/X86/asm_undefined2.ll (added)
+++ llvm/trunk/test/tools/gold/X86/asm_undefined2.ll Fri Sep 16 11:05:25 2016
@@ -0,0 +1,28 @@
+; RegularLTO testcase
+; 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
+
+; ThinLTO testcase
+; 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 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
+}




More information about the llvm-commits mailing list