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