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

Unify method pairs with and without Type param #6184

Merged
merged 6 commits into from
Dec 21, 2023

Conversation

aheejin
Copy link
Member

@aheejin aheejin commented Dec 19, 2023

As suggested in
#6181 (comment), using std::optional<Type>, this unifies two different versions of make***, for block-like structures (block, if, loop, try, and try_table) with and without a type parameter.

This also allows unifying of finalize methods, with and without a type. This also sets breakability argument of Block::finalize to Unknown so we can only have one Block::finalize that handles all cases.

This also adds an optional std::optional<Type> type parameter to blockifyWithName, and makeSequence functions in wasm-builder.h. blockify was not included because it has a variadic parameter.

As suggested in
WebAssembly#6181 (comment),
using `std::optional<Type>`, this unifies two different versions of
`make***`, for  block-like structures (`block`, `if`, `loop`, `try`, and
`try_table`) with and without a type parameter.

This also allows unifying of `finalize` methods, with and without a
type. This also sets `breakability` argument of `Block::finalize` to
`Unknown` so we can only have one `Block::finalize` that handles all
cases.

This also adds an optional `std::optional<Type> type` parameter to
`blockify`, `blockifyWithName`, and `makeSequence` functions in
`wasm-builder.h`.
@aheejin aheejin changed the title Unify methods pairs with and without Type param Unify method pairs with and without Type param Dec 19, 2023
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

Nice! I had only thought to do this in wasm-builder so very good idea to also do it in wasm.h as well.

lgtm with early returns + also please do a quick check to see this does not regress speed.

type = *type_;
if (type == Type::none && list.size() > 0) {
handleUnreachable(this, breakability);
}
Copy link
Member

Choose a reason for hiding this comment

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

Maybe early-return after this to avoid indenting all the rest in an else?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. Also did the same for If::finalize. (Others (Loop, Try, and TryTable) were kinda small and didn't seem worth it)

Comment on lines 1288 to 1290
Block* blockify(Expression* any,
Expression* append = nullptr,
std::optional<Type> type = std::nullopt) {
Copy link
Member

Choose a reason for hiding this comment

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

Oh neat, does this mean we can pass a type to the variadic version of blockify below now, too?

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 thought so, but it doesn't seem to work... It continues to blockify(any, append) and errors out as soon as the append is not Expression*, like this:

/usr/local/google/home/aheejin/binaryen/src/wasm-builder.h:1307:12: error: no matc
hing member function for call to 'blockify'
 1307 |     return blockify(blockify(any, append), args...);
      |            ^~~~~~~~
/usr/local/google/home/aheejin/binaryen/src/passes/StackCheck.cpp:112:25: note: in
 instantiation of function template specialization 'wasm::Builder::blockify<wasm::
Type>' requested here
  112 |     auto *ret = builder.blockify(check, newSet, Type(Type::i32));
      |                         ^
/usr/local/google/home/aheejin/binaryen/src/wasm-builder.h:1288:10: note: candidat
e function not viable: no known conversion from 'wasm::Type' to 'Expression *' for
 2nd argument
 1288 |   Block* blockify(Expression* any,
      |          ^
 1289 |                   Expression* append = nullptr,
      |                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/google/home/aheejin/binaryen/src/wasm-builder.h:1306:10: note: candidat
e function template not viable: no known conversion from 'wasm::Type' to 'Expressi
on *' for 2nd argument
 1306 |   Block* blockify(Expression* any, Expression* append, Ts... args) {
      |          ^                         ~~~~~~~~~~~~~~~~~~
1 error generated.

I guess I should revert the blockify part. blockifyWithName should be fine given that it doesn't use a variadic parameter though.

Comment on lines 201 to 204
// Calculate the supertype of the branch types and the flowed-out type. If
// there is no supertype among the available types, assume the current
// type is already correct. TODO: calculate proper LUBs to compute a new
// correct type in this situation.
Copy link
Member

Choose a reason for hiding this comment

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

This comment looks extremely stale since we do calculate LUBs now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

Comment on lines 884 to 885
// If none of the component bodies' type is a supertype of the others,
// assume the current type is already correct. TODO: Calculate a proper LUB.
Copy link
Member

Choose a reason for hiding this comment

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

Also stale.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@aheejin
Copy link
Member Author

aheejin commented Dec 21, 2023

@kripken

also please do a quick check to see this does not regress speed.

I ran check.py (% wasm-reduce test, which takes too long time) on main and this branch on my local machine, on a release build, and the time it took (in 3 averaged runs) were 191.44s (main) vs 191.11s (this), which doesn't seem to be a meaningful difference.

@aheejin aheejin merged commit 98cef80 into WebAssembly:main Dec 21, 2023
14 checks passed
@aheejin aheejin deleted the unify_pairs branch December 21, 2023 01:38
radekdoulik pushed a commit to dotnet/binaryen that referenced this pull request Jul 12, 2024
As suggested in
WebAssembly#6181 (comment),
using `std::optional<Type>`, this unifies two different versions of
`make***`, for  block-like structures (`block`, `if`, `loop`, `try`, and
`try_table`) with and without a type parameter.

This also allows unifying of `finalize` methods, with and without a
type. This also sets `breakability` argument of `Block::finalize` to
`Unknown` so we can only have one `Block::finalize` that handles all
cases.

This also adds an optional `std::optional<Type> type` parameter to
`blockifyWithName`, and `makeSequence` functions in `wasm-builder.h`.
blockify was not included because it has a variadic parameter.
@gkdn gkdn mentioned this pull request Aug 31, 2024
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.

3 participants