From 485ef16bd4bbcaf9610b2f1423f4f0f639203c26 Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Mon, 31 Oct 2016 22:12:21 +0000 Subject: [PATCH] [ThinLTO] Disable importing and other cross-module optis at -O0 Summary: There is no point to importing at -O0, since we won't inline. We should also disable other cross-module optimizations. (Plan to backport this fix to the 3.9 branch to fix PR30774) Reviewers: pcc Subscribers: johanengelen, mehdi_amini Differential Revision: https://reviews.llvm.org/D25918 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@285648 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/LTO/LTO.cpp | 60 ++++++++++--------- test/ThinLTO/X86/Inputs/funcimport2.ll | 11 ++++ test/ThinLTO/X86/funcimport2.ll | 28 +++++++++ .../gold/X86/Inputs/thinlto_funcimport.ll | 10 ++++ test/tools/gold/X86/thinlto_funcimport.ll | 28 +++++++++ tools/llvm-lto2/llvm-lto2.cpp | 7 +++ 6 files changed, 116 insertions(+), 28 deletions(-) create mode 100644 test/ThinLTO/X86/Inputs/funcimport2.ll create mode 100644 test/ThinLTO/X86/funcimport2.ll create mode 100644 test/tools/gold/X86/Inputs/thinlto_funcimport.ll create mode 100644 test/tools/gold/X86/thinlto_funcimport.ll diff --git a/lib/LTO/LTO.cpp b/lib/LTO/LTO.cpp index 654b86a0dfd..20da6d9f018 100644 --- a/lib/LTO/LTO.cpp +++ b/lib/LTO/LTO.cpp @@ -770,36 +770,40 @@ Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache, ThinLTO.ModuleMap.size()); StringMap ExportLists( ThinLTO.ModuleMap.size()); - ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries, - ImportLists, ExportLists); - - std::set ExportedGUIDs; - for (auto &Res : GlobalResolutions) { - if (!Res.second.IRName.empty() && - Res.second.Partition == GlobalResolution::External) - ExportedGUIDs.insert(GlobalValue::getGUID(Res.second.IRName)); - } - - auto isPrevailing = [&](GlobalValue::GUID GUID, const GlobalValueSummary *S) { - return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath(); - }; - auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) { - const auto &ExportList = ExportLists.find(ModuleIdentifier); - return (ExportList != ExportLists.end() && - ExportList->second.count(GUID)) || - ExportedGUIDs.count(GUID); - }; - thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported); - StringMap> ResolvedODR; - auto recordNewLinkage = [&](StringRef ModuleIdentifier, - GlobalValue::GUID GUID, - GlobalValue::LinkageTypes NewLinkage) { - ResolvedODR[ModuleIdentifier][GUID] = NewLinkage; - }; - thinLTOResolveWeakForLinkerInIndex(ThinLTO.CombinedIndex, isPrevailing, - recordNewLinkage); + if (Conf.OptLevel > 0) { + ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries, + ImportLists, ExportLists); + + std::set ExportedGUIDs; + for (auto &Res : GlobalResolutions) { + if (!Res.second.IRName.empty() && + Res.second.Partition == GlobalResolution::External) + ExportedGUIDs.insert(GlobalValue::getGUID(Res.second.IRName)); + } + + auto isPrevailing = [&](GlobalValue::GUID GUID, + const GlobalValueSummary *S) { + return ThinLTO.PrevailingModuleForGUID[GUID] == S->modulePath(); + }; + auto isExported = [&](StringRef ModuleIdentifier, GlobalValue::GUID GUID) { + const auto &ExportList = ExportLists.find(ModuleIdentifier); + return (ExportList != ExportLists.end() && + ExportList->second.count(GUID)) || + ExportedGUIDs.count(GUID); + }; + thinLTOInternalizeAndPromoteInIndex(ThinLTO.CombinedIndex, isExported); + + auto recordNewLinkage = [&](StringRef ModuleIdentifier, + GlobalValue::GUID GUID, + GlobalValue::LinkageTypes NewLinkage) { + ResolvedODR[ModuleIdentifier][GUID] = NewLinkage; + }; + + thinLTOResolveWeakForLinkerInIndex(ThinLTO.CombinedIndex, isPrevailing, + recordNewLinkage); + } std::unique_ptr BackendProc = ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries, diff --git a/test/ThinLTO/X86/Inputs/funcimport2.ll b/test/ThinLTO/X86/Inputs/funcimport2.ll new file mode 100644 index 00000000000..216ef0f1daa --- /dev/null +++ b/test/ThinLTO/X86/Inputs/funcimport2.ll @@ -0,0 +1,11 @@ +target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-apple-macosx10.11.0" + + +define i32 @main() #0 { +entry: + call void (...) @foo() + ret i32 0 +} + +declare void @foo(...) #1 diff --git a/test/ThinLTO/X86/funcimport2.ll b/test/ThinLTO/X86/funcimport2.ll new file mode 100644 index 00000000000..c83370be970 --- /dev/null +++ b/test/ThinLTO/X86/funcimport2.ll @@ -0,0 +1,28 @@ +; Do setup work for all below tests: generate bitcode and combined index +; RUN: opt -module-summary %s -o %t1.bc +; RUN: opt -module-summary %p/Inputs/funcimport2.ll -o %t2.bc + +; RUN: llvm-lto2 %t1.bc %t2.bc -o %t.o -save-temps \ +; RUN: -r=%t1.bc,_foo,plx \ +; RUN: -r=%t2.bc,_main,plx \ +; RUN: -r=%t2.bc,_foo,l +; RUN: llvm-dis %t.o.1.3.import.bc -o - | FileCheck %s +; CHECK: define available_externally void @foo() + +; We shouldn't do any importing at -O0 +; rm -f %t.o.1.3.import.bc +; RUN: llvm-lto2 %t1.bc %t2.bc -o %t.o -save-temps \ +; RUN: -O0 \ +; RUN: -r=%t1.bc,_foo,plx \ +; RUN: -r=%t2.bc,_main,plx \ +; RUN: -r=%t2.bc,_foo,l +; RUN: llvm-dis %t.o.1.3.import.bc -o - | FileCheck %s --check-prefix=CHECKO0 +; CHECKO0: declare void @foo(...) + +target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-apple-macosx10.11.0" + +define void @foo() #0 { +entry: + ret void +} diff --git a/test/tools/gold/X86/Inputs/thinlto_funcimport.ll b/test/tools/gold/X86/Inputs/thinlto_funcimport.ll new file mode 100644 index 00000000000..74246ab937c --- /dev/null +++ b/test/tools/gold/X86/Inputs/thinlto_funcimport.ll @@ -0,0 +1,10 @@ +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define i32 @main() #0 { +entry: + call void (...) @foo() + ret i32 0 +} + +declare void @foo(...) #1 diff --git a/test/tools/gold/X86/thinlto_funcimport.ll b/test/tools/gold/X86/thinlto_funcimport.ll new file mode 100644 index 00000000000..d00b42d54bb --- /dev/null +++ b/test/tools/gold/X86/thinlto_funcimport.ll @@ -0,0 +1,28 @@ +; Do setup work for all below tests: generate bitcode and combined index +; RUN: opt -module-summary %s -o %t1.bc +; RUN: opt -module-summary %p/Inputs/thinlto_funcimport.ll -o %t2.bc + +; RUN: %gold -plugin %llvmshlibdir/LLVMgold.so \ +; RUN: --plugin-opt=save-temps \ +; RUN: --plugin-opt=thinlto \ +; RUN: -shared %t1.bc %t2.bc -o %t +; RUN: llvm-dis %t2.bc.3.import.bc -o - | FileCheck %s +; CHECK: define available_externally void @foo() + +; We shouldn't do any importing at -O0 +; rm -f %t2.bc.3.import.bc +; RUN: %gold -plugin %llvmshlibdir/LLVMgold.so \ +; RUN: --plugin-opt=save-temps \ +; RUN: --plugin-opt=thinlto \ +; RUN: --plugin-opt=O0 \ +; RUN: -shared %t1.bc %t2.bc -o %t +; RUN: llvm-dis %t2.bc.3.import.bc -o - | FileCheck %s --check-prefix=CHECKO0 +; CHECKO0: declare void @foo(...) + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define void @foo() #0 { +entry: + ret void +} diff --git a/tools/llvm-lto2/llvm-lto2.cpp b/tools/llvm-lto2/llvm-lto2.cpp index 042f874596f..b48f6c1993e 100644 --- a/tools/llvm-lto2/llvm-lto2.cpp +++ b/tools/llvm-lto2/llvm-lto2.cpp @@ -26,6 +26,11 @@ using namespace llvm; using namespace lto; using namespace object; +static cl::opt + OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " + "(default = '-O2')"), + cl::Prefix, cl::ZeroOrMore, cl::init('2')); + static cl::list InputFilenames(cl::Positional, cl::OneOrMore, cl::desc("")); @@ -145,6 +150,8 @@ int main(int argc, char **argv) { Conf.OptPipeline = OptPipeline; Conf.AAPipeline = AAPipeline; + Conf.OptLevel = OptLevel - '0'; + ThinBackend Backend; if (ThinLTODistributedIndexes) Backend = createWriteIndexesThinBackend("", "", true, "");