2020-12-03 17:22:57 -08:00
|
|
|
//===- BuiltinAttributes.cpp - MLIR Builtin Attribute Classes -------------===//
|
|
|
|
//
|
|
|
|
// 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/BuiltinAttributes.h"
|
|
|
|
#include "AttributeDetail.h"
|
|
|
|
#include "mlir/IR/AffineMap.h"
|
2021-03-11 11:24:43 -08:00
|
|
|
#include "mlir/IR/BuiltinDialect.h"
|
2020-12-03 17:22:57 -08:00
|
|
|
#include "mlir/IR/Dialect.h"
|
[mlir] Add a new builtin DenseResourceElementsAttr
This attributes is intended cover the current set of use cases that abuse
DenseElementsAttr, e.g. when the data is large. Using resources for large
data is one of the major reasons why they were added; e.g. they can be
deallocated mid-compilation, they support a wide variety of data origins
(e.g, heap allocated, mmap'd, etc.), they can support mutation, etc.
I considered at length not having a builtin variant of this, and instead
having multiple versions of this attribute for dialects that are interested,
but they all boiled down to the exact same attribute definition. Given the
generality of this attribute, it feels more aligned to keep it next to DenseArrayAttr
(given that DenseArrayAttr covers the "small" case, and DenseResourcesElementsAttr
covers the "large" case). The underlying infra used to build this attribute is
general, and having a builtin attribute doesn't preclude users from defining
their own when it makes sense (they can even share a blob manager with the
builtin dialect to avoid data duplication).
Differential Revision: https://reviews.llvm.org/D130022
2022-07-19 18:22:55 -07:00
|
|
|
#include "mlir/IR/DialectResourceBlobManager.h"
|
2020-12-03 17:22:57 -08:00
|
|
|
#include "mlir/IR/IntegerSet.h"
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
#include "mlir/IR/OpImplementation.h"
|
2021-08-30 09:31:48 -07:00
|
|
|
#include "mlir/IR/Operation.h"
|
|
|
|
#include "mlir/IR/SymbolTable.h"
|
2020-12-03 17:22:57 -08:00
|
|
|
#include "mlir/IR/Types.h"
|
2021-06-01 19:18:19 +02:00
|
|
|
#include "llvm/ADT/APSInt.h"
|
2020-12-03 17:22:57 -08:00
|
|
|
#include "llvm/ADT/Sequence.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::detail;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2021-03-04 12:37:32 -08:00
|
|
|
/// Tablegen Attribute Definitions
|
2020-12-03 17:22:57 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-03-04 12:37:32 -08:00
|
|
|
#define GET_ATTRDEF_CLASSES
|
|
|
|
#include "mlir/IR/BuiltinAttributes.cpp.inc"
|
2020-12-03 17:22:57 -08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2021-03-11 11:24:43 -08:00
|
|
|
// BuiltinDialect
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void BuiltinDialect::registerAttributes() {
|
[mlir] Add a new builtin DenseResourceElementsAttr
This attributes is intended cover the current set of use cases that abuse
DenseElementsAttr, e.g. when the data is large. Using resources for large
data is one of the major reasons why they were added; e.g. they can be
deallocated mid-compilation, they support a wide variety of data origins
(e.g, heap allocated, mmap'd, etc.), they can support mutation, etc.
I considered at length not having a builtin variant of this, and instead
having multiple versions of this attribute for dialects that are interested,
but they all boiled down to the exact same attribute definition. Given the
generality of this attribute, it feels more aligned to keep it next to DenseArrayAttr
(given that DenseArrayAttr covers the "small" case, and DenseResourcesElementsAttr
covers the "large" case). The underlying infra used to build this attribute is
general, and having a builtin attribute doesn't preclude users from defining
their own when it makes sense (they can even share a blob manager with the
builtin dialect to avoid data duplication).
Differential Revision: https://reviews.llvm.org/D130022
2022-07-19 18:22:55 -07:00
|
|
|
addAttributes<
|
|
|
|
#define GET_ATTRDEF_LIST
|
|
|
|
#include "mlir/IR/BuiltinAttributes.cpp.inc"
|
|
|
|
>();
|
2021-03-11 11:24:43 -08:00
|
|
|
}
|
|
|
|
|
2021-06-10 17:22:49 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ArrayAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ArrayAttr::walkImmediateSubElements(
|
|
|
|
function_ref<void(Attribute)> walkAttrsFn,
|
|
|
|
function_ref<void(Type)> walkTypesFn) const {
|
|
|
|
for (Attribute attr : getValue())
|
|
|
|
walkAttrsFn(attr);
|
|
|
|
}
|
|
|
|
|
2022-07-26 13:22:19 -07:00
|
|
|
Attribute
|
|
|
|
ArrayAttr::replaceImmediateSubElements(ArrayRef<Attribute> replAttrs,
|
|
|
|
ArrayRef<Type> replTypes) const {
|
|
|
|
return get(getContext(), replAttrs);
|
2021-10-28 19:08:10 +02:00
|
|
|
}
|
|
|
|
|
2021-03-11 11:24:43 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-12-03 17:22:57 -08:00
|
|
|
// DictionaryAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Helper function that does either an in place sort or sorts from source array
|
|
|
|
/// into destination. If inPlace then storage is both the source and the
|
|
|
|
/// destination, else value is the source and storage destination. Returns
|
|
|
|
/// whether source was sorted.
|
|
|
|
template <bool inPlace>
|
|
|
|
static bool dictionaryAttrSort(ArrayRef<NamedAttribute> value,
|
|
|
|
SmallVectorImpl<NamedAttribute> &storage) {
|
|
|
|
// Specialize for the common case.
|
|
|
|
switch (value.size()) {
|
|
|
|
case 0:
|
|
|
|
// Zero already sorted.
|
2021-10-19 00:48:24 +00:00
|
|
|
if (!inPlace)
|
|
|
|
storage.clear();
|
2020-12-03 17:22:57 -08:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// One already sorted but may need to be copied.
|
|
|
|
if (!inPlace)
|
|
|
|
storage.assign({value[0]});
|
|
|
|
break;
|
|
|
|
case 2: {
|
|
|
|
bool isSorted = value[0] < value[1];
|
|
|
|
if (inPlace) {
|
|
|
|
if (!isSorted)
|
|
|
|
std::swap(storage[0], storage[1]);
|
|
|
|
} else if (isSorted) {
|
|
|
|
storage.assign({value[0], value[1]});
|
|
|
|
} else {
|
|
|
|
storage.assign({value[1], value[0]});
|
|
|
|
}
|
|
|
|
return !isSorted;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if (!inPlace)
|
|
|
|
storage.assign(value.begin(), value.end());
|
|
|
|
// Check to see they are sorted already.
|
|
|
|
bool isSorted = llvm::is_sorted(value);
|
2021-04-06 15:43:49 +02:00
|
|
|
// If not, do a general sort.
|
|
|
|
if (!isSorted)
|
2020-12-03 17:22:57 -08:00
|
|
|
llvm::array_pod_sort(storage.begin(), storage.end());
|
|
|
|
return !isSorted;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an entry with a duplicate name from the given sorted array of named
|
|
|
|
/// attributes. Returns llvm::None if all elements have unique names.
|
|
|
|
static Optional<NamedAttribute>
|
|
|
|
findDuplicateElement(ArrayRef<NamedAttribute> value) {
|
|
|
|
const Optional<NamedAttribute> none{llvm::None};
|
|
|
|
if (value.size() < 2)
|
|
|
|
return none;
|
|
|
|
|
|
|
|
if (value.size() == 2)
|
2021-11-18 05:23:32 +00:00
|
|
|
return value[0].getName() == value[1].getName() ? value[0] : none;
|
2020-12-03 17:22:57 -08:00
|
|
|
|
2021-12-20 19:45:05 +00:00
|
|
|
const auto *it = std::adjacent_find(value.begin(), value.end(),
|
|
|
|
[](NamedAttribute l, NamedAttribute r) {
|
|
|
|
return l.getName() == r.getName();
|
|
|
|
});
|
2020-12-03 17:22:57 -08:00
|
|
|
return it != value.end() ? *it : none;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DictionaryAttr::sort(ArrayRef<NamedAttribute> value,
|
|
|
|
SmallVectorImpl<NamedAttribute> &storage) {
|
|
|
|
bool isSorted = dictionaryAttrSort</*inPlace=*/false>(value, storage);
|
|
|
|
assert(!findDuplicateElement(storage) &&
|
|
|
|
"DictionaryAttr element names must be unique");
|
|
|
|
return isSorted;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DictionaryAttr::sortInPlace(SmallVectorImpl<NamedAttribute> &array) {
|
|
|
|
bool isSorted = dictionaryAttrSort</*inPlace=*/true>(array, array);
|
|
|
|
assert(!findDuplicateElement(array) &&
|
|
|
|
"DictionaryAttr element names must be unique");
|
|
|
|
return isSorted;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<NamedAttribute>
|
|
|
|
DictionaryAttr::findDuplicate(SmallVectorImpl<NamedAttribute> &array,
|
|
|
|
bool isSorted) {
|
|
|
|
if (!isSorted)
|
|
|
|
dictionaryAttrSort</*inPlace=*/true>(array, array);
|
|
|
|
return findDuplicateElement(array);
|
|
|
|
}
|
|
|
|
|
2021-02-08 09:44:03 +01:00
|
|
|
DictionaryAttr DictionaryAttr::get(MLIRContext *context,
|
|
|
|
ArrayRef<NamedAttribute> value) {
|
2020-12-03 17:22:57 -08:00
|
|
|
if (value.empty())
|
|
|
|
return DictionaryAttr::getEmpty(context);
|
|
|
|
|
|
|
|
// We need to sort the element list to canonicalize it.
|
|
|
|
SmallVector<NamedAttribute, 8> storage;
|
|
|
|
if (dictionaryAttrSort</*inPlace=*/false>(value, storage))
|
|
|
|
value = storage;
|
|
|
|
assert(!findDuplicateElement(value) &&
|
|
|
|
"DictionaryAttr element names must be unique");
|
|
|
|
return Base::get(context, value);
|
|
|
|
}
|
|
|
|
/// Construct a dictionary with an array of values that is known to already be
|
|
|
|
/// sorted by name and uniqued.
|
2021-03-04 12:37:32 -08:00
|
|
|
DictionaryAttr DictionaryAttr::getWithSorted(MLIRContext *context,
|
|
|
|
ArrayRef<NamedAttribute> value) {
|
2020-12-03 17:22:57 -08:00
|
|
|
if (value.empty())
|
|
|
|
return DictionaryAttr::getEmpty(context);
|
|
|
|
// Ensure that the attribute elements are unique and sorted.
|
2021-11-18 05:23:32 +00:00
|
|
|
assert(llvm::is_sorted(
|
|
|
|
value, [](NamedAttribute l, NamedAttribute r) { return l < r; }) &&
|
2020-12-03 17:22:57 -08:00
|
|
|
"expected attribute values to be sorted");
|
|
|
|
assert(!findDuplicateElement(value) &&
|
|
|
|
"DictionaryAttr element names must be unique");
|
|
|
|
return Base::get(context, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the specified attribute if present, null otherwise.
|
|
|
|
Attribute DictionaryAttr::get(StringRef name) const {
|
2021-11-04 20:34:01 +00:00
|
|
|
auto it = impl::findAttrSorted(begin(), end(), name);
|
2021-11-18 05:23:32 +00:00
|
|
|
return it.second ? it.first->getValue() : Attribute();
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
2021-11-16 17:21:15 +00:00
|
|
|
Attribute DictionaryAttr::get(StringAttr name) const {
|
2021-11-04 20:34:01 +00:00
|
|
|
auto it = impl::findAttrSorted(begin(), end(), name);
|
2021-11-18 05:23:32 +00:00
|
|
|
return it.second ? it.first->getValue() : Attribute();
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the specified named attribute if present, None otherwise.
|
|
|
|
Optional<NamedAttribute> DictionaryAttr::getNamed(StringRef name) const {
|
2021-11-04 20:34:01 +00:00
|
|
|
auto it = impl::findAttrSorted(begin(), end(), name);
|
|
|
|
return it.second ? *it.first : Optional<NamedAttribute>();
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
2021-11-16 17:21:15 +00:00
|
|
|
Optional<NamedAttribute> DictionaryAttr::getNamed(StringAttr name) const {
|
2021-11-04 20:34:01 +00:00
|
|
|
auto it = impl::findAttrSorted(begin(), end(), name);
|
|
|
|
return it.second ? *it.first : Optional<NamedAttribute>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return whether the specified attribute is present.
|
|
|
|
bool DictionaryAttr::contains(StringRef name) const {
|
|
|
|
return impl::findAttrSorted(begin(), end(), name).second;
|
|
|
|
}
|
2021-11-16 17:21:15 +00:00
|
|
|
bool DictionaryAttr::contains(StringAttr name) const {
|
2021-11-04 20:34:01 +00:00
|
|
|
return impl::findAttrSorted(begin(), end(), name).second;
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
DictionaryAttr::iterator DictionaryAttr::begin() const {
|
|
|
|
return getValue().begin();
|
|
|
|
}
|
|
|
|
DictionaryAttr::iterator DictionaryAttr::end() const {
|
|
|
|
return getValue().end();
|
|
|
|
}
|
|
|
|
size_t DictionaryAttr::size() const { return getValue().size(); }
|
|
|
|
|
2021-03-04 12:37:32 -08:00
|
|
|
DictionaryAttr DictionaryAttr::getEmptyUnchecked(MLIRContext *context) {
|
|
|
|
return Base::get(context, ArrayRef<NamedAttribute>());
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:22:49 -07:00
|
|
|
void DictionaryAttr::walkImmediateSubElements(
|
|
|
|
function_ref<void(Attribute)> walkAttrsFn,
|
|
|
|
function_ref<void(Type)> walkTypesFn) const {
|
2021-11-18 05:23:32 +00:00
|
|
|
for (const NamedAttribute &attr : getValue())
|
|
|
|
walkAttrsFn(attr.getValue());
|
2021-06-10 17:22:49 -07:00
|
|
|
}
|
|
|
|
|
2022-07-26 13:22:19 -07:00
|
|
|
Attribute
|
|
|
|
DictionaryAttr::replaceImmediateSubElements(ArrayRef<Attribute> replAttrs,
|
|
|
|
ArrayRef<Type> replTypes) const {
|
2021-10-28 19:08:10 +02:00
|
|
|
std::vector<NamedAttribute> vec = getValue().vec();
|
2022-07-26 13:22:19 -07:00
|
|
|
for (auto &it : llvm::enumerate(replAttrs))
|
|
|
|
vec[it.index()].setValue(it.value());
|
2021-11-18 05:23:32 +00:00
|
|
|
|
2021-10-28 19:08:10 +02:00
|
|
|
// The above only modifies the mapped value, but not the key, and therefore
|
|
|
|
// not the order of the elements. It remains sorted
|
|
|
|
return getWithSorted(getContext(), vec);
|
|
|
|
}
|
|
|
|
|
2021-06-05 11:38:31 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// StringAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-05-25 14:38:01 -07:00
|
|
|
StringAttr StringAttr::getEmptyStringAttrUnchecked(MLIRContext *context) {
|
|
|
|
return Base::get(context, "", NoneType::get(context));
|
|
|
|
}
|
|
|
|
|
2021-06-05 11:38:31 -07:00
|
|
|
/// Twine support for StringAttr.
|
|
|
|
StringAttr StringAttr::get(MLIRContext *context, const Twine &twine) {
|
|
|
|
// Fast-path empty twine.
|
|
|
|
if (twine.isTriviallyEmpty())
|
|
|
|
return get(context);
|
|
|
|
SmallVector<char, 32> tempStr;
|
|
|
|
return Base::get(context, twine.toStringRef(tempStr), NoneType::get(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Twine support for StringAttr.
|
|
|
|
StringAttr StringAttr::get(const Twine &twine, Type type) {
|
|
|
|
SmallVector<char, 32> tempStr;
|
|
|
|
return Base::get(type.getContext(), twine.toStringRef(tempStr), type);
|
|
|
|
}
|
|
|
|
|
2021-11-11 01:44:58 +00:00
|
|
|
StringRef StringAttr::getValue() const { return getImpl()->value; }
|
|
|
|
|
[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-18 21:32:38 -07:00
|
|
|
Type StringAttr::getType() const { return getImpl()->type; }
|
|
|
|
|
2021-11-11 01:44:58 +00:00
|
|
|
Dialect *StringAttr::getReferencedDialect() const {
|
|
|
|
return getImpl()->referencedDialect;
|
|
|
|
}
|
|
|
|
|
2020-12-03 17:22:57 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FloatAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
double FloatAttr::getValueAsDouble() const {
|
|
|
|
return getValueAsDouble(getValue());
|
|
|
|
}
|
|
|
|
double FloatAttr::getValueAsDouble(APFloat value) {
|
|
|
|
if (&value.getSemantics() != &APFloat::IEEEdouble()) {
|
|
|
|
bool losesInfo = false;
|
|
|
|
value.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
|
|
|
|
&losesInfo);
|
|
|
|
}
|
|
|
|
return value.convertToDouble();
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:30:19 -08:00
|
|
|
LogicalResult FloatAttr::verify(function_ref<InFlightDiagnostic()> emitError,
|
2022-01-03 06:31:44 +00:00
|
|
|
Type type, APFloat value) {
|
2020-12-03 17:22:57 -08:00
|
|
|
// Verify that the type is correct.
|
2021-03-16 16:30:46 -07:00
|
|
|
if (!type.isa<FloatType>())
|
|
|
|
return emitError() << "expected floating point type";
|
2020-12-03 17:22:57 -08:00
|
|
|
|
|
|
|
// Verify that the type semantics match that of the value.
|
|
|
|
if (&type.cast<FloatType>().getFloatSemantics() != &value.getSemantics()) {
|
2021-02-22 17:30:19 -08:00
|
|
|
return emitError()
|
|
|
|
<< "FloatAttr type doesn't match the type implied by its value";
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SymbolRefAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-08-30 09:31:48 -07:00
|
|
|
SymbolRefAttr SymbolRefAttr::get(MLIRContext *ctx, StringRef value,
|
|
|
|
ArrayRef<FlatSymbolRefAttr> nestedRefs) {
|
|
|
|
return get(StringAttr::get(ctx, value), nestedRefs);
|
|
|
|
}
|
|
|
|
|
2021-02-08 09:44:03 +01:00
|
|
|
FlatSymbolRefAttr SymbolRefAttr::get(MLIRContext *ctx, StringRef value) {
|
2021-08-30 09:31:48 -07:00
|
|
|
return get(ctx, value, {}).cast<FlatSymbolRefAttr>();
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
2021-08-29 14:22:24 -07:00
|
|
|
FlatSymbolRefAttr SymbolRefAttr::get(StringAttr value) {
|
|
|
|
return get(value, {}).cast<FlatSymbolRefAttr>();
|
|
|
|
}
|
|
|
|
|
2021-08-30 09:31:48 -07:00
|
|
|
FlatSymbolRefAttr SymbolRefAttr::get(Operation *symbol) {
|
|
|
|
auto symName =
|
|
|
|
symbol->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName());
|
|
|
|
assert(symName && "value does not have a valid symbol name");
|
|
|
|
return SymbolRefAttr::get(symName);
|
|
|
|
}
|
|
|
|
|
2021-08-29 14:22:24 -07:00
|
|
|
StringAttr SymbolRefAttr::getLeafReference() const {
|
2020-12-03 17:22:57 -08:00
|
|
|
ArrayRef<FlatSymbolRefAttr> nestedRefs = getNestedReferences();
|
2021-08-29 14:22:24 -07:00
|
|
|
return nestedRefs.empty() ? getRootReference() : nestedRefs.back().getAttr();
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
2022-07-26 13:22:19 -07:00
|
|
|
void SymbolRefAttr::walkImmediateSubElements(
|
|
|
|
function_ref<void(Attribute)> walkAttrsFn,
|
|
|
|
function_ref<void(Type)> walkTypesFn) const {
|
|
|
|
walkAttrsFn(getRootReference());
|
|
|
|
for (FlatSymbolRefAttr ref : getNestedReferences())
|
|
|
|
walkAttrsFn(ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
Attribute
|
|
|
|
SymbolRefAttr::replaceImmediateSubElements(ArrayRef<Attribute> replAttrs,
|
|
|
|
ArrayRef<Type> replTypes) const {
|
|
|
|
ArrayRef<Attribute> rawNestedRefs = replAttrs.drop_front();
|
|
|
|
ArrayRef<FlatSymbolRefAttr> nestedRefs(
|
|
|
|
static_cast<const FlatSymbolRefAttr *>(rawNestedRefs.data()),
|
|
|
|
rawNestedRefs.size());
|
|
|
|
return get(replAttrs[0].cast<StringAttr>(), nestedRefs);
|
|
|
|
}
|
|
|
|
|
2020-12-03 17:22:57 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IntegerAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
int64_t IntegerAttr::getInt() const {
|
2021-03-16 16:30:46 -07:00
|
|
|
assert((getType().isIndex() || getType().isSignlessInteger()) &&
|
2020-12-03 17:22:57 -08:00
|
|
|
"must be signless integer");
|
|
|
|
return getValue().getSExtValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t IntegerAttr::getSInt() const {
|
2021-03-16 16:30:46 -07:00
|
|
|
assert(getType().isSignedInteger() && "must be signed integer");
|
2020-12-03 17:22:57 -08:00
|
|
|
return getValue().getSExtValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t IntegerAttr::getUInt() const {
|
2021-03-16 16:30:46 -07:00
|
|
|
assert(getType().isUnsignedInteger() && "must be unsigned integer");
|
2020-12-03 17:22:57 -08:00
|
|
|
return getValue().getZExtValue();
|
|
|
|
}
|
|
|
|
|
2021-05-18 10:23:01 -07:00
|
|
|
/// Return the value as an APSInt which carries the signed from the type of
|
|
|
|
/// the attribute. This traps on signless integers types!
|
|
|
|
APSInt IntegerAttr::getAPSInt() const {
|
|
|
|
assert(!getType().isSignlessInteger() &&
|
|
|
|
"Signless integers don't carry a sign for APSInt");
|
|
|
|
return APSInt(getValue(), getType().isUnsignedInteger());
|
|
|
|
}
|
|
|
|
|
2021-02-22 17:30:19 -08:00
|
|
|
LogicalResult IntegerAttr::verify(function_ref<InFlightDiagnostic()> emitError,
|
2021-03-16 16:30:46 -07:00
|
|
|
Type type, APInt value) {
|
|
|
|
if (IntegerType integerType = type.dyn_cast<IntegerType>()) {
|
2020-12-03 17:22:57 -08:00
|
|
|
if (integerType.getWidth() != value.getBitWidth())
|
2021-02-22 17:30:19 -08:00
|
|
|
return emitError() << "integer type bit width (" << integerType.getWidth()
|
|
|
|
<< ") doesn't match value bit width ("
|
|
|
|
<< value.getBitWidth() << ")";
|
2021-03-16 16:30:46 -07:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
if (type.isa<IndexType>())
|
|
|
|
return success();
|
|
|
|
return emitError() << "expected integer or index type";
|
|
|
|
}
|
|
|
|
|
|
|
|
BoolAttr IntegerAttr::getBoolAttrUnchecked(IntegerType type, bool value) {
|
|
|
|
auto attr = Base::get(type.getContext(), type, APInt(/*numBits=*/1, value));
|
|
|
|
return attr.cast<BoolAttr>();
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BoolAttr
|
2021-12-08 01:24:51 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-12-03 17:22:57 -08:00
|
|
|
|
|
|
|
bool BoolAttr::getValue() const {
|
2021-03-16 16:30:46 -07:00
|
|
|
auto *storage = reinterpret_cast<IntegerAttrStorage *>(impl);
|
|
|
|
return storage->value.getBoolValue();
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BoolAttr::classof(Attribute attr) {
|
|
|
|
IntegerAttr intAttr = attr.dyn_cast<IntegerAttr>();
|
|
|
|
return intAttr && intAttr.getType().isSignlessInteger(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// OpaqueAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-02-22 17:30:19 -08:00
|
|
|
LogicalResult OpaqueAttr::verify(function_ref<InFlightDiagnostic()> emitError,
|
2021-11-16 17:21:15 +00:00
|
|
|
StringAttr dialect, StringRef attrData,
|
2021-02-22 17:30:19 -08:00
|
|
|
Type type) {
|
2020-12-03 17:22:57 -08:00
|
|
|
if (!Dialect::isValidNamespace(dialect.strref()))
|
2021-02-22 17:30:19 -08:00
|
|
|
return emitError() << "invalid dialect namespace '" << dialect << "'";
|
2021-04-02 12:45:19 -07:00
|
|
|
|
|
|
|
// Check that the dialect is actually registered.
|
|
|
|
MLIRContext *context = dialect.getContext();
|
|
|
|
if (!context->allowsUnregisteredDialects() &&
|
|
|
|
!context->getLoadedDialect(dialect.strref())) {
|
|
|
|
return emitError()
|
|
|
|
<< "#" << dialect << "<\"" << attrData << "\"> : " << type
|
|
|
|
<< " attribute created with unregistered dialect. If this is "
|
|
|
|
"intended, please call allowUnregisteredDialects() on the "
|
|
|
|
"MLIRContext, or use -allow-unregistered-dialect with "
|
2021-07-15 02:13:30 +00:00
|
|
|
"the MLIR opt tool used";
|
2021-04-02 12:45:19 -07:00
|
|
|
}
|
|
|
|
|
2020-12-03 17:22:57 -08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DenseElementsAttr Utilities
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Get the bitwidth of a dense element type within the buffer.
|
|
|
|
/// DenseElementsAttr requires bitwidths greater than 1 to be aligned by 8.
|
|
|
|
static size_t getDenseElementStorageWidth(size_t origWidth) {
|
|
|
|
return origWidth == 1 ? origWidth : llvm::alignTo<8>(origWidth);
|
|
|
|
}
|
|
|
|
static size_t getDenseElementStorageWidth(Type elementType) {
|
|
|
|
return getDenseElementStorageWidth(getDenseElementBitWidth(elementType));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set a bit to a specific value.
|
|
|
|
static void setBit(char *rawData, size_t bitPos, bool value) {
|
|
|
|
if (value)
|
|
|
|
rawData[bitPos / CHAR_BIT] |= (1 << (bitPos % CHAR_BIT));
|
|
|
|
else
|
|
|
|
rawData[bitPos / CHAR_BIT] &= ~(1 << (bitPos % CHAR_BIT));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the value of the specified bit.
|
|
|
|
static bool getBit(const char *rawData, size_t bitPos) {
|
|
|
|
return (rawData[bitPos / CHAR_BIT] & (1 << (bitPos % CHAR_BIT))) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Copy actual `numBytes` data from `value` (APInt) to char array(`result`) for
|
|
|
|
/// BE format.
|
|
|
|
static void copyAPIntToArrayForBEmachine(APInt value, size_t numBytes,
|
|
|
|
char *result) {
|
|
|
|
assert(llvm::support::endian::system_endianness() == // NOLINT
|
|
|
|
llvm::support::endianness::big); // NOLINT
|
|
|
|
assert(value.getNumWords() * APInt::APINT_WORD_SIZE >= numBytes);
|
|
|
|
|
|
|
|
// Copy the words filled with data.
|
|
|
|
// For example, when `value` has 2 words, the first word is filled with data.
|
|
|
|
// `value` (10 bytes, BE):|abcdefgh|------ij| ==> `result` (BE):|abcdefgh|--|
|
|
|
|
size_t numFilledWords = (value.getNumWords() - 1) * APInt::APINT_WORD_SIZE;
|
|
|
|
std::copy_n(reinterpret_cast<const char *>(value.getRawData()),
|
|
|
|
numFilledWords, result);
|
|
|
|
// Convert last word of APInt to LE format and store it in char
|
|
|
|
// array(`valueLE`).
|
|
|
|
// ex. last word of `value` (BE): |------ij| ==> `valueLE` (LE): |ji------|
|
|
|
|
size_t lastWordPos = numFilledWords;
|
|
|
|
SmallVector<char, 8> valueLE(APInt::APINT_WORD_SIZE);
|
|
|
|
DenseIntOrFPElementsAttr::convertEndianOfCharForBEmachine(
|
|
|
|
reinterpret_cast<const char *>(value.getRawData()) + lastWordPos,
|
|
|
|
valueLE.begin(), APInt::APINT_BITS_PER_WORD, 1);
|
|
|
|
// Extract actual APInt data from `valueLE`, convert endianness to BE format,
|
|
|
|
// and store it in `result`.
|
|
|
|
// ex. `valueLE` (LE): |ji------| ==> `result` (BE): |abcdefgh|ij|
|
|
|
|
DenseIntOrFPElementsAttr::convertEndianOfCharForBEmachine(
|
|
|
|
valueLE.begin(), result + lastWordPos,
|
|
|
|
(numBytes - lastWordPos) * CHAR_BIT, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Copy `numBytes` data from `inArray`(char array) to `result`(APINT) for BE
|
|
|
|
/// format.
|
|
|
|
static void copyArrayToAPIntForBEmachine(const char *inArray, size_t numBytes,
|
|
|
|
APInt &result) {
|
|
|
|
assert(llvm::support::endian::system_endianness() == // NOLINT
|
|
|
|
llvm::support::endianness::big); // NOLINT
|
|
|
|
assert(result.getNumWords() * APInt::APINT_WORD_SIZE >= numBytes);
|
|
|
|
|
|
|
|
// Copy the data that fills the word of `result` from `inArray`.
|
|
|
|
// For example, when `result` has 2 words, the first word will be filled with
|
|
|
|
// data. So, the first 8 bytes are copied from `inArray` here.
|
|
|
|
// `inArray` (10 bytes, BE): |abcdefgh|ij|
|
|
|
|
// ==> `result` (2 words, BE): |abcdefgh|--------|
|
|
|
|
size_t numFilledWords = (result.getNumWords() - 1) * APInt::APINT_WORD_SIZE;
|
|
|
|
std::copy_n(
|
|
|
|
inArray, numFilledWords,
|
|
|
|
const_cast<char *>(reinterpret_cast<const char *>(result.getRawData())));
|
|
|
|
|
|
|
|
// Convert array data which will be last word of `result` to LE format, and
|
|
|
|
// store it in char array(`inArrayLE`).
|
|
|
|
// ex. `inArray` (last two bytes, BE): |ij| ==> `inArrayLE` (LE): |ji------|
|
|
|
|
size_t lastWordPos = numFilledWords;
|
|
|
|
SmallVector<char, 8> inArrayLE(APInt::APINT_WORD_SIZE);
|
|
|
|
DenseIntOrFPElementsAttr::convertEndianOfCharForBEmachine(
|
|
|
|
inArray + lastWordPos, inArrayLE.begin(),
|
|
|
|
(numBytes - lastWordPos) * CHAR_BIT, 1);
|
|
|
|
|
|
|
|
// Convert `inArrayLE` to BE format, and store it in last word of `result`.
|
|
|
|
// ex. `inArrayLE` (LE): |ji------| ==> `result` (BE): |abcdefgh|------ij|
|
|
|
|
DenseIntOrFPElementsAttr::convertEndianOfCharForBEmachine(
|
|
|
|
inArrayLE.begin(),
|
|
|
|
const_cast<char *>(reinterpret_cast<const char *>(result.getRawData())) +
|
|
|
|
lastWordPos,
|
|
|
|
APInt::APINT_BITS_PER_WORD, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Writes value to the bit position `bitPos` in array `rawData`.
|
|
|
|
static void writeBits(char *rawData, size_t bitPos, APInt value) {
|
|
|
|
size_t bitWidth = value.getBitWidth();
|
|
|
|
|
|
|
|
// If the bitwidth is 1 we just toggle the specific bit.
|
|
|
|
if (bitWidth == 1)
|
|
|
|
return setBit(rawData, bitPos, value.isOneValue());
|
|
|
|
|
|
|
|
// Otherwise, the bit position is guaranteed to be byte aligned.
|
|
|
|
assert((bitPos % CHAR_BIT) == 0 && "expected bitPos to be 8-bit aligned");
|
|
|
|
if (llvm::support::endian::system_endianness() ==
|
|
|
|
llvm::support::endianness::big) {
|
|
|
|
// Copy from `value` to `rawData + (bitPos / CHAR_BIT)`.
|
|
|
|
// Copying the first `llvm::divideCeil(bitWidth, CHAR_BIT)` bytes doesn't
|
|
|
|
// work correctly in BE format.
|
|
|
|
// ex. `value` (2 words including 10 bytes)
|
|
|
|
// ==> BE: |abcdefgh|------ij|, LE: |hgfedcba|ji------|
|
|
|
|
copyAPIntToArrayForBEmachine(value, llvm::divideCeil(bitWidth, CHAR_BIT),
|
|
|
|
rawData + (bitPos / CHAR_BIT));
|
|
|
|
} else {
|
|
|
|
std::copy_n(reinterpret_cast<const char *>(value.getRawData()),
|
|
|
|
llvm::divideCeil(bitWidth, CHAR_BIT),
|
|
|
|
rawData + (bitPos / CHAR_BIT));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads the next `bitWidth` bits from the bit position `bitPos` in array
|
|
|
|
/// `rawData`.
|
|
|
|
static APInt readBits(const char *rawData, size_t bitPos, size_t bitWidth) {
|
|
|
|
// Handle a boolean bit position.
|
|
|
|
if (bitWidth == 1)
|
|
|
|
return APInt(1, getBit(rawData, bitPos) ? 1 : 0);
|
|
|
|
|
|
|
|
// Otherwise, the bit position must be 8-bit aligned.
|
|
|
|
assert((bitPos % CHAR_BIT) == 0 && "expected bitPos to be 8-bit aligned");
|
|
|
|
APInt result(bitWidth, 0);
|
|
|
|
if (llvm::support::endian::system_endianness() ==
|
|
|
|
llvm::support::endianness::big) {
|
|
|
|
// Copy from `rawData + (bitPos / CHAR_BIT)` to `result`.
|
|
|
|
// Copying the first `llvm::divideCeil(bitWidth, CHAR_BIT)` bytes doesn't
|
|
|
|
// work correctly in BE format.
|
|
|
|
// ex. `result` (2 words including 10 bytes)
|
|
|
|
// ==> BE: |abcdefgh|------ij|, LE: |hgfedcba|ji------| This function
|
|
|
|
copyArrayToAPIntForBEmachine(rawData + (bitPos / CHAR_BIT),
|
|
|
|
llvm::divideCeil(bitWidth, CHAR_BIT), result);
|
|
|
|
} else {
|
|
|
|
std::copy_n(rawData + (bitPos / CHAR_BIT),
|
|
|
|
llvm::divideCeil(bitWidth, CHAR_BIT),
|
|
|
|
const_cast<char *>(
|
|
|
|
reinterpret_cast<const char *>(result.getRawData())));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if 'values' corresponds to a splat, i.e. one element, or has
|
|
|
|
/// the same element count as 'type'.
|
|
|
|
template <typename Values>
|
|
|
|
static bool hasSameElementsOrSplat(ShapedType type, const Values &values) {
|
|
|
|
return (values.size() == 1) ||
|
|
|
|
(type.getNumElements() == static_cast<int64_t>(values.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DenseElementsAttr Iterators
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AttributeElementIterator
|
|
|
|
|
|
|
|
DenseElementsAttr::AttributeElementIterator::AttributeElementIterator(
|
|
|
|
DenseElementsAttr attr, size_t index)
|
|
|
|
: llvm::indexed_accessor_iterator<AttributeElementIterator, const void *,
|
|
|
|
Attribute, Attribute, Attribute>(
|
|
|
|
attr.getAsOpaquePointer(), index) {}
|
|
|
|
|
|
|
|
Attribute DenseElementsAttr::AttributeElementIterator::operator*() const {
|
|
|
|
auto owner = getFromOpaquePointer(base).cast<DenseElementsAttr>();
|
2021-09-21 01:40:22 +00:00
|
|
|
Type eltTy = owner.getElementType();
|
2020-12-03 17:22:57 -08:00
|
|
|
if (auto intEltTy = eltTy.dyn_cast<IntegerType>())
|
|
|
|
return IntegerAttr::get(eltTy, *IntElementIterator(owner, index));
|
|
|
|
if (eltTy.isa<IndexType>())
|
|
|
|
return IntegerAttr::get(eltTy, *IntElementIterator(owner, index));
|
|
|
|
if (auto floatEltTy = eltTy.dyn_cast<FloatType>()) {
|
|
|
|
IntElementIterator intIt(owner, index);
|
|
|
|
FloatElementIterator floatIt(floatEltTy.getFloatSemantics(), intIt);
|
|
|
|
return FloatAttr::get(eltTy, *floatIt);
|
|
|
|
}
|
2021-05-14 13:00:38 +02:00
|
|
|
if (auto complexTy = eltTy.dyn_cast<ComplexType>()) {
|
|
|
|
auto complexEltTy = complexTy.getElementType();
|
|
|
|
ComplexIntElementIterator complexIntIt(owner, index);
|
|
|
|
if (complexEltTy.isa<IntegerType>()) {
|
|
|
|
auto value = *complexIntIt;
|
|
|
|
auto real = IntegerAttr::get(complexEltTy, value.real());
|
|
|
|
auto imag = IntegerAttr::get(complexEltTy, value.imag());
|
|
|
|
return ArrayAttr::get(complexTy.getContext(),
|
|
|
|
ArrayRef<Attribute>{real, imag});
|
|
|
|
}
|
|
|
|
|
|
|
|
ComplexFloatElementIterator complexFloatIt(
|
|
|
|
complexEltTy.cast<FloatType>().getFloatSemantics(), complexIntIt);
|
|
|
|
auto value = *complexFloatIt;
|
|
|
|
auto real = FloatAttr::get(complexEltTy, value.real());
|
|
|
|
auto imag = FloatAttr::get(complexEltTy, value.imag());
|
|
|
|
return ArrayAttr::get(complexTy.getContext(),
|
|
|
|
ArrayRef<Attribute>{real, imag});
|
|
|
|
}
|
2020-12-03 17:22:57 -08:00
|
|
|
if (owner.isa<DenseStringElementsAttr>()) {
|
|
|
|
ArrayRef<StringRef> vals = owner.getRawStringData();
|
|
|
|
return StringAttr::get(owner.isSplat() ? vals.front() : vals[index], eltTy);
|
|
|
|
}
|
|
|
|
llvm_unreachable("unexpected element type");
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// BoolElementIterator
|
|
|
|
|
|
|
|
DenseElementsAttr::BoolElementIterator::BoolElementIterator(
|
|
|
|
DenseElementsAttr attr, size_t dataIndex)
|
|
|
|
: DenseElementIndexedIteratorImpl<BoolElementIterator, bool, bool, bool>(
|
|
|
|
attr.getRawData().data(), attr.isSplat(), dataIndex) {}
|
|
|
|
|
|
|
|
bool DenseElementsAttr::BoolElementIterator::operator*() const {
|
|
|
|
return getBit(getData(), getDataIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IntElementIterator
|
|
|
|
|
|
|
|
DenseElementsAttr::IntElementIterator::IntElementIterator(
|
|
|
|
DenseElementsAttr attr, size_t dataIndex)
|
|
|
|
: DenseElementIndexedIteratorImpl<IntElementIterator, APInt, APInt, APInt>(
|
|
|
|
attr.getRawData().data(), attr.isSplat(), dataIndex),
|
2021-09-21 01:40:22 +00:00
|
|
|
bitWidth(getDenseElementBitWidth(attr.getElementType())) {}
|
2020-12-03 17:22:57 -08:00
|
|
|
|
|
|
|
APInt DenseElementsAttr::IntElementIterator::operator*() const {
|
|
|
|
return readBits(getData(),
|
|
|
|
getDataIndex() * getDenseElementStorageWidth(bitWidth),
|
|
|
|
bitWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ComplexIntElementIterator
|
|
|
|
|
|
|
|
DenseElementsAttr::ComplexIntElementIterator::ComplexIntElementIterator(
|
|
|
|
DenseElementsAttr attr, size_t dataIndex)
|
|
|
|
: DenseElementIndexedIteratorImpl<ComplexIntElementIterator,
|
|
|
|
std::complex<APInt>, std::complex<APInt>,
|
|
|
|
std::complex<APInt>>(
|
|
|
|
attr.getRawData().data(), attr.isSplat(), dataIndex) {
|
2021-09-21 01:40:22 +00:00
|
|
|
auto complexType = attr.getElementType().cast<ComplexType>();
|
2020-12-03 17:22:57 -08:00
|
|
|
bitWidth = getDenseElementBitWidth(complexType.getElementType());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::complex<APInt>
|
|
|
|
DenseElementsAttr::ComplexIntElementIterator::operator*() const {
|
|
|
|
size_t storageWidth = getDenseElementStorageWidth(bitWidth);
|
|
|
|
size_t offset = getDataIndex() * storageWidth * 2;
|
|
|
|
return {readBits(getData(), offset, bitWidth),
|
|
|
|
readBits(getData(), offset + storageWidth, bitWidth)};
|
|
|
|
}
|
|
|
|
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DenseArrayAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2022-08-01 16:21:33 -04:00
|
|
|
const bool *DenseArrayBaseAttr::value_begin_impl(OverloadToken<bool>) const {
|
|
|
|
return cast<DenseBoolArrayAttr>().asArrayRef().begin();
|
|
|
|
}
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
const int8_t *
|
|
|
|
DenseArrayBaseAttr::value_begin_impl(OverloadToken<int8_t>) const {
|
|
|
|
return cast<DenseI8ArrayAttr>().asArrayRef().begin();
|
|
|
|
}
|
|
|
|
const int16_t *
|
|
|
|
DenseArrayBaseAttr::value_begin_impl(OverloadToken<int16_t>) const {
|
|
|
|
return cast<DenseI16ArrayAttr>().asArrayRef().begin();
|
|
|
|
}
|
|
|
|
const int32_t *
|
|
|
|
DenseArrayBaseAttr::value_begin_impl(OverloadToken<int32_t>) const {
|
|
|
|
return cast<DenseI32ArrayAttr>().asArrayRef().begin();
|
|
|
|
}
|
|
|
|
const int64_t *
|
|
|
|
DenseArrayBaseAttr::value_begin_impl(OverloadToken<int64_t>) const {
|
|
|
|
return cast<DenseI64ArrayAttr>().asArrayRef().begin();
|
|
|
|
}
|
|
|
|
const float *DenseArrayBaseAttr::value_begin_impl(OverloadToken<float>) const {
|
|
|
|
return cast<DenseF32ArrayAttr>().asArrayRef().begin();
|
|
|
|
}
|
|
|
|
const double *
|
|
|
|
DenseArrayBaseAttr::value_begin_impl(OverloadToken<double>) const {
|
|
|
|
return cast<DenseF64ArrayAttr>().asArrayRef().begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DenseArrayBaseAttr::print(AsmPrinter &printer) const {
|
|
|
|
print(printer.getStream());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DenseArrayBaseAttr::printWithoutBraces(raw_ostream &os) const {
|
|
|
|
switch (getElementType()) {
|
2022-08-01 16:21:33 -04:00
|
|
|
case DenseArrayBaseAttr::EltType::I1:
|
|
|
|
this->cast<DenseBoolArrayAttr>().printWithoutBraces(os);
|
|
|
|
return;
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
case DenseArrayBaseAttr::EltType::I8:
|
|
|
|
this->cast<DenseI8ArrayAttr>().printWithoutBraces(os);
|
|
|
|
return;
|
|
|
|
case DenseArrayBaseAttr::EltType::I16:
|
|
|
|
this->cast<DenseI16ArrayAttr>().printWithoutBraces(os);
|
|
|
|
return;
|
|
|
|
case DenseArrayBaseAttr::EltType::I32:
|
|
|
|
this->cast<DenseI32ArrayAttr>().printWithoutBraces(os);
|
|
|
|
return;
|
|
|
|
case DenseArrayBaseAttr::EltType::I64:
|
|
|
|
this->cast<DenseI64ArrayAttr>().printWithoutBraces(os);
|
|
|
|
return;
|
|
|
|
case DenseArrayBaseAttr::EltType::F32:
|
|
|
|
this->cast<DenseF32ArrayAttr>().printWithoutBraces(os);
|
|
|
|
return;
|
|
|
|
case DenseArrayBaseAttr::EltType::F64:
|
|
|
|
this->cast<DenseF64ArrayAttr>().printWithoutBraces(os);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
llvm_unreachable("<unknown DenseArrayBaseAttr>");
|
|
|
|
}
|
|
|
|
|
|
|
|
void DenseArrayBaseAttr::print(raw_ostream &os) const {
|
|
|
|
os << "[";
|
|
|
|
printWithoutBraces(os);
|
|
|
|
os << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DenseArrayAttr<T>::print(AsmPrinter &printer) const {
|
|
|
|
print(printer.getStream());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DenseArrayAttr<T>::printWithoutBraces(raw_ostream &os) const {
|
2022-08-01 16:21:33 -04:00
|
|
|
llvm::interleaveComma(asArrayRef(), os);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Specialization for bool to print `true` or `false`.
|
|
|
|
template <>
|
|
|
|
void DenseArrayAttr<bool>::printWithoutBraces(raw_ostream &os) const {
|
|
|
|
llvm::interleaveComma(asArrayRef(), os,
|
|
|
|
[&](bool v) { os << (v ? "true" : "false"); });
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Specialization for int8_t for forcing printing as number instead of chars.
|
|
|
|
template <>
|
|
|
|
void DenseArrayAttr<int8_t>::printWithoutBraces(raw_ostream &os) const {
|
2022-08-01 16:21:33 -04:00
|
|
|
llvm::interleaveComma(asArrayRef(), os, [&](int64_t v) { os << v; });
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DenseArrayAttr<T>::print(raw_ostream &os) const {
|
|
|
|
os << "[";
|
|
|
|
printWithoutBraces(os);
|
|
|
|
os << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a single element: generic template for int types, specialized for
|
2022-08-01 16:21:33 -04:00
|
|
|
/// floating point and boolean values below.
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
template <typename T>
|
|
|
|
static ParseResult parseDenseArrayAttrElt(AsmParser &parser, T &value) {
|
|
|
|
return parser.parseInteger(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
ParseResult parseDenseArrayAttrElt<float>(AsmParser &parser, float &value) {
|
|
|
|
double doubleVal;
|
|
|
|
if (parser.parseFloat(doubleVal))
|
|
|
|
return failure();
|
|
|
|
value = doubleVal;
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
ParseResult parseDenseArrayAttrElt<double>(AsmParser &parser, double &value) {
|
|
|
|
return parser.parseFloat(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a DenseArrayAttr without the braces: `1, 2, 3`
|
|
|
|
template <typename T>
|
|
|
|
Attribute DenseArrayAttr<T>::parseWithoutBraces(AsmParser &parser,
|
|
|
|
Type odsType) {
|
|
|
|
SmallVector<T> data;
|
|
|
|
if (failed(parser.parseCommaSeparatedList([&]() {
|
|
|
|
T value;
|
|
|
|
if (parseDenseArrayAttrElt(parser, value))
|
|
|
|
return failure();
|
|
|
|
data.push_back(value);
|
|
|
|
return success();
|
|
|
|
})))
|
|
|
|
return {};
|
|
|
|
return get(parser.getContext(), data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a DenseArrayAttr: `[ 1, 2, 3 ]`
|
|
|
|
template <typename T>
|
|
|
|
Attribute DenseArrayAttr<T>::parse(AsmParser &parser, Type odsType) {
|
|
|
|
if (parser.parseLSquare())
|
|
|
|
return {};
|
2022-07-13 08:52:38 +02:00
|
|
|
// Handle empty list case.
|
|
|
|
if (succeeded(parser.parseOptionalRSquare()))
|
|
|
|
return get(parser.getContext(), {});
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
Attribute result = parseWithoutBraces(parser, odsType);
|
|
|
|
if (parser.parseRSquare())
|
|
|
|
return {};
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Conversion from DenseArrayAttr<T> to ArrayRef<T>.
|
|
|
|
template <typename T>
|
|
|
|
DenseArrayAttr<T>::operator ArrayRef<T>() const {
|
2022-08-08 18:04:05 -04:00
|
|
|
ArrayRef<char> raw = getRawData();
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
assert((raw.size() % sizeof(T)) == 0);
|
|
|
|
return ArrayRef<T>(reinterpret_cast<const T *>(raw.data()),
|
|
|
|
raw.size() / sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// Mapping from C++ element type to MLIR DenseArrayAttr internals.
|
|
|
|
template <typename T>
|
|
|
|
struct denseArrayAttrEltTypeBuilder;
|
|
|
|
template <>
|
2022-08-01 16:21:33 -04:00
|
|
|
struct denseArrayAttrEltTypeBuilder<bool> {
|
|
|
|
constexpr static auto eltType = DenseArrayBaseAttr::EltType::I1;
|
|
|
|
static ShapedType getShapedType(MLIRContext *context,
|
|
|
|
ArrayRef<int64_t> shape) {
|
|
|
|
return RankedTensorType::get(shape, IntegerType::get(context, 1));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
struct denseArrayAttrEltTypeBuilder<int8_t> {
|
|
|
|
constexpr static auto eltType = DenseArrayBaseAttr::EltType::I8;
|
2022-07-13 08:52:38 +02:00
|
|
|
static ShapedType getShapedType(MLIRContext *context,
|
|
|
|
ArrayRef<int64_t> shape) {
|
2022-08-01 14:20:26 -04:00
|
|
|
return RankedTensorType::get(shape, IntegerType::get(context, 8));
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct denseArrayAttrEltTypeBuilder<int16_t> {
|
|
|
|
constexpr static auto eltType = DenseArrayBaseAttr::EltType::I16;
|
2022-07-13 08:52:38 +02:00
|
|
|
static ShapedType getShapedType(MLIRContext *context,
|
|
|
|
ArrayRef<int64_t> shape) {
|
2022-08-01 14:20:26 -04:00
|
|
|
return RankedTensorType::get(shape, IntegerType::get(context, 16));
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct denseArrayAttrEltTypeBuilder<int32_t> {
|
|
|
|
constexpr static auto eltType = DenseArrayBaseAttr::EltType::I32;
|
2022-07-13 08:52:38 +02:00
|
|
|
static ShapedType getShapedType(MLIRContext *context,
|
|
|
|
ArrayRef<int64_t> shape) {
|
2022-08-01 14:20:26 -04:00
|
|
|
return RankedTensorType::get(shape, IntegerType::get(context, 32));
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct denseArrayAttrEltTypeBuilder<int64_t> {
|
|
|
|
constexpr static auto eltType = DenseArrayBaseAttr::EltType::I64;
|
2022-07-13 08:52:38 +02:00
|
|
|
static ShapedType getShapedType(MLIRContext *context,
|
|
|
|
ArrayRef<int64_t> shape) {
|
2022-08-01 14:20:26 -04:00
|
|
|
return RankedTensorType::get(shape, IntegerType::get(context, 64));
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct denseArrayAttrEltTypeBuilder<float> {
|
|
|
|
constexpr static auto eltType = DenseArrayBaseAttr::EltType::F32;
|
2022-07-13 08:52:38 +02:00
|
|
|
static ShapedType getShapedType(MLIRContext *context,
|
|
|
|
ArrayRef<int64_t> shape) {
|
2022-08-01 14:20:26 -04:00
|
|
|
return RankedTensorType::get(shape, Float32Type::get(context));
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct denseArrayAttrEltTypeBuilder<double> {
|
|
|
|
constexpr static auto eltType = DenseArrayBaseAttr::EltType::F64;
|
2022-07-13 08:52:38 +02:00
|
|
|
static ShapedType getShapedType(MLIRContext *context,
|
|
|
|
ArrayRef<int64_t> shape) {
|
2022-08-01 14:20:26 -04:00
|
|
|
return RankedTensorType::get(shape, Float64Type::get(context));
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
/// Builds a DenseArrayAttr<T> from an ArrayRef<T>.
|
|
|
|
template <typename T>
|
|
|
|
DenseArrayAttr<T> DenseArrayAttr<T>::get(MLIRContext *context,
|
|
|
|
ArrayRef<T> content) {
|
2022-07-13 08:52:38 +02:00
|
|
|
auto size = static_cast<int64_t>(content.size());
|
2022-08-01 14:20:26 -04:00
|
|
|
auto shapedType =
|
|
|
|
denseArrayAttrEltTypeBuilder<T>::getShapedType(context, size);
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
auto eltType = denseArrayAttrEltTypeBuilder<T>::eltType;
|
|
|
|
auto rawArray = ArrayRef<char>(reinterpret_cast<const char *>(content.data()),
|
|
|
|
content.size() * sizeof(T));
|
|
|
|
return Base::get(context, shapedType, eltType, rawArray)
|
|
|
|
.template cast<DenseArrayAttr<T>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool DenseArrayAttr<T>::classof(Attribute attr) {
|
|
|
|
return attr.isa<DenseArrayBaseAttr>() &&
|
|
|
|
attr.cast<DenseArrayBaseAttr>().getElementType() ==
|
|
|
|
denseArrayAttrEltTypeBuilder<T>::eltType;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace mlir {
|
|
|
|
namespace detail {
|
|
|
|
// Explicit instantiation for all the supported DenseArrayAttr.
|
2022-08-01 16:21:33 -04:00
|
|
|
template class DenseArrayAttr<bool>;
|
Introduce a new Dense Array attribute
This attribute is similar to DenseElementsAttr but does not support
splat. As such it has a much simpler API and does not need any smart
iterator: it exposes direct ArrayRef access.
A new syntax is introduced so that the generic printing/parsing looks
like:
[:i64 1, -2, 3]
This attribute beings like an ArrayAttr but has a `:` token after the
opening square brace to introduce the element type (supported are I8,
I16, I32, I64, F32, F64) and the comma separated list for the data.
This is particularly convenient for attributes intended to be small,
like those referring to shapes.
For example a `transpose` operation with a `dims` attribute could be
defined as such:
let arguments = (ins AnyTensor:$input, DenseI64ArrayAttr:$dims);
let assemblyFormat = "$input `dims` `=` $dims attr-dict : type($input)";
And printed this way (the element type is elided in this case):
transpose %input dims = [0, 2, 1] : tensor<2x3x4xf32>
The C++ API for dims would just directly return an ArrayRef<int64>
RFC: https://discourse.llvm.org/t/rfc-introduce-a-new-dense-array-attribute/63279
Recommit with a custom DenseArrayBaseAttrStorage class to ensure
over-alignment of the storage to the largest type.
Reviewed By: rriddle
Differential Revision: https://reviews.llvm.org/D123774
2022-06-28 11:29:27 +00:00
|
|
|
template class DenseArrayAttr<int8_t>;
|
|
|
|
template class DenseArrayAttr<int16_t>;
|
|
|
|
template class DenseArrayAttr<int32_t>;
|
|
|
|
template class DenseArrayAttr<int64_t>;
|
|
|
|
template class DenseArrayAttr<float>;
|
|
|
|
template class DenseArrayAttr<double>;
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace mlir
|
|
|
|
|
2020-12-03 17:22:57 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DenseElementsAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Method for support type inquiry through isa, cast and dyn_cast.
|
|
|
|
bool DenseElementsAttr::classof(Attribute attr) {
|
|
|
|
return attr.isa<DenseIntOrFPElementsAttr, DenseStringElementsAttr>();
|
|
|
|
}
|
|
|
|
|
|
|
|
DenseElementsAttr DenseElementsAttr::get(ShapedType type,
|
|
|
|
ArrayRef<Attribute> values) {
|
|
|
|
assert(hasSameElementsOrSplat(type, values));
|
|
|
|
|
|
|
|
// If the element type is not based on int/float/index, assume it is a string
|
|
|
|
// type.
|
[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-18 21:32:38 -07:00
|
|
|
Type eltType = type.getElementType();
|
|
|
|
if (!eltType.isIntOrIndexOrFloat()) {
|
2020-12-03 17:22:57 -08:00
|
|
|
SmallVector<StringRef, 8> stringValues;
|
|
|
|
stringValues.reserve(values.size());
|
|
|
|
for (Attribute attr : values) {
|
|
|
|
assert(attr.isa<StringAttr>() &&
|
|
|
|
"expected string value for non integer/index/float element");
|
|
|
|
stringValues.push_back(attr.cast<StringAttr>().getValue());
|
|
|
|
}
|
|
|
|
return get(type, stringValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, get the raw storage width to use for the allocation.
|
|
|
|
size_t bitWidth = getDenseElementBitWidth(eltType);
|
|
|
|
size_t storageBitWidth = getDenseElementStorageWidth(bitWidth);
|
|
|
|
|
|
|
|
// Compress the attribute values into a character buffer.
|
2022-05-05 16:29:33 -07:00
|
|
|
SmallVector<char, 8> data(
|
|
|
|
llvm::divideCeil(storageBitWidth * values.size(), CHAR_BIT));
|
2020-12-03 17:22:57 -08:00
|
|
|
APInt intVal;
|
|
|
|
for (unsigned i = 0, e = values.size(); i < e; ++i) {
|
[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-18 21:32:38 -07:00
|
|
|
if (auto floatAttr = values[i].dyn_cast<FloatAttr>()) {
|
|
|
|
assert(floatAttr.getType() == eltType &&
|
|
|
|
"expected float attribute type to equal element type");
|
|
|
|
intVal = floatAttr.getValue().bitcastToAPInt();
|
|
|
|
} else {
|
|
|
|
auto intAttr = values[i].cast<IntegerAttr>();
|
|
|
|
assert(intAttr.getType() == eltType &&
|
|
|
|
"expected integer attribute type to equal element type");
|
|
|
|
intVal = intAttr.getValue();
|
|
|
|
}
|
2020-12-03 17:22:57 -08:00
|
|
|
|
|
|
|
assert(intVal.getBitWidth() == bitWidth &&
|
|
|
|
"expected value to have same bitwidth as element type");
|
|
|
|
writeBits(data.data(), i * storageBitWidth, intVal);
|
|
|
|
}
|
2022-05-12 05:32:16 +01:00
|
|
|
|
|
|
|
// Handle the special encoding of splat of bool.
|
[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-18 21:32:38 -07:00
|
|
|
if (values.size() == 1 && eltType.isInteger(1))
|
2022-05-12 05:32:16 +01:00
|
|
|
data[0] = data[0] ? -1 : 0;
|
|
|
|
|
|
|
|
return DenseIntOrFPElementsAttr::getRaw(type, data);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
DenseElementsAttr DenseElementsAttr::get(ShapedType type,
|
|
|
|
ArrayRef<bool> values) {
|
|
|
|
assert(hasSameElementsOrSplat(type, values));
|
|
|
|
assert(type.getElementType().isInteger(1));
|
|
|
|
|
|
|
|
std::vector<char> buff(llvm::divideCeil(values.size(), CHAR_BIT));
|
2022-05-12 05:32:16 +01:00
|
|
|
|
|
|
|
if (!values.empty()) {
|
|
|
|
bool isSplat = true;
|
|
|
|
bool firstValue = values[0];
|
|
|
|
for (int i = 0, e = values.size(); i != e; ++i) {
|
|
|
|
isSplat &= values[i] == firstValue;
|
|
|
|
setBit(buff.data(), i, values[i]);
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:17:52 +01:00
|
|
|
// Splat of bool is encoded as a byte with all-ones in it.
|
|
|
|
if (isSplat) {
|
2022-05-12 05:32:16 +01:00
|
|
|
buff.resize(1);
|
|
|
|
buff[0] = values[0] ? -1 : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return DenseIntOrFPElementsAttr::getRaw(type, buff);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
DenseElementsAttr DenseElementsAttr::get(ShapedType type,
|
|
|
|
ArrayRef<StringRef> values) {
|
|
|
|
assert(!type.getElementType().isIntOrFloat());
|
|
|
|
return DenseStringElementsAttr::get(type, values);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs a dense integer elements attribute from an array of APInt
|
|
|
|
/// values. Each APInt value is expected to have the same bitwidth as the
|
|
|
|
/// element type of 'type'.
|
|
|
|
DenseElementsAttr DenseElementsAttr::get(ShapedType type,
|
|
|
|
ArrayRef<APInt> values) {
|
|
|
|
assert(type.getElementType().isIntOrIndex());
|
|
|
|
assert(hasSameElementsOrSplat(type, values));
|
|
|
|
size_t storageBitWidth = getDenseElementStorageWidth(type.getElementType());
|
2022-05-12 05:32:16 +01:00
|
|
|
return DenseIntOrFPElementsAttr::getRaw(type, storageBitWidth, values);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
DenseElementsAttr DenseElementsAttr::get(ShapedType type,
|
|
|
|
ArrayRef<std::complex<APInt>> values) {
|
|
|
|
ComplexType complex = type.getElementType().cast<ComplexType>();
|
|
|
|
assert(complex.getElementType().isa<IntegerType>());
|
|
|
|
assert(hasSameElementsOrSplat(type, values));
|
|
|
|
size_t storageBitWidth = getDenseElementStorageWidth(complex) / 2;
|
|
|
|
ArrayRef<APInt> intVals(reinterpret_cast<const APInt *>(values.data()),
|
|
|
|
values.size() * 2);
|
2022-05-12 05:32:16 +01:00
|
|
|
return DenseIntOrFPElementsAttr::getRaw(type, storageBitWidth, intVals);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Constructs a dense float elements attribute from an array of APFloat
|
|
|
|
// values. Each APFloat value is expected to have the same bitwidth as the
|
|
|
|
// element type of 'type'.
|
|
|
|
DenseElementsAttr DenseElementsAttr::get(ShapedType type,
|
|
|
|
ArrayRef<APFloat> values) {
|
|
|
|
assert(type.getElementType().isa<FloatType>());
|
|
|
|
assert(hasSameElementsOrSplat(type, values));
|
|
|
|
size_t storageBitWidth = getDenseElementStorageWidth(type.getElementType());
|
2022-05-12 05:32:16 +01:00
|
|
|
return DenseIntOrFPElementsAttr::getRaw(type, storageBitWidth, values);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
DenseElementsAttr
|
|
|
|
DenseElementsAttr::get(ShapedType type,
|
|
|
|
ArrayRef<std::complex<APFloat>> values) {
|
|
|
|
ComplexType complex = type.getElementType().cast<ComplexType>();
|
|
|
|
assert(complex.getElementType().isa<FloatType>());
|
|
|
|
assert(hasSameElementsOrSplat(type, values));
|
|
|
|
ArrayRef<APFloat> apVals(reinterpret_cast<const APFloat *>(values.data()),
|
|
|
|
values.size() * 2);
|
|
|
|
size_t storageBitWidth = getDenseElementStorageWidth(complex) / 2;
|
2022-05-12 05:32:16 +01:00
|
|
|
return DenseIntOrFPElementsAttr::getRaw(type, storageBitWidth, apVals);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct a dense elements attribute from a raw buffer representing the
|
|
|
|
/// data for this attribute. Users should generally not use this methods as
|
|
|
|
/// the expected buffer format may not be a form the user expects.
|
2022-05-12 05:32:16 +01:00
|
|
|
DenseElementsAttr
|
|
|
|
DenseElementsAttr::getFromRawBuffer(ShapedType type, ArrayRef<char> rawBuffer) {
|
|
|
|
return DenseIntOrFPElementsAttr::getRaw(type, rawBuffer);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if the given buffer is a valid raw buffer for the given type.
|
|
|
|
bool DenseElementsAttr::isValidRawBuffer(ShapedType type,
|
|
|
|
ArrayRef<char> rawBuffer,
|
|
|
|
bool &detectedSplat) {
|
|
|
|
size_t storageWidth = getDenseElementStorageWidth(type.getElementType());
|
|
|
|
size_t rawBufferWidth = rawBuffer.size() * CHAR_BIT;
|
2022-05-14 11:48:17 +01:00
|
|
|
int64_t numElements = type.getNumElements();
|
|
|
|
|
|
|
|
// The initializer is always a splat if the result type has a single element.
|
|
|
|
detectedSplat = numElements == 1;
|
2020-12-03 17:22:57 -08:00
|
|
|
|
|
|
|
// Storage width of 1 is special as it is packed by the bit.
|
|
|
|
if (storageWidth == 1) {
|
2021-10-06 18:41:22 -07:00
|
|
|
// Check for a splat, or a buffer equal to the number of elements which
|
|
|
|
// consists of either all 0's or all 1's.
|
|
|
|
if (rawBuffer.size() == 1) {
|
|
|
|
auto rawByte = static_cast<uint8_t>(rawBuffer[0]);
|
|
|
|
if (rawByte == 0 || rawByte == 0xff) {
|
|
|
|
detectedSplat = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-05-14 11:48:17 +01:00
|
|
|
|
|
|
|
// This is a valid non-splat buffer if it has the right size.
|
|
|
|
return rawBufferWidth == llvm::alignTo<8>(numElements);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
2022-05-14 11:48:17 +01:00
|
|
|
|
|
|
|
// All other types are 8-bit aligned, so we can just check the buffer width
|
|
|
|
// to know if only a single initializer element was passed in.
|
|
|
|
if (rawBufferWidth == storageWidth) {
|
|
|
|
detectedSplat = true;
|
2020-12-03 17:22:57 -08:00
|
|
|
return true;
|
2022-05-14 11:48:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The raw buffer is valid if it has the right size.
|
|
|
|
return rawBufferWidth == storageWidth * numElements;
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check the information for a C++ data type, check if this type is valid for
|
|
|
|
/// the current attribute. This method is used to verify specific type
|
|
|
|
/// invariants that the templatized 'getValues' method cannot.
|
|
|
|
static bool isValidIntOrFloat(Type type, int64_t dataEltSize, bool isInt,
|
|
|
|
bool isSigned) {
|
|
|
|
// Make sure that the data element size is the same as the type element width.
|
|
|
|
if (getDenseElementBitWidth(type) !=
|
|
|
|
static_cast<size_t>(dataEltSize * CHAR_BIT))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check that the element type is either float or integer or index.
|
|
|
|
if (!isInt)
|
|
|
|
return type.isa<FloatType>();
|
|
|
|
if (type.isIndex())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto intType = type.dyn_cast<IntegerType>();
|
|
|
|
if (!intType)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Make sure signedness semantics is consistent.
|
|
|
|
if (intType.isSignless())
|
|
|
|
return true;
|
|
|
|
return intType.isSigned() ? isSigned : !isSigned;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Defaults down the subclass implementation.
|
|
|
|
DenseElementsAttr DenseElementsAttr::getRawComplex(ShapedType type,
|
|
|
|
ArrayRef<char> data,
|
|
|
|
int64_t dataEltSize,
|
|
|
|
bool isInt, bool isSigned) {
|
|
|
|
return DenseIntOrFPElementsAttr::getRawComplex(type, data, dataEltSize, isInt,
|
|
|
|
isSigned);
|
|
|
|
}
|
|
|
|
DenseElementsAttr DenseElementsAttr::getRawIntOrFloat(ShapedType type,
|
|
|
|
ArrayRef<char> data,
|
|
|
|
int64_t dataEltSize,
|
|
|
|
bool isInt,
|
|
|
|
bool isSigned) {
|
|
|
|
return DenseIntOrFPElementsAttr::getRawIntOrFloat(type, data, dataEltSize,
|
|
|
|
isInt, isSigned);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DenseElementsAttr::isValidIntOrFloat(int64_t dataEltSize, bool isInt,
|
|
|
|
bool isSigned) const {
|
2021-09-21 01:40:22 +00:00
|
|
|
return ::isValidIntOrFloat(getElementType(), dataEltSize, isInt, isSigned);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
bool DenseElementsAttr::isValidComplex(int64_t dataEltSize, bool isInt,
|
|
|
|
bool isSigned) const {
|
|
|
|
return ::isValidIntOrFloat(
|
2021-09-21 01:40:22 +00:00
|
|
|
getElementType().cast<ComplexType>().getElementType(), dataEltSize / 2,
|
|
|
|
isInt, isSigned);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if this attribute corresponds to a splat, i.e. if all element
|
|
|
|
/// values are the same.
|
|
|
|
bool DenseElementsAttr::isSplat() const {
|
|
|
|
return static_cast<DenseElementsAttributeStorage *>(impl)->isSplat;
|
|
|
|
}
|
|
|
|
|
2021-09-21 01:40:22 +00:00
|
|
|
/// Return if the given complex type has an integer element type.
|
2021-10-28 09:38:25 -07:00
|
|
|
LLVM_ATTRIBUTE_UNUSED static bool isComplexOfIntType(Type type) {
|
2021-09-21 01:40:22 +00:00
|
|
|
return type.cast<ComplexType>().getElementType().isa<IntegerType>();
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
auto DenseElementsAttr::getComplexIntValues() const
|
2021-11-09 00:05:55 +00:00
|
|
|
-> iterator_range_impl<ComplexIntElementIterator> {
|
2021-09-21 01:40:22 +00:00
|
|
|
assert(isComplexOfIntType(getElementType()) &&
|
|
|
|
"expected complex integral type");
|
2021-11-09 00:05:55 +00:00
|
|
|
return {getType(), ComplexIntElementIterator(*this, 0),
|
2020-12-03 17:22:57 -08:00
|
|
|
ComplexIntElementIterator(*this, getNumElements())};
|
|
|
|
}
|
2021-09-21 01:40:22 +00:00
|
|
|
auto DenseElementsAttr::complex_value_begin() const
|
|
|
|
-> ComplexIntElementIterator {
|
|
|
|
assert(isComplexOfIntType(getElementType()) &&
|
|
|
|
"expected complex integral type");
|
|
|
|
return ComplexIntElementIterator(*this, 0);
|
|
|
|
}
|
|
|
|
auto DenseElementsAttr::complex_value_end() const -> ComplexIntElementIterator {
|
|
|
|
assert(isComplexOfIntType(getElementType()) &&
|
|
|
|
"expected complex integral type");
|
|
|
|
return ComplexIntElementIterator(*this, getNumElements());
|
|
|
|
}
|
2020-12-03 17:22:57 -08:00
|
|
|
|
|
|
|
/// Return the held element values as a range of APFloat. The element type of
|
|
|
|
/// this attribute must be of float type.
|
|
|
|
auto DenseElementsAttr::getFloatValues() const
|
2021-11-09 00:05:55 +00:00
|
|
|
-> iterator_range_impl<FloatElementIterator> {
|
2021-09-21 01:40:22 +00:00
|
|
|
auto elementType = getElementType().cast<FloatType>();
|
2020-12-03 17:22:57 -08:00
|
|
|
const auto &elementSemantics = elementType.getFloatSemantics();
|
2021-11-09 00:05:55 +00:00
|
|
|
return {getType(), FloatElementIterator(elementSemantics, raw_int_begin()),
|
2020-12-03 17:22:57 -08:00
|
|
|
FloatElementIterator(elementSemantics, raw_int_end())};
|
|
|
|
}
|
|
|
|
auto DenseElementsAttr::float_value_begin() const -> FloatElementIterator {
|
2021-09-21 01:40:22 +00:00
|
|
|
auto elementType = getElementType().cast<FloatType>();
|
|
|
|
return FloatElementIterator(elementType.getFloatSemantics(), raw_int_begin());
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
auto DenseElementsAttr::float_value_end() const -> FloatElementIterator {
|
2021-09-21 01:40:22 +00:00
|
|
|
auto elementType = getElementType().cast<FloatType>();
|
|
|
|
return FloatElementIterator(elementType.getFloatSemantics(), raw_int_end());
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
2021-09-21 01:40:22 +00:00
|
|
|
|
2020-12-03 17:22:57 -08:00
|
|
|
auto DenseElementsAttr::getComplexFloatValues() const
|
2021-11-09 00:05:55 +00:00
|
|
|
-> iterator_range_impl<ComplexFloatElementIterator> {
|
2021-09-21 01:40:22 +00:00
|
|
|
Type eltTy = getElementType().cast<ComplexType>().getElementType();
|
2020-12-03 17:22:57 -08:00
|
|
|
assert(eltTy.isa<FloatType>() && "expected complex float type");
|
|
|
|
const auto &semantics = eltTy.cast<FloatType>().getFloatSemantics();
|
2021-11-09 00:05:55 +00:00
|
|
|
return {getType(),
|
|
|
|
{semantics, {*this, 0}},
|
2020-12-03 17:22:57 -08:00
|
|
|
{semantics, {*this, static_cast<size_t>(getNumElements())}}};
|
|
|
|
}
|
2021-09-21 01:40:22 +00:00
|
|
|
auto DenseElementsAttr::complex_float_value_begin() const
|
|
|
|
-> ComplexFloatElementIterator {
|
|
|
|
Type eltTy = getElementType().cast<ComplexType>().getElementType();
|
|
|
|
assert(eltTy.isa<FloatType>() && "expected complex float type");
|
|
|
|
return {eltTy.cast<FloatType>().getFloatSemantics(), {*this, 0}};
|
|
|
|
}
|
|
|
|
auto DenseElementsAttr::complex_float_value_end() const
|
|
|
|
-> ComplexFloatElementIterator {
|
|
|
|
Type eltTy = getElementType().cast<ComplexType>().getElementType();
|
|
|
|
assert(eltTy.isa<FloatType>() && "expected complex float type");
|
|
|
|
return {eltTy.cast<FloatType>().getFloatSemantics(),
|
|
|
|
{*this, static_cast<size_t>(getNumElements())}};
|
|
|
|
}
|
2020-12-03 17:22:57 -08:00
|
|
|
|
|
|
|
/// Return the raw storage data held by this attribute.
|
|
|
|
ArrayRef<char> DenseElementsAttr::getRawData() const {
|
2021-03-16 16:30:46 -07:00
|
|
|
return static_cast<DenseIntOrFPElementsAttrStorage *>(impl)->data;
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
ArrayRef<StringRef> DenseElementsAttr::getRawStringData() const {
|
2021-03-16 16:30:46 -07:00
|
|
|
return static_cast<DenseStringElementsAttrStorage *>(impl)->data;
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a new DenseElementsAttr that has the same data as the current
|
|
|
|
/// attribute, but has been reshaped to 'newType'. The new type must have the
|
|
|
|
/// same total number of elements as well as element type.
|
|
|
|
DenseElementsAttr DenseElementsAttr::reshape(ShapedType newType) {
|
|
|
|
ShapedType curType = getType();
|
|
|
|
if (curType == newType)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
assert(newType.getElementType() == curType.getElementType() &&
|
|
|
|
"expected the same element type");
|
|
|
|
assert(newType.getNumElements() == curType.getNumElements() &&
|
|
|
|
"expected the same number of elements");
|
2022-05-12 05:32:16 +01:00
|
|
|
return DenseIntOrFPElementsAttr::getRaw(newType, getRawData());
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
2022-02-18 18:07:36 +00:00
|
|
|
DenseElementsAttr DenseElementsAttr::resizeSplat(ShapedType newType) {
|
|
|
|
assert(isSplat() && "expected a splat type");
|
|
|
|
|
|
|
|
ShapedType curType = getType();
|
|
|
|
if (curType == newType)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
assert(newType.getElementType() == curType.getElementType() &&
|
|
|
|
"expected the same element type");
|
2022-05-12 05:32:16 +01:00
|
|
|
return DenseIntOrFPElementsAttr::getRaw(newType, getRawData());
|
2022-02-18 18:07:36 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 16:29:59 -07:00
|
|
|
/// Return a new DenseElementsAttr that has the same data as the current
|
|
|
|
/// attribute, but has bitcast elements such that it is now 'newType'. The new
|
|
|
|
/// type must have the same shape and element types of the same bitwidth as the
|
|
|
|
/// current type.
|
|
|
|
DenseElementsAttr DenseElementsAttr::bitcast(Type newElType) {
|
|
|
|
ShapedType curType = getType();
|
|
|
|
Type curElType = curType.getElementType();
|
|
|
|
if (curElType == newElType)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
assert(getDenseElementBitWidth(newElType) ==
|
|
|
|
getDenseElementBitWidth(curElType) &&
|
|
|
|
"expected element types with the same bitwidth");
|
|
|
|
return DenseIntOrFPElementsAttr::getRaw(curType.clone(newElType),
|
2022-05-12 05:32:16 +01:00
|
|
|
getRawData());
|
2021-08-05 16:29:59 -07:00
|
|
|
}
|
|
|
|
|
2020-12-03 17:22:57 -08:00
|
|
|
DenseElementsAttr
|
|
|
|
DenseElementsAttr::mapValues(Type newElementType,
|
|
|
|
function_ref<APInt(const APInt &)> mapping) const {
|
|
|
|
return cast<DenseIntElementsAttr>().mapValues(newElementType, mapping);
|
|
|
|
}
|
|
|
|
|
|
|
|
DenseElementsAttr DenseElementsAttr::mapValues(
|
|
|
|
Type newElementType, function_ref<APInt(const APFloat &)> mapping) const {
|
|
|
|
return cast<DenseFPElementsAttr>().mapValues(newElementType, mapping);
|
|
|
|
}
|
|
|
|
|
2021-09-21 01:40:45 +00:00
|
|
|
ShapedType DenseElementsAttr::getType() const {
|
[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-18 21:32:38 -07:00
|
|
|
return static_cast<const DenseElementsAttributeStorage *>(impl)->type;
|
2021-09-21 01:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Type DenseElementsAttr::getElementType() const {
|
|
|
|
return getType().getElementType();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t DenseElementsAttr::getNumElements() const {
|
|
|
|
return getType().getNumElements();
|
|
|
|
}
|
|
|
|
|
2020-12-03 17:22:57 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DenseIntOrFPElementsAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Utility method to write a range of APInt values to a buffer.
|
|
|
|
template <typename APRangeT>
|
|
|
|
static void writeAPIntsToBuffer(size_t storageWidth, std::vector<char> &data,
|
|
|
|
APRangeT &&values) {
|
2022-05-12 05:32:16 +01:00
|
|
|
size_t numValues = llvm::size(values);
|
|
|
|
data.resize(llvm::divideCeil(storageWidth * numValues, CHAR_BIT));
|
2020-12-03 17:22:57 -08:00
|
|
|
size_t offset = 0;
|
|
|
|
for (auto it = values.begin(), e = values.end(); it != e;
|
|
|
|
++it, offset += storageWidth) {
|
|
|
|
assert((*it).getBitWidth() <= storageWidth);
|
|
|
|
writeBits(data.data(), offset, *it);
|
|
|
|
}
|
2022-05-12 05:32:16 +01:00
|
|
|
|
|
|
|
// Handle the special encoding of splat of a boolean.
|
|
|
|
if (numValues == 1 && (*values.begin()).getBitWidth() == 1)
|
|
|
|
data[0] = data[0] ? -1 : 0;
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs a dense elements attribute from an array of raw APFloat values.
|
|
|
|
/// Each APFloat value is expected to have the same bitwidth as the element
|
|
|
|
/// type of 'type'. 'type' must be a vector or tensor with static shape.
|
|
|
|
DenseElementsAttr DenseIntOrFPElementsAttr::getRaw(ShapedType type,
|
|
|
|
size_t storageWidth,
|
2022-05-12 05:32:16 +01:00
|
|
|
ArrayRef<APFloat> values) {
|
2020-12-03 17:22:57 -08:00
|
|
|
std::vector<char> data;
|
|
|
|
auto unwrapFloat = [](const APFloat &val) { return val.bitcastToAPInt(); };
|
|
|
|
writeAPIntsToBuffer(storageWidth, data, llvm::map_range(values, unwrapFloat));
|
2022-05-12 05:32:16 +01:00
|
|
|
return DenseIntOrFPElementsAttr::getRaw(type, data);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs a dense elements attribute from an array of raw APInt values.
|
|
|
|
/// Each APInt value is expected to have the same bitwidth as the element type
|
|
|
|
/// of 'type'.
|
|
|
|
DenseElementsAttr DenseIntOrFPElementsAttr::getRaw(ShapedType type,
|
|
|
|
size_t storageWidth,
|
2022-05-12 05:32:16 +01:00
|
|
|
ArrayRef<APInt> values) {
|
2020-12-03 17:22:57 -08:00
|
|
|
std::vector<char> data;
|
|
|
|
writeAPIntsToBuffer(storageWidth, data, values);
|
2022-05-12 05:32:16 +01:00
|
|
|
return DenseIntOrFPElementsAttr::getRaw(type, data);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
DenseElementsAttr DenseIntOrFPElementsAttr::getRaw(ShapedType type,
|
2022-05-12 05:32:16 +01:00
|
|
|
ArrayRef<char> data) {
|
2020-12-03 17:22:57 -08:00
|
|
|
assert((type.isa<RankedTensorType, VectorType>()) &&
|
|
|
|
"type must be ranked tensor or vector");
|
|
|
|
assert(type.hasStaticShape() && "type must have static shape");
|
2022-05-12 05:32:16 +01:00
|
|
|
bool isSplat = false;
|
|
|
|
bool isValid = isValidRawBuffer(type, data, isSplat);
|
|
|
|
assert(isValid);
|
|
|
|
(void)isValid;
|
2020-12-03 17:22:57 -08:00
|
|
|
return Base::get(type.getContext(), type, data, isSplat);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Overload of the raw 'get' method that asserts that the given type is of
|
|
|
|
/// complex type. This method is used to verify type invariants that the
|
|
|
|
/// templatized 'get' method cannot.
|
|
|
|
DenseElementsAttr DenseIntOrFPElementsAttr::getRawComplex(ShapedType type,
|
|
|
|
ArrayRef<char> data,
|
|
|
|
int64_t dataEltSize,
|
|
|
|
bool isInt,
|
|
|
|
bool isSigned) {
|
|
|
|
assert(::isValidIntOrFloat(
|
|
|
|
type.getElementType().cast<ComplexType>().getElementType(),
|
|
|
|
dataEltSize / 2, isInt, isSigned));
|
|
|
|
|
|
|
|
int64_t numElements = data.size() / dataEltSize;
|
2022-05-12 17:59:39 +02:00
|
|
|
(void)numElements;
|
2020-12-03 17:22:57 -08:00
|
|
|
assert(numElements == 1 || numElements == type.getNumElements());
|
2022-05-12 05:32:16 +01:00
|
|
|
return getRaw(type, data);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Overload of the 'getRaw' method that asserts that the given type is of
|
|
|
|
/// integer type. This method is used to verify type invariants that the
|
|
|
|
/// templatized 'get' method cannot.
|
|
|
|
DenseElementsAttr
|
|
|
|
DenseIntOrFPElementsAttr::getRawIntOrFloat(ShapedType type, ArrayRef<char> data,
|
|
|
|
int64_t dataEltSize, bool isInt,
|
|
|
|
bool isSigned) {
|
|
|
|
assert(
|
|
|
|
::isValidIntOrFloat(type.getElementType(), dataEltSize, isInt, isSigned));
|
|
|
|
|
|
|
|
int64_t numElements = data.size() / dataEltSize;
|
|
|
|
assert(numElements == 1 || numElements == type.getNumElements());
|
2022-05-12 05:32:16 +01:00
|
|
|
(void)numElements;
|
|
|
|
return getRaw(type, data);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DenseIntOrFPElementsAttr::convertEndianOfCharForBEmachine(
|
|
|
|
const char *inRawData, char *outRawData, size_t elementBitWidth,
|
|
|
|
size_t numElements) {
|
|
|
|
using llvm::support::ulittle16_t;
|
|
|
|
using llvm::support::ulittle32_t;
|
|
|
|
using llvm::support::ulittle64_t;
|
|
|
|
|
|
|
|
assert(llvm::support::endian::system_endianness() == // NOLINT
|
|
|
|
llvm::support::endianness::big); // NOLINT
|
|
|
|
// NOLINT to avoid warning message about replacing by static_assert()
|
|
|
|
|
|
|
|
// Following std::copy_n always converts endianness on BE machine.
|
|
|
|
switch (elementBitWidth) {
|
|
|
|
case 16: {
|
|
|
|
const ulittle16_t *inRawDataPos =
|
|
|
|
reinterpret_cast<const ulittle16_t *>(inRawData);
|
|
|
|
uint16_t *outDataPos = reinterpret_cast<uint16_t *>(outRawData);
|
|
|
|
std::copy_n(inRawDataPos, numElements, outDataPos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 32: {
|
|
|
|
const ulittle32_t *inRawDataPos =
|
|
|
|
reinterpret_cast<const ulittle32_t *>(inRawData);
|
|
|
|
uint32_t *outDataPos = reinterpret_cast<uint32_t *>(outRawData);
|
|
|
|
std::copy_n(inRawDataPos, numElements, outDataPos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 64: {
|
|
|
|
const ulittle64_t *inRawDataPos =
|
|
|
|
reinterpret_cast<const ulittle64_t *>(inRawData);
|
|
|
|
uint64_t *outDataPos = reinterpret_cast<uint64_t *>(outRawData);
|
|
|
|
std::copy_n(inRawDataPos, numElements, outDataPos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
size_t nBytes = elementBitWidth / CHAR_BIT;
|
|
|
|
for (size_t i = 0; i < nBytes; i++)
|
|
|
|
std::copy_n(inRawData + (nBytes - 1 - i), 1, outRawData + i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DenseIntOrFPElementsAttr::convertEndianOfArrayRefForBEmachine(
|
|
|
|
ArrayRef<char> inRawData, MutableArrayRef<char> outRawData,
|
|
|
|
ShapedType type) {
|
|
|
|
size_t numElements = type.getNumElements();
|
|
|
|
Type elementType = type.getElementType();
|
|
|
|
if (ComplexType complexTy = elementType.dyn_cast<ComplexType>()) {
|
|
|
|
elementType = complexTy.getElementType();
|
|
|
|
numElements = numElements * 2;
|
|
|
|
}
|
|
|
|
size_t elementBitWidth = getDenseElementStorageWidth(elementType);
|
|
|
|
assert(numElements * elementBitWidth == inRawData.size() * CHAR_BIT &&
|
|
|
|
inRawData.size() <= outRawData.size());
|
2022-06-12 16:03:30 +02:00
|
|
|
if (elementBitWidth <= CHAR_BIT)
|
|
|
|
std::memcpy(outRawData.begin(), inRawData.begin(), inRawData.size());
|
|
|
|
else
|
|
|
|
convertEndianOfCharForBEmachine(inRawData.begin(), outRawData.begin(),
|
|
|
|
elementBitWidth, numElements);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DenseFPElementsAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
template <typename Fn, typename Attr>
|
|
|
|
static ShapedType mappingHelper(Fn mapping, Attr &attr, ShapedType inType,
|
|
|
|
Type newElementType,
|
|
|
|
llvm::SmallVectorImpl<char> &data) {
|
|
|
|
size_t bitWidth = getDenseElementBitWidth(newElementType);
|
|
|
|
size_t storageBitWidth = getDenseElementStorageWidth(bitWidth);
|
|
|
|
|
|
|
|
ShapedType newArrayType;
|
|
|
|
if (inType.isa<RankedTensorType>())
|
|
|
|
newArrayType = RankedTensorType::get(inType.getShape(), newElementType);
|
|
|
|
else if (inType.isa<UnrankedTensorType>())
|
|
|
|
newArrayType = RankedTensorType::get(inType.getShape(), newElementType);
|
2021-10-12 14:26:01 +01:00
|
|
|
else if (auto vType = inType.dyn_cast<VectorType>())
|
|
|
|
newArrayType = VectorType::get(vType.getShape(), newElementType,
|
|
|
|
vType.getNumScalableDims());
|
2020-12-03 17:22:57 -08:00
|
|
|
else
|
|
|
|
assert(newArrayType && "Unhandled tensor type");
|
|
|
|
|
|
|
|
size_t numRawElements = attr.isSplat() ? 1 : newArrayType.getNumElements();
|
2022-05-05 16:29:33 -07:00
|
|
|
data.resize(llvm::divideCeil(storageBitWidth * numRawElements, CHAR_BIT));
|
2020-12-03 17:22:57 -08:00
|
|
|
|
|
|
|
// Functor used to process a single element value of the attribute.
|
|
|
|
auto processElt = [&](decltype(*attr.begin()) value, size_t index) {
|
|
|
|
auto newInt = mapping(value);
|
|
|
|
assert(newInt.getBitWidth() == bitWidth);
|
|
|
|
writeBits(data.data(), index * storageBitWidth, newInt);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check for the splat case.
|
|
|
|
if (attr.isSplat()) {
|
|
|
|
processElt(*attr.begin(), /*index=*/0);
|
|
|
|
return newArrayType;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, process all of the element values.
|
|
|
|
uint64_t elementIdx = 0;
|
|
|
|
for (auto value : attr)
|
|
|
|
processElt(value, elementIdx++);
|
|
|
|
return newArrayType;
|
|
|
|
}
|
|
|
|
|
|
|
|
DenseElementsAttr DenseFPElementsAttr::mapValues(
|
|
|
|
Type newElementType, function_ref<APInt(const APFloat &)> mapping) const {
|
|
|
|
llvm::SmallVector<char, 8> elementData;
|
|
|
|
auto newArrayType =
|
|
|
|
mappingHelper(mapping, *this, getType(), newElementType, elementData);
|
|
|
|
|
2022-05-12 05:32:16 +01:00
|
|
|
return getRaw(newArrayType, elementData);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Method for supporting type inquiry through isa, cast and dyn_cast.
|
|
|
|
bool DenseFPElementsAttr::classof(Attribute attr) {
|
[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-18 21:32:38 -07:00
|
|
|
if (auto denseAttr = attr.dyn_cast<DenseElementsAttr>())
|
|
|
|
return denseAttr.getType().getElementType().isa<FloatType>();
|
|
|
|
return false;
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DenseIntElementsAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
DenseElementsAttr DenseIntElementsAttr::mapValues(
|
|
|
|
Type newElementType, function_ref<APInt(const APInt &)> mapping) const {
|
|
|
|
llvm::SmallVector<char, 8> elementData;
|
|
|
|
auto newArrayType =
|
|
|
|
mappingHelper(mapping, *this, getType(), newElementType, elementData);
|
2022-05-12 05:32:16 +01:00
|
|
|
return getRaw(newArrayType, elementData);
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Method for supporting type inquiry through isa, cast and dyn_cast.
|
|
|
|
bool DenseIntElementsAttr::classof(Attribute attr) {
|
[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-18 21:32:38 -07:00
|
|
|
if (auto denseAttr = attr.dyn_cast<DenseElementsAttr>())
|
|
|
|
return denseAttr.getType().getElementType().isIntOrIndex();
|
|
|
|
return false;
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
[mlir] Add a new builtin DenseResourceElementsAttr
This attributes is intended cover the current set of use cases that abuse
DenseElementsAttr, e.g. when the data is large. Using resources for large
data is one of the major reasons why they were added; e.g. they can be
deallocated mid-compilation, they support a wide variety of data origins
(e.g, heap allocated, mmap'd, etc.), they can support mutation, etc.
I considered at length not having a builtin variant of this, and instead
having multiple versions of this attribute for dialects that are interested,
but they all boiled down to the exact same attribute definition. Given the
generality of this attribute, it feels more aligned to keep it next to DenseArrayAttr
(given that DenseArrayAttr covers the "small" case, and DenseResourcesElementsAttr
covers the "large" case). The underlying infra used to build this attribute is
general, and having a builtin attribute doesn't preclude users from defining
their own when it makes sense (they can even share a blob manager with the
builtin dialect to avoid data duplication).
Differential Revision: https://reviews.llvm.org/D130022
2022-07-19 18:22:55 -07:00
|
|
|
// DenseResourceElementsAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
DenseResourceElementsAttr
|
|
|
|
DenseResourceElementsAttr::get(ShapedType type,
|
|
|
|
DenseResourceElementsHandle handle) {
|
|
|
|
return Base::get(type.getContext(), type, handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
DenseResourceElementsAttr DenseResourceElementsAttr::get(ShapedType type,
|
|
|
|
StringRef blobName,
|
|
|
|
AsmResourceBlob blob) {
|
|
|
|
// Extract the builtin dialect resource manager from context and construct a
|
|
|
|
// handle by inserting a new resource using the provided blob.
|
|
|
|
auto &manager =
|
|
|
|
DenseResourceElementsHandle::getManagerInterface(type.getContext());
|
|
|
|
return get(type, manager.insert(blobName, std::move(blob)));
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// DenseResourceElementsAttrBase
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// Instantiations of this class provide utilities for interacting with native
|
|
|
|
/// data types in the context of DenseResourceElementsAttr.
|
|
|
|
template <typename T>
|
|
|
|
struct DenseResourceAttrUtil;
|
|
|
|
template <size_t width, bool isSigned>
|
|
|
|
struct DenseResourceElementsAttrIntUtil {
|
|
|
|
static bool checkElementType(Type eltType) {
|
|
|
|
IntegerType type = eltType.dyn_cast<IntegerType>();
|
|
|
|
if (!type || type.getWidth() != width)
|
|
|
|
return false;
|
|
|
|
return isSigned ? !type.isUnsigned() : !type.isSigned();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct DenseResourceAttrUtil<bool> {
|
|
|
|
static bool checkElementType(Type eltType) {
|
|
|
|
return eltType.isSignlessInteger(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct DenseResourceAttrUtil<int8_t>
|
|
|
|
: public DenseResourceElementsAttrIntUtil<8, true> {};
|
|
|
|
template <>
|
|
|
|
struct DenseResourceAttrUtil<uint8_t>
|
|
|
|
: public DenseResourceElementsAttrIntUtil<8, false> {};
|
|
|
|
template <>
|
|
|
|
struct DenseResourceAttrUtil<int16_t>
|
|
|
|
: public DenseResourceElementsAttrIntUtil<16, true> {};
|
|
|
|
template <>
|
|
|
|
struct DenseResourceAttrUtil<uint16_t>
|
|
|
|
: public DenseResourceElementsAttrIntUtil<16, false> {};
|
|
|
|
template <>
|
|
|
|
struct DenseResourceAttrUtil<int32_t>
|
|
|
|
: public DenseResourceElementsAttrIntUtil<32, true> {};
|
|
|
|
template <>
|
|
|
|
struct DenseResourceAttrUtil<uint32_t>
|
|
|
|
: public DenseResourceElementsAttrIntUtil<32, false> {};
|
|
|
|
template <>
|
|
|
|
struct DenseResourceAttrUtil<int64_t>
|
|
|
|
: public DenseResourceElementsAttrIntUtil<64, true> {};
|
|
|
|
template <>
|
|
|
|
struct DenseResourceAttrUtil<uint64_t>
|
|
|
|
: public DenseResourceElementsAttrIntUtil<64, false> {};
|
|
|
|
template <>
|
|
|
|
struct DenseResourceAttrUtil<float> {
|
|
|
|
static bool checkElementType(Type eltType) { return eltType.isF32(); }
|
|
|
|
};
|
|
|
|
template <>
|
|
|
|
struct DenseResourceAttrUtil<double> {
|
|
|
|
static bool checkElementType(Type eltType) { return eltType.isF64(); }
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
DenseResourceElementsAttrBase<T>
|
|
|
|
DenseResourceElementsAttrBase<T>::get(ShapedType type, StringRef blobName,
|
|
|
|
AsmResourceBlob blob) {
|
|
|
|
// Check that the blob is in the form we were expecting.
|
|
|
|
assert(blob.getDataAlignment() == alignof(T) &&
|
|
|
|
"alignment mismatch between expected alignment and blob alignment");
|
|
|
|
assert(((blob.getData().size() % sizeof(T)) == 0) &&
|
|
|
|
"size mismatch between expected element width and blob size");
|
|
|
|
assert(DenseResourceAttrUtil<T>::checkElementType(type.getElementType()) &&
|
|
|
|
"invalid shape element type for provided type `T`");
|
|
|
|
return DenseResourceElementsAttr::get(type, blobName, std::move(blob))
|
|
|
|
.template cast<DenseResourceElementsAttrBase<T>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Optional<ArrayRef<T>>
|
|
|
|
DenseResourceElementsAttrBase<T>::tryGetAsArrayRef() const {
|
|
|
|
if (AsmResourceBlob *blob = this->getRawHandle().getBlob())
|
|
|
|
return blob->template getDataAs<T>();
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool DenseResourceElementsAttrBase<T>::classof(Attribute attr) {
|
|
|
|
auto resourceAttr = attr.dyn_cast<DenseResourceElementsAttr>();
|
|
|
|
return resourceAttr && DenseResourceAttrUtil<T>::checkElementType(
|
|
|
|
resourceAttr.getElementType());
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace mlir {
|
|
|
|
namespace detail {
|
|
|
|
// Explicit instantiation for all the supported DenseResourceElementsAttr.
|
|
|
|
template class DenseResourceElementsAttrBase<bool>;
|
|
|
|
template class DenseResourceElementsAttrBase<int8_t>;
|
|
|
|
template class DenseResourceElementsAttrBase<int16_t>;
|
|
|
|
template class DenseResourceElementsAttrBase<int32_t>;
|
|
|
|
template class DenseResourceElementsAttrBase<int64_t>;
|
|
|
|
template class DenseResourceElementsAttrBase<uint8_t>;
|
|
|
|
template class DenseResourceElementsAttrBase<uint16_t>;
|
|
|
|
template class DenseResourceElementsAttrBase<uint32_t>;
|
|
|
|
template class DenseResourceElementsAttrBase<uint64_t>;
|
|
|
|
template class DenseResourceElementsAttrBase<float>;
|
|
|
|
template class DenseResourceElementsAttrBase<double>;
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace mlir
|
|
|
|
|
2020-12-03 17:22:57 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SparseElementsAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Get a zero APFloat for the given sparse attribute.
|
|
|
|
APFloat SparseElementsAttr::getZeroAPFloat() const {
|
2021-09-21 01:40:22 +00:00
|
|
|
auto eltType = getElementType().cast<FloatType>();
|
2020-12-03 17:22:57 -08:00
|
|
|
return APFloat(eltType.getFloatSemantics());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a zero APInt for the given sparse attribute.
|
|
|
|
APInt SparseElementsAttr::getZeroAPInt() const {
|
2021-09-21 01:40:22 +00:00
|
|
|
auto eltType = getElementType().cast<IntegerType>();
|
2021-09-08 22:13:13 -07:00
|
|
|
return APInt::getZero(eltType.getWidth());
|
2020-12-03 17:22:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a zero attribute for the given attribute type.
|
|
|
|
Attribute SparseElementsAttr::getZeroAttr() const {
|
2021-09-21 01:40:22 +00:00
|
|
|
auto eltType = getElementType();
|
2020-12-03 17:22:57 -08:00
|
|
|
|
|
|
|
// Handle floating point elements.
|
|
|
|
if (eltType.isa<FloatType>())
|
|
|
|
return FloatAttr::get(eltType, 0);
|
|
|
|
|
2022-07-07 13:16:49 -07:00
|
|
|
// Handle complex elements.
|
|
|
|
if (auto complexTy = eltType.dyn_cast<ComplexType>()) {
|
|
|
|
auto eltType = complexTy.getElementType();
|
|
|
|
Attribute zero;
|
|
|
|
if (eltType.isa<FloatType>())
|
|
|
|
zero = FloatAttr::get(eltType, 0);
|
|
|
|
else // must be integer
|
|
|
|
zero = IntegerAttr::get(eltType, 0);
|
|
|
|
return ArrayAttr::get(complexTy.getContext(),
|
|
|
|
ArrayRef<Attribute>{zero, zero});
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:11:50 +00:00
|
|
|
// Handle string type.
|
|
|
|
if (getValues().isa<DenseStringElementsAttr>())
|
|
|
|
return StringAttr::get("", eltType);
|
|
|
|
|
2020-12-03 17:22:57 -08:00
|
|
|
// Otherwise, this is an integer.
|
|
|
|
return IntegerAttr::get(eltType, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Flatten, and return, all of the sparse indices in this attribute in
|
|
|
|
/// row-major order.
|
|
|
|
std::vector<ptrdiff_t> SparseElementsAttr::getFlattenedSparseIndices() const {
|
|
|
|
std::vector<ptrdiff_t> flatSparseIndices;
|
|
|
|
|
|
|
|
// The sparse indices are 64-bit integers, so we can reinterpret the raw data
|
|
|
|
// as a 1-D index array.
|
|
|
|
auto sparseIndices = getIndices();
|
|
|
|
auto sparseIndexValues = sparseIndices.getValues<uint64_t>();
|
|
|
|
if (sparseIndices.isSplat()) {
|
|
|
|
SmallVector<uint64_t, 8> indices(getType().getRank(),
|
|
|
|
*sparseIndexValues.begin());
|
|
|
|
flatSparseIndices.push_back(getFlattenedIndex(indices));
|
|
|
|
return flatSparseIndices;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, reinterpret each index as an ArrayRef when flattening.
|
|
|
|
auto numSparseIndices = sparseIndices.getType().getDimSize(0);
|
|
|
|
size_t rank = getType().getRank();
|
|
|
|
for (size_t i = 0, e = numSparseIndices; i != e; ++i)
|
|
|
|
flatSparseIndices.push_back(getFlattenedIndex(
|
|
|
|
{&*std::next(sparseIndexValues.begin(), i * rank), rank}));
|
|
|
|
return flatSparseIndices;
|
|
|
|
}
|
2021-06-10 17:22:49 -07:00
|
|
|
|
2021-09-21 01:40:04 +00:00
|
|
|
LogicalResult
|
|
|
|
SparseElementsAttr::verify(function_ref<InFlightDiagnostic()> emitError,
|
|
|
|
ShapedType type, DenseIntElementsAttr sparseIndices,
|
|
|
|
DenseElementsAttr values) {
|
|
|
|
ShapedType valuesType = values.getType();
|
|
|
|
if (valuesType.getRank() != 1)
|
|
|
|
return emitError() << "expected 1-d tensor for sparse element values";
|
|
|
|
|
|
|
|
// Verify the indices and values shape.
|
|
|
|
ShapedType indicesType = sparseIndices.getType();
|
|
|
|
auto emitShapeError = [&]() {
|
|
|
|
return emitError() << "expected shape ([" << type.getShape()
|
|
|
|
<< "]); inferred shape of indices literal (["
|
|
|
|
<< indicesType.getShape()
|
|
|
|
<< "]); inferred shape of values literal (["
|
|
|
|
<< valuesType.getShape() << "])";
|
|
|
|
};
|
|
|
|
// Verify indices shape.
|
|
|
|
size_t rank = type.getRank(), indicesRank = indicesType.getRank();
|
|
|
|
if (indicesRank == 2) {
|
2021-09-21 01:40:45 +00:00
|
|
|
if (indicesType.getDimSize(1) != static_cast<int64_t>(rank))
|
2021-09-21 01:40:04 +00:00
|
|
|
return emitShapeError();
|
|
|
|
} else if (indicesRank != 1 || rank != 1) {
|
|
|
|
return emitShapeError();
|
|
|
|
}
|
|
|
|
// Verify the values shape.
|
|
|
|
int64_t numSparseIndices = indicesType.getDimSize(0);
|
|
|
|
if (numSparseIndices != valuesType.getDimSize(0))
|
|
|
|
return emitShapeError();
|
|
|
|
|
|
|
|
// Verify that the sparse indices are within the value shape.
|
|
|
|
auto emitIndexError = [&](unsigned indexNum, ArrayRef<uint64_t> index) {
|
|
|
|
return emitError()
|
|
|
|
<< "sparse index #" << indexNum
|
|
|
|
<< " is not contained within the value shape, with index=[" << index
|
|
|
|
<< "], and type=" << type;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handle the case where the index values are a splat.
|
|
|
|
auto sparseIndexValues = sparseIndices.getValues<uint64_t>();
|
|
|
|
if (sparseIndices.isSplat()) {
|
|
|
|
SmallVector<uint64_t> indices(rank, *sparseIndexValues.begin());
|
|
|
|
if (!ElementsAttr::isValidIndex(type, indices))
|
|
|
|
return emitIndexError(0, indices);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, reinterpret each index as an ArrayRef.
|
|
|
|
for (size_t i = 0, e = numSparseIndices; i != e; ++i) {
|
|
|
|
ArrayRef<uint64_t> index(&*std::next(sparseIndexValues.begin(), i * rank),
|
|
|
|
rank);
|
|
|
|
if (!ElementsAttr::isValidIndex(type, index))
|
|
|
|
return emitIndexError(i, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
|
2021-06-10 17:22:49 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TypeAttr
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void TypeAttr::walkImmediateSubElements(
|
|
|
|
function_ref<void(Attribute)> walkAttrsFn,
|
|
|
|
function_ref<void(Type)> walkTypesFn) const {
|
|
|
|
walkTypesFn(getValue());
|
|
|
|
}
|
2022-07-26 13:22:19 -07:00
|
|
|
|
|
|
|
Attribute
|
|
|
|
TypeAttr::replaceImmediateSubElements(ArrayRef<Attribute> replAttrs,
|
|
|
|
ArrayRef<Type> replTypes) const {
|
|
|
|
return get(replTypes[0]);
|
|
|
|
}
|