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

Farzon Lotfi via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 15 11:03:09 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:

This pattern emerged because  there was previously a bug with using `select` but that was fixed and so you can see the new pattern in `ldexp_impl`, `faceforward_impl`, and, `lit_impl` is to just have one implementation.

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


More information about the llvm-commits mailing list