70 lines · cpp
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-exception6//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 20namespace mlir::wasmssa {21#include "mlir/Dialect/WasmSSA/IR/WasmSSAInterfaces.cpp.inc"22 23namespace detail {24LogicalResult verifyLabelBranchingOpInterface(Operation *op) {25 auto branchInterface = dyn_cast<LabelBranchingOpInterface>(op);26 llvm::FailureOr<LabelLevelOpInterface> res =27 LabelBranchingOpInterface::getTargetOpFromBlock(28 op->getBlock(), branchInterface.getExitLevel());29 return res;30}31 32LogicalResult verifyConstantExpressionInterface(Operation *op) {33 Region &initializerRegion = op->getRegion(0);34 WalkResult resultState =35 initializerRegion.walk([&](Operation *currentOp) -> WalkResult {36 if (isa<ReturnOp>(currentOp) ||37 currentOp->hasTrait<ConstantExprOpTrait>())38 return WalkResult::advance();39 op->emitError("expected a constant initializer for this operator, got ")40 << currentOp;41 return WalkResult::interrupt();42 });43 return success(!resultState.wasInterrupted());44}45 46LogicalResult verifyLabelLevelInterface(Operation *op) {47 Block *target = cast<LabelLevelOpInterface>(op).getLabelTarget();48 Region *targetRegion = target->getParent();49 if (targetRegion != op->getParentRegion() &&50 targetRegion->getParentOp() != op)51 return op->emitError("target should be a block defined in same level than "52 "operation or in its region.");53 return success();54}55} // namespace detail56 57llvm::FailureOr<LabelLevelOpInterface>58LabelBranchingOpInterface::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>(block->getParentOp());63 if (!res)64 return failure();65 block = res->getBlock();66 }67 return res;68}69} // namespace mlir::wasmssa70