xref: /aosp_15_r20/external/tensorflow/tensorflow/core/transforms/drop_unregistered_attribute/pass.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 "tensorflow/core/transforms/drop_unregistered_attribute/pass.h"
17 
18 #include <memory>
19 
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "mlir/Pass/Pass.h"  // from @llvm-project
23 #include "tensorflow/core/ir/ops.h"
24 #include "tensorflow/core/transforms/pass_detail.h"
25 
26 namespace mlir {
27 namespace tfg {
28 namespace {
29 
30 struct DropOutputShapesAttrPass
31     : DropOutputShapesAttrBase<DropOutputShapesAttrPass> {
initializemlir::tfg::__anonb5be1f130111::DropOutputShapesAttrPass32   LogicalResult initialize(MLIRContext* context) override {
33     for (auto& str : skip_) {
34       skip_id.insert(StringAttr::get(context, str));
35     }
36     return success();
37   }
runOnOperationmlir::tfg::__anonb5be1f130111::DropOutputShapesAttrPass38   void runOnOperation() override {
39     Operation* op = getOperation();
40     op->walk([this](Operation* op) {
41       if (!skip_id.count(op->getName().getIdentifier()))
42         op->removeAttr("_output_shapes");
43     });
44   }
45 
46   // Set of operation types to skip over.
47   DenseSet<StringAttr> skip_id;
48 };
49 
50 }  // namespace
51 
CreateDropOutputShapesAttrPass()52 std::unique_ptr<Pass> CreateDropOutputShapesAttrPass() {
53   return std::make_unique<DropOutputShapesAttrPass>();
54 }
55 
56 }  // namespace tfg
57 }  // namespace mlir
58