Skip to content

Intrinsic: dead.if.unused#8268

Open
kripken wants to merge 51 commits intoWebAssembly:mainfrom
kripken:callsIfMoved
Open

Intrinsic: dead.if.unused#8268
kripken wants to merge 51 commits intoWebAssembly:mainfrom
kripken:callsIfMoved

Conversation

@kripken
Copy link
Member

@kripken kripken commented Feb 5, 2026

Based on discussion in

#7574 (comment)

the @binaryen.dead.if.unused code annotation has the meaning that
if the result is unused (dropped), then the code can be considered
dead (no side effects, removable).

This can be used on a function to affect all calls to it, or on specific
call instructions. The optimizer then finds relevant dropped calls and
can remove them (in Vacuum).

Bikeshedding welcome on the name.

@kripken kripken requested a review from tlively February 5, 2026 16:44
src/wasm.h Outdated
// Toolchain hint: If this expression's result is unused, then the entire
// thing can be considered dead and removable.
// TODO: link to spec somewhere
std::optional<std::monostate> deadIfUnused;
Copy link
Member

Choose a reason for hiding this comment

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

Is it really better to use a std::optional rather than bool here? It doesn't look like we take advantage of the uniformity of using std::optional in any way.

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 think it's just nice for consistency. Each hint may be present or not, and handling that property uniformly with the same C++ mechanism seems clearer?

Copy link
Member

Choose a reason for hiding this comment

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

If any of the use sites had to change, I would agree. But I'm pretty sure making this a bool is a strict simplification, even if it is obviously different from the other hints. I think "std::optional if there is associated data and otherwise bool" is still a pretty simple guideline.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair enough, done.

Comment on lines 143 to 145
auto iter = annotations.find(target);
if (iter != annotations.end()) {
auto& annotation = iter->second;
Copy link
Member

Choose a reason for hiding this comment

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

It looks like it would be useful to have a func->getAnnotation(curr) helper of some sort to avoid mucking around with map lookups at every callsite. It could even return an empty annotations object when there is no annotation to avoid all branching logic in the caller.

It also seems somewhat magical that querying with a null expression gets the function annotations. I think ideally we would have a separate helper for that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a nicer helper now.

};

// Look for an annotation on the call.
if (checkDeadIfUnused(getFunction(), call)) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm imagining that this could be something like this:

Suggested change
if (checkDeadIfUnused(getFunction(), call)) {
if (getFunction()->getAnnotations(call).deadIfUnused) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's a good idea... I'll do some experimenting with a nicer API along those lines.

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)

Comment on lines 157 to 158
// Check on the called function, if it exists (it may not if the IR is
// still being built up).
Copy link
Member

Choose a reason for hiding this comment

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

When are we Vacuuming IR that is not yet constructed?

Copy link
Member Author

Choose a reason for hiding this comment

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

In wasm2js we apparently have situations where we optimize "partial" code, before the module is complete. We used to do that in asm2wasm back in the day (compile one function before later functions were even parsed) but we got rid of asm2wasm.

Copy link
Member

Choose a reason for hiding this comment

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

Do you think it would be possible to have wasm2js stop doing that?

Copy link
Member Author

Choose a reason for hiding this comment

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

It might not be trivial. Looks like the place is processStandaloneFunction which tests use to just handle a function outside of a module. The wasm2js testing stuff is... not simple, unfortunately.

// Check on the called function, if it exists (it may not if the IR is
// still being built up).
if (auto* target = getModule()->getFunctionOrNull(call->target)) {
if (checkDeadIfUnused(target, nullptr)) {
Copy link
Member

Choose a reason for hiding this comment

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

And I'm imagining something like this here:

Suggested change
if (checkDeadIfUnused(target, nullptr)) {
if (target->getFuncAnnotations().deadIfUnused) {

Copy link
Member

Choose a reason for hiding this comment

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

Should we check round-tripping of the function-level annotation as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added.

@@ -0,0 +1,88 @@
;; RUN: wasm-opt -all --vacuum %s -S -o - | filecheck %s
Copy link
Member

Choose a reason for hiding this comment

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

Can we autogenerate the output for this one?

Copy link
Member Author

Choose a reason for hiding this comment

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

It looked into this a little but it would be a huge refactoring of the update script. The issue is that module-level things like functions are all tracked by name, but this would not be a named thing, so a very different regex is needed, and different tracking to match it up to the right thing in the output. I did some experimentation, but it got very messy... Though maybe you know that script better and have an idea?

Copy link
Member

Choose a reason for hiding this comment

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

Oh I see. That's unfortunate. I can take a look at updating the script to handle annotations. We'll definitely want that for e.g. type annotations in the future.

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