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

[enhancement] missed optimization opportunity for duplicated logic in sel branches #1482

Open
proppy opened this issue Jun 14, 2024 · 0 comments
Labels
enhancement New feature or request optimizer Related to IR optimization or analysis

Comments

@proppy
Copy link
Member

proppy commented Jun 14, 2024

What's hard to do? (limit 100 words)

When refactoring deep if branches for readability in DSLX code one sometime encapsulates logic into fns that get called w/ a few different parameters and return the same result type.

This can negatively impact the PPA:

  • logic get duplicated across sel branches, bumping the gate count and the resulting area.
  • sel would get wider to accomodate unified return type resulting in worst timing.

(convoluted) example

colab

The following code:

fn opt1(sign: u1, a: u8, b: u1) -> u8 {
    a + if sign {
        // sign positive: increment a w/ b
        b
    } else {
        // sign negative: invert increment
        !b
    } as u8
}

results in the following schedule:

Image

and the following BoM:

Image

While the "refactored" version:

fn sign_positive (a:u8, b:u1) -> u8 {
    a + b as u8
}

fn sign_negative(a:u8, b:u1) -> u8 {
    a + !b as u8
}

fn opt2(sign: u1, a: u8, b: u1) -> u8 {
    if sign == u1:0 {
        sign_positive(a, b)
    } else {
        sign_negative(a, b)
    }
}

results in a worst schedule (+40ps, because of the wider sel):

Image

and an increased BoM (1 additional add(u8, u8)):

Image

Current best alternative workaround (limit 100 words)

One workaround could be to add additional arguments to the refactored function to share some common computation done ahead of the sel and return intermediate result to narrow the size of sel; lessening encapsulation and potentially going back to negatively impacting readability.

Your view of the "best case XLS enhancement" (limit 100 words)

It would be nice if opt_main could detect sel w/ logic duplicated on each branches and move the common logic ahead of the sel.

This would likely result in sensible area saving and (limited) timing improvement.

@proppy proppy added enhancement New feature or request optimizer Related to IR optimization or analysis labels Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request optimizer Related to IR optimization or analysis
Projects
None yet
Development

No branches or pull requests

1 participant