Skip to content

Commit ecb6a80

Browse files
committed
Use -emit-mlir=[core|cir] command-line options
1 parent 2419c15 commit ecb6a80

File tree

9 files changed

+50
-22
lines changed

9 files changed

+50
-22
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2987,8 +2987,13 @@ defm clangir : BoolFOption<"clangir",
29872987
BothFlags<[], [ClangOption, CC1Option], "">>;
29882988
def emit_cir : Flag<["-"], "emit-cir">, Visibility<[ClangOption, CC1Option]>,
29892989
Group<Action_Group>, HelpText<"Build ASTs and then lower to ClangIR">;
2990-
def emit_core_mlir : Flag<["-"], "emit-core-mlir">, Visibility<[CC1Option]>, Group<Action_Group>,
2991-
HelpText<"Build ASTs and then lower through ClangIR to core MLIR dialects, emit the .milr file">;
2990+
def emit_mlir_EQ : Joined<["-"], "emit-mlir=">, Visibility<[CC1Option]>, Group<Action_Group>,
2991+
HelpText<"Build ASTs and then generate/lower to the selected MLIR dialect, emit the .mlir or .cir file. "
2992+
"Allowed values are `core` for MLIR core dialects and `cir` for ClangIR">,
2993+
Values<"core,cir">,
2994+
NormalizedValuesScope<"clang::frontend">,
2995+
NormalizedValues<["MLIR_Core", "MLIR_CIR"]>,
2996+
MarshallingInfoEnum<FrontendOpts<"MLIRTargetDialect">, "MLIR_CIR">;
29922997
/// ClangIR-specific options - END
29932998

29942999
def flto_EQ : Joined<["-"], "flto=">,

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ enum ActionKind {
6969
EmitCIR,
7070

7171
/// Emit a .mlir file
72-
EmitCoreMLIR,
72+
EmitMLIR,
7373

7474
/// Emit a .ll file.
7575
EmitLLVM,
@@ -151,6 +151,8 @@ enum ActionKind {
151151
PrintDependencyDirectivesSourceMinimizerOutput
152152
};
153153

154+
enum MLIRDialectKind { MLIR_CIR, MLIR_Core };
155+
154156
} // namespace frontend
155157

156158
/// The kind of a file that we've been handed as an input.
@@ -420,6 +422,8 @@ class FrontendOptions {
420422
/// Specifies the output format of the AST.
421423
ASTDumpOutputFormat ASTDumpFormat = ADOF_Default;
422424

425+
frontend::MLIRDialectKind MLIRTargetDialect = frontend::MLIR_CIR;
426+
423427
/// The input kind, either specified via -x argument or deduced from the input
424428
/// file name.
425429
InputKind DashX;

clang/lib/CIR/FrontendAction/CIRGenAction.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "clang/CIR/LowerToLLVM.h"
1414
#include "clang/CodeGen/BackendUtil.h"
1515
#include "clang/Frontend/CompilerInstance.h"
16+
#include "clang/Frontend/FrontendOptions.h"
1617
#include "llvm/IR/Module.h"
1718

1819
using namespace cir;
@@ -86,21 +87,26 @@ class CIRGenConsumer : public clang::ASTConsumer {
8687
mlir::MLIRContext &MlirCtx = Gen->getMLIRContext();
8788
switch (Action) {
8889
case CIRGenAction::OutputType::EmitCIR:
89-
if (OutputStream && MlirModule) {
90+
assert(CI.getFrontendOpts().MLIRTargetDialect == frontend::MLIR_CIR);
91+
case CIRGenAction::OutputType::EmitMLIR: {
92+
switch (CI.getFrontendOpts().MLIRTargetDialect) {
93+
case frontend::MLIR_CIR:
94+
if (OutputStream && MlirModule) {
95+
mlir::OpPrintingFlags Flags;
96+
Flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false);
97+
MlirModule->print(*OutputStream, Flags);
98+
}
99+
break;
100+
case frontend::MLIR_Core:
101+
mlir::ModuleOp LoweredMlirModule =
102+
lowerFromCIRToMLIR(MlirModule, MlirCtx);
103+
assert(OutputStream && "No output stream when lowering to MLIR!");
104+
// FIXME: we cannot roundtrip prettyForm=true right now.
90105
mlir::OpPrintingFlags Flags;
91106
Flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false);
92-
MlirModule->print(*OutputStream, Flags);
107+
LoweredMlirModule->print(*OutputStream, Flags);
108+
break;
93109
}
94-
break;
95-
case CIRGenAction::OutputType::EmitMLIR: {
96-
mlir::ModuleOp LoweredMlirModule =
97-
lowerFromCIRToMLIR(MlirModule, MlirCtx);
98-
assert(OutputStream && "No output stream when lowering to MLIR!");
99-
// FIXME: we cannot roundtrip prettyForm=true right now.
100-
mlir::OpPrintingFlags Flags;
101-
Flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false);
102-
LoweredMlirModule->print(*OutputStream, Flags);
103-
break;
104110
}
105111
case CIRGenAction::OutputType::EmitLLVM:
106112
case CIRGenAction::OutputType::EmitBC:

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2740,7 +2740,7 @@ static const auto &getFrontendActionTable() {
27402740
{frontend::EmitAssembly, OPT_S},
27412741
{frontend::EmitBC, OPT_emit_llvm_bc},
27422742
{frontend::EmitCIR, OPT_emit_cir},
2743-
{frontend::EmitCoreMLIR, OPT_emit_core_mlir},
2743+
{frontend::EmitMLIR, OPT_emit_mlir_EQ},
27442744
{frontend::EmitHTML, OPT_emit_html},
27452745
{frontend::EmitLLVM, OPT_emit_llvm},
27462746
{frontend::EmitLLVMOnly, OPT_emit_llvm_only},
@@ -2857,6 +2857,13 @@ static void GenerateFrontendArgs(const FrontendOptions &Opts,
28572857
};
28582858
}
28592859

2860+
if (Opts.ProgramAction == frontend::EmitMLIR) {
2861+
GenerateProgramAction = [&]() {
2862+
if (Opts.MLIRTargetDialect == frontend::MLIR_CIR)
2863+
GenerateArg(Consumer, OPT_emit_cir);
2864+
};
2865+
}
2866+
28602867
GenerateProgramAction();
28612868

28622869
for (const auto &PluginArgs : Opts.PluginArgs) {
@@ -4632,7 +4639,7 @@ static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) {
46324639
case frontend::EmitAssembly:
46334640
case frontend::EmitBC:
46344641
case frontend::EmitCIR:
4635-
case frontend::EmitCoreMLIR:
4642+
case frontend::EmitMLIR:
46364643
case frontend::EmitHTML:
46374644
case frontend::EmitLLVM:
46384645
case frontend::EmitLLVMOnly:

clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ CreateFrontendBaseAction(CompilerInstance &CI) {
8080
#else
8181
llvm_unreachable("CIR suppport not built into clang");
8282
#endif
83-
case EmitCoreMLIR:
83+
case EmitMLIR:
8484
#if CLANG_ENABLE_CIR
8585
return std::make_unique<cir::EmitMLIRAction>();
8686
#else

clang/test/CIR/Lowering/ThroughMLIR/global-ptrs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-core-mlir %s -o %t.mlir
1+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=core %s -o %t.mlir
22
// RUN: FileCheck --input-file=%t.mlir %s
33

44
// XFAIL: *

clang/test/CIR/Lowering/ThroughMLIR/global.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-core-mlir %s -o %t.mlir
1+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=core %s -o %t.mlir
22
// RUN: FileCheck --input-file=%t.mlir %s
33

44
char c;

clang/test/CIR/global-var-simple.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Global variables of scalar typees with initial values
2-
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s
1+
// Global variables of scalar types with initial values
2+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=cir %s -o - | FileCheck %s
33

44
char c;
55
// CHECK: cir.global external @c : !cir.int<s, 8>

clang/test/CIR/hello.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Smoke test for ClangIR code generation
2+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=cir %s -o - | FileCheck %s
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - | FileCheck %s
4+
5+
void foo() {}
6+
// CHECK: cir.func @foo

0 commit comments

Comments
 (0)