Skip to content

Commit dc8e07b

Browse files
committedSep 16, 2016
[LTO] Prevent asm references to be dropped from the output.
Differential Revision: https://reviews.llvm.org/D24617 llvm-svn: 281741
1 parent 20d0319 commit dc8e07b

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
 

‎llvm/lib/LTO/LTOBackend.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/IR/PassManager.h"
2626
#include "llvm/IR/Verifier.h"
2727
#include "llvm/LTO/LTO.h"
28+
#include "llvm/LTO/legacy/UpdateCompilerUsed.h"
2829
#include "llvm/MC/SubtargetFeature.h"
2930
#include "llvm/Passes/PassBuilder.h"
3031
#include "llvm/Support/Error.h"
@@ -275,6 +276,19 @@ Expected<const Target *> initAndLookupTarget(Config &C, Module &Mod) {
275276

276277
}
277278

279+
static void handleAsmUndefinedRefs(Module &Mod, TargetMachine &TM) {
280+
// Collect the list of undefined symbols used in asm and update
281+
// llvm.compiler.used to prevent optimization to drop these from the output.
282+
StringSet<> AsmUndefinedRefs;
283+
object::IRObjectFile::CollectAsmUndefinedRefs(
284+
Triple(Mod.getTargetTriple()), Mod.getModuleInlineAsm(),
285+
[&AsmUndefinedRefs](StringRef Name, object::BasicSymbolRef::Flags Flags) {
286+
if (Flags & object::BasicSymbolRef::SF_Undefined)
287+
AsmUndefinedRefs.insert(Name);
288+
});
289+
updateCompilerUsed(Mod, TM, AsmUndefinedRefs);
290+
}
291+
278292
Error lto::backend(Config &C, AddOutputFn AddOutput,
279293
unsigned ParallelCodeGenParallelismLevel,
280294
std::unique_ptr<Module> Mod) {
@@ -285,6 +299,8 @@ Error lto::backend(Config &C, AddOutputFn AddOutput,
285299
std::unique_ptr<TargetMachine> TM =
286300
createTargetMachine(C, Mod->getTargetTriple(), *TOrErr);
287301

302+
handleAsmUndefinedRefs(*Mod, *TM);
303+
288304
if (!C.CodeGenOnly)
289305
if (!opt(C, TM.get(), 0, *Mod, /*IsThinLto=*/false))
290306
return Error();
@@ -310,6 +326,8 @@ Error lto::thinBackend(Config &Conf, unsigned Task, AddOutputFn AddOutput,
310326
std::unique_ptr<TargetMachine> TM =
311327
createTargetMachine(Conf, Mod.getTargetTriple(), *TOrErr);
312328

329+
handleAsmUndefinedRefs(Mod, *TM);
330+
313331
if (Conf.CodeGenOnly) {
314332
codegen(Conf, TM.get(), AddOutput, Task, Mod);
315333
return Error();
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; RegularLTO testcase
2+
; RUN: llvm-as %s -o %t.o
3+
; RUN: %gold -shared -m elf_x86_64 -o %t2 -plugin %llvmshlibdir/LLVMgold.so \
4+
; RUN: %t.o --plugin-opt=save-temps -upatatino
5+
; RUN: llvm-dis < %t2.0.5.precodegen.bc | FileCheck %s
6+
7+
; ThinLTO testcase
8+
; RUN: opt -module-summary %s -o %t.o
9+
; RUN: %gold -m elf_x86_64 -plugin %llvmshlibdir/LLVMgold.so \
10+
; RUN: --plugin-opt=save-temps \
11+
; RUN: --plugin-opt=thinlto -o %t2 %t.o
12+
; RUN: llvm-dis < %t.o.5.precodegen.bc | FileCheck %s
13+
14+
; Check that foo is properly appended to llvm.compiler.used
15+
; CHECK: @llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (void ()* @foo to i8*)], section "llvm.metadata"
16+
17+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
18+
target triple = "x86_64-unknown-linux-gnu"
19+
20+
module asm ".global patatino"
21+
module asm ".equ patatino, foo"
22+
23+
declare void @patatino()
24+
25+
define void @foo() {
26+
call void @patatino()
27+
ret void
28+
}

0 commit comments

Comments
 (0)
Please sign in to comment.