| 1 | //===- WasmSSAInterfaces.cpp - WasmSSA 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 WasmSSA dialect in MLIR. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Dialect/WasmSSA/IR/WasmSSAInterfaces.h" |
| 14 | #include "mlir/Dialect/WasmSSA/IR/WasmSSA.h" |
| 15 | #include "mlir/IR/Operation.h" |
| 16 | #include "mlir/IR/Visitors.h" |
| 17 | #include "mlir/Support/LLVM.h" |
| 18 | #include "llvm/Support/LogicalResult.h" |
| 19 | |
| 20 | namespace mlir::wasmssa { |
| 21 | #include "mlir/Dialect/WasmSSA/IR/WasmSSAInterfaces.cpp.inc" |
| 22 | |
| 23 | namespace detail { |
| 24 | LogicalResult verifyLabelBranchingOpInterface(Operation *op) { |
| 25 | auto branchInterface = dyn_cast<LabelBranchingOpInterface>(Val: op); |
| 26 | llvm::FailureOr<LabelLevelOpInterface> res = |
| 27 | LabelBranchingOpInterface::getTargetOpFromBlock( |
| 28 | block: op->getBlock(), level: branchInterface.getExitLevel()); |
| 29 | return res; |
| 30 | } |
| 31 | |
| 32 | LogicalResult verifyConstantExpressionInterface(Operation *op) { |
| 33 | Region &initializerRegion = op->getRegion(index: 0); |
| 34 | WalkResult resultState = |
| 35 | initializerRegion.walk(callback: [&](Operation *currentOp) -> WalkResult { |
| 36 | if (isa<ReturnOp>(Val: currentOp) || |
| 37 | currentOp->hasTrait<ConstantExprOpTrait>()) |
| 38 | return WalkResult::advance(); |
| 39 | op->emitError(message: "expected a constant initializer for this operator, got ") |
| 40 | << currentOp; |
| 41 | return WalkResult::interrupt(); |
| 42 | }); |
| 43 | return success(IsSuccess: !resultState.wasInterrupted()); |
| 44 | } |
| 45 | |
| 46 | LogicalResult verifyLabelLevelInterface(Operation *op) { |
| 47 | Block *target = cast<LabelLevelOpInterface>(Val: op).getLabelTarget(); |
| 48 | Region *targetRegion = target->getParent(); |
| 49 | if (targetRegion != op->getParentRegion() && |
| 50 | targetRegion->getParentOp() != op) |
| 51 | return op->emitError(message: "target should be a block defined in same level than " |
| 52 | "operation or in its region."); |
| 53 | return success(); |
| 54 | } |
| 55 | } // namespace detail |
| 56 | |
| 57 | llvm::FailureOr<LabelLevelOpInterface> |
| 58 | LabelBranchingOpInterface::getTargetOpFromBlock(::mlir::Block *block, |
| 59 | uint32_t breakLevel) { |
| 60 | LabelLevelOpInterface res{}; |
| 61 | for (size_t curLevel{0}; curLevel <= breakLevel; curLevel++) { |
| 62 | res = dyn_cast_or_null<LabelLevelOpInterface>(Val: block->getParentOp()); |
| 63 | if (!res) |
| 64 | return failure(); |
| 65 | block = res->getBlock(); |
| 66 | } |
| 67 | return res; |
| 68 | } |
| 69 | } // namespace mlir::wasmssa |
| 70 |