-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[MLIR][WASM] Introduce an importer for Wasm binaries #152131
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
Merged
joker-eph
merged 15 commits into
llvm:main
from
lforg37:ferdinand.upstream_wasm_importer
Aug 15, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b6cc91e
[mlir][wasm] Adding wasm import target to mlir-translate
lforg37 b942297
[mlir][wasm] Handling table in Wasm importer
lforg37 9af8a9d
[mlir][wasm] Handle memory in Wasm importer
lforg37 35655b8
[mlir][wasm] Handling of export at Wasm importer level
1ededd4
[mlir][wasm] Expression parsing mechanism for Wasm importer
lforg37 2cb8732
Formatting and other comments from the previous PR
6f6c527
[MLIR][WASM] Make WasmSSA Importer signal program failure on error
b58ddbe
[MLIR] Adding yaml2obj as dependency to mlir unit tests
595266e
Merge remote-tracking branch 'origin/oss/main' into ferdinand.upstrea…
ee8d388
[WASM][MLIR] Importer tests uses new custom format
6e952fc
[MLIR][WASM] Implement review remarks
5ebf0e1
[MLIR][WASM] Fix template to please MSVC
819e297
[MLIR][WASM] NFC remove deadcode in WasmImporter
bff79f3
[MLIR][WASM] NFC: fix typo and documentation
3e2caec
[MLIR][WASM] NFC: fix typo
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| //===- WasmBinaryEncoding.h - Byte encodings for Wasm binary format ===----===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| // Define various flags used to encode instructions, types, etc. in | ||
| // WebAssembly binary format. | ||
| // | ||
| // These encodings are defined in the WebAssembly binary format specification. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| #ifndef MLIR_TARGET_WASMBINARYENCODING | ||
| #define MLIR_TARGET_WASMBINARYENCODING | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| namespace mlir { | ||
| struct WasmBinaryEncoding { | ||
| /// Byte encodings for Wasm instructions. | ||
| struct OpCode { | ||
| // Locals, globals, constants. | ||
| static constexpr std::byte constI32{0x41}; | ||
| static constexpr std::byte constI64{0x42}; | ||
| static constexpr std::byte constFP32{0x43}; | ||
| static constexpr std::byte constFP64{0x44}; | ||
| }; | ||
|
|
||
| /// Byte encodings of types in Wasm binaries | ||
| struct Type { | ||
| static constexpr std::byte emptyBlockType{0x40}; | ||
| static constexpr std::byte funcType{0x60}; | ||
| static constexpr std::byte externRef{0x6F}; | ||
| static constexpr std::byte funcRef{0x70}; | ||
| static constexpr std::byte v128{0x7B}; | ||
| static constexpr std::byte f64{0x7C}; | ||
| static constexpr std::byte f32{0x7D}; | ||
| static constexpr std::byte i64{0x7E}; | ||
| static constexpr std::byte i32{0x7F}; | ||
| }; | ||
|
|
||
| /// Byte encodings of Wasm imports. | ||
| struct Import { | ||
| static constexpr std::byte typeID{0x00}; | ||
| static constexpr std::byte tableType{0x01}; | ||
| static constexpr std::byte memType{0x02}; | ||
| static constexpr std::byte globalType{0x03}; | ||
| }; | ||
|
|
||
| /// Byte encodings for Wasm limits. | ||
| struct LimitHeader { | ||
| static constexpr std::byte lowLimitOnly{0x00}; | ||
| static constexpr std::byte bothLimits{0x01}; | ||
| }; | ||
|
|
||
| /// Byte encodings describing the mutability of globals. | ||
| struct GlobalMutability { | ||
| static constexpr std::byte isConst{0x00}; | ||
| static constexpr std::byte isMutable{0x01}; | ||
| }; | ||
|
|
||
| /// Byte encodings describing Wasm exports. | ||
| struct Export { | ||
| static constexpr std::byte function{0x00}; | ||
| static constexpr std::byte table{0x01}; | ||
| static constexpr std::byte memory{0x02}; | ||
| static constexpr std::byte global{0x03}; | ||
| }; | ||
|
|
||
| static constexpr std::byte endByte{0x0B}; | ||
| }; | ||
| } // namespace mlir | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| //===- WasmImporter.h - Helpers to create WebAssembly emitter ---*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file defines helpers to import WebAssembly code using the WebAssembly | ||
| // dialect. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef MLIR_TARGET_WASM_WASMIMPORTER_H | ||
| #define MLIR_TARGET_WASM_WASMIMPORTER_H | ||
|
|
||
| #include "mlir/IR/BuiltinOps.h" | ||
| #include "mlir/IR/MLIRContext.h" | ||
| #include "mlir/IR/OwningOpRef.h" | ||
| #include "llvm/Support/SourceMgr.h" | ||
|
|
||
| namespace mlir::wasm { | ||
|
|
||
| /// If `source` contains a valid Wasm binary file, this function returns a | ||
| /// a ModuleOp containing the representation of the Wasm module encoded in | ||
| /// the source file in the `wasmssa` dialect. | ||
| OwningOpRef<ModuleOp> importWebAssemblyToModule(llvm::SourceMgr &source, | ||
| MLIRContext *context); | ||
| } // namespace mlir::wasm | ||
|
|
||
| #endif // MLIR_TARGET_WASM_WASMIMPORTER_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| add_mlir_translation_library(MLIRTargetWasmImport | ||
| TranslateRegistration.cpp | ||
| TranslateFromWasm.cpp | ||
|
|
||
| ADDITIONAL_HEADER_DIRS | ||
| ${MLIR_MAIN_INCLUDE_DIR}/Target/Wasm | ||
|
|
||
| LINK_LIBS PUBLIC | ||
| MLIRWasmSSADialect | ||
| MLIRIR | ||
| MLIRSupport | ||
| MLIRTranslateLib | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.