1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 // This file combines patterns for lowering shape dialect to standard ops,
17 // structured control flow and descriptors.
18
19 #include "mlir/Conversion/ShapeToStandard/ShapeToStandard.h" // from @llvm-project
20 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" // from @llvm-project
21 #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project
22 #include "mlir/Dialect/Math/IR/Math.h" // from @llvm-project
23 #include "mlir/Dialect/MemRef/IR/MemRef.h" // from @llvm-project
24 #include "mlir/Dialect/SCF/IR/SCF.h" // from @llvm-project
25 #include "mlir/Dialect/Shape/IR/Shape.h" // from @llvm-project
26 #include "mlir/Dialect/Shape/Transforms/Passes.h" // from @llvm-project
27 #include "mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project
28 #include "mlir/IR/MLIRContext.h" // from @llvm-project
29 #include "mlir/IR/PatternMatch.h" // from @llvm-project
30 #include "mlir/Transforms/DialectConversion.h" // from @llvm-project
31 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/passes.h"
32
33 namespace mlir {
34 namespace kernel_gen {
35 namespace transforms {
36 namespace {
37
38 #define GEN_PASS_CLASSES
39 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/kernel_gen_passes.h.inc"
40
41 struct ShapeToDescriptorsPass
42 : public ShapeToDescriptorsPassBase<ShapeToDescriptorsPass> {
getDependentDialectsmlir::kernel_gen::transforms::__anonab6416cd0111::ShapeToDescriptorsPass43 void getDependentDialects(DialectRegistry ®istry) const override {
44 registry.insert<scf::SCFDialect>();
45 }
46
47 public:
runOnOperationmlir::kernel_gen::transforms::__anonab6416cd0111::ShapeToDescriptorsPass48 void runOnOperation() override {
49 MLIRContext &ctx = getContext();
50
51 // Setup target legality.
52 ConversionTarget target(ctx);
53 target.addIllegalDialect<shape::ShapeDialect>();
54 target.addLegalDialect<arith::ArithmeticDialect>();
55 target.addLegalDialect<scf::SCFDialect>();
56 target.addLegalDialect<memref::MemRefDialect>();
57 target.addLegalDialect<func::FuncDialect>();
58 target.addLegalDialect<math::MathDialect>();
59 target.addLegalDialect<tensor::TensorDialect>();
60 // Don't mark the primary Cstr/Assuming ops as illegal, so they can be
61 // lowered at a later time to assertions.
62 target.addLegalOp<shape::AssumingOp, shape::AssumingYieldOp,
63 shape::AssumingAllOp, shape::CstrRequireOp>();
64
65 // Setup conversion patterns.
66 RewritePatternSet patterns(&getContext());
67 populateShapeRewritePatterns(patterns);
68 populateShapeToStandardConversionPatterns(patterns);
69
70 // Apply conversion.
71 auto module = getOperation();
72 if (failed(applyPartialConversion(module, target, std::move(patterns))))
73 signalPassFailure();
74 }
75 };
76
77 } // namespace
78
CreateShapeToDescriptorsPass()79 std::unique_ptr<OperationPass<ModuleOp> > CreateShapeToDescriptorsPass() {
80 return std::make_unique<ShapeToDescriptorsPass>();
81 }
82
83 } // namespace transforms
84 } // namespace kernel_gen
85 } // namespace mlir
86