xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/gpu/ir_emitter_context.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/Func/IR/FuncOps.h"  // from @llvm-project
21 #include "mlir/IR/MLIRContext.h"  // from @llvm-project
22 #include "tensorflow/compiler/xla/mlir_hlo/include/mlir-hlo/Dialect/lhlo/IR/lhlo_ops.h"
23 #include "tensorflow/compiler/xla/mlir_hlo/include/mlir-hlo/Dialect/lhlo_gpu/IR/lhlo_gpu_ops.h"
24 #include "tensorflow/compiler/xla/mlir_hlo/include/mlir-hlo/Dialect/mhlo/IR/hlo_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:
IrEmitterContext(const HloModule * hlo_module,const BufferAssignment * buffer_assignment,std::string platform_name,GpuDeviceInfo gpu_device_info,se::CudaComputeCapability cuda_compute_capability,se::RocmComputeCapability rocm_compute_capability,mlir::MLIRContext * mlir_context,llvm::Module * llvm_module)39   IrEmitterContext(const HloModule* hlo_module,
40                    const BufferAssignment* buffer_assignment,
41                    std::string platform_name, GpuDeviceInfo gpu_device_info,
42                    se::CudaComputeCapability cuda_compute_capability,
43                    se::RocmComputeCapability rocm_compute_capability,
44                    mlir::MLIRContext* mlir_context, llvm::Module* llvm_module)
45       : hlo_module_(hlo_module),
46         buffer_assignment_(buffer_assignment),
47         platform_name_(std::move(platform_name)),
48         gpu_device_info_(gpu_device_info),
49         cuda_compute_capability_(cuda_compute_capability),
50         rocm_compute_capability_(rocm_compute_capability),
51         mlir_context_(mlir_context),
52         llvm_module_(llvm_module) {}
53   // Disallow copy and assign.
54   IrEmitterContext(const IrEmitterContext&) = delete;
55   IrEmitterContext& operator=(const IrEmitterContext&) = delete;
56 
57   // Simple accessors.
hlo_module()58   const HloModule& hlo_module() const { return *hlo_module_; }
buffer_assignment()59   const BufferAssignment& buffer_assignment() const {
60     return *buffer_assignment_;
61   }
platform_name()62   absl::string_view platform_name() const { return platform_name_; }
gpu_device_info()63   GpuDeviceInfo gpu_device_info() const { return gpu_device_info_; }
cuda_compute_capability()64   se::CudaComputeCapability cuda_compute_capability() const {
65     return cuda_compute_capability_;
66   }
rocm_compute_capability()67   se::RocmComputeCapability rocm_compute_capability() const {
68     return rocm_compute_capability_;
69   }
mlir_context()70   mlir::MLIRContext* mlir_context() { return mlir_context_; }
llvm_module()71   llvm::Module* llvm_module() { return llvm_module_; }
name_uniquer()72   NameUniquer* name_uniquer() { return &name_uniquer_; }
73 
constants()74   std::vector<GpuExecutable::ConstantInfo>& constants() { return constants_; }
75 
allocations()76   absl::Span<const BufferAllocation> allocations() const {
77     if (buffer_assignment_) {
78       return buffer_assignment_->Allocations();
79     }
80     return allocations_;
81   }
82 
set_allocations(absl::Span<const BufferAllocation> allocations)83   void set_allocations(absl::Span<const BufferAllocation> allocations) {
84     CHECK_EQ(nullptr, buffer_assignment_);
85     allocations_ = allocations;
86   }
87 
88  private:
89   const HloModule* hlo_module_;
90   const BufferAssignment* buffer_assignment_;
91   absl::Span<const BufferAllocation> allocations_;
92   std::string platform_name_;
93   GpuDeviceInfo gpu_device_info_;
94   se::CudaComputeCapability cuda_compute_capability_;
95   se::RocmComputeCapability rocm_compute_capability_;
96   mlir::MLIRContext* mlir_context_;
97   llvm::Module* llvm_module_;
98   NameUniquer name_uniquer_;
99   std::vector<GpuExecutable::ConstantInfo> constants_;
100 };
101 
102 }  // namespace gpu
103 }  // namespace xla
104 
105 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_IR_EMITTER_CONTEXT_H_
106