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/tensorflow/transforms/rewrite_util.h" 17 18 #include <string> 19 20 #include "tensorflow/core/util/device_name_utils.h" 21 22 namespace mlir { 23 namespace TF { 24 25 namespace { 26 27 const char kDeviceAttr[] = "device"; 28 const char kDeviceGpu[] = "GPU"; 29 GetOpDevice(mlir::Operation * op)30llvm::Optional<std::string> GetOpDevice(mlir::Operation *op) { 31 mlir::StringAttr device = op->getAttrOfType<mlir::StringAttr>(kDeviceAttr); 32 if (!device || device.getValue().empty()) { 33 return llvm::None; 34 } 35 tensorflow::DeviceNameUtils::ParsedName parsed_name; 36 if (!tensorflow::DeviceNameUtils::ParseFullName(device.str(), &parsed_name)) { 37 return llvm::None; 38 } 39 if (!parsed_name.has_type) { 40 return llvm::None; 41 } 42 return parsed_name.type; 43 } 44 45 } // namespace 46 IsOnGpuDevice(mlir::Operation * op)47bool IsOnGpuDevice(mlir::Operation *op) { 48 llvm::Optional<std::string> device = GetOpDevice(op); 49 if (!device) return false; 50 return *device == kDeviceGpu; 51 } 52 53 } // namespace TF 54 } // namespace mlir 55