Skip to content

Commit 6870e41

Browse files
committed
[CIR] Add option to emit MLIR in LLVM dialect.
1 parent 5373f42 commit 6870e41

File tree

10 files changed

+96
-19
lines changed

10 files changed

+96
-19
lines changed

clang/include/clang/CIR/FrontendAction/CIRGenAction.h

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class CIRGenAction : public clang::ASTFrontendAction {
3737
EmitLLVM,
3838
EmitBC,
3939
EmitMLIR,
40+
EmitMLIRLLVM,
4041
EmitObj,
4142
None
4243
};
@@ -101,6 +102,13 @@ class EmitMLIRAction : public CIRGenAction {
101102
EmitMLIRAction(mlir::MLIRContext *mlirCtx = nullptr);
102103
};
103104

105+
class EmitMLIRLLVMAction : public CIRGenAction {
106+
virtual void anchor();
107+
108+
public:
109+
EmitMLIRLLVMAction(mlir::MLIRContext *mlirCtx = nullptr);
110+
};
111+
104112
class EmitLLVMAction : public CIRGenAction {
105113
virtual void anchor();
106114

clang/include/clang/CIR/LowerToLLVM.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,19 @@ class ModuleOp;
2929
namespace cir {
3030

3131
namespace direct {
32+
mlir::ModuleOp lowerDirectlyFromCIRToLLVMDialect(mlir::ModuleOp theModule,
33+
bool disableVerifier = false,
34+
bool disableCCLowering = false,
35+
bool disableDebugInfo = false);
36+
37+
// Lower directly from pristine CIR to LLVMIR.
3238
std::unique_ptr<llvm::Module> lowerDirectlyFromCIRToLLVMIR(
3339
mlir::ModuleOp theModule, llvm::LLVMContext &llvmCtx,
3440
bool disableVerifier = false, bool disableCCLowering = false,
3541
bool disableDebugInfo = false);
36-
}
3742

38-
// Lower directly from pristine CIR to LLVMIR.
43+
} // namespace direct
44+
3945
std::unique_ptr<llvm::Module>
4046
lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule,
4147
std::unique_ptr<mlir::MLIRContext> mlirCtx,

clang/include/clang/CIR/LowerToMLIR.h

+6
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@
1212
#ifndef CLANG_CIR_LOWERTOMLIR_H
1313
#define CLANG_CIR_LOWERTOMLIR_H
1414

15+
#include "mlir/Transforms/DialectConversion.h"
16+
1517
namespace cir {
1618

1719
void populateCIRLoopToSCFConversionPatterns(mlir::RewritePatternSet &patterns,
1820
mlir::TypeConverter &converter);
21+
22+
mlir::ModuleOp
23+
lowerFromCIRToMLIRToLLVMDialect(mlir::ModuleOp theModule,
24+
mlir::MLIRContext *mlirCtx = nullptr);
1925
} // namespace cir
2026

2127
#endif // CLANG_CIR_LOWERTOMLIR_H_

clang/include/clang/Driver/Options.td

+3-1
Original file line numberDiff line numberDiff line change
@@ -3120,7 +3120,9 @@ def emit_cir_only : Flag<["-"], "emit-cir-only">,
31203120
def emit_cir_flat : Flag<["-"], "emit-cir-flat">, Visibility<[ClangOption, CC1Option]>,
31213121
Group<Action_Group>, HelpText<"Similar to -emit-cir but also lowers structured CFG into basic blocks.">;
31223122
def emit_mlir : Flag<["-"], "emit-mlir">, Visibility<[CC1Option]>, Group<Action_Group>,
3123-
HelpText<"Build ASTs and then lower through ClangIR to MLIR, emit the .milr file">;
3123+
HelpText<"Build ASTs and then lower through ClangIR to MLIR standard dialects, emit the .mlir file">;
3124+
def emit_mlir_llvm : Flag<["-"], "emit-mlir-llvm">, Visibility<[CC1Option]>, Group<Action_Group>,
3125+
HelpText<"Build ASTs and then lower through ClangIR to MLIR LLVM Dialect, emit the .mlir file">;
31243126
/// ClangIR-specific options - END
31253127

31263128
def flto : Flag<["-"], "flto">,

clang/include/clang/Frontend/FrontendOptions.h

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ enum ActionKind {
7777
/// Emit a .mlir file
7878
EmitMLIR,
7979

80+
/// Emit an .mlir file with LLVM dialect
81+
EmitMLIRLLVM,
82+
8083
/// Emit a .ll file.
8184
EmitLLVM,
8285

clang/lib/CIR/FrontendAction/CIRGenAction.cpp

+26-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
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"
@@ -260,6 +261,14 @@ class CIRGenConsumer : public clang::ASTConsumer {
260261
}
261262
}
262263

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+
263272
switch (action) {
264273
case CIRGenAction::OutputType::EmitCIR:
265274
case CIRGenAction::OutputType::EmitCIRFlat:
@@ -274,11 +283,17 @@ class CIRGenConsumer : public clang::ASTConsumer {
274283
break;
275284
case CIRGenAction::OutputType::EmitMLIR: {
276285
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+
bool disableDebugInfo =
291+
codeGenOptions.getDebugInfo() == llvm::codegenoptions::NoDebugInfo;
292+
auto loweredMlirModule =
293+
feOptions.ClangIRDirectLowering
294+
? direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod)
295+
: lowerFromCIRToMLIRToLLVMDialect(mlirMod);
296+
printMlirModule(loweredMlirModule);
282297
break;
283298
}
284299
case CIRGenAction::OutputType::EmitLLVM:
@@ -362,6 +377,8 @@ getOutputStream(CompilerInstance &ci, StringRef inFile,
362377
return ci.createDefaultOutputFile(false, inFile, "cir");
363378
case CIRGenAction::OutputType::EmitMLIR:
364379
return ci.createDefaultOutputFile(false, inFile, "mlir");
380+
case CIRGenAction::OutputType::EmitMLIRLLVM:
381+
return ci.createDefaultOutputFile(false, inFile, "mlir");
365382
case CIRGenAction::OutputType::EmitLLVM:
366383
return ci.createDefaultOutputFile(false, inFile, "ll");
367384
case CIRGenAction::OutputType::EmitBC:
@@ -472,6 +489,10 @@ void EmitMLIRAction::anchor() {}
472489
EmitMLIRAction::EmitMLIRAction(mlir::MLIRContext *_MLIRContext)
473490
: CIRGenAction(OutputType::EmitMLIR, _MLIRContext) {}
474491

492+
void EmitMLIRLLVMAction::anchor() {}
493+
EmitMLIRLLVMAction::EmitMLIRLLVMAction(mlir::MLIRContext *_MLIRContext)
494+
: CIRGenAction(OutputType::EmitMLIRLLVM, _MLIRContext) {}
495+
475496
void EmitLLVMAction::anchor() {}
476497
EmitLLVMAction::EmitLLVMAction(mlir::MLIRContext *_MLIRContext)
477498
: CIRGenAction(OutputType::EmitLLVM, _MLIRContext) {}

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

+19-5
Original file line numberDiff line numberDiff line change
@@ -4689,11 +4689,11 @@ void populateCIRToLLVMPasses(mlir::OpPassManager &pm, bool useCCLowering) {
46894689

46904690
extern void registerCIRDialectTranslation(mlir::MLIRContext &context);
46914691

4692-
std::unique_ptr<llvm::Module>
4693-
lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx,
4694-
bool disableVerifier, bool disableCCLowering,
4695-
bool disableDebugInfo) {
4696-
llvm::TimeTraceScope scope("lower from CIR to LLVM directly");
4692+
mlir::ModuleOp lowerDirectlyFromCIRToLLVMDialect(mlir::ModuleOp theModule,
4693+
bool disableVerifier,
4694+
bool disableCCLowering,
4695+
bool disableDebugInfo) {
4696+
llvm::TimeTraceScope scope("lower from CIR to LLVM Dialect");
46974697

46984698
mlir::MLIRContext *mlirCtx = theModule.getContext();
46994699
mlir::PassManager pm(mlirCtx);
@@ -4722,6 +4722,20 @@ lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx,
47224722
if (theModule.verify().failed())
47234723
report_fatal_error("Verification of the final LLVMIR dialect failed!");
47244724

4725+
return theModule;
4726+
}
4727+
4728+
std::unique_ptr<llvm::Module>
4729+
lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx,
4730+
bool disableVerifier, bool disableCCLowering,
4731+
bool disableDebugInfo) {
4732+
llvm::TimeTraceScope scope("lower from CIR to LLVM directly");
4733+
4734+
lowerDirectlyFromCIRToLLVMDialect(theModule, disableVerifier,
4735+
disableCCLowering, disableDebugInfo);
4736+
4737+
mlir::MLIRContext *mlirCtx = theModule.getContext();
4738+
47254739
mlir::registerBuiltinDialectTranslation(*mlirCtx);
47264740
mlir::registerLLVMDialectTranslation(*mlirCtx);
47274741
mlir::registerOpenMPDialectTranslation(*mlirCtx);

clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp

+19-6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "mlir/Transforms/DialectConversion.h"
4848
#include "clang/CIR/Dialect/IR/CIRDialect.h"
4949
#include "clang/CIR/Dialect/IR/CIRTypes.h"
50+
#include "clang/CIR/LowerToLLVM.h"
5051
#include "clang/CIR/LowerToMLIR.h"
5152
#include "clang/CIR/LoweringHelpers.h"
5253
#include "clang/CIR/Passes.h"
@@ -1458,13 +1459,14 @@ void ConvertCIRToMLIRPass::runOnOperation() {
14581459
signalPassFailure();
14591460
}
14601461

1461-
std::unique_ptr<llvm::Module>
1462-
lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule,
1463-
std::unique_ptr<mlir::MLIRContext> mlirCtx,
1464-
LLVMContext &llvmCtx) {
1465-
llvm::TimeTraceScope scope("Lower from CIR to MLIR To LLVM");
1462+
mlir::ModuleOp lowerFromCIRToMLIRToLLVMDialect(mlir::ModuleOp theModule,
1463+
mlir::MLIRContext *mlirCtx) {
1464+
llvm::TimeTraceScope scope("Lower from CIR to MLIR To LLVM Dialect");
1465+
if (!mlirCtx) {
1466+
mlirCtx = theModule.getContext();
1467+
}
14661468

1467-
mlir::PassManager pm(mlirCtx.get());
1469+
mlir::PassManager pm(mlirCtx);
14681470

14691471
pm.addPass(createConvertCIRToMLIRPass());
14701472
pm.addPass(createConvertMLIRToLLVMPass());
@@ -1478,6 +1480,17 @@ lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule,
14781480
if (theModule.verify().failed())
14791481
report_fatal_error("Verification of the final LLVMIR dialect failed!");
14801482

1483+
return theModule;
1484+
}
1485+
1486+
std::unique_ptr<llvm::Module>
1487+
lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule,
1488+
std::unique_ptr<mlir::MLIRContext> mlirCtx,
1489+
LLVMContext &llvmCtx) {
1490+
llvm::TimeTraceScope scope("Lower from CIR to MLIR To LLVM");
1491+
1492+
lowerFromCIRToMLIRToLLVMDialect(theModule, mlirCtx.get());
1493+
14811494
mlir::registerBuiltinDialectTranslation(*mlirCtx);
14821495
mlir::registerLLVMDialectTranslation(*mlirCtx);
14831496
mlir::registerOpenMPDialectTranslation(*mlirCtx);

clang/lib/Frontend/CompilerInvocation.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -2738,6 +2738,7 @@ static const auto &getFrontendActionTable() {
27382738
{frontend::EmitCIRFlat, OPT_emit_cir_flat},
27392739
{frontend::EmitCIROnly, OPT_emit_cir_only},
27402740
{frontend::EmitMLIR, OPT_emit_mlir},
2741+
{frontend::EmitMLIRLLVM, OPT_emit_mlir_llvm},
27412742
{frontend::EmitHTML, OPT_emit_html},
27422743
{frontend::EmitLLVM, OPT_emit_llvm},
27432744
{frontend::EmitLLVMOnly, OPT_emit_llvm_only},
@@ -4669,6 +4670,7 @@ static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) {
46694670
case frontend::EmitCIRFlat:
46704671
case frontend::EmitCIROnly:
46714672
case frontend::EmitMLIR:
4673+
case frontend::EmitMLIRLLVM:
46724674
case frontend::EmitHTML:
46734675
case frontend::EmitLLVM:
46744676
case frontend::EmitLLVMOnly:

clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ CreateFrontendBaseAction(CompilerInstance &CI) {
9999
return std::make_unique<cir::EmitCIROnlyAction>();
100100
case EmitMLIR:
101101
return std::make_unique<cir::EmitMLIRAction>();
102+
case EmitMLIRLLVM:
103+
return std::make_unique<cir::EmitMLIRLLVMAction>();
102104
#else
103105
case EmitCIR:
104106
case EmitCIRFlat:

0 commit comments

Comments
 (0)