[clang] [llvm] [HLSL] Implement the `fwidth` intrinsic for DXIL and SPIR-V target (PR #161378)

Farzon Lotfi via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 15 10:59:17 PDT 2025


================
@@ -148,6 +148,31 @@ template <typename T> constexpr T ldexp_impl(T X, T Exp) {
   return exp2(Exp) * X;
 }
 
+template <typename T> constexpr T fwidth_impl(T input) {
+#if (__has_builtin(__builtin_spirv_fwidth))
+  return __builtin_spirv_fwidth(input);
+#else
+  T derivCoarseX = __builtin_hlsl_elementwise_deriv_coarse_x(input);
+  derivCoarseX = abs(derivCoarseX);
+  T derivCoarseY = __builtin_hlsl_elementwise_deriv_coarse_y(input);
+  derivCoarseY = abs(derivCoarseY);
+  return derivCoarseX + derivCoarseY;
+#endif
+}
----------------
farzonl wrote:

I feel like fwidth_impl and `fwidth_vec_impl` could be merged into one function see how we did refract:
https://github.com/llvm/llvm-project/commit/056f0a10b320fc2fd75f46aa67d68708303d89ad#diff-f5dc7c12e2bb511ab1504961855fdd1e81dbeda43b8e3ee174819fb4a61c4acb

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


More information about the cfe-commits mailing list