From 01510f2c922af8629c7a730ef92a31fa83bd9f49 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 15 Oct 2025 20:59:47 -0700 Subject: Immutable access qualifier for pointers and use `__ldg` on cuda. (#8710) This PR implements `Access.Immutable` to allow pointers to immutable data. The new type `ImmutablePtr` is defined as an alias of `Ptr`. By forming a immutable pointer, the programmer is conveying to the compiler that the data at the pointer address will never change during the execution of the current program. Therefore loads from immutable pointers can be deduplicated by the compiler, and will translate to `__ldg` when generating code for CUDA. The SPIRV backend is not changed in this PR, since the current SPIRV spec makes it very difficult to specify loads from immutable address without generating tons of wrappers and boilerplate type declarations. We would like to see the spec evolved a bit to around its support of `NonWritable` physical storage pointers or immutable loads before we attempt to express such immutability in SPIRV. For now we simply emit ordinary pointers and loads when generating spirv. --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- prelude/slang-cuda-prelude.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'prelude') diff --git a/prelude/slang-cuda-prelude.h b/prelude/slang-cuda-prelude.h index 9508ea796..69d01920c 100644 --- a/prelude/slang-cuda-prelude.h +++ b/prelude/slang-cuda-prelude.h @@ -250,6 +250,23 @@ struct __align__(4) bool4 } }; +SLANG_FORCE_INLINE SLANG_CUDA_CALL bool __ldg(const bool* ptr) +{ + return (bool)(__ldg((const char*)ptr)); +} + +SLANG_FORCE_INLINE SLANG_CUDA_CALL bool2 __ldg(const bool2* ptr) +{ + auto val = __ldg((const char2*)ptr); + return {val.x != 0, val.y != 0}; +} + +SLANG_FORCE_INLINE SLANG_CUDA_CALL bool4 __ldg(const bool4* ptr) +{ + auto val = __ldg((const char4*)ptr); + return {val.x != 0, val.y != 0, val.z != 0, val.w != 0}; +} + #if SLANG_CUDA_RTC typedef signed char int8_t; -- cgit v1.2.3