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

[analysis] Implement a vector lattice #6058

Merged
merged 2 commits into from
Oct 31, 2023
Merged

[analysis] Implement a vector lattice #6058

merged 2 commits into from
Oct 31, 2023

Conversation

tlively
Copy link
Member

@tlively tlively commented Oct 27, 2023

The vector lattice is nearly identical to the array lattice, except that the
size of the elements is specified at runtime when the lattice object is created
rather than at compile time. The code and tests are largely copy-pasted and
fixed up from the array implementation, but there are a couple differences.
First, initializing vector elements does not need the template magic used to
initialize array elements. Second, the obvious implementations of join and meet
do not work for vectors of bools because they might be specialized to be bit
vectors, so we need workarounds for that particular case.

}
result = GREATER;
continue;
}
Copy link
Member

Choose a reason for hiding this comment

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

That we copy-paste this switch logic is kind of disappointing. I'd have hoped with all the template magic there would be a way to avoid duplication? The same for the functions below.

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean the copy-paste between the array and vector lattices? I could probably try to use even more template magic to reduce the duplication, but I think at that point we could start calling it template obfuscation 😅

Copy link
Member

Choose a reason for hiding this comment

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

Can they both inherit from something?

Or perhaps they can both simply call a standalone utility class that does these operations?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could perhaps have a CRTP parent class, but I don't think it's worth it. These methods are simple enough that I think there's more value in being able to read their implementations directly without an extra layer of source indirection than there would be in deduplicating them.

Copy link
Member

Choose a reason for hiding this comment

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

What would be the harm in adding a utility function (not a CRTP parent class) "compareSingleLatticeElement" that contains the switch, and is called from those loops? Those functions would become much smaller and more readable, and we'd avoid the duplication.

Copy link
Member Author

Choose a reason for hiding this comment

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

The types are different, so at least that utility would have to be another template. It would also have to go in some new header file but would not be part of any public API, which is unfortunate. I don't anticipate the utility would be used anywhere except these two places, either.

I can do it if you feel strongly about it, but I don't think the extra complexity is worth the deduplication.

Copy link
Member

Choose a reason for hiding this comment

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

I don't feel that strongly about it, I suppose.

elem.push_back(l->lattice.makeElement());
}
return ElementImpl{std::move(elem)};
}
WASM_UNREACHABLE("unexpected lattice");
Copy link
Member

Choose a reason for hiding this comment

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

I won't ask for a change here, but I really do think virtual would be a nicer way to write code like this, rather than have to keep modifying RandomFullLattice for each type. That is, I would personally gone the other route and had a virtual method that this class calls, each new lattice just implements a "makeRandom" interface, so it all plugs in.

Copy link
Member Author

Choose a reason for hiding this comment

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

I plan to at least get rid of the distinction between RandomFullLattice and RandomLattice, which should simplify things.

I can also clean up this giant if-else chain by using std::visit combined with overloaded free functions, which will be closer to what you're envisioning, I think. I don't think that going all the way to creating an inheritance-based wrapper for each of the lattices would be worth it, though.

@tlively
Copy link
Member Author

tlively commented Oct 31, 2023

Merge activity

  • Oct 30, 10:45 PM: @tlively started a stack merge that includes this pull request via Graphite.
  • Oct 30, 10:45 PM: Graphite rebased this pull request as part of a merge.
  • Oct 30, 11:29 PM: @tlively merged this pull request with Graphite.

Base automatically changed from array-lattice to main October 31, 2023 02:45
The vector lattice is nearly identical to the array lattice, except that the
size of the elements is specified at runtime when the lattice object is created
rather than at compile time. The code and tests are largely copy-pasted and
fixed up from the array implementation, but there are a couple differences.
First, initializing vector elements does not need the template magic used to
initialize array elements. Second, the obvious implementations of join and meet
do not work for vectors of bools because they might be specialized to be bit
vectors, so we need workarounds for that particular case.
@tlively tlively merged commit 220196e into main Oct 31, 2023
14 checks passed
@tlively tlively deleted the vector-lattice branch October 31, 2023 03:29
radekdoulik pushed a commit to dotnet/binaryen that referenced this pull request Jul 12, 2024
The vector lattice is nearly identical to the array lattice, except that the
size of the elements is specified at runtime when the lattice object is created
rather than at compile time. The code and tests are largely copy-pasted and
fixed up from the array implementation, but there are a couple differences.
First, initializing vector elements does not need the template magic used to
initialize array elements. Second, the obvious implementations of join and meet
do not work for vectors of bools because they might be specialized to be bit
vectors, so we need workarounds for that particular case.
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