2010-02-25 12:37:45 +08:00
|
|
|
//===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
|
2008-10-22 07:49:24 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[CodeGen] [CUDA] Add the ability set default attrs on functions in linked modules.
Summary:
Now when you ask clang to link in a bitcode module, you can tell it to
set attributes on that module's functions to match what we would have
set if we'd emitted those functions ourselves.
This is particularly important for fast-math attributes in CUDA
compilations.
Each CUDA compilation links in libdevice, a bitcode library provided by
nvidia as part of the CUDA distribution. Without this patch, if we have
a user-function F that is compiled with -ffast-math that calls a
function G from libdevice, F will have the unsafe-fp-math=true (etc.)
attributes, but G will have no attributes.
Since F calls G, the inliner will merge G's attributes into F's. It
considers the lack of an unsafe-fp-math=true attribute on G to be
tantamount to unsafe-fp-math=false, so it "merges" these by setting
unsafe-fp-math=false on F.
This then continues up the call graph, until every function that
(transitively) calls something in libdevice gets unsafe-fp-math=false
set, thus disabling fastmath in almost all CUDA code.
Reviewers: echristo
Subscribers: hfinkel, llvm-commits, mehdi_amini
Differential Revision: https://reviews.llvm.org/D28538
llvm-svn: 293097
2017-01-26 05:29:48 +08:00
|
|
|
#include "clang/CodeGen/CodeGenAction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2014-08-05 02:41:51 +08:00
|
|
|
#include "CoverageMappingGen.h"
|
2017-02-10 06:07:24 +08:00
|
|
|
#include "MacroPPCallbacks.h"
|
2008-10-22 07:49:24 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-11-15 14:48:46 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2014-05-24 04:37:38 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "clang/AST/DeclGroup.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2010-06-16 01:48:49 +08:00
|
|
|
#include "clang/CodeGen/BackendUtil.h"
|
2009-11-15 14:48:46 +08:00
|
|
|
#include "clang/CodeGen/ModuleBuilder.h"
|
2010-02-25 12:37:45 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2009-12-03 17:12:54 +08:00
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2016-11-11 13:35:12 +08:00
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
2017-01-26 12:07:11 +08:00
|
|
|
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
|
2014-04-17 00:54:24 +08:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2014-02-07 02:30:43 +08:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/DiagnosticPrinter.h"
|
2017-03-14 02:08:11 +08:00
|
|
|
#include "llvm/IR/GlobalValue.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2013-03-26 10:25:54 +08:00
|
|
|
#include "llvm/IRReader/IRReader.h"
|
2014-03-06 11:46:44 +08:00
|
|
|
#include "llvm/Linker/Linker.h"
|
2010-06-08 07:21:04 +08:00
|
|
|
#include "llvm/Pass.h"
|
2010-04-07 02:38:50 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2009-02-18 09:37:30 +08:00
|
|
|
#include "llvm/Support/Timer.h"
|
2016-10-11 08:26:09 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2017-03-14 02:08:11 +08:00
|
|
|
#include "llvm/Transforms/IPO/Internalize.h"
|
|
|
|
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2008-10-22 07:49:24 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace llvm;
|
|
|
|
|
2011-01-26 04:34:14 +08:00
|
|
|
namespace clang {
|
2017-09-16 04:09:55 +08:00
|
|
|
class BackendConsumer;
|
|
|
|
class ClangDiagnosticHandler final : public DiagnosticHandler {
|
|
|
|
public:
|
|
|
|
ClangDiagnosticHandler(const CodeGenOptions &CGOpts, BackendConsumer *BCon)
|
|
|
|
: CodeGenOpts(CGOpts), BackendCon(BCon) {}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2017-09-16 04:09:55 +08:00
|
|
|
bool handleDiagnostics(const DiagnosticInfo &DI) override;
|
2017-09-20 01:59:40 +08:00
|
|
|
|
|
|
|
bool isAnalysisRemarkEnabled(StringRef PassName) const override {
|
2017-09-16 04:09:55 +08:00
|
|
|
return (CodeGenOpts.OptimizationRemarkAnalysisPattern &&
|
|
|
|
CodeGenOpts.OptimizationRemarkAnalysisPattern->match(PassName));
|
|
|
|
}
|
2017-09-20 01:59:40 +08:00
|
|
|
bool isMissedOptRemarkEnabled(StringRef PassName) const override {
|
2017-09-16 04:09:55 +08:00
|
|
|
return (CodeGenOpts.OptimizationRemarkMissedPattern &&
|
|
|
|
CodeGenOpts.OptimizationRemarkMissedPattern->match(PassName));
|
|
|
|
}
|
2017-09-20 01:59:40 +08:00
|
|
|
bool isPassedOptRemarkEnabled(StringRef PassName) const override {
|
2017-09-16 04:09:55 +08:00
|
|
|
return (CodeGenOpts.OptimizationRemarkPattern &&
|
|
|
|
CodeGenOpts.OptimizationRemarkPattern->match(PassName));
|
|
|
|
}
|
2017-09-20 01:59:40 +08:00
|
|
|
|
2017-09-20 07:00:59 +08:00
|
|
|
bool isAnyRemarkEnabled() const override {
|
|
|
|
return (CodeGenOpts.OptimizationRemarkAnalysisPattern ||
|
|
|
|
CodeGenOpts.OptimizationRemarkMissedPattern ||
|
|
|
|
CodeGenOpts.OptimizationRemarkPattern);
|
|
|
|
}
|
|
|
|
|
2017-09-16 04:09:55 +08:00
|
|
|
private:
|
|
|
|
const CodeGenOptions &CodeGenOpts;
|
|
|
|
BackendConsumer *BackendCon;
|
|
|
|
};
|
|
|
|
|
2009-11-28 18:07:24 +08:00
|
|
|
class BackendConsumer : public ASTConsumer {
|
[CodeGen] [CUDA] Add the ability set default attrs on functions in linked modules.
Summary:
Now when you ask clang to link in a bitcode module, you can tell it to
set attributes on that module's functions to match what we would have
set if we'd emitted those functions ourselves.
This is particularly important for fast-math attributes in CUDA
compilations.
Each CUDA compilation links in libdevice, a bitcode library provided by
nvidia as part of the CUDA distribution. Without this patch, if we have
a user-function F that is compiled with -ffast-math that calls a
function G from libdevice, F will have the unsafe-fp-math=true (etc.)
attributes, but G will have no attributes.
Since F calls G, the inliner will merge G's attributes into F's. It
considers the lack of an unsafe-fp-math=true attribute on G to be
tantamount to unsafe-fp-math=false, so it "merges" these by setting
unsafe-fp-math=false on F.
This then continues up the call graph, until every function that
(transitively) calls something in libdevice gets unsafe-fp-math=false
set, thus disabling fastmath in almost all CUDA code.
Reviewers: echristo
Subscribers: hfinkel, llvm-commits, mehdi_amini
Differential Revision: https://reviews.llvm.org/D28538
llvm-svn: 293097
2017-01-26 05:29:48 +08:00
|
|
|
using LinkModule = CodeGenAction::LinkModule;
|
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
virtual void anchor();
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine &Diags;
|
2008-10-22 07:49:24 +08:00
|
|
|
BackendAction Action;
|
2017-01-06 00:02:32 +08:00
|
|
|
const HeaderSearchOptions &HeaderSearchOpts;
|
2009-11-30 16:39:32 +08:00
|
|
|
const CodeGenOptions &CodeGenOpts;
|
|
|
|
const TargetOptions &TargetOpts;
|
2011-07-06 06:02:36 +08:00
|
|
|
const LangOptions &LangOpts;
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> AsmOutStream;
|
2009-03-05 16:00:35 +08:00
|
|
|
ASTContext *Context;
|
2018-04-11 02:53:28 +08:00
|
|
|
|
|
|
|
Timer LLVMIRGeneration;
|
|
|
|
unsigned LLVMIRGenerationRefCount;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2016-11-30 08:25:36 +08:00
|
|
|
/// True if we've finished generating IR. This prevents us from generating
|
|
|
|
/// additional LLVM IR after emitting output in HandleTranslationUnit. This
|
|
|
|
/// can happen when Clang plugins trigger additional AST deserialization.
|
|
|
|
bool IRGenFinished = false;
|
|
|
|
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<CodeGenerator> Gen;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
[CodeGen] [CUDA] Add the ability set default attrs on functions in linked modules.
Summary:
Now when you ask clang to link in a bitcode module, you can tell it to
set attributes on that module's functions to match what we would have
set if we'd emitted those functions ourselves.
This is particularly important for fast-math attributes in CUDA
compilations.
Each CUDA compilation links in libdevice, a bitcode library provided by
nvidia as part of the CUDA distribution. Without this patch, if we have
a user-function F that is compiled with -ffast-math that calls a
function G from libdevice, F will have the unsafe-fp-math=true (etc.)
attributes, but G will have no attributes.
Since F calls G, the inliner will merge G's attributes into F's. It
considers the lack of an unsafe-fp-math=true attribute on G to be
tantamount to unsafe-fp-math=false, so it "merges" these by setting
unsafe-fp-math=false on F.
This then continues up the call graph, until every function that
(transitively) calls something in libdevice gets unsafe-fp-math=false
set, thus disabling fastmath in almost all CUDA code.
Reviewers: echristo
Subscribers: hfinkel, llvm-commits, mehdi_amini
Differential Revision: https://reviews.llvm.org/D28538
llvm-svn: 293097
2017-01-26 05:29:48 +08:00
|
|
|
SmallVector<LinkModule, 4> LinkModules;
|
2008-10-22 07:49:24 +08:00
|
|
|
|
2015-12-15 07:17:07 +08:00
|
|
|
// This is here so that the diagnostic printer knows the module a diagnostic
|
|
|
|
// refers to.
|
|
|
|
llvm::Module *CurLinkModule = nullptr;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
public:
|
[CodeGen] [CUDA] Add the ability set default attrs on functions in linked modules.
Summary:
Now when you ask clang to link in a bitcode module, you can tell it to
set attributes on that module's functions to match what we would have
set if we'd emitted those functions ourselves.
This is particularly important for fast-math attributes in CUDA
compilations.
Each CUDA compilation links in libdevice, a bitcode library provided by
nvidia as part of the CUDA distribution. Without this patch, if we have
a user-function F that is compiled with -ffast-math that calls a
function G from libdevice, F will have the unsafe-fp-math=true (etc.)
attributes, but G will have no attributes.
Since F calls G, the inliner will merge G's attributes into F's. It
considers the lack of an unsafe-fp-math=true attribute on G to be
tantamount to unsafe-fp-math=false, so it "merges" these by setting
unsafe-fp-math=false on F.
This then continues up the call graph, until every function that
(transitively) calls something in libdevice gets unsafe-fp-math=false
set, thus disabling fastmath in almost all CUDA code.
Reviewers: echristo
Subscribers: hfinkel, llvm-commits, mehdi_amini
Differential Revision: https://reviews.llvm.org/D28538
llvm-svn: 293097
2017-01-26 05:29:48 +08:00
|
|
|
BackendConsumer(BackendAction Action, DiagnosticsEngine &Diags,
|
|
|
|
const HeaderSearchOptions &HeaderSearchOpts,
|
|
|
|
const PreprocessorOptions &PPOpts,
|
|
|
|
const CodeGenOptions &CodeGenOpts,
|
|
|
|
const TargetOptions &TargetOpts,
|
|
|
|
const LangOptions &LangOpts, bool TimePasses,
|
|
|
|
const std::string &InFile,
|
|
|
|
SmallVector<LinkModule, 4> LinkModules,
|
|
|
|
std::unique_ptr<raw_pwrite_stream> OS, LLVMContext &C,
|
|
|
|
CoverageSourceInfo *CoverageInfo = nullptr)
|
2017-01-06 00:02:32 +08:00
|
|
|
: Diags(Diags), Action(Action), HeaderSearchOpts(HeaderSearchOpts),
|
|
|
|
CodeGenOpts(CodeGenOpts), TargetOpts(TargetOpts), LangOpts(LangOpts),
|
2016-07-15 08:55:40 +08:00
|
|
|
AsmOutStream(std::move(OS)), Context(nullptr),
|
2018-04-11 02:53:28 +08:00
|
|
|
LLVMIRGeneration("irgen", "LLVM IR Generation Time"),
|
|
|
|
LLVMIRGenerationRefCount(0),
|
2015-06-30 10:26:03 +08:00
|
|
|
Gen(CreateLLVMCodeGen(Diags, InFile, HeaderSearchOpts, PPOpts,
|
[CodeGen] [CUDA] Add the ability set default attrs on functions in linked modules.
Summary:
Now when you ask clang to link in a bitcode module, you can tell it to
set attributes on that module's functions to match what we would have
set if we'd emitted those functions ourselves.
This is particularly important for fast-math attributes in CUDA
compilations.
Each CUDA compilation links in libdevice, a bitcode library provided by
nvidia as part of the CUDA distribution. Without this patch, if we have
a user-function F that is compiled with -ffast-math that calls a
function G from libdevice, F will have the unsafe-fp-math=true (etc.)
attributes, but G will have no attributes.
Since F calls G, the inliner will merge G's attributes into F's. It
considers the lack of an unsafe-fp-math=true attribute on G to be
tantamount to unsafe-fp-math=false, so it "merges" these by setting
unsafe-fp-math=false on F.
This then continues up the call graph, until every function that
(transitively) calls something in libdevice gets unsafe-fp-math=false
set, thus disabling fastmath in almost all CUDA code.
Reviewers: echristo
Subscribers: hfinkel, llvm-commits, mehdi_amini
Differential Revision: https://reviews.llvm.org/D28538
llvm-svn: 293097
2017-01-26 05:29:48 +08:00
|
|
|
CodeGenOpts, C, CoverageInfo)),
|
|
|
|
LinkModules(std::move(LinkModules)) {
|
2018-04-23 17:22:30 +08:00
|
|
|
FrontendTimesIsEnabled = TimePasses;
|
2009-02-18 09:23:44 +08:00
|
|
|
}
|
2016-02-19 00:42:09 +08:00
|
|
|
llvm::Module *getModule() const { return Gen->GetModule(); }
|
|
|
|
std::unique_ptr<llvm::Module> takeModule() {
|
|
|
|
return std::unique_ptr<llvm::Module>(Gen->ReleaseModule());
|
|
|
|
}
|
2010-02-25 12:37:50 +08:00
|
|
|
|
2017-02-10 06:07:24 +08:00
|
|
|
CodeGenerator *getCodeGenerator() { return Gen.get(); }
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override {
|
2012-03-08 23:51:03 +08:00
|
|
|
Gen->HandleCXXStaticMemberVarInstantiation(VD);
|
2012-03-05 18:54:55 +08:00
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void Initialize(ASTContext &Ctx) override {
|
2015-08-19 04:39:29 +08:00
|
|
|
assert(!Context && "initialized multiple times");
|
2018-04-11 02:53:28 +08:00
|
|
|
|
2009-03-28 10:18:25 +08:00
|
|
|
Context = &Ctx;
|
2018-04-11 02:53:28 +08:00
|
|
|
|
2018-04-23 17:22:30 +08:00
|
|
|
if (FrontendTimesIsEnabled)
|
2018-04-11 02:53:28 +08:00
|
|
|
LLVMIRGeneration.startTimer();
|
|
|
|
|
2009-03-28 10:18:25 +08:00
|
|
|
Gen->Initialize(Ctx);
|
2018-04-11 02:53:28 +08:00
|
|
|
|
2018-04-23 17:22:30 +08:00
|
|
|
if (FrontendTimesIsEnabled)
|
2018-04-11 02:53:28 +08:00
|
|
|
LLVMIRGeneration.stopTimer();
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
bool HandleTopLevelDecl(DeclGroupRef D) override {
|
2009-03-30 00:50:03 +08:00
|
|
|
PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
|
2009-03-05 16:00:35 +08:00
|
|
|
Context->getSourceManager(),
|
|
|
|
"LLVM IR generation of declaration");
|
2018-04-11 02:53:28 +08:00
|
|
|
|
|
|
|
// Recurse.
|
2018-04-23 17:22:30 +08:00
|
|
|
if (FrontendTimesIsEnabled) {
|
2018-04-11 02:53:28 +08:00
|
|
|
LLVMIRGenerationRefCount += 1;
|
|
|
|
if (LLVMIRGenerationRefCount == 1)
|
|
|
|
LLVMIRGeneration.startTimer();
|
|
|
|
}
|
2009-03-30 00:50:03 +08:00
|
|
|
|
2008-10-22 07:49:24 +08:00
|
|
|
Gen->HandleTopLevelDecl(D);
|
2018-04-11 02:53:28 +08:00
|
|
|
|
2018-04-23 17:22:30 +08:00
|
|
|
if (FrontendTimesIsEnabled) {
|
2018-04-11 02:53:28 +08:00
|
|
|
LLVMIRGenerationRefCount -= 1;
|
|
|
|
if (LLVMIRGenerationRefCount == 0)
|
|
|
|
LLVMIRGeneration.stopTimer();
|
|
|
|
}
|
|
|
|
|
2011-11-18 08:26:59 +08:00
|
|
|
return true;
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2016-03-30 14:27:31 +08:00
|
|
|
void HandleInlineFunctionDefinition(FunctionDecl *D) override {
|
2014-05-24 04:37:38 +08:00
|
|
|
PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
|
|
|
|
Context->getSourceManager(),
|
2016-03-30 14:27:31 +08:00
|
|
|
"LLVM IR generation of inline function");
|
2018-04-23 17:22:30 +08:00
|
|
|
if (FrontendTimesIsEnabled)
|
2018-04-11 02:53:28 +08:00
|
|
|
LLVMIRGeneration.startTimer();
|
2014-05-24 04:37:38 +08:00
|
|
|
|
2016-03-30 14:27:31 +08:00
|
|
|
Gen->HandleInlineFunctionDefinition(D);
|
2018-04-11 02:53:28 +08:00
|
|
|
|
2018-04-23 17:22:30 +08:00
|
|
|
if (FrontendTimesIsEnabled)
|
2018-04-11 02:53:28 +08:00
|
|
|
LLVMIRGeneration.stopTimer();
|
2014-05-24 04:37:38 +08:00
|
|
|
}
|
|
|
|
|
2016-11-30 09:32:53 +08:00
|
|
|
void HandleInterestingDecl(DeclGroupRef D) override {
|
2016-11-30 08:25:36 +08:00
|
|
|
// Ignore interesting decls from the AST reader after IRGen is finished.
|
|
|
|
if (!IRGenFinished)
|
|
|
|
HandleTopLevelDecl(D);
|
|
|
|
}
|
|
|
|
|
[CodeGen] [CUDA] Add the ability set default attrs on functions in linked modules.
Summary:
Now when you ask clang to link in a bitcode module, you can tell it to
set attributes on that module's functions to match what we would have
set if we'd emitted those functions ourselves.
This is particularly important for fast-math attributes in CUDA
compilations.
Each CUDA compilation links in libdevice, a bitcode library provided by
nvidia as part of the CUDA distribution. Without this patch, if we have
a user-function F that is compiled with -ffast-math that calls a
function G from libdevice, F will have the unsafe-fp-math=true (etc.)
attributes, but G will have no attributes.
Since F calls G, the inliner will merge G's attributes into F's. It
considers the lack of an unsafe-fp-math=true attribute on G to be
tantamount to unsafe-fp-math=false, so it "merges" these by setting
unsafe-fp-math=false on F.
This then continues up the call graph, until every function that
(transitively) calls something in libdevice gets unsafe-fp-math=false
set, thus disabling fastmath in almost all CUDA code.
Reviewers: echristo
Subscribers: hfinkel, llvm-commits, mehdi_amini
Differential Revision: https://reviews.llvm.org/D28538
llvm-svn: 293097
2017-01-26 05:29:48 +08:00
|
|
|
// Links each entry in LinkModules into our module. Returns true on error.
|
|
|
|
bool LinkInModules() {
|
|
|
|
for (auto &LM : LinkModules) {
|
|
|
|
if (LM.PropagateAttrs)
|
|
|
|
for (Function &F : *LM.Module)
|
|
|
|
Gen->CGM().AddDefaultFnAttrs(F);
|
|
|
|
|
|
|
|
CurLinkModule = LM.Module.get();
|
2017-03-14 02:08:11 +08:00
|
|
|
|
|
|
|
bool Err;
|
|
|
|
if (LM.Internalize) {
|
|
|
|
Err = Linker::linkModules(
|
|
|
|
*getModule(), std::move(LM.Module), LM.LinkFlags,
|
|
|
|
[](llvm::Module &M, const llvm::StringSet<> &GVS) {
|
2017-03-14 06:33:07 +08:00
|
|
|
internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) {
|
2017-03-14 02:08:11 +08:00
|
|
|
return !GV.hasName() || (GVS.count(GV.getName()) == 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Err = Linker::linkModules(*getModule(), std::move(LM.Module),
|
|
|
|
LM.LinkFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Err)
|
[CodeGen] [CUDA] Add the ability set default attrs on functions in linked modules.
Summary:
Now when you ask clang to link in a bitcode module, you can tell it to
set attributes on that module's functions to match what we would have
set if we'd emitted those functions ourselves.
This is particularly important for fast-math attributes in CUDA
compilations.
Each CUDA compilation links in libdevice, a bitcode library provided by
nvidia as part of the CUDA distribution. Without this patch, if we have
a user-function F that is compiled with -ffast-math that calls a
function G from libdevice, F will have the unsafe-fp-math=true (etc.)
attributes, but G will have no attributes.
Since F calls G, the inliner will merge G's attributes into F's. It
considers the lack of an unsafe-fp-math=true attribute on G to be
tantamount to unsafe-fp-math=false, so it "merges" these by setting
unsafe-fp-math=false on F.
This then continues up the call graph, until every function that
(transitively) calls something in libdevice gets unsafe-fp-math=false
set, thus disabling fastmath in almost all CUDA code.
Reviewers: echristo
Subscribers: hfinkel, llvm-commits, mehdi_amini
Differential Revision: https://reviews.llvm.org/D28538
llvm-svn: 293097
2017-01-26 05:29:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false; // success
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void HandleTranslationUnit(ASTContext &C) override {
|
2009-03-05 16:00:35 +08:00
|
|
|
{
|
2009-03-06 14:46:31 +08:00
|
|
|
PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
|
2018-04-23 17:22:30 +08:00
|
|
|
if (FrontendTimesIsEnabled) {
|
2018-04-11 02:53:28 +08:00
|
|
|
LLVMIRGenerationRefCount += 1;
|
|
|
|
if (LLVMIRGenerationRefCount == 1)
|
|
|
|
LLVMIRGeneration.startTimer();
|
|
|
|
}
|
2009-02-18 09:37:30 +08:00
|
|
|
|
2009-03-28 12:11:33 +08:00
|
|
|
Gen->HandleTranslationUnit(C);
|
2018-04-11 02:53:28 +08:00
|
|
|
|
2018-04-23 17:22:30 +08:00
|
|
|
if (FrontendTimesIsEnabled) {
|
2018-04-11 02:53:28 +08:00
|
|
|
LLVMIRGenerationRefCount -= 1;
|
|
|
|
if (LLVMIRGenerationRefCount == 0)
|
|
|
|
LLVMIRGeneration.stopTimer();
|
|
|
|
}
|
|
|
|
|
2018-07-20 16:19:20 +08:00
|
|
|
IRGenFinished = true;
|
2009-03-05 16:00:35 +08:00
|
|
|
}
|
2009-02-18 09:37:30 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
// Silently ignore if we weren't initialized for some reason.
|
2016-02-19 00:42:09 +08:00
|
|
|
if (!getModule())
|
2010-06-08 07:20:08 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Install an inline asm handler so that diagnostics get printed through
|
|
|
|
// our diagnostics hooks.
|
2016-02-19 00:42:09 +08:00
|
|
|
LLVMContext &Ctx = getModule()->getContext();
|
2010-11-17 16:13:04 +08:00
|
|
|
LLVMContext::InlineAsmDiagHandlerTy OldHandler =
|
|
|
|
Ctx.getInlineAsmDiagnosticHandler();
|
2010-06-08 07:20:08 +08:00
|
|
|
void *OldContext = Ctx.getInlineAsmDiagnosticContext();
|
2010-11-17 16:13:04 +08:00
|
|
|
Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
|
2010-06-08 07:20:08 +08:00
|
|
|
|
2017-09-16 04:09:55 +08:00
|
|
|
std::unique_ptr<DiagnosticHandler> OldDiagnosticHandler =
|
2014-02-07 02:30:43 +08:00
|
|
|
Ctx.getDiagnosticHandler();
|
2017-09-16 04:09:55 +08:00
|
|
|
Ctx.setDiagnosticHandler(llvm::make_unique<ClangDiagnosticHandler>(
|
|
|
|
CodeGenOpts, this));
|
2017-07-01 03:37:11 +08:00
|
|
|
Ctx.setDiagnosticsHotnessRequested(CodeGenOpts.DiagnosticsWithHotness);
|
2017-07-01 13:45:26 +08:00
|
|
|
if (CodeGenOpts.DiagnosticsHotnessThreshold != 0)
|
|
|
|
Ctx.setDiagnosticsHotnessThreshold(
|
|
|
|
CodeGenOpts.DiagnosticsHotnessThreshold);
|
2014-02-07 02:30:43 +08:00
|
|
|
|
2017-09-23 09:03:17 +08:00
|
|
|
std::unique_ptr<llvm::ToolOutputFile> OptRecordFile;
|
2016-10-11 08:26:09 +08:00
|
|
|
if (!CodeGenOpts.OptRecordFile.empty()) {
|
|
|
|
std::error_code EC;
|
2017-09-23 09:03:17 +08:00
|
|
|
OptRecordFile = llvm::make_unique<llvm::ToolOutputFile>(
|
|
|
|
CodeGenOpts.OptRecordFile, EC, sys::fs::F_None);
|
2016-10-11 08:26:09 +08:00
|
|
|
if (EC) {
|
|
|
|
Diags.Report(diag::err_cannot_open_file) <<
|
|
|
|
CodeGenOpts.OptRecordFile << EC.message();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-20 02:19:41 +08:00
|
|
|
Ctx.setDiagnosticsOutputFile(
|
|
|
|
llvm::make_unique<yaml::Output>(OptRecordFile->os()));
|
2016-10-11 08:26:09 +08:00
|
|
|
|
|
|
|
if (CodeGenOpts.getProfileUse() != CodeGenOptions::ProfileNone)
|
2017-07-01 03:37:11 +08:00
|
|
|
Ctx.setDiagnosticsHotnessRequested(true);
|
2016-10-11 08:26:09 +08:00
|
|
|
}
|
|
|
|
|
[CodeGen] [CUDA] Add the ability set default attrs on functions in linked modules.
Summary:
Now when you ask clang to link in a bitcode module, you can tell it to
set attributes on that module's functions to match what we would have
set if we'd emitted those functions ourselves.
This is particularly important for fast-math attributes in CUDA
compilations.
Each CUDA compilation links in libdevice, a bitcode library provided by
nvidia as part of the CUDA distribution. Without this patch, if we have
a user-function F that is compiled with -ffast-math that calls a
function G from libdevice, F will have the unsafe-fp-math=true (etc.)
attributes, but G will have no attributes.
Since F calls G, the inliner will merge G's attributes into F's. It
considers the lack of an unsafe-fp-math=true attribute on G to be
tantamount to unsafe-fp-math=false, so it "merges" these by setting
unsafe-fp-math=false on F.
This then continues up the call graph, until every function that
(transitively) calls something in libdevice gets unsafe-fp-math=false
set, thus disabling fastmath in almost all CUDA code.
Reviewers: echristo
Subscribers: hfinkel, llvm-commits, mehdi_amini
Differential Revision: https://reviews.llvm.org/D28538
llvm-svn: 293097
2017-01-26 05:29:48 +08:00
|
|
|
// Link each LinkModule into our module.
|
|
|
|
if (LinkInModules())
|
|
|
|
return;
|
2015-12-15 07:17:07 +08:00
|
|
|
|
2016-05-12 00:26:03 +08:00
|
|
|
EmbedBitcode(getModule(), CodeGenOpts, llvm::MemoryBufferRef());
|
|
|
|
|
2017-01-06 00:02:32 +08:00
|
|
|
EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts,
|
|
|
|
LangOpts, C.getTargetInfo().getDataLayout(),
|
2016-07-15 08:55:40 +08:00
|
|
|
getModule(), Action, std::move(AsmOutStream));
|
2013-12-19 00:38:48 +08:00
|
|
|
|
2010-06-08 07:20:08 +08:00
|
|
|
Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
|
2014-02-07 02:30:43 +08:00
|
|
|
|
2017-09-16 04:09:55 +08:00
|
|
|
Ctx.setDiagnosticHandler(std::move(OldDiagnosticHandler));
|
2016-10-11 08:26:09 +08:00
|
|
|
|
|
|
|
if (OptRecordFile)
|
|
|
|
OptRecordFile->keep();
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void HandleTagDeclDefinition(TagDecl *D) override {
|
2009-03-05 16:00:35 +08:00
|
|
|
PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
|
|
|
|
Context->getSourceManager(),
|
|
|
|
"LLVM IR generation of declaration");
|
2008-10-22 07:49:24 +08:00
|
|
|
Gen->HandleTagDeclDefinition(D);
|
|
|
|
}
|
2009-04-22 01:11:58 +08:00
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
|
2013-07-14 05:08:14 +08:00
|
|
|
Gen->HandleTagDeclRequiredDefinition(D);
|
|
|
|
}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void CompleteTentativeDefinition(VarDecl *D) override {
|
2009-04-22 01:11:58 +08:00
|
|
|
Gen->CompleteTentativeDefinition(D);
|
|
|
|
}
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2016-01-27 03:30:26 +08:00
|
|
|
void AssignInheritanceModel(CXXRecordDecl *RD) override {
|
|
|
|
Gen->AssignInheritanceModel(RD);
|
|
|
|
}
|
|
|
|
|
2015-01-15 12:07:35 +08:00
|
|
|
void HandleVTable(CXXRecordDecl *RD) override {
|
|
|
|
Gen->HandleVTable(RD);
|
Rework when and how vtables are emitted, by tracking where vtables are
"used" (e.g., we will refer to the vtable in the generated code) and
when they are defined (i.e., because we've seen the key function
definition). Previously, we were effectively tracking "potential
definitions" rather than uses, so we were a bit too eager about emitting
vtables for classes without key functions.
The new scheme:
- For every use of a vtable, Sema calls MarkVTableUsed() to indicate
the use. For example, this occurs when calling a virtual member
function of the class, defining a constructor of that class type,
dynamic_cast'ing from that type to a derived class, casting
to/through a virtual base class, etc.
- For every definition of a vtable, Sema calls MarkVTableUsed() to
indicate the definition. This happens at the end of the translation
unit for classes whose key function has been defined (so we can
delay computation of the key function; see PR6564), and will also
occur with explicit template instantiation definitions.
- For every vtable defined/used, we mark all of the virtual member
functions of that vtable as defined/used, unless we know that the key
function is in another translation unit. This instantiates virtual
member functions when needed.
- At the end of the translation unit, Sema tells CodeGen (via the
ASTConsumer) which vtables must be defined (CodeGen will define
them) and which may be used (for which CodeGen will define the
vtables lazily).
From a language perspective, both the old and the new schemes are
permissible: we're allowed to instantiate virtual member functions
whenever we want per the standard. However, all other C++ compilers
were more lazy than we were, and our eagerness was both a performance
issue (we instantiated too much) and a portability problem (we broke
Boost test cases, which now pass).
Notes:
(1) There's a ton of churn in the tests, because the order in which
vtables get emitted to IR has changed. I've tried to isolate some of
the larger tests from these issues.
(2) Some diagnostics related to
implicitly-instantiated/implicitly-defined virtual member functions
have moved to the point of first use/definition. It's better this
way.
(3) I could use a review of the places where we MarkVTableUsed, to
see if I missed any place where the language effectively requires a
vtable.
Fixes PR7114 and PR6564.
llvm-svn: 103718
2010-05-14 00:44:06 +08:00
|
|
|
}
|
|
|
|
|
2010-04-07 02:38:50 +08:00
|
|
|
static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
|
|
|
|
unsigned LocCookie) {
|
|
|
|
SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie);
|
|
|
|
((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
|
|
|
|
}
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2016-02-02 21:52:52 +08:00
|
|
|
/// Get the best possible source location to represent a diagnostic that
|
|
|
|
/// may have associated debug info.
|
|
|
|
const FullSourceLoc
|
2017-02-18 01:34:49 +08:00
|
|
|
getBestLocationFromDebugLoc(const llvm::DiagnosticInfoWithLocationBase &D,
|
2016-02-02 21:52:52 +08:00
|
|
|
bool &BadDebugInfo, StringRef &Filename,
|
|
|
|
unsigned &Line, unsigned &Column) const;
|
|
|
|
|
2010-04-07 02:38:50 +08:00
|
|
|
void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
|
|
|
|
SourceLocation LocCookie);
|
2014-02-07 02:30:43 +08:00
|
|
|
|
|
|
|
void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI);
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Specialized handler for InlineAsm diagnostic.
|
2014-02-07 02:30:43 +08:00
|
|
|
/// \return True if the diagnostic has been successfully reported, false
|
|
|
|
/// otherwise.
|
|
|
|
bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D);
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Specialized handler for StackSize diagnostic.
|
2014-02-07 02:30:43 +08:00
|
|
|
/// \return True if the diagnostic has been successfully reported, false
|
|
|
|
/// otherwise.
|
|
|
|
bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Specialized handler for unsupported backend feature diagnostic.
|
2016-02-02 21:52:52 +08:00
|
|
|
void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &D);
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Specialized handlers for optimization remarks.
|
2014-05-30 03:55:06 +08:00
|
|
|
/// Note that these handlers only accept remarks and they always handle
|
2014-04-17 00:54:24 +08:00
|
|
|
/// them.
|
2014-07-19 03:40:19 +08:00
|
|
|
void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D,
|
|
|
|
unsigned DiagID);
|
2017-01-26 12:07:11 +08:00
|
|
|
void
|
|
|
|
OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationBase &D);
|
2014-05-30 03:55:06 +08:00
|
|
|
void OptimizationRemarkHandler(
|
2016-09-28 06:19:29 +08:00
|
|
|
const llvm::OptimizationRemarkAnalysisFPCommute &D);
|
2014-05-30 03:55:06 +08:00
|
|
|
void OptimizationRemarkHandler(
|
2016-09-28 06:19:29 +08:00
|
|
|
const llvm::OptimizationRemarkAnalysisAliasing &D);
|
2014-07-19 03:40:19 +08:00
|
|
|
void OptimizationFailureHandler(
|
|
|
|
const llvm::DiagnosticInfoOptimizationFailure &D);
|
2009-09-09 23:08:12 +08:00
|
|
|
};
|
2017-03-14 02:08:11 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void BackendConsumer::anchor() {}
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2008-10-22 07:49:24 +08:00
|
|
|
|
2017-09-16 04:09:55 +08:00
|
|
|
bool ClangDiagnosticHandler::handleDiagnostics(const DiagnosticInfo &DI) {
|
|
|
|
BackendCon->DiagnosticHandlerImpl(DI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-04-08 08:23:06 +08:00
|
|
|
/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
|
|
|
|
/// buffer to be a valid FullSourceLoc.
|
|
|
|
static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
|
|
|
|
SourceManager &CSM) {
|
|
|
|
// Get both the clang and llvm source managers. The location is relative to
|
|
|
|
// a memory buffer that the LLVM Source Manager is handling, we need to add
|
2010-04-30 00:29:09 +08:00
|
|
|
// a copy to the Clang source manager.
|
2010-04-08 08:23:06 +08:00
|
|
|
const llvm::SourceMgr &LSM = *D.getSourceMgr();
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2010-04-08 08:23:06 +08:00
|
|
|
// We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
|
|
|
|
// already owns its one and clang::SourceManager wants to own its one.
|
|
|
|
const MemoryBuffer *LBuf =
|
|
|
|
LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2010-04-08 08:23:06 +08:00
|
|
|
// Create the copy and transfer ownership to clang::SourceManager.
|
2014-06-27 14:02:00 +08:00
|
|
|
// TODO: Avoid copying files into memory.
|
2014-08-28 04:03:29 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> CBuf =
|
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
|
|
|
|
LBuf->getBufferIdentifier());
|
2014-06-27 14:02:00 +08:00
|
|
|
// FIXME: Keep a file ID map instead of creating new IDs for each location.
|
2014-08-29 15:59:55 +08:00
|
|
|
FileID FID = CSM.createFileID(std::move(CBuf));
|
2010-04-30 00:29:09 +08:00
|
|
|
|
2010-04-08 08:23:06 +08:00
|
|
|
// Translate the offset into the file.
|
2014-06-27 14:02:00 +08:00
|
|
|
unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
|
2010-04-30 00:29:09 +08:00
|
|
|
SourceLocation NewLoc =
|
2011-09-20 04:40:19 +08:00
|
|
|
CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
|
2010-04-08 08:23:06 +08:00
|
|
|
return FullSourceLoc(NewLoc, CSM);
|
|
|
|
}
|
|
|
|
|
2010-04-07 01:52:14 +08:00
|
|
|
|
2010-04-07 02:38:50 +08:00
|
|
|
/// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
|
|
|
|
/// error parsing inline asm. The SMDiagnostic indicates the error relative to
|
2010-04-30 00:29:09 +08:00
|
|
|
/// the temporary memory buffer that the inline asm parser has set up.
|
2010-04-07 02:38:50 +08:00
|
|
|
void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
|
|
|
|
SourceLocation LocCookie) {
|
|
|
|
// There are a couple of different kinds of errors we could get here. First,
|
|
|
|
// we re-format the SMDiagnostic in terms of a clang diagnostic.
|
2012-02-01 14:36:49 +08:00
|
|
|
|
|
|
|
// Strip "error: " off the start of the message string.
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Message = D.getMessage();
|
2012-02-01 14:36:49 +08:00
|
|
|
if (Message.startswith("error: "))
|
|
|
|
Message = Message.substr(7);
|
2010-04-07 02:38:50 +08:00
|
|
|
|
2010-06-15 08:03:12 +08:00
|
|
|
// If the SMDiagnostic has an inline asm source location, translate it.
|
2010-04-07 02:38:50 +08:00
|
|
|
FullSourceLoc Loc;
|
2010-04-08 08:23:06 +08:00
|
|
|
if (D.getLoc() != SMLoc())
|
|
|
|
Loc = ConvertBackendLocation(D, Context->getSourceManager());
|
2012-02-01 14:36:49 +08:00
|
|
|
|
2014-06-06 05:23:42 +08:00
|
|
|
unsigned DiagID;
|
|
|
|
switch (D.getKind()) {
|
|
|
|
case llvm::SourceMgr::DK_Error:
|
|
|
|
DiagID = diag::err_fe_inline_asm;
|
|
|
|
break;
|
|
|
|
case llvm::SourceMgr::DK_Warning:
|
|
|
|
DiagID = diag::warn_fe_inline_asm;
|
|
|
|
break;
|
|
|
|
case llvm::SourceMgr::DK_Note:
|
|
|
|
DiagID = diag::note_fe_inline_asm;
|
|
|
|
break;
|
2017-10-13 07:56:54 +08:00
|
|
|
case llvm::SourceMgr::DK_Remark:
|
|
|
|
llvm_unreachable("remarks unexpected");
|
2014-06-06 05:23:42 +08:00
|
|
|
}
|
2010-06-15 08:03:12 +08:00
|
|
|
// If this problem has clang-level source location information, report the
|
2014-06-06 05:23:42 +08:00
|
|
|
// issue in the source with a note showing the instantiated
|
2010-06-15 08:03:12 +08:00
|
|
|
// code.
|
|
|
|
if (LocCookie.isValid()) {
|
2014-06-06 05:23:42 +08:00
|
|
|
Diags.Report(LocCookie, DiagID).AddString(Message);
|
2017-03-14 02:08:11 +08:00
|
|
|
|
2011-10-16 18:48:28 +08:00
|
|
|
if (D.getLoc().isValid()) {
|
|
|
|
DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
|
|
|
|
// Convert the SMDiagnostic ranges into SourceRange and attach them
|
|
|
|
// to the diagnostic.
|
2015-08-02 03:11:36 +08:00
|
|
|
for (const std::pair<unsigned, unsigned> &Range : D.getRanges()) {
|
2011-10-16 18:48:28 +08:00
|
|
|
unsigned Column = D.getColumnNo();
|
|
|
|
B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
|
|
|
|
Loc.getLocWithOffset(Range.second - Column));
|
|
|
|
}
|
|
|
|
}
|
2010-06-15 08:03:12 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-03-14 02:08:11 +08:00
|
|
|
|
2014-06-06 05:23:42 +08:00
|
|
|
// Otherwise, report the backend issue as occurring in the generated .s file.
|
|
|
|
// If Loc is invalid, we still need to report the issue, it just gets no
|
2010-06-15 08:03:12 +08:00
|
|
|
// location info.
|
2014-06-06 05:23:42 +08:00
|
|
|
Diags.Report(Loc, DiagID).AddString(Message);
|
2010-04-07 02:38:50 +08:00
|
|
|
}
|
|
|
|
|
2014-02-07 02:30:43 +08:00
|
|
|
#define ComputeDiagID(Severity, GroupName, DiagID) \
|
|
|
|
do { \
|
|
|
|
switch (Severity) { \
|
|
|
|
case llvm::DS_Error: \
|
|
|
|
DiagID = diag::err_fe_##GroupName; \
|
|
|
|
break; \
|
|
|
|
case llvm::DS_Warning: \
|
|
|
|
DiagID = diag::warn_fe_##GroupName; \
|
|
|
|
break; \
|
2014-02-28 17:11:08 +08:00
|
|
|
case llvm::DS_Remark: \
|
|
|
|
llvm_unreachable("'remark' severity not expected"); \
|
|
|
|
break; \
|
|
|
|
case llvm::DS_Note: \
|
|
|
|
DiagID = diag::note_fe_##GroupName; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define ComputeDiagRemarkID(Severity, GroupName, DiagID) \
|
|
|
|
do { \
|
|
|
|
switch (Severity) { \
|
|
|
|
case llvm::DS_Error: \
|
|
|
|
DiagID = diag::err_fe_##GroupName; \
|
|
|
|
break; \
|
|
|
|
case llvm::DS_Warning: \
|
|
|
|
DiagID = diag::warn_fe_##GroupName; \
|
|
|
|
break; \
|
|
|
|
case llvm::DS_Remark: \
|
|
|
|
DiagID = diag::remark_fe_##GroupName; \
|
|
|
|
break; \
|
2014-02-07 02:30:43 +08:00
|
|
|
case llvm::DS_Note: \
|
|
|
|
DiagID = diag::note_fe_##GroupName; \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
bool
|
|
|
|
BackendConsumer::InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D) {
|
|
|
|
unsigned DiagID;
|
|
|
|
ComputeDiagID(D.getSeverity(), inline_asm, DiagID);
|
|
|
|
std::string Message = D.getMsgStr().str();
|
|
|
|
|
|
|
|
// If this problem has clang-level source location information, report the
|
2014-02-26 18:21:56 +08:00
|
|
|
// issue as being a problem in the source with a note showing the instantiated
|
2014-02-07 02:30:43 +08:00
|
|
|
// code.
|
|
|
|
SourceLocation LocCookie =
|
|
|
|
SourceLocation::getFromRawEncoding(D.getLocCookie());
|
|
|
|
if (LocCookie.isValid())
|
|
|
|
Diags.Report(LocCookie, DiagID).AddString(Message);
|
|
|
|
else {
|
|
|
|
// Otherwise, report the backend diagnostic as occurring in the generated
|
|
|
|
// .s file.
|
|
|
|
// If Loc is invalid, we still need to report the diagnostic, it just gets
|
|
|
|
// no location info.
|
|
|
|
FullSourceLoc Loc;
|
|
|
|
Diags.Report(Loc, DiagID).AddString(Message);
|
|
|
|
}
|
|
|
|
// We handled all the possible severities.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BackendConsumer::StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D) {
|
|
|
|
if (D.getSeverity() != llvm::DS_Warning)
|
|
|
|
// For now, the only support we have for StackSize diagnostic is warning.
|
|
|
|
// We do not know how to format other severities.
|
|
|
|
return false;
|
|
|
|
|
2014-06-06 06:10:59 +08:00
|
|
|
if (const Decl *ND = Gen->GetDeclForMangledName(D.getFunction().getName())) {
|
2016-06-21 02:13:09 +08:00
|
|
|
// FIXME: Shouldn't need to truncate to uint32_t
|
2014-06-06 06:10:59 +08:00
|
|
|
Diags.Report(ND->getASTContext().getFullLoc(ND->getLocation()),
|
|
|
|
diag::warn_fe_frame_larger_than)
|
2016-06-21 02:13:09 +08:00
|
|
|
<< static_cast<uint32_t>(D.getStackSize()) << Decl::castToDeclContext(ND);
|
2014-06-06 06:10:59 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-02-07 02:30:43 +08:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:52:52 +08:00
|
|
|
const FullSourceLoc BackendConsumer::getBestLocationFromDebugLoc(
|
2017-02-18 01:34:49 +08:00
|
|
|
const llvm::DiagnosticInfoWithLocationBase &D, bool &BadDebugInfo,
|
|
|
|
StringRef &Filename, unsigned &Line, unsigned &Column) const {
|
2014-05-30 03:55:06 +08:00
|
|
|
SourceManager &SourceMgr = Context->getSourceManager();
|
|
|
|
FileManager &FileMgr = SourceMgr.getFileManager();
|
2014-06-06 06:11:12 +08:00
|
|
|
SourceLocation DILoc;
|
2015-05-09 04:59:56 +08:00
|
|
|
|
|
|
|
if (D.isLocationAvailable()) {
|
|
|
|
D.getLocation(&Filename, &Line, &Column);
|
|
|
|
const FileEntry *FE = FileMgr.getFile(Filename);
|
|
|
|
if (FE && Line > 0) {
|
|
|
|
// If -gcolumn-info was not used, Column will be 0. This upsets the
|
|
|
|
// source manager, so pass 1 if Column is not set.
|
|
|
|
DILoc = SourceMgr.translateFileLineCol(FE, Line, Column ? Column : 1);
|
|
|
|
}
|
2016-02-02 21:52:52 +08:00
|
|
|
BadDebugInfo = DILoc.isInvalid();
|
2014-04-17 00:54:24 +08:00
|
|
|
}
|
2014-06-06 06:11:12 +08:00
|
|
|
|
|
|
|
// If a location isn't available, try to approximate it using the associated
|
|
|
|
// function definition. We use the definition's right brace to differentiate
|
|
|
|
// from diagnostics that genuinely relate to the function itself.
|
|
|
|
FullSourceLoc Loc(DILoc, SourceMgr);
|
|
|
|
if (Loc.isInvalid())
|
|
|
|
if (const Decl *FD = Gen->GetDeclForMangledName(D.getFunction().getName()))
|
2016-02-02 21:52:52 +08:00
|
|
|
Loc = FD->getASTContext().getFullLoc(FD->getLocation());
|
|
|
|
|
|
|
|
if (DILoc.isInvalid() && D.isLocationAvailable())
|
|
|
|
// If we were not able to translate the file:line:col information
|
|
|
|
// back to a SourceLocation, at least emit a note stating that
|
|
|
|
// we could not translate this location. This can happen in the
|
|
|
|
// case of #line directives.
|
|
|
|
Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
|
2016-05-06 21:29:46 +08:00
|
|
|
<< Filename << Line << Column;
|
2016-02-02 21:52:52 +08:00
|
|
|
|
|
|
|
return Loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackendConsumer::UnsupportedDiagHandler(
|
|
|
|
const llvm::DiagnosticInfoUnsupported &D) {
|
|
|
|
// We only support errors.
|
|
|
|
assert(D.getSeverity() == llvm::DS_Error);
|
|
|
|
|
|
|
|
StringRef Filename;
|
|
|
|
unsigned Line, Column;
|
2017-05-12 08:10:49 +08:00
|
|
|
bool BadDebugInfo = false;
|
|
|
|
FullSourceLoc Loc =
|
|
|
|
getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column);
|
2016-02-02 21:52:52 +08:00
|
|
|
|
2017-10-12 07:54:34 +08:00
|
|
|
Diags.Report(Loc, diag::err_fe_backend_unsupported) << D.getMessage().str();
|
2016-02-02 21:52:52 +08:00
|
|
|
|
|
|
|
if (BadDebugInfo)
|
|
|
|
// If we were not able to translate the file:line:col information
|
|
|
|
// back to a SourceLocation, at least emit a note stating that
|
|
|
|
// we could not translate this location. This can happen in the
|
|
|
|
// case of #line directives.
|
|
|
|
Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
|
|
|
|
<< Filename << Line << Column;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackendConsumer::EmitOptimizationMessage(
|
|
|
|
const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID) {
|
|
|
|
// We only support warnings and remarks.
|
|
|
|
assert(D.getSeverity() == llvm::DS_Remark ||
|
|
|
|
D.getSeverity() == llvm::DS_Warning);
|
|
|
|
|
|
|
|
StringRef Filename;
|
|
|
|
unsigned Line, Column;
|
|
|
|
bool BadDebugInfo = false;
|
2017-05-12 08:10:49 +08:00
|
|
|
FullSourceLoc Loc =
|
|
|
|
getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column);
|
2014-06-06 06:11:12 +08:00
|
|
|
|
2016-09-13 12:32:40 +08:00
|
|
|
std::string Msg;
|
|
|
|
raw_string_ostream MsgStream(Msg);
|
2016-09-28 04:55:12 +08:00
|
|
|
MsgStream << D.getMsg();
|
2016-09-13 12:32:40 +08:00
|
|
|
|
|
|
|
if (D.getHotness())
|
|
|
|
MsgStream << " (hotness: " << *D.getHotness() << ")";
|
|
|
|
|
2014-07-19 03:40:19 +08:00
|
|
|
Diags.Report(Loc, DiagID)
|
2016-10-01 10:56:57 +08:00
|
|
|
<< AddFlagValue(D.getPassName())
|
2016-09-13 12:32:40 +08:00
|
|
|
<< MsgStream.str();
|
2014-05-30 03:55:06 +08:00
|
|
|
|
2016-02-02 21:52:52 +08:00
|
|
|
if (BadDebugInfo)
|
2014-05-30 03:55:06 +08:00
|
|
|
// If we were not able to translate the file:line:col information
|
|
|
|
// back to a SourceLocation, at least emit a note stating that
|
|
|
|
// we could not translate this location. This can happen in the
|
|
|
|
// case of #line directives.
|
2016-02-02 21:52:52 +08:00
|
|
|
Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
|
2014-05-30 03:55:06 +08:00
|
|
|
<< Filename << Line << Column;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BackendConsumer::OptimizationRemarkHandler(
|
2017-01-26 12:07:11 +08:00
|
|
|
const llvm::DiagnosticInfoOptimizationBase &D) {
|
2017-10-04 12:25:31 +08:00
|
|
|
// Without hotness information, don't show noisy remarks.
|
|
|
|
if (D.isVerbose() && !D.getHotness())
|
|
|
|
return;
|
|
|
|
|
2017-01-26 12:07:11 +08:00
|
|
|
if (D.isPassed()) {
|
|
|
|
// Optimization remarks are active only if the -Rpass flag has a regular
|
|
|
|
// expression that matches the name of the pass name in \p D.
|
|
|
|
if (CodeGenOpts.OptimizationRemarkPattern &&
|
|
|
|
CodeGenOpts.OptimizationRemarkPattern->match(D.getPassName()))
|
|
|
|
EmitOptimizationMessage(D, diag::remark_fe_backend_optimization_remark);
|
|
|
|
} else if (D.isMissed()) {
|
|
|
|
// Missed optimization remarks are active only if the -Rpass-missed
|
|
|
|
// flag has a regular expression that matches the name of the pass
|
|
|
|
// name in \p D.
|
|
|
|
if (CodeGenOpts.OptimizationRemarkMissedPattern &&
|
|
|
|
CodeGenOpts.OptimizationRemarkMissedPattern->match(D.getPassName()))
|
|
|
|
EmitOptimizationMessage(
|
|
|
|
D, diag::remark_fe_backend_optimization_remark_missed);
|
|
|
|
} else {
|
|
|
|
assert(D.isAnalysis() && "Unknown remark type");
|
|
|
|
|
|
|
|
bool ShouldAlwaysPrint = false;
|
|
|
|
if (auto *ORA = dyn_cast<llvm::OptimizationRemarkAnalysis>(&D))
|
|
|
|
ShouldAlwaysPrint = ORA->shouldAlwaysPrint();
|
|
|
|
|
|
|
|
if (ShouldAlwaysPrint ||
|
|
|
|
(CodeGenOpts.OptimizationRemarkAnalysisPattern &&
|
|
|
|
CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName())))
|
|
|
|
EmitOptimizationMessage(
|
|
|
|
D, diag::remark_fe_backend_optimization_remark_analysis);
|
|
|
|
}
|
2014-04-17 00:54:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-11 03:56:40 +08:00
|
|
|
void BackendConsumer::OptimizationRemarkHandler(
|
2016-09-28 06:19:29 +08:00
|
|
|
const llvm::OptimizationRemarkAnalysisFPCommute &D) {
|
2015-08-11 09:10:08 +08:00
|
|
|
// Optimization analysis remarks are active if the pass name is set to
|
|
|
|
// llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a
|
|
|
|
// regular expression that matches the name of the pass name in \p D.
|
|
|
|
|
2016-06-29 12:55:31 +08:00
|
|
|
if (D.shouldAlwaysPrint() ||
|
2015-08-11 09:10:08 +08:00
|
|
|
(CodeGenOpts.OptimizationRemarkAnalysisPattern &&
|
|
|
|
CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName())))
|
2015-08-11 03:56:40 +08:00
|
|
|
EmitOptimizationMessage(
|
|
|
|
D, diag::remark_fe_backend_optimization_remark_analysis_fpcommute);
|
|
|
|
}
|
|
|
|
|
2015-08-11 07:05:16 +08:00
|
|
|
void BackendConsumer::OptimizationRemarkHandler(
|
2016-09-28 06:19:29 +08:00
|
|
|
const llvm::OptimizationRemarkAnalysisAliasing &D) {
|
2015-08-11 09:10:08 +08:00
|
|
|
// Optimization analysis remarks are active if the pass name is set to
|
|
|
|
// llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a
|
|
|
|
// regular expression that matches the name of the pass name in \p D.
|
|
|
|
|
2016-06-29 12:55:31 +08:00
|
|
|
if (D.shouldAlwaysPrint() ||
|
2015-08-11 09:10:08 +08:00
|
|
|
(CodeGenOpts.OptimizationRemarkAnalysisPattern &&
|
|
|
|
CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName())))
|
2015-08-11 07:05:16 +08:00
|
|
|
EmitOptimizationMessage(
|
|
|
|
D, diag::remark_fe_backend_optimization_remark_analysis_aliasing);
|
|
|
|
}
|
|
|
|
|
2014-07-19 03:40:19 +08:00
|
|
|
void BackendConsumer::OptimizationFailureHandler(
|
|
|
|
const llvm::DiagnosticInfoOptimizationFailure &D) {
|
|
|
|
EmitOptimizationMessage(D, diag::warn_fe_backend_optimization_failure);
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// This function is invoked when the backend needs
|
2014-02-07 02:30:43 +08:00
|
|
|
/// to report something to the user.
|
|
|
|
void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) {
|
|
|
|
unsigned DiagID = diag::err_fe_inline_asm;
|
|
|
|
llvm::DiagnosticSeverity Severity = DI.getSeverity();
|
|
|
|
// Get the diagnostic ID based.
|
|
|
|
switch (DI.getKind()) {
|
|
|
|
case llvm::DK_InlineAsm:
|
|
|
|
if (InlineAsmDiagHandler(cast<DiagnosticInfoInlineAsm>(DI)))
|
|
|
|
return;
|
|
|
|
ComputeDiagID(Severity, inline_asm, DiagID);
|
|
|
|
break;
|
|
|
|
case llvm::DK_StackSize:
|
|
|
|
if (StackSizeDiagHandler(cast<DiagnosticInfoStackSize>(DI)))
|
|
|
|
return;
|
|
|
|
ComputeDiagID(Severity, backend_frame_larger_than, DiagID);
|
|
|
|
break;
|
2015-12-15 07:17:07 +08:00
|
|
|
case DK_Linker:
|
|
|
|
assert(CurLinkModule);
|
|
|
|
// FIXME: stop eating the warnings and notes.
|
|
|
|
if (Severity != DS_Error)
|
|
|
|
return;
|
|
|
|
DiagID = diag::err_fe_cannot_link_module;
|
|
|
|
break;
|
2014-04-17 00:54:24 +08:00
|
|
|
case llvm::DK_OptimizationRemark:
|
|
|
|
// Optimization remarks are always handled completely by this
|
|
|
|
// handler. There is no generic way of emitting them.
|
2016-09-28 06:19:29 +08:00
|
|
|
OptimizationRemarkHandler(cast<OptimizationRemark>(DI));
|
2014-04-17 00:54:24 +08:00
|
|
|
return;
|
2014-05-30 00:19:27 +08:00
|
|
|
case llvm::DK_OptimizationRemarkMissed:
|
2014-05-30 03:55:06 +08:00
|
|
|
// Optimization remarks are always handled completely by this
|
|
|
|
// handler. There is no generic way of emitting them.
|
2016-09-28 06:19:29 +08:00
|
|
|
OptimizationRemarkHandler(cast<OptimizationRemarkMissed>(DI));
|
2014-05-30 03:55:06 +08:00
|
|
|
return;
|
2014-05-30 00:19:27 +08:00
|
|
|
case llvm::DK_OptimizationRemarkAnalysis:
|
2014-05-30 03:55:06 +08:00
|
|
|
// Optimization remarks are always handled completely by this
|
|
|
|
// handler. There is no generic way of emitting them.
|
2016-09-28 06:19:29 +08:00
|
|
|
OptimizationRemarkHandler(cast<OptimizationRemarkAnalysis>(DI));
|
2014-05-30 00:19:27 +08:00
|
|
|
return;
|
2015-08-11 03:56:40 +08:00
|
|
|
case llvm::DK_OptimizationRemarkAnalysisFPCommute:
|
|
|
|
// Optimization remarks are always handled completely by this
|
|
|
|
// handler. There is no generic way of emitting them.
|
2016-09-28 06:19:29 +08:00
|
|
|
OptimizationRemarkHandler(cast<OptimizationRemarkAnalysisFPCommute>(DI));
|
2015-08-11 03:56:40 +08:00
|
|
|
return;
|
2015-08-11 07:05:16 +08:00
|
|
|
case llvm::DK_OptimizationRemarkAnalysisAliasing:
|
|
|
|
// Optimization remarks are always handled completely by this
|
|
|
|
// handler. There is no generic way of emitting them.
|
2016-09-28 06:19:29 +08:00
|
|
|
OptimizationRemarkHandler(cast<OptimizationRemarkAnalysisAliasing>(DI));
|
2015-08-11 07:05:16 +08:00
|
|
|
return;
|
2017-01-26 12:07:11 +08:00
|
|
|
case llvm::DK_MachineOptimizationRemark:
|
|
|
|
// Optimization remarks are always handled completely by this
|
|
|
|
// handler. There is no generic way of emitting them.
|
|
|
|
OptimizationRemarkHandler(cast<MachineOptimizationRemark>(DI));
|
|
|
|
return;
|
|
|
|
case llvm::DK_MachineOptimizationRemarkMissed:
|
|
|
|
// Optimization remarks are always handled completely by this
|
|
|
|
// handler. There is no generic way of emitting them.
|
|
|
|
OptimizationRemarkHandler(cast<MachineOptimizationRemarkMissed>(DI));
|
|
|
|
return;
|
|
|
|
case llvm::DK_MachineOptimizationRemarkAnalysis:
|
|
|
|
// Optimization remarks are always handled completely by this
|
|
|
|
// handler. There is no generic way of emitting them.
|
|
|
|
OptimizationRemarkHandler(cast<MachineOptimizationRemarkAnalysis>(DI));
|
|
|
|
return;
|
2014-07-19 03:40:19 +08:00
|
|
|
case llvm::DK_OptimizationFailure:
|
|
|
|
// Optimization failures are always handled completely by this
|
|
|
|
// handler.
|
|
|
|
OptimizationFailureHandler(cast<DiagnosticInfoOptimizationFailure>(DI));
|
|
|
|
return;
|
2016-02-02 21:52:52 +08:00
|
|
|
case llvm::DK_Unsupported:
|
|
|
|
UnsupportedDiagHandler(cast<DiagnosticInfoUnsupported>(DI));
|
|
|
|
return;
|
2014-02-07 02:30:43 +08:00
|
|
|
default:
|
|
|
|
// Plugin IDs are not bound to any value as they are set dynamically.
|
2014-02-28 17:11:08 +08:00
|
|
|
ComputeDiagRemarkID(Severity, backend_plugin, DiagID);
|
2014-02-07 02:30:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::string MsgStorage;
|
|
|
|
{
|
|
|
|
raw_string_ostream Stream(MsgStorage);
|
|
|
|
DiagnosticPrinterRawOStream DP(Stream);
|
|
|
|
DI.print(DP);
|
|
|
|
}
|
|
|
|
|
2015-12-15 07:17:07 +08:00
|
|
|
if (DiagID == diag::err_fe_cannot_link_module) {
|
|
|
|
Diags.Report(diag::err_fe_cannot_link_module)
|
|
|
|
<< CurLinkModule->getModuleIdentifier() << MsgStorage;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-07 02:30:43 +08:00
|
|
|
// Report the backend message using the usual diagnostic mechanism.
|
|
|
|
FullSourceLoc Loc;
|
|
|
|
Diags.Report(Loc, DiagID).AddString(MsgStorage);
|
|
|
|
}
|
|
|
|
#undef ComputeDiagID
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-02-20 07:03:58 +08:00
|
|
|
CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
|
2015-10-28 01:56:59 +08:00
|
|
|
: Act(_Act), VMContext(_VMContext ? _VMContext : new LLVMContext),
|
2016-03-12 09:47:11 +08:00
|
|
|
OwnsVMContext(!_VMContext) {}
|
2011-02-20 07:03:58 +08:00
|
|
|
|
|
|
|
CodeGenAction::~CodeGenAction() {
|
|
|
|
TheModule.reset();
|
|
|
|
if (OwnsVMContext)
|
|
|
|
delete VMContext;
|
|
|
|
}
|
2010-02-26 04:37:44 +08:00
|
|
|
|
2010-06-08 07:27:59 +08:00
|
|
|
bool CodeGenAction::hasIRSupport() const { return true; }
|
|
|
|
|
2010-02-25 12:37:50 +08:00
|
|
|
void CodeGenAction::EndSourceFileAction() {
|
|
|
|
// If the consumer creation failed, do nothing.
|
|
|
|
if (!getCompilerInstance().hasASTConsumer())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Steal the module from the consumer.
|
2014-08-29 13:08:19 +08:00
|
|
|
TheModule = BEConsumer->takeModule();
|
2010-02-25 12:37:50 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 22:36:35 +08:00
|
|
|
std::unique_ptr<llvm::Module> CodeGenAction::takeModule() {
|
|
|
|
return std::move(TheModule);
|
|
|
|
}
|
|
|
|
|
2011-02-20 07:03:58 +08:00
|
|
|
llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
|
|
|
|
OwnsVMContext = false;
|
|
|
|
return VMContext;
|
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
static std::unique_ptr<raw_pwrite_stream>
|
2015-04-14 23:15:49 +08:00
|
|
|
GetOutputStream(CompilerInstance &CI, StringRef InFile, BackendAction Action) {
|
2010-06-08 07:27:59 +08:00
|
|
|
switch (Action) {
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitAssembly:
|
2010-06-08 07:27:59 +08:00
|
|
|
return CI.createDefaultOutputFile(false, InFile, "s");
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitLL:
|
2010-06-08 07:27:59 +08:00
|
|
|
return CI.createDefaultOutputFile(false, InFile, "ll");
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitBC:
|
2010-06-08 07:27:59 +08:00
|
|
|
return CI.createDefaultOutputFile(true, InFile, "bc");
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitNothing:
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2010-05-26 02:41:01 +08:00
|
|
|
case Backend_EmitMCNull:
|
2014-06-04 01:23:34 +08:00
|
|
|
return CI.createNullOutputFile();
|
2010-02-25 12:37:45 +08:00
|
|
|
case Backend_EmitObj:
|
2010-06-08 07:27:59 +08:00
|
|
|
return CI.createDefaultOutputFile(true, InFile, "o");
|
2010-02-25 12:37:45 +08:00
|
|
|
}
|
2010-06-08 07:27:59 +08:00
|
|
|
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Invalid action!");
|
2010-06-08 07:27:59 +08:00
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2010-06-08 07:27:59 +08:00
|
|
|
BackendAction BA = static_cast<BackendAction>(Act);
|
2018-03-02 20:11:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS = CI.takeOutputStream();
|
|
|
|
if (!OS)
|
|
|
|
OS = GetOutputStream(CI, InFile, BA);
|
|
|
|
|
2010-02-25 12:37:45 +08:00
|
|
|
if (BA != Backend_EmitNothing && !OS)
|
2014-05-21 13:09:00 +08:00
|
|
|
return nullptr;
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2015-10-28 01:56:59 +08:00
|
|
|
// Load bitcode modules to link with, if we need to.
|
|
|
|
if (LinkModules.empty())
|
[CodeGen] [CUDA] Add the ability set default attrs on functions in linked modules.
Summary:
Now when you ask clang to link in a bitcode module, you can tell it to
set attributes on that module's functions to match what we would have
set if we'd emitted those functions ourselves.
This is particularly important for fast-math attributes in CUDA
compilations.
Each CUDA compilation links in libdevice, a bitcode library provided by
nvidia as part of the CUDA distribution. Without this patch, if we have
a user-function F that is compiled with -ffast-math that calls a
function G from libdevice, F will have the unsafe-fp-math=true (etc.)
attributes, but G will have no attributes.
Since F calls G, the inliner will merge G's attributes into F's. It
considers the lack of an unsafe-fp-math=true attribute on G to be
tantamount to unsafe-fp-math=false, so it "merges" these by setting
unsafe-fp-math=false on F.
This then continues up the call graph, until every function that
(transitively) calls something in libdevice gets unsafe-fp-math=false
set, thus disabling fastmath in almost all CUDA code.
Reviewers: echristo
Subscribers: hfinkel, llvm-commits, mehdi_amini
Differential Revision: https://reviews.llvm.org/D28538
llvm-svn: 293097
2017-01-26 05:29:48 +08:00
|
|
|
for (const CodeGenOptions::BitcodeFileToLink &F :
|
|
|
|
CI.getCodeGenOpts().LinkBitcodeFiles) {
|
|
|
|
auto BCBuf = CI.getFileManager().getBufferForFile(F.Filename);
|
2015-10-28 01:56:59 +08:00
|
|
|
if (!BCBuf) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_cannot_open_file)
|
[CodeGen] [CUDA] Add the ability set default attrs on functions in linked modules.
Summary:
Now when you ask clang to link in a bitcode module, you can tell it to
set attributes on that module's functions to match what we would have
set if we'd emitted those functions ourselves.
This is particularly important for fast-math attributes in CUDA
compilations.
Each CUDA compilation links in libdevice, a bitcode library provided by
nvidia as part of the CUDA distribution. Without this patch, if we have
a user-function F that is compiled with -ffast-math that calls a
function G from libdevice, F will have the unsafe-fp-math=true (etc.)
attributes, but G will have no attributes.
Since F calls G, the inliner will merge G's attributes into F's. It
considers the lack of an unsafe-fp-math=true attribute on G to be
tantamount to unsafe-fp-math=false, so it "merges" these by setting
unsafe-fp-math=false on F.
This then continues up the call graph, until every function that
(transitively) calls something in libdevice gets unsafe-fp-math=false
set, thus disabling fastmath in almost all CUDA code.
Reviewers: echristo
Subscribers: hfinkel, llvm-commits, mehdi_amini
Differential Revision: https://reviews.llvm.org/D28538
llvm-svn: 293097
2017-01-26 05:29:48 +08:00
|
|
|
<< F.Filename << BCBuf.getError().message();
|
2015-10-28 01:56:59 +08:00
|
|
|
LinkModules.clear();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2011-10-31 01:30:44 +08:00
|
|
|
|
2016-11-13 15:00:17 +08:00
|
|
|
Expected<std::unique_ptr<llvm::Module>> ModuleOrErr =
|
2016-11-08 14:03:43 +08:00
|
|
|
getOwningLazyBitcodeModule(std::move(*BCBuf), *VMContext);
|
2016-11-13 15:00:17 +08:00
|
|
|
if (!ModuleOrErr) {
|
|
|
|
handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_cannot_open_file)
|
[CodeGen] [CUDA] Add the ability set default attrs on functions in linked modules.
Summary:
Now when you ask clang to link in a bitcode module, you can tell it to
set attributes on that module's functions to match what we would have
set if we'd emitted those functions ourselves.
This is particularly important for fast-math attributes in CUDA
compilations.
Each CUDA compilation links in libdevice, a bitcode library provided by
nvidia as part of the CUDA distribution. Without this patch, if we have
a user-function F that is compiled with -ffast-math that calls a
function G from libdevice, F will have the unsafe-fp-math=true (etc.)
attributes, but G will have no attributes.
Since F calls G, the inliner will merge G's attributes into F's. It
considers the lack of an unsafe-fp-math=true attribute on G to be
tantamount to unsafe-fp-math=false, so it "merges" these by setting
unsafe-fp-math=false on F.
This then continues up the call graph, until every function that
(transitively) calls something in libdevice gets unsafe-fp-math=false
set, thus disabling fastmath in almost all CUDA code.
Reviewers: echristo
Subscribers: hfinkel, llvm-commits, mehdi_amini
Differential Revision: https://reviews.llvm.org/D28538
llvm-svn: 293097
2017-01-26 05:29:48 +08:00
|
|
|
<< F.Filename << EIB.message();
|
2016-11-13 15:00:17 +08:00
|
|
|
});
|
2015-10-28 01:56:59 +08:00
|
|
|
LinkModules.clear();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-03-14 02:08:11 +08:00
|
|
|
LinkModules.push_back({std::move(ModuleOrErr.get()), F.PropagateAttrs,
|
|
|
|
F.Internalize, F.LinkFlags});
|
2011-10-31 01:30:44 +08:00
|
|
|
}
|
|
|
|
|
2014-08-05 02:41:51 +08:00
|
|
|
CoverageSourceInfo *CoverageInfo = nullptr;
|
|
|
|
// Add the preprocessor callback only when the coverage mapping is generated.
|
|
|
|
if (CI.getCodeGenOpts().CoverageMapping) {
|
|
|
|
CoverageInfo = new CoverageSourceInfo;
|
2014-09-10 12:53:53 +08:00
|
|
|
CI.getPreprocessor().addPPCallbacks(
|
|
|
|
std::unique_ptr<PPCallbacks>(CoverageInfo));
|
2014-08-05 02:41:51 +08:00
|
|
|
}
|
2015-10-28 01:56:59 +08:00
|
|
|
|
2014-08-11 05:06:17 +08:00
|
|
|
std::unique_ptr<BackendConsumer> Result(new BackendConsumer(
|
2015-06-30 10:26:03 +08:00
|
|
|
BA, CI.getDiagnostics(), CI.getHeaderSearchOpts(),
|
|
|
|
CI.getPreprocessorOpts(), CI.getCodeGenOpts(), CI.getTargetOpts(),
|
[CodeGen] [CUDA] Add the ability set default attrs on functions in linked modules.
Summary:
Now when you ask clang to link in a bitcode module, you can tell it to
set attributes on that module's functions to match what we would have
set if we'd emitted those functions ourselves.
This is particularly important for fast-math attributes in CUDA
compilations.
Each CUDA compilation links in libdevice, a bitcode library provided by
nvidia as part of the CUDA distribution. Without this patch, if we have
a user-function F that is compiled with -ffast-math that calls a
function G from libdevice, F will have the unsafe-fp-math=true (etc.)
attributes, but G will have no attributes.
Since F calls G, the inliner will merge G's attributes into F's. It
considers the lack of an unsafe-fp-math=true attribute on G to be
tantamount to unsafe-fp-math=false, so it "merges" these by setting
unsafe-fp-math=false on F.
This then continues up the call graph, until every function that
(transitively) calls something in libdevice gets unsafe-fp-math=false
set, thus disabling fastmath in almost all CUDA code.
Reviewers: echristo
Subscribers: hfinkel, llvm-commits, mehdi_amini
Differential Revision: https://reviews.llvm.org/D28538
llvm-svn: 293097
2017-01-26 05:29:48 +08:00
|
|
|
CI.getLangOpts(), CI.getFrontendOpts().ShowTimers, InFile,
|
|
|
|
std::move(LinkModules), std::move(OS), *VMContext, CoverageInfo));
|
2014-08-11 03:56:51 +08:00
|
|
|
BEConsumer = Result.get();
|
2017-02-10 06:07:24 +08:00
|
|
|
|
|
|
|
// Enable generating macro debug info only when debug info is not disabled and
|
|
|
|
// also macro debug info is enabled.
|
|
|
|
if (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo &&
|
|
|
|
CI.getCodeGenOpts().MacroDebugInfo) {
|
|
|
|
std::unique_ptr<PPCallbacks> Callbacks =
|
|
|
|
llvm::make_unique<MacroPPCallbacks>(BEConsumer->getCodeGenerator(),
|
|
|
|
CI.getPreprocessor());
|
|
|
|
CI.getPreprocessor().addPPCallbacks(std::move(Callbacks));
|
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
return std::move(Result);
|
2008-10-22 07:49:24 +08:00
|
|
|
}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2016-04-07 03:58:07 +08:00
|
|
|
static void BitcodeInlineAsmDiagHandler(const llvm::SMDiagnostic &SM,
|
|
|
|
void *Context,
|
|
|
|
unsigned LocCookie) {
|
|
|
|
SM.print(nullptr, llvm::errs());
|
|
|
|
|
|
|
|
auto Diags = static_cast<DiagnosticsEngine *>(Context);
|
|
|
|
unsigned DiagID;
|
|
|
|
switch (SM.getKind()) {
|
|
|
|
case llvm::SourceMgr::DK_Error:
|
|
|
|
DiagID = diag::err_fe_inline_asm;
|
|
|
|
break;
|
|
|
|
case llvm::SourceMgr::DK_Warning:
|
|
|
|
DiagID = diag::warn_fe_inline_asm;
|
|
|
|
break;
|
|
|
|
case llvm::SourceMgr::DK_Note:
|
|
|
|
DiagID = diag::note_fe_inline_asm;
|
|
|
|
break;
|
2017-10-13 07:56:54 +08:00
|
|
|
case llvm::SourceMgr::DK_Remark:
|
|
|
|
llvm_unreachable("remarks unexpected");
|
2016-04-07 03:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Diags->Report(DiagID).AddString("cannot compile inline asm");
|
|
|
|
}
|
|
|
|
|
2017-01-25 03:55:38 +08:00
|
|
|
std::unique_ptr<llvm::Module> CodeGenAction::loadModule(MemoryBufferRef MBRef) {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
SourceManager &SM = CI.getSourceManager();
|
|
|
|
|
|
|
|
// For ThinLTO backend invocations, ensure that the context
|
2017-01-27 05:09:48 +08:00
|
|
|
// merges types based on ODR identifiers. We also need to read
|
|
|
|
// the correct module out of a multi-module bitcode file.
|
|
|
|
if (!CI.getCodeGenOpts().ThinLTOIndexFile.empty()) {
|
2017-01-25 03:55:38 +08:00
|
|
|
VMContext->enableDebugTypeODRUniquing();
|
|
|
|
|
2017-01-27 05:09:48 +08:00
|
|
|
auto DiagErrors = [&](Error E) -> std::unique_ptr<llvm::Module> {
|
|
|
|
unsigned DiagID =
|
|
|
|
CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0");
|
|
|
|
handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
|
|
|
|
CI.getDiagnostics().Report(DiagID) << EIB.message();
|
|
|
|
});
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
|
2018-02-17 07:34:16 +08:00
|
|
|
Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
|
|
|
|
if (!BMsOrErr)
|
|
|
|
return DiagErrors(BMsOrErr.takeError());
|
|
|
|
BitcodeModule *Bm = FindThinLTOModule(*BMsOrErr);
|
|
|
|
// We have nothing to do if the file contains no ThinLTO module. This is
|
|
|
|
// possible if ThinLTO compilation was not able to split module. Content of
|
|
|
|
// the file was already processed by indexing and will be passed to the
|
|
|
|
// linker using merged object file.
|
|
|
|
if (!Bm) {
|
|
|
|
auto M = llvm::make_unique<llvm::Module>("empty", *VMContext);
|
|
|
|
M->setTargetTriple(CI.getTargetOpts().Triple);
|
|
|
|
return M;
|
|
|
|
}
|
2017-01-27 05:09:48 +08:00
|
|
|
Expected<std::unique_ptr<llvm::Module>> MOrErr =
|
2018-02-17 07:34:16 +08:00
|
|
|
Bm->parseModule(*VMContext);
|
2017-01-27 05:09:48 +08:00
|
|
|
if (!MOrErr)
|
|
|
|
return DiagErrors(MOrErr.takeError());
|
|
|
|
return std::move(*MOrErr);
|
|
|
|
}
|
|
|
|
|
2017-01-25 03:55:38 +08:00
|
|
|
llvm::SMDiagnostic Err;
|
|
|
|
if (std::unique_ptr<llvm::Module> M = parseIR(MBRef, Err, *VMContext))
|
|
|
|
return M;
|
|
|
|
|
|
|
|
// Translate from the diagnostic info to the SourceManager location if
|
|
|
|
// available.
|
|
|
|
// TODO: Unify this with ConvertBackendLocation()
|
|
|
|
SourceLocation Loc;
|
|
|
|
if (Err.getLineNo() > 0) {
|
|
|
|
assert(Err.getColumnNo() >= 0);
|
|
|
|
Loc = SM.translateFileLineCol(SM.getFileEntryForID(SM.getMainFileID()),
|
|
|
|
Err.getLineNo(), Err.getColumnNo() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip off a leading diagnostic code if there is one.
|
|
|
|
StringRef Msg = Err.getMessage();
|
|
|
|
if (Msg.startswith("error: "))
|
|
|
|
Msg = Msg.substr(7);
|
|
|
|
|
|
|
|
unsigned DiagID =
|
|
|
|
CI.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error, "%0");
|
|
|
|
|
|
|
|
CI.getDiagnostics().Report(Loc, DiagID) << Msg;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2010-06-08 07:27:59 +08:00
|
|
|
void CodeGenAction::ExecuteAction() {
|
|
|
|
// If this is an IR file, we have to treat it specially.
|
2017-04-27 02:57:40 +08:00
|
|
|
if (getCurrentFileKind().getLanguage() == InputKind::LLVM_IR) {
|
2010-06-08 07:27:59 +08:00
|
|
|
BackendAction BA = static_cast<BackendAction>(Act);
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS =
|
|
|
|
GetOutputStream(CI, getCurrentFile(), BA);
|
2010-06-08 07:27:59 +08:00
|
|
|
if (BA != Backend_EmitNothing && !OS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool Invalid;
|
|
|
|
SourceManager &SM = CI.getSourceManager();
|
2014-06-30 09:33:59 +08:00
|
|
|
FileID FID = SM.getMainFileID();
|
|
|
|
llvm::MemoryBuffer *MainFile = SM.getBuffer(FID, &Invalid);
|
2010-06-08 07:27:59 +08:00
|
|
|
if (Invalid)
|
|
|
|
return;
|
|
|
|
|
2017-01-25 03:55:38 +08:00
|
|
|
TheModule = loadModule(*MainFile);
|
|
|
|
if (!TheModule)
|
2010-06-08 07:27:59 +08:00
|
|
|
return;
|
2017-01-25 03:55:38 +08:00
|
|
|
|
2013-12-21 06:01:25 +08:00
|
|
|
const TargetOptions &TargetOpts = CI.getTargetOpts();
|
|
|
|
if (TheModule->getTargetTriple() != TargetOpts.Triple) {
|
2015-01-29 14:25:59 +08:00
|
|
|
CI.getDiagnostics().Report(SourceLocation(),
|
|
|
|
diag::warn_fe_override_module)
|
|
|
|
<< TargetOpts.Triple;
|
2013-12-21 06:01:25 +08:00
|
|
|
TheModule->setTargetTriple(TargetOpts.Triple);
|
|
|
|
}
|
2010-06-08 07:27:59 +08:00
|
|
|
|
2016-05-12 00:26:03 +08:00
|
|
|
EmbedBitcode(TheModule.get(), CI.getCodeGenOpts(),
|
|
|
|
MainFile->getMemBufferRef());
|
|
|
|
|
2016-04-07 03:58:07 +08:00
|
|
|
LLVMContext &Ctx = TheModule->getContext();
|
|
|
|
Ctx.setInlineAsmDiagnosticHandler(BitcodeInlineAsmDiagHandler,
|
|
|
|
&CI.getDiagnostics());
|
2016-05-12 00:26:03 +08:00
|
|
|
|
2017-01-06 00:02:32 +08:00
|
|
|
EmitBackendOutput(CI.getDiagnostics(), CI.getHeaderSearchOpts(),
|
|
|
|
CI.getCodeGenOpts(), TargetOpts, CI.getLangOpts(),
|
|
|
|
CI.getTarget().getDataLayout(), TheModule.get(), BA,
|
|
|
|
std::move(OS));
|
2010-06-08 07:27:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise follow the normal AST path.
|
|
|
|
this->ASTFrontendAction::ExecuteAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitAssemblyAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitAssembly, _VMContext) {}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitBCAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitBC, _VMContext) {}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitLLVMAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitLL, _VMContext) {}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitLLVMOnlyAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitNothing, _VMContext) {}
|
2010-02-25 12:37:45 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitCodeGenOnlyAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitMCNull, _VMContext) {}
|
2010-05-26 02:41:01 +08:00
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void EmitObjAction::anchor() { }
|
2011-02-20 07:03:58 +08:00
|
|
|
EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
|
|
|
|
: CodeGenAction(Backend_EmitObj, _VMContext) {}
|