Skip to content

Conversation

@fabianschuiki
Copy link
Contributor

Instead of only narrowing comb.and operations that have a comb.extract operand, do so on any and that has a constant operand with leading or trailing zeros. Since we consider comb.concat to be free, this canonicalization essentially turns a wide comb.and into a cheaper narrow comb.and plus an additional, free comb.concat.

This helps canonicalize Comb ops that implement an integer equivalent of hw.array_inject into corresponding extracts and concats.

In particular, this helps canonicalize

hw.module @Foo(in %a : i1, out z : i2) {
  %c1_i2 = hw.constant 1 : i2
  %c-2_i2 = hw.constant -2 : i2
  %false = hw.constant false
  %0 = comb.and %5, %c-2_i2 : i2
  %1 = comb.concat %false, %a : i1, i1
  %2 = comb.or %0, %1 : i2
  %3 = comb.and %2, %c1_i2 : i2
  %4 = comb.concat %a, %false : i1, i1
  %5 = comb.or %3, %4 : i2
  hw.output %5 : i2
}

into the more compact

hw.module @Foo(in %a : i1, out z : i2) {
  %false = hw.constant false
  %0 = comb.concat %false, %a : i1, i1
  %1 = comb.concat %a, %false : i1, i1
  %2 = comb.or %0, %1 : i2
  hw.output %2 : i2
}

Instead of only narrowing `comb.and` operations that have a
`comb.extract` operand, do so on any and that has a constant operand
with leading or trailing zeros. Since we consider `comb.concat` to be
free, this canonicalization essentially turns a wide `comb.and` into a
cheaper narrow `comb.and` plus an additional, free `comb.concat`.

This helps canonicalize Comb ops that implement an integer equivalent
of `hw.array_inject` into corresponding extracts and concats.

In particular, this helps canonicalize
```mlir
hw.module @foo(in %a : i1, out z : i2) {
  %c1_i2 = hw.constant 1 : i2
  %c-2_i2 = hw.constant -2 : i2
  %false = hw.constant false
  %0 = comb.and %5, %c-2_i2 : i2
  %1 = comb.concat %false, %a : i1, i1
  %2 = comb.or %0, %1 : i2
  %3 = comb.and %2, %c1_i2 : i2
  %4 = comb.concat %a, %false : i1, i1
  %5 = comb.or %3, %4 : i2
  hw.output %5 : i2
}
```
into the more compact
```mlir
hw.module @foo(in %a : i1, out z : i2) {
  %false = hw.constant false
  %0 = comb.concat %false, %a : i1, i1
  %1 = comb.concat %a, %false : i1, i1
  %2 = comb.or %0, %1 : i2
  hw.output %2 : i2
}
```
@fabianschuiki fabianschuiki added the Comb Involving the `comb` dialect label Aug 16, 2025
Copy link
Contributor

@TaoBi22 TaoBi22 left a comment

Choose a reason for hiding this comment

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

Thanks!

@fabianschuiki fabianschuiki merged commit 36ff2c5 into main Aug 18, 2025
7 checks passed
@fabianschuiki fabianschuiki deleted the fschuiki/narrow-and branch August 18, 2025 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Comb Involving the `comb` dialect

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants