Skip to content

Commit 5d29c0b

Browse files
committed
[CIR] Remove -emit-mlir-llvm in favour of -emit-mlir=.
The -emit-mlir option remains and produces llvm dialect for direct lowering and std dialect for indirect lowering. The -emit-mlir=<std,llvm,cir> emits the dialect corresponding to the value.
1 parent 3af1d49 commit 5d29c0b

File tree

6 files changed

+64
-48
lines changed

6 files changed

+64
-48
lines changed

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

-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class CIRGenAction : public clang::ASTFrontendAction {
3737
EmitLLVM,
3838
EmitBC,
3939
EmitMLIR,
40-
EmitMLIRLLVM,
4140
EmitObj,
4241
None
4342
};
@@ -102,13 +101,6 @@ class EmitMLIRAction : public CIRGenAction {
102101
EmitMLIRAction(mlir::MLIRContext *mlirCtx = nullptr);
103102
};
104103

105-
class EmitMLIRLLVMAction : public CIRGenAction {
106-
virtual void anchor();
107-
108-
public:
109-
EmitMLIRLLVMAction(mlir::MLIRContext *mlirCtx = nullptr);
110-
};
111-
112104
class EmitLLVMAction : public CIRGenAction {
113105
virtual void anchor();
114106

clang/include/clang/Driver/Options.td

+7-3
Original file line numberDiff line numberDiff line change
@@ -3120,9 +3120,13 @@ 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">;
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">;
3123+
HelpText<"Build ASTs and then lower through ClangIR to MLIR (standard dialects or llvm), emit the .mlir file">;
3124+
def emit_mlir_EQ : Joined<["-"], "emit-mlir=">, Visibility<[CC1Option]>, Group<Action_Group>,
3125+
HelpText<"Build ASTs and then lower through ClangIR to the selected MLIR dialect, emit the .mlir file">,
3126+
Values<"std,llvm,cir">,
3127+
NormalizedValuesScope<"FrontendOptions">,
3128+
NormalizedValues<["MLIR_STD", "MLIR_LLVM", "MLIR_CIR"]>,
3129+
MarshallingInfoEnum<FrontendOpts<"MLIRTargetDialect">, "MLIR_Default">;
31263130
/// ClangIR-specific options - END
31273131

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

clang/include/clang/Frontend/FrontendOptions.h

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

80-
/// Emit an .mlir file with LLVM dialect
81-
EmitMLIRLLVM,
82-
8380
/// Emit a .ll file.
8481
EmitLLVM,
8582

@@ -533,6 +530,13 @@ class FrontendOptions {
533530
std::string ClangIRIdiomRecognizerOpts;
534531
std::string ClangIRLibOptOpts;
535532

533+
enum {
534+
MLIR_Default,
535+
MLIR_STD,
536+
MLIR_LLVM,
537+
MLIR_CIR
538+
} MLIRTargetDialect = MLIR_Default;
539+
536540
/// The input kind, either specified via -x argument or deduced from the input
537541
/// file name.
538542
InputKind DashX;

clang/lib/CIR/FrontendAction/CIRGenAction.cpp

+38-27
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,7 @@ class CIRGenConsumer : public clang::ASTConsumer {
261261
}
262262
}
263263

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-
272-
switch (action) {
273-
case CIRGenAction::OutputType::EmitCIR:
274-
case CIRGenAction::OutputType::EmitCIRFlat:
264+
auto emitCIR = [&]() {
275265
if (outputStream && mlirMod) {
276266
// FIXME: we cannot roundtrip prettyForm=true right now.
277267
mlir::OpPrintingFlags flags;
@@ -280,18 +270,45 @@ class CIRGenConsumer : public clang::ASTConsumer {
280270
flags.assumeVerified();
281271
mlirMod->print(*outputStream, flags);
282272
}
273+
};
274+
275+
switch (action) {
276+
case CIRGenAction::OutputType::EmitCIR:
277+
case CIRGenAction::OutputType::EmitCIRFlat:
278+
emitCIR();
283279
break;
284280
case CIRGenAction::OutputType::EmitMLIR: {
285-
auto loweredMlirModule = lowerFromCIRToMLIR(mlirMod, mlirCtx.get());
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);
281+
mlir::ModuleOp loweredMlirModule;
282+
switch (feOptions.MLIRTargetDialect) {
283+
case clang::FrontendOptions::MLIR_Default:
284+
loweredMlirModule =
285+
feOptions.ClangIRDirectLowering
286+
? direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod)
287+
: lowerFromCIRToMLIR(mlirMod, mlirCtx.get());
288+
289+
break;
290+
case clang::FrontendOptions::MLIR_STD:
291+
// Attempting to emit std with direct lowering is already checked by
292+
// Compiler Invocation
293+
loweredMlirModule = lowerFromCIRToMLIR(mlirMod, mlirCtx.get());
294+
break;
295+
case clang::FrontendOptions::MLIR_LLVM:
296+
loweredMlirModule =
297+
feOptions.ClangIRDirectLowering
298+
? direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod)
299+
: lowerFromCIRToMLIRToLLVMDialect(mlirMod, mlirCtx.get());
300+
break;
301+
case clang::FrontendOptions::MLIR_CIR:
302+
emitCIR();
303+
break;
304+
}
305+
if (loweredMlirModule) {
306+
assert(outputStream && "Why are we here without an output stream?");
307+
// FIXME: we cannot roundtrip prettyForm=true right now.
308+
mlir::OpPrintingFlags flags;
309+
flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false);
310+
loweredMlirModule->print(*outputStream, flags);
311+
}
295312
break;
296313
}
297314
case CIRGenAction::OutputType::EmitLLVM:
@@ -375,8 +392,6 @@ getOutputStream(CompilerInstance &ci, StringRef inFile,
375392
return ci.createDefaultOutputFile(false, inFile, "cir");
376393
case CIRGenAction::OutputType::EmitMLIR:
377394
return ci.createDefaultOutputFile(false, inFile, "mlir");
378-
case CIRGenAction::OutputType::EmitMLIRLLVM:
379-
return ci.createDefaultOutputFile(false, inFile, "mlir");
380395
case CIRGenAction::OutputType::EmitLLVM:
381396
return ci.createDefaultOutputFile(false, inFile, "ll");
382397
case CIRGenAction::OutputType::EmitBC:
@@ -487,10 +502,6 @@ void EmitMLIRAction::anchor() {}
487502
EmitMLIRAction::EmitMLIRAction(mlir::MLIRContext *_MLIRContext)
488503
: CIRGenAction(OutputType::EmitMLIR, _MLIRContext) {}
489504

490-
void EmitMLIRLLVMAction::anchor() {}
491-
EmitMLIRLLVMAction::EmitMLIRLLVMAction(mlir::MLIRContext *_MLIRContext)
492-
: CIRGenAction(OutputType::EmitMLIRLLVM, _MLIRContext) {}
493-
494505
void EmitLLVMAction::anchor() {}
495506
EmitLLVMAction::EmitLLVMAction(mlir::MLIRContext *_MLIRContext)
496507
: CIRGenAction(OutputType::EmitLLVM, _MLIRContext) {}

clang/lib/Frontend/CompilerInvocation.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -2738,7 +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},
2741+
{frontend::EmitMLIR, OPT_emit_mlir_EQ},
27422742
{frontend::EmitHTML, OPT_emit_html},
27432743
{frontend::EmitLLVM, OPT_emit_llvm},
27442744
{frontend::EmitLLVMOnly, OPT_emit_llvm_only},
@@ -2850,6 +2850,13 @@ static void GenerateFrontendArgs(const FrontendOptions &Opts,
28502850
};
28512851
}
28522852

2853+
if (Opts.ProgramAction == frontend::EmitMLIR) {
2854+
GenerateProgramAction = [&]() {
2855+
if (Opts.MLIRTargetDialect == FrontendOptions::MLIR_Default)
2856+
GenerateArg(Consumer, OPT_emit_mlir);
2857+
};
2858+
}
2859+
28532860
if (Opts.ProgramAction == frontend::FixIt && !Opts.FixItSuffix.empty()) {
28542861
GenerateProgramAction = [&]() {
28552862
GenerateArg(Consumer, OPT_fixit_EQ, Opts.FixItSuffix);
@@ -4670,7 +4677,6 @@ static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) {
46704677
case frontend::EmitCIRFlat:
46714678
case frontend::EmitCIROnly:
46724679
case frontend::EmitMLIR:
4673-
case frontend::EmitMLIRLLVM:
46744680
case frontend::EmitHTML:
46754681
case frontend::EmitLLVM:
46764682
case frontend::EmitLLVMOnly:

clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ CreateFrontendBaseAction(CompilerInstance &CI) {
6060
llvm::report_fatal_error(
6161
"-emit-cir and -emit-cir-only only valid when using -fclangir");
6262

63-
if (CI.getFrontendOpts().ClangIRDirectLowering && Act == EmitMLIR)
64-
llvm::report_fatal_error(
65-
"ClangIR direct lowering is incompatible with -emit-mlir");
63+
if (Act == EmitMLIR && CI.getFrontendOpts().ClangIRDirectLowering &&
64+
CI.getFrontendOpts().MLIRTargetDialect == FrontendOptions::MLIR_STD)
65+
llvm::report_fatal_error("ClangIR direct lowering is incompatible with "
66+
"emitting of MLIR standard dialects");
6667

6768
switch (CI.getFrontendOpts().ProgramAction) {
6869
case ASTDeclList: return std::make_unique<ASTDeclListAction>();
@@ -99,8 +100,6 @@ CreateFrontendBaseAction(CompilerInstance &CI) {
99100
return std::make_unique<cir::EmitCIROnlyAction>();
100101
case EmitMLIR:
101102
return std::make_unique<cir::EmitMLIRAction>();
102-
case EmitMLIRLLVM:
103-
return std::make_unique<cir::EmitMLIRLLVMAction>();
104103
#else
105104
case EmitCIR:
106105
case EmitCIRFlat:

0 commit comments

Comments
 (0)