xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/tools/kernel_gen/transforms/utils.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/compiler/mlir/tools/kernel_gen/transforms/utils.h"
17 
18 #include <string>
19 
20 #include "llvm/Support/FormatVariadic.h"
21 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"  // from @llvm-project
22 
23 namespace mlir {
24 namespace kernel_gen {
25 namespace transforms {
26 
27 using LLVM::LLVMFuncOp;
28 
GetOrInsertLLVMFunction(StringRef func_name,Type func_type,Operation * op,OpBuilder * b)29 FlatSymbolRefAttr GetOrInsertLLVMFunction(StringRef func_name, Type func_type,
30                                           Operation* op, OpBuilder* b) {
31   auto module = op->getParentOfType<ModuleOp>();
32   auto tf_func = module.lookupSymbol<LLVMFuncOp>(func_name);
33   if (!tf_func) {
34     OpBuilder::InsertionGuard guard(*b);
35     b->setInsertionPointToStart(module.getBody());
36     tf_func = b->create<LLVMFuncOp>(b->getUnknownLoc(), func_name, func_type);
37   }
38   return SymbolRefAttr::get(b->getContext(), func_name);
39 }
40 
GetGlobalName(StringRef base,StringRef content)41 std::string GetGlobalName(StringRef base, StringRef content) {
42   return llvm::formatv("{0}_{1}", base, llvm::hash_value(content));
43 }
44 
CreateOrFindGlobalStringConstant(Location loc,StringRef global_name,StringRef content,OpBuilder * b)45 Value CreateOrFindGlobalStringConstant(Location loc, StringRef global_name,
46                                        StringRef content, OpBuilder* b) {
47   auto module =
48       b->getInsertionBlock()->getParentOp()->getParentOfType<ModuleOp>();
49   Operation* global_constant = SymbolTable::lookupNearestSymbolFrom(
50       module, b->getStringAttr(global_name));
51   if (global_constant) {
52     Value global_ptr = b->create<LLVM::AddressOfOp>(
53         loc, cast<LLVM::GlobalOp>(global_constant));
54     Value c0 =
55         b->create<LLVM::ConstantOp>(loc, b->getI64Type(), b->getIndexAttr(0));
56     return b->create<LLVM::GEPOp>(
57         loc, LLVM::LLVMPointerType::get(b->getIntegerType(8)), global_ptr,
58         ValueRange{c0, c0});
59   }
60   return LLVM::createGlobalString(loc, *b, global_name, content,
61                                   LLVM::Linkage::Internal);
62 }
63 
64 }  // namespace transforms
65 }  // namespace kernel_gen
66 }  // namespace mlir
67