-
Notifications
You must be signed in to change notification settings - Fork 612
/
Passes.cpp
134 lines (118 loc) · 5.82 KB
/
Passes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2019 The IREE Authors
//
// Licensed 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 "iree_tf_compiler/TF/Passes.h"
#include "iree/compiler/Dialect/Flow/Transforms/Passes.h"
#include "iree/compiler/Dialect/Shape/Transforms/Passes.h"
#include "mlir-hlo/Dialect/mhlo/transforms/passes.h"
#include "mlir/Dialect/MemRef/Transforms/Passes.h"
#include "mlir/Dialect/Shape/Transforms/Passes.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Pass/PassRegistry.h"
#include "mlir/Transforms/Passes.h"
#include "tensorflow/compiler/mlir/tensorflow/transforms/passes.h"
#include "tensorflow/compiler/mlir/tensorflow/transforms/tf_saved_model_passes.h"
#include "tensorflow/compiler/mlir/tosa/tf_passes.h"
namespace mlir {
namespace iree_integrations {
namespace TF {
// All IREE-specific passes that lower TF representations before reaching the
// IREE core should go here.
void buildTFImportPassPipeline(OpPassManager &pm, bool useTosa) {
//----------------------------------------------------------------------------
// Clean up tf_executor and extraneous unused functions.
//----------------------------------------------------------------------------
pm.addPass(createSymbolDCEPass());
pm.addPass(tf_executor::CreateTFExecutorGraphPruningPass());
pm.addPass(::mlir::TF::CreateGuaranteeAllFuncsOneUsePass());
::mlir::TF::CreateTFStandardPipeline(pm,
::mlir::TF::StandardPipelineOptions());
pm.addPass(::mlir::TF::CreateDeviceIndexSelectorPass());
//----------------------------------------------------------------------------
// Try to get the IR in good condition.
//----------------------------------------------------------------------------
pm.addPass(createStripAssertsPass());
pm.addPass(createInlinerPass());
pm.addPass(createCanonicalizerPass());
pm.addPass(TFDevice::CreateDecomposeResourceOpsPass());
pm.addPass(createPropagateResourceCastsPass());
pm.addPass(::mlir::TF::CreateTFShapeInferencePass());
//----------------------------------------------------------------------------
// Lower to CFG.
// After this point, most TF optimizations won't work properly besides
// simple canonicalizations.
//----------------------------------------------------------------------------
pm.addPass(::mlir::TF::CreateTFFunctionalControlFlowToCFG());
// Inline, as tf-functional-control-flow-to-cfg leaves in calls.
pm.addPass(createInlinerPass());
//----------------------------------------------------------------------------
// Some further cleanups now that control flow is in better shape.
//----------------------------------------------------------------------------
pm.addPass(createSymbolDCEPass());
pm.addPass(createCanonicalizerPass());
//----------------------------------------------------------------------------
// Legalize to TOSA/XLA
//----------------------------------------------------------------------------
if (useTosa) {
tosa::TOSATFLegalizationPipelineOptions tosaOptions;
tosa::createTFtoTOSALegalizationPipeline(pm, tosaOptions);
} else {
pm.addPass(createConvertToMHLOPass());
pm.addPass(createCanonicalizerPass());
}
//----------------------------------------------------------------------------
// Now that the IR is starting to look nice, optimize global tensors.
//----------------------------------------------------------------------------
pm.addPass(tf_saved_model::CreateOptimizeGlobalTensorsPass());
//----------------------------------------------------------------------------
// Lowering shape-related constructs.
//----------------------------------------------------------------------------
// pm.addPass(iree_compiler::Shape::createConvertHLOToShapePass());
// TODO(GH-2277): Lower HLO shape constraints instead of eliding them here.
pm.addPass(createRemoveShapeConstraintsPass());
pm.addPass(createCanonicalizerPass());
// pm.addPass(iree_compiler::Shape::createConvertShapeToShapexPass());
// pm.addPass(createCanonicalizerPass());
//----------------------------------------------------------------------------
// Lowering tf_saved_model dialect to IREE dialects
//----------------------------------------------------------------------------
// First, eliminate tf_saved_model.global_tensor's and corresponding
// tf_saved_model.bound_input's.
pm.addPass(createLowerGlobalTensorsPass());
// Lower exported functions.
//
// This pass must run second because:
// - It assumes that tf_saved_model.bound_inputs have been eliminated
// - It removes tf_saved_model.semantics from the module, which we can only
// do at the very end.
pm.addPass(createSavedModelToIREEABIPass());
// Inline the wrapper functions.
pm.addPass(createInlinerPass());
//----------------------------------------------------------------------------
// Ensure that all Tensorflow has been legalized away
//----------------------------------------------------------------------------
pm.addPass(createStripModuleMetadataPass());
pm.nest<ModuleOp>().addPass(createStripFunctionMetadataPass());
pm.addPass(createVerifyFullyConvertedPass());
}
void registerTFImportPassPipeline() {
mlir::PassPipelineRegistration<> pipeline(
"iree-import-tf-pipeline",
"Run IREE-specific passes for importing TF code into IREE",
[](OpPassManager &passManager) {
buildTFImportPassPipeline(passManager, false);
});
}
void registerTFTosaImportPassPipeline() {
mlir::PassPipelineRegistration<> pipeline(
"iree-import-tf-tosa-pipeline",
"Run IREE-specific passes for importing TF code into IREE",
[](OpPassManager &passManager) {
buildTFImportPassPipeline(passManager, true);
});
}
} // namespace TF
} // namespace iree_integrations
} // namespace mlir