Skip to content

Commit ff2b10b

Browse files
authored
wasm-emscripten-finalize: Add mainReadsParams metadata (#2247)
The new flag indicates whether main reads the argc/argv parameters. If it does not, we can avoid emitting code to generate those arguments in the JS, which is not trivial in small programs - it requires some string conversion code. Nicely the existing test inputs were enough for testing this (see outputs). This depends on an emscripten change to land first, as emscripten.py asserts on metadata fields it doesn't recognize.
1 parent c8101d1 commit ff2b10b

15 files changed

Lines changed: 45 additions & 15 deletions

src/wasm/wasm-emscripten.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,23 @@ std::string EmscriptenGlueGenerator::generateEmscriptenMetadata(
10421042
wasm.features.iterFeatures([&](FeatureSet::Feature f) {
10431043
meta << nextElement() << "\"--enable-" << FeatureSet::toString(f) << '"';
10441044
});
1045-
meta << "\n ]\n";
1045+
meta << "\n ],\n";
1046+
1047+
auto mainReadsParams = false;
1048+
if (auto* exp = wasm.getExportOrNull("main")) {
1049+
if (exp->kind == ExternalKind::Function) {
1050+
auto* main = wasm.getFunction(exp->value);
1051+
mainReadsParams = true;
1052+
// If main does not read its parameters, it will just be a stub that
1053+
// calls __original_main (which has no parameters).
1054+
if (auto* call = main->body->dynCast<Call>()) {
1055+
if (call->operands.empty()) {
1056+
mainReadsParams = false;
1057+
}
1058+
}
1059+
}
1060+
}
1061+
meta << " \"mainReadsParams\": " << int(mainReadsParams) << '\n';
10461062

10471063
meta << "}\n";
10481064

test/lld/duplicate_imports.wast.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@
115115
"invoke_ffd"
116116
],
117117
"features": [
118-
]
118+
],
119+
"mainReadsParams": 1
119120
}
120121
-- END METADATA --
121122
;)

test/lld/em_asm.wast.mem.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@
262262
"invokeFuncs": [
263263
],
264264
"features": [
265-
]
265+
],
266+
"mainReadsParams": 0
266267
}
267268
-- END METADATA --
268269
;)

test/lld/em_asm.wast.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@
263263
"invokeFuncs": [
264264
],
265265
"features": [
266-
]
266+
],
267+
"mainReadsParams": 0
267268
}
268269
-- END METADATA --
269270
;)

test/lld/em_asm_O0.wast.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@
123123
"invokeFuncs": [
124124
],
125125
"features": [
126-
]
126+
],
127+
"mainReadsParams": 1
127128
}
128129
-- END METADATA --
129130
;)

test/lld/em_asm_shared.wast.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@
244244
"invokeFuncs": [
245245
],
246246
"features": [
247-
]
247+
],
248+
"mainReadsParams": 0
248249
}
249250
-- END METADATA --
250251
;)

test/lld/em_asm_table.wast.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@
9393
"invokeFuncs": [
9494
],
9595
"features": [
96-
]
96+
],
97+
"mainReadsParams": 0
9798
}
9899
-- END METADATA --
99100
;)

test/lld/em_js_O0.wast.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
"invokeFuncs": [
7474
],
7575
"features": [
76-
]
76+
],
77+
"mainReadsParams": 0
7778
}
7879
-- END METADATA --
7980
;)

test/lld/hello_world.passive.wast.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@
105105
"invokeFuncs": [
106106
],
107107
"features": [
108-
]
108+
],
109+
"mainReadsParams": 0
109110
}
110111
-- END METADATA --
111112
;)

test/lld/hello_world.wast.mem.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@
9797
"invokeFuncs": [
9898
],
9999
"features": [
100-
]
100+
],
101+
"mainReadsParams": 0
101102
}
102103
-- END METADATA --
103104
;)

0 commit comments

Comments
 (0)