[Mlir-commits] [mlir] [MLIR][Wasm] Introduce the WasmSSA MLIR dialect (PR #149233)

Ferdinand Lemaire llvmlistbot at llvm.org
Tue Jul 22 23:35:02 PDT 2025


================
@@ -0,0 +1,186 @@
+//===-- WebAssemblySSAInterfaces.td - WebAssemblySSA 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 WebAssemblySSA dialect in MLIR.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef WEBASSEMBLYSSA_INTERFACES
+#define WEBASSEMBLYSSA_INTERFACES
+
+include "mlir/IR/OpBase.td"
+include "mlir/IR/BuiltinAttributes.td"
+
+def WasmSSALabelLevelInterface : OpInterface<"WasmSSALabelLevelInterface"> {
+  let description = [{
+    Operation that defines one level of nesting for wasm branching.
+    These operation region can be targeted by branch instructions.
+  }];
+  let methods = [
+    InterfaceMethod<
+      /*desc=*/        "Returns the target block address",
+      /*returnType=*/  "::mlir::Block*",
+      /*methodName=*/  "getLabelTarget",
+      /*args=*/        (ins)
+    >
+  ];
+}
+
+def WasmSSALabelBranchingInterface : OpInterface<"WasmSSALabelBranchingInterface"> {
+  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=*/  "WasmSSALabelLevelInterface",
+      /*methodName=*/  "getTargetOp",
+      /*args=*/        (ins),
+      /*methodBody=*/ [{
+        return *WasmSSALabelBranchingInterface::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<WasmSSALabelBranchingInterface>(this->getOperation());
+        return op.getTargetOp().getLabelTarget();
+      }]
+    >
+  ];
+  let extraClassDeclaration = [{
+    static ::llvm::FailureOr<WasmSSALabelLevelInterface> getTargetOpFromBlock(::mlir::Block *block, uint32_t level);
+  }];
+  let verify = [{return verifyWasmSSALabelBranchingInterface($_op);}];
+}
+
+def WasmSSAImportOpInterface : OpInterface<"WasmSSAImportOpInterface"> {
+  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 WasmSSAConstantExpressionInitializerInterface :
+          OpInterface<"WasmSSAConstantExpressionInitializerInterface"> {
+  let description = [{
+    Operation that must be constant initialized. This
+    interface adds a verifier that checks that all ops
+    within the initializer region are "constant expressions"
+    as defined by the WASM standard.
+  }];
+
+  let verify = [{ return detail::verifyConstantExpressionInterface($_op); }];
+}
+
+def WasmSSAConstantExprCheckInterface :
+          OpInterface<"WasmSSAConstantExprCheckInterface"> {
+  let description = [{
+    Base interface for operations that can be used in a Wasm Constant Expression.
+    It shouldn't be used directly, use one of the derived instead.
+  }];
+
+  let methods = [
+    InterfaceMethod<
+      /*desc=*/        [{
+        Returns success if the current operation is valid in a constant expression context.
+      }],
+      /*returnType=*/  "::mlir::LogicalResult",
+      /*methodName=*/  "isValidInConstantExpr",
+      /*args=*/        (ins),
+      /*methodBody=*/ [{
+        return $_op.verifyConstantExprValidity();
+      }]
+      >
+  ];
+}
+
+def WasmSSAContextuallyConstantExprInterface :
+          OpInterface<"WasmSSAContextuallyConstantExprInterface", [WasmSSAConstantExprCheckInterface]> {
+  let description = [{
+    Base interface for operations that can be used in a Wasm Constant Expression
+    depending on the context.
+  }];
+
+  let methods = [
+    InterfaceMethod<
+      /*desc=*/        [{
+        Returns success if the current operation is valid in a constant expression context.
+      }],
+      /*returnType=*/  "::mlir::LogicalResult",
+      /*methodName=*/  "verifyConstantExprValidity",
+      /*args=*/        (ins)
+      >
+  ];
+}
+
+def WasmSSAConstantExprInterface :
+          OpInterface<"WasmSSAConstantExprInterface", [WasmSSAConstantExprCheckInterface]> {
+  let description = [{
+    Base interface for operations that can always 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 wrote:

Applied this comment, we now have a main interface implement by a trait to dispatch the methods

https://github.com/llvm/llvm-project/pull/149233


More information about the Mlir-commits mailing list