10#include "mlir/IR/MLIRContext.h"
11#include "mlir/IR/OwningOpRef.h"
19#include "llvm/ADT/ScopeExit.h"
20#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/StringSet.h"
22#include "llvm/IR/DiagnosticHandler.h"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/GlobalValue.h"
25#include "llvm/IR/LLVMContext.h"
26#include "llvm/IR/Module.h"
27#include "llvm/Linker/Linker.h"
28#include "llvm/Support/Path.h"
29#include "llvm/Support/raw_ostream.h"
30#include "llvm/Transforms/IPO/Internalize.h"
42 "Unsupported output type for getBackendActionFromOutputType!");
55 llvm_unreachable(
"Unsupported output type!");
58static std::unique_ptr<llvm::Module>
60 llvm::StringRef mlirSaveTempsOutFile = {},
61 llvm::vfs::FileSystem *fs =
nullptr) {
63 mlirSaveTempsOutFile, fs);
68 virtual void anchor();
74 std::unique_ptr<raw_pwrite_stream> OutputStream;
78 std::unique_ptr<CIRGenerator> Gen;
82 llvm::LLVMContext &LLVMCtx;
88 llvm::LLVMContext &LLVMCtx,
90 : Action(Action), CI(CI), OutputStream(
std::move(OS)),
91 FS(&CI.getVirtualFileSystem()),
93 CI.getCodeGenOpts())),
94 FEOptions(CI.getFrontendOpts()), CGO(CGO), LLVMCtx(LLVMCtx),
95 LinkModules(LinkModules) {}
98 assert(!Context &&
"initialized multiple times");
100 Gen->Initialize(Ctx);
104 Gen->HandleTopLevelDecl(D);
109 Gen->HandleCXXStaticMemberVarInstantiation(VD);
114 Gen->HandleOpenACCRoutineReference(FD, RD);
118 Gen->HandleInlineFunctionDefinition(D);
122 Gen->HandleTranslationUnit(
C);
124 if (!FEOptions.ClangIRDisableCIRVerifier) {
125 if (!Gen->verifyModule()) {
126 CI.getDiagnostics().Report(
127 diag::err_cir_verification_failed_pre_passes);
128 llvm::report_fatal_error(
129 "CIR codegen: module verification error before running CIR passes");
134 mlir::ModuleOp MlirModule = Gen->getModule();
135 mlir::MLIRContext &MlirCtx = Gen->getMLIRContext();
137 if (!FEOptions.ClangIRDisablePasses) {
140 MlirModule, MlirCtx,
C, !FEOptions.ClangIRDisableCIRVerifier,
141 FEOptions.ClangIREnableIdiomRecognizer, CGO.OptimizationLevel > 0)
143 CI.getDiagnostics().Report(diag::err_cir_to_cir_transform_failed);
150 if (OutputStream && MlirModule) {
151 mlir::OpPrintingFlags Flags;
152 Flags.enableDebugInfo(
true,
false);
153 MlirModule->print(*OutputStream, Flags);
160 StringRef saveTempsPrefix = CGO.SaveTempsFilePrefix;
161 std::string cirSaveTempsOutFile, mlirSaveTempsOutFile;
162 if (!saveTempsPrefix.empty()) {
164 llvm::sys::path::replace_extension(stem,
"cir");
165 cirSaveTempsOutFile = std::string(stem);
166 llvm::sys::path::replace_extension(stem,
"mlir");
167 mlirSaveTempsOutFile = std::string(stem);
170 if (!cirSaveTempsOutFile.empty()) {
172 llvm::raw_fd_ostream
out(cirSaveTempsOutFile, ec);
174 MlirModule->print(
out);
177 std::unique_ptr<llvm::Module> LLVMModule =
179 &CI.getVirtualFileSystem());
186 CI, CI.getCodeGenOpts(),
C.getTargetInfo().getDataLayoutString(),
187 LLVMModule.get(), BEAction, FS, std::move(OutputStream));
196 for (
auto &LM : LinkModules) {
197 assert(LM.Module &&
"LinkModule does not actually have a module");
199 if (LM.PropagateAttrs)
200 for (llvm::Function &F : *LM.Module) {
204 F, CGO, CI.getLangOpts(), CI.getTargetOpts(), LM.Internalize);
208 if (LM.Internalize) {
209 Err = llvm::Linker::linkModules(
210 M, std::move(LM.Module), LM.LinkFlags,
211 [](llvm::Module &M,
const llvm::StringSet<> &GVS) {
212 llvm::internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) {
213 return !GV.hasName() || (GVS.count(GV.getName()) == 0);
217 Err = llvm::Linker::linkModules(M, std::move(LM.Module), LM.LinkFlags);
230 Context->getSourceManager(),
231 "CIR generation of declaration");
232 Gen->HandleTagDeclDefinition(D);
236 Gen->HandleTagDeclRequiredDefinition(D);
240 Gen->CompleteTentativeDefinition(D);
247void CIRGenConsumer::anchor() {}
250 : MLIRCtx(MLIRCtx ? MLIRCtx : new
mlir::MLIRContext),
261static std::unique_ptr<raw_pwrite_stream>
276 llvm_unreachable(
"Invalid CIRGenAction::OutputType");
279std::unique_ptr<ASTConsumer>
286 auto Result = std::make_unique<cir::CIRGenConsumer>(
292void EmitAssemblyAction::anchor() {}
296void EmitCIRAction::anchor() {}
300void EmitLLVMAction::anchor() {}
304void EmitBCAction::anchor() {}
308void EmitObjAction::anchor() {}
static std::unique_ptr< raw_pwrite_stream > getOutputStream(CompilerInstance &CI, StringRef InFile, CIRGenAction::OutputType Action)
CIRGenAction(OutputType Action, mlir::MLIRContext *MLIRCtx=nullptr)
bool BeginSourceFileAction(clang::CompilerInstance &CI) override
Callback at the start of processing a single input.
std::unique_ptr< clang::ASTConsumer > CreateASTConsumer(clang::CompilerInstance &CI, llvm::StringRef InFile) override
void Initialize(ASTContext &Ctx) override
Initialize - This is called to initialize the consumer, providing the ASTContext.
bool HandleTopLevelDecl(DeclGroupRef D) override
HandleTopLevelDecl - Handle the specified top-level declaration.
void HandleTranslationUnit(ASTContext &C) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
bool linkInModules(llvm::Module &M)
void HandleInlineFunctionDefinition(FunctionDecl *D) override
This callback is invoked each time an inline (method or friend) function definition in a class is com...
void CompleteTentativeDefinition(VarDecl *D) override
CompleteTentativeDefinition - Callback invoked at the end of a translation unit to notify the consume...
void HandleTagDeclRequiredDefinition(const TagDecl *D) override
This callback is invoked the first time each TagDecl is required to be complete.
CIRGenConsumer(CIRGenAction::OutputType Action, CompilerInstance &CI, CodeGenOptions &CGO, std::unique_ptr< raw_pwrite_stream > OS, llvm::LLVMContext &LLVMCtx, SmallVectorImpl<::clang::LinkModule > &LinkModules)
void HandleVTable(CXXRecordDecl *RD) override
Callback involved at the end of a translation unit to notify the consumer that a vtable for the given...
void HandleCXXStaticMemberVarInstantiation(clang::VarDecl *VD) override
HandleCXXStaticMemberVarInstantiation - Tell the consumer that this.
void HandleOpenACCRoutineReference(const FunctionDecl *FD, const OpenACCRoutineDecl *RD) override
Callback to handle the end-of-translation unit attachment of OpenACC routine declaration information.
void HandleTagDeclDefinition(TagDecl *D) override
HandleTagDeclDefinition - This callback is invoked each time a TagDecl (e.g.
EmitAssemblyAction(mlir::MLIRContext *MLIRCtx=nullptr)
EmitBCAction(mlir::MLIRContext *MLIRCtx=nullptr)
EmitCIRAction(mlir::MLIRContext *MLIRCtx=nullptr)
EmitLLVMAction(mlir::MLIRContext *MLIRCtx=nullptr)
EmitObjAction(mlir::MLIRContext *MLIRCtx=nullptr)
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Represents a C++ struct/union/class.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="", bool RemoveFileOnSignal=true, bool CreateMissingDirectories=false, bool ForceUseTemporary=false, bool SetOnlyIfDifferent=false)
Create the default output file (from the invocation's options) and add it to the list of tracked outp...
std::unique_ptr< llvm::raw_pwrite_stream > takeOutputStream()
CodeGenOptions & getCodeGenOpts()
virtual bool BeginSourceFileAction(CompilerInstance &CI)
Callback at the start of processing a single input.
FrontendOptions - Options for controlling the behavior of the frontend.
Represents a function declaration or definition.
PrettyStackTraceDecl - If a crash occurs, indicate that it happened when doing something to a specifi...
Encodes a location in the source.
Represents the declaration of a struct/union/class/enum.
Represents a variable declaration or definition.
std::unique_ptr< llvm::Module > lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp mlirModule, llvm::LLVMContext &llvmCtx, llvm::StringRef mlirSaveTempsOutFile={}, llvm::vfs::FileSystem *fs=nullptr)
static BackendAction getBackendActionFromOutputType(CIRGenAction::OutputType Action)
static std::unique_ptr< llvm::Module > lowerFromCIRToLLVMIR(mlir::ModuleOp MLIRModule, llvm::LLVMContext &LLVMCtx, llvm::StringRef mlirSaveTempsOutFile={}, llvm::vfs::FileSystem *fs=nullptr)
mlir::LogicalResult runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext &mlirCtx, clang::ASTContext &astCtx, bool enableVerifier, bool enableIdiomRecognizer, bool enableCIRSimplify)
void mergeDefaultFunctionDefinitionAttributes(llvm::Function &F, const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts, const TargetOptions &TargetOpts, bool WillInternalize)
Adds attributes to F according to our CodeGenOpts and LangOpts, as though we had emitted it ourselves...
The JSON file list parser is used to communicate input to InstallAPI.
void emitBackendOutput(CompilerInstance &CI, CodeGenOptions &CGOpts, StringRef TDesc, llvm::Module *M, BackendAction Action, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, std::unique_ptr< raw_pwrite_stream > OS, BackendConsumer *BC=nullptr)
@ Backend_EmitAssembly
Emit native assembly files.
@ Backend_EmitLL
Emit human-readable LLVM assembly.
@ Backend_EmitBC
Emit LLVM bitcode files.
@ Backend_EmitObj
Emit native object files.
bool loadLinkModules(CompilerInstance &CI, llvm::LLVMContext &Ctx, llvm::SmallVectorImpl< LinkModule > &LinkModules)
Load every bitcode file listed in CodeGenOpts.LinkBitcodeFiles into LinkModules.
Diagnostic wrappers for TextAPI types for error reporting.