|
1 | 1 | use std::assert_matches::assert_matches; |
2 | 2 | use std::cmp::Ordering; |
3 | 3 |
|
4 | | -use rustc_abi::{Align, BackendRepr, ExternAbi, Float, HasDataLayout, Primitive, Size}; |
| 4 | +use rustc_abi::{ |
| 5 | + AddressSpace, Align, BackendRepr, ExternAbi, Float, HasDataLayout, Primitive, Size, |
| 6 | +}; |
5 | 7 | use rustc_codegen_ssa::base::{compare_simd_types, wants_msvc_seh, wants_wasm_eh}; |
6 | 8 | use rustc_codegen_ssa::codegen_attrs::autodiff_attrs; |
7 | 9 | use rustc_codegen_ssa::common::{IntPredicate, TypeKind}; |
@@ -539,6 +541,31 @@ impl<'ll, 'tcx> IntrinsicCallBuilderMethods<'tcx> for Builder<'_, 'll, 'tcx> { |
539 | 541 | return Ok(()); |
540 | 542 | } |
541 | 543 |
|
| 544 | + sym::gpu_dynamic_groupshared_mem => { |
| 545 | + // The name of the global variable is not relevant, the important properties are. |
| 546 | + // 1. The global is in the shared address space |
| 547 | + // 2. It is an extern global |
| 548 | + // All instances of extern addrspace(shared) globals are merged in the LLVM backend. |
| 549 | + // See https://docs.nvidia.com/cuda/cuda-c-programming-guide/#shared |
| 550 | + let global = self.declare_global_in_addrspace( |
| 551 | + "gpu_dynamic_groupshared_mem", |
| 552 | + self.type_array(self.type_i8(), 0), |
| 553 | + AddressSpace::GPU_SHARED, |
| 554 | + ); |
| 555 | + let ty::RawPtr(inner_ty, _) = result.layout.ty.kind() else { unreachable!() }; |
| 556 | + // The alignment of the global is used to specify the *minimum* alignment that the |
| 557 | + // must be obeyed by the GPU runtime. |
| 558 | + // When multiple of these global variables are merged, the maximum alignment is taken. |
| 559 | + // See https://github.com/llvm/llvm-project/blob/a271d07488a85ce677674bbe8101b10efff58c95/llvm/lib/Target/AMDGPU/AMDGPULowerModuleLDSPass.cpp#L821 |
| 560 | + let alignment = self.align_of(*inner_ty).bytes() as u32; |
| 561 | + unsafe { |
| 562 | + if alignment > llvm::LLVMGetAlignment(global) { |
| 563 | + llvm::LLVMSetAlignment(global, alignment); |
| 564 | + } |
| 565 | + } |
| 566 | + self.cx().const_pointercast(global, self.type_ptr()) |
| 567 | + } |
| 568 | + |
542 | 569 | _ if name.as_str().starts_with("simd_") => { |
543 | 570 | // Unpack non-power-of-2 #[repr(packed, simd)] arguments. |
544 | 571 | // This gives them the expected layout of a regular #[repr(simd)] vector. |
|
0 commit comments