Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hoist operations above selects if it enables constant folding #1611

Open
meheff opened this issue Sep 18, 2024 · 0 comments
Open

Hoist operations above selects if it enables constant folding #1611

meheff opened this issue Sep 18, 2024 · 0 comments
Labels
optimizer Related to IR optimization or analysis

Comments

@meheff
Copy link
Collaborator

meheff commented Sep 18, 2024

An operation in which all of its operations are selects on the same predicate can be hoisted above the select, for example:

negate(sel(p, [a, b])) => sel(p, [negate(a), negate(b)])

An obvious instance where this is advantageous is where the selected operands are constants which then enables constant folding (a and b are constants in the example).

Example IR which should be optimized but currently isn't:

package p

top fn foo(p: bits[1]) -> bits[8] {
  c0: bits[8] = literal(value=0xf0)
  c1: bits[8] = literal(value=0x0f)
  c2: bits[8] = literal(value=0xaa)
  c3: bits[8] = literal(value=0x55)
  op0: bits[8] = sel(p, cases=[c0, c1])
  op1: bits[8] = sel(p, cases=[c2, c3])
  ret sum: bits[8] = add(op0, op1)
}

This optimization is a special case of the following transform which moves operations across selects:

f(sel(p, [a_0, b_0, ...]), sel(p, [a_1, b_1,...]), ...)

   <=>

sel(p, [f(a_0, b_,0, ...), f(a_1, b_1, ...), ...])

Both directions may be possible/pprofitable. Fundamentally it's trading off multiple instances of a function with specialized parameters vs a single general instance plus/minus some select overhead. In general, there may be area/delay tradeoffs.

Related:
#938
#1068

@meheff meheff added the optimizer Related to IR optimization or analysis label Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimizer Related to IR optimization or analysis
Projects
None yet
Development

No branches or pull requests

1 participant