You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add intrinsic for launch-sized workgroup memory on GPUs
Workgroup memory is a memory region that is shared between all
threads in a workgroup on GPUs. Workgroup memory can be allocated
statically or after compilation, when launching a gpu-kernel.
The intrinsic added here returns the pointer to the memory that is
allocated at launch-time.
# Interface
With this change, workgroup memory can be accessed in Rust by
calling the new `gpu_launch_sized_workgroup_mem<T>() -> *mut T`
intrinsic.
It returns the pointer to workgroup memory guaranteeing that it is
aligned to at least the alignment of `T`.
The pointer is dereferencable for the size specified when launching the
current gpu-kernel (which may be the size of `T` but can also be larger
or smaller or zero).
All calls to this intrinsic return a pointer to the same address.
See the intrinsic documentation for more details.
## Alternative Interfaces
It was also considered to expose dynamic workgroup memory as extern
static variables in Rust, like they are represented in LLVM IR.
However, due to the pointer not being guaranteed to be dereferencable
(that depends on the allocated size at runtime), such a global must be
zero-sized, which makes global variables a bad fit.
# Implementation Details
Workgroup memory in amdgpu and nvptx lives in address space 3.
Workgroup memory from a launch is implemented by creating an
external global variable in address space 3. The global is declared with
size 0, as the actual size is only known at runtime. It is defined
behavior in LLVM to access an external global outside the defined size.
There is no similar way to get the allocated size of launch-sized
workgroup memory on amdgpu an nvptx, so users have to pass this
out-of-band or rely on target specific ways for now.
// Generate an anonymous global per call, with these properties:
624
+
// 1. The global is in the address space for workgroup memory
625
+
// 2. It is an `external` global
626
+
// 3. It is correctly aligned for the pointee `T`
627
+
// All instances of extern addrspace(gpu_workgroup) globals are merged in the LLVM backend.
628
+
// The name is irrelevant.
629
+
// See https://docs.nvidia.com/cuda/cuda-c-programming-guide/#shared
630
+
let name = if llvm_version < (23,0,0) && tcx.sess.target.arch == Arch::Nvptx64{
631
+
// The auto-assigned name for extern shared globals in the nvptx backend does
632
+
// not compile in ptxas. Workaround this issue by assigning a name.
633
+
// Fixed in LLVM 23.
634
+
"gpu_launch_sized_workgroup_mem"
635
+
}else{
636
+
""
637
+
};
638
+
let global = self.declare_global_in_addrspace(
639
+
name,
640
+
self.type_array(self.type_i8(),0),
641
+
AddressSpace::GPU_WORKGROUP,
642
+
);
643
+
let ty::RawPtr(inner_ty, _) = result.layout.ty.kind()else{unreachable!()};
644
+
// The alignment of the global is used to specify the *minimum* alignment that
645
+
// must be obeyed by the GPU runtime.
646
+
// When multiple of these global variables are used by a kernel, the maximum alignment is taken.
647
+
// See https://github.com/llvm/llvm-project/blob/a271d07488a85ce677674bbe8101b10efff58c95/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp#L821
648
+
let alignment = self.align_of(*inner_ty).bytes()asu32;
649
+
unsafe{
650
+
// FIXME Workaround the above issue by taking maximum alignment if the global existed
0 commit comments