From e5fcd125a5cc1bd3e3a0063373e343b9beab6b14 Mon Sep 17 00:00:00 2001 From: Johnsonms Date: Thu, 5 Mar 2026 01:20:26 -0800 Subject: [PATCH] [fix] Boolean.__dsl_and__ emits arith.andi directly for i1 operands (#3087) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this fix, combining two Boolean (i1) DSL values with Python `and` triggered a verbose i1→i32→i1 round-trip in __dsl_and__: arith.extui (×3), arith.select, arith.cmpi ne (×2) — 6 extra MLIR ops. Add a fast path: when both operands are Boolean, delegate directly to __and__, emitting a single arith.andi %a, %b : i1 — identical to `&`. Both operators were already semantically equivalent; this fix makes the generated MLIR identical as well. Includes: - repro_dsl_and_bool.py — minimal standalone reproducer / bug-report script - test_dsl_and_fix.py — pytest tests verifying the fixed behaviour --- python/CuTeDSL/cutlass/base_dsl/typing.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/python/CuTeDSL/cutlass/base_dsl/typing.py b/python/CuTeDSL/cutlass/base_dsl/typing.py index 89fc6fb0..0ef742a5 100644 --- a/python/CuTeDSL/cutlass/base_dsl/typing.py +++ b/python/CuTeDSL/cutlass/base_dsl/typing.py @@ -1021,6 +1021,14 @@ class Numeric(metaclass=NumericMeta, is_abstract=True): 0 and 3 -> 0 3 and 0 and ... -> 0 """ + # Fast path: Boolean & Boolean → single arith.andi i1 instruction. + # The general path promotes i1 operands to i32 via arith.extui, performs + # arith.select, then converts back to i1 via arith.cmpi ne — generating + # 6 unnecessary MLIR operations. For Boolean inputs the semantics of + # `and` are identical to bitwise AND, so delegate directly to __and__. + if isinstance(self, Boolean) and isinstance(other, Boolean): + return self.__and__(other, loc=loc, ip=ip) + is_true = self.__dsl_bool__(loc=loc, ip=ip) def and_op(lhs, rhs):