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

C++: Fix forex recursion in sign analysis #18310

Merged

Conversation

MathiasVP
Copy link
Contributor

It's well-known that recursion through forall/forex may perform poorly. Apparently we do recursion through forex in the sign analysis library (which is part of the new range analysis library for C++). This results in some unfortunate tuple counts because the "range" part of the forall/forex gets joined with itself:

Tuple counts for _RangeAnalysisImpl::SignAnalysis::zeroBound/3#c119dcdb_RangeAnalysisImpl::SignAnalysis::zeroBound/3#__#antijoin_rhs/3@i4#L3#76559w2g after 1m28s:
  256000    ~900%         {4} r1 = JOIN `RangeAnalysisImpl::SignAnalysis::zeroBoundOk/3#a6688e7a#prev_delta` WITH num#Sign::TZero#65a6fcab CARTESIAN PRODUCT OUTPUT Lhs.0, Lhs.1 'arg1', Lhs.2 'arg2', Rhs.0
  251000    ~1835%        {3}    | JOIN WITH `RangeAnalysisImpl::SignAnalysis::zeroBound/3#c119dcdb` ON FIRST 3 OUTPUT Lhs.1 'arg1', Lhs.2 'arg2', Lhs.3 'arg0'
  692772000 ~2073%        {4}    | JOIN WITH `RangeAnalysisImpl::SignAnalysis::zeroBound/3#c119dcdb_120#join_rhs` ON FIRST 2 OUTPUT Rhs.2, Lhs.0 'arg1', Lhs.1 'arg2', Lhs.2 'arg0'
                          {4}    | AND NOT `RangeAnalysisImpl::SignAnalysis::zeroBoundOk/3#a6688e7a#prev`(FIRST 3)
  64314500  ~1238106%     {3}    | SCAN OUTPUT In.3 'arg0', In.1 'arg1', In.2 'arg2'

Luckily, there's a standard way to work around this problem by rewriting the forall/forex to recursion over integers. This PR does just that. This results in much better RA.

@Copilot Copilot bot review requested due to automatic review settings December 18, 2024 00:09
@MathiasVP MathiasVP requested a review from a team as a code owner December 18, 2024 00:09

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again, by re-requesting a review.

@github-actions github-actions bot added the C++ label Dec 18, 2024
@MathiasVP MathiasVP added the no-change-note-required This PR does not need a change note label Dec 18, 2024
@MathiasVP
Copy link
Contributor Author

DCA looks good. Zero result changes (as expected), and now the problematic project no longer times out 🎉

/**
* Gets a possible sign of `v` at read position `pos`, where a guard could have
* ruled out the sign but does not.
* This does not check that the definition of `v` also allows the sign.
*/
private Sign guardedSsaSignOk(SemSsaVariable v, SsaReadPosition pos) {
result = TPos() and
forex(SemExpr bound | posBound(bound, v, pos) | posBoundOk(bound, v, pos))
posBoundGuardedSsaSignOk(v, pos)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's leave in the original line as a comment:

Suggested change
posBoundGuardedSsaSignOk(v, pos)
// optimised version of
// `forex(SemExpr bound | posBound(bound, v, pos) | posBoundOk(bound, v, pos))`
posBoundGuardedSsaSignOk(v, pos)

Copy link
Contributor

Choose a reason for hiding this comment

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

Same for the others.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 4ffe70d

Copy link
Contributor

@aschackmull aschackmull left a comment

Choose a reason for hiding this comment

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

It's a shame that it's necessary, but LGTM.

@MathiasVP MathiasVP merged commit 927d359 into github:main Dec 18, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++ no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants