1 /* Copyright 2017 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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_IR_EMITTER_CONTEXT_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_IR_EMITTER_CONTEXT_H_ 18 19 #include "llvm/IR/Module.h" 20 #include "mlir/Dialect/StandardOps/IR/Ops.h" // from @llvm-project 21 #include "mlir/IR/MLIRContext.h" // from @llvm-project 22 #include "tensorflow/compiler/mlir/hlo/include/mlir-hlo/Dialect/mhlo/IR/hlo_ops.h" 23 #include "tensorflow/compiler/mlir/hlo/include/mlir-hlo/Dialect/mhlo/IR/lhlo_gpu_ops.h" 24 #include "tensorflow/compiler/mlir/hlo/include/mlir-hlo/Dialect/mhlo/IR/lhlo_ops.h" 25 #include "tensorflow/compiler/xla/service/buffer_assignment.h" 26 #include "tensorflow/compiler/xla/service/gpu/gpu_executable.h" 27 #include "tensorflow/compiler/xla/service/gpu/launch_dimensions.h" 28 #include "tensorflow/compiler/xla/service/hlo_execution_profile.h" 29 #include "tensorflow/compiler/xla/service/name_uniquer.h" 30 31 namespace xla { 32 namespace gpu { 33 34 // IrEmitterContext encapsulates common (mutable and immutable) data structures 35 // used by both IrEmitterNested and IrEmitterUnnested, such as the buffer 36 // assignment and the name uniquer. 37 class IrEmitterContext { 38 public: 39 // cuda_compute_capability is nullopt if we're not compiling for NVIDIA GPUs. IrEmitterContext(const HloModule * hlo_module,const BufferAssignment * buffer_assignment,std::string platform_name,GpuDeviceInfo gpu_device_info,absl::optional<CudaComputeCapability> cuda_compute_capability,const HloProfileIndexMap * profile_index_map,mlir::MLIRContext * mlir_context,llvm::Module * llvm_module)40 IrEmitterContext( 41 const HloModule* hlo_module, const BufferAssignment* buffer_assignment, 42 std::string platform_name, GpuDeviceInfo gpu_device_info, 43 absl::optional<CudaComputeCapability> cuda_compute_capability, 44 const HloProfileIndexMap* profile_index_map, 45 mlir::MLIRContext* mlir_context, llvm::Module* llvm_module) 46 : hlo_module_(hlo_module), 47 buffer_assignment_(buffer_assignment), 48 platform_name_(std::move(platform_name)), 49 gpu_device_info_(gpu_device_info), 50 cuda_compute_capability_(cuda_compute_capability), 51 profile_index_map_(profile_index_map), 52 mlir_context_(mlir_context), 53 llvm_module_(llvm_module) {} 54 // Disallow copy and assign. 55 IrEmitterContext(const IrEmitterContext&) = delete; 56 IrEmitterContext& operator=(const IrEmitterContext&) = delete; 57 58 // Simple accessors. hlo_module()59 const HloModule& hlo_module() const { return *hlo_module_; } buffer_assignment()60 const BufferAssignment& buffer_assignment() const { 61 return *buffer_assignment_; 62 } platform_name()63 absl::string_view platform_name() const { return platform_name_; } gpu_device_info()64 GpuDeviceInfo gpu_device_info() const { return gpu_device_info_; } cuda_compute_capability()65 absl::optional<CudaComputeCapability> cuda_compute_capability() const { 66 return cuda_compute_capability_; 67 } profile_index_map()68 const HloProfileIndexMap* profile_index_map() { return profile_index_map_; } mlir_context()69 mlir::MLIRContext* mlir_context() { return mlir_context_; } llvm_module()70 llvm::Module* llvm_module() { return llvm_module_; } name_uniquer()71 NameUniquer* name_uniquer() { return &name_uniquer_; } 72 constants()73 std::vector<GpuExecutable::ConstantInfo>& constants() { return constants_; } 74 allocations()75 absl::Span<const BufferAllocation> allocations() const { 76 if (buffer_assignment_) { 77 return buffer_assignment_->Allocations(); 78 } 79 return allocations_; 80 } 81 set_allocations(absl::Span<const BufferAllocation> allocations)82 void set_allocations(absl::Span<const BufferAllocation> allocations) { 83 CHECK_EQ(nullptr, buffer_assignment_); 84 allocations_ = allocations; 85 } 86 87 private: 88 const HloModule* hlo_module_; 89 const BufferAssignment* buffer_assignment_; 90 absl::Span<const BufferAllocation> allocations_; 91 std::string platform_name_; 92 GpuDeviceInfo gpu_device_info_; 93 absl::optional<CudaComputeCapability> cuda_compute_capability_; 94 const HloProfileIndexMap* profile_index_map_; 95 mlir::MLIRContext* mlir_context_; 96 llvm::Module* llvm_module_; 97 NameUniquer name_uniquer_; 98 std::vector<GpuExecutable::ConstantInfo> constants_; 99 }; 100 101 } // namespace gpu 102 } // namespace xla 103 104 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_IR_EMITTER_CONTEXT_H_ 105