2626#include " clang/CIR/CIRToCIRPasses.h"
2727#include " clang/CIR/Dialect/IR/CIRDialect.h"
2828#include " clang/CIR/LowerToLLVM.h"
29+ #include " clang/CIR/LowerToMLIR.h"
2930#include " clang/CIR/Passes.h"
3031#include " clang/CodeGen/BackendUtil.h"
3132#include " clang/CodeGen/ModuleBuilder.h"
@@ -201,8 +202,16 @@ class CIRGenConsumer : public clang::ASTConsumer {
201202 if (feOptions.ClangIRLibOpt )
202203 libOptOpts = sanitizePassOptions (feOptions.ClangIRLibOptOpts );
203204
204- bool enableCCLowering = feOptions.ClangIRCallConvLowering &&
205- action != CIRGenAction::OutputType::EmitCIR;
205+ bool enableCCLowering =
206+ feOptions.ClangIRCallConvLowering &&
207+ !(action == CIRGenAction::OutputType::EmitMLIR &&
208+ feOptions.MLIRTargetDialect == frontend::MLIR_CIR);
209+ bool flattenCIR =
210+ action == CIRGenAction::OutputType::EmitMLIR &&
211+ feOptions.MLIRTargetDialect == clang::frontend::MLIR_CIR_FLAT;
212+
213+ bool emitCore = action == CIRGenAction::OutputType::EmitMLIR &&
214+ feOptions.MLIRTargetDialect == clang::frontend::MLIR_CORE;
206215
207216 // Setup and run CIR pipeline.
208217 std::string passOptParsingFailure;
@@ -211,10 +220,8 @@ class CIRGenConsumer : public clang::ASTConsumer {
211220 feOptions.ClangIRLifetimeCheck , lifetimeOpts,
212221 feOptions.ClangIRIdiomRecognizer , idiomRecognizerOpts,
213222 feOptions.ClangIRLibOpt , libOptOpts, passOptParsingFailure,
214- codeGenOptions.OptimizationLevel > 0 ,
215- action == CIRGenAction::OutputType::EmitCIRFlat,
216- action == CIRGenAction::OutputType::EmitMLIR, enableCCLowering,
217- feOptions.ClangIREnableMem2Reg )
223+ codeGenOptions.OptimizationLevel > 0 , flattenCIR, emitCore,
224+ enableCCLowering, feOptions.ClangIREnableMem2Reg )
218225 .failed ()) {
219226 if (!passOptParsingFailure.empty ())
220227 diagnosticsEngine.Report (diag::err_drv_cir_pass_opt_parsing)
@@ -260,25 +267,39 @@ class CIRGenConsumer : public clang::ASTConsumer {
260267 }
261268 }
262269
263- switch (action) {
264- case CIRGenAction::OutputType::EmitCIR:
265- case CIRGenAction::OutputType::EmitCIRFlat:
266- if (outputStream && mlirMod) {
267- // FIXME: we cannot roundtrip prettyForm=true right now.
268- mlir::OpPrintingFlags flags;
269- flags.enableDebugInfo (/* enable=*/ true , /* prettyForm=*/ false );
270- if (feOptions.ClangIRDisableCIRVerifier )
271- flags.assumeVerified ();
272- mlirMod->print (*outputStream, flags);
273- }
274- break ;
275- case CIRGenAction::OutputType::EmitMLIR: {
276- auto loweredMlirModule = lowerFromCIRToMLIR (mlirMod, mlirCtx.get ());
270+ auto emitMLIR = [&](mlir::Operation *mlirMod, bool verify) {
271+ assert (mlirMod &&
272+ " MLIR module does not exist, but lowering did not fail?" );
277273 assert (outputStream && " Why are we here without an output stream?" );
278274 // FIXME: we cannot roundtrip prettyForm=true right now.
279275 mlir::OpPrintingFlags flags;
280276 flags.enableDebugInfo (/* enable=*/ true , /* prettyForm=*/ false );
281- loweredMlirModule->print (*outputStream, flags);
277+ if (!verify)
278+ flags.assumeVerified ();
279+ mlirMod->print (*outputStream, flags);
280+ };
281+
282+ switch (action) {
283+ case CIRGenAction::OutputType::EmitMLIR: {
284+ switch (feOptions.MLIRTargetDialect ) {
285+ case clang::frontend::MLIR_CORE:
286+ // case for direct lowering is already checked in compiler invocation
287+ // no need to check here
288+ emitMLIR (lowerFromCIRToMLIR (mlirMod, mlirCtx.get ()), false );
289+ break ;
290+ case clang::frontend::MLIR_LLVM: {
291+ mlir::ModuleOp loweredMLIRModule =
292+ feOptions.ClangIRDirectLowering
293+ ? direct::lowerDirectlyFromCIRToLLVMDialect (mlirMod)
294+ : lowerFromCIRToMLIRToLLVMDialect (mlirMod, mlirCtx.get ());
295+ emitMLIR (loweredMLIRModule, false );
296+ break ;
297+ }
298+ case clang::frontend::MLIR_CIR:
299+ case clang::frontend::MLIR_CIR_FLAT:
300+ emitMLIR (mlirMod, feOptions.ClangIRDisableCIRVerifier );
301+ break ;
302+ }
282303 break ;
283304 }
284305 case CIRGenAction::OutputType::EmitLLVM:
@@ -356,10 +377,6 @@ getOutputStream(CompilerInstance &ci, StringRef inFile,
356377 switch (action) {
357378 case CIRGenAction::OutputType::EmitAssembly:
358379 return ci.createDefaultOutputFile (false , inFile, " s" );
359- case CIRGenAction::OutputType::EmitCIR:
360- return ci.createDefaultOutputFile (false , inFile, " cir" );
361- case CIRGenAction::OutputType::EmitCIRFlat:
362- return ci.createDefaultOutputFile (false , inFile, " cir" );
363380 case CIRGenAction::OutputType::EmitMLIR:
364381 return ci.createDefaultOutputFile (false , inFile, " mlir" );
365382 case CIRGenAction::OutputType::EmitLLVM:
@@ -456,14 +473,6 @@ void EmitAssemblyAction::anchor() {}
456473EmitAssemblyAction::EmitAssemblyAction (mlir::MLIRContext *_MLIRContext)
457474 : CIRGenAction(OutputType::EmitAssembly, _MLIRContext) {}
458475
459- void EmitCIRAction::anchor () {}
460- EmitCIRAction::EmitCIRAction (mlir::MLIRContext *_MLIRContext)
461- : CIRGenAction(OutputType::EmitCIR, _MLIRContext) {}
462-
463- void EmitCIRFlatAction::anchor () {}
464- EmitCIRFlatAction::EmitCIRFlatAction (mlir::MLIRContext *_MLIRContext)
465- : CIRGenAction(OutputType::EmitCIRFlat, _MLIRContext) {}
466-
467476void EmitCIROnlyAction::anchor () {}
468477EmitCIROnlyAction::EmitCIROnlyAction (mlir::MLIRContext *_MLIRContext)
469478 : CIRGenAction(OutputType::None, _MLIRContext) {}
0 commit comments