11#include "mlir/Bytecode/BytecodeWriter.h"
12#include "mlir/IR/BuiltinOps.h"
13#include "mlir/IR/MLIRContext.h"
14#include "mlir/IR/OwningOpRef.h"
15#include "mlir/Parser/Parser.h"
28#include "llvm/ADT/STLFunctionalExtras.h"
29#include "llvm/ADT/ScopeExit.h"
30#include "llvm/ADT/SmallString.h"
31#include "llvm/Frontend/Offloading/OffloadWrapper.h"
32#include "llvm/IR/DiagnosticHandler.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/GlobalValue.h"
35#include "llvm/IR/LLVMContext.h"
36#include "llvm/IR/Module.h"
37#include "llvm/Support/LibraryOptionsContext.h"
38#include "llvm/Support/MemoryBuffer.h"
39#include "llvm/Support/Path.h"
40#include "llvm/Support/raw_ostream.h"
53 "Unsupported output type for getBackendActionFromOutputType!");
66 llvm_unreachable(
"Unsupported output type!");
70 mlir::ModuleOp MLIRModule, llvm::LLVMContext &LLVMCtx,
bool EnableOpenMP,
71 llvm::StringRef mlirSaveTempsOutFile = {},
72 llvm::vfs::FileSystem *fs =
nullptr,
bool enableOptimizations =
false) {
74 mlirSaveTempsOutFile, fs,
81 mlir::OpPrintingFlags Flags;
82 Flags.enableDebugInfo(
true,
false);
83 MLIRModule->print(OS, Flags);
92 if (std::error_code ec = bufferOrErr.getError()) {
94 << fileName << ec.message();
97 std::unique_ptr<llvm::MemoryBuffer> buffer = std::move(bufferOrErr.get());
98 if (llvm::Error err = llvm::offloading::wrapSYCLBinaries(
99 M,
ArrayRef<char>(buffer->getBufferStart(), buffer->getBufferSize()),
100 llvm::offloading::SYCLJITOptions(),
true)) {
102 << llvm::toString(std::move(err));
111 mlir::ModuleOp MlirModule,
112 mlir::MLIRContext &MlirCtx) {
114 return mlir::success();
115 const bool EnableLibOpt =
132 mlir::ModuleOp MlirModule, llvm::LLVMContext &LLVMCtx,
135 std::unique_ptr<raw_pwrite_stream> OutputStream,
136 llvm::function_ref<
void(llvm::Module &)> AddFunctionSourceLocations = {}) {
140 if (OutputStream && MlirModule)
144 if (OutputStream && MlirModule &&
145 failed(mlir::writeBytecodeToFile(MlirModule, *OutputStream)) &&
154 std::string cirSaveTempsOutFile, mlirSaveTempsOutFile;
155 if (!saveTempsPrefix.empty()) {
157 llvm::sys::path::replace_extension(stem,
"cir");
158 cirSaveTempsOutFile = std::string(stem);
159 llvm::sys::path::replace_extension(stem,
"mlir");
160 mlirSaveTempsOutFile = std::string(stem);
163 if (!cirSaveTempsOutFile.empty()) {
165 llvm::raw_fd_ostream
out(cirSaveTempsOutFile, ec);
167 MlirModule->print(
out);
176 auto LangOptsAttr = MlirModule->getAttrOfType<cir::LoweringOptionsAttr>(
177 cir::CIRDialect::getLoweringOptionsAttrName());
178 const bool EnableOpenMP = LangOptsAttr && LangOptsAttr.getOpenmp();
183 std::unique_ptr<llvm::DiagnosticHandler> OldDiagnosticHandler =
184 LLVMCtx.getDiagnosticHandler();
185 llvm::scope_exit RestoreDiagnosticHandler([&]() {
186 LLVMCtx.setDiagnosticHandler(std::move(OldDiagnosticHandler));
197 std::unique_ptr<llvm::Module> LLVMModule =
200 CGO.OptimizationLevel > 0);
211 if (AddFunctionSourceLocations)
212 AddFunctionSourceLocations(*LLVMModule);
233 "ClangIR code gen Not Yet Implemented: embedding offload objects "
234 "for CUDA, HIP or OpenMP offloading"));
242 EmbedBitcode(LLVMModule.get(), CGO, llvm::MemoryBufferRef());
254 virtual void anchor();
260 std::unique_ptr<raw_pwrite_stream> OutputStream;
264 std::unique_ptr<CIRGenerator> Gen;
268 llvm::LLVMContext &LLVMCtx;
271 std::optional<CIRDiagnosticHandler> MLIRDiagHandler;
281 llvm::LLVMContext &LLVMCtx,
283 : Action(Action), CI(CI), OutputStream(
std::move(OS)),
284 FS(&CI.getVirtualFileSystem()),
286 CI.getCodeGenOpts())),
287 FEOptions(CI.getFrontendOpts()), CGO(CGO), LLVMCtx(LLVMCtx),
288 LinkModules(LinkModules),
289 DiagConsumer(CI.getDiagnostics(), CI.getCodeGenOpts()) {}
292 assert(!Context &&
"initialized multiple times");
295 Gen->Initialize(Ctx);
299 MLIRDiagHandler.emplace(&Gen->getMLIRContext(), CI.getDiagnostics(),
300 CI.getSourceManager(), CI.getFileManager());
304 Gen->HandleTopLevelDecl(D);
309 Gen->HandleCXXStaticMemberVarInstantiation(VD);
314 Gen->HandleOpenACCRoutineReference(FD, RD);
318 Gen->HandleInlineFunctionDefinition(D);
322 Gen->HandleTranslationUnit(
C);
324 if (!FEOptions.ClangIRDisableCIRVerifier) {
325 if (!Gen->verifyModule()) {
328 if (!CI.getDiagnostics().hasErrorOccurred())
329 CI.getDiagnostics().Report(
330 diag::err_cir_verification_failed_pre_passes);
331 llvm::report_fatal_error(
332 "CIR codegen: module verification error before running CIR passes");
337 mlir::ModuleOp MlirModule = Gen->getModule();
338 mlir::MLIRContext &MlirCtx = Gen->getMLIRContext();
340 if (
runCIRPipeline(FEOptions, CGO, MlirModule, MlirCtx).failed()) {
343 if (!CI.getDiagnostics().hasErrorOccurred())
344 CI.getDiagnostics().Report(diag::err_cir_to_cir_transform_failed);
350 auto AddFunctionSourceLocations = [&](llvm::Module &LLVMModule) {
351 for (llvm::Function &F : LLVMModule.functions())
352 if (
const Decl *FD = Gen->getDeclForMangledName(F.getName()))
353 DiagConsumer.addFunctionSourceLocation(
354 F.getName(), FD->getASTContext().getFullLoc(FD->getLocation()));
357 DiagConsumer, std::move(OutputStream),
358 AddFunctionSourceLocations);
363 Context->getSourceManager(),
364 "CIR generation of declaration");
365 Gen->HandleTagDeclDefinition(D);
369 Gen->HandleTagDeclRequiredDefinition(D);
373 Gen->CompleteTentativeDefinition(D);
380void CIRGenConsumer::anchor() {}
396static std::unique_ptr<raw_pwrite_stream>
416 std::optional<llvm::MemoryBufferRef> MainFile =
421 mlir::MLIRContext MLIRContext;
434 mlir::OwningOpRef<mlir::ModuleOp>
Module =
435 mlir::parseSourceString<mlir::ModuleOp>(MainFile->getBuffer(),
436 mlir::ParserConfig(&MLIRContext),
437 MainFile->getBufferIdentifier());
440 Diags.
Report(diag::err_invalid_cir);
446 auto ModuleTriple = mlir::dyn_cast_if_present<mlir::StringAttr>(
447 (*Module)->getAttr(cir::CIRDialect::getTripleAttrName()));
449 Diags.
Report(diag::err_cir_input_missing_triple);
453 if (ModuleTriple.getValue() != TargetTriple) {
454 Diags.
Report(diag::err_cir_input_triple_mismatch)
455 << ModuleTriple.getValue() << TargetTriple;
465 Diags.
Report(diag::err_cir_to_cir_transform_failed);
474static std::unique_ptr<raw_pwrite_stream>
491 llvm_unreachable(
"Invalid CIRGenAction::OutputType");
494std::unique_ptr<ASTConsumer>
501 auto Result = std::make_unique<cir::CIRGenConsumer>(
507void EmitAssemblyAction::anchor() {}
511void EmitCIRAction::anchor() {}
514void EmitCIRBCAction::anchor() {}
517void EmitLLVMAction::anchor() {}
520void EmitBCAction::anchor() {}
523void EmitObjAction::anchor() {}
Defines the clang::ASTContext interface.
static std::unique_ptr< raw_pwrite_stream > getOutputStream(CompilerInstance &CI, StringRef InFile, CIRGenAction::OutputType Action)
Routes MLIR-side diagnostics emitted during CIR passes, the verifier, and CIR-to-LLVM lowering throug...
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
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
CIRGenAction(OutputType Action)
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...
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.
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 ...
SourceManager & getSourceManager()
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer.
Owns the state needed to translate LLVM backend diagnostics into clang diagnostics,...
std::unique_ptr< llvm::DiagnosticHandler > createDiagnosticHandler()
Create an llvm::DiagnosticHandler that forwards diagnostics to this consumer.
void setSourceManager(SourceManager *SM)
The SourceManager used to translate backend-reported file:line:col locations back into clang SourceLo...
Represents a C++ struct/union/class.
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
std::string OffloadBinaryToEmbedFile
Name of file passed with -foffload-include-binary option to forward to offloading runtime back-end fo...
std::string SaveTempsFilePrefix
Prefix to use for -save-temps output.
std::vector< std::string > OffloadObjects
List of filenames passed in using the -fembed-offload-object option.
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...
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
FileManager & getFileManager() const
Return the current file manager to the caller.
const llvm::cl::LibraryOptionsContext & getLibraryOptions() const
IntrusiveRefCntPtr< llvm::vfs::FileSystem > getVirtualFileSystemPtr() const
TargetOptions & getTargetOpts()
std::unique_ptr< llvm::raw_pwrite_stream > takeOutputStream()
FrontendOptions & getFrontendOpts()
TargetInfo & getTarget() const
llvm::vfs::FileSystem & getVirtualFileSystem() const
LangOptions & getLangOpts()
CodeGenOptions & getCodeGenOpts()
SourceManager & getSourceManager() const
Return the current source manager.
Decl - This represents one declaration (or definition), e.g.
Concrete class used by the front-end to report problems and issues.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
unsigned getCustomDiagID(Level L, const char(&FormatString)[N])
Return an ID for a diagnostic with the specified format string and level.
bool hasErrorOccurred() const
InputKind getCurrentFileKind() const
virtual bool BeginSourceFileAction(CompilerInstance &CI)
Callback at the start of processing a single input.
CompilerInstance & getCompilerInstance() const
StringRef getCurrentFileOrBufferName() const
FrontendOptions - Options for controlling the behavior of the frontend.
unsigned ClangIRDisablePasses
Disable Clang IR specific (CIR) passes.
unsigned ClangIRDisableCIRVerifier
Disable Clang IR (CIR) verifier.
unsigned ClangIRLibOptEnabled
Enable ClangIR library optimization.
std::string ClangIRLibOptOptions
Options to control ClangIR library optimization.
unsigned ClangIREnableIdiomRecognizer
Enable Clang IR (CIR) idiom recognizer.
unsigned ClangIRCallConvLowering
Run the Clang IR (CIR) calling-convention lowering pass.
Represents a function declaration or definition.
std::vector< llvm::Triple > OMPTargetTriples
Triples of the OpenMP targets that the host code codegen should take into account in order to generat...
Describes a module or submodule.
PrettyStackTraceDecl - If a crash occurs, indicate that it happened when doing something to a specifi...
Encodes a location in the source.
This class handles loading and caching of source files into memory.
FileID getMainFileID() const
Returns the FileID of the main source file.
std::optional< llvm::MemoryBufferRef > getBufferOrNone(FileID FID, SourceLocation Loc=SourceLocation()) const
Return the buffer for the specified FileID.
Represents the declaration of a struct/union/class/enum.
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
const char * getDataLayoutString() const
Represents a variable declaration or definition.
std::unique_ptr< llvm::Module > lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp mlirModule, llvm::LLVMContext &llvmCtx, bool enableOpenMP, llvm::StringRef mlirSaveTempsOutFile={}, llvm::vfs::FileSystem *fs=nullptr, bool enableOptimizations=false)
Lowers mlirModule, a CIR module, to an LLVM module in llvmCtx.
static BackendAction getBackendActionFromOutputType(CIRGenAction::OutputType Action)
static void emitCIRModuleOutput(CIRGenAction::OutputType Action, CompilerInstance &CI, mlir::ModuleOp MlirModule, llvm::LLVMContext &LLVMCtx, SmallVectorImpl<::clang::LinkModule > &LinkModules, BackendDiagnosticConsumer &DiagConsumer, std::unique_ptr< raw_pwrite_stream > OutputStream, llvm::function_ref< void(llvm::Module &)> AddFunctionSourceLocations={})
mlir::LogicalResult runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext &mlirCtx, bool enableVerifier, bool enableIdiomRecognizer, bool enableCIRSimplify, bool enableLibOpt, llvm::StringRef libOptOptions, bool enableCallConvLowering)
static void printCIRModule(mlir::ModuleOp MLIRModule, raw_ostream &OS)
static std::unique_ptr< llvm::Module > lowerFromCIRToLLVMIR(mlir::ModuleOp MLIRModule, llvm::LLVMContext &LLVMCtx, bool EnableOpenMP, llvm::StringRef mlirSaveTempsOutFile={}, llvm::vfs::FileSystem *fs=nullptr, bool enableOptimizations=false)
void registerAllDialects(mlir::DialectRegistry ®istry)
static void embedSYCLDeviceBinary(CompilerInstance &CI, llvm::Module &M)
static mlir::LogicalResult runCIRPipeline(const FrontendOptions &FEOpts, const CodeGenOptions &CGO, mlir::ModuleOp MlirModule, mlir::MLIRContext &MlirCtx)
Top level wrappers for InstallAPI frontend operations.
bool loadLinkModules(CompilerInstance &CI, llvm::LLVMContext &Ctx, llvm::SmallVectorImpl< LinkModule > &LinkModules, bool OnlyInternalized)
Load every bitcode file listed in CodeGenOpts.LinkBitcodeFiles into LinkModules.
@ CIR
LLVM IR & CIR: we accept these so that we can run the optimizer on them, and compile them to assembly...
bool linkInModules(llvm::Module &M, llvm::SmallVectorImpl< LinkModule > &LinkModules, const CodeGenOptions &CodeGenOpts, const LangOptions &LangOpts, const TargetOptions &TargetOpts, BackendDiagnosticConsumer &DiagConsumer)
Link every module in LinkModules into M, propagating default function attributes and internalizing as...
void EmbedObject(llvm::Module *M, const CodeGenOptions &CGOpts, llvm::vfs::FileSystem &VFS, DiagnosticsEngine &Diags)
void emitBackendOutput(CompilerInstance &CI, CodeGenOptions &CGOpts, llvm::Module *M, BackendAction Action, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, std::unique_ptr< raw_pwrite_stream > OS, BackendConsumer *BC=nullptr)
void EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts, llvm::MemoryBufferRef Buf)
@ 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.
Diagnostic wrappers for TextAPI types for error reporting.