1 /* Copyright 2021 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 #include "mlir-hlo/Analysis/shape_component_analysis.h" 17 #include "mlir-hlo/Dialect/mhlo/IR/hlo_ops.h" 18 #include "mlir-hlo/Transforms/PassDetail.h" 19 #include "mlir-hlo/Transforms/passes.h" 20 #include "mlir/Dialect/Func/IR/FuncOps.h" 21 #include "mlir/Pass/Pass.h" 22 23 namespace mlir { 24 25 using SymbolicExpr = ShapeComponentAnalysis::SymbolicExpr; 26 27 namespace { 28 29 struct TestShapeComponentAnalysisPass 30 : public TestShapeComponentAnalysisBase<TestShapeComponentAnalysisPass> { getDependentDialectsmlir::__anonbd4386a00111::TestShapeComponentAnalysisPass31 void getDependentDialects(DialectRegistry& registry) const override { 32 registry.insert<mlir::mhlo::MhloDialect>(); 33 } 34 runOnOperationmlir::__anonbd4386a00111::TestShapeComponentAnalysisPass35 void runOnOperation() override { 36 ShapeComponentAnalysis shapeComponent; 37 llvm::outs() << "Testing : " << getOperation().getName() << '\n'; 38 // Analyze anything that looks like a shape tensor. 39 getOperation().walk([&](Operation* op) { 40 // Skip ops with more than one result. 41 if (op->getNumResults() != 1) return; 42 Value result = op->getResults().front(); 43 44 // Dump shape info if any. 45 if (auto shapeInfo = shapeComponent.GetShapeInfo(result)) { 46 llvm::outs() << "Shape info for " << result << ":\n"; 47 for (const SymbolicExpr& d : *shapeInfo) { 48 llvm::outs().indent(2); 49 d.dump(llvm::outs()); 50 } 51 } 52 53 // Dump value info if any. 54 if (auto valueInfo = shapeComponent.GetValueInfo(result)) { 55 llvm::outs() << "Value info for " << result << ":\n"; 56 for (const SymbolicExpr& d : *valueInfo) { 57 llvm::outs().indent(2); 58 d.dump(llvm::outs()); 59 } 60 } 61 }); 62 } 63 }; 64 65 } // end anonymous namespace 66 67 std::unique_ptr<OperationPass<func::FuncOp>> createTestShapeComponentAnalysisPass()68createTestShapeComponentAnalysisPass() { 69 return std::make_unique<TestShapeComponentAnalysisPass>(); 70 } 71 72 } // namespace mlir 73