Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mlir/include/mlir/InitAllTranslations.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace mlir {

void registerFromLLVMIRTranslation();
void registerFromSPIRVTranslation();
void registerFromWasmTranslation();
void registerToCppTranslation();
void registerToLLVMIRTranslation();
void registerToSPIRVTranslation();
Expand All @@ -36,6 +37,7 @@ inline void registerAllTranslations() {
registerFromLLVMIRTranslation();
registerFromSPIRVTranslation();
registerIRDLToCppTranslation();
registerFromWasmTranslation();
registerToCppTranslation();
registerToLLVMIRTranslation();
registerToSPIRVTranslation();
Expand Down
74 changes: 74 additions & 0 deletions mlir/include/mlir/Target/Wasm/WasmBinaryEncoding.h
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
31 changes: 31 additions & 0 deletions mlir/include/mlir/Target/Wasm/WasmImporter.h
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 trhe 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
1 change: 1 addition & 0 deletions mlir/lib/Target/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ add_subdirectory(SPIRV)
add_subdirectory(LLVMIR)
add_subdirectory(LLVM)
add_subdirectory(SMTLIB)
add_subdirectory(Wasm)
13 changes: 13 additions & 0 deletions mlir/lib/Target/Wasm/CMakeLists.txt
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
)
Loading