[clang] [llvm] [HLSL][DXIL][SPIRV] WavePrefixSum intrinsic support (PR #167946)

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 20 06:13:34 PST 2025


================
@@ -49,6 +49,33 @@
     }                                                                          \
   }
 
+// A function generator macro for picking the right intrinsic for the target
+// backend given IsUnsigned boolean condition. If IsUnsigned == true, it calls
+// getUnsignedIntrinsicVariant(IID) to retrieve the unsigned variant of the
+// intrinsic else the regular intrinsic is returned. (NOTE:
+// getUnsignedIntrinsicVariant returns IID itself if there is no unsigned
+// variant).
+#define GENERATE_HLSL_INTRINSIC_FUNCTION_SELECT_UNSIGNED(FunctionName,         \
+                                                         IntrinsicPostfix)     \
+  llvm::Intrinsic::ID get##FunctionName##Intrinsic(bool IsUnsigned) {          \
+    llvm::Triple::ArchType Arch = getArch();                                   \
+    switch (Arch) {                                                            \
+    case llvm::Triple::dxil: {                                                 \
+      static constexpr llvm::Intrinsic::ID IID =                               \
+          llvm::Intrinsic::dx_##IntrinsicPostfix;                              \
+      return IsUnsigned ? getUnsignedIntrinsicVariant(IID) : IID;              \
+    }                                                                          \
+    case llvm::Triple::spirv: {                                                \
+      static constexpr llvm::Intrinsic::ID IID =                               \
+          llvm::Intrinsic::spv_##IntrinsicPostfix;                             \
+      return IsUnsigned ? getUnsignedIntrinsicVariant(IID) : IID;              \
+    }                                                                          \
+    default:                                                                   \
+      llvm_unreachable("Intrinsic " #IntrinsicPostfix                          \
+                       " not supported by target architecture");               \
+    }                                                                          \
+  }
----------------
kcloudy0717 wrote:

I just realized the approach for the signed unsigned dot product might not be applicable to wave prefix sum in this case. This is due to DXIL having both signed/unsigned variant of the intrinsic, but SPIRV only have a single intrinsic the macro will not compile if we had `usum` version. For now, this PR will use the same approach as `WaveActiveSum`, which is adding a static function in `CGHLSLBuiltins.cpp` that picks the right intrinsic.

@llvm-beanz suggested another change that could make it cleaner. For the purpose of this PR, I won't implement any refactors to existing code, it shall be done in another NFC branch.

https://github.com/llvm/llvm-project/pull/167946


More information about the llvm-commits mailing list