4747#include " llvm/LTO/LTOBackend.h"
4848#include " llvm/Linker/Linker.h"
4949#include " llvm/Pass.h"
50+ #include " llvm/ADT/SmallString.h"
5051#include " llvm/Support/MemoryBuffer.h"
5152#include " llvm/Support/Signals.h"
5253#include " llvm/Support/SourceMgr.h"
54+ #include " llvm/Support/Path.h"
5355#include " llvm/Support/TimeProfiler.h"
5456#include " llvm/Support/Timer.h"
5557#include " llvm/Support/ToolOutputFile.h"
@@ -118,6 +120,7 @@ class CIRGenConsumer : public clang::ASTConsumer {
118120 [[maybe_unused]] const LangOptions &langOptions;
119121 const FrontendOptions &feOptions;
120122
123+ std::string InputFileName;
121124 std::unique_ptr<raw_pwrite_stream> outputStream;
122125
123126 ASTContext *astContext{nullptr };
@@ -133,13 +136,14 @@ class CIRGenConsumer : public clang::ASTConsumer {
133136 CodeGenOptions &codeGenOptions,
134137 const TargetOptions &targetOptions,
135138 const LangOptions &langOptions,
136- const FrontendOptions &feOptions,
139+ const FrontendOptions &feOptions, StringRef InputFile,
137140 std::unique_ptr<raw_pwrite_stream> os)
138141 : action(action), compilerInstance(compilerInstance),
139142 diagnosticsEngine (diagnosticsEngine),
140143 headerSearchOptions(headerSearchOptions),
141144 codeGenOptions(codeGenOptions), targetOptions(targetOptions),
142145 langOptions(langOptions), feOptions(feOptions),
146+ InputFileName(InputFile.str()),
143147 outputStream(std::move(os)), FS(VFS),
144148 gen(std::make_unique<CIRGenerator>(diagnosticsEngine, std::move(VFS),
145149 codeGenOptions)) {}
@@ -265,6 +269,50 @@ class CIRGenConsumer : public clang::ASTConsumer {
265269 }
266270 }
267271
272+ bool EmitCIR = langOptions.EmitCIRToFile || feOptions.EmitClangIRFile ||
273+ !langOptions.CIRFile .empty () ||
274+ !feOptions.ClangIRFile .empty ();
275+ if (EmitCIR) {
276+ std::unique_ptr<raw_pwrite_stream> CIRStream;
277+ llvm::SmallString<128 > DefaultPath;
278+ if (!feOptions.ClangIRFile .empty ()) {
279+ CIRStream = compilerInstance.createOutputFile (
280+ feOptions.ClangIRFile ,
281+ /* Binary=*/ false ,
282+ /* RemoveFileOnSignal=*/ true ,
283+ /* UseTemporary=*/ true );
284+ } else if (!langOptions.CIRFile .empty ()) {
285+ CIRStream = compilerInstance.createOutputFile (
286+ langOptions.CIRFile ,
287+ /* Binary=*/ false ,
288+ /* RemoveFileOnSignal=*/ true ,
289+ /* UseTemporary=*/ true );
290+ } else {
291+ if (!feOptions.OutputFile .empty () && feOptions.OutputFile != " -" ) {
292+ DefaultPath = feOptions.OutputFile ;
293+ } else if (!InputFileName.empty () && InputFileName != " -" ) {
294+ DefaultPath = InputFileName;
295+ } else if (!feOptions.Inputs .empty () && feOptions.Inputs [0 ].isFile () &&
296+ feOptions.Inputs [0 ].getFile () != " -" ) {
297+ DefaultPath = feOptions.Inputs [0 ].getFile ();
298+ } else {
299+ DefaultPath = " clangir-output" ;
300+ }
301+ llvm::sys::path::replace_extension (DefaultPath, " cir" );
302+ CIRStream = compilerInstance.createOutputFile (
303+ DefaultPath,
304+ /* Binary=*/ false ,
305+ /* RemoveFileOnSignal=*/ true ,
306+ /* UseTemporary=*/ true );
307+ }
308+
309+ if (CIRStream) {
310+ mlir::OpPrintingFlags Flags;
311+ Flags.enableDebugInfo (/* enable=*/ true , /* prettyForm=*/ false );
312+ mlirMod->print (*CIRStream, Flags);
313+ }
314+ }
315+
268316 auto emitMLIR = [&](mlir::Operation *mlirMod, bool verify) {
269317 assert (mlirMod &&
270318 " MLIR module does not exist, but lowering did not fail?" );
@@ -399,7 +447,7 @@ CIRGenAction::CreateASTConsumer(CompilerInstance &ci, StringRef inputFile) {
399447 auto Result = std::make_unique<cir::CIRGenConsumer>(
400448 action, ci, ci.getDiagnostics (), &ci.getVirtualFileSystem (),
401449 ci.getHeaderSearchOpts (), ci.getCodeGenOpts (), ci.getTargetOpts (),
402- ci.getLangOpts (), ci.getFrontendOpts (), std::move (out));
450+ ci.getLangOpts (), ci.getFrontendOpts (), inputFile, std::move (out));
403451 cgConsumer = Result.get ();
404452
405453 // Enable generating macro debug info only when debug info is not disabled and
@@ -507,7 +555,8 @@ AnalysisOnlyActionBase::CreateASTConsumer(clang::CompilerInstance &ci,
507555 Consumers.push_back (std::make_unique<cir::CIRGenConsumer>(
508556 CIRGenAction::OutputType::None, ci, ci.getDiagnostics (),
509557 &ci.getVirtualFileSystem (), ci.getHeaderSearchOpts (), ci.getCodeGenOpts (),
510- ci.getTargetOpts (), ci.getLangOpts (), ci.getFrontendOpts (), nullptr ));
558+ ci.getTargetOpts (), ci.getLangOpts (), ci.getFrontendOpts (), inFile,
559+ nullptr ));
511560 return std::make_unique<MultiplexConsumer>(std::move (Consumers));
512561}
513562
0 commit comments