Skip to content

Conversation

@mrkajetanp
Copy link
Contributor

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.

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 <kajetan.puchalski@arm.com>
@mrkajetanp mrkajetanp requested review from tblah and vzakhari July 1, 2025 14:03
@llvmbot llvmbot added the flang Flang issues not falling into any other category label Jul 1, 2025
@mrkajetanp
Copy link
Contributor Author

I can also rename to just -emit-mlir for brevity, but I wasn't sure whether that'd be confusing or not on account of it all technically being mlir.

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see this, I'm sure I won't be the only one who can save time not having to dump mlir after every pass. I think the name is good.

Please could you add a test that uses this flag and checks that what it gets is LLVM dialect MLIR.

I'll add some other reviewers who often work on LLVMIR codegen as they may also find this useful.

@llvmbot
Copy link
Member

llvmbot commented Jul 1, 2025

@llvm/pr-subscribers-flang-driver

Author: Kajetan Puchalski (mrkajetanp)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/146533.diff

2 Files Affected:

  • (added) flang/test/Driver/tco-emit-final-mlir.fir (+19)
  • (modified) flang/tools/tco/tco.cpp (+9-2)
diff --git a/flang/test/Driver/tco-emit-final-mlir.fir b/flang/test/Driver/tco-emit-final-mlir.fir new file mode 100644 index 0000000000000..75f8f153127af --- /dev/null +++ b/flang/test/Driver/tco-emit-final-mlir.fir @@ -0,0 +1,19 @@ +// Test tco's -emit-final-mlir option. + +// RUN: tco -emit-final-mlir %s 2>&1 | FileCheck %s + +// Check that the FIR file is translated into the LLVM IR +// MLIR dialect, but not into LLVM IR itself. + +// CHECK-NOT: func.func +// CHECK-LABEL: llvm.func @_QPfoo() +// CHECK-NOT: fir.alloca +// CHECK: %[[VAL_0:.*]] = llvm.mlir.constant(1 : i64) : i64 +// CHECK: %[[VAL_1:.*]] = llvm.alloca %[[VAL_0]] x i32 : (i64) -> !llvm.ptr +// CHECK: llvm.return +// CHECK-NOT: func.func + +func.func @_QPfoo() { + %1 = fir.alloca i32 + return +} diff --git a/flang/tools/tco/tco.cpp b/flang/tools/tco/tco.cpp index eaf4bae088454..806cd3148d09f 100644 --- a/flang/tools/tco/tco.cpp +++ b/flang/tools/tco/tco.cpp @@ -70,6 +70,11 @@ static cl::opt<bool> codeGenLLVM( cl::desc("Run only CodeGen passes and translate FIR to LLVM IR"), cl::init(false)); +static cl::opt<bool> emitFinalMLIR( + "emit-final-mlir", + cl::desc("Only translate FIR to MLIR, do not lower to LLVM IR"), + cl::init(false)); + #include "flang/Optimizer/Passes/CommandLineOpts.h" #include "flang/Optimizer/Passes/Pipelines.h" @@ -149,13 +154,15 @@ compileFIR(const mlir::PassPipelineCLParser &passPipeline) { fir::registerDefaultInlinerPass(config); fir::createMLIRToLLVMPassPipeline(pm, config); } - fir::addLLVMDialectToLLVMPass(pm, out.os()); + if (!emitFinalMLIR) + fir::addLLVMDialectToLLVMPass(pm, out.os()); } // run the pass manager if (mlir::succeeded(pm.run(*owningRef))) { // passes ran successfully, so keep the output - if ((emitFir || passPipeline.hasAnyOccurrences()) && !codeGenLLVM) + if ((emitFir || passPipeline.hasAnyOccurrences() || emitFinalMLIR) && + !codeGenLLVM) printModule(*owningRef, out.os()); out.keep(); return mlir::success(); 
Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Copy link
Member

@skatrak skatrak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, LGTM!

@kaviya2510
Copy link
Contributor

Thanks for the patch, LGTM!

@mrkajetanp mrkajetanp merged commit 69b69cb into llvm:main Jul 1, 2025
8 checks passed
tblah added a commit to tblah/llvm-project that referenced this pull request Jul 11, 2025
Build on top of llvm#146533 Adding some more options I use to generate minimal testcases for MLIR->LLVMIR conversion. The following options are added - `-simplify-mlir` runs CSE and canonicalization at the end of the MLIR pipelien - `-enable-aa` allows toggling whether TBAA is enabled - `-test-gen` is shorthand for `-emit-final-mlir -simplify-mlir -enable-aa=false`
tblah added a commit that referenced this pull request Jul 14, 2025
Built on top of #146533 Adding some more options I use to generate minimal testcases for MLIR->LLVMIR conversion. Hopefully this will save us all some time when writing these tests. The following options are added - `-simplify-mlir` runs CSE and canonicalization at the end of the MLIR pipeline - `-enable-aa` allows toggling whether TBAA is enabled - `-test-gen` is shorthand for `-emit-final-mlir -simplify-mlir -enable-aa=false`
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Jul 14, 2025
…#148171) Built on top of llvm/llvm-project#146533 Adding some more options I use to generate minimal testcases for MLIR->LLVMIR conversion. Hopefully this will save us all some time when writing these tests. The following options are added - `-simplify-mlir` runs CSE and canonicalization at the end of the MLIR pipeline - `-enable-aa` allows toggling whether TBAA is enabled - `-test-gen` is shorthand for `-emit-final-mlir -simplify-mlir -enable-aa=false`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:driver flang Flang issues not falling into any other category

6 participants