Skip to content

Commit 6bb8f6f

Browse files
flemairen6lforg37Ferdinand LemaireJessica PaquetteLuc Forget
authored
[MLIR][WASM] Introduce an importer for Wasm binaries (#152131)
First step in introducing the wasm-import target to mlir-translate. This is the first PR to introduce the pass, with this PR, there is very little support for the actual WebAssembly language, it's mostly there to introduce the skeleton of the importer. A follow-up will come with support for a wider range of operators. It was split to make it easier to review, since it's a good chunk of work. --------- Co-authored-by: Luc Forget <[email protected]> Co-authored-by: Ferdinand Lemaire <[email protected]> Co-authored-by: Jessica Paquette <[email protected]> Co-authored-by: Luc Forget <[email protected]>
1 parent b014d10 commit 6bb8f6f

23 files changed

+1637
-1
lines changed

mlir/include/mlir/InitAllTranslations.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace mlir {
2020

2121
void registerFromLLVMIRTranslation();
2222
void registerFromSPIRVTranslation();
23+
void registerFromWasmTranslation();
2324
void registerToCppTranslation();
2425
void registerToLLVMIRTranslation();
2526
void registerToSPIRVTranslation();
@@ -36,6 +37,7 @@ inline void registerAllTranslations() {
3637
registerFromLLVMIRTranslation();
3738
registerFromSPIRVTranslation();
3839
registerIRDLToCppTranslation();
40+
registerFromWasmTranslation();
3941
registerToCppTranslation();
4042
registerToLLVMIRTranslation();
4143
registerToSPIRVTranslation();
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===- WasmBinaryEncoding.h - Byte encodings for Wasm binary format ===----===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
// Define various flags used to encode instructions, types, etc. in
8+
// WebAssembly binary format.
9+
//
10+
// These encodings are defined in the WebAssembly binary format specification.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
#ifndef MLIR_TARGET_WASMBINARYENCODING
14+
#define MLIR_TARGET_WASMBINARYENCODING
15+
16+
#include <cstddef>
17+
18+
namespace mlir {
19+
struct WasmBinaryEncoding {
20+
/// Byte encodings for Wasm instructions.
21+
struct OpCode {
22+
// Locals, globals, constants.
23+
static constexpr std::byte constI32{0x41};
24+
static constexpr std::byte constI64{0x42};
25+
static constexpr std::byte constFP32{0x43};
26+
static constexpr std::byte constFP64{0x44};
27+
};
28+
29+
/// Byte encodings of types in Wasm binaries
30+
struct Type {
31+
static constexpr std::byte emptyBlockType{0x40};
32+
static constexpr std::byte funcType{0x60};
33+
static constexpr std::byte externRef{0x6F};
34+
static constexpr std::byte funcRef{0x70};
35+
static constexpr std::byte v128{0x7B};
36+
static constexpr std::byte f64{0x7C};
37+
static constexpr std::byte f32{0x7D};
38+
static constexpr std::byte i64{0x7E};
39+
static constexpr std::byte i32{0x7F};
40+
};
41+
42+
/// Byte encodings of Wasm imports.
43+
struct Import {
44+
static constexpr std::byte typeID{0x00};
45+
static constexpr std::byte tableType{0x01};
46+
static constexpr std::byte memType{0x02};
47+
static constexpr std::byte globalType{0x03};
48+
};
49+
50+
/// Byte encodings for Wasm limits.
51+
struct LimitHeader {
52+
static constexpr std::byte lowLimitOnly{0x00};
53+
static constexpr std::byte bothLimits{0x01};
54+
};
55+
56+
/// Byte encodings describing the mutability of globals.
57+
struct GlobalMutability {
58+
static constexpr std::byte isConst{0x00};
59+
static constexpr std::byte isMutable{0x01};
60+
};
61+
62+
/// Byte encodings describing Wasm exports.
63+
struct Export {
64+
static constexpr std::byte function{0x00};
65+
static constexpr std::byte table{0x01};
66+
static constexpr std::byte memory{0x02};
67+
static constexpr std::byte global{0x03};
68+
};
69+
70+
static constexpr std::byte endByte{0x0B};
71+
};
72+
} // namespace mlir
73+
74+
#endif
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===- WasmImporter.h - Helpers to create WebAssembly emitter ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines helpers to import WebAssembly code using the WebAssembly
10+
// dialect.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef MLIR_TARGET_WASM_WASMIMPORTER_H
15+
#define MLIR_TARGET_WASM_WASMIMPORTER_H
16+
17+
#include "mlir/IR/BuiltinOps.h"
18+
#include "mlir/IR/MLIRContext.h"
19+
#include "mlir/IR/OwningOpRef.h"
20+
#include "llvm/Support/SourceMgr.h"
21+
22+
namespace mlir::wasm {
23+
24+
/// If `source` contains a valid Wasm binary file, this function returns a
25+
/// a ModuleOp containing the representation of the Wasm module encoded in
26+
/// the source file in the `wasmssa` dialect.
27+
OwningOpRef<ModuleOp> importWebAssemblyToModule(llvm::SourceMgr &source,
28+
MLIRContext *context);
29+
} // namespace mlir::wasm
30+
31+
#endif // MLIR_TARGET_WASM_WASMIMPORTER_H

mlir/lib/Target/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ add_subdirectory(SPIRV)
44
add_subdirectory(LLVMIR)
55
add_subdirectory(LLVM)
66
add_subdirectory(SMTLIB)
7+
add_subdirectory(Wasm)

mlir/lib/Target/Wasm/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
add_mlir_translation_library(MLIRTargetWasmImport
2+
TranslateRegistration.cpp
3+
TranslateFromWasm.cpp
4+
5+
ADDITIONAL_HEADER_DIRS
6+
${MLIR_MAIN_INCLUDE_DIR}/Target/Wasm
7+
8+
LINK_LIBS PUBLIC
9+
MLIRWasmSSADialect
10+
MLIRIR
11+
MLIRSupport
12+
MLIRTranslateLib
13+
)

0 commit comments

Comments
 (0)