2008-02-27 04:26:43 +08:00
|
|
|
//===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-03-31 18:50:14 +08:00
|
|
|
//
|
2008-02-27 04:26:43 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-03-31 18:50:14 +08:00
|
|
|
// This file implements the Link Time Optimization library. This library is
|
2008-02-27 04:26:43 +08:00
|
|
|
// intended to be used by linker to optimize code at link time.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-15 05:21:16 +08:00
|
|
|
#include "llvm/LTO/legacy/LTOCodeGenerator.h"
|
2016-04-12 14:34:10 +08:00
|
|
|
|
2016-02-17 05:41:51 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2009-06-17 14:52:10 +08:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2015-01-15 10:16:27 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2015-01-31 19:17:59 +08:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2016-11-11 13:34:58 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2015-08-28 07:37:36 +08:00
|
|
|
#include "llvm/CodeGen/ParallelCG.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2012-03-31 19:15:43 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
2016-05-10 03:57:15 +08:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-01-16 06:04:35 +08:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2019-10-29 05:53:31 +08:00
|
|
|
#include "llvm/IR/LLVMRemarkStreamer.h"
|
2015-02-13 18:01:29 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2014-01-08 05:19:40 +08:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2018-09-26 21:01:43 +08:00
|
|
|
#include "llvm/IR/PassTimingInfo.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2013-07-23 02:40:34 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2017-02-11 07:49:38 +08:00
|
|
|
#include "llvm/LTO/LTO.h"
|
2021-02-15 03:33:36 +08:00
|
|
|
#include "llvm/LTO/LTOBackend.h"
|
2016-07-15 05:21:16 +08:00
|
|
|
#include "llvm/LTO/legacy/LTOModule.h"
|
|
|
|
#include "llvm/LTO/legacy/UpdateCompilerUsed.h"
|
2014-03-06 11:42:23 +08:00
|
|
|
#include "llvm/Linker/Linker.h"
|
2010-03-13 02:44:54 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2011-06-29 09:14:12 +08:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
2021-10-09 01:48:15 +08:00
|
|
|
#include "llvm/MC/TargetRegistry.h"
|
2020-11-18 02:37:59 +08:00
|
|
|
#include "llvm/Remarks/HotnessThresholdParser.h"
|
2010-03-13 02:44:54 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2013-06-18 02:05:35 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2010-11-30 02:47:54 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-11-30 02:47:54 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2011-08-25 02:08:43 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2016-11-19 02:06:28 +08:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2014-03-04 18:07:28 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2013-10-01 00:39:19 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2016-04-13 14:32:46 +08:00
|
|
|
#include "llvm/Transforms/IPO/Internalize.h"
|
2012-12-04 18:44:52 +08:00
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
2020-01-25 04:24:18 +08:00
|
|
|
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
|
2013-03-30 07:28:55 +08:00
|
|
|
#include "llvm/Transforms/ObjCARC.h"
|
2016-10-26 07:53:31 +08:00
|
|
|
#include "llvm/Transforms/Utils/ModuleUtils.h"
|
2014-06-13 01:38:55 +08:00
|
|
|
#include <system_error>
|
2008-02-27 04:26:43 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2012-03-31 18:49:43 +08:00
|
|
|
const char* LTOCodeGenerator::getVersionString() {
|
2008-02-27 04:26:43 +08:00
|
|
|
#ifdef LLVM_VERSION_INFO
|
2012-03-31 18:49:43 +08:00
|
|
|
return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
|
2008-02-27 04:26:43 +08:00
|
|
|
#else
|
2012-03-31 18:49:43 +08:00
|
|
|
return PACKAGE_NAME " version " PACKAGE_VERSION;
|
2008-02-27 04:26:43 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
Add a flag to the LLVMContext to disable name for Value other than GlobalValue
Summary:
This is intended to be a performance flag, on the same level as clang
cc1 option "--disable-free". LLVM will never initialize it by default,
it will be up to the client creating the LLVMContext to request this
behavior. Clang will do it by default in Release build (just like
--disable-free).
"opt" and "llc" can opt-in using -disable-named-value command line
option.
When performing LTO on llvm-tblgen, the initial merging of IR peaks
at 92MB without this patch, and 86MB after this patch,setNameImpl()
drops from 6.5MB to 0.5MB.
The total link time goes from ~29.5s to ~27.8s.
Compared to a compile-time flag (like the IRBuilder one), it performs
very close. I profiled on SROA and obtain these results:
420ms with IRBuilder that preserve name
372ms with IRBuilder that strip name
375ms with IRBuilder that preserve name, and a runtime flag to strip
Reviewers: chandlerc, dexonsmith, bogner
Subscribers: joker.eph, llvm-commits
Differential Revision: http://reviews.llvm.org/D17946
From: Mehdi Amini <mehdi.amini@apple.com>
llvm-svn: 263086
2016-03-10 09:28:54 +08:00
|
|
|
namespace llvm {
|
|
|
|
cl::opt<bool> LTODiscardValueNames(
|
2016-03-11 01:06:52 +08:00
|
|
|
"lto-discard-value-names",
|
|
|
|
cl::desc("Strip names from Value during LTO (other than GlobalValue)."),
|
Add a flag to the LLVMContext to disable name for Value other than GlobalValue
Summary:
This is intended to be a performance flag, on the same level as clang
cc1 option "--disable-free". LLVM will never initialize it by default,
it will be up to the client creating the LLVMContext to request this
behavior. Clang will do it by default in Release build (just like
--disable-free).
"opt" and "llc" can opt-in using -disable-named-value command line
option.
When performing LTO on llvm-tblgen, the initial merging of IR peaks
at 92MB without this patch, and 86MB after this patch,setNameImpl()
drops from 6.5MB to 0.5MB.
The total link time goes from ~29.5s to ~27.8s.
Compared to a compile-time flag (like the IRBuilder one), it performs
very close. I profiled on SROA and obtain these results:
420ms with IRBuilder that preserve name
372ms with IRBuilder that strip name
375ms with IRBuilder that preserve name, and a runtime flag to strip
Reviewers: chandlerc, dexonsmith, bogner
Subscribers: joker.eph, llvm-commits
Differential Revision: http://reviews.llvm.org/D17946
From: Mehdi Amini <mehdi.amini@apple.com>
llvm-svn: 263086
2016-03-10 09:28:54 +08:00
|
|
|
#ifdef NDEBUG
|
|
|
|
cl::init(true),
|
|
|
|
#else
|
|
|
|
cl::init(false),
|
|
|
|
#endif
|
|
|
|
cl::Hidden);
|
2016-05-10 03:57:15 +08:00
|
|
|
|
2019-06-15 00:20:51 +08:00
|
|
|
cl::opt<bool> RemarksWithHotness(
|
2019-06-14 08:05:56 +08:00
|
|
|
"lto-pass-remarks-with-hotness",
|
|
|
|
cl::desc("With PGO, include profile count in optimization remarks"),
|
|
|
|
cl::Hidden);
|
2019-06-14 05:46:57 +08:00
|
|
|
|
2020-11-18 02:37:59 +08:00
|
|
|
cl::opt<Optional<uint64_t>, false, remarks::HotnessThresholdParser>
|
|
|
|
RemarksHotnessThreshold(
|
|
|
|
"lto-pass-remarks-hotness-threshold",
|
|
|
|
cl::desc("Minimum profile count required for an "
|
|
|
|
"optimization remark to be output."
|
|
|
|
" Use 'auto' to apply the threshold from profile summary."),
|
|
|
|
cl::value_desc("uint or 'auto'"), cl::init(0), cl::Hidden);
|
|
|
|
|
2019-06-15 00:20:51 +08:00
|
|
|
cl::opt<std::string>
|
|
|
|
RemarksFilename("lto-pass-remarks-output",
|
|
|
|
cl::desc("Output filename for pass remarks"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
cl::opt<std::string>
|
|
|
|
RemarksPasses("lto-pass-remarks-filter",
|
|
|
|
cl::desc("Only record optimization remarks from passes whose "
|
|
|
|
"names match the given regular expression"),
|
|
|
|
cl::value_desc("regex"));
|
|
|
|
|
2019-06-18 00:06:00 +08:00
|
|
|
cl::opt<std::string> RemarksFormat(
|
|
|
|
"lto-pass-remarks-format",
|
|
|
|
cl::desc("The format used for serializing remarks (default: YAML)"),
|
|
|
|
cl::value_desc("format"), cl::init("yaml"));
|
|
|
|
|
2019-04-19 20:36:41 +08:00
|
|
|
cl::opt<std::string> LTOStatsFile(
|
|
|
|
"lto-stats-file",
|
|
|
|
cl::desc("Save statistics to the specified file"),
|
|
|
|
cl::Hidden);
|
2016-11-20 02:20:05 +08:00
|
|
|
}
|
2016-11-19 02:06:28 +08:00
|
|
|
|
2015-12-04 10:42:28 +08:00
|
|
|
LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
|
2015-12-05 06:08:53 +08:00
|
|
|
: Context(Context), MergedModule(new Module("ld-temp.o", Context)),
|
2015-12-19 03:28:59 +08:00
|
|
|
TheLinker(new Linker(*MergedModule)) {
|
Add a flag to the LLVMContext to disable name for Value other than GlobalValue
Summary:
This is intended to be a performance flag, on the same level as clang
cc1 option "--disable-free". LLVM will never initialize it by default,
it will be up to the client creating the LLVMContext to request this
behavior. Clang will do it by default in Release build (just like
--disable-free).
"opt" and "llc" can opt-in using -disable-named-value command line
option.
When performing LTO on llvm-tblgen, the initial merging of IR peaks
at 92MB without this patch, and 86MB after this patch,setNameImpl()
drops from 6.5MB to 0.5MB.
The total link time goes from ~29.5s to ~27.8s.
Compared to a compile-time flag (like the IRBuilder one), it performs
very close. I profiled on SROA and obtain these results:
420ms with IRBuilder that preserve name
372ms with IRBuilder that strip name
375ms with IRBuilder that preserve name, and a runtime flag to strip
Reviewers: chandlerc, dexonsmith, bogner
Subscribers: joker.eph, llvm-commits
Differential Revision: http://reviews.llvm.org/D17946
From: Mehdi Amini <mehdi.amini@apple.com>
llvm-svn: 263086
2016-03-10 09:28:54 +08:00
|
|
|
Context.setDiscardValueNames(LTODiscardValueNames);
|
2016-04-19 12:55:25 +08:00
|
|
|
Context.enableDebugTypeODRUniquing();
|
2021-02-15 03:33:36 +08:00
|
|
|
|
|
|
|
Config.CodeModel = None;
|
|
|
|
Config.StatsFile = LTOStatsFile;
|
|
|
|
Config.PreCodeGenPassesHook = [](legacy::PassManager &PM) {
|
|
|
|
PM.add(createObjCARCContractPass());
|
|
|
|
};
|
2021-02-04 06:45:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LTOCodeGenerator::~LTOCodeGenerator() {}
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2016-08-23 20:32:57 +08:00
|
|
|
void LTOCodeGenerator::setAsmUndefinedRefs(LTOModule *Mod) {
|
2016-10-08 03:05:14 +08:00
|
|
|
const std::vector<StringRef> &undefs = Mod->getAsmUndefinedRefs();
|
2016-08-23 20:32:57 +08:00
|
|
|
for (int i = 0, e = undefs.size(); i != e; ++i)
|
2019-10-11 04:22:53 +08:00
|
|
|
AsmUndefinedRefs.insert(undefs[i]);
|
2016-08-23 20:32:57 +08:00
|
|
|
}
|
|
|
|
|
2015-09-01 07:44:06 +08:00
|
|
|
bool LTOCodeGenerator::addModule(LTOModule *Mod) {
|
|
|
|
assert(&Mod->getModule().getContext() == &Context &&
|
2014-11-12 07:13:10 +08:00
|
|
|
"Expected module in same context");
|
|
|
|
|
2015-12-19 03:28:59 +08:00
|
|
|
bool ret = TheLinker->linkInModule(Mod->takeModule());
|
2016-08-23 20:32:57 +08:00
|
|
|
setAsmUndefinedRefs(Mod);
|
2011-03-02 12:14:42 +08:00
|
|
|
|
2016-04-21 01:48:22 +08:00
|
|
|
// We've just changed the input, so let's make sure we verify it.
|
|
|
|
HasVerifiedInput = false;
|
|
|
|
|
2013-08-07 13:19:23 +08:00
|
|
|
return !ret;
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
2012-03-31 18:50:14 +08:00
|
|
|
|
2015-08-25 06:22:53 +08:00
|
|
|
void LTOCodeGenerator::setModule(std::unique_ptr<LTOModule> Mod) {
|
2015-02-24 08:45:56 +08:00
|
|
|
assert(&Mod->getModule().getContext() == &Context &&
|
|
|
|
"Expected module in same context");
|
|
|
|
|
|
|
|
AsmUndefinedRefs.clear();
|
|
|
|
|
2015-08-25 06:22:53 +08:00
|
|
|
MergedModule = Mod->takeModule();
|
2019-08-15 23:54:37 +08:00
|
|
|
TheLinker = std::make_unique<Linker>(*MergedModule);
|
2016-08-23 20:32:57 +08:00
|
|
|
setAsmUndefinedRefs(&*Mod);
|
2016-04-21 01:48:22 +08:00
|
|
|
|
|
|
|
// We've just changed the input, so let's make sure we verify it.
|
|
|
|
HasVerifiedInput = false;
|
2015-02-24 08:45:56 +08:00
|
|
|
}
|
|
|
|
|
2016-06-09 03:09:22 +08:00
|
|
|
void LTOCodeGenerator::setTargetOptions(const TargetOptions &Options) {
|
2021-02-15 03:33:36 +08:00
|
|
|
Config.Options = Options;
|
2013-10-01 00:39:19 +08:00
|
|
|
}
|
|
|
|
|
2015-09-01 07:44:06 +08:00
|
|
|
void LTOCodeGenerator::setDebugInfo(lto_debug_model Debug) {
|
|
|
|
switch (Debug) {
|
2012-03-31 19:15:43 +08:00
|
|
|
case LTO_DEBUG_MODEL_NONE:
|
2013-09-05 01:44:24 +08:00
|
|
|
EmitDwarfDebugInfo = false;
|
2013-08-07 13:19:23 +08:00
|
|
|
return;
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2012-03-31 19:15:43 +08:00
|
|
|
case LTO_DEBUG_MODEL_DWARF:
|
2013-09-05 01:44:24 +08:00
|
|
|
EmitDwarfDebugInfo = true;
|
2013-08-07 13:19:23 +08:00
|
|
|
return;
|
2012-03-31 19:15:43 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("Unknown debug format!");
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2015-09-01 07:44:06 +08:00
|
|
|
void LTOCodeGenerator::setOptLevel(unsigned Level) {
|
2021-02-15 03:33:36 +08:00
|
|
|
Config.OptLevel = Level;
|
|
|
|
Config.PTO.LoopVectorization = Config.OptLevel > 1;
|
|
|
|
Config.PTO.SLPVectorization = Config.OptLevel > 1;
|
|
|
|
switch (Config.OptLevel) {
|
2015-08-22 10:25:53 +08:00
|
|
|
case 0:
|
2021-02-15 03:33:36 +08:00
|
|
|
Config.CGOptLevel = CodeGenOpt::None;
|
2016-08-26 23:22:59 +08:00
|
|
|
return;
|
2015-08-22 10:25:53 +08:00
|
|
|
case 1:
|
2021-02-15 03:33:36 +08:00
|
|
|
Config.CGOptLevel = CodeGenOpt::Less;
|
2016-08-26 23:22:59 +08:00
|
|
|
return;
|
2015-08-22 10:25:53 +08:00
|
|
|
case 2:
|
2021-02-15 03:33:36 +08:00
|
|
|
Config.CGOptLevel = CodeGenOpt::Default;
|
2016-08-26 23:22:59 +08:00
|
|
|
return;
|
2015-08-22 10:25:53 +08:00
|
|
|
case 3:
|
2021-02-15 03:33:36 +08:00
|
|
|
Config.CGOptLevel = CodeGenOpt::Aggressive;
|
2016-08-26 23:22:59 +08:00
|
|
|
return;
|
2015-08-22 10:25:53 +08:00
|
|
|
}
|
2016-08-26 23:22:59 +08:00
|
|
|
llvm_unreachable("Unknown optimization level!");
|
2015-08-22 10:25:53 +08:00
|
|
|
}
|
|
|
|
|
2016-10-01 09:18:23 +08:00
|
|
|
bool LTOCodeGenerator::writeMergedModules(StringRef Path) {
|
2015-11-18 03:48:12 +08:00
|
|
|
if (!determineTarget())
|
2013-08-07 13:19:23 +08:00
|
|
|
return false;
|
2009-08-23 15:49:08 +08:00
|
|
|
|
2016-04-21 01:48:22 +08:00
|
|
|
// We always run the verifier once on the merged module.
|
|
|
|
verifyMergedModuleOnce();
|
|
|
|
|
2013-08-09 07:51:04 +08:00
|
|
|
// mark which symbols can not be internalized
|
|
|
|
applyScopeRestrictions();
|
2009-08-23 15:49:08 +08:00
|
|
|
|
|
|
|
// create output file
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
2019-08-05 13:43:48 +08:00
|
|
|
ToolOutputFile Out(Path, EC, sys::fs::OF_None);
|
2014-08-26 02:16:47 +08:00
|
|
|
if (EC) {
|
2015-11-18 03:48:12 +08:00
|
|
|
std::string ErrMsg = "could not open bitcode file for writing: ";
|
2017-10-24 09:26:22 +08:00
|
|
|
ErrMsg += Path.str() + ": " + EC.message();
|
2015-11-18 03:48:12 +08:00
|
|
|
emitError(ErrMsg);
|
2013-08-07 13:19:23 +08:00
|
|
|
return false;
|
2009-08-23 15:49:08 +08:00
|
|
|
}
|
2012-03-31 18:50:14 +08:00
|
|
|
|
2009-08-23 15:49:08 +08:00
|
|
|
// write bitcode to it
|
2018-02-15 03:11:32 +08:00
|
|
|
WriteBitcodeToFile(*MergedModule, Out.os(), ShouldEmbedUselists);
|
2010-09-01 22:20:41 +08:00
|
|
|
Out.os().close();
|
2010-05-28 04:19:47 +08:00
|
|
|
|
2010-09-01 22:20:41 +08:00
|
|
|
if (Out.os().has_error()) {
|
2015-11-18 03:48:12 +08:00
|
|
|
std::string ErrMsg = "could not write bitcode file: ";
|
2017-10-24 09:26:22 +08:00
|
|
|
ErrMsg += Path.str() + ": " + Out.os().error().message();
|
2015-11-18 03:48:12 +08:00
|
|
|
emitError(ErrMsg);
|
2010-09-01 22:20:41 +08:00
|
|
|
Out.os().clear_error();
|
2013-08-07 13:19:23 +08:00
|
|
|
return false;
|
2009-08-23 15:49:08 +08:00
|
|
|
}
|
2012-03-31 18:50:14 +08:00
|
|
|
|
2010-08-21 00:59:15 +08:00
|
|
|
Out.keep();
|
2013-08-07 13:19:23 +08:00
|
|
|
return true;
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2015-11-18 03:48:12 +08:00
|
|
|
bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
|
2015-11-20 07:59:24 +08:00
|
|
|
// make unique temp output file to put generated code
|
2013-08-13 05:07:31 +08:00
|
|
|
SmallString<128> Filename;
|
2015-11-20 07:59:24 +08:00
|
|
|
|
2021-10-19 09:40:57 +08:00
|
|
|
auto AddStream = [&](size_t Task) -> std::unique_ptr<NativeObjectStream> {
|
2021-02-15 03:33:36 +08:00
|
|
|
StringRef Extension(Config.CGFileType == CGFT_AssemblyFile ? "s" : "o");
|
2015-11-20 07:59:24 +08:00
|
|
|
|
2021-02-15 03:33:36 +08:00
|
|
|
int FD;
|
|
|
|
std::error_code EC =
|
|
|
|
sys::fs::createTemporaryFile("lto-llvm", Extension, FD, Filename);
|
|
|
|
if (EC)
|
|
|
|
emitError(EC.message());
|
2011-03-23 04:57:13 +08:00
|
|
|
|
2021-10-19 09:40:57 +08:00
|
|
|
return std::make_unique<NativeObjectStream>(
|
2021-02-15 03:33:36 +08:00
|
|
|
std::make_unique<llvm::raw_fd_ostream>(FD, true));
|
|
|
|
};
|
2012-03-31 19:15:43 +08:00
|
|
|
|
2021-02-15 03:33:36 +08:00
|
|
|
bool genResult = compileOptimized(AddStream, 1);
|
2012-03-31 19:15:43 +08:00
|
|
|
|
2013-08-13 05:07:31 +08:00
|
|
|
if (!genResult) {
|
|
|
|
sys::fs::remove(Twine(Filename));
|
2013-08-07 13:19:23 +08:00
|
|
|
return false;
|
2013-08-13 05:07:31 +08:00
|
|
|
}
|
2011-02-25 05:04:06 +08:00
|
|
|
|
2021-02-15 03:33:36 +08:00
|
|
|
// If statistics were requested, save them to the specified file or
|
|
|
|
// print them out after codegen.
|
|
|
|
if (StatsFile)
|
|
|
|
PrintStatisticsJSON(StatsFile->os());
|
|
|
|
else if (AreStatisticsEnabled())
|
|
|
|
PrintStatistics();
|
|
|
|
|
2013-09-05 01:44:24 +08:00
|
|
|
NativeObjectPath = Filename.c_str();
|
2015-09-01 07:44:06 +08:00
|
|
|
*Name = NativeObjectPath.c_str();
|
2013-08-07 13:19:23 +08:00
|
|
|
return true;
|
2011-03-23 04:57:13 +08:00
|
|
|
}
|
2011-02-25 05:04:06 +08:00
|
|
|
|
2015-06-02 04:08:30 +08:00
|
|
|
std::unique_ptr<MemoryBuffer>
|
2015-11-18 03:48:12 +08:00
|
|
|
LTOCodeGenerator::compileOptimized() {
|
2011-03-23 04:57:13 +08:00
|
|
|
const char *name;
|
2015-11-18 03:48:12 +08:00
|
|
|
if (!compileOptimizedToFile(&name))
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2011-03-23 04:57:13 +08:00
|
|
|
|
|
|
|
// read .o file into memory buffer
|
[NFC] Reordering parameters in getFile and getFileOrSTDIN
In future patches I will be setting the IsText parameter frequently so I will refactor the args to be in the following order. I have removed the FileSize parameter because it is never used.
```
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true, bool IsVolatile = false);
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileOrSTDIN(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true);
static ErrorOr<std::unique_ptr<MB>>
getFileAux(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
bool IsText, bool RequiresNullTerminator, bool IsVolatile);
static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getFile(const Twine &Filename, bool IsVolatile = false);
```
Reviewed By: jhenderson
Differential Revision: https://reviews.llvm.org/D99182
2021-03-25 21:47:25 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = MemoryBuffer::getFile(
|
|
|
|
name, /*IsText=*/false, /*RequiresNullTerminator=*/false);
|
2014-07-07 01:43:13 +08:00
|
|
|
if (std::error_code EC = BufferOrErr.getError()) {
|
2015-11-18 03:48:12 +08:00
|
|
|
emitError(EC.message());
|
2013-09-05 01:44:24 +08:00
|
|
|
sys::fs::remove(NativeObjectPath);
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2011-03-23 04:57:13 +08:00
|
|
|
}
|
2008-02-28 06:25:36 +08:00
|
|
|
|
2013-08-13 05:07:31 +08:00
|
|
|
// remove temp files
|
2013-09-05 01:44:24 +08:00
|
|
|
sys::fs::remove(NativeObjectPath);
|
2013-08-13 02:29:43 +08:00
|
|
|
|
2015-06-02 04:08:30 +08:00
|
|
|
return std::move(*BufferOrErr);
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2021-01-24 21:14:19 +08:00
|
|
|
bool LTOCodeGenerator::compile_to_file(const char **Name) {
|
|
|
|
if (!optimize())
|
2015-02-04 02:39:15 +08:00
|
|
|
return false;
|
|
|
|
|
2015-11-18 03:48:12 +08:00
|
|
|
return compileOptimizedToFile(Name);
|
2015-02-04 02:39:15 +08:00
|
|
|
}
|
|
|
|
|
2021-01-24 21:14:19 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> LTOCodeGenerator::compile() {
|
|
|
|
if (!optimize())
|
2015-02-04 02:39:15 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2015-11-18 03:48:12 +08:00
|
|
|
return compileOptimized();
|
2015-02-04 02:39:15 +08:00
|
|
|
}
|
|
|
|
|
2015-11-18 03:48:12 +08:00
|
|
|
bool LTOCodeGenerator::determineTarget() {
|
2014-04-15 14:32:26 +08:00
|
|
|
if (TargetMach)
|
2013-08-07 05:51:21 +08:00
|
|
|
return true;
|
2012-08-07 06:52:45 +08:00
|
|
|
|
2016-04-18 02:42:27 +08:00
|
|
|
TripleStr = MergedModule->getTargetTriple();
|
2015-08-22 10:25:53 +08:00
|
|
|
if (TripleStr.empty()) {
|
2012-10-13 01:39:25 +08:00
|
|
|
TripleStr = sys::getDefaultTargetTriple();
|
2015-08-25 06:22:53 +08:00
|
|
|
MergedModule->setTargetTriple(TripleStr);
|
2015-08-22 10:25:53 +08:00
|
|
|
}
|
2012-10-13 01:39:25 +08:00
|
|
|
llvm::Triple Triple(TripleStr);
|
2012-08-07 06:52:45 +08:00
|
|
|
|
|
|
|
// create target machine from info for merged modules
|
2015-11-18 03:48:12 +08:00
|
|
|
std::string ErrMsg;
|
2016-04-18 02:42:27 +08:00
|
|
|
MArch = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
|
|
|
|
if (!MArch) {
|
2015-11-18 03:48:12 +08:00
|
|
|
emitError(ErrMsg);
|
2013-08-07 05:51:21 +08:00
|
|
|
return false;
|
2015-11-18 03:48:12 +08:00
|
|
|
}
|
2012-08-07 06:52:45 +08:00
|
|
|
|
2014-04-26 05:46:51 +08:00
|
|
|
// Construct LTOModule, hand over ownership of module and target. Use MAttr as
|
|
|
|
// the default set of features.
|
2021-02-15 03:33:36 +08:00
|
|
|
SubtargetFeatures Features(join(Config.MAttrs, ""));
|
2012-10-13 01:39:25 +08:00
|
|
|
Features.getDefaultSubtargetFeatures(Triple);
|
2015-08-22 10:25:53 +08:00
|
|
|
FeatureStr = Features.getString();
|
2012-10-13 01:39:25 +08:00
|
|
|
// Set a default CPU for Darwin triples.
|
2021-02-15 03:33:36 +08:00
|
|
|
if (Config.CPU.empty() && Triple.isOSDarwin()) {
|
2012-10-13 01:39:25 +08:00
|
|
|
if (Triple.getArch() == llvm::Triple::x86_64)
|
2021-02-15 03:33:36 +08:00
|
|
|
Config.CPU = "core2";
|
2012-10-13 01:39:25 +08:00
|
|
|
else if (Triple.getArch() == llvm::Triple::x86)
|
2021-02-15 03:33:36 +08:00
|
|
|
Config.CPU = "yonah";
|
2020-09-03 23:43:21 +08:00
|
|
|
else if (Triple.isArm64e())
|
2021-02-15 03:33:36 +08:00
|
|
|
Config.CPU = "apple-a12";
|
2019-09-12 18:22:23 +08:00
|
|
|
else if (Triple.getArch() == llvm::Triple::aarch64 ||
|
|
|
|
Triple.getArch() == llvm::Triple::aarch64_32)
|
2021-02-15 03:33:36 +08:00
|
|
|
Config.CPU = "cyclone";
|
2012-10-13 01:39:25 +08:00
|
|
|
}
|
2013-10-01 00:39:19 +08:00
|
|
|
|
2016-04-18 02:42:27 +08:00
|
|
|
TargetMach = createTargetMachine();
|
2020-11-22 13:04:12 +08:00
|
|
|
assert(TargetMach && "Unable to create target machine");
|
|
|
|
|
2013-08-07 05:51:21 +08:00
|
|
|
return true;
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2016-04-18 02:42:27 +08:00
|
|
|
std::unique_ptr<TargetMachine> LTOCodeGenerator::createTargetMachine() {
|
2020-11-22 13:04:12 +08:00
|
|
|
assert(MArch && "MArch is not set!");
|
2017-08-03 10:16:21 +08:00
|
|
|
return std::unique_ptr<TargetMachine>(MArch->createTargetMachine(
|
2021-02-15 03:33:36 +08:00
|
|
|
TripleStr, Config.CPU, FeatureStr, Config.Options, Config.RelocModel,
|
|
|
|
None, Config.CGOptLevel));
|
2016-04-18 02:42:27 +08:00
|
|
|
}
|
|
|
|
|
2016-05-05 13:14:24 +08:00
|
|
|
// If a linkonce global is present in the MustPreserveSymbols, we need to make
|
|
|
|
// sure we honor this. To force the compiler to not drop it, we add it to the
|
|
|
|
// "llvm.compiler.used" global.
|
2016-05-06 04:05:33 +08:00
|
|
|
void LTOCodeGenerator::preserveDiscardableGVs(
|
2016-05-05 13:14:24 +08:00
|
|
|
Module &TheModule,
|
|
|
|
llvm::function_ref<bool(const GlobalValue &)> mustPreserveGV) {
|
2016-10-26 07:53:31 +08:00
|
|
|
std::vector<GlobalValue *> Used;
|
2016-05-05 13:14:24 +08:00
|
|
|
auto mayPreserveGlobal = [&](GlobalValue &GV) {
|
2016-09-03 00:37:31 +08:00
|
|
|
if (!GV.isDiscardableIfUnused() || GV.isDeclaration() ||
|
2016-10-26 07:53:31 +08:00
|
|
|
!mustPreserveGV(GV))
|
2016-05-05 13:14:24 +08:00
|
|
|
return;
|
2016-09-03 00:37:31 +08:00
|
|
|
if (GV.hasAvailableExternallyLinkage())
|
|
|
|
return emitWarning(
|
2016-05-06 04:05:33 +08:00
|
|
|
(Twine("Linker asked to preserve available_externally global: '") +
|
|
|
|
GV.getName() + "'").str());
|
2016-09-03 00:37:31 +08:00
|
|
|
if (GV.hasInternalLinkage())
|
|
|
|
return emitWarning((Twine("Linker asked to preserve internal global: '") +
|
2016-05-06 04:05:33 +08:00
|
|
|
GV.getName() + "'").str());
|
2016-10-26 07:53:31 +08:00
|
|
|
Used.push_back(&GV);
|
2016-05-05 13:14:24 +08:00
|
|
|
};
|
|
|
|
for (auto &GV : TheModule)
|
|
|
|
mayPreserveGlobal(GV);
|
|
|
|
for (auto &GV : TheModule.globals())
|
|
|
|
mayPreserveGlobal(GV);
|
|
|
|
for (auto &GV : TheModule.aliases())
|
|
|
|
mayPreserveGlobal(GV);
|
|
|
|
|
2016-10-26 07:53:31 +08:00
|
|
|
if (Used.empty())
|
2016-05-05 13:14:24 +08:00
|
|
|
return;
|
|
|
|
|
2016-10-26 07:53:31 +08:00
|
|
|
appendToCompilerUsed(TheModule, Used);
|
2016-05-05 13:14:24 +08:00
|
|
|
}
|
|
|
|
|
2010-03-13 02:44:54 +08:00
|
|
|
void LTOCodeGenerator::applyScopeRestrictions() {
|
2016-05-05 13:14:24 +08:00
|
|
|
if (ScopeRestrictionsDone)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Declare a callback for the internalize pass that will ask for every
|
|
|
|
// candidate GlobalValue if it can be internalized or not.
|
2016-10-13 04:12:19 +08:00
|
|
|
Mangler Mang;
|
2016-05-05 13:14:24 +08:00
|
|
|
SmallString<64> MangledName;
|
|
|
|
auto mustPreserveGV = [&](const GlobalValue &GV) -> bool {
|
|
|
|
// Unnamed globals can't be mangled, but they can't be preserved either.
|
|
|
|
if (!GV.hasName())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Need to mangle the GV as the "MustPreserveSymbols" StringSet is filled
|
|
|
|
// with the linker supplied name, which on Darwin includes a leading
|
|
|
|
// underscore.
|
|
|
|
MangledName.clear();
|
|
|
|
MangledName.reserve(GV.getName().size() + 1);
|
2016-10-13 04:12:19 +08:00
|
|
|
Mang.getNameWithPrefix(MangledName, &GV, /*CannotUsePrivateLabel=*/false);
|
2016-05-05 13:14:24 +08:00
|
|
|
return MustPreserveSymbols.count(MangledName);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Preserve linkonce value on linker request
|
|
|
|
preserveDiscardableGVs(*MergedModule, mustPreserveGV);
|
|
|
|
|
|
|
|
if (!ShouldInternalize)
|
2013-09-05 01:44:24 +08:00
|
|
|
return;
|
2010-03-13 02:44:54 +08:00
|
|
|
|
2016-04-13 13:36:06 +08:00
|
|
|
if (ShouldRestoreGlobalsLinkage) {
|
|
|
|
// Record the linkage type of non-local symbols so they can be restored
|
2016-05-05 13:14:20 +08:00
|
|
|
// prior
|
|
|
|
// to module splitting.
|
2016-04-13 13:36:06 +08:00
|
|
|
auto RecordLinkage = [&](const GlobalValue &GV) {
|
|
|
|
if (!GV.hasAvailableExternallyLinkage() && !GV.hasLocalLinkage() &&
|
|
|
|
GV.hasName())
|
|
|
|
ExternalSymbols.insert(std::make_pair(GV.getName(), GV.getLinkage()));
|
|
|
|
};
|
|
|
|
for (auto &GV : *MergedModule)
|
|
|
|
RecordLinkage(GV);
|
|
|
|
for (auto &GV : MergedModule->globals())
|
|
|
|
RecordLinkage(GV);
|
|
|
|
for (auto &GV : MergedModule->aliases())
|
|
|
|
RecordLinkage(GV);
|
|
|
|
}
|
|
|
|
|
2016-04-13 14:32:46 +08:00
|
|
|
// Update the llvm.compiler_used globals to force preserving libcalls and
|
|
|
|
// symbols referenced from asm
|
2016-06-23 03:50:42 +08:00
|
|
|
updateCompilerUsed(*MergedModule, *TargetMach, AsmUndefinedRefs);
|
2016-04-13 14:32:46 +08:00
|
|
|
|
2016-05-05 13:14:24 +08:00
|
|
|
internalizeModule(*MergedModule, mustPreserveGV);
|
2012-03-31 18:50:14 +08:00
|
|
|
|
2013-09-05 01:44:24 +08:00
|
|
|
ScopeRestrictionsDone = true;
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2016-01-19 07:24:54 +08:00
|
|
|
/// Restore original linkage for symbols that may have been internalized
|
|
|
|
void LTOCodeGenerator::restoreLinkageForExternals() {
|
|
|
|
if (!ShouldInternalize || !ShouldRestoreGlobalsLinkage)
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(ScopeRestrictionsDone &&
|
|
|
|
"Cannot externalize without internalization!");
|
|
|
|
|
|
|
|
if (ExternalSymbols.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto externalize = [this](GlobalValue &GV) {
|
|
|
|
if (!GV.hasLocalLinkage() || !GV.hasName())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto I = ExternalSymbols.find(GV.getName());
|
|
|
|
if (I == ExternalSymbols.end())
|
|
|
|
return;
|
|
|
|
|
2017-12-19 03:46:56 +08:00
|
|
|
GV.setLinkage(I->second);
|
|
|
|
};
|
|
|
|
|
|
|
|
llvm::for_each(MergedModule->functions(), externalize);
|
|
|
|
llvm::for_each(MergedModule->globals(), externalize);
|
|
|
|
llvm::for_each(MergedModule->aliases(), externalize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LTOCodeGenerator::verifyMergedModuleOnce() {
|
2016-04-21 01:48:22 +08:00
|
|
|
// Only run on the first call.
|
|
|
|
if (HasVerifiedInput)
|
|
|
|
return;
|
|
|
|
HasVerifiedInput = true;
|
|
|
|
|
2017-05-13 05:38:32 +08:00
|
|
|
bool BrokenDebugInfo = false;
|
2017-10-03 02:31:29 +08:00
|
|
|
if (verifyModule(*MergedModule, &dbgs(), &BrokenDebugInfo))
|
2016-04-21 01:48:22 +08:00
|
|
|
report_fatal_error("Broken module found, compilation aborted!");
|
2017-05-13 05:38:32 +08:00
|
|
|
if (BrokenDebugInfo) {
|
|
|
|
emitWarning("Invalid debug info found, debug info will be stripped");
|
|
|
|
StripDebugInfo(*MergedModule);
|
|
|
|
}
|
2016-04-21 01:48:22 +08:00
|
|
|
}
|
|
|
|
|
2016-11-19 02:06:28 +08:00
|
|
|
void LTOCodeGenerator::finishOptimizationRemarks() {
|
|
|
|
if (DiagnosticOutputFile) {
|
|
|
|
DiagnosticOutputFile->keep();
|
|
|
|
// FIXME: LTOCodeGenerator dtor is not invoked on Darwin
|
|
|
|
DiagnosticOutputFile->os().flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-27 04:26:43 +08:00
|
|
|
/// Optimize merged modules using various IPO passes
|
2021-01-24 21:14:19 +08:00
|
|
|
bool LTOCodeGenerator::optimize() {
|
2015-11-18 03:48:12 +08:00
|
|
|
if (!this->determineTarget())
|
2013-08-07 05:51:21 +08:00
|
|
|
return false;
|
2008-02-27 04:26:43 +08:00
|
|
|
|
2020-11-18 02:37:59 +08:00
|
|
|
auto DiagFileOrErr = lto::setupLLVMOptimizationRemarks(
|
|
|
|
Context, RemarksFilename, RemarksPasses, RemarksFormat,
|
|
|
|
RemarksWithHotness, RemarksHotnessThreshold);
|
2017-02-11 06:16:17 +08:00
|
|
|
if (!DiagFileOrErr) {
|
|
|
|
errs() << "Error: " << toString(DiagFileOrErr.takeError()) << "\n";
|
|
|
|
report_fatal_error("Can't get an output file for the remarks");
|
|
|
|
}
|
|
|
|
DiagnosticOutputFile = std::move(*DiagFileOrErr);
|
2016-11-19 02:06:28 +08:00
|
|
|
|
2019-04-19 20:36:41 +08:00
|
|
|
// Setup output file to emit statistics.
|
|
|
|
auto StatsFileOrErr = lto::setupStatsFile(LTOStatsFile);
|
|
|
|
if (!StatsFileOrErr) {
|
|
|
|
errs() << "Error: " << toString(StatsFileOrErr.takeError()) << "\n";
|
|
|
|
report_fatal_error("Can't get an output file for the statistics");
|
|
|
|
}
|
|
|
|
StatsFile = std::move(StatsFileOrErr.get());
|
|
|
|
|
2020-01-25 04:24:18 +08:00
|
|
|
// Currently there is no support for enabling whole program visibility via a
|
|
|
|
// linker option in the old LTO API, but this call allows it to be specified
|
|
|
|
// via the internal option. Must be done before WPD invoked via the optimizer
|
|
|
|
// pipeline run below.
|
|
|
|
updateVCallVisibilityInModule(*MergedModule,
|
2020-12-31 07:56:53 +08:00
|
|
|
/* WholeProgramVisibilityEnabledInLTO */ false,
|
|
|
|
// FIXME: This needs linker information via a
|
|
|
|
// TBD new interface.
|
|
|
|
/* DynamicExportSymbols */ {});
|
2020-01-25 04:24:18 +08:00
|
|
|
|
2016-04-12 14:34:10 +08:00
|
|
|
// We always run the verifier once on the merged module, the `DisableVerify`
|
|
|
|
// parameter only applies to subsequent verify.
|
2016-04-21 01:48:22 +08:00
|
|
|
verifyMergedModuleOnce();
|
2016-04-12 14:34:10 +08:00
|
|
|
|
2013-05-24 05:21:50 +08:00
|
|
|
// Mark which symbols can not be internalized
|
2012-04-10 06:18:01 +08:00
|
|
|
this->applyScopeRestrictions();
|
|
|
|
|
2020-08-27 02:17:26 +08:00
|
|
|
// Write LTOPostLink flag for passes that require all the modules.
|
|
|
|
MergedModule->addModuleFlag(Module::Error, "LTOPostLink", 1);
|
|
|
|
|
2012-10-09 00:39:34 +08:00
|
|
|
// Add an appropriate DataLayout instance for this module...
|
2015-08-25 05:15:35 +08:00
|
|
|
MergedModule->setDataLayout(TargetMach->createDataLayout());
|
2012-03-31 18:50:14 +08:00
|
|
|
|
2021-02-15 03:33:36 +08:00
|
|
|
ModuleSummaryIndex CombinedIndex(false);
|
|
|
|
TargetMach = createTargetMachine();
|
|
|
|
if (!opt(Config, TargetMach.get(), 0, *MergedModule, /*IsThinLTO=*/false,
|
|
|
|
/*ExportSummary=*/&CombinedIndex, /*ImportSummary=*/nullptr,
|
|
|
|
/*CmdArgs*/ std::vector<uint8_t>())) {
|
|
|
|
emitError("LTO middle-end optimizations failed");
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-04 02:39:15 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:40:57 +08:00
|
|
|
bool LTOCodeGenerator::compileOptimized(AddStreamFn AddStream,
|
2021-02-15 03:33:36 +08:00
|
|
|
unsigned ParallelismLevel) {
|
2015-11-18 03:48:12 +08:00
|
|
|
if (!this->determineTarget())
|
2015-02-04 02:39:15 +08:00
|
|
|
return false;
|
|
|
|
|
2016-04-21 01:48:22 +08:00
|
|
|
// We always run the verifier once on the merged module. If it has already
|
|
|
|
// been called in optimize(), this call will return early.
|
|
|
|
verifyMergedModuleOnce();
|
|
|
|
|
2016-01-19 07:24:54 +08:00
|
|
|
// Re-externalize globals that may have been internalized to increase scope
|
|
|
|
// for splitting
|
|
|
|
restoreLinkageForExternals();
|
|
|
|
|
2021-02-15 03:33:36 +08:00
|
|
|
ModuleSummaryIndex CombinedIndex(false);
|
|
|
|
|
|
|
|
Config.CodeGenOnly = true;
|
|
|
|
Error Err = backend(Config, AddStream, ParallelismLevel, *MergedModule,
|
|
|
|
CombinedIndex);
|
|
|
|
assert(!Err && "unexpected code-generation failure");
|
|
|
|
(void)Err;
|
2009-07-27 06:16:39 +08:00
|
|
|
|
2019-04-19 20:36:41 +08:00
|
|
|
// If statistics were requested, save them to the specified file or
|
|
|
|
// print them out after codegen.
|
|
|
|
if (StatsFile)
|
|
|
|
PrintStatisticsJSON(StatsFile->os());
|
|
|
|
else if (AreStatisticsEnabled())
|
|
|
|
PrintStatistics();
|
|
|
|
|
2017-05-16 17:43:21 +08:00
|
|
|
reportAndResetTimings();
|
2016-02-17 05:41:51 +08:00
|
|
|
|
2016-11-29 00:51:49 +08:00
|
|
|
finishOptimizationRemarks();
|
|
|
|
|
2013-08-07 05:51:21 +08:00
|
|
|
return true;
|
2008-02-27 04:26:43 +08:00
|
|
|
}
|
|
|
|
|
2020-06-22 18:14:53 +08:00
|
|
|
void LTOCodeGenerator::setCodeGenDebugOptions(ArrayRef<StringRef> Options) {
|
2019-11-20 06:04:59 +08:00
|
|
|
for (StringRef Option : Options)
|
2020-06-22 18:14:53 +08:00
|
|
|
CodegenOptions.push_back(Option.str());
|
2008-07-09 05:14:10 +08:00
|
|
|
}
|
2013-10-02 22:36:23 +08:00
|
|
|
|
|
|
|
void LTOCodeGenerator::parseCodeGenDebugOptions() {
|
2021-04-01 00:42:12 +08:00
|
|
|
if (!CodegenOptions.empty())
|
|
|
|
llvm::parseCommandLineOptions(CodegenOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::parseCommandLineOptions(std::vector<std::string> &Options) {
|
|
|
|
if (!Options.empty()) {
|
2015-08-21 12:45:55 +08:00
|
|
|
// ParseCommandLineOptions() expects argv[0] to be program name.
|
|
|
|
std::vector<const char *> CodegenArgv(1, "libLLVMLTO");
|
2021-04-01 00:42:12 +08:00
|
|
|
for (std::string &Arg : Options)
|
2015-08-21 12:45:55 +08:00
|
|
|
CodegenArgv.push_back(Arg.c_str());
|
|
|
|
cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data());
|
|
|
|
}
|
2013-10-02 22:36:23 +08:00
|
|
|
}
|
2014-01-16 06:04:35 +08:00
|
|
|
|
2017-09-16 04:10:09 +08:00
|
|
|
void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI) {
|
2014-01-16 06:04:35 +08:00
|
|
|
// Map the LLVM internal diagnostic severity to the LTO diagnostic severity.
|
|
|
|
lto_codegen_diagnostic_severity_t Severity;
|
|
|
|
switch (DI.getSeverity()) {
|
|
|
|
case DS_Error:
|
|
|
|
Severity = LTO_DS_ERROR;
|
|
|
|
break;
|
|
|
|
case DS_Warning:
|
|
|
|
Severity = LTO_DS_WARNING;
|
|
|
|
break;
|
2014-02-28 17:08:45 +08:00
|
|
|
case DS_Remark:
|
|
|
|
Severity = LTO_DS_REMARK;
|
|
|
|
break;
|
2014-01-16 06:04:35 +08:00
|
|
|
case DS_Note:
|
|
|
|
Severity = LTO_DS_NOTE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Create the string that will be reported to the external diagnostic handler.
|
2014-06-27 06:52:05 +08:00
|
|
|
std::string MsgStorage;
|
|
|
|
raw_string_ostream Stream(MsgStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(Stream);
|
2014-01-16 06:04:35 +08:00
|
|
|
DI.print(DP);
|
2014-06-27 06:52:05 +08:00
|
|
|
Stream.flush();
|
2014-01-16 06:04:35 +08:00
|
|
|
|
|
|
|
// If this method has been called it means someone has set up an external
|
|
|
|
// diagnostic handler. Assert on that.
|
|
|
|
assert(DiagHandler && "Invalid diagnostic handler");
|
2014-06-27 06:52:05 +08:00
|
|
|
(*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
|
2014-01-16 06:04:35 +08:00
|
|
|
}
|
|
|
|
|
2017-09-16 04:10:09 +08:00
|
|
|
namespace {
|
|
|
|
struct LTODiagnosticHandler : public DiagnosticHandler {
|
|
|
|
LTOCodeGenerator *CodeGenerator;
|
|
|
|
LTODiagnosticHandler(LTOCodeGenerator *CodeGenPtr)
|
|
|
|
: CodeGenerator(CodeGenPtr) {}
|
|
|
|
bool handleDiagnostics(const DiagnosticInfo &DI) override {
|
|
|
|
CodeGenerator->DiagnosticHandler(DI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-01-16 06:04:35 +08:00
|
|
|
void
|
|
|
|
LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
|
|
|
|
void *Ctxt) {
|
|
|
|
this->DiagHandler = DiagHandler;
|
|
|
|
this->DiagContext = Ctxt;
|
|
|
|
if (!DiagHandler)
|
2017-09-16 04:10:09 +08:00
|
|
|
return Context.setDiagnosticHandler(nullptr);
|
2014-01-16 06:04:35 +08:00
|
|
|
// Register the LTOCodeGenerator stub in the LLVMContext to forward the
|
|
|
|
// diagnostic to the external DiagHandler.
|
2019-08-15 23:54:37 +08:00
|
|
|
Context.setDiagnosticHandler(std::make_unique<LTODiagnosticHandler>(this),
|
2017-09-16 04:10:09 +08:00
|
|
|
true);
|
2014-01-16 06:04:35 +08:00
|
|
|
}
|
2015-11-18 03:48:12 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class LTODiagnosticInfo : public DiagnosticInfo {
|
|
|
|
const Twine &Msg;
|
|
|
|
public:
|
|
|
|
LTODiagnosticInfo(const Twine &DiagMsg, DiagnosticSeverity Severity=DS_Error)
|
|
|
|
: DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
|
|
|
|
void print(DiagnosticPrinter &DP) const override { DP << Msg; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void LTOCodeGenerator::emitError(const std::string &ErrMsg) {
|
|
|
|
if (DiagHandler)
|
|
|
|
(*DiagHandler)(LTO_DS_ERROR, ErrMsg.c_str(), DiagContext);
|
|
|
|
else
|
|
|
|
Context.diagnose(LTODiagnosticInfo(ErrMsg));
|
|
|
|
}
|
2016-05-06 04:05:33 +08:00
|
|
|
|
|
|
|
void LTOCodeGenerator::emitWarning(const std::string &ErrMsg) {
|
|
|
|
if (DiagHandler)
|
|
|
|
(*DiagHandler)(LTO_DS_WARNING, ErrMsg.c_str(), DiagContext);
|
|
|
|
else
|
|
|
|
Context.diagnose(LTODiagnosticInfo(ErrMsg, DS_Warning));
|
|
|
|
}
|