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 transformation pass takes operations in TensorFlow dialect and 17 // optimizes them to resulting operations in TensorFlow.js dialect. 18 19 #include <memory> 20 21 #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project 22 #include "mlir/IR/Attributes.h" // from @llvm-project 23 #include "mlir/IR/BuiltinTypes.h" // from @llvm-project 24 #include "mlir/IR/Matchers.h" // from @llvm-project 25 #include "mlir/IR/PatternMatch.h" // from @llvm-project 26 #include "mlir/Pass/Pass.h" // from @llvm-project 27 #include "mlir/Support/LLVM.h" // from @llvm-project 28 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project 29 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" 30 #include "tensorflow/compiler/mlir/tfjs/ir/tfjs_ops.h" 31 #include "tensorflow/compiler/mlir/tfjs/transforms/passes_detail.h" 32 33 namespace mlir { 34 namespace tfjs { 35 36 //===----------------------------------------------------------------------===// 37 // The actual Optimize Pass. 38 namespace { 39 40 // Optimize TFJS operations in functions. 41 struct Optimize : public tfjs::OptimizePassBase<Optimize> { getDependentDialectsmlir::tfjs::__anon63e27a700111::Optimize42 void getDependentDialects(DialectRegistry ®istry) const final { 43 registry.insert<TFJSDialect>(); 44 } 45 void runOnOperation() override; 46 }; 47 48 #include "tensorflow/compiler/mlir/tfjs/transforms/generated_optimize.inc" 49 runOnOperation()50void Optimize::runOnOperation() { 51 RewritePatternSet patterns(&getContext()); 52 auto func = getOperation(); 53 54 populateWithGenerated(patterns); 55 (void)applyPatternsAndFoldGreedily(func, std::move(patterns)); 56 } 57 } // namespace 58 59 // Creates an instance of the TensorFlow.js dialect Optimize pass. CreateOptimizePass()60std::unique_ptr<OperationPass<func::FuncOp>> CreateOptimizePass() { 61 return std::make_unique<Optimize>(); 62 } 63 64 } // namespace tfjs 65 } // namespace mlir 66