MLIR 22.0.0git
WasmSSAInterfaces.cpp
Go to the documentation of this file.
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
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 {
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
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
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 detail
56
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::wasmssa
return success()
Block represents an ordered list of Operations.
Definition Block.h:33
Operation * getParentOp()
Returns the closest surrounding operation that contains this block.
Definition Block.cpp:31
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
Definition Operation.h:686
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition Operation.h:749
Block * getBlock()
Returns the operation block that contains this operation.
Definition Operation.h:213
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Region * getParentRegion()
Returns the region to which the instruction belongs.
Definition Operation.h:230
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
Operation * getParentOp()
Return the parent operation this region is attached to.
Definition Region.h:200
RetT walk(FnT &&callback)
Walk all nested operations, blocks or regions (including this region), depending on the type of callb...
Definition Region.h:285
A utility result that is used to signal how to proceed with an ongoing walk:
Definition WalkResult.h:29
static WalkResult advance()
Definition WalkResult.h:47
bool wasInterrupted() const
Returns true if the walk was interrupted.
Definition WalkResult.h:51
static WalkResult interrupt()
Definition WalkResult.h:46
LogicalResult verifyConstantExpressionInterface(Operation *op)
Verify that op conforms to the ConstantExpressionInterface.
LogicalResult verifyLabelBranchingOpInterface(Operation *op)
Verify that op conforms to the LabelBranchingOpInterface Checks that the branching is targetting some...
LogicalResult verifyLabelLevelInterface(Operation *op)
Verify that op conforms to the LabelLevelInterface op's target should defined at the same scope level...
Operations implementing this trait are considered as valid constant expressions in any context (In co...