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 "llvm/ADT/None.h" 17 #include "llvm/Support/Casting.h" 18 #include "llvm/Support/raw_ostream.h" 19 #include "mlir/Dialect/StandardOps/IR/Ops.h" // from @llvm-project 20 #include "mlir/IR/Attributes.h" // from @llvm-project 21 #include "mlir/IR/Builders.h" // from @llvm-project 22 #include "mlir/IR/BuiltinAttributes.h" // from @llvm-project 23 #include "mlir/IR/MLIRContext.h" // from @llvm-project 24 #include "mlir/IR/Operation.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/Transforms/DialectConversion.h" // from @llvm-project 28 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project 29 #include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" 30 #include "tensorflow/compiler/mlir/lite/transforms/passes.h" 31 #include "tensorflow/compiler/mlir/lite/utils/variables_utils.h" 32 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" 33 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops_n_z.h" 34 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_saved_model.h" 35 36 namespace mlir { 37 namespace TFL { 38 namespace { 39 // Attribute name to identify whether variables should be legalized to TFLite or 40 // not. 41 const char kLegalizeTflVariables[] = "tfl._legalize_tfl_variables"; 42 HasSupportedElementType(Operation * op)43bool HasSupportedElementType(Operation* op) { 44 return utils::IsSupportedVariableType(op); 45 } 46 IsSupportedElementType(ShapedType type)47bool IsSupportedElementType(ShapedType type) { 48 return utils::IsSupportedVariableType(type); 49 } 50 51 #include "tensorflow/compiler/mlir/lite/transforms/generated_legalize_variables.inc" 52 53 // Pass which legalizes TF variables which are already passed as bounded 54 // arguments to functions, to TFLite variables. 55 class LegalizeVariables 56 : public PassWrapper<LegalizeVariables, OperationPass<ModuleOp>> { 57 public: getDependentDialects(DialectRegistry & registry) const58 void getDependentDialects(DialectRegistry& registry) const override { 59 registry.insert<TFL::TensorFlowLiteDialect>(); 60 } 61 getArgument() const62 StringRef getArgument() const final { 63 // This is the argument used to refer to the pass in 64 // the textual format (on the commandline for example). 65 return "tfl-legalize-variables-tf"; 66 } getDescription() const67 StringRef getDescription() const final { 68 // This is a brief description of the pass. 69 return "Legalize TensorFlow variables to TensorFlow Lite dialect"; 70 } 71 runOnOperation()72 void runOnOperation() override { 73 auto module = getOperation(); 74 // If TFLite variable legalization is not allowed, then we skip this pass. 75 if (auto legalize_tfl_variables_attr = 76 module->getAttr(kLegalizeTflVariables)) { 77 if (!legalize_tfl_variables_attr.cast<BoolAttr>().getValue()) return; 78 } 79 80 OwningRewritePatternList patterns(&getContext()); 81 populateWithGenerated(patterns); 82 (void)applyPatternsAndFoldGreedily(module, std::move(patterns)); 83 } 84 }; 85 86 } // namespace 87 CreateLegalizeVariablesPass()88std::unique_ptr<OperationPass<ModuleOp>> CreateLegalizeVariablesPass() { 89 return std::make_unique<LegalizeVariables>(); 90 } 91 92 static PassRegistration<LegalizeVariables> pass; 93 94 } // namespace TFL 95 } // namespace mlir 96