Skip to content

ConstraintAnalysis: Increment constants - #8969

Open
kripken wants to merge 13 commits into
WebAssembly:mainfrom
kripken:constraint.add
Open

ConstraintAnalysis: Increment constants#8969
kripken wants to merge 13 commits into
WebAssembly:mainfrom
kripken:constraint.add

Conversation

@kripken

@kripken kripken commented Aug 4, 2026

Copy link
Copy Markdown
Member

If we know x == C and have x++, we can set it to x == C + 1, etc.

A risk when computing this is that we might end up doing x++ from
0 to 1 to 2 to 3 and so forth, until we reach some high limit. Add a
mechanism to stop such incrementing after a linear amount of work.

This is necessary to compute loop overflows.

@kripken
kripken requested a review from tlively August 4, 2026 22:27
@kripken
kripken requested a review from a team as a code owner August 4, 2026 22:27
Comment thread src/ir/constraint.cpp

// Apply an increment of a local, x = y + 1.
Index y;
if (matches(value, binary(Abstract::Add, local(&y), ival(1)))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🎉 nice use of matches!

Comment thread src/ir/constraint.cpp
approximateAnd(index, c);
}

void BasicBlockConstraintMap::set(Index index,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We can redefine the existing set in terms of this one.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It might be less efficient, though. We can apply a single constraint without a loop, and without checking if the set is empty.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Though I guess inlining might make it fast. I'll simplify and then see if it shows up in profiles later.

Comment thread src/ir/constraint.cpp
Index y;
if (matches(value, binary(Abstract::Add, local(&y), ival(1)))) {
// The local y must have old constraints that we know how to increment.
auto old = get(y);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need to be making a copy here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, see how we modify the copy in-place, below.

Comment thread src/ir/constraint.cpp
case Eq:
*N = N->add(Literal::makeFromInt32(1, N->type));
continue;
// x >= N, x++ => x > N

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could simplify this by always just updating the constant (by adding one, although it looks like this would be pretty easy to generalize) and leaving the operator alone.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm, true. However, that would require checking for overflows in more places, and also make things more complicated later when we have non-constants (when x < y, x++, we can infer x <= y without incrementing anything, and in fact can't increment).

Comment on lines +466 to +469
// when it reaches the loop bound, which may be very high. We don't want to
// spend significant time on such constant operations, as other passes will
// propagate them anyhow, so we verify that we don't apply such x = y + 1
// operations too many times.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was expecting to see a new "widening" mechanism to prevent unbounded iteration, but I don't see it. Am I missing something?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You are seeing the future, for that is in the next PR 😄

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.

2 participants