9 #ifndef MLIR_IR_BUILTINATTRIBUTES_H
10 #define MLIR_IR_BUILTINATTRIBUTES_H
13 #include "llvm/ADT/APFloat.h"
14 #include "llvm/ADT/Sequence.h"
20 class AsmResourceBlob;
23 class DenseIntElementsAttr;
25 struct DialectResourceBlobHandle;
26 class FlatSymbolRefAttr;
32 class RankedTensorType;
44 template <
typename ConcreteT,
typename T,
typename PointerT = T *,
45 typename ReferenceT = T &>
47 :
public llvm::indexed_accessor_iterator<ConcreteT, DenseIterPtrAndSplat, T,
48 PointerT, ReferenceT> {
53 PointerT, ReferenceT>({data, isSplat},
59 bool isSplat = this->base.second;
60 return isSplat ? 0 : this->index;
64 const char *
getData()
const {
return this->base.first; }
81 operator ElementsAttr()
const {
return cast_if_present<ElementsAttr>(*
this); }
83 operator TypedAttr()
const {
return ElementsAttr(*
this); }
94 static constexpr
bool value = llvm::is_one_of<T, float, double>::value ||
95 (std::numeric_limits<T>::is_specialized &&
96 !std::numeric_limits<T>::is_integer);
112 template <
typename T,
113 typename = std::enable_if_t<std::numeric_limits<T>::is_integer ||
116 const char *data =
reinterpret_cast<const char *
>(values.data());
119 std::numeric_limits<T>::is_integer, std::numeric_limits<T>::is_signed);
123 template <
typename T,
124 typename = std::enable_if_t<std::numeric_limits<T>::is_integer ||
135 typename T,
typename ElementT =
typename T::value_type,
136 typename = std::enable_if_t<detail::is_complex_t<T>::value &&
137 (std::numeric_limits<ElementT>::is_integer ||
140 const char *data =
reinterpret_cast<const char *
>(values.data());
142 sizeof(T), std::numeric_limits<ElementT>::is_integer,
143 std::numeric_limits<ElementT>::is_signed);
164 ArrayRef<std::complex<APInt>> values);
177 ArrayRef<std::complex<APFloat>> values);
182 template <
typename T>
184 const std::initializer_list<T> &list) {
214 bool &detectedSplat);
221 template <
typename IteratorT>
225 template <
typename T,
typename AttrT = DenseElementsAttr>
226 using iterator = decltype(std::declval<AttrT>().
template value_begin<T>());
228 template <
typename T,
typename AttrT = DenseElementsAttr>
230 decltype(std::declval<AttrT>().
template getValues<T>());
252 template <
typename T>
289 APInt, APInt, APInt> {
308 ComplexIntElementIterator, std::complex<APInt>, std::complex<APInt>,
309 std::complex<APInt>> {
326 :
public llvm::mapped_iterator_base<FloatElementIterator,
327 IntElementIterator, APFloat> {
331 return APFloat(*smt, value);
339 :
BaseT(it), smt(&smt) {}
342 const llvm::fltSemantics *smt;
347 :
public llvm::mapped_iterator_base<ComplexFloatElementIterator,
348 ComplexIntElementIterator,
349 std::complex<APFloat>> {
352 std::complex<APFloat>
mapElement(
const std::complex<APInt> &value)
const {
353 return {APFloat(*smt, value.real()), APFloat(*smt, value.imag())};
362 :
BaseT(it), smt(&smt) {}
365 const llvm::fltSemantics *smt;
378 template <
typename T>
379 std::enable_if_t<!std::is_base_of<Attribute, T>::value ||
380 std::is_same<Attribute, T>::value,
383 assert(
isSplat() &&
"expected the attribute to be a splat");
384 return *value_begin<T>();
387 template <
typename T>
388 std::enable_if_t<std::is_base_of<Attribute, T>::value &&
389 !std::is_same<Attribute, T>::value,
392 return llvm::cast<T>(getSplatValue<Attribute>());
397 template <
typename T>
399 auto range = tryGetValues<T>();
400 using iterator = decltype(range->begin());
406 template <
typename T>
408 auto range = tryGetValues<T>();
409 using iterator = decltype(range->begin());
414 template <
typename T>
416 auto range = tryGetValues<T>();
417 assert(
succeeded(range) &&
"element type cannot be iterated");
418 return std::move(*range);
422 template <
typename T>
424 return getValues<T>().begin();
428 template <
typename T>
430 return getValues<T>().end();
435 template <
typename T>
437 std::enable_if_t<(!std::is_same<T, bool>::value &&
438 std::numeric_limits<T>::is_integer) ||
440 template <
typename T,
typename = IntFloatValueTemplateCheckT<T>>
443 std::numeric_limits<T>::is_signed))
453 template <
typename T,
typename ElementT>
455 std::enable_if_t<detail::is_complex_t<T>::value &&
456 (std::numeric_limits<ElementT>::is_integer ||
458 template <
typename T,
typename ElementT =
typename T::value_type,
461 if (!
isValidComplex(
sizeof(T), std::numeric_limits<ElementT>::is_integer,
462 std::numeric_limits<ElementT>::is_signed))
472 template <
typename T>
474 std::enable_if_t<std::is_same<T, StringRef>::value>;
475 template <
typename T,
typename = StringRefValueTemplateCheckT<T>>
479 const char *ptr =
reinterpret_cast<const char *
>(stringRefs.data());
487 template <
typename T>
489 std::enable_if_t<std::is_same<T, Attribute>::value>;
490 template <
typename T,
typename = AttributeValueTemplateCheckT<T>>
500 template <
typename T>
502 std::enable_if_t<std::is_base_of<Attribute, T>::value &&
503 !std::is_same<Attribute, T>::value>;
504 template <
typename T>
506 :
public llvm::mapped_iterator_base<DerivedAttributeElementIterator<T>,
507 AttributeElementIterator, T> {
508 using llvm::mapped_iterator_base<DerivedAttributeElementIterator<T>,
510 T>::mapped_iterator_base;
515 template <
typename T,
typename = DerivedAttrValueTemplateCheckT<T>>
520 getType(), DerivedIterT(value_begin<Attribute>()),
521 DerivedIterT(value_end<Attribute>()));
526 template <
typename T>
528 std::enable_if_t<std::is_same<T, bool>::value>;
529 template <
typename T,
typename = BoolValueTemplateCheckT<T>>
540 template <
typename T>
542 std::enable_if_t<std::is_same<T, APInt>::value>;
543 template <
typename T,
typename = APIntValueTemplateCheckT<T>>
553 template <
typename T>
555 std::enable_if_t<std::is_same<T, std::complex<APInt>>::value>;
556 template <
typename T,
typename = ComplexAPIntValueTemplateCheckT<T>>
564 template <
typename T>
566 std::enable_if_t<std::is_same<T, APFloat>::value>;
567 template <
typename T,
typename = APFloatValueTemplateCheckT<T>>
574 template <
typename T>
576 std::enable_if_t<std::is_same<T, std::complex<APFloat>>::value>;
577 template <
typename T,
typename = ComplexAPFloatValueTemplateCheckT<T>>
660 int64_t dataEltSize,
bool isInt,
668 int64_t dataEltSize,
bool isInt,
676 bool isValidComplex(int64_t dataEltSize,
bool isInt,
bool isSigned)
const;
683 using DenseElementsAttr::DenseElementsAttr;
687 auto denseAttr = llvm::dyn_cast<DenseElementsAttr>(attr);
688 return denseAttr && denseAttr.isSplat();
704 #define GET_ATTRDEF_CLASSES
705 #include "mlir/IR/BuiltinAttributes.h.inc"
718 template <
typename T>
721 using DenseArrayAttr::DenseArrayAttr;
735 void print(raw_ostream &os)
const;
749 extern template class DenseArrayAttrImpl<bool>;
750 extern template class DenseArrayAttrImpl<int8_t>;
751 extern template class DenseArrayAttrImpl<int16_t>;
752 extern template class DenseArrayAttrImpl<int32_t>;
753 extern template class DenseArrayAttrImpl<int64_t>;
754 extern template class DenseArrayAttrImpl<float>;
755 extern template class DenseArrayAttrImpl<double>;
773 template <
typename T>
776 using DenseResourceElementsAttr::DenseResourceElementsAttr;
788 std::optional<ArrayRef<T>> tryGetAsArrayRef()
const;
847 operator IntegerAttr()
const {
return IntegerAttr(
impl); }
848 operator TypedAttr()
const {
return IntegerAttr(
impl); }
851 bool getValue()
const;
865 using SymbolRefAttr::SymbolRefAttr;
883 StringAttr
getAttr()
const {
return getRootReference(); }
886 StringRef
getValue()
const {
return getAttr().getValue(); }
890 SymbolRefAttr refAttr = llvm::dyn_cast<SymbolRefAttr>(attr);
891 return refAttr && refAttr.getNestedReferences().empty();
896 using SymbolRefAttr::getNestedReferences;
909 using DenseIntOrFPElementsAttr::DenseIntOrFPElementsAttr;
913 template <
typename Arg>
915 return llvm::cast<DenseFPElementsAttr>(
918 template <
typename T>
920 const std::initializer_list<T> &list) {
927 mapValues(
Type newElementType,
950 using DenseIntOrFPElementsAttr::DenseIntOrFPElementsAttr;
954 template <
typename Arg>
956 return llvm::cast<DenseIntElementsAttr>(
959 template <
typename T>
961 const std::initializer_list<T> &list) {
982 template <
typename T>
983 auto SparseElementsAttr::try_value_begin_impl(OverloadToken<T>)
const
984 -> FailureOr<iterator<T>> {
985 auto zeroValue = getZeroValue<T>();
986 auto valueIt = getValues().try_value_begin<T>();
989 const std::vector<ptrdiff_t> flatSparseIndices(getFlattenedSparseIndices());
990 std::function<T(ptrdiff_t)> mapFn =
991 [flatSparseIndices{flatSparseIndices}, valueIt{std::move(*valueIt)},
992 zeroValue{std::move(zeroValue)}](ptrdiff_t index) {
994 for (
unsigned i = 0, e = flatSparseIndices.size(); i != e; ++i)
995 if (flatSparseIndices[i] == index)
996 return *std::next(valueIt, i);
1000 return iterator<T>(llvm::seq<ptrdiff_t>(0,
getNumElements()).begin(), mapFn);
1009 inline bool operator==(StringAttr lhs, std::nullptr_t) {
return !lhs; }
1011 return static_cast<bool>(lhs);
1016 inline bool operator!=(StringAttr lhs, StringAttr rhs) {
return !(lhs == rhs); }
1020 return lhs.getValue() == rhs;
1022 inline bool operator!=(StringAttr lhs, StringRef rhs) {
return !(lhs == rhs); }
1024 return rhs.getValue() == lhs;
1026 inline bool operator!=(StringRef lhs, StringAttr rhs) {
return !(lhs == rhs); }
1054 MLIRContext *context);
1064 return mlir::StringAttr::getFromOpaquePointer(pointer);
1068 return mlir::StringAttr::getFromOpaquePointer(pointer);
1072 struct PointerLikeTypeTraits<
mlir::StringAttr>
1073 :
public PointerLikeTypeTraits<mlir::Attribute> {
1075 return mlir::StringAttr::getFromOpaquePointer(p);
1080 struct PointerLikeTypeTraits<
mlir::IntegerAttr>
1081 :
public PointerLikeTypeTraits<mlir::Attribute> {
1083 return mlir::IntegerAttr::getFromOpaquePointer(p);
1088 struct PointerLikeTypeTraits<
mlir::SymbolRefAttr>
1089 :
public PointerLikeTypeTraits<mlir::Attribute> {
1091 return mlir::SymbolRefAttr::getFromOpaquePointer(ptr);
static int64_t getNumElements(ShapedType type)
This base class exposes generic asm parser hooks, usable across the various derived parsers.
This base class exposes generic asm printer hooks, usable across the various derived printers.
The following classes enable support for parsing and printing resources within MLIR assembly formats.
Attributes are known-constant values of operations.
constexpr Attribute()=default
Special case of IntegerAttr to represent boolean integers, i.e., signless i1 integers.
A utility iterator that allows walking over the internal Attribute values of a DenseElementsAttr.
A utility iterator that allows walking over the internal bool values.
bool operator*() const
Accesses the bool value at this iterator position.
Iterator for walking over complex APFloat values.
std::complex< APFloat > mapElement(const std::complex< APInt > &value) const
Map the element to the iterator result type.
A utility iterator that allows walking over the internal raw complex APInt values.
std::complex< APInt > operator*() const
Accesses the raw std::complex<APInt> value at this iterator position.
Iterator for walking raw element values of the specified type 'T', which may be any c++ data type mat...
const T & operator*() const
Accesses the raw value at this iterator position.
Iterator for walking over APFloat values.
APFloat mapElement(const APInt &value) const
Map the element to the iterator result type.
A utility iterator that allows walking over the internal raw APInt values.
APInt operator*() const
Accesses the raw APInt value at this iterator position.
An attribute that represents a reference to a dense vector or tensor object.
std::enable_if_t<!std::is_base_of< Attribute, T >::value||std::is_same< Attribute, T >::value, T > getSplatValue() const
Return the splat value for this attribute.
ArrayRef< StringRef > getRawStringData() const
Return the raw StringRef data held by this attribute.
IntElementIterator raw_int_begin() const
Iterators to various elements that require out-of-line definition.
static DenseElementsAttr getRawIntOrFloat(ShapedType type, ArrayRef< char > data, int64_t dataEltSize, bool isInt, bool isSigned)
Overload of the raw 'get' method that asserts that the given type is of integer or floating-point typ...
auto value_begin() const
Get an iterator of the given type to the start of the held element values.
static DenseElementsAttr getRawComplex(ShapedType type, ArrayRef< char > data, int64_t dataEltSize, bool isInt, bool isSigned)
Overload of the raw 'get' method that asserts that the given type is of complex type.
auto try_value_begin() const
Try to get an iterator of the given type to the start of the held element values.
static bool classof(Attribute attr)
Method for support type inquiry through isa, cast and dyn_cast.
std::enable_if_t< std::is_same< T, std::complex< APFloat > >::value > ComplexAPFloatValueTemplateCheckT
Try to get the held element values as a range of complex APFloat.
std::enable_if_t< std::is_same< T, APFloat >::value > APFloatValueTemplateCheckT
Try to get the held element values as a range of APFloat.
std::enable_if_t< std::is_same< T, StringRef >::value > StringRefValueTemplateCheckT
Try to get the held element values as a range of StringRef.
FailureOr< iterator_range_impl< BoolElementIterator > > tryGetValues() const
std::enable_if_t< std::is_same< T, Attribute >::value > AttributeValueTemplateCheckT
Try to get the held element values as a range of Attributes.
bool isValidComplex(int64_t dataEltSize, bool isInt, bool isSigned) const
FailureOr< iterator_range_impl< ComplexFloatElementIterator > > tryGetValues() const
auto getValues() const
Return the held element values as a range of the given type.
DenseElementsAttr resizeSplat(ShapedType newType)
Return a new DenseElementsAttr that has the same data as the current attribute, but with a different ...
int64_t getNumElements() const
Returns the number of elements held by this attribute.
static DenseElementsAttr getFromRawBuffer(ShapedType type, ArrayRef< char > rawBuffer)
Construct a dense elements attribute from a raw buffer representing the data for this attribute.
std::enable_if_t< std::is_base_of< Attribute, T >::value &&!std::is_same< Attribute, T >::value, T > getSplatValue() const
Return the splat value for derived attribute element types.
int64_t size() const
Returns the number of elements held by this attribute.
FailureOr< iterator_range_impl< ComplexIntElementIterator > > tryGetValues() const
bool isValidBool() const
Check the information for a C++ data type, check if this type is valid for the current attribute.
bool isSplat() const
Returns true if this attribute corresponds to a splat, i.e.
auto try_value_end() const
Try to get an iterator of the given type to the end of the held element values.
ArrayRef< char > getRawData() const
Return the raw storage data held by this attribute.
FailureOr< iterator_range_impl< DerivedAttributeElementIterator< T > > > tryGetValues() const
std::enable_if_t<(!std::is_same< T, bool >::value &&std::numeric_limits< T >::is_integer)||is_valid_cpp_fp_type< T >::value > IntFloatValueTemplateCheckT
Try to get the held element values as a range of integer or floating-point values.
FailureOr< iterator_range_impl< ElementIterator< T > > > tryGetValues() const
constexpr Attribute()=default
static DenseElementsAttr get(const ShapedType &type, ArrayRef< T > values)
Constructs a dense integer elements attribute from an array of integer or floating-point values.
FailureOr< iterator_range_impl< FloatElementIterator > > tryGetValues() const
DenseElementsAttr mapValues(Type newElementType, function_ref< APInt(const APInt &)> mapping) const
Generates a new DenseElementsAttr by mapping each int value to a new underlying APInt.
std::enable_if_t< detail::is_complex_t< T >::value &&(std::numeric_limits< ElementT >::is_integer||is_valid_cpp_fp_type< ElementT >::value)> ComplexValueTemplateCheckT
Try to get the held element values as a range of std::complex.
static DenseElementsAttr get(const ShapedType &type, const std::initializer_list< T > &list)
Construct a dense elements attribute for an initializer_list of values.
decltype(std::declval< AttrT >().template value_begin< T >()) iterator
The iterator for the given element type T.
decltype(std::declval< AttrT >().template getValues< T >()) iterator_range
The iterator range over the given element T.
Type getElementType() const
Return the element type of this DenseElementsAttr.
FailureOr< iterator_range_impl< ComplexFloatElementIterator > > tryGetComplexFloatValues() const
IntElementIterator raw_int_end() const
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
ShapedType getType() const
Return the type of this ElementsAttr, guaranteed to be a vector or tensor with static shape.
FailureOr< iterator_range_impl< FloatElementIterator > > tryGetFloatValues() const
static bool isValidRawBuffer(ShapedType type, ArrayRef< char > rawBuffer, bool &detectedSplat)
Returns true if the given buffer is a valid raw buffer for the given type.
FailureOr< iterator_range_impl< AttributeElementIterator > > tryGetValues() const
bool empty() const
Returns if the number of elements held by this attribute is 0.
FailureOr< iterator_range_impl< IntElementIterator > > tryGetValues() const
DenseElementsAttr bitcast(Type newElType)
Return a new DenseElementsAttr that has the same data as the current attribute, but has bitcast eleme...
static DenseElementsAttr get(const ShapedType &type, T value)
Constructs a dense integer elements attribute from a single element.
std::enable_if_t< std::is_same< T, bool >::value > BoolValueTemplateCheckT
Try to get the held element values as a range of bool.
bool isValidIntOrFloat(int64_t dataEltSize, bool isInt, bool isSigned) const
DenseElementsAttr reshape(ShapedType newType)
Return a new DenseElementsAttr that has the same data as the current attribute, but has been reshaped...
std::enable_if_t< std::is_same< T, APInt >::value > APIntValueTemplateCheckT
Try to get the held element values as a range of APInts.
std::enable_if_t< std::is_same< T, std::complex< APInt > >::value > ComplexAPIntValueTemplateCheckT
Try to get the held element values as a range of complex APInts.
FailureOr< iterator_range_impl< ElementIterator< StringRef > > > tryGetValues() const
FailureOr< iterator_range_impl< ComplexIntElementIterator > > tryGetComplexIntValues() const
auto value_end() const
Get an iterator of the given type to the end of the held element values.
static DenseElementsAttr get(const ShapedType &type, ArrayRef< T > values)
Constructs a dense complex elements attribute from an array of complex values.
FailureOr< iterator_range_impl< ElementIterator< T > > > tryGetValues() const
std::enable_if_t< std::is_base_of< Attribute, T >::value &&!std::is_same< Attribute, T >::value > DerivedAttrValueTemplateCheckT
Try to get the held element values a range of T, where T is a derived attribute type.
An attribute that represents a reference to a dense float vector or tensor object.
static DenseFPElementsAttr get(const ShapedType &type, const std::initializer_list< T > &list)
static DenseFPElementsAttr get(const ShapedType &type, Arg &&arg)
Get an instance of a DenseFPElementsAttr with the given arguments.
iterator begin() const
Iterator access to the float element values.
An attribute that represents a reference to a dense integer vector or tensor object.
static DenseIntElementsAttr get(const ShapedType &type, const std::initializer_list< T > &list)
iterator begin() const
Iterator access to the integer element values.
static DenseIntElementsAttr get(const ShapedType &type, Arg &&arg)
Get an instance of a DenseIntElementsAttr with the given arguments.
This class provides support for representing a failure result, or a valid value of type T.
A symbol reference with a reference path containing a single element.
static FlatSymbolRefAttr get(StringAttr value)
Construct a symbol reference for the given value name.
static FlatSymbolRefAttr get(MLIRContext *ctx, StringRef value)
static bool classof(Attribute attr)
Methods for support type inquiry through isa, cast, and dyn_cast.
static FlatSymbolRefAttr get(Operation *symbol)
Convenience getter for building a SymbolRefAttr based on an operation that implements the SymbolTrait...
StringRef getValue() const
Returns the name of the held symbol reference.
StringAttr getAttr() const
Returns the name of the held symbol reference as a StringAttr.
MLIRContext is the top-level object for a collection of MLIR operations.
Operation is the basic unit of execution within MLIR.
An attribute that represents a reference to a splat vector or tensor constant, meaning all of the ele...
static bool classof(Attribute attr)
Method for support type inquiry through isa, cast and dyn_cast.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
bool isInteger(unsigned width) const
Return true if this is an integer type with the specified width.
Base class for DenseArrayAttr that is instantiated and specialized for each supported element type be...
static DenseArrayAttrImpl get(MLIRContext *context, ArrayRef< T > content)
Builder from ArrayRef<T>.
static bool classof(Attribute attr)
Support for isa<>/cast<>.
ArrayRef< T > asArrayRef() const
void print(AsmPrinter &printer) const
Print the short form [42, 100, -1] without any type prefix.
static Attribute parse(AsmParser &parser, Type type)
Parse the short form [42, 100, -1] without any type prefix.
static Attribute parseWithoutBraces(AsmParser &parser, Type type)
Parse the short form 42, 100, -1 without any type prefix or braces.
void printWithoutBraces(raw_ostream &os) const
Print the short form 42, 100, -1 without any braces or type prefix.
T operator[](std::size_t index) const
Random access to elements.
Impl iterator for indexed DenseElementsAttr iterators that records a data pointer and data index that...
const char * getData() const
Return the data base pointer.
ptrdiff_t getDataIndex() const
Return the current index for this iterator, adjusted for the case of a splat.
DenseElementIndexedIteratorImpl(const char *data, bool isSplat, size_t dataIndex)
Base class for DenseResourceElementsAttr that is instantiated and specialized for each supported elem...
This class provides iterator utilities for an ElementsAttr range.
Include the generated interface declarations.
std::pair< const char *, bool > DenseIterPtrAndSplat
Pair of raw pointer and a boolean flag of whether the pointer holds a splat,.
This header declares functions that assit transformations in the MemRef dialect.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
bool operator==(StringAttr lhs, std::nullptr_t)
Define comparisons for StringAttr against nullptr and itself to avoid the StringRef overloads from be...
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value.
bool operator!=(StringAttr lhs, std::nullptr_t)
AffineExpr operator*(int64_t val, AffineExpr expr)
AffineMap makeStridedLinearLayoutMap(ArrayRef< int64_t > strides, int64_t offset, MLIRContext *context)
Given a list of strides (in which ShapedType::kDynamic represents a dynamic value),...
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
static mlir::StringAttr getTombstoneKey()
static mlir::StringAttr getEmptyKey()
static mlir::IntegerAttr getFromVoidPointer(void *p)
static mlir::StringAttr getFromVoidPointer(void *p)
static mlir::SymbolRefAttr getFromVoidPointer(void *ptr)
T mapElement(Attribute attr) const
Map the element to the iterator result type.
Type trait used to check if the given type T is a potentially valid C++ floating point type that can ...
static constexpr bool value
The type is a valid floating point type if it is a builtin floating point type, or is a potentially u...
This class defines a dialect specific handle to a resource blob.
Type trait detector that checks if a given type T is a complex type.