xref: /aosp_15_r20/external/tensorflow/tensorflow/core/transforms/legacy_call/pass.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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/legacy_call/pass.h"
17 
18 #include <memory>
19 
20 #include "mlir/IR/BuiltinAttributes.h"  // from @llvm-project
21 #include "mlir/IR/MLIRContext.h"  // from @llvm-project
22 #include "mlir/Pass/Pass.h"  // from @llvm-project
23 #include "mlir/Support/LogicalResult.h"  // from @llvm-project
24 #include "tensorflow/core/ir/interfaces.h"
25 #include "tensorflow/core/ir/ops.h"
26 #include "tensorflow/core/transforms/pass_detail.h"
27 
28 namespace mlir {
29 namespace tfg {
30 namespace {
31 class LiftLegacyCallPass : public LiftLegacyCallBase<LiftLegacyCallPass> {
32  public:
initialize(MLIRContext * context)33   LogicalResult initialize(MLIRContext *context) override {
34     tfg_legacy_call_id_ = StringAttr::get(context, "tfg.legacy_call");
35     return success();
36   }
37 
runOnOperation()38   void runOnOperation() override {
39     FunctionTable table(getOperation());
40     for (Operation &op : getOperation().getOps()) {
41       op.walk([&](Operation *op) {
42         if (op->hasTrait<OpTrait::IntrinsicOperation>() ||
43             !table.IsLegacyCall(op))
44           return;
45 
46         op->setAttr(tfg_legacy_call_id_,
47                     FlatSymbolRefAttr::get(&getContext(),
48                                            op->getName().stripDialect()));
49       });
50     }
51   }
52 
53  private:
54   // The cached identifier of the legacy call tag.
55   StringAttr tfg_legacy_call_id_;
56 };
57 }  // namespace
CreateLiftLegacyCallPass()58 std::unique_ptr<Pass> CreateLiftLegacyCallPass() {
59   return std::make_unique<LiftLegacyCallPass>();
60 }
61 }  // namespace tfg
62 }  // namespace mlir
63