Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/ir/module-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ template<typename T> struct CallGraphPropertyAnalysis {
struct FunctionInfo {
std::set<Function*> callsTo;
std::set<Function*> calledBy;
bool hasIndirectCall = false;
};

typedef std::map<Function*, T> Map;
Expand All @@ -362,6 +363,10 @@ template<typename T> struct CallGraphPropertyAnalysis {
info.callsTo.insert(module->getFunction(curr->target));
}

void visitCallIndirect(CallIndirect* curr) {
info.hasIndirectCall = true;
}

private:
Module* module;
T& info;
Expand All @@ -382,14 +387,20 @@ template<typename T> struct CallGraphPropertyAnalysis {
}
}

enum IndirectCalls { IgnoreIndirectCalls, IndirectCallsHaveProperty };

// Propagate a property from a function to those that call it.
void propagateBack(std::function<bool(const T&)> hasProperty,
std::function<bool(const T&)> canHaveProperty,
std::function<void(T&)> addProperty) {
std::function<void(T&)> addProperty,
IndirectCalls indirectCalls) {
// The work queue contains items we just learned can change the state.
UniqueDeferredQueue<Function*> work;
for (auto& func : wasm.functions) {
if (hasProperty(map[func.get()])) {
if (hasProperty(map[func.get()]) ||
(indirectCalls == IndirectCallsHaveProperty &&
map[func.get()].hasIndirectCall)) {
addProperty(map[func.get()]);
work.push(func.get());
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/passes/Asyncify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ class ModuleAnalyzer {
return !info.isBottomMostRuntime &&
!info.inBlacklist;
},
[](Info& info) { info.canChangeState = true; });
[](Info& info) { info.canChangeState = true; },
scanner.IgnoreIndirectCalls);

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.

It seems strange to use scanner in the path to IgnoreIndirectCalls, but I guess the enclosing type is a pain to type out, so this is fine.

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.

Yeah, this seemed shorter, but I'm not totally happy with it either. Another option might be get_type_or_whatsitcalled(scanner)::IgnoreIndirectCalls?


map.swap(scanner.map);

Expand Down
4 changes: 3 additions & 1 deletion src/passes/PostEmscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,11 @@ struct PostEmscripten : public Pass {
}
});

// Assume an indirect call might throw.
analyzer.propagateBack([](const Info& info) { return info.canThrow; },
[](const Info& info) { return true; },
[](Info& info) { info.canThrow = true; });
[](Info& info) { info.canThrow = true; },
analyzer.IndirectCallsHaveProperty);

// Apply the information.
struct OptimizeInvokes : public WalkerPass<PostWalker<OptimizeInvokes>> {
Expand Down
24 changes: 24 additions & 0 deletions test/passes/post-emscripten.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,27 @@
(nop)
)
)
(module
(type $none_=>_none (func))
(type $i32_i32_f32_=>_none (func (param i32 i32 f32)))
(type $i32_f32_=>_none (func (param i32 f32)))
(type $f64_f64_=>_f64 (func (param f64 f64) (result f64)))
(import "env" "glob" (global $glob i32))
(import "global.Math" "pow" (func $Math_pow (param f64 f64) (result f64)))
(import "env" "invoke_vif" (func $invoke_vif (param i32 i32 f32)))
(memory $0 256 256)
(table $0 7 7 funcref)
(elem (i32.const 0) $other_safe)
(func $exc (; 2 ;)
(call $invoke_vif
(i32.const 0)
(i32.const 42)
(f32.const 3.141590118408203)
)
)
(func $other_safe (; 3 ;) (param $0 i32) (param $1 f32)
(call_indirect (type $none_=>_none)
(i32.const 0)
)
)
)
23 changes: 22 additions & 1 deletion test/passes/post-emscripten.wast
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
(call $call)
)
)
(module
(module ;; non-constant base for elem
(type $0 (func (param i32)))
(import "global.Math" "pow" (func $Math_pow (param f64 f64) (result f64)))
(import "env" "invoke_vif" (func $invoke_vif (param i32 i32 f32)))
Expand All @@ -127,3 +127,24 @@
(func $other_safe (param i32) (param f32)
)
)
(module ;; indirect call in the invoke target, which we assume might throw
(type $none_=>_none (func))
(import "global.Math" "pow" (func $Math_pow (param f64 f64) (result f64)))
(import "env" "invoke_vif" (func $invoke_vif (param i32 i32 f32)))
(import "env" "glob" (global $glob i32)) ;; non-constant table offset
(memory 256 256)
(table 7 7 funcref)
(elem (i32.const 0) $other_safe)
(func $exc
(call $invoke_vif
(i32.const 0) ;; other_safe()
(i32.const 42)
(f32.const 3.14159)
)
)
(func $other_safe (param i32) (param f32)
(call_indirect (type $none_=>_none)
(i32.const 0)
)
)
)