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

[FIRRTL][InferWidths] Fix back-prop, fix ref equality, fix upper bound. #5403

Conversation

dtzSiFive
Copy link
Contributor

Fix upperBoundSolution being set from bool indicating if cycle, instead of the value solved for (.first instead of .second).

Tweak how/when upper bound constraint is solved.

Fixes #5002.
Fixes #5391.

Fix upperBoundSolution being set from bool indicating if cycle,
instead of the value solved for (.first instead of .second).

Tweak how/when upper bound constraint is solved.

Fixes llvm#5002.
Fixes llvm#5391.
// This should only happen if somehow the constraint is
// solved before visiting this expression, so that our upperBound
// was not added to the worklist such that it was handled first.
if (expr->upperBound && solvedExprs.contains(expr->upperBound))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure we don't need to handle this re:seenVars and checkCycles/etc.?
Or if constraint expressions can be re-used such that we could have solution for the constraint w/o having put the upper bound on the worklist first?

@@ -1819,7 +1821,7 @@ void InferenceMapping::constrainTypes(Value larger, Value smaller) {
} else if (type.isGround()) {
// Leaf element, look up their expressions, and create the constraint.
constrainTypes(getExpr(FieldRef(larger, fieldID)),
getExpr(FieldRef(smaller, fieldID)));
getExpr(FieldRef(smaller, fieldID)), false, equal);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't love this re:bools and ergonomics but wanted to get this reviewed for functionality first, but once that seems good suggestions on improvements welcome (please!). FWIW-- imposeUpperBounds adds LEQ in compatible way to GEQ (if x >= y is requested, it'll add y <= x if x isn't free variable), where-as "equal" causes LEQ (x >= y && x <= y) to be added. Anyway.

@dtzSiFive
Copy link
Contributor Author

Note that the discussion on whether widthCast should actually/correctly be "Pure" still is open, but at least this avoids known inference mistakes.

Copy link
Contributor

@fabianschuiki fabianschuiki left a comment

Choose a reason for hiding this comment

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

This looks good to me! Since this is mostly touching the upperBound, which isn't used for most of the widths, this shouldn't have any impact on existing width inference.

I wish there was a saner way to do width inference overall than building this shadow expression graph and then running this custom solver. Problem for another day 😉

@dtzSiFive
Copy link
Contributor Author

This looks good to me! Since this is mostly touching the upperBound, which isn't used for most of the widths, this shouldn't have any impact on existing width inference.

FWIW this explicitly fixes a bug where we inferred wrong widths, not involving upper bound, mostly because the operation-visiting code put constraints in both directions so we propagated the wrong way (perhaps because didn't have a way to express what it wanted re:new equal).
But insofar as this means "impact on existing width inference [done by solver]", totally and agreed re:finding a better way 😄 .

@dtzSiFive dtzSiFive merged commit 6c01320 into llvm:main Jun 14, 2023
5 checks passed
@dtzSiFive dtzSiFive deleted the fix/infer-widths/backprop-upperbound-and-refs-oh-my branch June 14, 2023 22:04
@dtzSiFive
Copy link
Contributor Author

tyvm for review and discussion/explanation!

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.

[FIRRTL] Width inference bug as of 1.40.0 [InferWidths] Constrain ref.define operands to same width
2 participants