Files
clang-p2996/mlir/lib/IR/BuiltinAttributeInterfaces.cpp
Jeff Niu e179532284 [mlir] Remove types from attributes
This patch removes the `type` field from `Attribute` along with the
`Attribute::getType` accessor.

Going forward, this means that attributes in MLIR will no longer have
types as a first-class concept. This patch lays the groundwork to
incrementally remove or refactor code that relies on generic attributes
being typed. The immediate impact will be on attributes that rely on
`Attribute` containing a type, such as `IntegerAttr`,
`DenseElementsAttr`, and `ml_program::ExternAttr`, which will now need
to define a type parameter on their storage classes. This will save
memory as all other attribute kinds will no longer contain a type.

Moreover, it will not be possible to generically query the type of an
attribute directly. This patch provides an attribute interface
`TypedAttr` that implements only one method, `getType`, which can be
used to generically query the types of attributes that implement the
interface. This interface can be used to retain the concept of a "typed
attribute". The ODS-generated accessor for a `type` parameter
automatically implements this method.

Next steps will be to refactor the assembly formats of certain operations
that rely on `parseAttribute(type)` and `printAttributeWithoutType` to
remove special handling of type elision until `type` can be removed from
the dialect parsing hook entirely; and incrementally remove uses of
`TypedAttr`.

Reviewed By: lattner, rriddle, jpienaar

Differential Revision: https://reviews.llvm.org/D130092
2022-07-31 20:01:31 -04:00

86 lines
3.1 KiB
C++

//===- BuiltinAttributeInterfaces.cpp -------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/IR/BuiltinAttributeInterfaces.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Diagnostics.h"
#include "llvm/ADT/Sequence.h"
using namespace mlir;
using namespace mlir::detail;
//===----------------------------------------------------------------------===//
/// Tablegen Interface Definitions
//===----------------------------------------------------------------------===//
#include "mlir/IR/BuiltinAttributeInterfaces.cpp.inc"
//===----------------------------------------------------------------------===//
// ElementsAttr
//===----------------------------------------------------------------------===//
Type ElementsAttr::getElementType(ElementsAttr elementsAttr) {
return elementsAttr.getType().getElementType();
}
int64_t ElementsAttr::getNumElements(ElementsAttr elementsAttr) {
return elementsAttr.getType().getNumElements();
}
bool ElementsAttr::isValidIndex(ShapedType type, ArrayRef<uint64_t> index) {
// Verify that the rank of the indices matches the held type.
int64_t rank = type.getRank();
if (rank == 0 && index.size() == 1 && index[0] == 0)
return true;
if (rank != static_cast<int64_t>(index.size()))
return false;
// Verify that all of the indices are within the shape dimensions.
ArrayRef<int64_t> shape = type.getShape();
return llvm::all_of(llvm::seq<int>(0, rank), [&](int i) {
int64_t dim = static_cast<int64_t>(index[i]);
return 0 <= dim && dim < shape[i];
});
}
bool ElementsAttr::isValidIndex(ElementsAttr elementsAttr,
ArrayRef<uint64_t> index) {
return isValidIndex(elementsAttr.getType(), index);
}
uint64_t ElementsAttr::getFlattenedIndex(Type type, ArrayRef<uint64_t> index) {
ShapedType shapeType = type.cast<ShapedType>();
assert(isValidIndex(shapeType, index) &&
"expected valid multi-dimensional index");
// Reduce the provided multidimensional index into a flattended 1D row-major
// index.
auto rank = shapeType.getRank();
ArrayRef<int64_t> shape = shapeType.getShape();
uint64_t valueIndex = 0;
uint64_t dimMultiplier = 1;
for (int i = rank - 1; i >= 0; --i) {
valueIndex += index[i] * dimMultiplier;
dimMultiplier *= shape[i];
}
return valueIndex;
}
//===----------------------------------------------------------------------===//
// MemRefLayoutAttrInterface
//===----------------------------------------------------------------------===//
LogicalResult mlir::detail::verifyAffineMapAsLayout(
AffineMap m, ArrayRef<int64_t> shape,
function_ref<InFlightDiagnostic()> emitError) {
if (m.getNumDims() != shape.size())
return emitError() << "memref layout mismatch between rank and affine map: "
<< shape.size() << " != " << m.getNumDims();
return success();
}