xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/mlir/tensorflow/ir/tf_structs.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 // This file defines the types used in the standard MLIR TensorFlow dialect.
17 
18 #ifndef TENSORFLOW_COMPILER_MLIR_TENSORFLOW_IR_TF_STRUCTS_H_
19 #define TENSORFLOW_COMPILER_MLIR_TENSORFLOW_IR_TF_STRUCTS_H_
20 
21 #include "llvm/ADT/StringMap.h"
22 #include "mlir/IR/BuiltinTypes.h"  // from @llvm-project
23 #include "mlir/IR/Diagnostics.h"  // from @llvm-project
24 #include "mlir/IR/Location.h"  // from @llvm-project
25 #include "mlir/IR/Operation.h"  // from @llvm-project
26 #include "mlir/IR/Types.h"  // from @llvm-project
27 #include "tensorflow/core/ir/types/dialect.h"
28 #include "tensorflow/core/util/device_name_utils.h"
29 
30 namespace mlir {
31 namespace TF {
32 
33 using GpuDeviceMetadata = tf_type::GpuDeviceMetadataAttr;
34 
35 // Tensorflow devices available at runtime with corresponding metadata if it is
36 // available. It's completely valid to have a device without any metadata
37 // attached to it.
38 class RuntimeDevices {
39   using DeviceNameUtils = ::tensorflow::DeviceNameUtils;
40   using ParsedName = ::tensorflow::DeviceNameUtils::ParsedName;
41 
42  public:
43   // Adds a device with and empty metadata. Device can be of any type.
44   void AddDevice(const ParsedName& device);
45 
46   // Adds a GPU device with GPU specific metadata.
47   void AddGpuDevice(const ParsedName& device,
48                     const GpuDeviceMetadata& metadata);
49 
device_names()50   llvm::ArrayRef<ParsedName> device_names() const { return device_names_; }
NumDevices()51   size_t NumDevices() const { return device_names_.size(); }
52 
53   // Returns GPU device metadata if it is available, otherwise returns None.
54   llvm::Optional<GpuDeviceMetadata> GetGpuDeviceMetadata(
55       const ParsedName& device) const;
56 
57  private:
58   llvm::SmallVector<ParsedName, 8> device_names_;
59   // TODO(ezhulenev): Add DenseMapInfo<ParsedName> specialization to be able to
60   // use ParsedName as a key in a DenseMap.
61   llvm::StringMap<GpuDeviceMetadata> gpu_metadata_;
62 };
63 
64 }  // namespace TF
65 }  // namespace mlir
66 
67 #endif  // TENSORFLOW_COMPILER_MLIR_TENSORFLOW_IR_TF_STRUCTS_H_
68