vtn/opencl: add ctz support

ctz is a CL2.0 opcode but 3.0 requires it as well so just add support
for it.

Tested against CTS integer_ops integer_ctz test.

(long line broken up)

Reviewed-by: Francisco Jerez <currojerez@riseup.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7468>
diff --git a/src/compiler/nir/nir_builtin_builder.h b/src/compiler/nir/nir_builtin_builder.h
index 0ddfcd1..a9fc9a5 100644
--- a/src/compiler/nir/nir_builtin_builder.h
+++ b/src/compiler/nir/nir_builtin_builder.h
@@ -261,6 +261,16 @@
    return nir_u2u(b, val, a->bit_size);
 }
 
+static inline nir_ssa_def *
+nir_ctz_u(nir_builder *b, nir_ssa_def *a)
+{
+   nir_ssa_def *cond = nir_ieq(b, a, nir_imm_intN_t(b, 0, a->bit_size));
+
+   return nir_bcsel(b, cond,
+                    nir_imm_intN_t(b, a->bit_size, a->bit_size),
+                    nir_u2u(b, nir_find_lsb(b, a), a->bit_size));
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/compiler/spirv/vtn_opencl.c b/src/compiler/spirv/vtn_opencl.c
index 301cb11..127fc9e 100644
--- a/src/compiler/spirv/vtn_opencl.c
+++ b/src/compiler/spirv/vtn_opencl.c
@@ -533,6 +533,8 @@
       return nir_normalize(nb, srcs[0]);
    case OpenCLstd_Clz:
       return nir_clz_u(nb, srcs[0]);
+   case OpenCLstd_Ctz:
+      return nir_ctz_u(nb, srcs[0]);
    case OpenCLstd_Select:
       return nir_select(nb, srcs[0], srcs[1], srcs[2]);
    case OpenCLstd_S_Upsample:
@@ -876,6 +878,7 @@
    case OpenCLstd_S_Upsample:
    case OpenCLstd_U_Upsample:
    case OpenCLstd_Clz:
+   case OpenCLstd_Ctz:
    case OpenCLstd_Native_exp:
    case OpenCLstd_Native_exp10:
    case OpenCLstd_Native_log: