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/Math/IR/Math.h" // from @llvm-project
21 #include "mlir/Dialect/SCF/SCF.h" // from @llvm-project
22 #include "mlir/Dialect/Shape/IR/Shape.h" // from @llvm-project
23 #include "mlir/Dialect/Shape/Transforms/Passes.h" // from @llvm-project
24 #include "mlir/Dialect/StandardOps/IR/Ops.h" // from @llvm-project
25 #include "mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project
26 #include "mlir/IR/MLIRContext.h" // from @llvm-project
27 #include "mlir/IR/PatternMatch.h" // from @llvm-project
28 #include "mlir/Transforms/DialectConversion.h" // from @llvm-project
29 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/passes.h"
30
31 namespace mlir {
32 namespace kernel_gen {
33 namespace transforms {
34 namespace {
35
36 #define GEN_PASS_CLASSES
37 #include "tensorflow/compiler/mlir/tools/kernel_gen/transforms/kernel_gen_passes.h.inc"
38
39 struct ShapeToDescriptorsPass
40 : public ShapeToDescriptorsPassBase<ShapeToDescriptorsPass> {
getDependentDialectsmlir::kernel_gen::transforms::__anon6953331e0111::ShapeToDescriptorsPass41 void getDependentDialects(DialectRegistry ®istry) const override {
42 registry.insert<scf::SCFDialect>();
43 }
44
45 public:
runOnOperationmlir::kernel_gen::transforms::__anon6953331e0111::ShapeToDescriptorsPass46 void runOnOperation() override {
47 MLIRContext &ctx = getContext();
48
49 // Setup target legality.
50 ConversionTarget target(ctx);
51 target.addIllegalDialect<shape::ShapeDialect>();
52 target.addLegalDialect<scf::SCFDialect>();
53 target.addLegalDialect<StandardOpsDialect>();
54 target.addLegalDialect<math::MathDialect>();
55 target.addLegalDialect<tensor::TensorDialect>();
56 // Don't mark the primary Cstr/Assuming ops as illegal, so they can be
57 // lowered at a later time to assertions.
58 target.addLegalOp<shape::AssumingOp, shape::AssumingYieldOp,
59 shape::CstrRequireOp>();
60
61 // Setup conversion patterns.
62 OwningRewritePatternList patterns;
63 populateShapeRewritePatterns(&ctx, patterns);
64 populateShapeToStandardConversionPatterns(patterns, &ctx);
65
66 // Apply conversion.
67 auto module = getOperation();
68 if (failed(applyPartialConversion(module, target, std::move(patterns))))
69 signalPassFailure();
70 }
71 };
72
73 } // namespace
74
CreateShapeToDescriptorsPass()75 std::unique_ptr<OperationPass<ModuleOp> > CreateShapeToDescriptorsPass() {
76 return std::make_unique<ShapeToDescriptorsPass>();
77 }
78
79 } // namespace transforms
80 } // namespace kernel_gen
81 } // namespace mlir
82