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 <utility> 17 18 #include "llvm/ADT/None.h" 19 #include "llvm/Support/Casting.h" 20 #include "llvm/Support/raw_ostream.h" 21 #include "mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project 22 #include "mlir/IR/Attributes.h" // from @llvm-project 23 #include "mlir/IR/Builders.h" // from @llvm-project 24 #include "mlir/IR/BuiltinAttributes.h" // from @llvm-project 25 #include "mlir/IR/MLIRContext.h" // from @llvm-project 26 #include "mlir/IR/Operation.h" // from @llvm-project 27 #include "mlir/IR/PatternMatch.h" // from @llvm-project 28 #include "mlir/Pass/Pass.h" // from @llvm-project 29 #include "mlir/Transforms/DialectConversion.h" // from @llvm-project 30 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project 31 #include "tensorflow/compiler/mlir/lite/ir/tfl_ops.h" 32 #include "tensorflow/compiler/mlir/lite/transforms/passes.h" 33 #include "tensorflow/compiler/mlir/lite/utils/variables_utils.h" 34 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" 35 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops_n_z.h" 36 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_saved_model.h" 37 38 namespace mlir { 39 namespace TFL { 40 namespace { 41 #define GEN_PASS_CLASSES 42 #include "tensorflow/compiler/mlir/lite/transforms/passes.h.inc" 43 44 // Attribute name to identify whether variables should be legalized to TFLite or 45 // not. 46 const char kLegalizeTflVariables[] = "tfl._legalize_tfl_variables"; 47 HasSupportedElementType(Operation * op)48bool HasSupportedElementType(Operation* op) { 49 return utils::IsSupportedVariableType(op); 50 } 51 IsSupportedElementType(ShapedType type)52bool IsSupportedElementType(ShapedType type) { 53 return utils::IsSupportedVariableType(type); 54 } 55 56 #include "tensorflow/compiler/mlir/lite/transforms/generated_legalize_variables.inc" 57 58 // Pass which legalizes TF variables which are already passed as bounded 59 // arguments to functions, to TFLite variables. 60 class LegalizeVariablesPass 61 : public LegalizeVariablesPassBase<LegalizeVariablesPass> { 62 public: MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(LegalizeVariablesPass)63 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(LegalizeVariablesPass) 64 65 void runOnOperation() override { 66 auto module = getOperation(); 67 // If TFLite variable legalization is not allowed, then we skip this pass. 68 if (auto legalize_tfl_variables_attr = 69 module->getAttr(kLegalizeTflVariables)) { 70 if (!legalize_tfl_variables_attr.cast<BoolAttr>().getValue()) return; 71 } 72 73 RewritePatternSet patterns(&getContext()); 74 populateWithGenerated(patterns); 75 (void)applyPatternsAndFoldGreedily(module, std::move(patterns)); 76 } 77 }; 78 79 } // namespace 80 CreateLegalizeVariablesPass()81std::unique_ptr<OperationPass<ModuleOp>> CreateLegalizeVariablesPass() { 82 return std::make_unique<LegalizeVariablesPass>(); 83 } 84 85 } // namespace TFL 86 } // namespace mlir 87