Skip to content

Commit 124bede

Browse files
committed
fixup! [CIR] Add option to emit MLIR in LLVM dialect.
1 parent d51b625 commit 124bede

File tree

4 files changed

+45
-41
lines changed

4 files changed

+45
-41
lines changed

clang/include/clang/Driver/Options.td

+5-5
Original file line numberDiff line numberDiff line change
@@ -3121,14 +3121,14 @@ def emit_cir_flat : Flag<["-"], "emit-cir-flat">, Visibility<[ClangOption, CC1Op
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>,
31233123
HelpText<"Build ASTs and then lower through ClangIR to MLIR (standard dialects "
3124-
"or the LLVM dialect), emit the .mlir file.">;
3124+
"when `-fno-clangir-direct-lowering` is used or the LLVM dialect when "
3125+
"`-fclangir-direct-lowering` is used), emit the .mlir file.">;
31253126
def emit_mlir_EQ : Joined<["-"], "emit-mlir=">, Visibility<[CC1Option]>, Group<Action_Group>,
31263127
HelpText<"Build ASTs and then lower through ClangIR to the selected MLIR dialect, emit the .mlir file. "
3127-
"Allowed values are `std` for MLIR standard dialects "
3128-
"`llvm` for the LLVM dialect and `cir` for the ClangIR dialect.">,
3129-
Values<"std,llvm,cir">,
3128+
"Allowed values are `std` for MLIR standard dialects and `llvm` for the LLVM dialect.">,
3129+
Values<"std,llvm">,
31303130
NormalizedValuesScope<"FrontendOptions">,
3131-
NormalizedValues<["MLIR_STD", "MLIR_LLVM", "MLIR_CIR"]>,
3131+
NormalizedValues<["MLIR_STD", "MLIR_LLVM"]>,
31323132
MarshallingInfoEnum<FrontendOpts<"MLIRTargetDialect">, "MLIR_Default">;
31333133
/// ClangIR-specific options - END
31343134

clang/include/clang/Frontend/FrontendOptions.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,7 @@ class FrontendOptions {
533533
enum {
534534
MLIR_Default,
535535
MLIR_STD,
536-
MLIR_LLVM,
537-
MLIR_CIR
536+
MLIR_LLVM
538537
} MLIRTargetDialect = MLIR_Default;
539538

540539
/// The input kind, either specified via -x argument or deduced from the input

clang/lib/CIR/FrontendAction/CIRGenAction.cpp

+16-34
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ class CIRGenConsumer : public clang::ASTConsumer {
261261
}
262262
}
263263

264-
auto emitCIR = [&]() {
264+
switch (action) {
265+
case CIRGenAction::OutputType::EmitCIR:
266+
case CIRGenAction::OutputType::EmitCIRFlat:
265267
if (outputStream && mlirMod) {
266268
// FIXME: we cannot roundtrip prettyForm=true right now.
267269
mlir::OpPrintingFlags flags;
@@ -270,45 +272,25 @@ class CIRGenConsumer : public clang::ASTConsumer {
270272
flags.assumeVerified();
271273
mlirMod->print(*outputStream, flags);
272274
}
273-
};
274-
275-
switch (action) {
276-
case CIRGenAction::OutputType::EmitCIR:
277-
case CIRGenAction::OutputType::EmitCIRFlat:
278-
emitCIR();
279275
break;
280276
case CIRGenAction::OutputType::EmitMLIR: {
281277
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:
278+
if (feOptions.ClangIRDirectLowering) {
291279
// Attempting to emit std with direct lowering is already checked by
292280
// 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);
281+
loweredMlirModule = direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod);
282+
} else {
283+
if (feOptions.MLIRTargetDialect == clang::FrontendOptions::MLIR_LLVM)
284+
loweredMlirModule = lowerFromCIRToMLIRToLLVMDialect(mlirMod, mlirCtx.get());
285+
else // STD and Default cases
286+
loweredMlirModule = lowerFromCIRToMLIR(mlirMod, mlirCtx.get());
311287
}
288+
assert(loweredMlirModule && "MLIR module does not exist, but lowering did not fail?");
289+
assert(outputStream && "Why are we here without an output stream?");
290+
// FIXME: we cannot roundtrip prettyForm=true right now.
291+
mlir::OpPrintingFlags flags;
292+
flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false);
293+
loweredMlirModule->print(*outputStream, flags);
312294
break;
313295
}
314296
case CIRGenAction::OutputType::EmitLLVM:

clang/test/CIR/emit-mlir.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir %s -o - | FileCheck %s -check-prefix=LLVM
2+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir %s -o - | FileCheck %s -check-prefix=STD
3+
4+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=llvm %s -o - | FileCheck %s -check-prefix=LLVM
5+
// RUN: not %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-mlir=std %s -o - 2>&1 | FileCheck %s -check-prefix=STD_ERR
6+
7+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir=llvm %s -o - | FileCheck %s -check-prefix=LLVM
8+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fno-clangir-direct-lowering -emit-mlir=std %s -o - | FileCheck %s -check-prefix=STD
9+
10+
// RUN: %clang -fclangir -Xclang -emit-mlir %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_NO_VALUE
11+
// RUN: %clang -fclangir -Xclang -emit-mlir=llvm %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_LLVM
12+
// RUN: %clang -fno-clangir-direct-lowering -Xclang -emit-mlir=std %s -o - -### 2>&1 | FileCheck %s -check-prefix=OPTS_STD
13+
14+
int foo(int a, int b) {
15+
return a + b;
16+
}
17+
18+
// LLVM: llvm.func @foo
19+
// STD: func.func @foo
20+
// STD_ERR: ClangIR direct lowering is incompatible with emitting of MLIR standard dialects
21+
// OPTS_NO_VALUE: "-emit-mlir"
22+
// OPTS_LLVM: "-emit-mlir=llvm"
23+
// OPTS_STD: "-emit-mlir=std"

0 commit comments

Comments
 (0)