Skip to content

Commit 03c6593

Browse files
committed
[ThinLTO] Handle -emit-llvm* in ThinLTO backends
Summary: Use PreCodeGenModuleHook to invoke the correct writer when emitting LLVM IR, returning false to skip codegen from within thinBackend. Reviewers: pcc, mehdi_amini Subscribers: Prazek, cfe-commits Differential Revision: https://reviews.llvm.org/D31534 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@299274 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent c3deccb commit 03c6593

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/CodeGen/BackendUtil.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,9 +994,30 @@ static void runThinLTOBackend(ModuleSummaryIndex *CombinedIndex, Module *M,
994994
Conf.MAttrs = TOpts.Features;
995995
Conf.RelocModel = getRelocModel(CGOpts);
996996
Conf.CGOptLevel = getCGOptLevel(CGOpts);
997-
Conf.CGFileType = getCodeGenFileType(Action);
998997
initTargetOptions(Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
999998
Conf.SampleProfile = std::move(SampleProfile);
999+
switch (Action) {
1000+
case Backend_EmitNothing:
1001+
Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1002+
return false;
1003+
};
1004+
break;
1005+
case Backend_EmitLL:
1006+
Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1007+
M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1008+
return false;
1009+
};
1010+
break;
1011+
case Backend_EmitBC:
1012+
Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1013+
WriteBitcodeToFile(M, *OS, CGOpts.EmitLLVMUseLists);
1014+
return false;
1015+
};
1016+
break;
1017+
default:
1018+
Conf.CGFileType = getCodeGenFileType(Action);
1019+
break;
1020+
}
10001021
if (Error E = thinBackend(
10011022
Conf, 0, AddStream, *M, *CombinedIndex, ImportList,
10021023
ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) {

test/CodeGen/thinlto-emit-llvm.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test to ensure -emit-llvm and -emit-llvm-bc work when invoking the
2+
// ThinLTO backend path.
3+
// RUN: %clang -O2 %s -flto=thin -c -o %t.o
4+
// RUN: llvm-lto -thinlto -o %t %t.o
5+
// RUN: %clang_cc1 -O2 -x ir %t.o -fthinlto-index=%t.thinlto.bc -emit-llvm -o - | FileCheck %s
6+
// RUN: %clang_cc1 -O2 -x ir %t.o -fthinlto-index=%t.thinlto.bc -emit-llvm-bc -o - | llvm-dis -o - | FileCheck %s
7+
8+
// CHECK: define void @foo()
9+
void foo() {
10+
}

0 commit comments

Comments
 (0)