2010-06-08 07:20:08 +08:00
|
|
|
//===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
|
|
|
|
//
|
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
|
2010-06-08 07:20:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-06-16 01:48:49 +08:00
|
|
|
#include "clang/CodeGen/BackendUtil.h"
|
2018-12-11 11:18:39 +08:00
|
|
|
#include "clang/Basic/CodeGenOptions.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2011-07-06 06:02:36 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/TargetOptions.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2013-12-27 16:11:08 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2017-01-06 00:02:32 +08:00
|
|
|
#include "clang/Lex/HeaderSearchOptions.h"
|
2016-12-15 10:19:17 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2015-09-26 09:25:08 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2014-05-08 10:28:32 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
2016-05-12 00:26:03 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2020-02-28 07:43:53 +08:00
|
|
|
#include "llvm/Analysis/StackSafetyAnalysis.h"
|
2015-01-15 10:16:55 +08:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2015-01-31 19:18:46 +08:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2016-11-11 13:35:12 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
2016-12-24 04:44:01 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriterPass.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "llvm/CodeGen/RegAllocRegistry.h"
|
|
|
|
#include "llvm/CodeGen/SchedulerRegistry.h"
|
2018-01-10 06:03:47 +08:00
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-12 19:11:50 +08:00
|
|
|
#include "llvm/IR/IRPrintingPasses.h"
|
2015-02-13 17:57:03 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2016-12-24 04:44:01 +08:00
|
|
|
#include "llvm/IR/ModuleSummaryIndex.h"
|
2020-05-28 14:12:36 +08:00
|
|
|
#include "llvm/IR/PassManager.h"
|
2014-01-13 17:26:48 +08:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2016-08-13 02:12:08 +08:00
|
|
|
#include "llvm/LTO/LTOBackend.h"
|
2017-01-06 00:02:32 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2011-06-29 09:14:32 +08:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
2016-12-24 04:44:01 +08:00
|
|
|
#include "llvm/Passes/PassBuilder.h"
|
2019-02-03 07:19:32 +08:00
|
|
|
#include "llvm/Passes/PassPlugin.h"
|
2019-08-14 15:11:09 +08:00
|
|
|
#include "llvm/Passes/StandardInstrumentations.h"
|
2018-11-18 02:04:13 +08:00
|
|
|
#include "llvm/Support/BuryPointer.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2016-08-13 02:12:08 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
2011-08-25 02:09:14 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2019-03-30 16:42:48 +08:00
|
|
|
#include "llvm/Support/TimeProfiler.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2020-05-09 22:10:51 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2010-06-08 07:20:08 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2016-10-04 06:44:48 +08:00
|
|
|
#include "llvm/Transforms/Coroutines.h"
|
2019-12-26 21:00:00 +08:00
|
|
|
#include "llvm/Transforms/Coroutines/CoroCleanup.h"
|
|
|
|
#include "llvm/Transforms/Coroutines/CoroEarly.h"
|
|
|
|
#include "llvm/Transforms/Coroutines/CoroElide.h"
|
|
|
|
#include "llvm/Transforms/Coroutines/CoroSplit.h"
|
2011-08-03 05:51:02 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2016-08-17 11:09:11 +08:00
|
|
|
#include "llvm/Transforms/IPO/AlwaysInliner.h"
|
2020-01-25 04:24:18 +08:00
|
|
|
#include "llvm/Transforms/IPO/LowerTypeTests.h"
|
2011-08-03 05:51:02 +08:00
|
|
|
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
|
2017-06-02 07:27:51 +08:00
|
|
|
#include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
|
2018-04-24 08:59:22 +08:00
|
|
|
#include "llvm/Transforms/InstCombine/InstCombine.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
2019-02-14 06:22:48 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
|
2017-11-14 09:47:24 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
|
2020-11-13 05:19:04 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
|
2018-03-24 06:16:59 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
|
2019-05-15 05:17:21 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
|
2019-04-26 01:52:43 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
|
2020-09-15 00:12:13 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/MemProfiler.h"
|
2019-01-16 17:28:01 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
|
2019-07-26 04:53:15 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
|
2019-01-16 17:28:01 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
|
2013-01-28 09:36:00 +08:00
|
|
|
#include "llvm/Transforms/ObjCARC.h"
|
2011-08-03 05:51:02 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2016-03-11 17:02:43 +08:00
|
|
|
#include "llvm/Transforms/Scalar/GVN.h"
|
2020-07-21 23:33:24 +08:00
|
|
|
#include "llvm/Transforms/Scalar/LowerMatrixIntrinsics.h"
|
2018-03-29 01:45:10 +08:00
|
|
|
#include "llvm/Transforms/Utils.h"
|
2019-01-05 03:05:01 +08:00
|
|
|
#include "llvm/Transforms/Utils/CanonicalizeAliases.h"
|
2019-06-21 03:35:25 +08:00
|
|
|
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
|
2017-06-30 07:08:38 +08:00
|
|
|
#include "llvm/Transforms/Utils/NameAnonGlobals.h"
|
2015-01-09 13:10:20 +08:00
|
|
|
#include "llvm/Transforms/Utils/SymbolRewriter.h"
|
2020-05-08 09:18:37 +08:00
|
|
|
#include "llvm/Transforms/Utils/UniqueInternalLinkageNames.h"
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2010-06-08 07:20:08 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace llvm;
|
|
|
|
|
2019-06-08 23:37:47 +08:00
|
|
|
#define HANDLE_EXTENSION(Ext) \
|
|
|
|
llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
|
|
|
|
#include "llvm/Support/Extension.def"
|
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
namespace {
|
|
|
|
|
2017-02-14 00:07:05 +08:00
|
|
|
// Default filename used for profile generation.
|
|
|
|
static constexpr StringLiteral DefaultProfileGenName = "default_%m.profraw";
|
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
class EmitAssemblyHelper {
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags;
|
2017-01-06 00:02:32 +08:00
|
|
|
const HeaderSearchOptions &HSOpts;
|
2010-06-08 07:20:08 +08:00
|
|
|
const CodeGenOptions &CodeGenOpts;
|
2011-12-03 06:17:00 +08:00
|
|
|
const clang::TargetOptions &TargetOpts;
|
2011-07-06 06:02:36 +08:00
|
|
|
const LangOptions &LangOpts;
|
2010-06-08 07:20:08 +08:00
|
|
|
Module *TheModule;
|
|
|
|
|
|
|
|
Timer CodeGenerationTime;
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS;
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2015-02-01 20:26:23 +08:00
|
|
|
TargetIRAnalysis getTargetIRAnalysis() const {
|
2015-01-31 19:18:46 +08:00
|
|
|
if (TM)
|
2015-02-01 20:26:23 +08:00
|
|
|
return TM->getTargetIRAnalysis();
|
2015-01-31 19:18:46 +08:00
|
|
|
|
2015-02-01 20:26:23 +08:00
|
|
|
return TargetIRAnalysis();
|
2015-01-31 19:18:46 +08:00
|
|
|
}
|
|
|
|
|
2016-08-13 02:12:08 +08:00
|
|
|
void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM);
|
2012-10-24 08:53:38 +08:00
|
|
|
|
2015-04-14 23:15:49 +08:00
|
|
|
/// Generates the TargetMachine.
|
2016-07-15 08:55:40 +08:00
|
|
|
/// Leaves TM unchanged if it is unable to create the target machine.
|
2012-10-24 11:52:31 +08:00
|
|
|
/// Some of our clang tests specify triples which are not built
|
|
|
|
/// into clang. This is okay because these tests check the generated
|
|
|
|
/// IR, and they require DataLayout which depends on the triple.
|
|
|
|
/// In this case, we allow this method to fail and not report an error.
|
|
|
|
/// When MustCreateTM is used, we print an error if we are unable to load
|
|
|
|
/// the requested target.
|
2016-07-15 08:55:40 +08:00
|
|
|
void CreateTargetMachine(bool MustCreateTM);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2015-04-14 23:15:49 +08:00
|
|
|
/// Add passes necessary to emit assembly or LLVM IR.
|
2010-06-08 07:20:08 +08:00
|
|
|
///
|
|
|
|
/// \return True on success.
|
2016-07-15 08:55:40 +08:00
|
|
|
bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
|
2018-05-23 02:52:37 +08:00
|
|
|
raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS);
|
|
|
|
|
|
|
|
std::unique_ptr<llvm::ToolOutputFile> openOutputFile(StringRef Path) {
|
|
|
|
std::error_code EC;
|
2019-08-15 07:04:18 +08:00
|
|
|
auto F = std::make_unique<llvm::ToolOutputFile>(Path, EC,
|
2019-08-05 13:43:48 +08:00
|
|
|
llvm::sys::fs::OF_None);
|
2018-05-23 02:52:37 +08:00
|
|
|
if (EC) {
|
|
|
|
Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message();
|
|
|
|
F.reset();
|
|
|
|
}
|
|
|
|
return F;
|
|
|
|
}
|
2010-06-08 07:20:08 +08:00
|
|
|
|
|
|
|
public:
|
2017-01-06 00:02:32 +08:00
|
|
|
EmitAssemblyHelper(DiagnosticsEngine &_Diags,
|
|
|
|
const HeaderSearchOptions &HeaderSearchOpts,
|
|
|
|
const CodeGenOptions &CGOpts,
|
2011-12-03 06:17:00 +08:00
|
|
|
const clang::TargetOptions &TOpts,
|
2016-01-09 01:04:29 +08:00
|
|
|
const LangOptions &LOpts, Module *M)
|
2017-01-06 00:02:32 +08:00
|
|
|
: Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
|
|
|
|
TargetOpts(TOpts), LangOpts(LOpts), TheModule(M),
|
|
|
|
CodeGenerationTime("codegen", "Code Generation Time") {}
|
2010-06-08 07:20:08 +08:00
|
|
|
|
|
|
|
~EmitAssemblyHelper() {
|
2013-12-21 04:26:53 +08:00
|
|
|
if (CodeGenOpts.DisableFree)
|
Provide a BuryPointer for unique_ptrs.
In theory, it'd be nice if we could move to a case where all buried
pointers were buried via unique_ptr to demonstrate that the program had
finished with the value (that we could really have cleanly deallocated
it) but instead chose to bury it.
I think the main reason that's not possible right now is the various
IntrusiveRefCntPtrs in the Frontend, sharing ownership for a variety of
compiler bits (see the various similar
"CompilerInstance::releaseAndLeak*" functions). I have yet to figure out
their correct ownership semantics - but perhaps, even if the
intrusiveness can be removed, the shared ownership may yet remain and
that would lead to a non-unique burying as is there today. (though we
could model that a little better - by passing in a shared_ptr, etc -
rather than needing the two step that's currently used in those other
releaseAndLeak* functions)
This might be a bit more robust if BuryPointer took the boolean:
BuryPointer(bool, unique_ptr<T>)
and the choice to bury was made internally - that way, even when
DisableFree was not set, the unique_ptr would still be null in the
caller and there'd be no chance of accidentally having a different
codepath where the value is used after burial in !DisableFree, but it
becomes null only in DisableFree, etc...
llvm-svn: 216742
2014-08-30 00:53:14 +08:00
|
|
|
BuryPointer(std::move(TM));
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<TargetMachine> TM;
|
2013-12-21 04:26:53 +08:00
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
void EmitAssembly(BackendAction Action,
|
|
|
|
std::unique_ptr<raw_pwrite_stream> OS);
|
2016-12-24 04:44:01 +08:00
|
|
|
|
|
|
|
void EmitAssemblyWithNewPassManager(BackendAction Action,
|
|
|
|
std::unique_ptr<raw_pwrite_stream> OS);
|
2010-06-08 07:20:08 +08:00
|
|
|
};
|
|
|
|
|
2012-12-28 17:31:34 +08:00
|
|
|
// We need this wrapper to access LangOpts and CGOpts from extension functions
|
|
|
|
// that we add to the PassManagerBuilder.
|
2012-11-30 06:36:21 +08:00
|
|
|
class PassManagerBuilderWrapper : public PassManagerBuilder {
|
|
|
|
public:
|
2017-04-25 03:34:12 +08:00
|
|
|
PassManagerBuilderWrapper(const Triple &TargetTriple,
|
|
|
|
const CodeGenOptions &CGOpts,
|
2012-12-04 03:12:58 +08:00
|
|
|
const LangOptions &LangOpts)
|
2017-04-25 03:34:12 +08:00
|
|
|
: PassManagerBuilder(), TargetTriple(TargetTriple), CGOpts(CGOpts),
|
|
|
|
LangOpts(LangOpts) {}
|
|
|
|
const Triple &getTargetTriple() const { return TargetTriple; }
|
2012-12-04 03:12:58 +08:00
|
|
|
const CodeGenOptions &getCGOpts() const { return CGOpts; }
|
2012-11-30 06:36:21 +08:00
|
|
|
const LangOptions &getLangOpts() const { return LangOpts; }
|
2017-04-25 03:34:12 +08:00
|
|
|
|
2012-11-30 06:36:21 +08:00
|
|
|
private:
|
2017-04-25 03:34:12 +08:00
|
|
|
const Triple &TargetTriple;
|
2012-12-04 03:12:58 +08:00
|
|
|
const CodeGenOptions &CGOpts;
|
2012-11-30 06:36:21 +08:00
|
|
|
const LangOptions &LangOpts;
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2012-01-18 04:54:51 +08:00
|
|
|
static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
|
|
|
|
if (Builder.OptLevel > 0)
|
|
|
|
PM.add(createObjCARCAPElimPass());
|
|
|
|
}
|
|
|
|
|
2011-07-06 06:02:36 +08:00
|
|
|
static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
|
|
|
|
if (Builder.OptLevel > 0)
|
|
|
|
PM.add(createObjCARCExpandPass());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
|
|
|
|
if (Builder.OptLevel > 0)
|
|
|
|
PM.add(createObjCARCOptPass());
|
|
|
|
}
|
|
|
|
|
2014-03-04 04:06:18 +08:00
|
|
|
static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManagerBase &PM) {
|
2014-03-04 04:06:18 +08:00
|
|
|
PM.add(createAddDiscriminatorsPass());
|
|
|
|
}
|
|
|
|
|
2012-05-23 01:19:45 +08:00
|
|
|
static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
|
2016-06-02 08:24:20 +08:00
|
|
|
legacy::PassManagerBase &PM) {
|
2017-11-14 09:30:04 +08:00
|
|
|
PM.add(createBoundsCheckingLegacyPass());
|
2012-05-23 01:19:45 +08:00
|
|
|
}
|
|
|
|
|
2019-07-26 04:53:15 +08:00
|
|
|
static SanitizerCoverageOptions
|
|
|
|
getSancovOptsFromCGOpts(const CodeGenOptions &CGOpts) {
|
2015-05-08 02:31:29 +08:00
|
|
|
SanitizerCoverageOptions Opts;
|
|
|
|
Opts.CoverageType =
|
|
|
|
static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
|
|
|
|
Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
|
|
|
|
Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
|
|
|
|
Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
|
2016-08-30 09:27:03 +08:00
|
|
|
Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
|
|
|
|
Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
|
2015-05-08 02:31:29 +08:00
|
|
|
Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
|
2016-02-18 05:34:43 +08:00
|
|
|
Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
|
2016-09-14 09:39:49 +08:00
|
|
|
Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
|
2017-05-06 07:28:18 +08:00
|
|
|
Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune;
|
2017-06-09 06:58:19 +08:00
|
|
|
Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters;
|
2020-04-09 17:33:36 +08:00
|
|
|
Opts.InlineBoolFlag = CGOpts.SanitizeCoverageInlineBoolFlag;
|
2017-07-28 08:10:10 +08:00
|
|
|
Opts.PCTable = CGOpts.SanitizeCoveragePCTable;
|
2017-08-19 02:43:30 +08:00
|
|
|
Opts.StackDepth = CGOpts.SanitizeCoverageStackDepth;
|
2019-07-26 04:53:15 +08:00
|
|
|
return Opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
|
|
|
|
legacy::PassManagerBase &PM) {
|
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper &>(Builder);
|
|
|
|
const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
|
|
|
|
auto Opts = getSancovOptsFromCGOpts(CGOpts);
|
Implement `-fsanitize-coverage-whitelist` and `-fsanitize-coverage-blacklist` for clang
Summary:
This commit adds two command-line options to clang.
These options let the user decide which functions will receive SanitizerCoverage instrumentation.
This is most useful in the libFuzzer use case, where it enables targeted coverage-guided fuzzing.
Patch by Yannis Juglaret of DGA-MI, Rennes, France
libFuzzer tests its target against an evolving corpus, and relies on SanitizerCoverage instrumentation to collect the code coverage information that drives corpus evolution. Currently, libFuzzer collects such information for all functions of the target under test, and adds to the corpus every mutated sample that finds a new code coverage path in any function of the target. We propose instead to let the user specify which functions' code coverage information is relevant for building the upcoming fuzzing campaign's corpus. To this end, we add two new command line options for clang, enabling targeted coverage-guided fuzzing with libFuzzer. We see targeted coverage guided fuzzing as a simple way to leverage libFuzzer for big targets with thousands of functions or multiple dependencies. We publish this patch as work from DGA-MI of Rennes, France, with proper authorization from the hierarchy.
Targeted coverage-guided fuzzing can accelerate bug finding for two reasons. First, the compiler will avoid costly instrumentation for non-relevant functions, accelerating fuzzer execution for each call to any of these functions. Second, the built fuzzer will produce and use a more accurate corpus, because it will not keep the samples that find new coverage paths in non-relevant functions.
The two new command line options are `-fsanitize-coverage-whitelist` and `-fsanitize-coverage-blacklist`. They accept files in the same format as the existing `-fsanitize-blacklist` option <https://clang.llvm.org/docs/SanitizerSpecialCaseList.html#format>. The new options influence SanitizerCoverage so that it will only instrument a subset of the functions in the target. We explain these options in detail in `clang/docs/SanitizerCoverage.rst`.
Consider now the woff2 fuzzing example from the libFuzzer tutorial <https://github.com/google/fuzzer-test-suite/blob/master/tutorial/libFuzzerTutorial.md>. We are aware that we cannot conclude much from this example because mutating compressed data is generally a bad idea, but let us use it anyway as an illustration for its simplicity. Let us use an empty blacklist together with one of the three following whitelists:
```
# (a)
src:*
fun:*
# (b)
src:SRC/*
fun:*
# (c)
src:SRC/src/woff2_dec.cc
fun:*
```
Running the built fuzzers shows how many instrumentation points the compiler adds, the fuzzer will output //XXX PCs//. Whitelist (a) is the instrument-everything whitelist, it produces 11912 instrumentation points. Whitelist (b) focuses coverage to instrument woff2 source code only, ignoring the dependency code for brotli (de)compression; it produces 3984 instrumented instrumentation points. Whitelist (c) focuses coverage to only instrument functions in the main file that deals with WOFF2 to TTF conversion, resulting in 1056 instrumentation points.
For experimentation purposes, we ran each fuzzer approximately 100 times, single process, with the initial corpus provided in the tutorial. We let the fuzzer run until it either found the heap buffer overflow or went out of memory. On this simple example, whitelists (b) and (c) found the heap buffer overflow more reliably and 5x faster than whitelist (a). The average execution times when finding the heap buffer overflow were as follows: (a) 904 s, (b) 156 s, and (c) 176 s.
We explain these results by the fact that WOFF2 to TTF conversion calls the brotli decompression algorithm's functions, which are mostly irrelevant for finding bugs in WOFF2 font reconstruction but nevertheless instrumented and used by whitelist (a) to guide fuzzing. This results in longer execution time for these functions and a partially irrelevant corpus. Contrary to whitelist (a), whitelists (b) and (c) will execute brotli-related functions without instrumentation overhead, and ignore new code paths found in them. This results in faster bug finding for WOFF2 font reconstruction.
The results for whitelist (b) are similar to the ones for whitelist (c). Indeed, WOFF2 to TTF conversion calls functions that are mostly located in SRC/src/woff2_dec.cc. The 2892 extra instrumentation points allowed by whitelist (b) do not tamper with bug finding, even though they are mostly irrelevant, simply because most of these functions do not get called. We get a slightly faster average time for bug finding with whitelist (b), which might indicate that some of the extra instrumentation points are actually relevant, or might just be random noise.
Reviewers: kcc, morehouse, vitalybuka
Reviewed By: morehouse, vitalybuka
Subscribers: pratyai, vitalybuka, eternalsakura, xwlin222, dende, srhines, kubamracek, #sanitizers, lebedev.ri, hiraditya, cfe-commits, llvm-commits
Tags: #clang, #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D63616
2020-04-11 01:42:41 +08:00
|
|
|
PM.add(createModuleSanitizerCoverageLegacyPassPass(
|
2020-06-20 13:22:47 +08:00
|
|
|
Opts, CGOpts.SanitizeCoverageAllowlistFiles,
|
|
|
|
CGOpts.SanitizeCoverageBlocklistFiles));
|
2014-11-12 06:15:07 +08:00
|
|
|
}
|
|
|
|
|
2017-04-25 03:34:12 +08:00
|
|
|
// Check if ASan should use GC-friendly instrumentation for globals.
|
|
|
|
// First of all, there is no point if -fdata-sections is off (expect for MachO,
|
|
|
|
// where this is not a factor). Also, on ELF this feature requires an assembler
|
|
|
|
// extension that only works with -integrated-as at the moment.
|
|
|
|
static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) {
|
2017-05-10 05:57:43 +08:00
|
|
|
if (!CGOpts.SanitizeAddressGlobalsDeadStripping)
|
|
|
|
return false;
|
2017-04-25 03:34:12 +08:00
|
|
|
switch (T.getObjectFormat()) {
|
|
|
|
case Triple::MachO:
|
|
|
|
case Triple::COFF:
|
2017-04-26 08:51:06 +08:00
|
|
|
return true;
|
2017-04-25 03:34:12 +08:00
|
|
|
case Triple::ELF:
|
|
|
|
return CGOpts.DataSections && !CGOpts.DisableIntegratedAS;
|
2020-06-19 19:43:35 +08:00
|
|
|
case Triple::GOFF:
|
|
|
|
llvm::report_fatal_error("ASan not implemented for GOFF");
|
[sanitizers] Use covering ObjectFormatType switches
Summary:
This patch removes the `default` case from some switches on
`llvm::Triple::ObjectFormatType`, and cases for the missing enumerators
(`UnknownObjectFormat`, `Wasm`, and `XCOFF`) are then added.
For `UnknownObjectFormat`, the effect of the action for the `default`
case is maintained; otherwise, where `llvm_unreachable` is called,
`report_fatal_error` is used instead.
Where the `default` case returns a default value, `report_fatal_error`
is used for XCOFF as a placeholder. For `Wasm`, the effect of the action
for the `default` case in maintained.
The code is structured to avoid strongly implying that the `Wasm` case
is present for any reason other than to make the switch cover all
`ObjectFormatType` enumerator values.
Reviewers: sfertile, jasonliu, daltenty
Reviewed By: sfertile
Subscribers: hiraditya, aheejin, sunfish, llvm-commits, cfe-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64222
llvm-svn: 366544
2019-07-19 16:46:18 +08:00
|
|
|
case Triple::XCOFF:
|
|
|
|
llvm::report_fatal_error("ASan not implemented for XCOFF.");
|
|
|
|
case Triple::Wasm:
|
|
|
|
case Triple::UnknownObjectFormat:
|
|
|
|
break;
|
2017-04-25 03:34:12 +08:00
|
|
|
}
|
[sanitizers] Use covering ObjectFormatType switches
Summary:
This patch removes the `default` case from some switches on
`llvm::Triple::ObjectFormatType`, and cases for the missing enumerators
(`UnknownObjectFormat`, `Wasm`, and `XCOFF`) are then added.
For `UnknownObjectFormat`, the effect of the action for the `default`
case is maintained; otherwise, where `llvm_unreachable` is called,
`report_fatal_error` is used instead.
Where the `default` case returns a default value, `report_fatal_error`
is used for XCOFF as a placeholder. For `Wasm`, the effect of the action
for the `default` case in maintained.
The code is structured to avoid strongly implying that the `Wasm` case
is present for any reason other than to make the switch cover all
`ObjectFormatType` enumerator values.
Reviewers: sfertile, jasonliu, daltenty
Reviewed By: sfertile
Subscribers: hiraditya, aheejin, sunfish, llvm-commits, cfe-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64222
llvm-svn: 366544
2019-07-19 16:46:18 +08:00
|
|
|
return false;
|
2017-04-25 03:34:12 +08:00
|
|
|
}
|
|
|
|
|
2020-09-15 00:12:13 +08:00
|
|
|
static void addMemProfilerPasses(const PassManagerBuilder &Builder,
|
|
|
|
legacy::PassManagerBase &PM) {
|
|
|
|
PM.add(createMemProfilerFunctionPass());
|
|
|
|
PM.add(createModuleMemProfilerLegacyPassPass());
|
2020-08-14 07:29:38 +08:00
|
|
|
}
|
|
|
|
|
2012-11-30 06:36:21 +08:00
|
|
|
static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManagerBase &PM) {
|
2015-11-11 18:45:48 +08:00
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper&>(Builder);
|
2017-04-25 03:34:12 +08:00
|
|
|
const Triple &T = BuilderWrapper.getTargetTriple();
|
2015-11-11 18:45:48 +08:00
|
|
|
const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
|
|
|
|
bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address);
|
2016-06-02 08:24:20 +08:00
|
|
|
bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope;
|
2018-12-05 09:44:31 +08:00
|
|
|
bool UseOdrIndicator = CGOpts.SanitizeAddressUseOdrIndicator;
|
2017-04-25 03:34:12 +08:00
|
|
|
bool UseGlobalsGC = asanUseGlobalsGC(T, CGOpts);
|
2016-06-02 08:24:20 +08:00
|
|
|
PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover,
|
|
|
|
UseAfterScope));
|
2019-02-14 06:22:48 +08:00
|
|
|
PM.add(createModuleAddressSanitizerLegacyPassPass(
|
|
|
|
/*CompileKernel*/ false, Recover, UseGlobalsGC, UseOdrIndicator));
|
2015-06-19 20:19:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
|
|
|
|
legacy::PassManagerBase &PM) {
|
2016-06-02 08:24:20 +08:00
|
|
|
PM.add(createAddressSanitizerFunctionPass(
|
2018-04-14 02:05:21 +08:00
|
|
|
/*CompileKernel*/ true, /*Recover*/ true, /*UseAfterScope*/ false));
|
2019-02-14 06:22:48 +08:00
|
|
|
PM.add(createModuleAddressSanitizerLegacyPassPass(
|
2018-12-05 09:44:31 +08:00
|
|
|
/*CompileKernel*/ true, /*Recover*/ true, /*UseGlobalsGC*/ true,
|
|
|
|
/*UseOdrIndicator*/ false));
|
2011-11-17 01:34:26 +08:00
|
|
|
}
|
|
|
|
|
2017-12-09 09:32:07 +08:00
|
|
|
static void addHWAddressSanitizerPasses(const PassManagerBuilder &Builder,
|
|
|
|
legacy::PassManagerBase &PM) {
|
2017-12-21 03:05:44 +08:00
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper &>(Builder);
|
|
|
|
const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
|
|
|
|
bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
|
2019-05-15 05:17:21 +08:00
|
|
|
PM.add(
|
|
|
|
createHWAddressSanitizerLegacyPassPass(/*CompileKernel*/ false, Recover));
|
2018-04-14 02:05:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void addKernelHWAddressSanitizerPasses(const PassManagerBuilder &Builder,
|
|
|
|
legacy::PassManagerBase &PM) {
|
2019-05-15 05:17:21 +08:00
|
|
|
PM.add(createHWAddressSanitizerLegacyPassPass(
|
2018-04-14 02:05:21 +08:00
|
|
|
/*CompileKernel*/ true, /*Recover*/ true));
|
2017-12-09 09:32:07 +08:00
|
|
|
}
|
|
|
|
|
2018-09-07 17:21:09 +08:00
|
|
|
static void addGeneralOptsForMemorySanitizer(const PassManagerBuilder &Builder,
|
|
|
|
legacy::PassManagerBase &PM,
|
|
|
|
bool CompileKernel) {
|
2012-12-24 16:42:34 +08:00
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper&>(Builder);
|
|
|
|
const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
|
2016-11-08 05:02:11 +08:00
|
|
|
int TrackOrigins = CGOpts.SanitizeMemoryTrackOrigins;
|
|
|
|
bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Memory);
|
2019-02-05 05:02:49 +08:00
|
|
|
PM.add(createMemorySanitizerLegacyPassPass(
|
|
|
|
MemorySanitizerOptions{TrackOrigins, Recover, CompileKernel}));
|
2013-01-31 17:53:29 +08:00
|
|
|
|
|
|
|
// MemorySanitizer inserts complex instrumentation that mostly follows
|
|
|
|
// the logic of the original code, but operates on "shadow" values.
|
|
|
|
// It can benefit from re-running some general purpose optimization passes.
|
|
|
|
if (Builder.OptLevel > 0) {
|
|
|
|
PM.add(createEarlyCSEPass());
|
|
|
|
PM.add(createReassociatePass());
|
|
|
|
PM.add(createLICMPass());
|
|
|
|
PM.add(createGVNPass());
|
|
|
|
PM.add(createInstructionCombiningPass());
|
|
|
|
PM.add(createDeadStoreEliminationPass());
|
|
|
|
}
|
2012-12-03 21:20:43 +08:00
|
|
|
}
|
|
|
|
|
2018-09-07 17:21:09 +08:00
|
|
|
static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
|
|
|
|
legacy::PassManagerBase &PM) {
|
|
|
|
addGeneralOptsForMemorySanitizer(Builder, PM, /*CompileKernel*/ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addKernelMemorySanitizerPass(const PassManagerBuilder &Builder,
|
|
|
|
legacy::PassManagerBase &PM) {
|
|
|
|
addGeneralOptsForMemorySanitizer(Builder, PM, /*CompileKernel*/ true);
|
|
|
|
}
|
|
|
|
|
2012-03-02 06:27:08 +08:00
|
|
|
static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManagerBase &PM) {
|
2019-01-16 17:28:01 +08:00
|
|
|
PM.add(createThreadSanitizerLegacyPassPass());
|
2012-03-02 06:27:08 +08:00
|
|
|
}
|
|
|
|
|
2013-08-08 06:47:34 +08:00
|
|
|
static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManagerBase &PM) {
|
2013-08-15 02:54:18 +08:00
|
|
|
const PassManagerBuilderWrapper &BuilderWrapper =
|
|
|
|
static_cast<const PassManagerBuilderWrapper&>(Builder);
|
2014-10-16 04:22:54 +08:00
|
|
|
const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
|
2020-07-28 05:19:12 +08:00
|
|
|
PM.add(
|
|
|
|
createDataFlowSanitizerLegacyPassPass(LangOpts.SanitizerBlacklistFiles));
|
2013-08-08 06:47:34 +08:00
|
|
|
}
|
|
|
|
|
2020-04-11 09:00:58 +08:00
|
|
|
static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
|
|
|
|
const CodeGenOptions &CodeGenOpts) {
|
|
|
|
TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
|
|
|
|
|
|
|
|
switch (CodeGenOpts.getVecLib()) {
|
|
|
|
case CodeGenOptions::Accelerate:
|
|
|
|
TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
|
|
|
|
break;
|
2020-10-23 04:00:34 +08:00
|
|
|
case CodeGenOptions::LIBMVEC:
|
|
|
|
switch(TargetTriple.getArch()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case llvm::Triple::x86_64:
|
|
|
|
TLII->addVectorizableFunctionsFromVecLib
|
|
|
|
(TargetLibraryInfoImpl::LIBMVEC_X86);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2020-04-11 09:00:58 +08:00
|
|
|
case CodeGenOptions::MASSV:
|
|
|
|
TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::MASSV);
|
|
|
|
break;
|
|
|
|
case CodeGenOptions::SVML:
|
|
|
|
TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return TLII;
|
2014-08-22 01:58:42 +08:00
|
|
|
}
|
|
|
|
|
2015-01-09 13:10:20 +08:00
|
|
|
static void addSymbolRewriterPass(const CodeGenOptions &Opts,
|
2015-02-13 17:47:49 +08:00
|
|
|
legacy::PassManager *MPM) {
|
2015-01-09 13:10:20 +08:00
|
|
|
llvm::SymbolRewriter::RewriteDescriptorList DL;
|
|
|
|
|
|
|
|
llvm::SymbolRewriter::RewriteMapParser MapParser;
|
|
|
|
for (const auto &MapFile : Opts.RewriteMapFiles)
|
|
|
|
MapParser.parse(MapFile, &DL);
|
|
|
|
|
|
|
|
MPM->add(createRewriteSymbolsPass(DL));
|
|
|
|
}
|
|
|
|
|
2017-03-31 10:05:15 +08:00
|
|
|
static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) {
|
|
|
|
switch (CodeGenOpts.OptimizationLevel) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid optimization level!");
|
|
|
|
case 0:
|
|
|
|
return CodeGenOpt::None;
|
|
|
|
case 1:
|
|
|
|
return CodeGenOpt::Less;
|
|
|
|
case 2:
|
|
|
|
return CodeGenOpt::Default; // O2/Os/Oz
|
|
|
|
case 3:
|
|
|
|
return CodeGenOpt::Aggressive;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 10:16:28 +08:00
|
|
|
static Optional<llvm::CodeModel::Model>
|
|
|
|
getCodeModel(const CodeGenOptions &CodeGenOpts) {
|
|
|
|
unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
|
2018-08-22 19:34:28 +08:00
|
|
|
.Case("tiny", llvm::CodeModel::Tiny)
|
2017-08-03 10:16:28 +08:00
|
|
|
.Case("small", llvm::CodeModel::Small)
|
|
|
|
.Case("kernel", llvm::CodeModel::Kernel)
|
|
|
|
.Case("medium", llvm::CodeModel::Medium)
|
|
|
|
.Case("large", llvm::CodeModel::Large)
|
|
|
|
.Case("default", ~1u)
|
|
|
|
.Default(~0u);
|
2017-03-31 10:05:15 +08:00
|
|
|
assert(CodeModel != ~0u && "invalid code model!");
|
2017-08-03 10:16:28 +08:00
|
|
|
if (CodeModel == ~1u)
|
|
|
|
return None;
|
2017-03-31 10:05:15 +08:00
|
|
|
return static_cast<llvm::CodeModel::Model>(CodeModel);
|
|
|
|
}
|
|
|
|
|
2019-11-14 07:17:46 +08:00
|
|
|
static CodeGenFileType getCodeGenFileType(BackendAction Action) {
|
2017-03-31 10:05:15 +08:00
|
|
|
if (Action == Backend_EmitObj)
|
2019-11-14 07:17:46 +08:00
|
|
|
return CGFT_ObjectFile;
|
2017-03-31 10:05:15 +08:00
|
|
|
else if (Action == Backend_EmitMCNull)
|
2019-11-14 07:17:46 +08:00
|
|
|
return CGFT_Null;
|
2017-03-31 10:05:15 +08:00
|
|
|
else {
|
|
|
|
assert(Action == Backend_EmitAssembly && "Invalid action!");
|
2019-11-14 07:17:46 +08:00
|
|
|
return CGFT_AssemblyFile;
|
2017-03-31 10:05:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 01:26:37 +08:00
|
|
|
static bool initTargetOptions(DiagnosticsEngine &Diags,
|
2020-06-02 14:17:29 +08:00
|
|
|
llvm::TargetOptions &Options,
|
2017-03-31 10:05:15 +08:00
|
|
|
const CodeGenOptions &CodeGenOpts,
|
|
|
|
const clang::TargetOptions &TargetOpts,
|
|
|
|
const LangOptions &LangOpts,
|
|
|
|
const HeaderSearchOptions &HSOpts) {
|
2020-11-19 07:26:21 +08:00
|
|
|
switch (LangOpts.getThreadModel()) {
|
|
|
|
case LangOptions::ThreadModelKind::POSIX:
|
|
|
|
Options.ThreadModel = llvm::ThreadModel::POSIX;
|
|
|
|
break;
|
|
|
|
case LangOptions::ThreadModelKind::Single:
|
|
|
|
Options.ThreadModel = llvm::ThreadModel::Single;
|
|
|
|
break;
|
|
|
|
}
|
2017-03-31 10:05:15 +08:00
|
|
|
|
|
|
|
// Set float ABI type.
|
|
|
|
assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
|
|
|
|
CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
|
|
|
|
"Invalid Floating Point ABI!");
|
|
|
|
Options.FloatABIType =
|
|
|
|
llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
|
|
|
|
.Case("soft", llvm::FloatABI::Soft)
|
|
|
|
.Case("softfp", llvm::FloatABI::Soft)
|
|
|
|
.Case("hard", llvm::FloatABI::Hard)
|
|
|
|
.Default(llvm::FloatABI::Default);
|
|
|
|
|
|
|
|
// Set FP fusion mode.
|
|
|
|
switch (LangOpts.getDefaultFPContractMode()) {
|
2020-05-05 01:48:12 +08:00
|
|
|
case LangOptions::FPM_Off:
|
2017-04-21 01:09:35 +08:00
|
|
|
// Preserve any contraction performed by the front-end. (Strict performs
|
Misc typos fixes in ./lib folder
Summary: Found via `codespell -q 3 -I ../clang-whitelist.txt -L uint,importd,crasher,gonna,cant,ue,ons,orign,ned`
Reviewers: teemperor
Reviewed By: teemperor
Subscribers: teemperor, jholewinski, jvesely, nhaehnle, whisperity, jfb, cfe-commits
Differential Revision: https://reviews.llvm.org/D55475
llvm-svn: 348755
2018-12-10 20:37:46 +08:00
|
|
|
// splitting of the muladd intrinsic in the backend.)
|
2017-04-21 01:09:35 +08:00
|
|
|
Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
|
2017-03-31 10:05:15 +08:00
|
|
|
break;
|
2020-05-05 01:48:12 +08:00
|
|
|
case LangOptions::FPM_On:
|
[HIP] Fix regressions due to fp contract change
Recently HIP toolchain made a change to use clang instead of opt/llc to do compilation
(https://reviews.llvm.org/D81861). The intention is to make HIP toolchain canonical like
other toolchains.
However, this change introduced an unintentional change regarding backend fp fuse
option, which caused regressions in some HIP applications.
Basically before the change, HIP toolchain used clang to generate bitcode, then use
opt/llc to optimize bitcode and generate ISA. As such, the amdgpu backend takes
the default fp fuse mode which is 'Standard'. This mode respect contract flag of
fmul/fadd instructions and do not fuse fmul/fadd instructions without contract flag.
However, after the change, HIP toolchain now use clang to generate IR, do optimization,
and generate ISA as one process. Now amdgpu backend fp fuse option is determined
by -ffp-contract option, which is 'fast' by default. And this -ffp-contract=fast language option
is translated to 'Fast' fp fuse option in backend. Suddenly backend starts to fuse fmul/fadd
instructions without contract flag.
This causes wrong result for some device library functions, e.g. tan(-1e20), which should
return 0.8446, now returns -0.933. What is worse is that since backend with 'Fast' fp fuse
option does not respect contract flag, there is no way to use #pragma clang fp contract
directive to enforce fp contract requirements.
This patch fixes the regression by introducing a new value 'fast-honor-pragmas' for -ffp-contract
and use it for HIP by default. 'fast-honor-pragmas' is equivalent to 'fast' in frontend but
let the backend to use 'Standard' fp fuse option. 'fast-honor-pragmas' is useful since 'Fast'
fp fuse option in backend does not honor contract flag, it is of little use to HIP
applications since all code with #pragma STDC FP_CONTRACT or any IR from a
source compiled with -ffp-contract=on is broken.
Differential Revision: https://reviews.llvm.org/D90174
2020-10-24 04:24:48 +08:00
|
|
|
case LangOptions::FPM_FastHonorPragmas:
|
2017-03-31 10:05:15 +08:00
|
|
|
Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
|
|
|
|
break;
|
2020-05-05 01:48:12 +08:00
|
|
|
case LangOptions::FPM_Fast:
|
2017-03-31 10:05:15 +08:00
|
|
|
Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Options.UseInitArray = CodeGenOpts.UseInitArray;
|
|
|
|
Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
|
2017-06-09 08:40:30 +08:00
|
|
|
Options.CompressDebugSections = CodeGenOpts.getCompressDebugSections();
|
2017-03-31 10:05:15 +08:00
|
|
|
Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
|
|
|
|
|
|
|
|
// Set EABI version.
|
2017-07-01 15:57:23 +08:00
|
|
|
Options.EABIVersion = TargetOpts.EABIVersion;
|
2017-03-31 10:05:15 +08:00
|
|
|
|
|
|
|
if (LangOpts.SjLjExceptions)
|
|
|
|
Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
|
2017-11-29 15:25:12 +08:00
|
|
|
if (LangOpts.SEHExceptions)
|
|
|
|
Options.ExceptionModel = llvm::ExceptionHandling::WinEH;
|
|
|
|
if (LangOpts.DWARFExceptions)
|
|
|
|
Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI;
|
2019-09-12 12:01:37 +08:00
|
|
|
if (LangOpts.WasmExceptions)
|
|
|
|
Options.ExceptionModel = llvm::ExceptionHandling::Wasm;
|
2017-03-31 10:05:15 +08:00
|
|
|
|
2020-06-02 09:02:02 +08:00
|
|
|
Options.NoInfsFPMath = LangOpts.NoHonorInfs;
|
|
|
|
Options.NoNaNsFPMath = LangOpts.NoHonorNaNs;
|
2017-03-31 10:05:15 +08:00
|
|
|
Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
|
2020-06-02 09:02:02 +08:00
|
|
|
Options.UnsafeFPMath = LangOpts.UnsafeFPMath;
|
2017-03-31 10:05:15 +08:00
|
|
|
Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
|
2020-06-02 14:17:29 +08:00
|
|
|
|
|
|
|
Options.BBSections =
|
|
|
|
llvm::StringSwitch<llvm::BasicBlockSection>(CodeGenOpts.BBSections)
|
|
|
|
.Case("all", llvm::BasicBlockSection::All)
|
|
|
|
.Case("labels", llvm::BasicBlockSection::Labels)
|
|
|
|
.StartsWith("list=", llvm::BasicBlockSection::List)
|
|
|
|
.Case("none", llvm::BasicBlockSection::None)
|
|
|
|
.Default(llvm::BasicBlockSection::None);
|
|
|
|
|
|
|
|
if (Options.BBSections == llvm::BasicBlockSection::List) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
|
|
|
|
MemoryBuffer::getFile(CodeGenOpts.BBSections.substr(5));
|
2020-11-10 01:26:37 +08:00
|
|
|
if (!MBOrErr) {
|
2020-06-02 14:17:29 +08:00
|
|
|
Diags.Report(diag::err_fe_unable_to_load_basic_block_sections_file)
|
|
|
|
<< MBOrErr.getError().message();
|
2020-11-10 01:26:37 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
|
2020-06-02 14:17:29 +08:00
|
|
|
}
|
|
|
|
|
2020-09-03 02:00:46 +08:00
|
|
|
Options.EnableMachineFunctionSplitter = CodeGenOpts.SplitMachineFunctions;
|
2017-03-31 10:05:15 +08:00
|
|
|
Options.FunctionSections = CodeGenOpts.FunctionSections;
|
|
|
|
Options.DataSections = CodeGenOpts.DataSections;
|
2020-10-08 21:34:58 +08:00
|
|
|
Options.IgnoreXCOFFVisibility = CodeGenOpts.IgnoreXCOFFVisibility;
|
2017-03-31 10:05:15 +08:00
|
|
|
Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
|
2020-06-02 14:17:29 +08:00
|
|
|
Options.UniqueBasicBlockSectionNames =
|
|
|
|
CodeGenOpts.UniqueBasicBlockSectionNames;
|
2020-10-22 09:46:42 +08:00
|
|
|
Options.StackProtectorGuard =
|
|
|
|
llvm::StringSwitch<llvm::StackProtectorGuards>(CodeGenOpts
|
|
|
|
.StackProtectorGuard)
|
|
|
|
.Case("tls", llvm::StackProtectorGuards::TLS)
|
|
|
|
.Case("global", llvm::StackProtectorGuards::Global)
|
|
|
|
.Default(llvm::StackProtectorGuards::None);
|
|
|
|
Options.StackProtectorGuardOffset = CodeGenOpts.StackProtectorGuardOffset;
|
|
|
|
Options.StackProtectorGuardReg = CodeGenOpts.StackProtectorGuardReg;
|
2020-01-13 17:28:02 +08:00
|
|
|
Options.TLSSize = CodeGenOpts.TLSSize;
|
2017-03-31 10:05:15 +08:00
|
|
|
Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
|
2018-03-02 06:26:19 +08:00
|
|
|
Options.ExplicitEmulatedTLS = CodeGenOpts.ExplicitEmulatedTLS;
|
2017-03-31 10:05:15 +08:00
|
|
|
Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
|
2018-01-08 21:42:26 +08:00
|
|
|
Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;
|
2018-07-18 08:27:07 +08:00
|
|
|
Options.EmitAddrsig = CodeGenOpts.Addrsig;
|
2019-10-31 16:55:57 +08:00
|
|
|
Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection;
|
2020-03-09 18:02:35 +08:00
|
|
|
Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo;
|
2020-11-25 07:11:46 +08:00
|
|
|
Options.EnableAIXExtendedAltivecABI = CodeGenOpts.EnableAIXExtendedAltivecABI;
|
2020-08-25 21:23:14 +08:00
|
|
|
Options.ValueTrackingVariableLocations =
|
|
|
|
CodeGenOpts.ValueTrackingVariableLocations;
|
2020-06-17 08:36:11 +08:00
|
|
|
Options.XRayOmitFunctionIndex = CodeGenOpts.XRayOmitFunctionIndex;
|
2017-03-31 10:05:15 +08:00
|
|
|
|
2019-06-27 05:36:35 +08:00
|
|
|
Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
|
2017-03-31 10:05:15 +08:00
|
|
|
Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
|
|
|
|
Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
|
|
|
|
Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
|
|
|
|
Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
|
|
|
|
Options.MCOptions.MCIncrementalLinkerCompatible =
|
|
|
|
CodeGenOpts.IncrementalLinkerCompatible;
|
|
|
|
Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
|
2019-08-09 03:19:20 +08:00
|
|
|
Options.MCOptions.MCNoWarn = CodeGenOpts.NoWarn;
|
2017-03-31 10:05:15 +08:00
|
|
|
Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
|
|
|
|
Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
|
|
|
|
Options.MCOptions.ABIName = TargetOpts.ABI;
|
|
|
|
for (const auto &Entry : HSOpts.UserEntries)
|
|
|
|
if (!Entry.IsFramework &&
|
|
|
|
(Entry.Group == frontend::IncludeDirGroup::Quoted ||
|
|
|
|
Entry.Group == frontend::IncludeDirGroup::Angled ||
|
|
|
|
Entry.Group == frontend::IncludeDirGroup::System))
|
|
|
|
Options.MCOptions.IASSearchPaths.push_back(
|
|
|
|
Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
|
2020-06-18 20:57:50 +08:00
|
|
|
Options.MCOptions.Argv0 = CodeGenOpts.Argv0;
|
|
|
|
Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
|
2020-11-10 01:26:37 +08:00
|
|
|
|
|
|
|
return true;
|
2017-03-31 10:05:15 +08:00
|
|
|
}
|
2020-08-29 07:32:35 +08:00
|
|
|
|
|
|
|
static Optional<GCOVOptions> getGCOVOptions(const CodeGenOptions &CodeGenOpts,
|
|
|
|
const LangOptions &LangOpts) {
|
2018-01-10 06:03:47 +08:00
|
|
|
if (!CodeGenOpts.EmitGcovArcs && !CodeGenOpts.EmitGcovNotes)
|
|
|
|
return None;
|
|
|
|
// Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
|
|
|
|
// LLVM's -default-gcov-version flag is set to something invalid.
|
|
|
|
GCOVOptions Options;
|
|
|
|
Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
|
|
|
|
Options.EmitData = CodeGenOpts.EmitGcovArcs;
|
|
|
|
llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version));
|
|
|
|
Options.NoRedZone = CodeGenOpts.DisableRedZone;
|
2018-11-18 03:41:39 +08:00
|
|
|
Options.Filter = CodeGenOpts.ProfileFilterFiles;
|
|
|
|
Options.Exclude = CodeGenOpts.ProfileExcludeFiles;
|
2020-09-30 01:38:51 +08:00
|
|
|
Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
|
2018-01-10 06:03:47 +08:00
|
|
|
return Options;
|
|
|
|
}
|
2017-03-31 10:05:15 +08:00
|
|
|
|
2019-04-26 01:52:43 +08:00
|
|
|
static Optional<InstrProfOptions>
|
|
|
|
getInstrProfOptions(const CodeGenOptions &CodeGenOpts,
|
|
|
|
const LangOptions &LangOpts) {
|
|
|
|
if (!CodeGenOpts.hasProfileClangInstr())
|
|
|
|
return None;
|
|
|
|
InstrProfOptions Options;
|
|
|
|
Options.NoRedZone = CodeGenOpts.DisableRedZone;
|
|
|
|
Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
|
2020-09-30 01:38:51 +08:00
|
|
|
Options.Atomic = CodeGenOpts.AtomicProfileUpdate;
|
2019-04-26 01:52:43 +08:00
|
|
|
return Options;
|
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,
|
2016-08-13 02:12:08 +08:00
|
|
|
legacy::FunctionPassManager &FPM) {
|
2016-12-23 08:23:01 +08:00
|
|
|
// Handle disabling of all LLVM passes, where we want to preserve the
|
|
|
|
// internal module before any optimization.
|
2015-07-18 04:09:56 +08:00
|
|
|
if (CodeGenOpts.DisableLLVMPasses)
|
|
|
|
return;
|
|
|
|
|
2016-11-23 04:03:35 +08:00
|
|
|
// Figure out TargetLibraryInfo. This needs to be added to MPM and FPM
|
|
|
|
// manually (and not via PMBuilder), since some passes (eg. InstrProfiling)
|
|
|
|
// are inserted before PMBuilder ones - they'd get the default-constructed
|
|
|
|
// TLI with an unknown target otherwise.
|
2015-12-08 03:21:34 +08:00
|
|
|
Triple TargetTriple(TheModule->getTargetTriple());
|
2020-04-11 09:00:58 +08:00
|
|
|
std::unique_ptr<TargetLibraryInfoImpl> TLII(
|
|
|
|
createTLII(TargetTriple, CodeGenOpts));
|
2015-12-08 03:21:34 +08:00
|
|
|
|
2020-01-25 04:24:18 +08:00
|
|
|
// If we reached here with a non-empty index file name, then the index file
|
|
|
|
// was empty and we are not performing ThinLTO backend compilation (used in
|
|
|
|
// testing in a distributed build environment). Drop any the type test
|
|
|
|
// assume sequences inserted for whole program vtables so that codegen doesn't
|
|
|
|
// complain.
|
|
|
|
if (!CodeGenOpts.ThinLTOIndexFile.empty())
|
|
|
|
MPM.add(createLowerTypeTestsPass(/*ExportSummary=*/nullptr,
|
|
|
|
/*ImportSummary=*/nullptr,
|
|
|
|
/*DropTypeTests=*/true));
|
|
|
|
|
2017-04-25 03:34:12 +08:00
|
|
|
PassManagerBuilderWrapper PMBuilder(TargetTriple, CodeGenOpts, LangOpts);
|
|
|
|
|
Cleanup the handling of noinline function attributes, -fno-inline,
-fno-inline-functions, -O0, and optnone.
These were really, really tangled together:
- We used the noinline LLVM attribute for -fno-inline
- But not for -fno-inline-functions (breaking LTO)
- But we did use it for -finline-hint-functions (yay, LTO is happy!)
- But we didn't for -O0 (LTO is sad yet again...)
- We had weird structuring of CodeGenOpts with both an inlining
enumeration and a boolean. They interacted in weird ways and
needlessly.
- A *lot* of set smashing went on with setting these, and then got worse
when we considered optnone and other inlining-effecting attributes.
- A bunch of inline affecting attributes were managed in a completely
different place from -fno-inline.
- Even with -fno-inline we failed to put the LLVM noinline attribute
onto many generated function definitions because they didn't show up
as AST-level functions.
- If you passed -O0 but -finline-functions we would run the normal
inliner pass in LLVM despite it being in the O0 pipeline, which really
doesn't make much sense.
- Lastly, we used things like '-fno-inline' to manipulate the pass
pipeline which forced the pass pipeline to be much more
parameterizable than it really needs to be. Instead we can *just* use
the optimization level to select a pipeline and control the rest via
attributes.
Sadly, this causes a bunch of churn in tests because we don't run the
optimizer in the tests and check the contents of attribute sets. It
would be awesome if attribute sets were a bit more FileCheck friendly,
but oh well.
I think this is a significant improvement and should remove the semantic
need to change what inliner pass we run in order to comply with the
requested inlining semantics by relying completely on attributes. It
also cleans up tho optnone and related handling a bit.
One unfortunate aspect of this is that for generating alwaysinline
routines like those in OpenMP we end up removing noinline and then
adding alwaysinline. I tried a bunch of other approaches, but because we
recompute function attributes from scratch and don't have a declaration
here I couldn't find anything substantially cleaner than this.
Differential Revision: https://reviews.llvm.org/D28053
llvm-svn: 290398
2016-12-23 09:24:49 +08:00
|
|
|
// At O0 and O1 we only run the always inliner which is more efficient. At
|
|
|
|
// higher optimization levels we run the normal inliner.
|
|
|
|
if (CodeGenOpts.OptimizationLevel <= 1) {
|
2020-03-13 15:27:45 +08:00
|
|
|
bool InsertLifetimeIntrinsics = ((CodeGenOpts.OptimizationLevel != 0 &&
|
|
|
|
!CodeGenOpts.DisableLifetimeMarkers) ||
|
|
|
|
LangOpts.Coroutines);
|
Cleanup the handling of noinline function attributes, -fno-inline,
-fno-inline-functions, -O0, and optnone.
These were really, really tangled together:
- We used the noinline LLVM attribute for -fno-inline
- But not for -fno-inline-functions (breaking LTO)
- But we did use it for -finline-hint-functions (yay, LTO is happy!)
- But we didn't for -O0 (LTO is sad yet again...)
- We had weird structuring of CodeGenOpts with both an inlining
enumeration and a boolean. They interacted in weird ways and
needlessly.
- A *lot* of set smashing went on with setting these, and then got worse
when we considered optnone and other inlining-effecting attributes.
- A bunch of inline affecting attributes were managed in a completely
different place from -fno-inline.
- Even with -fno-inline we failed to put the LLVM noinline attribute
onto many generated function definitions because they didn't show up
as AST-level functions.
- If you passed -O0 but -finline-functions we would run the normal
inliner pass in LLVM despite it being in the O0 pipeline, which really
doesn't make much sense.
- Lastly, we used things like '-fno-inline' to manipulate the pass
pipeline which forced the pass pipeline to be much more
parameterizable than it really needs to be. Instead we can *just* use
the optimization level to select a pipeline and control the rest via
attributes.
Sadly, this causes a bunch of churn in tests because we don't run the
optimizer in the tests and check the contents of attribute sets. It
would be awesome if attribute sets were a bit more FileCheck friendly,
but oh well.
I think this is a significant improvement and should remove the semantic
need to change what inliner pass we run in order to comply with the
requested inlining semantics by relying completely on attributes. It
also cleans up tho optnone and related handling a bit.
One unfortunate aspect of this is that for generating alwaysinline
routines like those in OpenMP we end up removing noinline and then
adding alwaysinline. I tried a bunch of other approaches, but because we
recompute function attributes from scratch and don't have a declaration
here I couldn't find anything substantially cleaner than this.
Differential Revision: https://reviews.llvm.org/D28053
llvm-svn: 290398
2016-12-23 09:24:49 +08:00
|
|
|
PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics);
|
|
|
|
} else {
|
2017-03-22 03:55:46 +08:00
|
|
|
// We do not want to inline hot callsites for SamplePGO module-summary build
|
|
|
|
// because profile annotation will happen again in ThinLTO backend, and we
|
|
|
|
// want the IR of the hot path to match the profile.
|
Cleanup the handling of noinline function attributes, -fno-inline,
-fno-inline-functions, -O0, and optnone.
These were really, really tangled together:
- We used the noinline LLVM attribute for -fno-inline
- But not for -fno-inline-functions (breaking LTO)
- But we did use it for -finline-hint-functions (yay, LTO is happy!)
- But we didn't for -O0 (LTO is sad yet again...)
- We had weird structuring of CodeGenOpts with both an inlining
enumeration and a boolean. They interacted in weird ways and
needlessly.
- A *lot* of set smashing went on with setting these, and then got worse
when we considered optnone and other inlining-effecting attributes.
- A bunch of inline affecting attributes were managed in a completely
different place from -fno-inline.
- Even with -fno-inline we failed to put the LLVM noinline attribute
onto many generated function definitions because they didn't show up
as AST-level functions.
- If you passed -O0 but -finline-functions we would run the normal
inliner pass in LLVM despite it being in the O0 pipeline, which really
doesn't make much sense.
- Lastly, we used things like '-fno-inline' to manipulate the pass
pipeline which forced the pass pipeline to be much more
parameterizable than it really needs to be. Instead we can *just* use
the optimization level to select a pipeline and control the rest via
attributes.
Sadly, this causes a bunch of churn in tests because we don't run the
optimizer in the tests and check the contents of attribute sets. It
would be awesome if attribute sets were a bit more FileCheck friendly,
but oh well.
I think this is a significant improvement and should remove the semantic
need to change what inliner pass we run in order to comply with the
requested inlining semantics by relying completely on attributes. It
also cleans up tho optnone and related handling a bit.
One unfortunate aspect of this is that for generating alwaysinline
routines like those in OpenMP we end up removing noinline and then
adding alwaysinline. I tried a bunch of other approaches, but because we
recompute function attributes from scratch and don't have a declaration
here I couldn't find anything substantially cleaner than this.
Differential Revision: https://reviews.llvm.org/D28053
llvm-svn: 290398
2016-12-23 09:24:49 +08:00
|
|
|
PMBuilder.Inliner = createFunctionInliningPass(
|
2017-03-22 03:55:46 +08:00
|
|
|
CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
|
|
|
|
(!CodeGenOpts.SampleProfileFile.empty() &&
|
2018-06-23 04:23:21 +08:00
|
|
|
CodeGenOpts.PrepareForThinLTO));
|
2015-12-08 03:21:34 +08:00
|
|
|
}
|
|
|
|
|
Cleanup the handling of noinline function attributes, -fno-inline,
-fno-inline-functions, -O0, and optnone.
These were really, really tangled together:
- We used the noinline LLVM attribute for -fno-inline
- But not for -fno-inline-functions (breaking LTO)
- But we did use it for -finline-hint-functions (yay, LTO is happy!)
- But we didn't for -O0 (LTO is sad yet again...)
- We had weird structuring of CodeGenOpts with both an inlining
enumeration and a boolean. They interacted in weird ways and
needlessly.
- A *lot* of set smashing went on with setting these, and then got worse
when we considered optnone and other inlining-effecting attributes.
- A bunch of inline affecting attributes were managed in a completely
different place from -fno-inline.
- Even with -fno-inline we failed to put the LLVM noinline attribute
onto many generated function definitions because they didn't show up
as AST-level functions.
- If you passed -O0 but -finline-functions we would run the normal
inliner pass in LLVM despite it being in the O0 pipeline, which really
doesn't make much sense.
- Lastly, we used things like '-fno-inline' to manipulate the pass
pipeline which forced the pass pipeline to be much more
parameterizable than it really needs to be. Instead we can *just* use
the optimization level to select a pipeline and control the rest via
attributes.
Sadly, this causes a bunch of churn in tests because we don't run the
optimizer in the tests and check the contents of attribute sets. It
would be awesome if attribute sets were a bit more FileCheck friendly,
but oh well.
I think this is a significant improvement and should remove the semantic
need to change what inliner pass we run in order to comply with the
requested inlining semantics by relying completely on attributes. It
also cleans up tho optnone and related handling a bit.
One unfortunate aspect of this is that for generating alwaysinline
routines like those in OpenMP we end up removing noinline and then
adding alwaysinline. I tried a bunch of other approaches, but because we
recompute function attributes from scratch and don't have a declaration
here I couldn't find anything substantially cleaner than this.
Differential Revision: https://reviews.llvm.org/D28053
llvm-svn: 290398
2016-12-23 09:24:49 +08:00
|
|
|
PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
|
2011-05-22 07:50:44 +08:00
|
|
|
PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
|
2013-06-25 09:49:44 +08:00
|
|
|
PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
|
|
|
|
PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
|
[LPM] Port CGProfilePass from NPM to LPM
Reviewers: hans, chandlerc!, asbirlea, nikic
Reviewed By: hans, nikic
Subscribers: steven_wu, dexonsmith, nikic, echristo, void, zhizhouy, cfe-commits, aeubanks, MaskRay, jvesely, nhaehnle, hiraditya, kerbowa, llvm-commits
Tags: #llvm, #clang
Differential Revision: https://reviews.llvm.org/D83013
2020-07-09 03:30:28 +08:00
|
|
|
// Only enable CGProfilePass when using integrated assembler, since
|
|
|
|
// non-integrated assemblers don't recognize .cgprofile section.
|
|
|
|
PMBuilder.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS;
|
2011-05-22 04:40:11 +08:00
|
|
|
|
2011-05-22 07:50:44 +08:00
|
|
|
PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
|
2019-05-01 05:29:23 +08:00
|
|
|
// Loop interleaving in the loop vectorizer has historically been set to be
|
|
|
|
// enabled when loop unrolling is enabled.
|
|
|
|
PMBuilder.LoopsInterleaved = CodeGenOpts.UnrollLoops;
|
2014-10-24 08:49:29 +08:00
|
|
|
PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
|
2018-06-23 04:23:21 +08:00
|
|
|
PMBuilder.PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
|
2015-07-07 00:23:00 +08:00
|
|
|
PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
|
2013-11-18 00:03:29 +08:00
|
|
|
PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
|
2011-07-06 06:02:36 +08:00
|
|
|
|
2016-11-23 04:03:35 +08:00
|
|
|
MPM.add(new TargetLibraryInfoWrapperPass(*TLII));
|
|
|
|
|
2016-04-28 03:12:56 +08:00
|
|
|
if (TM)
|
2017-01-27 00:49:21 +08:00
|
|
|
TM->adjustPassManager(PMBuilder);
|
2016-04-28 03:12:56 +08:00
|
|
|
|
2017-02-22 04:36:21 +08:00
|
|
|
if (CodeGenOpts.DebugInfoForProfiling ||
|
|
|
|
!CodeGenOpts.SampleProfileFile.empty())
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
|
|
|
|
addAddDiscriminatorsPass);
|
2014-03-04 04:06:18 +08:00
|
|
|
|
2011-07-06 06:02:36 +08:00
|
|
|
// In ObjC ARC mode, add the main ARC optimization passes.
|
|
|
|
if (LangOpts.ObjCAutoRefCount) {
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
|
|
|
|
addObjCARCExpandPass);
|
2012-01-18 04:54:51 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
|
|
|
|
addObjCARCAPElimPass);
|
2011-07-06 06:02:36 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
|
|
|
|
addObjCARCOptPass);
|
|
|
|
}
|
2011-11-17 01:34:26 +08:00
|
|
|
|
2019-02-24 05:06:26 +08:00
|
|
|
if (LangOpts.Coroutines)
|
[Coroutines] Schedule coro-split before asan
Summary:
The docs for the LLVM coroutines intrinsic `@llvm.coro.id` state that
"The second argument, if not null, designates a particular alloca instruction
to be a coroutine promise."
However, if the address sanitizer pass is run before the `@llvm.coro.id`
intrinsic is lowered, the `alloca` instruction passed to the intrinsic as its
second argument is converted, as per the
https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm docs, to
an `inttoptr` instruction that accesses the address of the promise.
On optimization levels `-O1` and above, the `-asan` pass is run after
`-coro-early`, `-coro-split`, and `-coro-elide`, and before
`-coro-cleanup`, and so there is no issue. At `-O0`, however, `-asan`
is run in between `-coro-early` and `-coro-split`, which causes an
assertion to be hit when the `inttoptr` instruction is forcibly cast to
an `alloca`.
Rearrange the passes such that the coroutine passes are registered
before the sanitizer passes.
Test Plan:
Compile a simple C++ program that uses coroutines in `-O0` with
`-fsanitize-address`, and confirm no assertion is hit:
`clang++ coro-example.cpp -fcoroutines-ts -g -fsanitize=address -fno-omit-frame-pointer`.
Reviewers: GorNishanov, lewissbaker, EricWF
Reviewed By: GorNishanov
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D43927
llvm-svn: 328951
2018-04-02 07:55:21 +08:00
|
|
|
addCoroutinePassesToExtensionPoints(PMBuilder);
|
|
|
|
|
2020-09-30 06:53:41 +08:00
|
|
|
if (!CodeGenOpts.MemoryProfileOutput.empty()) {
|
2020-08-14 07:29:38 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
2020-09-15 00:12:13 +08:00
|
|
|
addMemProfilerPasses);
|
2020-08-14 07:29:38 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
2020-09-15 00:12:13 +08:00
|
|
|
addMemProfilerPasses);
|
2020-08-14 07:29:38 +08:00
|
|
|
}
|
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
|
2012-05-23 01:19:45 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
|
|
|
|
addBoundsCheckingPass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addBoundsCheckingPass);
|
|
|
|
}
|
|
|
|
|
2015-05-08 06:34:06 +08:00
|
|
|
if (CodeGenOpts.SanitizeCoverageType ||
|
|
|
|
CodeGenOpts.SanitizeCoverageIndirectCalls ||
|
|
|
|
CodeGenOpts.SanitizeCoverageTraceCmp) {
|
2014-11-12 06:15:07 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
|
|
|
addSanitizerCoveragePass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addSanitizerCoveragePass);
|
|
|
|
}
|
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
|
2012-10-15 22:22:56 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
2012-11-30 06:36:21 +08:00
|
|
|
addAddressSanitizerPasses);
|
2011-12-01 06:20:21 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
2012-11-30 06:36:21 +08:00
|
|
|
addAddressSanitizerPasses);
|
2011-11-17 01:34:26 +08:00
|
|
|
}
|
2012-03-02 06:27:08 +08:00
|
|
|
|
2015-06-19 20:19:07 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
|
|
|
addKernelAddressSanitizerPasses);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addKernelAddressSanitizerPasses);
|
|
|
|
}
|
|
|
|
|
2017-12-09 09:32:07 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
|
|
|
addHWAddressSanitizerPasses);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addHWAddressSanitizerPasses);
|
|
|
|
}
|
|
|
|
|
2018-04-14 02:05:21 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
|
|
|
addKernelHWAddressSanitizerPasses);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addKernelHWAddressSanitizerPasses);
|
|
|
|
}
|
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
|
2012-12-03 21:20:43 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
|
|
|
addMemorySanitizerPass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addMemorySanitizerPass);
|
|
|
|
}
|
|
|
|
|
2018-09-07 17:21:09 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::KernelMemory)) {
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
|
|
|
addKernelMemorySanitizerPass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addKernelMemorySanitizerPass);
|
|
|
|
}
|
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
|
2012-03-24 07:25:23 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
2012-03-02 06:27:08 +08:00
|
|
|
addThreadSanitizerPass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addThreadSanitizerPass);
|
|
|
|
}
|
|
|
|
|
2014-11-08 06:29:38 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
|
2013-08-08 06:47:34 +08:00
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
|
|
|
|
addDataFlowSanitizerPass);
|
|
|
|
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
|
|
|
|
addDataFlowSanitizerPass);
|
|
|
|
}
|
|
|
|
|
2011-05-22 04:40:11 +08:00
|
|
|
// Set up the per-function pass manager.
|
2016-11-23 04:03:35 +08:00
|
|
|
FPM.add(new TargetLibraryInfoWrapperPass(*TLII));
|
2011-05-22 04:40:11 +08:00
|
|
|
if (CodeGenOpts.VerifyModule)
|
2016-07-15 08:55:40 +08:00
|
|
|
FPM.add(createVerifierPass());
|
2011-04-06 02:49:32 +08:00
|
|
|
|
2011-05-22 04:40:11 +08:00
|
|
|
// Set up the per-module pass manager.
|
2015-01-09 13:10:20 +08:00
|
|
|
if (!CodeGenOpts.RewriteMapFiles.empty())
|
2016-07-15 08:55:40 +08:00
|
|
|
addSymbolRewriterPass(CodeGenOpts, &MPM);
|
2011-02-19 06:20:38 +08:00
|
|
|
|
2020-05-08 09:18:37 +08:00
|
|
|
// Add UniqueInternalLinkageNames Pass which renames internal linkage symbols
|
|
|
|
// with unique names.
|
|
|
|
if (CodeGenOpts.UniqueInternalLinkageNames) {
|
|
|
|
MPM.add(createUniqueInternalLinkageNamesPass());
|
|
|
|
}
|
|
|
|
|
2020-08-29 07:32:35 +08:00
|
|
|
if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts, LangOpts)) {
|
2018-01-10 06:03:47 +08:00
|
|
|
MPM.add(createGCOVProfilerPass(*Options));
|
2016-02-02 19:06:51 +08:00
|
|
|
if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
|
2016-07-15 08:55:40 +08:00
|
|
|
MPM.add(createStripSymbolsPass(true));
|
2011-04-22 07:44:07 +08:00
|
|
|
}
|
2012-10-24 11:52:31 +08:00
|
|
|
|
2019-04-26 01:52:43 +08:00
|
|
|
if (Optional<InstrProfOptions> Options =
|
|
|
|
getInstrProfOptions(CodeGenOpts, LangOpts))
|
|
|
|
MPM.add(createInstrProfilingLegacyPass(*Options, false));
|
2018-08-17 06:24:47 +08:00
|
|
|
|
2019-03-05 04:21:31 +08:00
|
|
|
bool hasIRInstr = false;
|
2016-03-01 02:54:59 +08:00
|
|
|
if (CodeGenOpts.hasProfileIRInstr()) {
|
2016-07-23 12:28:59 +08:00
|
|
|
PMBuilder.EnablePGOInstrGen = true;
|
2019-03-05 04:21:31 +08:00
|
|
|
hasIRInstr = true;
|
|
|
|
}
|
|
|
|
if (CodeGenOpts.hasProfileCSIRInstr()) {
|
|
|
|
assert(!CodeGenOpts.hasProfileCSIRUse() &&
|
|
|
|
"Cannot have both CSProfileUse pass and CSProfileGen pass at the "
|
|
|
|
"same time");
|
|
|
|
assert(!hasIRInstr &&
|
|
|
|
"Cannot have both ProfileGen pass and CSProfileGen pass at the "
|
|
|
|
"same time");
|
|
|
|
PMBuilder.EnablePGOCSInstrGen = true;
|
|
|
|
hasIRInstr = true;
|
|
|
|
}
|
|
|
|
if (hasIRInstr) {
|
2016-03-01 02:54:59 +08:00
|
|
|
if (!CodeGenOpts.InstrProfileOutput.empty())
|
|
|
|
PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
|
|
|
|
else
|
2020-01-29 03:23:46 +08:00
|
|
|
PMBuilder.PGOInstrGen = std::string(DefaultProfileGenName);
|
2016-03-01 02:54:59 +08:00
|
|
|
}
|
2019-03-05 04:21:31 +08:00
|
|
|
if (CodeGenOpts.hasProfileIRUse()) {
|
2016-03-03 04:59:36 +08:00
|
|
|
PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
|
2019-03-05 04:21:31 +08:00
|
|
|
PMBuilder.EnablePGOCSInstrUse = CodeGenOpts.hasProfileCSIRUse();
|
|
|
|
}
|
2014-12-09 03:04:51 +08:00
|
|
|
|
2016-12-15 05:41:04 +08:00
|
|
|
if (!CodeGenOpts.SampleProfileFile.empty())
|
|
|
|
PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile;
|
2015-08-25 23:25:13 +08:00
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
PMBuilder.populateFunctionPassManager(FPM);
|
|
|
|
PMBuilder.populateModulePassManager(MPM);
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
2017-04-20 04:08:21 +08:00
|
|
|
static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
|
2016-04-13 04:22:32 +08:00
|
|
|
SmallVector<const char *, 16> BackendArgs;
|
|
|
|
BackendArgs.push_back("clang"); // Fake program name.
|
|
|
|
if (!CodeGenOpts.DebugPass.empty()) {
|
|
|
|
BackendArgs.push_back("-debug-pass");
|
|
|
|
BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
|
|
|
|
}
|
|
|
|
if (!CodeGenOpts.LimitFloatPrecision.empty()) {
|
|
|
|
BackendArgs.push_back("-limit-float-precision");
|
|
|
|
BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
|
|
|
|
}
|
|
|
|
BackendArgs.push_back(nullptr);
|
|
|
|
llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
|
|
|
|
BackendArgs.data());
|
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
|
2010-06-08 07:20:08 +08:00
|
|
|
// Create the TargetMachine for generating code.
|
|
|
|
std::string Error;
|
|
|
|
std::string Triple = TheModule->getTargetTriple();
|
|
|
|
const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
|
|
|
|
if (!TheTarget) {
|
2012-10-24 11:52:31 +08:00
|
|
|
if (MustCreateTM)
|
2013-03-27 08:14:35 +08:00
|
|
|
Diags.Report(diag::err_fe_unable_to_create_target) << Error;
|
2016-07-15 08:55:40 +08:00
|
|
|
return;
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
2017-08-03 10:16:28 +08:00
|
|
|
Optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
|
2015-09-26 09:25:08 +08:00
|
|
|
std::string FeaturesStr =
|
|
|
|
llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
|
2018-01-18 08:20:03 +08:00
|
|
|
llvm::Reloc::Model RM = CodeGenOpts.RelocationModel;
|
2017-03-31 10:05:15 +08:00
|
|
|
CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts);
|
2011-11-16 16:38:55 +08:00
|
|
|
|
2011-12-03 06:17:00 +08:00
|
|
|
llvm::TargetOptions Options;
|
2020-11-10 01:26:37 +08:00
|
|
|
if (!initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts,
|
|
|
|
HSOpts))
|
|
|
|
return;
|
2016-07-15 08:55:40 +08:00
|
|
|
TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
|
|
|
|
Options, RM, CM, OptLevel));
|
2012-10-24 08:53:38 +08:00
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
|
|
|
|
BackendAction Action,
|
2018-05-23 02:52:37 +08:00
|
|
|
raw_pwrite_stream &OS,
|
|
|
|
raw_pwrite_stream *DwoOS) {
|
2012-03-01 04:14:59 +08:00
|
|
|
// Add LibraryInfo.
|
2012-10-20 04:10:10 +08:00
|
|
|
llvm::Triple TargetTriple(TheModule->getTargetTriple());
|
2020-04-11 09:00:58 +08:00
|
|
|
std::unique_ptr<TargetLibraryInfoImpl> TLII(
|
|
|
|
createTLII(TargetTriple, CodeGenOpts));
|
2016-07-15 08:55:40 +08:00
|
|
|
CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
|
2012-03-01 04:14:59 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
// Normal mode, emit a .s or .o file by running the code generator. Note,
|
|
|
|
// this also adds codegenerator level optimization passes.
|
2019-11-14 07:17:46 +08:00
|
|
|
CodeGenFileType CGFT = getCodeGenFileType(Action);
|
2011-07-06 06:02:36 +08:00
|
|
|
|
|
|
|
// Add ObjC ARC final-cleanup optimizations. This is done as part of the
|
|
|
|
// "codegen" passes so that it isn't run multiple times when there is
|
|
|
|
// inlining happening.
|
2015-05-02 08:56:15 +08:00
|
|
|
if (CodeGenOpts.OptimizationLevel > 0)
|
2016-07-15 08:55:40 +08:00
|
|
|
CodeGenPasses.add(createObjCARCContractPass());
|
2011-07-06 06:02:36 +08:00
|
|
|
|
2018-05-23 02:52:37 +08:00
|
|
|
if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT,
|
2010-06-08 07:20:08 +08:00
|
|
|
/*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
|
|
|
|
Diags.Report(diag::err_fe_unable_to_interface_with_target);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-14 23:15:49 +08:00
|
|
|
void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS) {
|
2018-04-23 17:22:30 +08:00
|
|
|
TimeRegion Region(FrontendTimesIsEnabled ? &CodeGenerationTime : nullptr);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2017-04-20 04:08:21 +08:00
|
|
|
setCommandLineOpts(CodeGenOpts);
|
2016-04-13 04:22:32 +08:00
|
|
|
|
2012-10-24 11:52:31 +08:00
|
|
|
bool UsesCodeGen = (Action != Backend_EmitNothing &&
|
|
|
|
Action != Backend_EmitBC &&
|
|
|
|
Action != Backend_EmitLL);
|
2016-07-15 08:55:40 +08:00
|
|
|
CreateTargetMachine(UsesCodeGen);
|
2013-12-21 04:26:53 +08:00
|
|
|
|
2015-06-23 21:59:36 +08:00
|
|
|
if (UsesCodeGen && !TM)
|
|
|
|
return;
|
|
|
|
if (TM)
|
2015-07-25 00:04:29 +08:00
|
|
|
TheModule->setDataLayout(TM->createDataLayout());
|
2016-01-09 01:04:29 +08:00
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
legacy::PassManager PerModulePasses;
|
|
|
|
PerModulePasses.add(
|
|
|
|
createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
|
|
|
|
|
|
|
|
legacy::FunctionPassManager PerFunctionPasses(TheModule);
|
|
|
|
PerFunctionPasses.add(
|
|
|
|
createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
|
|
|
|
|
2016-08-13 02:12:08 +08:00
|
|
|
CreatePasses(PerModulePasses, PerFunctionPasses);
|
2016-07-15 08:55:40 +08:00
|
|
|
|
|
|
|
legacy::PassManager CodeGenPasses;
|
|
|
|
CodeGenPasses.add(
|
|
|
|
createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
|
2012-10-24 08:53:38 +08:00
|
|
|
|
2018-05-23 02:52:37 +08:00
|
|
|
std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
|
2017-03-24 03:47:49 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
switch (Action) {
|
|
|
|
case Backend_EmitNothing:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Backend_EmitBC:
|
2018-08-25 03:31:52 +08:00
|
|
|
if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
|
2017-03-24 03:47:49 +08:00
|
|
|
if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
|
2018-05-23 02:52:37 +08:00
|
|
|
ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
|
|
|
|
if (!ThinLinkOS)
|
2017-03-24 03:47:49 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-01-12 02:32:07 +08:00
|
|
|
TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
|
|
|
|
CodeGenOpts.EnableSplitLTOUnit);
|
2018-05-23 02:52:37 +08:00
|
|
|
PerModulePasses.add(createWriteThinLTOBitcodePass(
|
|
|
|
*OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
|
2018-06-23 04:23:21 +08:00
|
|
|
} else {
|
|
|
|
// Emit a module summary by default for Regular LTO except for ld64
|
|
|
|
// targets
|
|
|
|
bool EmitLTOSummary =
|
|
|
|
(CodeGenOpts.PrepareForLTO &&
|
2018-08-25 03:31:52 +08:00
|
|
|
!CodeGenOpts.DisableLLVMPasses &&
|
2018-06-23 04:23:21 +08:00
|
|
|
llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
|
|
|
|
llvm::Triple::Apple);
|
2019-01-12 02:32:07 +08:00
|
|
|
if (EmitLTOSummary) {
|
|
|
|
if (!TheModule->getModuleFlag("ThinLTO"))
|
|
|
|
TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
|
|
|
|
TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
|
2019-07-20 07:02:58 +08:00
|
|
|
uint32_t(1));
|
2019-01-12 02:32:07 +08:00
|
|
|
}
|
2018-06-23 04:23:21 +08:00
|
|
|
|
2019-01-12 02:32:07 +08:00
|
|
|
PerModulePasses.add(createBitcodeWriterPass(
|
|
|
|
*OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
|
2018-06-23 04:23:21 +08:00
|
|
|
}
|
2010-06-08 07:20:08 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Backend_EmitLL:
|
2016-07-15 08:55:40 +08:00
|
|
|
PerModulePasses.add(
|
2015-04-15 10:45:28 +08:00
|
|
|
createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
|
2010-06-08 07:20:08 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2019-06-27 05:36:35 +08:00
|
|
|
if (!CodeGenOpts.SplitDwarfOutput.empty()) {
|
2019-06-15 22:07:43 +08:00
|
|
|
DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
|
2018-05-23 02:52:37 +08:00
|
|
|
if (!DwoOS)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!AddEmitPasses(CodeGenPasses, Action, *OS,
|
|
|
|
DwoOS ? &DwoOS->os() : nullptr))
|
2010-06-08 07:20:08 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-06 02:56:55 +08:00
|
|
|
// Before executing passes, print the final values of the LLVM options.
|
|
|
|
cl::PrintOptionValues();
|
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
// Run passes. For now we do all passes at once, but eventually we
|
|
|
|
// would like to have the option of streaming code generation.
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
{
|
2010-06-08 07:20:08 +08:00
|
|
|
PrettyStackTraceString CrashInfo("Per-function optimization");
|
2019-12-11 19:49:42 +08:00
|
|
|
llvm::TimeTraceScope TimeScope("PerFunctionPasses");
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
PerFunctionPasses.doInitialization();
|
2015-06-05 17:40:53 +08:00
|
|
|
for (Function &F : *TheModule)
|
|
|
|
if (!F.isDeclaration())
|
2016-07-15 08:55:40 +08:00
|
|
|
PerFunctionPasses.run(F);
|
|
|
|
PerFunctionPasses.doFinalization();
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
{
|
2010-06-08 07:20:08 +08:00
|
|
|
PrettyStackTraceString CrashInfo("Per-module optimization passes");
|
2019-12-11 19:49:42 +08:00
|
|
|
llvm::TimeTraceScope TimeScope("PerModulePasses");
|
2016-07-15 08:55:40 +08:00
|
|
|
PerModulePasses.run(*TheModule);
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
{
|
2010-06-08 07:20:08 +08:00
|
|
|
PrettyStackTraceString CrashInfo("Code generation");
|
2019-12-11 19:49:42 +08:00
|
|
|
llvm::TimeTraceScope TimeScope("CodeGenPasses");
|
2016-07-15 08:55:40 +08:00
|
|
|
CodeGenPasses.run(*TheModule);
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
2018-05-23 02:52:37 +08:00
|
|
|
|
|
|
|
if (ThinLinkOS)
|
|
|
|
ThinLinkOS->keep();
|
|
|
|
if (DwoOS)
|
|
|
|
DwoOS->keep();
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
|
|
|
|
2016-12-24 04:44:01 +08:00
|
|
|
static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
|
|
|
|
switch (Opts.OptimizationLevel) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid optimization level!");
|
|
|
|
|
2020-11-09 05:51:29 +08:00
|
|
|
case 0:
|
|
|
|
return PassBuilder::OptimizationLevel::O0;
|
|
|
|
|
2016-12-24 04:44:01 +08:00
|
|
|
case 1:
|
[llvm] Make new pass manager's OptimizationLevel a class
Summary:
The old pass manager separated speed optimization and size optimization
levels into two unsigned values. Coallescing both in an enum in the new
pass manager may lead to unintentional casts and comparisons.
In particular, taking a look at how the loop unroll passes were constructed
previously, the Os/Oz are now (==new pass manager) treated just like O3,
likely unintentionally.
This change disallows raw comparisons between optimization levels, to
avoid such unintended effects. As an effect, the O{s|z} behavior changes
for loop unrolling and loop unroll and jam, matching O2 rather than O3.
The change also parameterizes the threshold values used for loop
unrolling, primarily to aid testing.
Reviewers: tejohnson, davidxl
Reviewed By: tejohnson
Subscribers: zzheng, ychen, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D72547
2020-01-17 00:51:50 +08:00
|
|
|
return PassBuilder::OptimizationLevel::O1;
|
2016-12-24 04:44:01 +08:00
|
|
|
|
|
|
|
case 2:
|
|
|
|
switch (Opts.OptimizeSize) {
|
|
|
|
default:
|
2018-04-06 23:14:32 +08:00
|
|
|
llvm_unreachable("Invalid optimization level for size!");
|
2016-12-24 04:44:01 +08:00
|
|
|
|
|
|
|
case 0:
|
[llvm] Make new pass manager's OptimizationLevel a class
Summary:
The old pass manager separated speed optimization and size optimization
levels into two unsigned values. Coallescing both in an enum in the new
pass manager may lead to unintentional casts and comparisons.
In particular, taking a look at how the loop unroll passes were constructed
previously, the Os/Oz are now (==new pass manager) treated just like O3,
likely unintentionally.
This change disallows raw comparisons between optimization levels, to
avoid such unintended effects. As an effect, the O{s|z} behavior changes
for loop unrolling and loop unroll and jam, matching O2 rather than O3.
The change also parameterizes the threshold values used for loop
unrolling, primarily to aid testing.
Reviewers: tejohnson, davidxl
Reviewed By: tejohnson
Subscribers: zzheng, ychen, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D72547
2020-01-17 00:51:50 +08:00
|
|
|
return PassBuilder::OptimizationLevel::O2;
|
2016-12-24 04:44:01 +08:00
|
|
|
|
|
|
|
case 1:
|
[llvm] Make new pass manager's OptimizationLevel a class
Summary:
The old pass manager separated speed optimization and size optimization
levels into two unsigned values. Coallescing both in an enum in the new
pass manager may lead to unintentional casts and comparisons.
In particular, taking a look at how the loop unroll passes were constructed
previously, the Os/Oz are now (==new pass manager) treated just like O3,
likely unintentionally.
This change disallows raw comparisons between optimization levels, to
avoid such unintended effects. As an effect, the O{s|z} behavior changes
for loop unrolling and loop unroll and jam, matching O2 rather than O3.
The change also parameterizes the threshold values used for loop
unrolling, primarily to aid testing.
Reviewers: tejohnson, davidxl
Reviewed By: tejohnson
Subscribers: zzheng, ychen, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D72547
2020-01-17 00:51:50 +08:00
|
|
|
return PassBuilder::OptimizationLevel::Os;
|
2016-12-24 04:44:01 +08:00
|
|
|
|
|
|
|
case 2:
|
[llvm] Make new pass manager's OptimizationLevel a class
Summary:
The old pass manager separated speed optimization and size optimization
levels into two unsigned values. Coallescing both in an enum in the new
pass manager may lead to unintentional casts and comparisons.
In particular, taking a look at how the loop unroll passes were constructed
previously, the Os/Oz are now (==new pass manager) treated just like O3,
likely unintentionally.
This change disallows raw comparisons between optimization levels, to
avoid such unintended effects. As an effect, the O{s|z} behavior changes
for loop unrolling and loop unroll and jam, matching O2 rather than O3.
The change also parameterizes the threshold values used for loop
unrolling, primarily to aid testing.
Reviewers: tejohnson, davidxl
Reviewed By: tejohnson
Subscribers: zzheng, ychen, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D72547
2020-01-17 00:51:50 +08:00
|
|
|
return PassBuilder::OptimizationLevel::Oz;
|
2016-12-24 04:44:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case 3:
|
[llvm] Make new pass manager's OptimizationLevel a class
Summary:
The old pass manager separated speed optimization and size optimization
levels into two unsigned values. Coallescing both in an enum in the new
pass manager may lead to unintentional casts and comparisons.
In particular, taking a look at how the loop unroll passes were constructed
previously, the Os/Oz are now (==new pass manager) treated just like O3,
likely unintentionally.
This change disallows raw comparisons between optimization levels, to
avoid such unintended effects. As an effect, the O{s|z} behavior changes
for loop unrolling and loop unroll and jam, matching O2 rather than O3.
The change also parameterizes the threshold values used for loop
unrolling, primarily to aid testing.
Reviewers: tejohnson, davidxl
Reviewed By: tejohnson
Subscribers: zzheng, ychen, mehdi_amini, hiraditya, steven_wu, dexonsmith, dang, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D72547
2020-01-17 00:51:50 +08:00
|
|
|
return PassBuilder::OptimizationLevel::O3;
|
2016-12-24 04:44:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A clean version of `EmitAssembly` that uses the new pass manager.
|
|
|
|
///
|
|
|
|
/// Not all features are currently supported in this system, but where
|
|
|
|
/// necessary it falls back to the legacy pass manager to at least provide
|
|
|
|
/// basic functionality.
|
|
|
|
///
|
|
|
|
/// This API is planned to have its functionality finished and then to replace
|
|
|
|
/// `EmitAssembly` at some point in the future when the default switches.
|
|
|
|
void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
|
|
|
|
BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) {
|
2018-04-23 17:22:30 +08:00
|
|
|
TimeRegion Region(FrontendTimesIsEnabled ? &CodeGenerationTime : nullptr);
|
2017-04-20 04:08:21 +08:00
|
|
|
setCommandLineOpts(CodeGenOpts);
|
2016-12-24 04:44:01 +08:00
|
|
|
|
2019-05-07 07:24:17 +08:00
|
|
|
bool RequiresCodeGen = (Action != Backend_EmitNothing &&
|
|
|
|
Action != Backend_EmitBC &&
|
|
|
|
Action != Backend_EmitLL);
|
|
|
|
CreateTargetMachine(RequiresCodeGen);
|
|
|
|
|
|
|
|
if (RequiresCodeGen && !TM)
|
2016-12-24 04:44:01 +08:00
|
|
|
return;
|
2019-05-07 07:24:17 +08:00
|
|
|
if (TM)
|
|
|
|
TheModule->setDataLayout(TM->createDataLayout());
|
2016-12-24 04:44:01 +08:00
|
|
|
|
2017-07-27 23:29:53 +08:00
|
|
|
Optional<PGOOptions> PGOOpt;
|
|
|
|
|
|
|
|
if (CodeGenOpts.hasProfileIRInstr())
|
|
|
|
// -fprofile-generate.
|
|
|
|
PGOOpt = PGOOptions(CodeGenOpts.InstrProfileOutput.empty()
|
2020-01-29 03:23:46 +08:00
|
|
|
? std::string(DefaultProfileGenName)
|
2017-07-27 23:29:53 +08:00
|
|
|
: CodeGenOpts.InstrProfileOutput,
|
2019-03-05 04:21:31 +08:00
|
|
|
"", "", PGOOptions::IRInstr, PGOOptions::NoCSAction,
|
2018-10-11 07:13:35 +08:00
|
|
|
CodeGenOpts.DebugInfoForProfiling);
|
2019-03-05 04:21:31 +08:00
|
|
|
else if (CodeGenOpts.hasProfileIRUse()) {
|
2017-07-27 23:29:53 +08:00
|
|
|
// -fprofile-use.
|
2019-03-05 04:21:31 +08:00
|
|
|
auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
|
|
|
|
: PGOOptions::NoCSAction;
|
|
|
|
PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",
|
|
|
|
CodeGenOpts.ProfileRemappingFile, PGOOptions::IRUse,
|
|
|
|
CSAction, CodeGenOpts.DebugInfoForProfiling);
|
|
|
|
} else if (!CodeGenOpts.SampleProfileFile.empty())
|
2017-07-27 23:29:53 +08:00
|
|
|
// -fprofile-sample-use
|
2019-03-05 04:21:31 +08:00
|
|
|
PGOOpt =
|
|
|
|
PGOOptions(CodeGenOpts.SampleProfileFile, "",
|
|
|
|
CodeGenOpts.ProfileRemappingFile, PGOOptions::SampleUse,
|
|
|
|
PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling);
|
2017-07-27 23:29:53 +08:00
|
|
|
else if (CodeGenOpts.DebugInfoForProfiling)
|
|
|
|
// -fdebug-info-for-profiling
|
2019-03-05 04:21:31 +08:00
|
|
|
PGOOpt = PGOOptions("", "", "", PGOOptions::NoAction,
|
|
|
|
PGOOptions::NoCSAction, true);
|
|
|
|
|
|
|
|
// Check to see if we want to generate a CS profile.
|
|
|
|
if (CodeGenOpts.hasProfileCSIRInstr()) {
|
|
|
|
assert(!CodeGenOpts.hasProfileCSIRUse() &&
|
|
|
|
"Cannot have both CSProfileUse pass and CSProfileGen pass at "
|
|
|
|
"the same time");
|
|
|
|
if (PGOOpt.hasValue()) {
|
|
|
|
assert(PGOOpt->Action != PGOOptions::IRInstr &&
|
|
|
|
PGOOpt->Action != PGOOptions::SampleUse &&
|
|
|
|
"Cannot run CSProfileGen pass with ProfileGen or SampleUse "
|
|
|
|
" pass");
|
|
|
|
PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty()
|
2020-01-29 03:23:46 +08:00
|
|
|
? std::string(DefaultProfileGenName)
|
2019-03-05 04:21:31 +08:00
|
|
|
: CodeGenOpts.InstrProfileOutput;
|
|
|
|
PGOOpt->CSAction = PGOOptions::CSIRInstr;
|
|
|
|
} else
|
|
|
|
PGOOpt = PGOOptions("",
|
|
|
|
CodeGenOpts.InstrProfileOutput.empty()
|
2020-01-29 03:23:46 +08:00
|
|
|
? std::string(DefaultProfileGenName)
|
2019-03-05 04:21:31 +08:00
|
|
|
: CodeGenOpts.InstrProfileOutput,
|
|
|
|
"", PGOOptions::NoAction, PGOOptions::CSIRInstr,
|
|
|
|
CodeGenOpts.DebugInfoForProfiling);
|
|
|
|
}
|
2017-07-27 23:29:53 +08:00
|
|
|
|
2019-05-24 02:51:02 +08:00
|
|
|
PipelineTuningOptions PTO;
|
2019-05-25 01:40:52 +08:00
|
|
|
PTO.LoopUnrolling = CodeGenOpts.UnrollLoops;
|
2019-05-24 02:51:02 +08:00
|
|
|
// For historical reasons, loop interleaving is set to mirror setting for loop
|
|
|
|
// unrolling.
|
|
|
|
PTO.LoopInterleaving = CodeGenOpts.UnrollLoops;
|
|
|
|
PTO.LoopVectorization = CodeGenOpts.VectorizeLoop;
|
|
|
|
PTO.SLPVectorization = CodeGenOpts.VectorizeSLP;
|
[LPM] Port CGProfilePass from NPM to LPM
Reviewers: hans, chandlerc!, asbirlea, nikic
Reviewed By: hans, nikic
Subscribers: steven_wu, dexonsmith, nikic, echristo, void, zhizhouy, cfe-commits, aeubanks, MaskRay, jvesely, nhaehnle, hiraditya, kerbowa, llvm-commits
Tags: #llvm, #clang
Differential Revision: https://reviews.llvm.org/D83013
2020-07-09 03:30:28 +08:00
|
|
|
// Only enable CGProfilePass when using integrated assembler, since
|
|
|
|
// non-integrated assemblers don't recognize .cgprofile section.
|
|
|
|
PTO.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS;
|
2019-12-26 21:00:00 +08:00
|
|
|
PTO.Coroutines = LangOpts.Coroutines;
|
2019-05-24 02:51:02 +08:00
|
|
|
|
2019-08-14 15:11:09 +08:00
|
|
|
PassInstrumentationCallbacks PIC;
|
2020-07-22 00:48:22 +08:00
|
|
|
StandardInstrumentations SI(CodeGenOpts.DebugPassManager);
|
2019-08-14 15:11:09 +08:00
|
|
|
SI.registerCallbacks(PIC);
|
2020-10-30 06:43:31 +08:00
|
|
|
PassBuilder PB(CodeGenOpts.DebugPassManager, TM.get(), PTO, PGOOpt, &PIC);
|
2016-12-24 04:44:01 +08:00
|
|
|
|
2019-02-03 07:19:32 +08:00
|
|
|
// Attempt to load pass plugins and register their callbacks with PB.
|
|
|
|
for (auto &PluginFN : CodeGenOpts.PassPlugins) {
|
|
|
|
auto PassPlugin = PassPlugin::Load(PluginFN);
|
|
|
|
if (PassPlugin) {
|
|
|
|
PassPlugin->registerPassBuilderCallbacks(PB);
|
|
|
|
} else {
|
|
|
|
Diags.Report(diag::err_fe_unable_to_load_plugin)
|
|
|
|
<< PluginFN << toString(PassPlugin.takeError());
|
|
|
|
}
|
|
|
|
}
|
2019-06-08 23:37:47 +08:00
|
|
|
#define HANDLE_EXTENSION(Ext) \
|
|
|
|
get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
|
|
|
|
#include "llvm/Support/Extension.def"
|
2019-02-03 07:19:32 +08:00
|
|
|
|
2017-11-14 16:48:28 +08:00
|
|
|
LoopAnalysisManager LAM(CodeGenOpts.DebugPassManager);
|
|
|
|
FunctionAnalysisManager FAM(CodeGenOpts.DebugPassManager);
|
|
|
|
CGSCCAnalysisManager CGAM(CodeGenOpts.DebugPassManager);
|
|
|
|
ModuleAnalysisManager MAM(CodeGenOpts.DebugPassManager);
|
2016-12-24 04:44:01 +08:00
|
|
|
|
|
|
|
// Register the AA manager first so that our version is the one used.
|
|
|
|
FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });
|
|
|
|
|
2017-07-25 18:46:07 +08:00
|
|
|
// Register the target library analysis directly and give it a customized
|
|
|
|
// preset TLI.
|
|
|
|
Triple TargetTriple(TheModule->getTargetTriple());
|
2020-04-11 09:00:58 +08:00
|
|
|
std::unique_ptr<TargetLibraryInfoImpl> TLII(
|
|
|
|
createTLII(TargetTriple, CodeGenOpts));
|
2017-07-25 18:46:07 +08:00
|
|
|
FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
|
|
|
|
|
2016-12-24 04:44:01 +08:00
|
|
|
// Register all the basic analyses with the managers.
|
|
|
|
PB.registerModuleAnalyses(MAM);
|
|
|
|
PB.registerCGSCCAnalyses(CGAM);
|
|
|
|
PB.registerFunctionAnalyses(FAM);
|
|
|
|
PB.registerLoopAnalyses(LAM);
|
|
|
|
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
|
|
|
|
|
2017-06-30 07:10:13 +08:00
|
|
|
ModulePassManager MPM(CodeGenOpts.DebugPassManager);
|
2016-12-24 04:44:01 +08:00
|
|
|
|
2016-12-27 08:13:09 +08:00
|
|
|
if (!CodeGenOpts.DisableLLVMPasses) {
|
2020-11-09 05:51:29 +08:00
|
|
|
// Map our optimization levels into one of the distinct levels used to
|
|
|
|
// configure the pipeline.
|
|
|
|
PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts);
|
|
|
|
|
2018-06-23 04:23:21 +08:00
|
|
|
bool IsThinLTO = CodeGenOpts.PrepareForThinLTO;
|
2017-06-30 07:08:38 +08:00
|
|
|
bool IsLTO = CodeGenOpts.PrepareForLTO;
|
|
|
|
|
2020-11-06 02:25:35 +08:00
|
|
|
// If we reached here with a non-empty index file name, then the index
|
|
|
|
// file was empty and we are not performing ThinLTO backend compilation
|
|
|
|
// (used in testing in a distributed build environment). Drop any the type
|
|
|
|
// test assume sequences inserted for whole program vtables so that
|
|
|
|
// codegen doesn't complain.
|
|
|
|
if (!CodeGenOpts.ThinLTOIndexFile.empty())
|
|
|
|
PB.registerPipelineStartEPCallback(
|
|
|
|
[](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
|
|
|
|
MPM.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr,
|
|
|
|
/*ImportSummary=*/nullptr,
|
|
|
|
/*DropTypeTests=*/true));
|
|
|
|
});
|
2020-01-25 04:24:18 +08:00
|
|
|
|
2020-11-06 02:25:35 +08:00
|
|
|
if (Level != PassBuilder::OptimizationLevel::O0) {
|
2020-11-05 02:14:37 +08:00
|
|
|
PB.registerPipelineStartEPCallback(
|
|
|
|
[](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
|
|
|
|
MPM.addPass(createModuleToFunctionPassAdaptor(
|
|
|
|
EntryExitInstrumenterPass(/*PostInlining=*/false)));
|
|
|
|
});
|
2020-11-06 02:25:35 +08:00
|
|
|
}
|
2019-06-21 03:35:25 +08:00
|
|
|
|
2020-11-06 02:25:35 +08:00
|
|
|
// Register callbacks to schedule sanitizer passes at the appropriate part
|
|
|
|
// of the pipeline.
|
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
|
|
|
|
PB.registerScalarOptimizerLateEPCallback(
|
|
|
|
[](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
|
|
|
|
FPM.addPass(BoundsCheckingPass());
|
|
|
|
});
|
2020-05-28 14:12:36 +08:00
|
|
|
|
2020-11-06 02:25:35 +08:00
|
|
|
if (CodeGenOpts.SanitizeCoverageType ||
|
|
|
|
CodeGenOpts.SanitizeCoverageIndirectCalls ||
|
|
|
|
CodeGenOpts.SanitizeCoverageTraceCmp) {
|
|
|
|
PB.registerOptimizerLastEPCallback(
|
|
|
|
[this](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
|
|
|
|
auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts);
|
|
|
|
MPM.addPass(ModuleSanitizerCoveragePass(
|
|
|
|
SancovOpts, CodeGenOpts.SanitizeCoverageAllowlistFiles,
|
|
|
|
CodeGenOpts.SanitizeCoverageBlocklistFiles));
|
|
|
|
});
|
|
|
|
}
|
2020-05-28 14:12:36 +08:00
|
|
|
|
2020-11-06 02:25:35 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
|
|
|
|
int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
|
|
|
|
bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::Memory);
|
|
|
|
PB.registerOptimizerLastEPCallback(
|
|
|
|
[TrackOrigins, Recover](ModulePassManager &MPM,
|
|
|
|
PassBuilder::OptimizationLevel Level) {
|
|
|
|
MPM.addPass(MemorySanitizerPass({TrackOrigins, Recover, false}));
|
|
|
|
MPM.addPass(createModuleToFunctionPassAdaptor(
|
|
|
|
MemorySanitizerPass({TrackOrigins, Recover, false})));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
|
|
|
|
PB.registerOptimizerLastEPCallback(
|
|
|
|
[](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
|
|
|
|
MPM.addPass(ThreadSanitizerPass());
|
|
|
|
MPM.addPass(
|
|
|
|
createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
|
|
|
|
if (LangOpts.Sanitize.has(Mask)) {
|
|
|
|
bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
|
2019-02-14 09:07:47 +08:00
|
|
|
bool UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
|
2019-02-14 06:22:48 +08:00
|
|
|
bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
|
2019-02-14 09:07:47 +08:00
|
|
|
bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator;
|
Move *San module passes later in the NPM pipeline
Summary:
This fixes pr33372.cpp under the new pass manager.
ASan adds padding to globals. For example, it will change a {i32, i32, i32} to a {{i32, i32, i32}, [52 x i8]}. However, when loading from the {i32, i32, i32}, InstCombine may (after various optimizations) end up loading 16 bytes instead of 12, likely because it thinks the [52 x i8] padding is ok to load from. But ASan checks that padding should not be loaded from.
Ultimately this is an issue of *San passes wanting to be run after all optimizations. This change moves the module passes right next to the corresponding function passes.
Also remove comment that's no longer relevant, this is the last ASan/MSan/TSan failure under the NPM (hopefully...).
As mentioned in https://reviews.llvm.org/rG1285e8bcac2c54ddd924ffb813b2b187467ac2a6, NPM doesn't support LTO + sanitizers, so modified some tests that test for that.
Reviewers: leonardchan, vitalybuka
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81323
2020-05-29 06:00:17 +08:00
|
|
|
PB.registerOptimizerLastEPCallback(
|
2020-11-06 02:25:35 +08:00
|
|
|
[CompileKernel, Recover, UseAfterScope, ModuleUseAfterScope,
|
|
|
|
UseOdrIndicator](ModulePassManager &MPM,
|
|
|
|
PassBuilder::OptimizationLevel Level) {
|
Move *San module passes later in the NPM pipeline
Summary:
This fixes pr33372.cpp under the new pass manager.
ASan adds padding to globals. For example, it will change a {i32, i32, i32} to a {{i32, i32, i32}, [52 x i8]}. However, when loading from the {i32, i32, i32}, InstCombine may (after various optimizations) end up loading 16 bytes instead of 12, likely because it thinks the [52 x i8] padding is ok to load from. But ASan checks that padding should not be loaded from.
Ultimately this is an issue of *San passes wanting to be run after all optimizations. This change moves the module passes right next to the corresponding function passes.
Also remove comment that's no longer relevant, this is the last ASan/MSan/TSan failure under the NPM (hopefully...).
As mentioned in https://reviews.llvm.org/rG1285e8bcac2c54ddd924ffb813b2b187467ac2a6, NPM doesn't support LTO + sanitizers, so modified some tests that test for that.
Reviewers: leonardchan, vitalybuka
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D81323
2020-05-29 06:00:17 +08:00
|
|
|
MPM.addPass(
|
|
|
|
RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
|
2020-11-06 02:25:35 +08:00
|
|
|
MPM.addPass(ModuleAddressSanitizerPass(CompileKernel, Recover,
|
|
|
|
ModuleUseAfterScope,
|
|
|
|
UseOdrIndicator));
|
|
|
|
MPM.addPass(createModuleToFunctionPassAdaptor(
|
|
|
|
AddressSanitizerPass(CompileKernel, Recover, UseAfterScope)));
|
2019-02-14 09:07:47 +08:00
|
|
|
});
|
2019-02-14 06:22:48 +08:00
|
|
|
}
|
2020-11-06 02:25:35 +08:00
|
|
|
};
|
|
|
|
ASanPass(SanitizerKind::Address, false);
|
|
|
|
ASanPass(SanitizerKind::KernelAddress, true);
|
2020-10-07 07:13:50 +08:00
|
|
|
|
2020-11-06 02:25:35 +08:00
|
|
|
auto HWASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
|
|
|
|
if (LangOpts.Sanitize.has(Mask)) {
|
|
|
|
bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
|
2020-10-07 07:13:50 +08:00
|
|
|
PB.registerOptimizerLastEPCallback(
|
2020-11-06 02:25:35 +08:00
|
|
|
[CompileKernel, Recover](ModulePassManager &MPM,
|
|
|
|
PassBuilder::OptimizationLevel Level) {
|
|
|
|
MPM.addPass(HWAddressSanitizerPass(CompileKernel, Recover));
|
2020-10-07 07:13:50 +08:00
|
|
|
});
|
|
|
|
}
|
2020-11-06 02:25:35 +08:00
|
|
|
};
|
|
|
|
HWASanPass(SanitizerKind::HWAddress, false);
|
|
|
|
HWASanPass(SanitizerKind::KernelHWAddress, true);
|
2020-10-07 07:13:50 +08:00
|
|
|
|
2020-11-13 05:19:04 +08:00
|
|
|
if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
|
|
|
|
PB.registerOptimizerLastEPCallback(
|
|
|
|
[this](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
|
|
|
|
MPM.addPass(
|
|
|
|
DataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-06 02:25:35 +08:00
|
|
|
if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts, LangOpts))
|
|
|
|
PB.registerPipelineStartEPCallback(
|
|
|
|
[Options](ModulePassManager &MPM,
|
|
|
|
PassBuilder::OptimizationLevel Level) {
|
|
|
|
MPM.addPass(GCOVProfilerPass(*Options));
|
|
|
|
});
|
|
|
|
if (Optional<InstrProfOptions> Options =
|
|
|
|
getInstrProfOptions(CodeGenOpts, LangOpts))
|
|
|
|
PB.registerPipelineStartEPCallback(
|
|
|
|
[Options](ModulePassManager &MPM,
|
|
|
|
PassBuilder::OptimizationLevel Level) {
|
|
|
|
MPM.addPass(InstrProfiling(*Options, false));
|
|
|
|
});
|
2019-02-14 06:22:48 +08:00
|
|
|
|
2020-11-06 02:25:35 +08:00
|
|
|
if (CodeGenOpts.OptimizationLevel == 0) {
|
2020-11-17 04:48:42 +08:00
|
|
|
MPM = PB.buildO0DefaultPipeline(Level, IsLTO || IsThinLTO);
|
2020-11-06 02:25:35 +08:00
|
|
|
} else if (IsThinLTO) {
|
|
|
|
MPM = PB.buildThinLTOPreLinkDefaultPipeline(Level);
|
|
|
|
} else if (IsLTO) {
|
|
|
|
MPM = PB.buildLTOPreLinkDefaultPipeline(Level);
|
|
|
|
} else {
|
|
|
|
MPM = PB.buildPerModuleDefaultPipeline(Level);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add UniqueInternalLinkageNames Pass which renames internal linkage
|
|
|
|
// symbols with unique names.
|
|
|
|
if (CodeGenOpts.UniqueInternalLinkageNames)
|
|
|
|
MPM.addPass(UniqueInternalLinkageNamesPass());
|
|
|
|
|
|
|
|
if (!CodeGenOpts.MemoryProfileOutput.empty()) {
|
|
|
|
MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass()));
|
|
|
|
MPM.addPass(ModuleMemProfilerPass());
|
2019-07-26 04:53:15 +08:00
|
|
|
}
|
2016-12-24 04:44:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: We still use the legacy pass manager to do code generation. We
|
|
|
|
// create that pass manager here and use it as needed below.
|
|
|
|
legacy::PassManager CodeGenPasses;
|
|
|
|
bool NeedCodeGen = false;
|
2018-05-23 02:52:37 +08:00
|
|
|
std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
|
2016-12-24 04:44:01 +08:00
|
|
|
|
|
|
|
// Append any output we need to the pass manager.
|
|
|
|
switch (Action) {
|
|
|
|
case Backend_EmitNothing:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Backend_EmitBC:
|
2018-08-25 03:31:52 +08:00
|
|
|
if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
|
2017-06-02 07:27:51 +08:00
|
|
|
if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
|
2018-05-23 02:52:37 +08:00
|
|
|
ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
|
|
|
|
if (!ThinLinkOS)
|
2017-06-02 07:27:51 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-01-12 02:32:07 +08:00
|
|
|
TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
|
|
|
|
CodeGenOpts.EnableSplitLTOUnit);
|
2018-05-23 02:52:37 +08:00
|
|
|
MPM.addPass(ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &ThinLinkOS->os()
|
|
|
|
: nullptr));
|
2017-06-02 07:27:51 +08:00
|
|
|
} else {
|
2018-06-23 04:23:21 +08:00
|
|
|
// Emit a module summary by default for Regular LTO except for ld64
|
|
|
|
// targets
|
|
|
|
bool EmitLTOSummary =
|
|
|
|
(CodeGenOpts.PrepareForLTO &&
|
2018-08-25 03:31:52 +08:00
|
|
|
!CodeGenOpts.DisableLLVMPasses &&
|
2018-06-23 04:23:21 +08:00
|
|
|
llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
|
|
|
|
llvm::Triple::Apple);
|
2019-01-12 02:32:07 +08:00
|
|
|
if (EmitLTOSummary) {
|
|
|
|
if (!TheModule->getModuleFlag("ThinLTO"))
|
|
|
|
TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
|
|
|
|
TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
|
2019-08-22 01:24:14 +08:00
|
|
|
uint32_t(1));
|
2019-01-12 02:32:07 +08:00
|
|
|
}
|
|
|
|
MPM.addPass(
|
|
|
|
BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
|
2017-06-02 07:27:51 +08:00
|
|
|
}
|
2016-12-24 04:44:01 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Backend_EmitLL:
|
|
|
|
MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Backend_EmitAssembly:
|
|
|
|
case Backend_EmitMCNull:
|
|
|
|
case Backend_EmitObj:
|
|
|
|
NeedCodeGen = true;
|
|
|
|
CodeGenPasses.add(
|
|
|
|
createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
|
2019-06-27 05:36:35 +08:00
|
|
|
if (!CodeGenOpts.SplitDwarfOutput.empty()) {
|
2019-06-15 22:07:43 +08:00
|
|
|
DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
|
2018-05-23 02:52:37 +08:00
|
|
|
if (!DwoOS)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!AddEmitPasses(CodeGenPasses, Action, *OS,
|
|
|
|
DwoOS ? &DwoOS->os() : nullptr))
|
2016-12-24 04:44:01 +08:00
|
|
|
// FIXME: Should we handle this error differently?
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before executing passes, print the final values of the LLVM options.
|
|
|
|
cl::PrintOptionValues();
|
|
|
|
|
|
|
|
// Now that we have all of the passes ready, run them.
|
|
|
|
{
|
|
|
|
PrettyStackTraceString CrashInfo("Optimizer");
|
|
|
|
MPM.run(*TheModule, MAM);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now if needed, run the legacy PM for codegen.
|
|
|
|
if (NeedCodeGen) {
|
|
|
|
PrettyStackTraceString CrashInfo("Code generation");
|
|
|
|
CodeGenPasses.run(*TheModule);
|
|
|
|
}
|
2018-05-23 02:52:37 +08:00
|
|
|
|
|
|
|
if (ThinLinkOS)
|
|
|
|
ThinLinkOS->keep();
|
|
|
|
if (DwoOS)
|
|
|
|
DwoOS->keep();
|
2016-12-24 04:44:01 +08:00
|
|
|
}
|
|
|
|
|
2020-06-02 14:17:29 +08:00
|
|
|
static void runThinLTOBackend(
|
|
|
|
DiagnosticsEngine &Diags, ModuleSummaryIndex *CombinedIndex, Module *M,
|
|
|
|
const HeaderSearchOptions &HeaderOpts, const CodeGenOptions &CGOpts,
|
|
|
|
const clang::TargetOptions &TOpts, const LangOptions &LOpts,
|
|
|
|
std::unique_ptr<raw_pwrite_stream> OS, std::string SampleProfile,
|
|
|
|
std::string ProfileRemapping, BackendAction Action) {
|
2017-07-11 04:31:37 +08:00
|
|
|
StringMap<DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
|
2016-08-13 02:12:08 +08:00
|
|
|
ModuleToDefinedGVSummaries;
|
|
|
|
CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
|
|
|
|
|
2017-04-20 04:08:21 +08:00
|
|
|
setCommandLineOpts(CGOpts);
|
|
|
|
|
2016-12-29 02:00:08 +08:00
|
|
|
// We can simply import the values mentioned in the combined index, since
|
|
|
|
// we should only invoke this using the individual indexes written out
|
|
|
|
// via a WriteIndexesThinBackend.
|
2016-08-13 02:12:08 +08:00
|
|
|
FunctionImporter::ImportMapTy ImportList;
|
|
|
|
std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports;
|
2016-12-14 09:17:59 +08:00
|
|
|
MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap;
|
2020-09-17 03:08:15 +08:00
|
|
|
if (!lto::loadReferencedModules(*M, *CombinedIndex, ImportList, ModuleMap,
|
|
|
|
OwnedImports))
|
|
|
|
return;
|
2016-08-13 02:12:08 +08:00
|
|
|
|
2016-09-24 05:43:51 +08:00
|
|
|
auto AddStream = [&](size_t Task) {
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<lto::NativeObjectStream>(std::move(OS));
|
2016-08-17 14:23:08 +08:00
|
|
|
};
|
2016-08-13 02:12:08 +08:00
|
|
|
lto::Config Conf;
|
2018-04-18 00:39:25 +08:00
|
|
|
if (CGOpts.SaveTempsFilePrefix != "") {
|
|
|
|
if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".",
|
|
|
|
/* UseInputModulePath */ false)) {
|
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
|
|
errs() << "Error setting up ThinLTO save-temps: " << EIB.message()
|
|
|
|
<< '\n';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-03-31 10:05:15 +08:00
|
|
|
Conf.CPU = TOpts.CPU;
|
|
|
|
Conf.CodeModel = getCodeModel(CGOpts);
|
|
|
|
Conf.MAttrs = TOpts.Features;
|
2018-01-18 08:20:03 +08:00
|
|
|
Conf.RelocModel = CGOpts.RelocationModel;
|
2017-03-31 10:05:15 +08:00
|
|
|
Conf.CGOptLevel = getCGOptLevel(CGOpts);
|
[ThinLTO] Pass down opt level to LTO backend and handle -O0 LTO in new PM
Summary:
The opt level was not being passed down to the ThinLTO backend when
invoked via clang (for distributed ThinLTO).
This exposed an issue where the new PM was asserting if the Thin or
regular LTO backend pipelines were invoked with -O0 (not a new issue,
could be provoked by invoking in-process *LTO backends via linker using
new PM and -O0). Fix this similar to the old PM where -O0 only does the
necessary lowering of type metadata (WPD and LowerTypeTest passes) and
then quits, rather than asserting.
Reviewers: xur
Subscribers: mehdi_amini, inglorion, eraman, hiraditya, steven_wu, dexonsmith, cfe-commits, llvm-commits, pcc
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D61022
llvm-svn: 359025
2019-04-24 02:56:19 +08:00
|
|
|
Conf.OptLevel = CGOpts.OptimizationLevel;
|
2020-06-02 14:17:29 +08:00
|
|
|
initTargetOptions(Diags, Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
|
2017-03-22 05:35:04 +08:00
|
|
|
Conf.SampleProfile = std::move(SampleProfile);
|
2020-01-10 12:58:31 +08:00
|
|
|
Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops;
|
|
|
|
// For historical reasons, loop interleaving is set to mirror setting for loop
|
|
|
|
// unrolling.
|
|
|
|
Conf.PTO.LoopInterleaving = CGOpts.UnrollLoops;
|
|
|
|
Conf.PTO.LoopVectorization = CGOpts.VectorizeLoop;
|
|
|
|
Conf.PTO.SLPVectorization = CGOpts.VectorizeSLP;
|
[LPM] Port CGProfilePass from NPM to LPM
Reviewers: hans, chandlerc!, asbirlea, nikic
Reviewed By: hans, nikic
Subscribers: steven_wu, dexonsmith, nikic, echristo, void, zhizhouy, cfe-commits, aeubanks, MaskRay, jvesely, nhaehnle, hiraditya, kerbowa, llvm-commits
Tags: #llvm, #clang
Differential Revision: https://reviews.llvm.org/D83013
2020-07-09 03:30:28 +08:00
|
|
|
// Only enable CGProfilePass when using integrated assembler, since
|
|
|
|
// non-integrated assemblers don't recognize .cgprofile section.
|
|
|
|
Conf.PTO.CallGraphProfile = !CGOpts.DisableIntegratedAS;
|
2019-03-05 04:21:31 +08:00
|
|
|
|
|
|
|
// Context sensitive profile.
|
|
|
|
if (CGOpts.hasProfileCSIRInstr()) {
|
|
|
|
Conf.RunCSIRInstr = true;
|
|
|
|
Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput);
|
|
|
|
} else if (CGOpts.hasProfileCSIRUse()) {
|
|
|
|
Conf.RunCSIRInstr = false;
|
|
|
|
Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath);
|
|
|
|
}
|
|
|
|
|
2018-10-11 07:13:35 +08:00
|
|
|
Conf.ProfileRemapping = std::move(ProfileRemapping);
|
2017-06-02 07:27:51 +08:00
|
|
|
Conf.UseNewPM = CGOpts.ExperimentalNewPassManager;
|
2017-11-13 23:38:33 +08:00
|
|
|
Conf.DebugPassManager = CGOpts.DebugPassManager;
|
2018-05-05 22:37:29 +08:00
|
|
|
Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness;
|
|
|
|
Conf.RemarksFilename = CGOpts.OptRecordFile;
|
2019-03-13 05:22:27 +08:00
|
|
|
Conf.RemarksPasses = CGOpts.OptRecordPasses;
|
2019-06-18 00:06:00 +08:00
|
|
|
Conf.RemarksFormat = CGOpts.OptRecordFormat;
|
2019-06-15 23:38:51 +08:00
|
|
|
Conf.SplitDwarfFile = CGOpts.SplitDwarfFile;
|
|
|
|
Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput;
|
2017-04-01 06:35:47 +08:00
|
|
|
switch (Action) {
|
|
|
|
case Backend_EmitNothing:
|
|
|
|
Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case Backend_EmitLL:
|
|
|
|
Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
|
|
|
|
M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case Backend_EmitBC:
|
|
|
|
Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
|
2018-02-15 03:11:37 +08:00
|
|
|
WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists);
|
2017-04-01 06:35:47 +08:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Conf.CGFileType = getCodeGenFileType(Action);
|
|
|
|
break;
|
|
|
|
}
|
2020-09-15 01:45:00 +08:00
|
|
|
if (Error E =
|
|
|
|
thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
|
|
|
|
ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
|
2020-10-29 03:08:26 +08:00
|
|
|
ModuleMap, CGOpts.CmdArgs)) {
|
2016-08-13 02:12:08 +08:00
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
|
|
errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
|
2017-01-06 00:02:32 +08:00
|
|
|
const HeaderSearchOptions &HeaderOpts,
|
2011-09-26 07:23:43 +08:00
|
|
|
const CodeGenOptions &CGOpts,
|
2011-12-03 06:17:00 +08:00
|
|
|
const clang::TargetOptions &TOpts,
|
2017-01-06 00:02:32 +08:00
|
|
|
const LangOptions &LOpts,
|
|
|
|
const llvm::DataLayout &TDesc, Module *M,
|
|
|
|
BackendAction Action,
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS) {
|
2019-03-30 16:42:48 +08:00
|
|
|
|
2019-12-11 19:49:42 +08:00
|
|
|
llvm::TimeTraceScope TimeScope("Backend");
|
2019-03-30 16:42:48 +08:00
|
|
|
|
2018-02-17 07:38:22 +08:00
|
|
|
std::unique_ptr<llvm::Module> EmptyModule;
|
2016-08-13 02:12:08 +08:00
|
|
|
if (!CGOpts.ThinLTOIndexFile.empty()) {
|
2017-01-07 07:37:33 +08:00
|
|
|
// If we are performing a ThinLTO importing compile, load the function index
|
|
|
|
// into memory and pass it into runThinLTOBackend, which will run the
|
|
|
|
// function importer and invoke LTO passes.
|
|
|
|
Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
|
2017-05-13 03:32:17 +08:00
|
|
|
llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile,
|
|
|
|
/*IgnoreEmptyThinLTOIndexFile*/true);
|
2017-01-07 07:37:33 +08:00
|
|
|
if (!IndexOrErr) {
|
|
|
|
logAllUnhandledErrors(IndexOrErr.takeError(), errs(),
|
|
|
|
"Error loading index file '" +
|
|
|
|
CGOpts.ThinLTOIndexFile + "': ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr);
|
|
|
|
// A null CombinedIndex means we should skip ThinLTO compilation
|
|
|
|
// (LLVM will optionally ignore empty index files, returning null instead
|
|
|
|
// of an error).
|
2018-02-17 07:38:22 +08:00
|
|
|
if (CombinedIndex) {
|
|
|
|
if (!CombinedIndex->skipModuleByDistributedBackend()) {
|
2020-06-02 14:17:29 +08:00
|
|
|
runThinLTOBackend(Diags, CombinedIndex.get(), M, HeaderOpts, CGOpts,
|
|
|
|
TOpts, LOpts, std::move(OS), CGOpts.SampleProfileFile,
|
2018-10-11 07:13:35 +08:00
|
|
|
CGOpts.ProfileRemappingFile, Action);
|
2018-02-17 07:38:22 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Distributed indexing detected that nothing from the module is needed
|
|
|
|
// for the final linking. So we can skip the compilation. We sill need to
|
|
|
|
// output an empty object file to make sure that a linker does not fail
|
|
|
|
// trying to read it. Also for some features, like CFI, we must skip
|
|
|
|
// the compilation as CombinedIndex does not contain all required
|
|
|
|
// information.
|
2019-08-15 07:04:18 +08:00
|
|
|
EmptyModule = std::make_unique<llvm::Module>("empty", M->getContext());
|
2018-02-17 07:38:22 +08:00
|
|
|
EmptyModule->setTargetTriple(M->getTargetTriple());
|
|
|
|
M = EmptyModule.get();
|
2017-01-07 07:37:33 +08:00
|
|
|
}
|
2016-08-13 02:12:08 +08:00
|
|
|
}
|
|
|
|
|
2017-01-06 00:02:32 +08:00
|
|
|
EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2016-12-24 04:44:01 +08:00
|
|
|
if (CGOpts.ExperimentalNewPassManager)
|
|
|
|
AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS));
|
|
|
|
else
|
|
|
|
AsmHelper.EmitAssembly(Action, std::move(OS));
|
2014-01-02 23:08:04 +08:00
|
|
|
|
2016-03-05 03:00:41 +08:00
|
|
|
// Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
|
|
|
|
// DataLayout.
|
|
|
|
if (AsmHelper.TM) {
|
2015-07-25 00:04:29 +08:00
|
|
|
std::string DLDesc = M->getDataLayout().getStringRepresentation();
|
2016-03-05 03:00:41 +08:00
|
|
|
if (DLDesc != TDesc.getStringRepresentation()) {
|
2014-01-02 23:08:04 +08:00
|
|
|
unsigned DiagID = Diags.getCustomDiagID(
|
|
|
|
DiagnosticsEngine::Error, "backend data layout '%0' does not match "
|
|
|
|
"expected target description '%1'");
|
2016-03-05 03:00:41 +08:00
|
|
|
Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
|
2014-01-02 23:08:04 +08:00
|
|
|
}
|
|
|
|
}
|
2010-06-08 07:20:08 +08:00
|
|
|
}
|
2016-05-12 00:26:03 +08:00
|
|
|
|
|
|
|
// With -fembed-bitcode, save a copy of the llvm IR as data in the
|
|
|
|
// __LLVM,__bitcode section.
|
|
|
|
void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
|
|
|
|
llvm::MemoryBufferRef Buf) {
|
|
|
|
if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
|
|
|
|
return;
|
[LTO] Support for embedding bitcode section during LTO
Summary:
This adds support for embedding bitcode in a binary during LTO. The libLTO gains supports the `-lto-embed-bitcode` flag. The option allows users of the LTO library to embed a bitcode section. For example, LLD can pass the option via `ld.lld -mllvm=-lto-embed-bitcode`.
This feature allows doing something comparable to `clang -c -fembed-bitcode`, but on the (LTO) linker level. Having bitcode alongside native code has many use-cases. To give an example, the MacOS linker can create a `-bitcode_bundle` section containing bitcode. Also, having this feature built into LLVM is an alternative to 3rd party tools such as [[ https://github.com/travitch/whole-program-llvm | wllvm ]] or [[ https://github.com/SRI-CSL/gllvm | gllvm ]]. As with these tools, this feature simplifies creating "whole-program" llvm bitcode files, but in contrast to wllvm/gllvm it does not rely on a specific llvm frontend/driver.
Patch by Josef Eisl <josef.eisl@oracle.com>
Reviewers: #llvm, #clang, rsmith, pcc, alexshap, tejohnson
Reviewed By: tejohnson
Subscribers: tejohnson, mehdi_amini, inglorion, hiraditya, aheejin, steven_wu, dexonsmith, dang, cfe-commits, llvm-commits, #llvm, #clang
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D68213
2019-12-13 03:59:36 +08:00
|
|
|
llvm::EmbedBitcodeInModule(
|
|
|
|
*M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker,
|
|
|
|
CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode,
|
2020-10-29 03:08:26 +08:00
|
|
|
CGOpts.CmdArgs);
|
2016-05-12 00:26:03 +08:00
|
|
|
}
|