Skip to content

Commit 280772e

Browse files
lforg37Ferdinand LemaireJessica Paquette
committed
[mlir][wasm] Introduce the MLIR wasm dialect
This dialect is an SSA representation of a WebAssembly program. --------- Co-authored-by: Ferdinand Lemaire <ferdinand.lemaire@woven-planet.global> Co-authored-by: Jessica Paquette <jessica.paquette@woven-planet.global>
1 parent f56211e commit 280772e

23 files changed

+1885
-0
lines changed

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@
105105
/mlir/**/*ToSPIRV/ @antiagainst @kuhar
106106
/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp @antiagainst @kuhar
107107

108+
# WASM Dialect in MLIR.
109+
/mlir/**/WebAssemblySSA/ @flemairen6 @lforg37 @ornata
110+
108111
# MLIR Sparsifier.
109112
/mlir/**/*SparseTensor*/ @aartbik @PeimingLiu @yinying-lisa-li @matthias-springer
110113

mlir/include/mlir/Dialect/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ add_subdirectory(Transform)
4141
add_subdirectory(UB)
4242
add_subdirectory(Utils)
4343
add_subdirectory(Vector)
44+
add_subdirectory(WebAssemblySSA)
4445
add_subdirectory(X86Vector)
4546
add_subdirectory(XeGPU)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(IR)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
set(LLVM_TARGET_DEFINITIONS WebAssemblySSATypes.td)
2+
mlir_tablegen(WebAssemblySSATypeConstraints.h.inc -gen-type-constraint-decls)
3+
mlir_tablegen(WebAssemblySSATypeConstraints.cpp.inc -gen-type-constraint-defs)
4+
5+
set (LLVM_TARGET_DEFINITIONS WebAssemblySSAInterfaces.td)
6+
mlir_tablegen(WebAssemblySSAInterfaces.h.inc -gen-op-interface-decls)
7+
mlir_tablegen(WebAssemblySSAInterfaces.cpp.inc -gen-op-interface-defs)
8+
add_public_tablegen_target(MLIRWebAssemblySSAInterfacesIncGen)
9+
10+
set(LLVM_TARGET_DEFINITIONS WebAssemblySSAOps.td)
11+
12+
add_mlir_dialect(WebAssemblySSAOps wasmssa)
13+
add_mlir_doc(WebAssemblySSAOps WebAssemblySSAOps Dialects/ -gen-dialect-doc)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===- WebAssemblySSA.h - WebAssemblySSA dialect ------------------*- 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+
#ifndef MLIR_DIALECT_WEBASSEMBLYSSA_IR_WEBASSEMBLYSSA_H_
10+
#define MLIR_DIALECT_WEBASSEMBLYSSA_IR_WEBASSEMBLYSSA_H_
11+
12+
#include "mlir/Bytecode/BytecodeOpInterface.h"
13+
#include "mlir/IR/Dialect.h"
14+
15+
//===----------------------------------------------------------------------===//
16+
// WebAssemblyDialect
17+
//===----------------------------------------------------------------------===//
18+
19+
#include "mlir/Dialect/WebAssemblySSA/IR/WebAssemblySSAOpsDialect.h.inc"
20+
21+
//===----------------------------------------------------------------------===//
22+
// WebAssembly Dialect Types
23+
//===----------------------------------------------------------------------===//
24+
25+
#define GET_TYPEDEF_CLASSES
26+
#include "mlir/Dialect/WebAssemblySSA/IR/WebAssemblySSAOpsTypes.h.inc"
27+
28+
//===----------------------------------------------------------------------===//
29+
// WebAssembly Interfaces
30+
//===----------------------------------------------------------------------===//
31+
32+
#include "mlir/Dialect/WebAssemblySSA/IR/WebAssemblySSAInterfaces.h"
33+
34+
//===----------------------------------------------------------------------===//
35+
// WebAssembly Dialect Operations
36+
//===----------------------------------------------------------------------===//
37+
#include "mlir/IR/SymbolTable.h"
38+
#include "mlir/Interfaces/CallInterfaces.h"
39+
#include "mlir/Interfaces/FunctionInterfaces.h"
40+
#include "mlir/Interfaces/InferTypeOpInterface.h"
41+
42+
//===----------------------------------------------------------------------===//
43+
// WebAssembly Constraints
44+
//===----------------------------------------------------------------------===//
45+
46+
namespace mlir {
47+
namespace wasmssa {
48+
#include "mlir/Dialect/WebAssemblySSA/IR/WebAssemblySSATypeConstraints.h.inc"
49+
}
50+
} // namespace mlir
51+
52+
#define GET_OP_CLASSES
53+
#include "mlir/Dialect/WebAssemblySSA/IR/WebAssemblySSAOps.h.inc"
54+
55+
#endif // MLIR_DIALECT_WEBASSEMBLYSSA_IR_WEBASSEMBLYSSA_H_
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===- WebAssemblySSABase.td - Base defs for wasmssa dialect -*- tablegen -*-==//
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+
#ifndef WEBASSEMBLYSSA_BASE
10+
#define WEBASSEMBLYSSA_BASE
11+
12+
include "mlir/IR/EnumAttr.td"
13+
include "mlir/IR/OpBase.td"
14+
15+
def WasmSSA_Dialect : Dialect {
16+
let name = "wasmssa";
17+
let cppNamespace = "::mlir::wasmssa";
18+
let description = [{
19+
The wasmssa dialect is intended to represent WebAssembly
20+
modules in ssa form for easier manipulation.
21+
}];
22+
let useDefaultTypePrinterParser = true;
23+
}
24+
25+
#endif //WEBASSEMBLYSSA_BASE
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===- WebAssemblySSAInterfaces.h - WebAssemblySSA Interfaces ---*- 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 op interfaces for the WebAssemblySSA dialect in MLIR.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_DIALECT_WEBASSEMBLYSSA_IR_WEBASSEMBLYSSAINTERFACES_H_
14+
#define MLIR_DIALECT_WEBASSEMBLYSSA_IR_WEBASSEMBLYSSAINTERFACES_H_
15+
16+
#include "mlir/IR/BuiltinAttributes.h"
17+
#include "mlir/IR/OpDefinition.h"
18+
19+
namespace mlir {
20+
namespace wasmssa {
21+
namespace detail {
22+
LogicalResult verifyConstantExpressionInterface(Operation *op);
23+
LogicalResult verifyWasmSSALabelBranchingInterface(Operation *op);
24+
} // namespace detail
25+
26+
#include "mlir/Dialect/WebAssemblySSA/IR/WebAssemblySSAInterfaces.h.inc"
27+
} // namespace wasmssa
28+
} // namespace mlir
29+
30+
#endif // MLIR_DIALECT_WEBASSEMBLYSSA_IR_WEBASSEMBLYSSAINTERFACES_H_
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
//===-- WebAssemblySSAInterfaces.td - WebAssemblySSA Interfaces -*- tablegen -*-===//
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 interfaces for the WebAssemblySSA dialect in MLIR.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef WEBASSEMBLYSSA_INTERFACES
14+
#define WEBASSEMBLYSSA_INTERFACES
15+
16+
include "mlir/IR/OpBase.td"
17+
include "mlir/IR/BuiltinAttributes.td"
18+
19+
def WasmSSALabelLevelInterface : OpInterface<"WasmSSALabelLevelInterface"> {
20+
let description = [{
21+
Operation that defines one level of nesting for wasm branching.
22+
These operation region can be targeted by branch instructions.
23+
}];
24+
let methods = [
25+
InterfaceMethod<
26+
/*desc=*/ "Returns the target block address",
27+
/*returnType=*/ "::mlir::Block*",
28+
/*methodName=*/ "getLabelTarget",
29+
/*args=*/ (ins)
30+
>
31+
];
32+
}
33+
34+
def WasmSSALabelBranchingInterface : OpInterface<"WasmSSALabelBranchingInterface"> {
35+
let description = [{
36+
Wasm operation that targets a label for a jump.
37+
}];
38+
let methods = [
39+
InterfaceMethod<
40+
/*desc=*/ "Returns the number of context to break from",
41+
/*returnType=*/ "size_t",
42+
/*methodName=*/ "getExitLevel",
43+
/*args=*/ (ins)
44+
>,
45+
InterfaceMethod<
46+
/*desc=*/ "Returns the destination of this operation",
47+
/*returnType=*/ "WasmSSALabelLevelInterface",
48+
/*methodName=*/ "getTargetOp",
49+
/*args=*/ (ins),
50+
/*methodBody=*/ [{
51+
return *WasmSSALabelBranchingInterface::getTargetOpFromBlock($_op.getOperation()->getBlock(), $_op.getExitLevel());
52+
}]
53+
>,
54+
InterfaceMethod<
55+
/*desc=*/ "Return the target control flow ops that defined the label of this operation",
56+
/*returnType=*/ "::mlir::Block*",
57+
/*methodName=*/ "getTarget",
58+
/*args=*/ (ins),
59+
/*methodBody=*/ [{}],
60+
/*defaultImpl=*/ [{
61+
auto op = mlir::cast<WasmSSALabelBranchingInterface>(this->getOperation());
62+
return op.getTargetOp().getLabelTarget();
63+
}]
64+
>
65+
];
66+
let extraClassDeclaration = [{
67+
static ::llvm::FailureOr<WasmSSALabelLevelInterface> getTargetOpFromBlock(::mlir::Block *block, uint32_t level);
68+
}];
69+
let verify = [{return verifyWasmSSALabelBranchingInterface($_op);}];
70+
}
71+
72+
def WasmSSAImportOpInterface : OpInterface<"WasmSSAImportOpInterface"> {
73+
let description = [{
74+
Operation that imports a symbol from an external wasm module;
75+
}];
76+
77+
let methods = [
78+
InterfaceMethod<
79+
/*desc=*/ "Returns the module name for the import",
80+
/*returnType=*/ "::llvm::StringRef",
81+
/*methodName=*/ "getModuleName",
82+
/*args=*/ (ins)
83+
>,
84+
InterfaceMethod<
85+
/*desc=*/ "Returns the import name for the import",
86+
/*returnType=*/ "::llvm::StringRef",
87+
/*methodName=*/ "getImportName",
88+
/*args=*/ (ins)
89+
>,
90+
InterfaceMethod<
91+
/*desc=*/ "Returns the wasm index based symbol of the op",
92+
/*returnType=*/ "::mlir::StringAttr",
93+
/*methodName=*/ "getSymbolName",
94+
/*args=*/ (ins),
95+
/*methodBody=*/ [{}],
96+
/*defaultImpl=*/ [{
97+
auto op = mlir::cast<ConcreteOp>(this->getOperation());
98+
return op.getSymNameAttr();
99+
}]
100+
>,
101+
InterfaceMethod<
102+
/*desc=*/ "Returns the qualified name of the import",
103+
/*returnType=*/ "std::string",
104+
/*methodName=*/ "getQualifiedImportName",
105+
/*args=*/ (ins),
106+
/*methodBody=*/ [{
107+
return ($_op.getModuleName() + llvm::Twine{"::"} + $_op.getImportName()).str();
108+
}]
109+
>,
110+
];
111+
}
112+
113+
def WasmSSAConstantExpressionInitializerInterface :
114+
OpInterface<"WasmSSAConstantExpressionInitializerInterface"> {
115+
let description = [{
116+
Operation that must be constant initialized. This
117+
interface adds a verifier that checks that all ops
118+
within the initializer region are "constant expressions"
119+
as defined by the WASM standard.
120+
}];
121+
122+
let verify = [{ return detail::verifyConstantExpressionInterface($_op); }];
123+
}
124+
125+
def WasmSSAConstantExprCheckInterface :
126+
OpInterface<"WasmSSAConstantExprCheckInterface"> {
127+
let description = [{
128+
Base interface for operations that can be used in a Wasm Constant Expression.
129+
It shouldn't be used directly, use one of the derived instead.
130+
}];
131+
132+
let methods = [
133+
InterfaceMethod<
134+
/*desc=*/ [{
135+
Returns success if the current operation is valid in a constant expression context.
136+
}],
137+
/*returnType=*/ "::mlir::LogicalResult",
138+
/*methodName=*/ "isValidInConstantExpr",
139+
/*args=*/ (ins),
140+
/*methodBody=*/ [{
141+
return $_op.verifyConstantExprValidity();
142+
}]
143+
>
144+
];
145+
}
146+
147+
def WasmSSAContextuallyConstantExprInterface :
148+
OpInterface<"WasmSSAContextuallyConstantExprInterface", [WasmSSAConstantExprCheckInterface]> {
149+
let description = [{
150+
Base interface for operations that can be used in a Wasm Constant Expression
151+
depending on the context.
152+
}];
153+
154+
let methods = [
155+
InterfaceMethod<
156+
/*desc=*/ [{
157+
Returns success if the current operation is valid in a constant expression context.
158+
}],
159+
/*returnType=*/ "::mlir::LogicalResult",
160+
/*methodName=*/ "verifyConstantExprValidity",
161+
/*args=*/ (ins)
162+
>
163+
];
164+
}
165+
166+
def WasmSSAConstantExprInterface :
167+
OpInterface<"WasmSSAConstantExprInterface", [WasmSSAConstantExprCheckInterface]> {
168+
let description = [{
169+
Base interface for operations that can always be used in a Wasm Constant Expression.
170+
}];
171+
172+
let methods = [
173+
InterfaceMethod<
174+
/*desc=*/ [{
175+
Returns success if the current operation is valid in a constant expression context.
176+
}],
177+
/*returnType=*/ "::mlir::LogicalResult",
178+
/*methodName=*/ "verifyConstantExprValidity",
179+
/*args=*/ (ins),
180+
/*methodBody=*/ [{}],
181+
/*DefaultImplementation=*/ [{ return success(); }]
182+
>
183+
];
184+
}
185+
186+
#endif // WEBASSEMBLYSSA_INTERFACES

0 commit comments

Comments
 (0)