26
26
#include " clang/CIR/CIRToCIRPasses.h"
27
27
#include " clang/CIR/Dialect/IR/CIRDialect.h"
28
28
#include " clang/CIR/LowerToLLVM.h"
29
+ #include " clang/CIR/LowerToMLIR.h"
29
30
#include " clang/CIR/Passes.h"
30
31
#include " clang/CodeGen/BackendUtil.h"
31
32
#include " clang/CodeGen/ModuleBuilder.h"
@@ -260,6 +261,14 @@ class CIRGenConsumer : public clang::ASTConsumer {
260
261
}
261
262
}
262
263
264
+ auto printMlirModule = [&](mlir::ModuleOp root) {
265
+ assert (outputStream && " Why are we here without an output stream?" );
266
+ // FIXME: we cannot roundtrip prettyForm=true right now.
267
+ mlir::OpPrintingFlags flags;
268
+ flags.enableDebugInfo (/* enable=*/ true , /* prettyForm=*/ false );
269
+ root->print (*outputStream, flags);
270
+ };
271
+
263
272
switch (action) {
264
273
case CIRGenAction::OutputType::EmitCIR:
265
274
case CIRGenAction::OutputType::EmitCIRFlat:
@@ -274,11 +283,15 @@ class CIRGenConsumer : public clang::ASTConsumer {
274
283
break ;
275
284
case CIRGenAction::OutputType::EmitMLIR: {
276
285
auto loweredMlirModule = lowerFromCIRToMLIR (mlirMod, mlirCtx.get ());
277
- assert (outputStream && " Why are we here without an output stream?" );
278
- // FIXME: we cannot roundtrip prettyForm=true right now.
279
- mlir::OpPrintingFlags flags;
280
- flags.enableDebugInfo (/* enable=*/ true , /* prettyForm=*/ false );
281
- loweredMlirModule->print (*outputStream, flags);
286
+ printMlirModule (loweredMlirModule);
287
+ break ;
288
+ }
289
+ case CIRGenAction::OutputType::EmitMLIRLLVM: {
290
+ auto loweredMlirModule =
291
+ feOptions.ClangIRDirectLowering
292
+ ? direct::lowerDirectlyFromCIRToLLVMDialect (mlirMod)
293
+ : lowerFromCIRToMLIRToLLVMDialect (mlirMod);
294
+ printMlirModule (loweredMlirModule);
282
295
break ;
283
296
}
284
297
case CIRGenAction::OutputType::EmitLLVM:
@@ -362,6 +375,8 @@ getOutputStream(CompilerInstance &ci, StringRef inFile,
362
375
return ci.createDefaultOutputFile (false , inFile, " cir" );
363
376
case CIRGenAction::OutputType::EmitMLIR:
364
377
return ci.createDefaultOutputFile (false , inFile, " mlir" );
378
+ case CIRGenAction::OutputType::EmitMLIRLLVM:
379
+ return ci.createDefaultOutputFile (false , inFile, " mlir" );
365
380
case CIRGenAction::OutputType::EmitLLVM:
366
381
return ci.createDefaultOutputFile (false , inFile, " ll" );
367
382
case CIRGenAction::OutputType::EmitBC:
@@ -472,6 +487,10 @@ void EmitMLIRAction::anchor() {}
472
487
EmitMLIRAction::EmitMLIRAction (mlir::MLIRContext *_MLIRContext)
473
488
: CIRGenAction(OutputType::EmitMLIR, _MLIRContext) {}
474
489
490
+ void EmitMLIRLLVMAction::anchor () {}
491
+ EmitMLIRLLVMAction::EmitMLIRLLVMAction (mlir::MLIRContext *_MLIRContext)
492
+ : CIRGenAction(OutputType::EmitMLIRLLVM, _MLIRContext) {}
493
+
475
494
void EmitLLVMAction::anchor () {}
476
495
EmitLLVMAction::EmitLLVMAction (mlir::MLIRContext *_MLIRContext)
477
496
: CIRGenAction(OutputType::EmitLLVM, _MLIRContext) {}
0 commit comments