10#include "mlir/IR/MLIRContext.h"
11#include "mlir/IR/OwningOpRef.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/IR/Module.h"
20#include "llvm/Support/Path.h"
21#include "llvm/Support/raw_ostream.h"
33 "Unsupported output type for getBackendActionFromOutputType!");
46 llvm_unreachable(
"Unsupported output type!");
49static std::unique_ptr<llvm::Module>
51 llvm::StringRef mlirSaveTempsOutFile = {}) {
53 mlirSaveTempsOutFile);
58 virtual void anchor();
64 std::unique_ptr<raw_pwrite_stream> OutputStream;
68 std::unique_ptr<CIRGenerator> Gen;
75 : Action(Action), CI(CI), OutputStream(
std::move(OS)),
76 FS(&CI.getVirtualFileSystem()),
78 CI.getCodeGenOpts())),
79 FEOptions(CI.getFrontendOpts()), CGO(CGO) {}
82 assert(!Context &&
"initialized multiple times");
88 Gen->HandleTopLevelDecl(D);
93 Gen->HandleCXXStaticMemberVarInstantiation(VD);
98 Gen->HandleOpenACCRoutineReference(FD, RD);
102 Gen->HandleInlineFunctionDefinition(D);
106 Gen->HandleTranslationUnit(
C);
108 if (!FEOptions.ClangIRDisableCIRVerifier) {
109 if (!Gen->verifyModule()) {
110 CI.getDiagnostics().Report(
111 diag::err_cir_verification_failed_pre_passes);
112 llvm::report_fatal_error(
113 "CIR codegen: module verification error before running CIR passes");
118 mlir::ModuleOp MlirModule = Gen->getModule();
119 mlir::MLIRContext &MlirCtx = Gen->getMLIRContext();
121 if (!FEOptions.ClangIRDisablePasses) {
124 MlirModule, MlirCtx,
C, !FEOptions.ClangIRDisableCIRVerifier,
125 FEOptions.ClangIREnableIdiomRecognizer, CGO.OptimizationLevel > 0)
127 CI.getDiagnostics().Report(diag::err_cir_to_cir_transform_failed);
134 if (OutputStream && MlirModule) {
135 mlir::OpPrintingFlags Flags;
136 Flags.enableDebugInfo(
true,
false);
137 MlirModule->print(*OutputStream, Flags);
144 StringRef saveTempsPrefix = CGO.SaveTempsFilePrefix;
145 std::string cirSaveTempsOutFile, mlirSaveTempsOutFile;
146 if (!saveTempsPrefix.empty()) {
148 llvm::sys::path::replace_extension(stem,
"cir");
149 cirSaveTempsOutFile = std::string(stem);
150 llvm::sys::path::replace_extension(stem,
"mlir");
151 mlirSaveTempsOutFile = std::string(stem);
154 if (!cirSaveTempsOutFile.empty()) {
156 llvm::raw_fd_ostream
out(cirSaveTempsOutFile, ec);
158 MlirModule->print(
out);
161 llvm::LLVMContext LLVMCtx;
162 std::unique_ptr<llvm::Module> LLVMModule =
167 CI, CI.getCodeGenOpts(),
C.getTargetInfo().getDataLayoutString(),
168 LLVMModule.get(), BEAction, FS, std::move(OutputStream));
176 Context->getSourceManager(),
177 "CIR generation of declaration");
178 Gen->HandleTagDeclDefinition(D);
182 Gen->HandleTagDeclRequiredDefinition(D);
186 Gen->CompleteTentativeDefinition(D);
193void CIRGenConsumer::anchor() {}
196 : MLIRCtx(MLIRCtx ? MLIRCtx : new
mlir::MLIRContext),
Action(Act) {}
200static std::unique_ptr<raw_pwrite_stream>
215 llvm_unreachable(
"Invalid CIRGenAction::OutputType");
218std::unique_ptr<ASTConsumer>
225 auto Result = std::make_unique<cir::CIRGenConsumer>(
231void EmitAssemblyAction::anchor() {}
235void EmitCIRAction::anchor() {}
239void EmitLLVMAction::anchor() {}
243void EmitBCAction::anchor() {}
247void EmitObjAction::anchor() {}
static std::unique_ptr< raw_pwrite_stream > getOutputStream(CompilerInstance &CI, StringRef InFile, CIRGenAction::OutputType Action)
CIRGenAction(OutputType Action, mlir::MLIRContext *MLIRCtx=nullptr)
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...
CIRGenConsumer(CIRGenAction::OutputType Action, CompilerInstance &CI, CodeGenOptions &CGO, std::unique_ptr< raw_pwrite_stream > OS)
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.
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)
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()
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={})
static BackendAction getBackendActionFromOutputType(CIRGenAction::OutputType Action)
mlir::LogicalResult runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext &mlirCtx, clang::ASTContext &astCtx, bool enableVerifier, bool enableIdiomRecognizer, bool enableCIRSimplify)
static std::unique_ptr< llvm::Module > lowerFromCIRToLLVMIR(mlir::ModuleOp MLIRModule, llvm::LLVMContext &LLVMCtx, llvm::StringRef mlirSaveTempsOutFile={})
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.