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"
@@ -201,8 +202,16 @@ class CIRGenConsumer : public clang::ASTConsumer {
201
202
if (feOptions.ClangIRLibOpt )
202
203
libOptOpts = sanitizePassOptions (feOptions.ClangIRLibOptOpts );
203
204
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;
206
215
207
216
// Setup and run CIR pipeline.
208
217
std::string passOptParsingFailure;
@@ -211,10 +220,8 @@ class CIRGenConsumer : public clang::ASTConsumer {
211
220
feOptions.ClangIRLifetimeCheck , lifetimeOpts,
212
221
feOptions.ClangIRIdiomRecognizer , idiomRecognizerOpts,
213
222
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 )
218
225
.failed ()) {
219
226
if (!passOptParsingFailure.empty ())
220
227
diagnosticsEngine.Report (diag::err_drv_cir_pass_opt_parsing)
@@ -260,25 +267,39 @@ class CIRGenConsumer : public clang::ASTConsumer {
260
267
}
261
268
}
262
269
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?" );
277
273
assert (outputStream && " Why are we here without an output stream?" );
278
274
// FIXME: we cannot roundtrip prettyForm=true right now.
279
275
mlir::OpPrintingFlags flags;
280
276
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
+ }
282
303
break ;
283
304
}
284
305
case CIRGenAction::OutputType::EmitLLVM:
@@ -356,10 +377,6 @@ getOutputStream(CompilerInstance &ci, StringRef inFile,
356
377
switch (action) {
357
378
case CIRGenAction::OutputType::EmitAssembly:
358
379
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" );
363
380
case CIRGenAction::OutputType::EmitMLIR:
364
381
return ci.createDefaultOutputFile (false , inFile, " mlir" );
365
382
case CIRGenAction::OutputType::EmitLLVM:
@@ -456,14 +473,6 @@ void EmitAssemblyAction::anchor() {}
456
473
EmitAssemblyAction::EmitAssemblyAction (mlir::MLIRContext *_MLIRContext)
457
474
: CIRGenAction(OutputType::EmitAssembly, _MLIRContext) {}
458
475
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
-
467
476
void EmitCIROnlyAction::anchor () {}
468
477
EmitCIROnlyAction::EmitCIROnlyAction (mlir::MLIRContext *_MLIRContext)
469
478
: CIRGenAction(OutputType::None, _MLIRContext) {}
0 commit comments