Skip to content

[BUGFIX][TIR] Skip bool-typed expressions in CSE#19508

Closed
tqchen wants to merge 1 commit intoapache:mainfrom
tqchen:tvm-cse-should-not-lift-bool
Closed

[BUGFIX][TIR] Skip bool-typed expressions in CSE#19508
tqchen wants to merge 1 commit intoapache:mainfrom
tqchen:tvm-cse-should-not-lift-bool

Conversation

@tqchen
Copy link
Copy Markdown
Member

@tqchen tqchen commented May 6, 2026

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().

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().
@tqchen tqchen closed this May 6, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
if (expr.dtype().is_bool()) return false;
if (expr.dtype().bits() == 1) return false;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant