2018-12-28 13:21:41 +08:00
|
|
|
//===- Operation.cpp - Operation support code -----------------------------===//
|
2018-07-05 11:45:39 +08:00
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
|
2019-03-27 05:45:38 +08:00
|
|
|
#include "mlir/IR/Operation.h"
|
|
|
|
#include "mlir/IR/BlockAndValueMapping.h"
|
2019-05-02 02:14:15 +08:00
|
|
|
#include "mlir/IR/Diagnostics.h"
|
2018-11-10 06:04:03 +08:00
|
|
|
#include "mlir/IR/Dialect.h"
|
2018-12-28 03:07:34 +08:00
|
|
|
#include "mlir/IR/Function.h"
|
2018-08-02 01:18:59 +08:00
|
|
|
#include "mlir/IR/MLIRContext.h"
|
2018-09-10 11:40:23 +08:00
|
|
|
#include "mlir/IR/OpDefinition.h"
|
2018-09-27 01:07:16 +08:00
|
|
|
#include "mlir/IR/OpImplementation.h"
|
2019-04-28 11:55:38 +08:00
|
|
|
#include "mlir/IR/PatternMatch.h"
|
2019-01-04 06:29:52 +08:00
|
|
|
#include "mlir/IR/StandardTypes.h"
|
2019-10-06 01:05:40 +08:00
|
|
|
#include "mlir/IR/TypeUtilities.h"
|
2019-03-27 08:28:18 +08:00
|
|
|
#include <numeric>
|
2019-10-06 01:05:40 +08:00
|
|
|
|
2018-07-05 11:45:39 +08:00
|
|
|
using namespace mlir;
|
|
|
|
|
2018-10-10 13:08:52 +08:00
|
|
|
/// Form the OperationName for an op with the specified string. This either is
|
|
|
|
/// a reference to an AbstractOperation if one is known, or a uniqued Identifier
|
|
|
|
/// if not.
|
|
|
|
OperationName::OperationName(StringRef name, MLIRContext *context) {
|
2018-10-22 10:49:31 +08:00
|
|
|
if (auto *op = AbstractOperation::lookup(name, context))
|
2018-10-10 13:08:52 +08:00
|
|
|
representation = op;
|
|
|
|
else
|
|
|
|
representation = Identifier::get(name, context);
|
|
|
|
}
|
|
|
|
|
2019-06-04 07:32:25 +08:00
|
|
|
/// Return the name of the dialect this operation is registered to.
|
|
|
|
StringRef OperationName::getDialect() const {
|
|
|
|
return getStringRef().split('.').first;
|
|
|
|
}
|
|
|
|
|
2018-10-10 13:08:52 +08:00
|
|
|
/// Return the name of this operation. This always succeeds.
|
|
|
|
StringRef OperationName::getStringRef() const {
|
|
|
|
if (auto *op = representation.dyn_cast<const AbstractOperation *>())
|
|
|
|
return op->name;
|
|
|
|
return representation.get<Identifier>().strref();
|
|
|
|
}
|
|
|
|
|
|
|
|
const AbstractOperation *OperationName::getAbstractOperation() const {
|
|
|
|
return representation.dyn_cast<const AbstractOperation *>();
|
|
|
|
}
|
|
|
|
|
|
|
|
OperationName OperationName::getFromOpaquePointer(void *pointer) {
|
|
|
|
return OperationName(RepresentationUnion::getFromOpaqueValue(pointer));
|
|
|
|
}
|
|
|
|
|
2018-10-22 10:49:31 +08:00
|
|
|
OpAsmParser::~OpAsmParser() {}
|
|
|
|
|
2019-03-27 05:45:38 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-03-27 08:05:09 +08:00
|
|
|
// OpResult
|
2019-03-27 05:45:38 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Return the result number of this result.
|
2019-03-27 08:05:09 +08:00
|
|
|
unsigned OpResult::getResultNumber() {
|
2019-03-27 05:45:38 +08:00
|
|
|
// Results are always stored consecutively, so use pointer subtraction to
|
|
|
|
// figure out what number this is.
|
2019-03-27 08:05:09 +08:00
|
|
|
return this - &getOwner()->getOpResults()[0];
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2019-03-29 02:25:19 +08:00
|
|
|
// OpOperand
|
2019-03-27 05:45:38 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-05-05 03:13:41 +08:00
|
|
|
// TODO: This namespace is only required because of a bug in GCC<7.0.
|
|
|
|
namespace mlir {
|
2019-03-27 05:45:38 +08:00
|
|
|
/// Return which operand this is in the operand list.
|
2019-03-29 02:25:19 +08:00
|
|
|
template <> unsigned OpOperand::getOperandNumber() {
|
|
|
|
return this - &getOwner()->getOpOperands()[0];
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
2019-05-05 03:13:41 +08:00
|
|
|
} // end namespace mlir
|
2019-03-27 05:45:38 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BlockOperand
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-05-05 03:13:41 +08:00
|
|
|
// TODO: This namespace is only required because of a bug in GCC<7.0.
|
|
|
|
namespace mlir {
|
2019-03-27 05:45:38 +08:00
|
|
|
/// Return which operand this is in the operand list.
|
|
|
|
template <> unsigned BlockOperand::getOperandNumber() {
|
|
|
|
return this - &getOwner()->getBlockOperands()[0];
|
|
|
|
}
|
2019-05-05 03:13:41 +08:00
|
|
|
} // end namespace mlir
|
2019-03-27 05:45:38 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Operation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Create a new Operation with the specific fields.
|
|
|
|
Operation *Operation::create(Location location, OperationName name,
|
|
|
|
ArrayRef<Type> resultTypes,
|
2019-09-29 00:35:23 +08:00
|
|
|
ArrayRef<Value *> operands,
|
2019-03-27 05:45:38 +08:00
|
|
|
ArrayRef<NamedAttribute> attributes,
|
|
|
|
ArrayRef<Block *> successors, unsigned numRegions,
|
2019-08-27 08:34:06 +08:00
|
|
|
bool resizableOperandList) {
|
2019-09-29 00:35:23 +08:00
|
|
|
return create(location, name, resultTypes, operands,
|
2019-05-07 03:40:43 +08:00
|
|
|
NamedAttributeList(attributes), successors, numRegions,
|
2019-08-27 08:34:06 +08:00
|
|
|
resizableOperandList);
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 20:11:58 +08:00
|
|
|
/// Create a new Operation from operation state.
|
|
|
|
Operation *Operation::create(const OperationState &state) {
|
|
|
|
unsigned numRegions = state.regions.size();
|
2019-09-29 00:35:23 +08:00
|
|
|
Operation *op = create(state.location, state.name, state.types,
|
|
|
|
state.operands, state.attributes, state.successors,
|
2019-08-27 08:34:06 +08:00
|
|
|
numRegions, state.resizableOperandList);
|
2019-03-27 20:11:58 +08:00
|
|
|
for (unsigned i = 0; i < numRegions; ++i)
|
|
|
|
if (state.regions[i])
|
2019-03-28 23:24:38 +08:00
|
|
|
op->getRegion(i).takeBody(*state.regions[i]);
|
|
|
|
return op;
|
2019-03-27 20:11:58 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 05:45:38 +08:00
|
|
|
/// Overload of create that takes an existing NamedAttributeList to avoid
|
|
|
|
/// unnecessarily uniquing a list of attributes.
|
|
|
|
Operation *Operation::create(Location location, OperationName name,
|
|
|
|
ArrayRef<Type> resultTypes,
|
2019-09-29 00:35:23 +08:00
|
|
|
ArrayRef<Value *> operands,
|
2019-03-27 05:45:38 +08:00
|
|
|
const NamedAttributeList &attributes,
|
|
|
|
ArrayRef<Block *> successors, unsigned numRegions,
|
2019-08-27 08:34:06 +08:00
|
|
|
bool resizableOperandList) {
|
2019-03-27 05:45:38 +08:00
|
|
|
unsigned numSuccessors = successors.size();
|
|
|
|
|
|
|
|
// Input operands are nullptr-separated for each successor, the null operands
|
|
|
|
// aren't actually stored.
|
|
|
|
unsigned numOperands = operands.size() - numSuccessors;
|
|
|
|
|
|
|
|
// Compute the byte size for the operation and the operand storage.
|
2019-03-27 08:05:09 +08:00
|
|
|
auto byteSize = totalSizeToAlloc<OpResult, BlockOperand, unsigned, Region,
|
2019-03-27 05:45:38 +08:00
|
|
|
detail::OperandStorage>(
|
|
|
|
resultTypes.size(), numSuccessors, numSuccessors, numRegions,
|
|
|
|
/*detail::OperandStorage*/ 1);
|
|
|
|
byteSize += llvm::alignTo(detail::OperandStorage::additionalAllocSize(
|
|
|
|
numOperands, resizableOperandList),
|
|
|
|
alignof(Operation));
|
|
|
|
void *rawMem = malloc(byteSize);
|
|
|
|
|
|
|
|
// Create the new Operation.
|
2019-08-27 08:34:06 +08:00
|
|
|
auto op = ::new (rawMem) Operation(location, name, resultTypes.size(),
|
|
|
|
numSuccessors, numRegions, attributes);
|
2019-03-27 05:45:38 +08:00
|
|
|
|
|
|
|
assert((numSuccessors == 0 || !op->isKnownNonTerminator()) &&
|
|
|
|
"unexpected successors in a non-terminator operation");
|
|
|
|
|
|
|
|
// Initialize the regions.
|
|
|
|
for (unsigned i = 0; i != numRegions; ++i)
|
|
|
|
new (&op->getRegion(i)) Region(op);
|
|
|
|
|
|
|
|
// Initialize the results and operands.
|
|
|
|
new (&op->getOperandStorage())
|
|
|
|
detail::OperandStorage(numOperands, resizableOperandList);
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
auto instResults = op->getOpResults();
|
2019-03-27 05:45:38 +08:00
|
|
|
for (unsigned i = 0, e = resultTypes.size(); i != e; ++i)
|
2019-03-27 08:05:09 +08:00
|
|
|
new (&instResults[i]) OpResult(resultTypes[i], op);
|
2019-03-27 05:45:38 +08:00
|
|
|
|
2019-03-29 02:25:19 +08:00
|
|
|
auto opOperands = op->getOpOperands();
|
2019-03-27 05:45:38 +08:00
|
|
|
|
|
|
|
// Initialize normal operands.
|
|
|
|
unsigned operandIt = 0, operandE = operands.size();
|
|
|
|
unsigned nextOperand = 0;
|
|
|
|
for (; operandIt != operandE; ++operandIt) {
|
|
|
|
// Null operands are used as sentinels between successor operand lists. If
|
|
|
|
// we encounter one here, break and handle the successor operands lists
|
|
|
|
// separately below.
|
|
|
|
if (!operands[operandIt])
|
|
|
|
break;
|
2019-03-29 02:25:19 +08:00
|
|
|
new (&opOperands[nextOperand++]) OpOperand(op, operands[operandIt]);
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned currentSuccNum = 0;
|
|
|
|
if (operandIt == operandE) {
|
|
|
|
// Verify that the amount of sentinel operands is equivalent to the number
|
|
|
|
// of successors.
|
|
|
|
assert(currentSuccNum == numSuccessors);
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!op->isKnownNonTerminator() &&
|
|
|
|
"Unexpected nullptr in operand list when creating non-terminator.");
|
|
|
|
auto instBlockOperands = op->getBlockOperands();
|
|
|
|
unsigned *succOperandCountIt = op->getTrailingObjects<unsigned>();
|
|
|
|
unsigned *succOperandCountE = succOperandCountIt + numSuccessors;
|
|
|
|
(void)succOperandCountE;
|
|
|
|
|
|
|
|
for (; operandIt != operandE; ++operandIt) {
|
|
|
|
// If we encounter a sentinel branch to the next operand update the count
|
|
|
|
// variable.
|
|
|
|
if (!operands[operandIt]) {
|
|
|
|
assert(currentSuccNum < numSuccessors);
|
|
|
|
|
|
|
|
// After the first iteration update the successor operand count
|
|
|
|
// variable.
|
|
|
|
if (currentSuccNum != 0) {
|
|
|
|
++succOperandCountIt;
|
|
|
|
assert(succOperandCountIt != succOperandCountE &&
|
|
|
|
"More sentinel operands than successors.");
|
|
|
|
}
|
|
|
|
|
|
|
|
new (&instBlockOperands[currentSuccNum])
|
|
|
|
BlockOperand(op, successors[currentSuccNum]);
|
|
|
|
*succOperandCountIt = 0;
|
|
|
|
++currentSuccNum;
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-29 02:25:19 +08:00
|
|
|
new (&opOperands[nextOperand++]) OpOperand(op, operands[operandIt]);
|
2019-03-27 05:45:38 +08:00
|
|
|
++(*succOperandCountIt);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the amount of sentinel operands is equivalent to the number of
|
|
|
|
// successors.
|
|
|
|
assert(currentSuccNum == numSuccessors);
|
|
|
|
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
|
|
|
Operation::Operation(Location location, OperationName name, unsigned numResults,
|
|
|
|
unsigned numSuccessors, unsigned numRegions,
|
2019-08-27 08:34:06 +08:00
|
|
|
const NamedAttributeList &attributes)
|
2019-03-27 05:45:38 +08:00
|
|
|
: location(location), numResults(numResults), numSuccs(numSuccessors),
|
|
|
|
numRegions(numRegions), name(name), attrs(attributes) {}
|
|
|
|
|
|
|
|
// Operations are deleted through the destroy() member because they are
|
|
|
|
// allocated via malloc.
|
|
|
|
Operation::~Operation() {
|
|
|
|
assert(block == nullptr && "operation destroyed but still in a block");
|
|
|
|
|
|
|
|
// Explicitly run the destructors for the operands and results.
|
|
|
|
getOperandStorage().~OperandStorage();
|
|
|
|
|
2019-03-27 08:05:09 +08:00
|
|
|
for (auto &result : getOpResults())
|
|
|
|
result.~OpResult();
|
2019-03-27 05:45:38 +08:00
|
|
|
|
|
|
|
// Explicitly run the destructors for the successors.
|
|
|
|
for (auto &successor : getBlockOperands())
|
|
|
|
successor.~BlockOperand();
|
|
|
|
|
|
|
|
// Explicitly destroy the regions.
|
|
|
|
for (auto ®ion : getRegions())
|
|
|
|
region.~Region();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Destroy this operation or one of its subclasses.
|
|
|
|
void Operation::destroy() {
|
|
|
|
this->~Operation();
|
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the context this operation is associated with.
|
2019-06-25 00:22:07 +08:00
|
|
|
MLIRContext *Operation::getContext() { return location->getContext(); }
|
2019-03-27 05:45:38 +08:00
|
|
|
|
|
|
|
/// Return the dialact this operation is associated with, or nullptr if the
|
|
|
|
/// associated dialect is not registered.
|
|
|
|
Dialect *Operation::getDialect() {
|
|
|
|
if (auto *abstractOp = getAbstractOperation())
|
|
|
|
return &abstractOp->dialect;
|
|
|
|
|
|
|
|
// If this operation hasn't been registered or doesn't have abstract
|
2019-06-04 07:32:25 +08:00
|
|
|
// operation, try looking up the dialect name in the context.
|
|
|
|
return getContext()->getRegisteredDialect(getName().getDialect());
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
2019-08-10 11:07:25 +08:00
|
|
|
Region *Operation::getParentRegion() {
|
2019-04-25 00:24:57 +08:00
|
|
|
return block ? block->getParent() : nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:55:17 +08:00
|
|
|
Operation *Operation::getParentOp() {
|
2019-08-10 11:07:25 +08:00
|
|
|
return block ? block->getParentOp() : nullptr;
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
2019-09-19 09:23:12 +08:00
|
|
|
/// Return true if this operation is a proper ancestor of the `other`
|
|
|
|
/// operation.
|
|
|
|
bool Operation::isProperAncestor(Operation *other) {
|
|
|
|
while ((other = other->getParentOp()))
|
|
|
|
if (this == other)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-20 06:42:49 +08:00
|
|
|
/// Replace any uses of 'from' with 'to' within this operation.
|
|
|
|
void Operation::replaceUsesOfWith(Value *from, Value *to) {
|
|
|
|
if (from == to)
|
|
|
|
return;
|
|
|
|
for (auto &operand : getOpOperands())
|
|
|
|
if (operand.get() == from)
|
|
|
|
operand.set(to);
|
|
|
|
}
|
|
|
|
|
2019-03-27 05:45:38 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Other
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-05-04 02:40:57 +08:00
|
|
|
/// Emit an error about fatal conditions with this operation, reporting up to
|
|
|
|
/// any diagnostic handlers that may be listening.
|
|
|
|
InFlightDiagnostic Operation::emitError(const Twine &message) {
|
2019-06-26 12:31:54 +08:00
|
|
|
return mlir::emitError(getLoc(), message);
|
2019-05-02 03:13:44 +08:00
|
|
|
}
|
|
|
|
|
2019-03-27 05:45:38 +08:00
|
|
|
/// Emit a warning about this operation, reporting up to any diagnostic
|
|
|
|
/// handlers that may be listening.
|
2019-05-04 01:01:01 +08:00
|
|
|
InFlightDiagnostic Operation::emitWarning(const Twine &message) {
|
2019-06-26 12:31:54 +08:00
|
|
|
return mlir::emitWarning(getLoc(), message);
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
2019-05-04 02:40:57 +08:00
|
|
|
/// Emit a remark about this operation, reporting up to any diagnostic
|
|
|
|
/// handlers that may be listening.
|
|
|
|
InFlightDiagnostic Operation::emitRemark(const Twine &message) {
|
2019-06-26 12:31:54 +08:00
|
|
|
return mlir::emitRemark(getLoc(), message);
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given an operation 'other' that is within the same parent block, return
|
|
|
|
/// whether the current operation is before 'other' in the operation list
|
|
|
|
/// of the parent block.
|
|
|
|
/// Note: This function has an average complexity of O(1), but worst case may
|
|
|
|
/// take O(N) where N is the number of operations within the parent block.
|
|
|
|
bool Operation::isBeforeInBlock(Operation *other) {
|
|
|
|
assert(block && "Operations without parent blocks have no order.");
|
|
|
|
assert(other && other->block == block &&
|
|
|
|
"Expected other operation to have the same parent block.");
|
|
|
|
// Recompute the parent ordering if necessary.
|
|
|
|
if (!block->isInstOrderValid())
|
|
|
|
block->recomputeInstOrder();
|
|
|
|
return orderIndex < other->orderIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ilist_traits for Operation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
auto llvm::ilist_detail::SpecificNodeAccess<
|
|
|
|
typename llvm::ilist_detail::compute_node_options<
|
|
|
|
::mlir::Operation>::type>::getNodePtr(pointer N) -> node_type * {
|
|
|
|
return NodeAccess::getNodePtr<OptionsT>(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto llvm::ilist_detail::SpecificNodeAccess<
|
|
|
|
typename llvm::ilist_detail::compute_node_options<
|
|
|
|
::mlir::Operation>::type>::getNodePtr(const_pointer N)
|
|
|
|
-> const node_type * {
|
|
|
|
return NodeAccess::getNodePtr<OptionsT>(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto llvm::ilist_detail::SpecificNodeAccess<
|
|
|
|
typename llvm::ilist_detail::compute_node_options<
|
|
|
|
::mlir::Operation>::type>::getValuePtr(node_type *N) -> pointer {
|
|
|
|
return NodeAccess::getValuePtr<OptionsT>(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto llvm::ilist_detail::SpecificNodeAccess<
|
|
|
|
typename llvm::ilist_detail::compute_node_options<
|
|
|
|
::mlir::Operation>::type>::getValuePtr(const node_type *N)
|
|
|
|
-> const_pointer {
|
|
|
|
return NodeAccess::getValuePtr<OptionsT>(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::ilist_traits<::mlir::Operation>::deleteNode(Operation *op) {
|
|
|
|
op->destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
Block *llvm::ilist_traits<::mlir::Operation>::getContainingBlock() {
|
|
|
|
size_t Offset(size_t(&((Block *)nullptr->*Block::getSublistAccess(nullptr))));
|
|
|
|
iplist<Operation> *Anchor(static_cast<iplist<Operation> *>(this));
|
|
|
|
return reinterpret_cast<Block *>(reinterpret_cast<char *>(Anchor) - Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a trait method invoked when a operation is added to a block. We
|
|
|
|
/// keep the block pointer up to date.
|
|
|
|
void llvm::ilist_traits<::mlir::Operation>::addNodeToList(Operation *op) {
|
|
|
|
assert(!op->getBlock() && "already in a operation block!");
|
|
|
|
op->block = getContainingBlock();
|
|
|
|
|
|
|
|
// Invalidate the block ordering.
|
|
|
|
op->block->invalidateInstOrder();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a trait method invoked when a operation is removed from a block.
|
|
|
|
/// We keep the block pointer up to date.
|
|
|
|
void llvm::ilist_traits<::mlir::Operation>::removeNodeFromList(Operation *op) {
|
|
|
|
assert(op->block && "not already in a operation block!");
|
|
|
|
op->block = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a trait method invoked when a operation is moved from one block
|
|
|
|
/// to another. We keep the block pointer up to date.
|
|
|
|
void llvm::ilist_traits<::mlir::Operation>::transferNodesFromList(
|
2019-03-27 08:05:09 +08:00
|
|
|
ilist_traits<Operation> &otherList, op_iterator first, op_iterator last) {
|
2019-03-27 05:45:38 +08:00
|
|
|
Block *curParent = getContainingBlock();
|
|
|
|
|
|
|
|
// Invalidate the ordering of the parent block.
|
|
|
|
curParent->invalidateInstOrder();
|
|
|
|
|
|
|
|
// If we are transferring operations within the same block, the block
|
|
|
|
// pointer doesn't need to be updated.
|
|
|
|
if (curParent == otherList.getContainingBlock())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Update the 'block' member of each operation.
|
|
|
|
for (; first != last; ++first)
|
|
|
|
first->block = curParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove this operation (and its descendants) from its Block and delete
|
|
|
|
/// all of them.
|
|
|
|
void Operation::erase() {
|
2019-07-04 04:21:24 +08:00
|
|
|
if (auto *parent = getBlock())
|
|
|
|
parent->getOperations().erase(this);
|
|
|
|
else
|
|
|
|
destroy();
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unlink this operation from its current block and insert it right before
|
|
|
|
/// `existingInst` which may be in the same or another block in the same
|
|
|
|
/// function.
|
|
|
|
void Operation::moveBefore(Operation *existingInst) {
|
|
|
|
moveBefore(existingInst->getBlock(), existingInst->getIterator());
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:55:17 +08:00
|
|
|
/// Unlink this operation from its current basic block and insert it right
|
|
|
|
/// before `iterator` in the specified basic block.
|
2019-03-27 05:45:38 +08:00
|
|
|
void Operation::moveBefore(Block *block,
|
|
|
|
llvm::iplist<Operation>::iterator iterator) {
|
2019-03-27 08:05:09 +08:00
|
|
|
block->getOperations().splice(iterator, getBlock()->getOperations(),
|
|
|
|
getIterator());
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This drops all operand uses from this operation, which is an essential
|
|
|
|
/// step in breaking cyclic dependences between references when they are to
|
|
|
|
/// be deleted.
|
|
|
|
void Operation::dropAllReferences() {
|
2019-03-29 02:25:19 +08:00
|
|
|
for (auto &op : getOpOperands())
|
2019-03-27 05:45:38 +08:00
|
|
|
op.drop();
|
|
|
|
|
|
|
|
for (auto ®ion : getRegions())
|
2019-07-12 01:00:54 +08:00
|
|
|
region.dropAllReferences();
|
2019-03-27 05:45:38 +08:00
|
|
|
|
|
|
|
for (auto &dest : getBlockOperands())
|
|
|
|
dest.drop();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This drops all uses of any values defined by this operation or its nested
|
|
|
|
/// regions, wherever they are located.
|
|
|
|
void Operation::dropAllDefinedValueUses() {
|
2019-03-27 08:05:09 +08:00
|
|
|
for (auto &val : getOpResults())
|
2019-03-27 05:45:38 +08:00
|
|
|
val.dropAllUses();
|
|
|
|
|
|
|
|
for (auto ®ion : getRegions())
|
|
|
|
for (auto &block : region)
|
|
|
|
block.dropAllDefinedValueUses();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if there are no users of any results of this operation.
|
|
|
|
bool Operation::use_empty() {
|
|
|
|
for (auto *result : getResults())
|
|
|
|
if (!result->use_empty())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Operation::setSuccessor(Block *block, unsigned index) {
|
|
|
|
assert(index < getNumSuccessors());
|
|
|
|
getBlockOperands()[index].set(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Operation::getNonSuccessorOperands() -> operand_range {
|
|
|
|
return {operand_iterator(this, 0),
|
2019-06-18 06:31:53 +08:00
|
|
|
operand_iterator(this, hasSuccessors() ? getSuccessorOperandIndex(0)
|
|
|
|
: getNumOperands())};
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the index of the first operand of the successor at the provided
|
|
|
|
/// index.
|
|
|
|
unsigned Operation::getSuccessorOperandIndex(unsigned index) {
|
|
|
|
assert(!isKnownNonTerminator() && "only terminators may have successors");
|
|
|
|
assert(index < getNumSuccessors());
|
|
|
|
|
|
|
|
// Count the number of operands for each of the successors after, and
|
|
|
|
// including, the one at 'index'. This is based upon the assumption that all
|
|
|
|
// non successor operands are placed at the beginning of the operand list.
|
|
|
|
auto *successorOpCountBegin = getTrailingObjects<unsigned>();
|
|
|
|
unsigned postSuccessorOpCount =
|
|
|
|
std::accumulate(successorOpCountBegin + index,
|
|
|
|
successorOpCountBegin + getNumSuccessors(), 0u);
|
|
|
|
return getNumOperands() - postSuccessorOpCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Operation::getSuccessorOperands(unsigned index) -> operand_range {
|
|
|
|
unsigned succOperandIndex = getSuccessorOperandIndex(index);
|
|
|
|
return {operand_iterator(this, succOperandIndex),
|
|
|
|
operand_iterator(this,
|
|
|
|
succOperandIndex + getNumSuccessorOperands(index))};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempt to fold this operation using the Op's registered foldHook.
|
2019-05-17 03:51:45 +08:00
|
|
|
LogicalResult Operation::fold(ArrayRef<Attribute> operands,
|
|
|
|
SmallVectorImpl<OpFoldResult> &results) {
|
|
|
|
// If we have a registered operation definition matching this one, use it to
|
|
|
|
// try to constant fold the operation.
|
|
|
|
auto *abstractOp = getAbstractOperation();
|
|
|
|
if (abstractOp && succeeded(abstractOp->foldHook(this, operands, results)))
|
|
|
|
return success();
|
|
|
|
|
|
|
|
// Otherwise, fall back on the dialect hook to handle it.
|
2019-06-04 07:32:25 +08:00
|
|
|
Dialect *dialect = getDialect();
|
|
|
|
if (!dialect)
|
|
|
|
return failure();
|
2019-05-17 03:51:45 +08:00
|
|
|
|
|
|
|
SmallVector<Attribute, 8> constants;
|
|
|
|
if (failed(dialect->constantFoldHook(this, operands, constants)))
|
|
|
|
return failure();
|
|
|
|
results.assign(constants.begin(), constants.end());
|
|
|
|
return success();
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit an error with the op name prefixed, like "'dim' op " which is
|
|
|
|
/// convenient for verifiers.
|
2019-05-04 01:01:01 +08:00
|
|
|
InFlightDiagnostic Operation::emitOpError(const Twine &message) {
|
2019-05-20 13:35:01 +08:00
|
|
|
return emitError() << "'" << getName() << "' op " << message;
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Operation Cloning
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-03-29 06:58:53 +08:00
|
|
|
/// Create a deep copy of this operation but keep the operation regions empty.
|
|
|
|
/// Operands are remapped using `mapper` (if present), and `mapper` is updated
|
|
|
|
/// to contain the results.
|
2019-06-28 07:42:50 +08:00
|
|
|
Operation *Operation::cloneWithoutRegions(BlockAndValueMapping &mapper) {
|
2019-03-27 05:45:38 +08:00
|
|
|
SmallVector<Value *, 8> operands;
|
|
|
|
SmallVector<Block *, 2> successors;
|
|
|
|
|
|
|
|
operands.reserve(getNumOperands() + getNumSuccessors());
|
|
|
|
|
|
|
|
if (getNumSuccessors() == 0) {
|
|
|
|
// Non-branching operations can just add all the operands.
|
|
|
|
for (auto *opValue : getOperands())
|
|
|
|
operands.push_back(mapper.lookupOrDefault(opValue));
|
|
|
|
} else {
|
|
|
|
// We add the operands separated by nullptr's for each successor.
|
|
|
|
unsigned firstSuccOperand =
|
|
|
|
getNumSuccessors() ? getSuccessorOperandIndex(0) : getNumOperands();
|
2019-03-29 02:25:19 +08:00
|
|
|
auto opOperands = getOpOperands();
|
2019-03-27 05:45:38 +08:00
|
|
|
|
|
|
|
unsigned i = 0;
|
|
|
|
for (; i != firstSuccOperand; ++i)
|
2019-03-29 02:25:19 +08:00
|
|
|
operands.push_back(mapper.lookupOrDefault(opOperands[i].get()));
|
2019-03-27 05:45:38 +08:00
|
|
|
|
|
|
|
successors.reserve(getNumSuccessors());
|
|
|
|
for (unsigned succ = 0, e = getNumSuccessors(); succ != e; ++succ) {
|
|
|
|
successors.push_back(mapper.lookupOrDefault(getSuccessor(succ)));
|
|
|
|
|
|
|
|
// Add sentinel to delineate successor operands.
|
|
|
|
operands.push_back(nullptr);
|
|
|
|
|
|
|
|
// Remap the successors operands.
|
|
|
|
for (auto *operand : getSuccessorOperands(succ))
|
|
|
|
operands.push_back(mapper.lookupOrDefault(operand));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 04:28:55 +08:00
|
|
|
SmallVector<Type, 8> resultTypes(getResultTypes());
|
2019-03-27 05:45:38 +08:00
|
|
|
unsigned numRegions = getNumRegions();
|
2019-08-27 08:34:06 +08:00
|
|
|
auto *newOp =
|
2019-09-29 00:35:23 +08:00
|
|
|
Operation::create(getLoc(), getName(), resultTypes, operands, attrs,
|
2019-08-27 08:34:06 +08:00
|
|
|
successors, numRegions, hasResizableOperandsList());
|
2019-03-27 05:45:38 +08:00
|
|
|
|
2019-03-29 06:58:53 +08:00
|
|
|
// Remember the mapping of any results.
|
|
|
|
for (unsigned i = 0, e = getNumResults(); i != e; ++i)
|
|
|
|
mapper.map(getResult(i), newOp->getResult(i));
|
|
|
|
|
|
|
|
return newOp;
|
|
|
|
}
|
|
|
|
|
2019-06-28 07:42:50 +08:00
|
|
|
Operation *Operation::cloneWithoutRegions() {
|
2019-03-29 06:58:53 +08:00
|
|
|
BlockAndValueMapping mapper;
|
2019-06-28 07:42:50 +08:00
|
|
|
return cloneWithoutRegions(mapper);
|
2019-03-29 06:58:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a deep copy of this operation, remapping any operands that use
|
|
|
|
/// values outside of the operation using the map that is provided (leaving
|
|
|
|
/// them alone if no entry is present). Replaces references to cloned
|
|
|
|
/// sub-operations to the corresponding operation that is copied, and adds
|
|
|
|
/// those mappings to the map.
|
2019-06-28 07:42:50 +08:00
|
|
|
Operation *Operation::clone(BlockAndValueMapping &mapper) {
|
|
|
|
auto *newOp = cloneWithoutRegions(mapper);
|
2019-03-29 06:58:53 +08:00
|
|
|
|
2019-03-27 05:45:38 +08:00
|
|
|
// Clone the regions.
|
|
|
|
for (unsigned i = 0; i != numRegions; ++i)
|
2019-06-28 07:42:50 +08:00
|
|
|
getRegion(i).cloneInto(&newOp->getRegion(i), mapper);
|
2019-03-27 05:45:38 +08:00
|
|
|
|
|
|
|
return newOp;
|
|
|
|
}
|
|
|
|
|
2019-06-28 07:42:50 +08:00
|
|
|
Operation *Operation::clone() {
|
2019-03-27 05:45:38 +08:00
|
|
|
BlockAndValueMapping mapper;
|
2019-06-28 07:42:50 +08:00
|
|
|
return clone(mapper);
|
2019-03-27 05:45:38 +08:00
|
|
|
}
|
|
|
|
|
2018-09-10 11:40:23 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-09-27 06:06:38 +08:00
|
|
|
// OpState trait class.
|
2018-09-10 11:40:23 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-01-24 03:26:56 +08:00
|
|
|
// The fallback for the parser is to reject the custom assembly form.
|
2019-09-21 10:47:05 +08:00
|
|
|
ParseResult OpState::parse(OpAsmParser &parser, OperationState &result) {
|
2019-09-21 02:36:49 +08:00
|
|
|
return parser.emitError(parser.getNameLoc(), "has no custom assembly form");
|
2018-10-22 10:49:31 +08:00
|
|
|
}
|
|
|
|
|
2019-01-24 03:26:56 +08:00
|
|
|
// The fallback for the printer is to print in the generic assembly form.
|
2019-09-21 11:43:02 +08:00
|
|
|
void OpState::print(OpAsmPrinter &p) { p.printGenericOp(getOperation()); }
|
2018-10-22 10:49:31 +08:00
|
|
|
|
2018-09-10 11:40:23 +08:00
|
|
|
/// Emit an error about fatal conditions with this operation, reporting up to
|
2019-05-04 01:01:01 +08:00
|
|
|
/// any diagnostic handlers that may be listening.
|
|
|
|
InFlightDiagnostic OpState::emitError(const Twine &message) {
|
2019-03-27 08:05:09 +08:00
|
|
|
return getOperation()->emitError(message);
|
2018-09-10 11:40:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit an error with the op name prefixed, like "'dim' op " which is
|
|
|
|
/// convenient for verifiers.
|
2019-05-04 01:01:01 +08:00
|
|
|
InFlightDiagnostic OpState::emitOpError(const Twine &message) {
|
2019-03-27 08:05:09 +08:00
|
|
|
return getOperation()->emitOpError(message);
|
2018-09-10 11:40:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit a warning about this operation, reporting up to any diagnostic
|
|
|
|
/// handlers that may be listening.
|
2019-05-04 01:01:01 +08:00
|
|
|
InFlightDiagnostic OpState::emitWarning(const Twine &message) {
|
|
|
|
return getOperation()->emitWarning(message);
|
2018-09-10 11:40:23 +08:00
|
|
|
}
|
|
|
|
|
2019-05-02 03:13:44 +08:00
|
|
|
/// Emit a remark about this operation, reporting up to any diagnostic
|
|
|
|
/// handlers that may be listening.
|
2019-05-04 01:01:01 +08:00
|
|
|
InFlightDiagnostic OpState::emitRemark(const Twine &message) {
|
|
|
|
return getOperation()->emitRemark(message);
|
2019-05-02 03:13:44 +08:00
|
|
|
}
|
|
|
|
|
2018-09-27 01:07:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Op Trait implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyZeroOperands(Operation *op) {
|
2018-09-27 12:18:42 +08:00
|
|
|
if (op->getNumOperands() != 0)
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError() << "requires zero operands";
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-09-27 12:18:42 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyOneOperand(Operation *op) {
|
2018-09-27 12:18:42 +08:00
|
|
|
if (op->getNumOperands() != 1)
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError() << "requires a single operand";
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-09-27 12:18:42 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyNOperands(Operation *op,
|
|
|
|
unsigned numOperands) {
|
2018-10-10 06:04:27 +08:00
|
|
|
if (op->getNumOperands() != numOperands) {
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError() << "expected " << numOperands
|
|
|
|
<< " operands, but found " << op->getNumOperands();
|
2018-10-10 06:04:27 +08:00
|
|
|
}
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-09-27 12:18:42 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyAtLeastNOperands(Operation *op,
|
|
|
|
unsigned numOperands) {
|
2018-09-27 12:18:42 +08:00
|
|
|
if (op->getNumOperands() < numOperands)
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError()
|
|
|
|
<< "expected " << numOperands << " or more operands";
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-09-27 12:18:42 +08:00
|
|
|
}
|
|
|
|
|
2018-11-07 07:37:39 +08:00
|
|
|
/// If this is a vector type, or a tensor type, return the scalar element type
|
|
|
|
/// that it is built around, otherwise return the type unmodified.
|
|
|
|
static Type getTensorOrVectorElementType(Type type) {
|
|
|
|
if (auto vec = type.dyn_cast<VectorType>())
|
|
|
|
return vec.getElementType();
|
|
|
|
|
|
|
|
// Look through tensor<vector<...>> to find the underlying element type.
|
|
|
|
if (auto tensor = type.dyn_cast<TensorType>())
|
|
|
|
return getTensorOrVectorElementType(tensor.getElementType());
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyOperandsAreIntegerLike(Operation *op) {
|
2019-05-25 04:28:55 +08:00
|
|
|
for (auto opType : op->getOperandTypes()) {
|
|
|
|
auto type = getTensorOrVectorElementType(opType);
|
2018-12-05 20:31:59 +08:00
|
|
|
if (!type.isIntOrIndex())
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError() << "requires an integer or index type";
|
2018-11-07 07:37:39 +08:00
|
|
|
}
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-11-07 07:37:39 +08:00
|
|
|
}
|
|
|
|
|
2019-05-07 08:51:08 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyOperandsAreFloatLike(Operation *op) {
|
2019-05-25 04:28:55 +08:00
|
|
|
for (auto opType : op->getOperandTypes()) {
|
|
|
|
auto type = getTensorOrVectorElementType(opType);
|
2019-05-07 08:51:08 +08:00
|
|
|
if (!type.isa<FloatType>())
|
|
|
|
return op->emitOpError("requires a float type");
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifySameTypeOperands(Operation *op) {
|
2018-11-07 07:37:39 +08:00
|
|
|
// Zero or one operand always have the "same" type.
|
|
|
|
unsigned nOperands = op->getNumOperands();
|
|
|
|
if (nOperands < 2)
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-11-07 07:37:39 +08:00
|
|
|
|
|
|
|
auto type = op->getOperand(0)->getType();
|
2019-05-25 04:28:55 +08:00
|
|
|
for (auto opType : llvm::drop_begin(op->getOperandTypes(), 1))
|
|
|
|
if (opType != type)
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError() << "requires all operands to have the same type";
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-11-07 07:37:39 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyZeroResult(Operation *op) {
|
2018-09-27 12:18:42 +08:00
|
|
|
if (op->getNumResults() != 0)
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError() << "requires zero results";
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-09-27 12:18:42 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyOneResult(Operation *op) {
|
2018-09-27 12:18:42 +08:00
|
|
|
if (op->getNumResults() != 1)
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError() << "requires one result";
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-09-27 12:18:42 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyNResults(Operation *op,
|
|
|
|
unsigned numOperands) {
|
2018-09-27 12:18:42 +08:00
|
|
|
if (op->getNumResults() != numOperands)
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError() << "expected " << numOperands << " results";
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-09-27 12:18:42 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyAtLeastNResults(Operation *op,
|
|
|
|
unsigned numOperands) {
|
2018-09-27 12:18:42 +08:00
|
|
|
if (op->getNumResults() < numOperands)
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError()
|
|
|
|
<< "expected " << numOperands << " or more results";
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-09-27 12:18:42 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
/// Returns success if the given two types have the same shape. That is,
|
2019-05-18 20:31:35 +08:00
|
|
|
/// they are both scalars (not shaped), or they are both shaped types and at
|
|
|
|
/// least one is unranked or they have the same shape. The element type does not
|
|
|
|
/// matter.
|
2019-04-03 04:09:34 +08:00
|
|
|
static LogicalResult verifyShapeMatch(Type type1, Type type2) {
|
2019-05-18 20:31:35 +08:00
|
|
|
auto sType1 = type1.dyn_cast<ShapedType>();
|
|
|
|
auto sType2 = type2.dyn_cast<ShapedType>();
|
2018-11-29 03:49:26 +08:00
|
|
|
|
2019-05-18 20:31:35 +08:00
|
|
|
// Either both or neither type should be shaped.
|
|
|
|
if (!sType1)
|
|
|
|
return success(!sType2);
|
2019-05-25 09:01:20 +08:00
|
|
|
if (!sType2)
|
|
|
|
return failure();
|
2018-11-29 03:49:26 +08:00
|
|
|
|
2019-05-25 08:46:58 +08:00
|
|
|
if (!sType1.hasRank() || !sType2.hasRank())
|
2019-05-18 20:31:35 +08:00
|
|
|
return success();
|
2018-11-29 03:49:26 +08:00
|
|
|
|
2019-05-18 20:31:35 +08:00
|
|
|
return success(sType1.getShape() == sType2.getShape());
|
2018-11-29 03:49:26 +08:00
|
|
|
}
|
|
|
|
|
2019-08-29 02:25:19 +08:00
|
|
|
LogicalResult OpTrait::impl::verifySameOperandsShape(Operation *op) {
|
2019-10-01 15:56:38 +08:00
|
|
|
if (failed(verifyAtLeastNOperands(op, 1)))
|
2019-08-29 02:25:19 +08:00
|
|
|
return failure();
|
|
|
|
|
|
|
|
auto type = op->getOperand(0)->getType();
|
|
|
|
for (auto opType : llvm::drop_begin(op->getOperandTypes(), 1)) {
|
|
|
|
if (failed(verifyShapeMatch(opType, type)))
|
|
|
|
return op->emitOpError() << "requires the same shape for all operands";
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifySameOperandsAndResultShape(Operation *op) {
|
2019-10-01 15:56:38 +08:00
|
|
|
if (failed(verifyAtLeastNOperands(op, 1)) ||
|
|
|
|
failed(verifyAtLeastNResults(op, 1)))
|
2019-04-03 04:09:34 +08:00
|
|
|
return failure();
|
2018-11-29 03:49:26 +08:00
|
|
|
|
|
|
|
auto type = op->getOperand(0)->getType();
|
2019-05-25 04:28:55 +08:00
|
|
|
for (auto resultType : op->getResultTypes()) {
|
|
|
|
if (failed(verifyShapeMatch(resultType, type)))
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError()
|
|
|
|
<< "requires the same shape for all operands and results";
|
2018-11-29 03:49:26 +08:00
|
|
|
}
|
2019-05-25 04:28:55 +08:00
|
|
|
for (auto opType : llvm::drop_begin(op->getOperandTypes(), 1)) {
|
|
|
|
if (failed(verifyShapeMatch(opType, type)))
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError()
|
|
|
|
<< "requires the same shape for all operands and results";
|
2018-11-29 03:49:26 +08:00
|
|
|
}
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-11-29 03:49:26 +08:00
|
|
|
}
|
|
|
|
|
2019-08-29 02:25:19 +08:00
|
|
|
LogicalResult OpTrait::impl::verifySameOperandsElementType(Operation *op) {
|
2019-10-01 15:56:38 +08:00
|
|
|
if (failed(verifyAtLeastNOperands(op, 1)))
|
2019-08-29 02:25:19 +08:00
|
|
|
return failure();
|
2019-10-06 01:05:40 +08:00
|
|
|
auto elementType = getElementTypeOrSelf(op->getOperand(0));
|
2019-08-29 02:25:19 +08:00
|
|
|
|
2019-10-06 01:05:40 +08:00
|
|
|
for (auto operand : llvm::drop_begin(op->getOperands(), 1)) {
|
|
|
|
if (getElementTypeOrSelf(operand) != elementType)
|
2019-08-29 02:25:19 +08:00
|
|
|
return op->emitOpError("requires the same element type for all operands");
|
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-05-05 02:14:40 +08:00
|
|
|
LogicalResult
|
|
|
|
OpTrait::impl::verifySameOperandsAndResultElementType(Operation *op) {
|
2019-10-01 15:56:38 +08:00
|
|
|
if (failed(verifyAtLeastNOperands(op, 1)) ||
|
|
|
|
failed(verifyAtLeastNResults(op, 1)))
|
2019-05-05 02:14:40 +08:00
|
|
|
return failure();
|
|
|
|
|
2019-10-06 01:05:40 +08:00
|
|
|
auto elementType = getElementTypeOrSelf(op->getResult(0));
|
2019-05-05 02:14:40 +08:00
|
|
|
|
|
|
|
// Verify result element type matches first result's element type.
|
|
|
|
for (auto result : drop_begin(op->getResults(), 1)) {
|
2019-10-06 01:05:40 +08:00
|
|
|
if (getElementTypeOrSelf(result) != elementType)
|
2019-05-05 02:14:40 +08:00
|
|
|
return op->emitOpError(
|
|
|
|
"requires the same element type for all operands and results");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify operand's element type matches first result's element type.
|
|
|
|
for (auto operand : op->getOperands()) {
|
2019-10-06 01:05:40 +08:00
|
|
|
if (getElementTypeOrSelf(operand) != elementType)
|
2019-05-05 02:14:40 +08:00
|
|
|
return op->emitOpError(
|
|
|
|
"requires the same element type for all operands and results");
|
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifySameOperandsAndResultType(Operation *op) {
|
2019-10-01 15:56:38 +08:00
|
|
|
if (failed(verifyAtLeastNOperands(op, 1)) ||
|
|
|
|
failed(verifyAtLeastNResults(op, 1)))
|
2019-04-03 04:09:34 +08:00
|
|
|
return failure();
|
2018-11-29 03:49:26 +08:00
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
auto type = op->getResult(0)->getType();
|
2019-05-25 04:28:55 +08:00
|
|
|
for (auto resultType : llvm::drop_begin(op->getResultTypes(), 1)) {
|
|
|
|
if (resultType != type)
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError()
|
|
|
|
<< "requires the same type for all operands and results";
|
2018-09-27 01:07:16 +08:00
|
|
|
}
|
2019-05-25 04:28:55 +08:00
|
|
|
for (auto opType : op->getOperandTypes()) {
|
|
|
|
if (opType != type)
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError()
|
|
|
|
<< "requires the same type for all operands and results";
|
2018-09-27 01:07:16 +08:00
|
|
|
}
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-09-27 01:07:16 +08:00
|
|
|
}
|
|
|
|
|
2019-04-05 07:24:10 +08:00
|
|
|
static LogicalResult verifyBBArguments(Operation::operand_range operands,
|
|
|
|
Block *destBB, Operation *op) {
|
2018-11-16 01:56:06 +08:00
|
|
|
unsigned operandCount = std::distance(operands.begin(), operands.end());
|
2018-12-08 01:30:25 +08:00
|
|
|
if (operandCount != destBB->getNumArguments())
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitError() << "branch has " << operandCount
|
|
|
|
<< " operands, but target block has "
|
|
|
|
<< destBB->getNumArguments();
|
2018-11-16 01:56:06 +08:00
|
|
|
|
|
|
|
auto operandIt = operands.begin();
|
|
|
|
for (unsigned i = 0, e = operandCount; i != e; ++i, ++operandIt) {
|
2018-12-08 01:30:25 +08:00
|
|
|
if ((*operandIt)->getType() != destBB->getArgument(i)->getType())
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitError() << "type mismatch in bb argument #" << i;
|
2018-11-16 01:56:06 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-11-16 01:56:06 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
static LogicalResult verifyTerminatorSuccessors(Operation *op) {
|
2019-08-10 11:07:25 +08:00
|
|
|
auto *parent = op->getParentRegion();
|
2019-07-02 01:29:09 +08:00
|
|
|
|
2018-11-16 01:56:06 +08:00
|
|
|
// Verify that the operands lines up with the BB arguments in the successor.
|
|
|
|
for (unsigned i = 0, e = op->getNumSuccessors(); i != e; ++i) {
|
|
|
|
auto *succ = op->getSuccessor(i);
|
2019-07-02 01:29:09 +08:00
|
|
|
if (succ->getParent() != parent)
|
|
|
|
return op->emitError("reference to block defined in another region");
|
2019-04-03 04:09:34 +08:00
|
|
|
if (failed(verifyBBArguments(op->getSuccessorOperands(i), succ, op)))
|
|
|
|
return failure();
|
2018-11-16 01:56:06 +08:00
|
|
|
}
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-11-16 01:56:06 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyIsTerminator(Operation *op) {
|
2019-03-22 08:53:00 +08:00
|
|
|
Block *block = op->getBlock();
|
2018-11-14 01:49:27 +08:00
|
|
|
// Verify that the operation is at the end of the respective parent block.
|
2018-12-30 01:11:58 +08:00
|
|
|
if (!block || &block->back() != op)
|
2019-03-27 05:45:38 +08:00
|
|
|
return op->emitOpError("must be the last operation in the parent block");
|
2018-12-30 01:11:58 +08:00
|
|
|
|
2018-11-16 01:56:06 +08:00
|
|
|
// Verify the state of the successor blocks.
|
2019-04-03 04:09:34 +08:00
|
|
|
if (op->getNumSuccessors() != 0 && failed(verifyTerminatorSuccessors(op)))
|
|
|
|
return failure();
|
|
|
|
return success();
|
2018-11-14 01:49:27 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyResultsAreBoolLike(Operation *op) {
|
2019-05-25 04:28:55 +08:00
|
|
|
for (auto resultType : op->getResultTypes()) {
|
|
|
|
auto elementType = getTensorOrVectorElementType(resultType);
|
2019-01-04 06:29:52 +08:00
|
|
|
bool isBoolType = elementType.isInteger(1);
|
2018-11-29 03:49:26 +08:00
|
|
|
if (!isBoolType)
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError() << "requires a bool result type";
|
2018-11-29 03:49:26 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-11-29 03:49:26 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyResultsAreFloatLike(Operation *op) {
|
2019-05-25 04:28:55 +08:00
|
|
|
for (auto resultType : op->getResultTypes())
|
|
|
|
if (!getTensorOrVectorElementType(resultType).isa<FloatType>())
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError() << "requires a floating point type";
|
2018-09-27 01:07:16 +08:00
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-09-27 01:07:16 +08:00
|
|
|
}
|
|
|
|
|
2019-04-03 04:09:34 +08:00
|
|
|
LogicalResult OpTrait::impl::verifyResultsAreIntegerLike(Operation *op) {
|
2019-05-25 04:28:55 +08:00
|
|
|
for (auto resultType : op->getResultTypes())
|
|
|
|
if (!getTensorOrVectorElementType(resultType).isIntOrIndex())
|
2019-05-04 02:40:57 +08:00
|
|
|
return op->emitOpError() << "requires an integer or index type";
|
2019-04-03 04:09:34 +08:00
|
|
|
return success();
|
2018-09-27 01:07:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BinaryOp implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// These functions are out-of-line implementations of the methods in BinaryOp,
|
|
|
|
// which avoids them being template instantiated/duplicated.
|
|
|
|
|
2019-09-21 10:47:05 +08:00
|
|
|
void impl::buildBinaryOp(Builder *builder, OperationState &result, Value *lhs,
|
2018-12-28 06:35:10 +08:00
|
|
|
Value *rhs) {
|
2018-09-27 01:07:16 +08:00
|
|
|
assert(lhs->getType() == rhs->getType());
|
2019-09-21 10:47:05 +08:00
|
|
|
result.addOperands({lhs, rhs});
|
|
|
|
result.types.push_back(lhs->getType());
|
2018-09-27 01:07:16 +08:00
|
|
|
}
|
|
|
|
|
2019-10-04 03:59:42 +08:00
|
|
|
ParseResult impl::parseOneResultSameOperandTypeOp(OpAsmParser &parser,
|
|
|
|
OperationState &result) {
|
2018-09-27 01:07:16 +08:00
|
|
|
SmallVector<OpAsmParser::OperandType, 2> ops;
|
2018-10-31 05:59:22 +08:00
|
|
|
Type type;
|
2019-10-04 03:59:42 +08:00
|
|
|
return failure(parser.parseOperandList(ops) ||
|
2019-09-21 10:47:05 +08:00
|
|
|
parser.parseOptionalAttributeDict(result.attributes) ||
|
2019-09-21 02:36:49 +08:00
|
|
|
parser.parseColonType(type) ||
|
2019-09-21 10:47:05 +08:00
|
|
|
parser.resolveOperands(ops, type, result.operands) ||
|
|
|
|
parser.addTypeToList(type, result.types));
|
2018-09-27 01:07:16 +08:00
|
|
|
}
|
|
|
|
|
2019-10-04 03:59:42 +08:00
|
|
|
void impl::printOneResultOp(Operation *op, OpAsmPrinter &p) {
|
|
|
|
assert(op->getNumResults() == 1 && "op should have one result");
|
2019-01-17 04:49:11 +08:00
|
|
|
|
|
|
|
// If not all the operand and result types are the same, just use the
|
2019-01-24 03:26:56 +08:00
|
|
|
// generic assembly form to avoid omitting information in printing.
|
2019-01-17 04:49:11 +08:00
|
|
|
auto resultType = op->getResult(0)->getType();
|
2019-10-04 03:59:42 +08:00
|
|
|
if (llvm::any_of(op->getOperandTypes(),
|
|
|
|
[&](Type type) { return type != resultType; })) {
|
2019-09-21 11:43:02 +08:00
|
|
|
p.printGenericOp(op);
|
2019-01-17 04:49:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-04 03:59:42 +08:00
|
|
|
p << op->getName() << ' ';
|
|
|
|
p.printOperands(op->getOperands());
|
2019-09-21 11:43:02 +08:00
|
|
|
p.printOptionalAttrDict(op->getAttrs());
|
2019-01-17 04:49:11 +08:00
|
|
|
// Now we can output only one type for all operands and the result.
|
2019-10-04 03:59:42 +08:00
|
|
|
p << " : " << resultType;
|
2018-09-27 01:07:16 +08:00
|
|
|
}
|
2018-10-23 00:00:03 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CastOp implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-09-21 10:47:05 +08:00
|
|
|
void impl::buildCastOp(Builder *builder, OperationState &result, Value *source,
|
2018-12-28 06:35:10 +08:00
|
|
|
Type destType) {
|
2019-09-21 10:47:05 +08:00
|
|
|
result.addOperands(source);
|
|
|
|
result.addTypes(destType);
|
2018-10-23 00:00:03 +08:00
|
|
|
}
|
|
|
|
|
2019-09-21 10:47:05 +08:00
|
|
|
ParseResult impl::parseCastOp(OpAsmParser &parser, OperationState &result) {
|
2018-10-23 00:00:03 +08:00
|
|
|
OpAsmParser::OperandType srcInfo;
|
2018-10-31 05:59:22 +08:00
|
|
|
Type srcType, dstType;
|
2019-09-21 02:36:49 +08:00
|
|
|
return failure(parser.parseOperand(srcInfo) ||
|
2019-09-21 10:47:05 +08:00
|
|
|
parser.parseOptionalAttributeDict(result.attributes) ||
|
2019-09-21 02:36:49 +08:00
|
|
|
parser.parseColonType(srcType) ||
|
2019-09-21 10:47:05 +08:00
|
|
|
parser.resolveOperand(srcInfo, srcType, result.operands) ||
|
2019-09-21 02:36:49 +08:00
|
|
|
parser.parseKeywordType("to", dstType) ||
|
2019-09-21 10:47:05 +08:00
|
|
|
parser.addTypeToList(dstType, result.types));
|
2018-10-23 00:00:03 +08:00
|
|
|
}
|
|
|
|
|
2019-09-21 11:43:02 +08:00
|
|
|
void impl::printCastOp(Operation *op, OpAsmPrinter &p) {
|
|
|
|
p << op->getName() << ' ' << *op->getOperand(0);
|
|
|
|
p.printOptionalAttrDict(op->getAttrs());
|
|
|
|
p << " : " << op->getOperand(0)->getType() << " to "
|
|
|
|
<< op->getResult(0)->getType();
|
2018-10-23 00:00:03 +08:00
|
|
|
}
|
2019-04-28 11:55:38 +08:00
|
|
|
|
|
|
|
Value *impl::foldCastOp(Operation *op) {
|
|
|
|
// Identity cast
|
|
|
|
if (op->getOperand(0)->getType() == op->getResult(0)->getType())
|
|
|
|
return op->getOperand(0);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-06-22 11:20:27 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CastOp implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Insert an operation, generated by `buildTerminatorOp`, at the end of the
|
|
|
|
/// region's only block if it does not have a terminator already. If the region
|
|
|
|
/// is empty, insert a new block first. `buildTerminatorOp` should return the
|
|
|
|
/// terminator operation to insert.
|
|
|
|
void impl::ensureRegionTerminator(
|
|
|
|
Region ®ion, Location loc,
|
|
|
|
llvm::function_ref<Operation *()> buildTerminatorOp) {
|
|
|
|
if (region.empty())
|
|
|
|
region.push_back(new Block);
|
|
|
|
|
|
|
|
Block &block = region.back();
|
|
|
|
if (!block.empty() && block.back().isKnownTerminator())
|
|
|
|
return;
|
|
|
|
|
|
|
|
block.push_back(buildTerminatorOp());
|
|
|
|
}
|
2019-09-10 09:12:12 +08:00
|
|
|
|
|
|
|
UseIterator::UseIterator(Operation *op, bool end)
|
|
|
|
: op(op), res(end ? op->result_end() : op->result_begin()) {
|
|
|
|
// Only initialize current use if there are results/can be uses.
|
|
|
|
if (op->getNumResults())
|
|
|
|
skipOverResultsWithNoUsers();
|
|
|
|
}
|
|
|
|
|
|
|
|
UseIterator &UseIterator::operator++() {
|
|
|
|
// We increment over uses, if we reach the last use then move to next
|
|
|
|
// result.
|
|
|
|
if (use != (*res)->use_end())
|
|
|
|
++use;
|
|
|
|
if (use == (*res)->use_end()) {
|
|
|
|
++res;
|
|
|
|
skipOverResultsWithNoUsers();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UseIterator::operator==(const UseIterator &other) const {
|
|
|
|
if (op != other.op)
|
|
|
|
return false;
|
|
|
|
if (op->getNumResults() == 0)
|
|
|
|
return true;
|
|
|
|
return res == other.res && use == other.use;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UseIterator::operator!=(const UseIterator &other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UseIterator::skipOverResultsWithNoUsers() {
|
|
|
|
while (res != op->result_end() && (*res)->use_empty())
|
|
|
|
++res;
|
|
|
|
|
|
|
|
// If we are at the last result, then set use to first use of
|
|
|
|
// first result (sentinel value used for end).
|
|
|
|
if (res == op->result_end())
|
|
|
|
use = {};
|
|
|
|
else
|
|
|
|
use = (*res)->use_begin();
|
|
|
|
}
|