Skip to content

Commit b339fac

Browse files
mrkajetanprlavaee
authored andcommitted
[flang][tco] Add -emit-final-mlir flag (llvm#146533)
Add a flag to tco for emitting the final MLIR, prior to lowering to LLVM IR. This is intended to produce output that can be passed directly to mlir-translate. --------- Signed-off-by: Kajetan Puchalski <[email protected]>
1 parent 9f4ce1b commit b339fac

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Test tco's -emit-final-mlir option.
2+
3+
// RUN: tco -emit-final-mlir %s 2>&1 | FileCheck %s
4+
5+
// Check that the FIR file is translated into the LLVM IR
6+
// MLIR dialect, but not into LLVM IR itself.
7+
8+
// CHECK-NOT: func.func
9+
// CHECK-LABEL: llvm.func @_QPfoo()
10+
// CHECK-NOT: fir.alloca
11+
// CHECK: %[[VAL_0:.*]] = llvm.mlir.constant(1 : i64) : i64
12+
// CHECK: %[[VAL_1:.*]] = llvm.alloca %[[VAL_0]] x i32 : (i64) -> !llvm.ptr
13+
// CHECK: llvm.return
14+
// CHECK-NOT: func.func
15+
16+
func.func @_QPfoo() {
17+
%1 = fir.alloca i32
18+
return
19+
}

flang/tools/tco/tco.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ static cl::opt<bool> codeGenLLVM(
7070
cl::desc("Run only CodeGen passes and translate FIR to LLVM IR"),
7171
cl::init(false));
7272

73+
static cl::opt<bool> emitFinalMLIR(
74+
"emit-final-mlir",
75+
cl::desc("Only translate FIR to MLIR, do not lower to LLVM IR"),
76+
cl::init(false));
77+
7378
#include "flang/Optimizer/Passes/CommandLineOpts.h"
7479
#include "flang/Optimizer/Passes/Pipelines.h"
7580

@@ -149,13 +154,15 @@ compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
149154
fir::registerDefaultInlinerPass(config);
150155
fir::createMLIRToLLVMPassPipeline(pm, config);
151156
}
152-
fir::addLLVMDialectToLLVMPass(pm, out.os());
157+
if (!emitFinalMLIR)
158+
fir::addLLVMDialectToLLVMPass(pm, out.os());
153159
}
154160

155161
// run the pass manager
156162
if (mlir::succeeded(pm.run(*owningRef))) {
157163
// passes ran successfully, so keep the output
158-
if ((emitFir || passPipeline.hasAnyOccurrences()) && !codeGenLLVM)
164+
if ((emitFir || passPipeline.hasAnyOccurrences() || emitFinalMLIR) &&
165+
!codeGenLLVM)
159166
printModule(*owningRef, out.os());
160167
out.keep();
161168
return mlir::success();

0 commit comments

Comments
 (0)