/
hlo_xla_runtime_pipeline.cc
342 lines (303 loc) · 15.9 KB
/
hlo_xla_runtime_pipeline.cc
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/compiler/xla/service/cpu/hlo_xla_runtime_pipeline.h"
#include <utility>
#include "mlir/Dialect/Linalg/Transforms/TilingInterfaceImpl.h"
#include "mlir/Conversion/BufferizationToMemRef/BufferizationToMemRef.h" // from @llvm-project
#include "mlir/Conversion/ComplexToStandard/ComplexToStandard.h" // from @llvm-project
#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h" // from @llvm-project
#include "mlir/Conversion/ShapeToStandard/ShapeToStandard.h" // from @llvm-project
#include "mlir/Conversion/TensorToLinalg/TensorToLinalgPass.h" // from @llvm-project
#include "mlir/Dialect/Arith/Transforms/BufferizableOpInterfaceImpl.h" // from @llvm-project
#include "mlir/Dialect/Bufferization/Transforms/FuncBufferizableOpInterfaceImpl.h" // from @llvm-project
#include "mlir/Dialect/Bufferization/Transforms/OneShotAnalysis.h" // from @llvm-project
#include "mlir/Dialect/Bufferization/Transforms/Passes.h" // from @llvm-project
#include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project
#include "mlir/Dialect/Linalg/Passes.h" // from @llvm-project
#include "mlir/Dialect/Linalg/Transforms/BufferizableOpInterfaceImpl.h" // from @llvm-project
#include "mlir/Dialect/MemRef/Transforms/Passes.h" // from @llvm-project
#include "mlir/Dialect/SCF/Transforms/BufferizableOpInterfaceImpl.h" // from @llvm-project
#include "mlir/Dialect/Shape/Transforms/BufferizableOpInterfaceImpl.h" // from @llvm-project
#include "mlir/Dialect/Shape/Transforms/Passes.h" // from @llvm-project
#include "mlir/Dialect/SparseTensor/Transforms/BufferizableOpInterfaceImpl.h" // from @llvm-project
#include "mlir/Dialect/SparseTensor/Transforms/Passes.h" // from @llvm-project
#include "mlir/Dialect/Tensor/Transforms/BufferizableOpInterfaceImpl.h" // from @llvm-project
#include "mlir/Dialect/Vector/Transforms/BufferizableOpInterfaceImpl.h" // from @llvm-project
#include "mlir/Pass/PassManager.h" // from @llvm-project
#include "mlir/Transforms/Passes.h" // from @llvm-project
#include "tensorflow/compiler/xla/mlir/backends/cpu/transforms/passes.h"
#include "tensorflow/compiler/xla/mlir/framework/transforms/passes.h"
#include "tensorflow/compiler/xla/mlir/runtime/transforms/compiler.h"
#include "tensorflow/compiler/xla/mlir_hlo/deallocation/transforms/passes.h"
#include "tensorflow/compiler/xla/mlir_hlo/gml_st/transforms/passes.h"
#include "tensorflow/compiler/xla/mlir_hlo/mhlo/interfaces/bufferizable_op_interface_impl.h"
#include "tensorflow/compiler/xla/mlir_hlo/mhlo/transforms/passes.h"
#include "tensorflow/compiler/xla/mlir_hlo/transforms/passes.h"
#include "tensorflow/compiler/xla/status.h"
#include "tensorflow/tsl/platform/errors.h"
#include "tensorflow/tsl/platform/logging.h"
namespace xla {
namespace cpu {
namespace {
using mlir::func::FuncOp;
mlir::bufferization::OneShotBufferizationOptions GetBufferizationOptions(
bool new_deallocator) {
using mlir::bufferization::BufferizationOptions;
using mlir::bufferization::LayoutMapOption;
using mlir::bufferization::OneShotBufferizationOptions;
OneShotBufferizationOptions options;
options.bufferizeFunctionBoundaries = true;
options.allowReturnAllocs = true;
options.functionBoundaryTypeConversion = LayoutMapOption::IdentityLayoutMap;
options.createDeallocs = !new_deallocator;
options.unknownTypeConverterFn = [](mlir::Value value,
mlir::Attribute memorySpace,
const BufferizationOptions& options) {
return mlir::bufferization::getMemRefTypeWithStaticIdentityLayout(
value.getType().cast<mlir::TensorType>(), memorySpace);
};
return options;
}
void AddSparsificationPasses(mlir::OpPassManager& pm, bool new_deallocator) {
pm.addNestedPass<FuncOp>(mlir::createLinalgGeneralizationPass());
pm.addNestedPass<FuncOp>(mlir::gml_st::createRewriteFromElementsOpPass());
pm.addPass(mlir::bufferization::createEmptyTensorEliminationPass());
pm.addNestedPass<FuncOp>(
mlir::bufferization::createEmptyTensorToAllocTensorPass());
pm.addPass(mlir::createPreSparsificationRewritePass());
pm.addPass(mlir::createSparsificationAndBufferizationPass(
GetBufferizationOptions(new_deallocator), mlir::SparsificationOptions(),
mlir::SparseTensorConversionOptions(), /*createSparseDeallocs=*/false,
/*enableRuntimeLibrary=*/false,
/*enableBufferInitialization=*/false,
/*vectorLength=*/0,
/*enableVLAVectorization=*/false,
/*enableSIMDIndex32*/ false));
pm.addNestedPass<mlir::func::FuncOp>(
mlir::bufferization::createFinalizingBufferizePass());
}
void AddSparsificationPassPipeline(mlir::OpPassManager& pm) {
AddSparsificationPasses(pm, false);
}
} // namespace
// -------------------------------------------------------------------------- //
// Assemble a HLO XLA Runtime pipeline to lower from HLO to Linalg on buffers.
// -------------------------------------------------------------------------- //
static Status CreateHloXlaPipeline(
mlir::OpPassManager& pm, const HloXlaRuntimePipelineOptions& options) {
// Resolve all shape constraints (e.g. broadcast constraints that can be
// proved statically and changed to const witness) early to allow more
// efficient broadcast operations moving.
// Move up broadcasting operations to allow for more fusion opportunities.
pm.addPass(mlir::createInlinerPass());
pm.addPass(mlir::mhlo::createExpandHloTuplesPass("main"));
// TODO(b/233771980): Remove once custom_call doesn't use tuples.
pm.addNestedPass<mlir::func::FuncOp>(mlir::mhlo::createFlattenTuplePass());
pm.addPass(createXlaAbiLegalizationPass());
pm.addNestedPass<mlir::func::FuncOp>(
mlir::mhlo::createLegalizeGeneralDotPass());
pm.addNestedPass<mlir::func::FuncOp>(
mlir::mhlo::createBroadcastPropagationPass());
pm.addPass(mlir::createCSEPass());
pm.addPass(mlir::createCanonicalizerPass());
// Some early sparse rewriting rules.
if (options.sparse_bufferization) {
pm.addNestedPass<FuncOp>(createSparseCustomCallRewritingPass());
// We wrap some CHLO unary operations with custom calls to preserve the
// sparsity information for those operations during the roundtrip. We now
// invoke the needed passes to lower such CHLO operations to HLO after we
// rewrite the custom calls back to such CHLO unary operations.
pm.addNestedPass<mlir::func::FuncOp>(
mlir::mhlo::createLegalizeSparseChloToLinalgPass());
pm.addNestedPass<mlir::func::FuncOp>(
mlir::mhlo::createChloLegalizeToHloPass());
pm.addNestedPass<mlir::func::FuncOp>(
mlir::mhlo::createSparseRewritingPass());
}
// Transform HLO operations to Linalg.
pm.addNestedPass<mlir::func::FuncOp>(mlir::mhlo::createLegalizeSortPass());
pm.addNestedPass<mlir::func::FuncOp>(
mlir::mhlo::createLegalizeControlFlowPass());
pm.addPass(::mlir::mhlo::createLegalizeToArithmeticPass());
pm.addNestedPass<mlir::func::FuncOp>(
xla::cpu::createLegalizeCollectiveOpsPass());
pm.addNestedPass<mlir::func::FuncOp>(
mlir::mhlo::createMhloExpandOpsSimplifierPass());
pm.addNestedPass<mlir::func::FuncOp>(
mlir::mhlo::createHloCanonicalizeScatterPass());
pm.addNestedPass<FuncOp>(mlir::mhlo::createHloCanonicalizeDotPass());
pm.addNestedPass<FuncOp>(mlir::mhlo::createGroupReductionDimensionsPass());
pm.addNestedPass<mlir::func::FuncOp>(
mlir::mhlo::createLegalizeMHLOToTHLOPass());
pm.addNestedPass<mlir::func::FuncOp>(
mlir::mhlo::createLegalizeHloToLinalgPass(
options.enable_tiling_and_fusion));
// Lower index cast on tensors to tensor.generate.
pm.addNestedPass<mlir::func::FuncOp>(mlir::createLowerIndexCastPass());
pm.addPass(mlir::mhlo::createConvertToSignlessPass());
// Tile tHLO ops to 1.
if (!options.enable_tiling_and_fusion) {
pm.addNestedPass<mlir::func::FuncOp>(mlir::gml_st::createTileByOnePass());
}
// Lower shape dialect to standard to enable linalg canonicalizations (e.g.
// use linalg inputs instead of outputs for memref.dim operations).
pm.addNestedPass<mlir::func::FuncOp>(mlir::mhlo::createShapeSimplification());
pm.addNestedPass<mlir::func::FuncOp>(mlir::createShapeToShapeLowering());
pm.addPass(mlir::createConvertShapeToStandardPass());
pm.addNestedPass<mlir::func::FuncOp>(
mlir::createConvertShapeConstraintsPass());
// Fuse Linalg on tensors operations.
pm.addPass(mlir::createCSEPass());
pm.addPass(mlir::memref::createResolveShapedTypeResultDimsPass());
pm.addPass(mlir::createCanonicalizerPass());
pm.addNestedPass<mlir::func::FuncOp>(
mlir::gml_st::createOptimizeLinalgOpsPass());
if (options.enable_tiling_and_fusion) {
mlir::gml_st::GmlStCPUTilingOptions opts =
mlir::gml_st::getDefaultCPUPipelineOptions(options.cpu_name);
opts.matmulTileSizes = options.matmul_tile_sizes;
if (options.enable_fusion_outlining) {
opts.enableFusionClusters = true;
opts.enableFusionClusterOutlining = true;
}
mlir::gml_st::addCPUTilingPipeline(pm, opts);
} else {
pm.addNestedPass<mlir::func::FuncOp>(
mlir::createLinalgElementwiseOpFusionPass());
}
pm.addPass(mlir::createReconcileUnrealizedCastsPass());
pm.addPass(mlir::createConvertTensorToLinalgPass());
// Detensorize SCF iter args.
pm.addNestedPass<mlir::func::FuncOp>(mlir::createDetensorizeScfOpsPass());
// mhlo ops on unit tensors generate trivial linalg.generics, which
// one-shot-bufferize generates unnecessary allocs for. The detensorize pass
// replaces these linalg.generics with scalar ops.
auto detensorize = mlir::createLinalgDetensorizePass();
if (detensorize->initializeOptions("aggressive-mode=true").failed()) {
return tsl::errors::Internal("Failed to set up detensorize pass.");
}
pm.addNestedPass<mlir::func::FuncOp>(std::move(detensorize));
pm.addNestedPass<mlir::func::FuncOp>(mlir::gml_st::createScalarizationPass());
pm.addNestedPass<FuncOp>(mlir::gml_st::createRewriteFromElementsOpPass());
pm.addPass(mlir::bufferization::createEmptyTensorEliminationPass());
pm.addNestedPass<mlir::func::FuncOp>(
mlir::bufferization::createEmptyTensorToAllocTensorPass());
// Always run canonicalizer (which does dead code removal) before
// bufferizing anything.
pm.addPass(mlir::createCanonicalizerPass());
if (options.experimental_deallocation) {
// Experimental deallocation needs input IR without any buffer reuse to
// work optimally. This pass ensures that's the case.
pm.addNestedPass<FuncOp>(mlir::deallocation::createSplitAllocTensorsPass());
}
if (options.sparse_bufferization) {
// Convert Sparse tensors.
AddSparsificationPasses(pm, options.experimental_deallocation);
} else {
pm.addPass(mlir::hlo::createOneShotBufferizePass());
}
pm.addNestedPass<mlir::func::FuncOp>(createRewriteReallocToAllocPass());
if (options.enable_tiling_and_fusion) {
pm.addNestedPass<FuncOp>(mlir::gml_st::createVectorizeCopyPass());
pm.addNestedPass<FuncOp>(mlir::gml_st::createNaiveCopyRemovalPass());
}
// Handle framework specific requirements for buffers and then insert
// deallocations for temporary buffers.
pm.addNestedPass<mlir::func::FuncOp>(mlir::createConvertLinalgToLoopsPass());
pm.addPass(mlir::createCSEPass());
pm.addPass(mlir::createCanonicalizerPass());
mlir::bufferization::BufferResultsToOutParamsOptions out_params_options;
out_params_options.filterFn = [](mlir::func::FuncOp* func) {
// Only transform the entry point.
return func->getSymName() == "main";
};
pm.addPass(mlir::bufferization::createBufferResultsToOutParamsPass(
out_params_options));
if (options.outline_with_xla_framework) {
pm.addPass(mlir::xla_framework::CreateOutlineWithXLAFrameworkPass());
}
if (options.experimental_deallocation) {
pm.addNestedPass<FuncOp>(
mlir::deallocation::createXlaBufferArgRewritePass());
pm.addPass(mlir::deallocation::createDeallocatePass());
pm.addNestedPass<FuncOp>(
mlir::deallocation::createDeallocationSimplificationPass());
// Remove SCF iter args that became redundant after simplification.
pm.addPass(mlir::createCanonicalizerPass());
pm.addNestedPass<FuncOp>(mlir::deallocation::createBufferReusePass());
pm.addNestedPass<FuncOp>(
mlir::deallocation::createDeallocationSimplificationPass());
pm.addNestedPass<FuncOp>(mlir::deallocation::createDeallocationToScfPass());
} else {
pm.addNestedPass<FuncOp>(
mlir::bufferization::createPromoteBuffersToStackPass(nullptr));
pm.addNestedPass<mlir::func::FuncOp>(
mlir::bufferization::createBufferDeallocationPass());
pm.addPass(mlir::createBufferizationToMemRefPass());
pm.addNestedPass<mlir::func::FuncOp>(
xla::cpu::createRemoveCopiesToOutParamsPass());
}
// Specialize linalg.matmul to linalg.dot, linalg.matvec or linalg.vecmat,
// and immediately canonicalize to clean up not taken branches.
// pm.addNestedPass<mlir::func::FuncOp>(CreateLinalgMatmulSpecializationPass());
pm.addPass(mlir::createCanonicalizerPass());
// TODO(tpopp): Move hits to mlir::hlo::createGenericHostToLLVMPass?
pm.addNestedPass<mlir::func::FuncOp>(
mlir::createConvertComplexToStandardPass());
pm.addPass(mlir::createCSEPass());
pm.addPass(mlir::createCanonicalizerPass());
pm.addNestedPass<FuncOp>(
mlir::gml_st::createLowerVectorsPass(options.enable_avx2));
pm.addNestedPass<FuncOp>(xla::cpu::createLegalizeI1VectorTransferOpsPass());
pm.addNestedPass<FuncOp>(
xla::cpu::createConvertXlaCpuMemRefElementCastToLLVMPass());
return OkStatus();
}
Status CreateHloXlaRuntimePipeline(
xla::runtime::PassManager& passes,
const HloXlaRuntimePipelineOptions& options) {
return CreateHloXlaPipeline(*passes, options);
}
Status CreateDefaultHloXlaRuntimePipeline(xla::runtime::PassManager& passes) {
HloXlaRuntimePipelineOptions options;
return CreateHloXlaPipeline(*passes, options);
}
void RegisterHloXlaRuntimePipelineDialects(mlir::DialectRegistry& dialects) {
mlir::arith::registerBufferizableOpInterfaceExternalModels(dialects);
mlir::bufferization::func_ext::registerBufferizableOpInterfaceExternalModels(
dialects);
mlir::linalg::registerBufferizableOpInterfaceExternalModels(dialects);
mlir::linalg::registerTilingInterfaceExternalModels(dialects);
mlir::mhlo::registerBufferizableOpInterfaceExternalModels(dialects);
mlir::scf::registerBufferizableOpInterfaceExternalModels(dialects);
mlir::shape::registerBufferizableOpInterfaceExternalModels(dialects);
mlir::sparse_tensor::registerBufferizableOpInterfaceExternalModels(dialects);
mlir::tensor::registerBufferizableOpInterfaceExternalModels(dialects);
mlir::vector::registerBufferizableOpInterfaceExternalModels(dialects);
}
static mlir::PassPipelineRegistration<> hlo_xla_runtime_pipeline(
"hlo-xla-runtime-pipeline",
"Convert HLO dialect to XLA Runtime compatible dialects",
[](mlir::OpPassManager& pm) {
HloXlaRuntimePipelineOptions options;
Status status = CreateHloXlaPipeline(pm, options);
if (!status.ok()) {
LOG(FATAL) << "HLO-XLA Runtime pipeline failed with: "
<< status.error_message();
}
});
static mlir::PassPipelineRegistration<> sparsification_pipeline(
"hlo-xla-runtime-sparsification",
"Sparsification passes from HLO-XLA Runtime pipeline",
AddSparsificationPassPipeline);
} // namespace cpu
} // namespace xla