Skip to content

Commit 0c23394

Browse files
authored
LogExecution: Optionally take a module name for the logger function (#6629)
--log-execution=NAME will use NAME as the module for the logger function import, rather than infer it. If the name is not provided (--log-execution as before this PR) then we will try to automatically decide which to use ("env", unless we see another module name is used, which can be the case in optimized modules).
1 parent 18bb1f5 commit 0c23394

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

src/passes/LogExecution.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,18 @@ namespace wasm {
3939
Name LOGGER("log_execution");
4040

4141
struct LogExecution : public WalkerPass<PostWalker<LogExecution>> {
42+
// The module name the logger function is imported from.
43+
IString loggerModule;
44+
4245
// Adds calls to new imports.
4346
bool addsEffects() override { return true; }
4447

48+
void run(Module* module) override {
49+
auto& options = getPassOptions();
50+
loggerModule = options.getArgumentOrDefault("log-execution", "");
51+
super::run(module);
52+
}
53+
4554
void visitLoop(Loop* curr) { curr->body = makeLogCall(curr->body); }
4655

4756
void visitReturn(Return* curr) { replaceCurrent(makeLogCall(curr)); }
@@ -63,23 +72,32 @@ struct LogExecution : public WalkerPass<PostWalker<LogExecution>> {
6372
auto import =
6473
Builder::makeFunction(LOGGER, Signature(Type::i32, Type::none), {});
6574

66-
// Import the log function from import "env" if the module
67-
// imports other functions from that name.
68-
for (auto& func : curr->functions) {
69-
if (func->imported() && func->module == ENV) {
70-
import->module = func->module;
71-
break;
72-
}
73-
}
74-
75-
// If not, then pick the import name of the first function we find.
76-
if (!import->module) {
75+
if (loggerModule != "") {
76+
import->module = loggerModule;
77+
} else {
78+
// Import the log function from import "env" if the module
79+
// imports other functions from that name.
7780
for (auto& func : curr->functions) {
78-
if (func->imported()) {
81+
if (func->imported() && func->module == ENV) {
7982
import->module = func->module;
8083
break;
8184
}
8285
}
86+
87+
// If not, then pick the import name of the first function we find.
88+
if (!import->module) {
89+
for (auto& func : curr->functions) {
90+
if (func->imported()) {
91+
import->module = func->module;
92+
break;
93+
}
94+
}
95+
}
96+
97+
// If no function was found, use ENV.
98+
if (!import->module) {
99+
import->module = ENV;
100+
}
83101
}
84102

85103
import->base = LOGGER;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
2+
;; Test the option to provide the module name as an argument.
3+
;; RUN: foreach %s %t wasm-opt --log-execution=foo -S -o - | filecheck %s
4+
5+
(module
6+
;; CHECK: (type $0 (func))
7+
8+
;; CHECK: (type $1 (func (param i32)))
9+
10+
;; CHECK: (import "env" "func" (func $import))
11+
(import "env" "func" (func $import))
12+
;; CHECK: (import "foo" "log_execution" (func $log_execution (param i32)))
13+
14+
;; CHECK: (func $nopp
15+
;; CHECK-NEXT: (call $log_execution
16+
;; CHECK-NEXT: (i32.const 0)
17+
;; CHECK-NEXT: )
18+
;; CHECK-NEXT: (nop)
19+
;; CHECK-NEXT: )
20+
(func $nopp
21+
(nop)
22+
)
23+
)
24+

0 commit comments

Comments
 (0)