Skip to content

Commit 32188fd

Browse files
lforg37Ferdinand LemaireJessica Paquette
authored andcommitted
[mlir][wasm] Adding support for global in Wasm importer
--------- Co-authored-by: Ferdinand Lemaire <[email protected]> Co-authored-by: Jessica Paquette <[email protected]>
1 parent d42a1d4 commit 32188fd

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

mlir/lib/Target/Wasm/TranslateFromWasm.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,9 @@ class WasmBinaryParser {
10361036
if (failed(parsingMems))
10371037
return;
10381038

1039+
auto parsingGlobals = parseSection<WasmSectionType::GLOBAL>();
1040+
if (failed(parsingGlobals))
1041+
return;
10391042
LogicalResult parsingExports = parseSection<WasmSectionType::EXPORT>();
10401043
if (failed(parsingExports))
10411044
return;
@@ -1229,6 +1232,38 @@ WasmBinaryParser::parseSectionItem<WasmSectionType::MEMORY>(ParserHead &ph,
12291232
symbols.memSymbols.push_back({SymbolRefAttr::get(memOp)});
12301233
return success();
12311234
}
1235+
1236+
template <>
1237+
LogicalResult
1238+
WasmBinaryParser::parseSectionItem<WasmSectionType::GLOBAL>(ParserHead &ph,
1239+
size_t) {
1240+
auto globalLocation = ph.getLocation();
1241+
auto globalTypeParsed = ph.parseGlobalType(ctx);
1242+
if (failed(globalTypeParsed))
1243+
return failure();
1244+
1245+
auto globalType = *globalTypeParsed;
1246+
auto symbol = builder.getStringAttr(symbols.getNewGlobalSymbolName());
1247+
auto globalOp = builder.create<wasmssa::GlobalOp>(
1248+
globalLocation, symbol, globalType.type, globalType.isMutable);
1249+
symbols.globalSymbols.push_back(
1250+
{{FlatSymbolRefAttr::get(globalOp)}, globalOp.getType()});
1251+
auto ip = builder.saveInsertionPoint();
1252+
auto *block = builder.createBlock(&globalOp.getInitializer());
1253+
builder.setInsertionPointToStart(block);
1254+
auto expr = ph.parseExpression(builder, symbols);
1255+
if (failed(expr))
1256+
return failure();
1257+
if (block->empty())
1258+
return emitError(globalLocation, "global with empty initializer");
1259+
if (expr->size() != 1 && (*expr)[0].getType() != globalType.type)
1260+
return emitError(
1261+
globalLocation,
1262+
"initializer result type does not match global declaration type");
1263+
builder.create<ReturnOp>(globalLocation, *expr);
1264+
builder.restoreInsertionPoint(ip);
1265+
return success();
1266+
}
12321267
} // namespace
12331268

12341269
namespace mlir::wasm {

0 commit comments

Comments
 (0)