[Mlir-commits] [mlir] [mlir][SPIR-V] Convert math.cttz to spirv.GLFindILsb (PR #200455)

Igor Wodiany llvmlistbot at llvm.org
Fri May 29 12:47:07 PDT 2026


================
@@ -242,6 +242,45 @@ struct CountLeadingZerosPattern final
   }
 };
 
+/// Converts math.cttz to GL FindILsb. GL FindILsb returns -1 for a zero
+/// input while math.cttz must return the bitwidth, so the zero case is
+/// patched up with a select.
+struct CountTrailingZerosPattern final
+    : public OpConversionPattern<math::CountTrailingZerosOp> {
+  using Base::Base;
+
+  LogicalResult
+  matchAndRewrite(math::CountTrailingZerosOp countOp, OpAdaptor adaptor,
+                  ConversionPatternRewriter &rewriter) const override {
+    if (LogicalResult res = checkSourceOpTypes(rewriter, countOp); failed(res))
+      return res;
+
+    Type type = getTypeConverter()->convertType(countOp.getType());
+    if (!type)
+      return failure();
+
+    unsigned bitwidth = 0;
+    if (isa<IntegerType>(type))
+      bitwidth = type.getIntOrFloatBitWidth();
+    if (auto vectorType = dyn_cast<VectorType>(type))
----------------
IgWod wrote:

`else if`?

Also won't that accidentally allow `f32` vectors? Do we need to check that the element type is int? Or does type converter take care of it?

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


More information about the Mlir-commits mailing list