-
Notifications
You must be signed in to change notification settings - Fork 860
Use indirect call effects in LinearExecutionWalker #8738
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
base: main
Are you sure you want to change the base?
Changes from all commits
58eb5c2
253c092
2156d99
7facc2a
5aac82a
63d5515
2fd577a
979db05
cae5353
038480b
acb16ad
4633c60
1d75c25
b8567b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -80,7 +80,12 @@ struct LinearExecutionWalker : public PostWalker<SubType, VisitorType> { | |||||||||||
| static void scan(SubType* self, Expression** currp) { | ||||||||||||
| Expression* curr = *currp; | ||||||||||||
|
|
||||||||||||
| auto handleCall = [&](bool mayThrow, bool isReturn) { | ||||||||||||
| auto handleCall = [&](bool isReturn, const EffectAnalyzer* effects) { | ||||||||||||
| bool refutesThrowEffect = effects && !effects->throws_; | ||||||||||||
| bool mayThrow = !self->getModule() || | ||||||||||||
| self->getModule()->features.hasExceptionHandling(); | ||||||||||||
| mayThrow = mayThrow && !refutesThrowEffect; | ||||||||||||
|
|
||||||||||||
| if (!self->connectAdjacentBlocks) { | ||||||||||||
| // Control is nonlinear if we return or throw. Traps don't need to be | ||||||||||||
| // taken into account since they don't break control flow in a way | ||||||||||||
|
|
@@ -156,40 +161,52 @@ struct LinearExecutionWalker : public PostWalker<SubType, VisitorType> { | |||||||||||
| case Expression::Id::CallId: { | ||||||||||||
| auto* call = curr->cast<Call>(); | ||||||||||||
|
|
||||||||||||
| bool mayThrow = !self->getModule() || | ||||||||||||
| self->getModule()->features.hasExceptionHandling(); | ||||||||||||
| if (mayThrow && self->getModule()) { | ||||||||||||
| auto* effects = | ||||||||||||
| self->getModule()->getFunction(call->target)->effects.get(); | ||||||||||||
|
|
||||||||||||
| if (effects && !effects->throws_) { | ||||||||||||
| mayThrow = false; | ||||||||||||
| const EffectAnalyzer* effects = nullptr; | ||||||||||||
| if (self->getModule()) { | ||||||||||||
| auto* func = self->getModule()->getFunctionOrNull(call->target); | ||||||||||||
| if (func) { | ||||||||||||
| effects = func->effects.get(); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| handleCall(mayThrow, call->isReturn); | ||||||||||||
| handleCall(call->isReturn, effects); | ||||||||||||
| break; | ||||||||||||
| } | ||||||||||||
| case Expression::Id::CallRefId: { | ||||||||||||
| auto* callRef = curr->cast<CallRef>(); | ||||||||||||
|
|
||||||||||||
| // TODO: Effect analysis for indirect calls isn't implemented yet. | ||||||||||||
| // Assume any indirect call may throw for now. | ||||||||||||
| bool mayThrow = !self->getModule() || | ||||||||||||
| self->getModule()->features.hasExceptionHandling(); | ||||||||||||
| const EffectAnalyzer* effects = [&]() -> const EffectAnalyzer* { | ||||||||||||
| if (!self->getModule()) { | ||||||||||||
| return nullptr; | ||||||||||||
| } | ||||||||||||
| if (!callRef->target->type.isRef()) { | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might as well filter out
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hadn't thought of that, but I think we don't want to do that because that means that we'd fail to lookup effects for calls to these types and be overly conservative as a result. We want the lookup to succeed and see empty effects which is what happens today: binaryen/src/passes/GlobalEffects.cpp Lines 176 to 178 in 84ace4a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would expect these to get a |
||||||||||||
| return nullptr; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| handleCall(mayThrow, callRef->isReturn); | ||||||||||||
| auto* effects_ptr = | ||||||||||||
|
stevenfontanella marked this conversation as resolved.
|
||||||||||||
| find_or_null(self->getModule()->indirectCallEffects, | ||||||||||||
| callRef->target->type.getHeapType()); | ||||||||||||
| if (!effects_ptr) { | ||||||||||||
| return nullptr; | ||||||||||||
| } | ||||||||||||
| return effects_ptr->get(); | ||||||||||||
| }(); | ||||||||||||
|
|
||||||||||||
| handleCall(callRef->isReturn, effects); | ||||||||||||
| break; | ||||||||||||
| } | ||||||||||||
| case Expression::Id::CallIndirectId: { | ||||||||||||
| auto* callIndirect = curr->cast<CallIndirect>(); | ||||||||||||
|
|
||||||||||||
| // TODO: Effect analysis for indirect calls isn't implemented yet. | ||||||||||||
| // Assume any indirect call may throw for now. | ||||||||||||
| bool mayThrow = !self->getModule() || | ||||||||||||
| self->getModule()->features.hasExceptionHandling(); | ||||||||||||
|
|
||||||||||||
| handleCall(mayThrow, callIndirect->isReturn); | ||||||||||||
| const EffectAnalyzer* effects = nullptr; | ||||||||||||
| if (self->getModule()) { | ||||||||||||
| if (const auto& effects_ptr = | ||||||||||||
| find_or_null(self->getModule()->indirectCallEffects, | ||||||||||||
| callIndirect->heapType)) { | ||||||||||||
| effects = effects_ptr->get(); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| handleCall(callIndirect->isReturn, effects); | ||||||||||||
| break; | ||||||||||||
| } | ||||||||||||
| case Expression::Id::TryId: { | ||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect us to be able to assume that the target function exists, given that it would be invalid for it not to exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought so too, but when we run with --asyncify it causes us to fail when trying to lookup __asyncify_get_call_index. Maybe it's a problem with when the function is generated?
The repro is from test/lit/passes/asyncify_optimize-level=1.wast. The earlier code assumed that call targets exist but looks like it never hit an error before by luck.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (true || func)😆 what is going on there?It looks like Asyncify is temporarily adding calls to "intrinsics" that it later replaces with real code sequences. I think it should be fixed to add its "intrinsics" as actual function imports to avoid running other passes on invalid IR. But maybe that can be done as a follow-up. I'd be happy with a TODO here pointing to the problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just quickly added that as an equivalent to your snippet to test out!
Sounds good, will add a todo here and follow up.