[flang] Add inliner pass.
authorEric Schweitz <eschweitz@nvidia.com>
Thu, 2 Jul 2020 00:00:02 +0000 (17:00 -0700)
committerEric Schweitz <eschweitz@nvidia.com>
Thu, 2 Jul 2020 13:10:54 +0000 (06:10 -0700)
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
flang/include/flang/Optimizer/Transforms/CMakeLists.txt [new file with mode: 0644]
flang/include/flang/Optimizer/Transforms/Passes.h [new file with mode: 0644]
flang/include/flang/Optimizer/Transforms/Passes.td [new file with mode: 0644]
flang/lib/Optimizer/CMakeLists.txt
flang/lib/Optimizer/Transforms/Inliner.cpp [new file with mode: 0644]

index 09ad1fd..0fa521d 100644 (file)
@@ -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 (file)
index 0000000..fde17eb
--- /dev/null
@@ -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 (file)
index 0000000..9377c2d
--- /dev/null
@@ -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 <memory>
+
+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<mlir::Pass> createControlFlowLoweringPass();
+
+/// Effects aware CSE pass
+std::unique_ptr<mlir::Pass> createCSEPass();
+
+/// Convert FIR loop constructs to the Affine dialect
+std::unique_ptr<mlir::Pass> 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<mlir::Pass> 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<mlir::Pass> 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 (file)
index 0000000..c558d99
--- /dev/null
@@ -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
index abeec93..1375e2c 100644 (file)
@@ -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 (file)
index 0000000..716665a
--- /dev/null
@@ -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<bool>
+    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;
+}