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.
|
|
|
|
// =============================================================================
|
|
|
|
|
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"
|
2019-02-04 01:49:39 +08:00
|
|
|
#include "mlir/IR/Instruction.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-01-04 06:29:52 +08:00
|
|
|
#include "mlir/IR/StandardTypes.h"
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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() {}
|
|
|
|
|
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.
|
2018-10-22 10:49:31 +08:00
|
|
|
bool OpState::parse(OpAsmParser *parser, OperationState *result) {
|
2019-01-24 03:26:56 +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.
|
2018-10-22 10:49:31 +08:00
|
|
|
void OpState::print(OpAsmPrinter *p) const {
|
2019-01-24 03:26:56 +08:00
|
|
|
p->printGenericOp(getInstruction());
|
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
|
|
|
|
/// any diagnostic handlers that may be listening. NOTE: This may terminate
|
|
|
|
/// the containing application, only use when the IR is in an inconsistent
|
|
|
|
/// state.
|
2018-12-08 01:30:25 +08:00
|
|
|
bool OpState::emitError(const Twine &message) const {
|
2018-12-28 20:14:52 +08:00
|
|
|
return getInstruction()->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.
|
2018-09-27 06:06:38 +08:00
|
|
|
bool OpState::emitOpError(const Twine &message) const {
|
2018-12-28 20:14:52 +08:00
|
|
|
return getInstruction()->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.
|
2018-09-27 06:06:38 +08:00
|
|
|
void OpState::emitWarning(const Twine &message) const {
|
2018-12-28 20:14:52 +08:00
|
|
|
getInstruction()->emitWarning(message);
|
2018-09-10 11:40:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit a note about this operation, reporting up to any diagnostic
|
|
|
|
/// handlers that may be listening.
|
2018-09-27 06:06:38 +08:00
|
|
|
void OpState::emitNote(const Twine &message) const {
|
2018-12-28 20:14:52 +08:00
|
|
|
getInstruction()->emitNote(message);
|
2018-09-10 11:40:23 +08:00
|
|
|
}
|
2018-09-27 01:07:16 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Op Trait implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifyZeroOperands(const Instruction *op) {
|
2018-09-27 12:18:42 +08:00
|
|
|
if (op->getNumOperands() != 0)
|
|
|
|
return op->emitOpError("requires zero operands");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifyOneOperand(const Instruction *op) {
|
2018-09-27 12:18:42 +08:00
|
|
|
if (op->getNumOperands() != 1)
|
|
|
|
return op->emitOpError("requires a single operand");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifyNOperands(const Instruction *op,
|
2018-12-28 13:21:41 +08:00
|
|
|
unsigned numOperands) {
|
2018-10-10 06:04:27 +08:00
|
|
|
if (op->getNumOperands() != numOperands) {
|
|
|
|
return op->emitOpError("expected " + Twine(numOperands) +
|
|
|
|
" operands, but found " +
|
|
|
|
Twine(op->getNumOperands()));
|
|
|
|
}
|
2018-09-27 12:18:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifyAtLeastNOperands(const Instruction *op,
|
2018-09-27 12:18:42 +08:00
|
|
|
unsigned numOperands) {
|
|
|
|
if (op->getNumOperands() < numOperands)
|
|
|
|
return op->emitOpError("expected " + Twine(numOperands) +
|
|
|
|
" or more operands");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifyOperandsAreIntegerLike(const Instruction *op) {
|
2018-11-07 07:37:39 +08:00
|
|
|
for (auto *operand : op->getOperands()) {
|
Enable arithmetics for index types.
Arithmetic and comparison instructions are necessary to implement, e.g.,
control flow when lowering MLFunctions to CFGFunctions. (While it is possible
to replace some of the arithmetics by affine_apply instructions for loop
bounds, it is still necessary for loop bounds checking, steps, if-conditions,
non-trivial memref subscripts, etc.) Furthermore, working with indirect
accesses in, e.g., lookup tables for large embeddings, may require operating on
tensors of indexes. For example, the equivalents to C code "LUT[Index[i]]" or
"ResultIndex[i] = i + j" where i, j are loop induction variables require the
arithmetics on indices as well as the possibility to operate on tensors
thereof. Allow arithmetic and comparison operations to apply to index types by
declaring them integer-like. Allow tensors whose element type is index for
indirection purposes.
The absence of vectors with "index" element type is explicitly tested, but the
only justification for this restriction in the CL introducing the test is
"because we don't need them". Do NOT enable vectors of index types, although
it makes vector and tensor types inconsistent with respect to allowed element
types.
PiperOrigin-RevId: 220614055
2018-11-08 20:04:32 +08:00
|
|
|
auto type = getTensorOrVectorElementType(operand->getType());
|
2018-12-05 20:31:59 +08:00
|
|
|
if (!type.isIntOrIndex())
|
Enable arithmetics for index types.
Arithmetic and comparison instructions are necessary to implement, e.g.,
control flow when lowering MLFunctions to CFGFunctions. (While it is possible
to replace some of the arithmetics by affine_apply instructions for loop
bounds, it is still necessary for loop bounds checking, steps, if-conditions,
non-trivial memref subscripts, etc.) Furthermore, working with indirect
accesses in, e.g., lookup tables for large embeddings, may require operating on
tensors of indexes. For example, the equivalents to C code "LUT[Index[i]]" or
"ResultIndex[i] = i + j" where i, j are loop induction variables require the
arithmetics on indices as well as the possibility to operate on tensors
thereof. Allow arithmetic and comparison operations to apply to index types by
declaring them integer-like. Allow tensors whose element type is index for
indirection purposes.
The absence of vectors with "index" element type is explicitly tested, but the
only justification for this restriction in the CL introducing the test is
"because we don't need them". Do NOT enable vectors of index types, although
it makes vector and tensor types inconsistent with respect to allowed element
types.
PiperOrigin-RevId: 220614055
2018-11-08 20:04:32 +08:00
|
|
|
return op->emitOpError("requires an integer or index type");
|
2018-11-07 07:37:39 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifySameTypeOperands(const Instruction *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)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto type = op->getOperand(0)->getType();
|
|
|
|
for (unsigned i = 1; i < nOperands; ++i) {
|
|
|
|
if (op->getOperand(i)->getType() != type)
|
|
|
|
return op->emitOpError("requires all operands to have the same type");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifyZeroResult(const Instruction *op) {
|
2018-09-27 12:18:42 +08:00
|
|
|
if (op->getNumResults() != 0)
|
|
|
|
return op->emitOpError("requires zero results");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifyOneResult(const Instruction *op) {
|
2018-09-27 12:18:42 +08:00
|
|
|
if (op->getNumResults() != 1)
|
|
|
|
return op->emitOpError("requires one result");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifyNResults(const Instruction *op,
|
2018-12-28 13:21:41 +08:00
|
|
|
unsigned numOperands) {
|
2018-09-27 12:18:42 +08:00
|
|
|
if (op->getNumResults() != numOperands)
|
|
|
|
return op->emitOpError("expected " + Twine(numOperands) + " results");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifyAtLeastNResults(const Instruction *op,
|
2018-09-27 12:18:42 +08:00
|
|
|
unsigned numOperands) {
|
|
|
|
if (op->getNumResults() < numOperands)
|
|
|
|
return op->emitOpError("expected " + Twine(numOperands) +
|
|
|
|
" or more results");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-29 03:49:26 +08:00
|
|
|
/// Returns false if the given two types have the same shape. That is,
|
|
|
|
/// they are both scalars, or they are both vectors / ranked tensors with
|
|
|
|
/// the same dimension specifications. The element type does not matter.
|
|
|
|
static bool verifyShapeMatch(Type type1, Type type2) {
|
|
|
|
// Check scalar cases
|
2018-12-05 20:31:59 +08:00
|
|
|
if (type1.isIntOrIndexOrFloat())
|
|
|
|
return !type2.isIntOrIndexOrFloat();
|
2018-11-29 03:49:26 +08:00
|
|
|
|
|
|
|
// Check unranked tensor cases
|
|
|
|
if (type1.isa<UnrankedTensorType>() || type2.isa<UnrankedTensorType>())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check normal vector/tensor cases
|
|
|
|
if (auto vtType1 = type1.dyn_cast<VectorOrTensorType>()) {
|
|
|
|
auto vtType2 = type2.dyn_cast<VectorOrTensorType>();
|
|
|
|
return !(vtType2 && vtType1.getShape() == vtType2.getShape());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifySameOperandsAndResultShape(const Instruction *op) {
|
2018-11-29 03:49:26 +08:00
|
|
|
if (op->getNumOperands() == 0 || op->getNumResults() == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto type = op->getOperand(0)->getType();
|
|
|
|
for (unsigned i = 0, e = op->getNumResults(); i < e; ++i) {
|
|
|
|
if (verifyShapeMatch(op->getResult(i)->getType(), type))
|
|
|
|
return op->emitOpError(
|
|
|
|
"requires the same shape for all operands and results");
|
|
|
|
}
|
|
|
|
for (unsigned i = 1, e = op->getNumOperands(); i < e; ++i) {
|
|
|
|
if (verifyShapeMatch(op->getOperand(i)->getType(), type))
|
|
|
|
return op->emitOpError(
|
|
|
|
"requires the same shape for all operands and results");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifySameOperandsAndResultType(const Instruction *op) {
|
2018-11-29 03:49:26 +08:00
|
|
|
if (op->getNumOperands() == 0 || op->getNumResults() == 0)
|
|
|
|
return true;
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
auto type = op->getResult(0)->getType();
|
2018-09-27 01:07:16 +08:00
|
|
|
for (unsigned i = 1, e = op->getNumResults(); i < e; ++i) {
|
|
|
|
if (op->getResult(i)->getType() != type)
|
|
|
|
return op->emitOpError(
|
|
|
|
"requires the same type for all operands and results");
|
|
|
|
}
|
|
|
|
for (unsigned i = 0, e = op->getNumOperands(); i < e; ++i) {
|
|
|
|
if (op->getOperand(i)->getType() != type)
|
|
|
|
return op->emitOpError(
|
|
|
|
"requires the same type for all operands and results");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-16 01:56:06 +08:00
|
|
|
static bool verifyBBArguments(
|
2019-02-04 02:03:46 +08:00
|
|
|
llvm::iterator_range<Instruction::const_operand_iterator> operands,
|
|
|
|
const Block *destBB, const Instruction *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())
|
|
|
|
return op->emitError("branch has " + Twine(operandCount) +
|
|
|
|
" operands, but target block has " +
|
|
|
|
Twine(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())
|
|
|
|
return op->emitError("type mismatch in bb argument #" + Twine(i));
|
2018-11-16 01:56:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
static bool verifyTerminatorSuccessors(const Instruction *op) {
|
2018-11-16 01:56:06 +08:00
|
|
|
// Verify that the operands lines up with the BB arguments in the successor.
|
2018-12-28 13:21:41 +08:00
|
|
|
const Function *fn = op->getFunction();
|
2018-11-16 01:56:06 +08:00
|
|
|
for (unsigned i = 0, e = op->getNumSuccessors(); i != e; ++i) {
|
|
|
|
auto *succ = op->getSuccessor(i);
|
2018-12-08 01:30:25 +08:00
|
|
|
if (succ->getFunction() != fn)
|
|
|
|
return op->emitError("reference to block defined in another function");
|
2018-11-16 01:56:06 +08:00
|
|
|
if (verifyBBArguments(op->getSuccessorOperands(i), succ, op))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifyIsTerminator(const Instruction *op) {
|
2018-12-30 01:11:58 +08:00
|
|
|
const 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)
|
|
|
|
return op->emitOpError("must be the last instruction in the parent block");
|
|
|
|
|
2018-11-16 01:56:06 +08:00
|
|
|
// Verify the state of the successor blocks.
|
|
|
|
if (op->getNumSuccessors() != 0 && verifyTerminatorSuccessors(op))
|
|
|
|
return true;
|
2018-11-14 01:49:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifyResultsAreBoolLike(const Instruction *op) {
|
2018-11-29 03:49:26 +08:00
|
|
|
for (auto *result : op->getResults()) {
|
|
|
|
auto elementType = getTensorOrVectorElementType(result->getType());
|
2019-01-04 06:29:52 +08:00
|
|
|
bool isBoolType = elementType.isInteger(1);
|
2018-11-29 03:49:26 +08:00
|
|
|
if (!isBoolType)
|
|
|
|
return op->emitOpError("requires a bool result type");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifyResultsAreFloatLike(const Instruction *op) {
|
2018-09-27 01:07:16 +08:00
|
|
|
for (auto *result : op->getResults()) {
|
2018-10-31 05:59:22 +08:00
|
|
|
if (!getTensorOrVectorElementType(result->getType()).isa<FloatType>())
|
2018-09-27 01:07:16 +08:00
|
|
|
return op->emitOpError("requires a floating point type");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
bool OpTrait::impl::verifyResultsAreIntegerLike(const Instruction *op) {
|
2018-09-27 01:07:16 +08:00
|
|
|
for (auto *result : op->getResults()) {
|
Enable arithmetics for index types.
Arithmetic and comparison instructions are necessary to implement, e.g.,
control flow when lowering MLFunctions to CFGFunctions. (While it is possible
to replace some of the arithmetics by affine_apply instructions for loop
bounds, it is still necessary for loop bounds checking, steps, if-conditions,
non-trivial memref subscripts, etc.) Furthermore, working with indirect
accesses in, e.g., lookup tables for large embeddings, may require operating on
tensors of indexes. For example, the equivalents to C code "LUT[Index[i]]" or
"ResultIndex[i] = i + j" where i, j are loop induction variables require the
arithmetics on indices as well as the possibility to operate on tensors
thereof. Allow arithmetic and comparison operations to apply to index types by
declaring them integer-like. Allow tensors whose element type is index for
indirection purposes.
The absence of vectors with "index" element type is explicitly tested, but the
only justification for this restriction in the CL introducing the test is
"because we don't need them". Do NOT enable vectors of index types, although
it makes vector and tensor types inconsistent with respect to allowed element
types.
PiperOrigin-RevId: 220614055
2018-11-08 20:04:32 +08:00
|
|
|
auto type = getTensorOrVectorElementType(result->getType());
|
2018-12-05 20:31:59 +08:00
|
|
|
if (!type.isIntOrIndex())
|
Enable arithmetics for index types.
Arithmetic and comparison instructions are necessary to implement, e.g.,
control flow when lowering MLFunctions to CFGFunctions. (While it is possible
to replace some of the arithmetics by affine_apply instructions for loop
bounds, it is still necessary for loop bounds checking, steps, if-conditions,
non-trivial memref subscripts, etc.) Furthermore, working with indirect
accesses in, e.g., lookup tables for large embeddings, may require operating on
tensors of indexes. For example, the equivalents to C code "LUT[Index[i]]" or
"ResultIndex[i] = i + j" where i, j are loop induction variables require the
arithmetics on indices as well as the possibility to operate on tensors
thereof. Allow arithmetic and comparison operations to apply to index types by
declaring them integer-like. Allow tensors whose element type is index for
indirection purposes.
The absence of vectors with "index" element type is explicitly tested, but the
only justification for this restriction in the CL introducing the test is
"because we don't need them". Do NOT enable vectors of index types, although
it makes vector and tensor types inconsistent with respect to allowed element
types.
PiperOrigin-RevId: 220614055
2018-11-08 20:04:32 +08:00
|
|
|
return op->emitOpError("requires an integer or index type");
|
2018-09-27 01:07:16 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BinaryOp implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// These functions are out-of-line implementations of the methods in BinaryOp,
|
|
|
|
// which avoids them being template instantiated/duplicated.
|
|
|
|
|
2018-12-28 06:35:10 +08:00
|
|
|
void impl::buildBinaryOp(Builder *builder, OperationState *result, Value *lhs,
|
|
|
|
Value *rhs) {
|
2018-09-27 01:07:16 +08:00
|
|
|
assert(lhs->getType() == rhs->getType());
|
|
|
|
result->addOperands({lhs, rhs});
|
|
|
|
result->types.push_back(lhs->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool impl::parseBinaryOp(OpAsmParser *parser, OperationState *result) {
|
|
|
|
SmallVector<OpAsmParser::OperandType, 2> ops;
|
2018-10-31 05:59:22 +08:00
|
|
|
Type type;
|
2018-09-27 01:07:16 +08:00
|
|
|
return parser->parseOperandList(ops, 2) ||
|
|
|
|
parser->parseOptionalAttributeDict(result->attributes) ||
|
|
|
|
parser->parseColonType(type) ||
|
|
|
|
parser->resolveOperands(ops, type, result->operands) ||
|
|
|
|
parser->addTypeToList(type, result->types);
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
void impl::printBinaryOp(const Instruction *op, OpAsmPrinter *p) {
|
2019-01-17 04:49:11 +08:00
|
|
|
assert(op->getNumOperands() == 2 && "binary op should have two operands");
|
|
|
|
assert(op->getNumResults() == 1 && "binary op should have one result");
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
if (op->getOperand(0)->getType() != resultType ||
|
|
|
|
op->getOperand(1)->getType() != resultType) {
|
2019-01-24 03:26:56 +08:00
|
|
|
p->printGenericOp(op);
|
2019-01-17 04:49:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-23 00:00:03 +08:00
|
|
|
*p << op->getName() << ' ' << *op->getOperand(0) << ", "
|
2018-09-27 01:07:16 +08:00
|
|
|
<< *op->getOperand(1);
|
|
|
|
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.
|
2018-10-31 05:59:22 +08:00
|
|
|
*p << " : " << op->getResult(0)->getType();
|
2018-09-27 01:07:16 +08:00
|
|
|
}
|
2018-10-23 00:00:03 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// CastOp implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-12-28 06:35:10 +08:00
|
|
|
void impl::buildCastOp(Builder *builder, OperationState *result, Value *source,
|
|
|
|
Type destType) {
|
2018-10-23 00:00:03 +08:00
|
|
|
result->addOperands(source);
|
|
|
|
result->addTypes(destType);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool impl::parseCastOp(OpAsmParser *parser, OperationState *result) {
|
|
|
|
OpAsmParser::OperandType srcInfo;
|
2018-10-31 05:59:22 +08:00
|
|
|
Type srcType, dstType;
|
2018-10-23 00:00:03 +08:00
|
|
|
return parser->parseOperand(srcInfo) || parser->parseColonType(srcType) ||
|
|
|
|
parser->resolveOperand(srcInfo, srcType, result->operands) ||
|
|
|
|
parser->parseKeywordType("to", dstType) ||
|
|
|
|
parser->addTypeToList(dstType, result->types);
|
|
|
|
}
|
|
|
|
|
2019-02-04 02:03:46 +08:00
|
|
|
void impl::printCastOp(const Instruction *op, OpAsmPrinter *p) {
|
2018-10-23 00:00:03 +08:00
|
|
|
*p << op->getName() << ' ' << *op->getOperand(0) << " : "
|
2018-10-31 05:59:22 +08:00
|
|
|
<< op->getOperand(0)->getType() << " to " << op->getResult(0)->getType();
|
2018-10-23 00:00:03 +08:00
|
|
|
}
|