1 /* Copyright 2020 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/tensorflow/ir/tf_op_interfaces.h"
17
18 namespace mlir {
19 namespace TF {
20
GetDeviceOrEmpty(Operation * op)21 llvm::StringRef GetDeviceOrEmpty(Operation *op) {
22 if (auto device_attr = op->getAttrOfType<StringAttr>("device"))
23 return device_attr.getValue();
24 return llvm::StringRef();
25 }
26
27 // Returns resource handle value and id for resource op based on attributes. If
28 // a resource handle is anonymous, a new id is always returned.
GetResourceHandleValueAndIdBase(llvm::StringRef container,llvm::StringRef shared_name,llvm::StringRef device,Value resource,llvm::SmallDenseMap<ResourceHandle,int64_t> & resource_handle_id_map,int64_t & next_id)29 ResourceHandleValueAndId GetResourceHandleValueAndIdBase(
30 llvm::StringRef container, llvm::StringRef shared_name,
31 llvm::StringRef device, Value resource,
32 llvm::SmallDenseMap<ResourceHandle, int64_t> &resource_handle_id_map,
33 int64_t &next_id) {
34 // Always create a new ID for anonymous handle.
35 if (IsResourceHandleAnonymous(shared_name)) return {resource, next_id++};
36
37 ResourceHandle handle(container, shared_name, device, /*op=*/nullptr);
38 auto emplace_res = resource_handle_id_map.try_emplace(handle, next_id);
39 // New ID created, increment next_id.
40 if (emplace_res.second) ++next_id;
41 return {resource, emplace_res.first->second};
42 }
43
44 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_op_interfaces.cc.inc"
45 } // namespace TF
46 } // namespace mlir
47