[BUGFIX][TIR] Skip bool-typed expressions in CSE#19508
[BUGFIX][TIR] Skip bool-typed expressions in CSE#19508tqchen wants to merge 1 commit intoapache:mainfrom
Conversation
The TIR CSE pass currently lifts bool-typed sub-expressions like `i < n` or `a && b` into `cse_v: bool = ...` bindings whenever they appear twice. Boolean expressions are almost always predicates feeding `if` / `Select` / `assert`, where reading the condition inline is clearer than going through a boolean temporary, and where downstream simplification (ProveCondition, branch elimination) benefits from seeing the predicate directly. Extend CSEPlanner::IsEligible to reject any expression whose result dtype is_bool().
There was a problem hiding this comment.
Code Review
This pull request modifies the Common Subexpression Elimination (CSE) pass to exclude boolean-typed expressions from being hoisted, which preserves readability in control flow and enables more effective downstream simplification. Feedback suggests that the implementation should be updated to also exclude vectorized boolean expressions by checking for a 1-bit width.
| // the predicate directly. BoolImm is already filtered above as an IntImm | ||
| // leaf, so this rule only affects compound bool expressions | ||
| // (LT/LE/GT/GE/EQ/NE/And/Or/Not/Cast-to-bool/Select-of-bool). | ||
| if (expr.dtype().is_bool()) return false; |
There was a problem hiding this comment.
The current check expr.dtype().is_bool() only identifies scalar boolean expressions (typically uint1 with 1 lane). In TIR, boolean predicates are frequently vectorized (e.g., uint1x4 or uint1x8), especially when feeding into vectorized Select nodes.
Since the stated goal is to keep predicates inline to facilitate downstream simplification and readability, this logic should also apply to vectorized boolean expressions. Downstream passes like Simplify or ProveCondition often benefit from seeing the vectorized comparison directly within the Select condition.
Consider checking for a 1-bit width to cover both scalar and vector boolean types.
| if (expr.dtype().is_bool()) return false; | |
| if (expr.dtype().bits() == 1) return false; |
The TIR CSE pass currently lifts bool-typed sub-expressions like
i < nora && bintocse_v: bool = ...bindings whenever they appear twice. Boolean expressions are almost always predicates feedingif/Select/assert, where reading the condition inline is clearer than going through a boolean temporary, and where downstream simplification (ProveCondition, branch elimination) benefits from seeing the predicate directly.Extend CSEPlanner::IsEligible to reject any expression whose result dtype is_bool().