[clang] [llvm] [HLSL] Move length support out of the DirectX Backend (PR #121611)

Justin Bogner via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 3 15:58:35 PST 2025


================
@@ -33,6 +41,21 @@ constexpr enable_if_t<sizeof(U) == sizeof(T), U> bit_cast(T F) {
   return __builtin_bit_cast(U, F);
 }
 
+template <typename T>
+constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
+length_impl(T X) {
+  return __builtin_elementwise_abs(X);
+}
+
+template <typename T, int N>
+enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
+length_vec_impl(vector<T, N> X) {
+  vector<T, N> XSquared = X * X;
+  T XSquaredSum = XSquared[0];
+  [unroll] for (int i = 1; i < N; ++i) XSquaredSum += XSquared[i];
----------------
bogner wrote:

clang-format doesn't seem to handle `[unroll]`, but this should probably be one of:
```c++
  [unroll]
  for (int i = 1; i < N; ++i)
    XSquaredSum += XSquared[i];
```
or
```c++
  [unroll] for (int i = 1; i < N; ++i)
    XSquaredSum += XSquared[i];
```

The latter is closer to what clang-format does with `[[unroll]]` instead of `[unroll]` to trick it into seeing that as an attribute.

(aside: I'm not sure if we have a bug for clang-format for this yet, we should check)

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


More information about the llvm-commits mailing list