From 5c02a2421b951a14d1294598d0082b32659c7082 Mon Sep 17 00:00:00 2001 From: Eric Schweitz Date: Wed, 1 Jul 2020 17:00:02 -0700 Subject: [PATCH] [flang] Add inliner pass. This adds a minimalist inliner implementation. Along with the inliner, a minimum number of support files are also included. These will pave the way for future diffs to add more transformation passes to flang. A future diff will add the inline test, which cannot be run successfully quite yet as some components have not yet been upstreamed. Differential revision: --- flang/include/flang/Optimizer/CMakeLists.txt | 1 + .../flang/Optimizer/Transforms/CMakeLists.txt | 6 ++ .../flang/Optimizer/Transforms/Passes.h | 58 +++++++++++++++++++ .../flang/Optimizer/Transforms/Passes.td | 51 ++++++++++++++++ flang/lib/Optimizer/CMakeLists.txt | 3 + flang/lib/Optimizer/Transforms/Inliner.cpp | 24 ++++++++ 6 files changed, 143 insertions(+) create mode 100644 flang/include/flang/Optimizer/Transforms/CMakeLists.txt create mode 100644 flang/include/flang/Optimizer/Transforms/Passes.h create mode 100644 flang/include/flang/Optimizer/Transforms/Passes.td create mode 100644 flang/lib/Optimizer/Transforms/Inliner.cpp diff --git a/flang/include/flang/Optimizer/CMakeLists.txt b/flang/include/flang/Optimizer/CMakeLists.txt index 09ad1fdc2671..0fa521d5b967 100644 --- a/flang/include/flang/Optimizer/CMakeLists.txt +++ b/flang/include/flang/Optimizer/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(CodeGen) add_subdirectory(Dialect) +add_subdirectory(Transforms) diff --git a/flang/include/flang/Optimizer/Transforms/CMakeLists.txt b/flang/include/flang/Optimizer/Transforms/CMakeLists.txt new file mode 100644 index 000000000000..fde17eb88622 --- /dev/null +++ b/flang/include/flang/Optimizer/Transforms/CMakeLists.txt @@ -0,0 +1,6 @@ + +set(LLVM_TARGET_DEFINITIONS Passes.td) +mlir_tablegen(Passes.h.inc -gen-pass-decls) +add_public_tablegen_target(FIROptTransformsPassIncGen) + +add_mlir_doc(Passes -gen-pass-doc OptimizerTransformPasses ./) diff --git a/flang/include/flang/Optimizer/Transforms/Passes.h b/flang/include/flang/Optimizer/Transforms/Passes.h new file mode 100644 index 000000000000..9377c2dc61cc --- /dev/null +++ b/flang/include/flang/Optimizer/Transforms/Passes.h @@ -0,0 +1,58 @@ +//===-- Optimizer/Transforms/Passes.h ---------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef OPTIMIZER_TRANSFORMS_PASSES_H +#define OPTIMIZER_TRANSFORMS_PASSES_H + +#include "mlir/Pass/Pass.h" +#include "mlir/Pass/PassRegistry.h" +#include + +namespace mlir { +class BlockAndValueMapping; +class Operation; +class Pass; +class Region; +} // namespace mlir + +namespace fir { + +/// Convert fir.select_type to the standard dialect +std::unique_ptr createControlFlowLoweringPass(); + +/// Effects aware CSE pass +std::unique_ptr createCSEPass(); + +/// Convert FIR loop constructs to the Affine dialect +std::unique_ptr createPromoteToAffinePass(); + +/// Convert `fir.do_loop` and `fir.if` to a CFG. This +/// conversion enables the `createLowerToCFGPass` to transform these to CFG +/// form. +std::unique_ptr createFirToCfgPass(); + +/// A pass to convert the FIR dialect from "Mem-SSA" form to "Reg-SSA" +/// form. This pass is a port of LLVM's mem2reg pass, but modified for the FIR +/// dialect as well as the restructuring of MLIR's representation to present PHI +/// nodes as block arguments. +std::unique_ptr createMemToRegPass(); + +/// Support for inlining on FIR. +bool canLegallyInline(mlir::Operation *op, mlir::Region *reg, + mlir::BlockAndValueMapping &map); + +inline void registerOptTransformPasses() { +using mlir::Pass; +// declarative passes +#define GEN_PASS_REGISTRATION +#include "flang/Optimizer/Transforms/Passes.h.inc" +} + +} // namespace fir + +#endif // OPTIMIZER_TRANSFORMS_PASSES_H diff --git a/flang/include/flang/Optimizer/Transforms/Passes.td b/flang/include/flang/Optimizer/Transforms/Passes.td new file mode 100644 index 000000000000..c558d99af8dc --- /dev/null +++ b/flang/include/flang/Optimizer/Transforms/Passes.td @@ -0,0 +1,51 @@ +//===-- Passes.td - Transforms pass definition file --------*- tablegen -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file contains definitions for passes within the Optimizer/Transforms/ +// directory. +// +//===----------------------------------------------------------------------===// + +#ifndef FLANG_OPTIMIZER_TRANSFORMS_PASSES +#define FLANG_OPTIMIZER_TRANSFORMS_PASSES + +include "mlir/Pass/PassBase.td" + +def AffineDialectPromotion : FunctionPass<"promote-to-affine"> { + let summary = "Promotes fir.loop and fir.where to affine.for and affine.if where possible"; + let description = [{ + TODO + }]; + let constructor = "fir::createPromoteToAffinePass()"; +} + +def BasicCSE : FunctionPass<"basic-cse"> { + let summary = "Basic common sub-expression elimination"; + let description = [{ + TODO + }]; + let constructor = "fir::createCSEPass()"; +} + +def ControlFlowLowering : FunctionPass<"lower-control-flow"> { + let summary = "Convert affine dialect, fir.select_type to standard dialect"; + let description = [{ + TODO + }]; + let constructor = "fir::createControlFlowLoweringPass()"; +} + +def CFGConversion : FunctionPass<"cfg-conversion"> { + let summary = "Convert FIR structured control flow ops to CFG ops."; + let description = [{ + TODO + }]; + let constructor = "fir::createFirToCfgPass()"; +} + +#endif // FLANG_OPTIMIZER_TRANSFORMS_PASSES diff --git a/flang/lib/Optimizer/CMakeLists.txt b/flang/lib/Optimizer/CMakeLists.txt index abeec930ae18..1375e2c10836 100644 --- a/flang/lib/Optimizer/CMakeLists.txt +++ b/flang/lib/Optimizer/CMakeLists.txt @@ -10,8 +10,11 @@ add_flang_library(FIROptimizer Support/InternalNames.cpp Support/KindMapping.cpp + Transforms/Inliner.cpp + DEPENDS FIROpsIncGen + FIROptTransformsPassIncGen ${dialect_libs} LINK_LIBS diff --git a/flang/lib/Optimizer/Transforms/Inliner.cpp b/flang/lib/Optimizer/Transforms/Inliner.cpp new file mode 100644 index 000000000000..716665a5c240 --- /dev/null +++ b/flang/lib/Optimizer/Transforms/Inliner.cpp @@ -0,0 +1,24 @@ +//===-- Inliner.cpp -------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "flang/Optimizer/Dialect/FIRDialect.h" +#include "flang/Optimizer/Dialect/FIROps.h" +#include "flang/Optimizer/Transforms/Passes.h" +#include "mlir/Transforms/Passes.h" +#include "llvm/Support/CommandLine.h" + +static llvm::cl::opt + aggressivelyInline("inline-all", + llvm::cl::desc("aggressively inline everything"), + llvm::cl::init(false)); + +/// Should we inline the callable `op` into region `reg`? +bool fir::canLegallyInline(mlir::Operation *op, mlir::Region *reg, + mlir::BlockAndValueMapping &map) { + return aggressivelyInline; +}