xref: /aosp_15_r20/external/tensorflow/tensorflow/dtensor/mlir/function_renaming.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 <string>
17 
18 #include "mlir/Dialect/Func/IR/FuncOps.h"  // from @llvm-project
19 #include "mlir/IR/Attributes.h"  // from @llvm-project
20 #include "mlir/IR/Operation.h"  // from @llvm-project
21 #include "mlir/IR/SymbolTable.h"  // from @llvm-project
22 #include "mlir/Pass/Pass.h"  // from @llvm-project
23 #include "tensorflow/dtensor/cc/constants.h"
24 #include "tensorflow/dtensor/mlir/dtensor_mlir_passes.h"
25 #include "tensorflow/dtensor/mlir/dtensor_mlir_passes_classes.h"
26 
27 namespace tensorflow {
28 
29 namespace dtensor {
30 namespace {
31 #define GEN_PASS_CLASSES
32 #include "tensorflow/dtensor/mlir/dtensor_passes.h.inc"
33 
34 struct DTensorFunctionRenaming
35     : public DTensorFunctionRenamingBase<DTensorFunctionRenaming> {
runOnOperationtensorflow::dtensor::__anonb121fce60111::DTensorFunctionRenaming36   void runOnOperation() override {
37     mlir::ModuleOp module = getOperation();
38 
39     const std::string append =
40         module->getAttrOfType<mlir::StringAttr>(dtensor::kCacheKey)
41             .getValue()
42             .str();
43 
44     // If the cache key isn't set, simply return without renameing functions.
45     if (append.empty()) return;
46 
47     mlir::SymbolTableCollection symbol_table;
48     mlir::SymbolUserMap symbolUsers(symbol_table, module);
49 
50     for (mlir::func::FuncOp func_op :
51          llvm::make_early_inc_range(module.getOps<mlir::func::FuncOp>())) {
52       // Only rename private functions, functions which are public (i.e. the
53       // main function of the module), must have stable names since they are
54       // public and may be used by other modules/pieces of code.
55       if (func_op.getVisibility() != mlir::SymbolTable::Visibility::Private)
56         continue;
57       std::string new_name = absl::StrCat(
58           mlir::SymbolTable::getSymbolName(func_op).getValue().str(), append);
59       symbolUsers.replaceAllUsesWith(
60           func_op, mlir::StringAttr::get(&getContext(), new_name));
61       mlir::SymbolTable::setSymbolName(func_op, new_name);
62     }
63   };
64 };
65 
66 }  // namespace
67 
68 std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>>
CreateFunctionRenamingPass()69 CreateFunctionRenamingPass() {
70   return std::make_unique<DTensorFunctionRenaming>();
71 }
72 
73 }  // namespace dtensor
74 }  // namespace tensorflow
75