Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
73 changes: 73 additions & 0 deletions mlir/include/mlir/Target/Wasm/WasmBinaryEncoding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//===- 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 encodings for WebAssembly instructions, types, etc from the
// WebAssembly binary format.
//
// Each encoding is defined in the WebAssembly binary 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
34 changes: 34 additions & 0 deletions mlir/include/mlir/Target/Wasm/WasmImporter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===- 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 {

/// Translates the given operation to C++ code. The operation or operations in
/// the region of 'op' need almost all be in EmitC dialect. The parameter
/// 'declareVariablesAtTop' enforces that all variables for op results and block
/// arguments are declared at the beginning of the function.
/// If parameter 'fileId' is non-empty, then body of `emitc.file` ops
/// with matching id are emitted.
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
Loading