Skip to content

Commit

Permalink
Move SCFToCFG into Util dialect and later in the pipeline (#14583)
Browse files Browse the repository at this point in the history
It is difficult to investigate buffer / dispatch behavior across CFG,
while SCF is much simpler to trace their usage. Pushed SCF to CFG later
in the pipeline.
  • Loading branch information
rsuderman committed Aug 16, 2023
1 parent fb38d20 commit 71081dd
Show file tree
Hide file tree
Showing 17 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ iree_compiler_cc_library(
"SplitReduction.cpp",
"StripSignedness.cpp",
"TensorPadToTensorInsertSlice.cpp",
"TopLevelSCFToCFG.cpp",
"VerifyInputLegality.cpp",
],
hdrs = [
Expand Down Expand Up @@ -117,6 +118,7 @@ iree_compiler_cc_library(
"@llvm-project//mlir:Parser",
"@llvm-project//mlir:Pass",
"@llvm-project//mlir:SCFDialect",
"@llvm-project//mlir:SCFToControlFlow",
"@llvm-project//mlir:Support",
"@llvm-project//mlir:TensorDialect",
"@llvm-project//mlir:TensorTransforms",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ iree_cc_library(
"SplitReduction.cpp"
"StripSignedness.cpp"
"TensorPadToTensorInsertSlice.cpp"
"TopLevelSCFToCFG.cpp"
"VerifyInputLegality.cpp"
DEPS
::PassesIncGen
Expand Down Expand Up @@ -98,6 +99,7 @@ iree_cc_library(
MLIRParser
MLIRPass
MLIRSCFDialect
MLIRSCFToControlFlow
MLIRSupport
MLIRTensorDialect
MLIRTensorTransforms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ void buildFlowTransformPassPipeline(OpPassManager &passManager,
// Expand tensor shapes into SSA values and optimize the whole program.
// The more we are able to equate shape dimensions at this level the better
// our fusions will be.
FunctionLikeNest(passManager).addPass(createTopLevelSCFToCFGPass);
passManager.addPass(IREE::Flow::createExpandTensorShapesPass());
buildGlobalOptimizationPassPipeline(passManager, transformOptions);

Expand Down
4 changes: 4 additions & 0 deletions compiler/src/iree/compiler/Dialect/Flow/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ createStripSignednessPass();
std::unique_ptr<InterfacePass<mlir::FunctionOpInterface>>
createRemoveZeroExtentTensorsPass();

// Decomposes top-level SCF operations to CFG.
std::unique_ptr<InterfacePass<mlir::FunctionOpInterface>>
createTopLevelSCFToCFGPass();

// Verifies that the input to the Flow transformation pipeline is legal.
// This includes checking for operations from dialects that are expected
// to be legalized before this pass.
Expand Down
6 changes: 6 additions & 0 deletions compiler/src/iree/compiler/Dialect/Flow/Transforms/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ def StripSignedness :
let constructor = "mlir::iree_compiler::IREE::Flow::createStripSignednessPass()";
}

def TopLevelSCFToCFG :
InterfacePass<"iree-top-level-scf-to-cfg", "mlir::FunctionOpInterface"> {
let summary = "Converts non-nested SCF constructs to CFG (not traversing into opaque operations).";
let constructor = "mlir::iree_compiler::IREE::Flow::createTopLevelSCFToCFGPass()";
}

def VerifyInputLegality: Pass<"iree-verify-input-legality", ""> {
let summary = "Checks the legality of the IR at the start of IREE flow transformation pipeline.";
let constructor = "mlir::iree_compiler::IREE::Flow::createVerifyInputLegalityPass()";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "iree/compiler/InputConversion/Common/PassDetail.h"
#include "iree/compiler/InputConversion/Common/Passes.h"
#include "iree/compiler/Dialect/Flow/Transforms/PassDetail.h"
#include "iree/compiler/Dialect/Flow/Transforms/Passes.h"
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/IR/FunctionInterfaces.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Transforms/DialectConversion.h"

namespace mlir {
namespace iree_compiler {
namespace IREE {
namespace Flow {

namespace {

Expand Down Expand Up @@ -46,9 +49,12 @@ void TopLevelSCFToCFGPass::runOnOperation() {
signalPassFailure();
}

std::unique_ptr<OperationPass<func::FuncOp>> createTopLevelSCFToCFGPass() {
std::unique_ptr<InterfacePass<mlir::FunctionOpInterface>>
createTopLevelSCFToCFGPass() {
return std::make_unique<TopLevelSCFToCFGPass>();
}

} // namespace Flow
} // namespace IREE
} // namespace iree_compiler
} // namespace mlir
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ iree_lit_test_suite(
"set_encoding.mlir",
"strip_signedness.mlir",
"tensor_pad_to_tensor_insert_slice.mlir",
"top_level_scf_to_cfg.mlir",
"transform_dispatch_region_formation.mlir",
"transformation_pipeline.mlir",
"verify_input_ir.mlir",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ iree_lit_test_suite(
"set_encoding.mlir"
"strip_signedness.mlir"
"tensor_pad_to_tensor_insert_slice.mlir"
"top_level_scf_to_cfg.mlir"
"transform_dispatch_region_formation.mlir"
"transformation_pipeline.mlir"
"verify_input_ir.mlir"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: iree-opt --split-input-file --iree-top-level-scf-to-cfg %s | FileCheck %s
// RUN: iree-opt --split-input-file --pass-pipeline="builtin.module(func.func(iree-top-level-scf-to-cfg))" %s | FileCheck %s

// CHECK-LABEL: @generic_nested_for
// While not super recommended, we do have cases of SCF constructs embedded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ iree_compiler_cc_library(
"QuantizedConvToConv.cpp",
"QuantizedMatmulToMatmul.cpp",
"SanitizeModuleNames.cpp",
"TopLevelSCFToCFG.cpp",
"Utils.cpp",
],
hdrs = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ iree_cc_library(
"QuantizedConvToConv.cpp"
"QuantizedMatmulToMatmul.cpp"
"SanitizeModuleNames.cpp"
"TopLevelSCFToCFG.cpp"
"Utils.cpp"
DEPS
::PassHeaders
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace {

void buildCommonInputConversionPassPipeline(OpPassManager &passManager) {
// Currently we don't handle SCF ops well and have to convert them all to CFG.
passManager.addNestedPass<func::FuncOp>(createTopLevelSCFToCFGPass());
passManager.addPass(createIREEImportPublicPass());
passManager.addPass(createImportMLProgramPass());
passManager.addPass(createSanitizeModuleNamesPass());
Expand Down
1 change: 0 additions & 1 deletion compiler/src/iree/compiler/InputConversion/Common/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ createLinalgQuantizedConvToConvPass();
std::unique_ptr<OperationPass<func::FuncOp>>
createLinalgQuantizedMatmulToMatmulPass();
std::unique_ptr<OperationPass<ModuleOp>> createSanitizeModuleNamesPass();
std::unique_ptr<OperationPass<func::FuncOp>> createTopLevelSCFToCFGPass();

//===----------------------------------------------------------------------===//
// Register all Passes
Expand Down
6 changes: 0 additions & 6 deletions compiler/src/iree/compiler/InputConversion/Common/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ def SanitizeModuleNames :
let constructor = "mlir::iree_compiler::createSanitizeModuleNamesPass()";
}

def TopLevelSCFToCFG :
Pass<"iree-top-level-scf-to-cfg", "func::FuncOp"> {
let summary = "Converts non-nested SCF constructs to CFG (not traversing into opaque operations).";
let constructor = "mlir::iree_compiler::createTopLevelSCFToCFGPass()";
}

def AutoInputConversionPipeline :
Pass<"iree-auto-input-conversion", "ModuleOp"> {
let summary = "Analyzes and runs appropriate input pipeline.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ iree_lit_test_suite(
"linalg_quantized_conv_to_conv.mlir",
"linalg_quantized_matmul_to_matmul.mlir",
"sanitize_module_names.mlir",
"top_level_scf_to_cfg.mlir",
],
include = ["*.mlir"],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ iree_lit_test_suite(
"linalg_quantized_conv_to_conv.mlir"
"linalg_quantized_matmul_to_matmul.mlir"
"sanitize_module_names.mlir"
"top_level_scf_to_cfg.mlir"
TOOLS
FileCheck
iree-opt
Expand Down
1 change: 0 additions & 1 deletion compiler/src/iree/compiler/InputConversion/TOSA/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ void buildTOSAInputConversionPassPipeline(OpPassManager &passManager) {
// In the future it would be nice if we could have all of flow be both scf
// and cfg compatible.
passManager.addNestedPass<func::FuncOp>(tosa::createTosaToSCF());
passManager.addNestedPass<func::FuncOp>(createTopLevelSCFToCFGPass());

// We also don't handle calls well on the old codepath; until we remove the
// use of the CFG we can continue inlining.
Expand Down

0 comments on commit 71081dd

Please sign in to comment.