-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[MLIR][Wasm] Introduce the WasmSSA MLIR dialect #149233
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
Open
flemairen6
wants to merge
10
commits into
llvm:main
Choose a base branch
from
lforg37:ferdinand.start_wasm_mlir_upstream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,918
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d8b21ad
[mlir][wasm] Introduce the MLIR wasm dialect
lforg37 f79a7d5
[mlir][wasm] Apply review comments regarding formatting and documenta…
ba474ce
[mlir][wasm] Apply review comments on interface replacement by trait
e56c631
[mlir][wasm] Change ConstantExprInitializerInterface to Trait
50efecc
Rename everything to WasmSSA for consistency
881ec8e
Rename interfaces without WasmSSA since the namespace is added via cp…
995b710
[mlir][wasm] LabelLevelInterface requires IsTerminator trait + doc
318b65b
[mlir][wasm] Remove direct use of odsState in custom builder
89b64f4
[mlir][wasm] Verifier for LabelLevelInterface Target validity
d0aed4d
Add tests for verifiers, improve reporting of verifiers errors and do…
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 @@ | ||
add_subdirectory(IR) |
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 @@ | ||
set(LLVM_TARGET_DEFINITIONS WasmSSATypes.td) | ||
mlir_tablegen(WasmSSATypeConstraints.h.inc -gen-type-constraint-decls) | ||
mlir_tablegen(WasmSSATypeConstraints.cpp.inc -gen-type-constraint-defs) | ||
|
||
set (LLVM_TARGET_DEFINITIONS WasmSSAInterfaces.td) | ||
mlir_tablegen(WasmSSAInterfaces.h.inc -gen-op-interface-decls) | ||
mlir_tablegen(WasmSSAInterfaces.cpp.inc -gen-op-interface-defs) | ||
add_public_tablegen_target(MLIRWasmSSAInterfacesIncGen) | ||
|
||
set(LLVM_TARGET_DEFINITIONS WasmSSAOps.td) | ||
|
||
add_mlir_dialect(WasmSSAOps wasmssa) | ||
add_mlir_doc(WasmSSAOps WasmSSAOps Dialects/ -gen-dialect-doc) |
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,55 @@ | ||
//===- WasmSSA.h - WasmSSA dialect ------------------*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_DIALECT_WasmSSA_IR_WasmSSA_H_ | ||
#define MLIR_DIALECT_WasmSSA_IR_WasmSSA_H_ | ||
|
||
#include "mlir/Bytecode/BytecodeOpInterface.h" | ||
#include "mlir/IR/Dialect.h" | ||
|
||
//===----------------------------------------------------------------------===// | ||
// WebAssemblyDialect | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/WasmSSA/IR/WasmSSAOpsDialect.h.inc" | ||
|
||
//===----------------------------------------------------------------------===// | ||
// WebAssembly Dialect Types | ||
//===----------------------------------------------------------------------===// | ||
|
||
#define GET_TYPEDEF_CLASSES | ||
#include "mlir/Dialect/WasmSSA/IR/WasmSSAOpsTypes.h.inc" | ||
|
||
//===----------------------------------------------------------------------===// | ||
// WebAssembly Interfaces | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/WasmSSA/IR/WasmSSAInterfaces.h" | ||
|
||
//===----------------------------------------------------------------------===// | ||
// WebAssembly Dialect Operations | ||
//===----------------------------------------------------------------------===// | ||
#include "mlir/IR/SymbolTable.h" | ||
#include "mlir/Interfaces/CallInterfaces.h" | ||
#include "mlir/Interfaces/FunctionInterfaces.h" | ||
#include "mlir/Interfaces/InferTypeOpInterface.h" | ||
|
||
//===----------------------------------------------------------------------===// | ||
// WebAssembly Constraints | ||
//===----------------------------------------------------------------------===// | ||
|
||
namespace mlir { | ||
namespace wasmssa { | ||
#include "mlir/Dialect/WasmSSA/IR/WasmSSATypeConstraints.h.inc" | ||
} | ||
} // namespace mlir | ||
|
||
#define GET_OP_CLASSES | ||
#include "mlir/Dialect/WasmSSA/IR/WasmSSAOps.h.inc" | ||
|
||
#endif // MLIR_DIALECT_WasmSSA_IR_WasmSSA_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//===- WasmSSABase.td - Base defs for wasmssa dialect -*- tablegen -*-==// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef WasmSSA_BASE | ||
#define WasmSSA_BASE | ||
|
||
include "mlir/IR/EnumAttr.td" | ||
include "mlir/IR/OpBase.td" | ||
|
||
def WasmSSA_Dialect : Dialect { | ||
let name = "wasmssa"; | ||
let cppNamespace = "::mlir::wasmssa"; | ||
let description = [{ | ||
The `wasmssa` dialect is intended to represent WebAssembly | ||
modules in SSA form for easier manipulation. | ||
}]; | ||
let useDefaultTypePrinterParser = true; | ||
} | ||
|
||
#endif //WasmSSA_BASE |
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,60 @@ | ||
//===- WasmSSAInterfaces.h - WasmSSA Interfaces ---*- 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 op interfaces for the WasmSSA dialect in MLIR. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_DIALECT_WasmSSA_IR_WasmSSAINTERFACES_H_ | ||
#define MLIR_DIALECT_WasmSSA_IR_WasmSSAINTERFACES_H_ | ||
|
||
#include "mlir/IR/BuiltinAttributes.h" | ||
#include "mlir/IR/OpDefinition.h" | ||
|
||
namespace mlir::wasmssa { | ||
namespace detail { | ||
/// Verify that `op` conforms to the ConstantExpressionInterface. | ||
/// `op` must be initialized with valid constant expressions. | ||
LogicalResult verifyConstantExpressionInterface(Operation *op); | ||
|
||
/// Verify that `op` conforms to the LabelBranchingOpInterface | ||
/// Checks that the branching is targetting something within its scope. | ||
LogicalResult verifyLabelBranchingOpInterface(Operation *op); | ||
|
||
/// Verify that `op` conforms to LabelLevelInterfaceIsTerminator | ||
template <typename OpType> | ||
LogicalResult verifyLabelLevelInterfaceIsTerminator() { | ||
static_assert(OpType::template hasTrait<::mlir::OpTrait::IsTerminator>(), | ||
"LabelLevelOp should be terminator ops"); | ||
return success(); | ||
} | ||
|
||
/// Verify that `op` conforms to the LabelLevelInterface | ||
/// `op`'s target should defined at the same scope level. | ||
LogicalResult verifyLabelLevelInterface(Operation *op); | ||
} // namespace detail | ||
|
||
/// Operations implementing this trait are considered as valid | ||
/// constant expressions in any context (In contrast of ConstantExprCheckOpInterface | ||
/// which are sometimes considered valid constant expressions. | ||
template <class OperationType> | ||
struct AlwaysValidConstantExprOpTrait | ||
: public OpTrait::TraitBase<OperationType, AlwaysValidConstantExprOpTrait> {}; | ||
|
||
/// Trait used to verify operations that need a constant expression initializer. | ||
template<typename OpType> | ||
struct ConstantExpressionInitializerOpTrait : public OpTrait::TraitBase<OpType, ConstantExpressionInitializerOpTrait>{ | ||
static LogicalResult verifyTrait(Operation* op) { | ||
return detail::verifyConstantExpressionInterface(op); | ||
} | ||
}; | ||
|
||
} // namespace mlir::wasmssa | ||
#include "mlir/Dialect/WasmSSA/IR/WasmSSAInterfaces.h.inc" | ||
|
||
#endif // MLIR_DIALECT_WasmSSA_IR_WasmSSAINTERFACES_H_ |
162 changes: 162 additions & 0 deletions
162
mlir/include/mlir/Dialect/WasmSSA/IR/WasmSSAInterfaces.td
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,162 @@ | ||
//===-- WasmSSAInterfaces.td - WasmSSA Interfaces -*- tablegen -*--===// | ||
// | ||
// 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 interfaces for the WasmSSA dialect in MLIR. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef WasmSSA_INTERFACES | ||
#define WasmSSA_INTERFACES | ||
|
||
include "mlir/IR/OpBase.td" | ||
include "mlir/IR/BuiltinAttributes.td" | ||
|
||
def LabelLevelOpInterface : OpInterface<"LabelLevelOpInterface"> { | ||
let cppNamespace = "::mlir::wasmssa"; | ||
let description = [{ | ||
Operation that defines one level of nesting for wasm branching. | ||
|
||
These ops defines Wasm control flow nesting levels (Wasm Labels) that Wasm | ||
branching operations can target. | ||
The branching operations specify a number of nesting level they want to exit, | ||
and are redirected to the target of the corresponding nesting LabelLevelOp. | ||
|
||
As multiple level can be escaped at once, the level defining ops need themselves | ||
to be `Terminator` ops. | ||
}]; | ||
let methods = [ | ||
InterfaceMethod< | ||
/*desc=*/ "Returns the target block address", | ||
/*returnType=*/ "::mlir::Block*", | ||
/*methodName=*/ "getLabelTarget", | ||
/*args=*/ (ins) | ||
> | ||
]; | ||
|
||
let verify = [{ | ||
return success( | ||
succeeded(verifyLabelLevelInterfaceIsTerminator<ConcreteOp>()) && | ||
succeeded(verifyLabelLevelInterface($_op))); | ||
}]; | ||
} | ||
|
||
def LabelBranchingOpInterface : OpInterface<"LabelBranchingOpInterface"> { | ||
let cppNamespace = "::mlir::wasmssa"; | ||
let description = [{ | ||
Wasm operation that targets a label for a jump. | ||
}]; | ||
let methods = [ | ||
InterfaceMethod< | ||
/*desc=*/ "Returns the number of context to break from", | ||
/*returnType=*/ "size_t", | ||
/*methodName=*/ "getExitLevel", | ||
/*args=*/ (ins) | ||
>, | ||
InterfaceMethod< | ||
/*desc=*/ "Returns the destination of this operation", | ||
/*returnType=*/ "LabelLevelOpInterface", | ||
/*methodName=*/ "getTargetOp", | ||
/*args=*/ (ins), | ||
/*methodBody=*/ [{ | ||
return *LabelBranchingOpInterface::getTargetOpFromBlock($_op.getOperation()->getBlock(), $_op.getExitLevel()); | ||
}] | ||
>, | ||
InterfaceMethod< | ||
/*desc=*/ "Return the target control flow ops that defined the label of this operation", | ||
/*returnType=*/ "::mlir::Block*", | ||
/*methodName=*/ "getTarget", | ||
/*args=*/ (ins), | ||
/*methodBody=*/ [{}], | ||
/*defaultImpl=*/ [{ | ||
auto op = mlir::cast<LabelBranchingOpInterface>(this->getOperation()); | ||
return op.getTargetOp().getLabelTarget(); | ||
}] | ||
> | ||
]; | ||
|
||
let extraClassDeclaration = [{ | ||
static ::llvm::FailureOr<LabelLevelOpInterface> getTargetOpFromBlock(::mlir::Block *block, uint32_t level); | ||
}]; | ||
let verify = [{return verifyLabelBranchingOpInterface($_op);}]; | ||
} | ||
|
||
def ImportOpInterface : OpInterface<"ImportOpInterface"> { | ||
let cppNamespace = "::mlir::wasmssa"; | ||
let description = [{ | ||
Operation that imports a symbol from an external wasm module; | ||
}]; | ||
|
||
let methods = [ | ||
InterfaceMethod< | ||
/*desc=*/ "Returns the module name for the import", | ||
/*returnType=*/ "::llvm::StringRef", | ||
/*methodName=*/ "getModuleName", | ||
/*args=*/ (ins) | ||
>, | ||
InterfaceMethod< | ||
/*desc=*/ "Returns the import name for the import", | ||
/*returnType=*/ "::llvm::StringRef", | ||
/*methodName=*/ "getImportName", | ||
/*args=*/ (ins) | ||
>, | ||
InterfaceMethod< | ||
/*desc=*/ "Returns the wasm index based symbol of the op", | ||
/*returnType=*/ "::mlir::StringAttr", | ||
/*methodName=*/ "getSymbolName", | ||
/*args=*/ (ins), | ||
/*methodBody=*/ [{}], | ||
/*defaultImpl=*/ [{ | ||
auto op = mlir::cast<ConcreteOp>(this->getOperation()); | ||
return op.getSymNameAttr(); | ||
}] | ||
>, | ||
InterfaceMethod< | ||
/*desc=*/ "Returns the qualified name of the import", | ||
/*returnType=*/ "std::string", | ||
/*methodName=*/ "getQualifiedImportName", | ||
/*args=*/ (ins), | ||
/*methodBody=*/ [{ | ||
return ($_op.getModuleName() + ::llvm::Twine{"::"} + $_op.getImportName()).str(); | ||
}] | ||
>, | ||
]; | ||
} | ||
|
||
def ConstantExpressionInitializerOpTrait : NativeOpTrait<"ConstantExpressionInitializerOpTrait"> { | ||
let cppNamespace = "::mlir::wasmssa"; | ||
} | ||
|
||
def ConstantExprCheckOpInterface : | ||
OpInterface<"ConstantExprCheckOpInterface"> { | ||
let cppNamespace = "::mlir::wasmssa"; | ||
let description = [{ | ||
Interface for allowing to verify that operations can be used in a Wasm Constant Expression. | ||
}]; | ||
|
||
let methods = [ | ||
InterfaceMethod< | ||
/*desc=*/ [{ | ||
Returns success if the current operation is valid in a constant expression context. | ||
flemairen6 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
A diagnostic is emitted on error. | ||
}], | ||
/*returnType=*/ "::mlir::LogicalResult", | ||
/*methodName=*/ "CheckValidInConstantExpr", | ||
/*args=*/ (ins) | ||
> | ||
]; | ||
} | ||
|
||
def AlwaysValidInConstantExprOpTrait : NativeOpTrait<"AlwaysValidConstantExprOpTrait", [], [{ | ||
::mlir::LogicalResult CheckValidInConstantExpr() { | ||
return success(); | ||
} | ||
}]> { | ||
let cppNamespace = "::mlir::wasmssa"; | ||
} | ||
|
||
#endif // WEBASSEMBLY_INTERFACES |
Oops, something went wrong.
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.