MLIR 23.0.0git
BuiltinAttributes.h
Go to the documentation of this file.
1//===- BuiltinAttributes.h - MLIR Builtin Attribute Classes -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef MLIR_IR_BUILTINATTRIBUTES_H
10#define MLIR_IR_BUILTINATTRIBUTES_H
11
13#include "llvm/ADT/APFloat.h"
14#include "llvm/ADT/Sequence.h"
15#include <complex>
16#include <optional>
17
18namespace mlir {
19class AffineMap;
20class AsmResourceBlob;
21class BoolAttr;
22class BuiltinDialect;
24template <typename T>
27class FunctionType;
28class IntegerSet;
29class IntegerType;
30class Location;
31class Operation;
32class RankedTensorType;
33
34namespace detail {
38} // namespace detail
39
40//===----------------------------------------------------------------------===//
41// Elements Attributes
42//===----------------------------------------------------------------------===//
43
44namespace detail {
45/// Pair of raw pointer and a boolean flag of whether the pointer holds a splat,
46using DenseIterPtrAndSplat = std::pair<const char *, bool>;
47
48/// Impl iterator for indexed DenseElementsAttr iterators that records a data
49/// pointer and data index that is adjusted for the case of a splat attribute.
50template <typename ConcreteT, typename T, typename PointerT = T *,
51 typename ReferenceT = T &>
53 : public llvm::indexed_accessor_iterator<ConcreteT, DenseIterPtrAndSplat, T,
54 PointerT, ReferenceT> {
55protected:
56 DenseElementIndexedIteratorImpl(const char *data, bool isSplat,
57 size_t dataIndex)
58 : llvm::indexed_accessor_iterator<ConcreteT, DenseIterPtrAndSplat, T,
59 PointerT, ReferenceT>({data, isSplat},
60 dataIndex) {}
61
62 /// Return the current index for this iterator, adjusted for the case of a
63 /// splat.
64 ptrdiff_t getDataIndex() const {
65 bool isSplat = this->base.second;
66 return isSplat ? 0 : this->index;
67 }
68
69 /// Return the data base pointer.
70 const char *getData() const { return this->base.first; }
71};
72
73/// Type trait detector that checks if a given type T is a complex type.
74template <typename T>
75struct is_complex_t : public std::false_type {};
76template <typename T>
77struct is_complex_t<std::complex<T>> : public std::true_type {};
78} // namespace detail
79
80/// An attribute that represents a reference to a dense vector or tensor
81/// object.
83public:
85
86 /// Allow implicit conversion to ElementsAttr.
87 operator ElementsAttr() const { return cast_if_present<ElementsAttr>(*this); }
88 /// Allow implicit conversion to TypedAttr.
89 operator TypedAttr() const { return ElementsAttr(*this); }
90
91 /// Type trait used to check if the given type T is a potentially valid C++
92 /// floating point type that can be used to access the underlying element
93 /// types of a DenseElementsAttr.
94 template <typename T>
96 /// The type is a valid floating point type if it is a builtin floating
97 /// point type, or is a potentially user defined floating point type. The
98 /// latter allows for supporting users that have custom types defined for
99 /// bfloat16/half/etc.
100 static constexpr bool value = llvm::is_one_of<T, float, double>::value ||
101 (std::numeric_limits<T>::is_specialized &&
102 !std::numeric_limits<T>::is_integer);
103 };
104
105 /// Method for support type inquiry through isa, cast and dyn_cast.
106 static bool classof(Attribute attr);
107
108 /// Constructs a dense elements attribute from an array of element values.
109 /// Each element attribute value is expected to be an element of 'type'.
110 /// 'type' must be a vector or tensor with static shape. If the element of
111 /// `type` is non-integer/index/float it is assumed to be a string type.
112 static DenseElementsAttr get(ShapedType type, ArrayRef<Attribute> values);
113
114 /// Constructs a dense integer elements attribute from an array of integer
115 /// or floating-point values. Each value is expected to be the same bitwidth
116 /// of the element type of 'type'. 'type' must be a vector or tensor with
117 /// static shape.
118 template <typename T,
119 typename = std::enable_if_t<std::numeric_limits<T>::is_integer ||
121 static DenseElementsAttr get(const ShapedType &type, ArrayRef<T> values) {
122 const char *data = reinterpret_cast<const char *>(values.data());
123 return getRawIntOrFloat(
124 type, ArrayRef<char>(data, values.size() * sizeof(T)), sizeof(T),
125 std::numeric_limits<T>::is_integer, std::numeric_limits<T>::is_signed);
126 }
127
128 /// Constructs a dense integer elements attribute from a single element.
129 template <typename T,
130 typename = std::enable_if_t<std::numeric_limits<T>::is_integer ||
133 static DenseElementsAttr get(const ShapedType &type, T value) {
134 return get(type, llvm::ArrayRef(value));
135 }
136
137 /// Constructs a dense complex elements attribute from an array of complex
138 /// values. Each value is expected to be the same bitwidth of the element type
139 /// of 'type'. 'type' must be a vector or tensor with static shape.
140 template <
141 typename T, typename ElementT = typename T::value_type,
142 typename = std::enable_if_t<detail::is_complex_t<T>::value &&
143 (std::numeric_limits<ElementT>::is_integer ||
145 static DenseElementsAttr get(const ShapedType &type, ArrayRef<T> values) {
146 const char *data = reinterpret_cast<const char *>(values.data());
147 return getRawComplex(type, ArrayRef<char>(data, values.size() * sizeof(T)),
148 sizeof(T), std::numeric_limits<ElementT>::is_integer,
149 std::numeric_limits<ElementT>::is_signed);
150 }
151
152 /// Overload of the above 'get' method that is specialized for boolean values.
153 static DenseElementsAttr get(ShapedType type, ArrayRef<bool> values);
154
155 /// Overload of the above 'get' method that is specialized for StringRef
156 /// values.
157 static DenseElementsAttr get(ShapedType type, ArrayRef<StringRef> values);
158
159 /// Constructs a dense integer elements attribute from an array of APInt
160 /// values. Each APInt value is expected to have the same bitwidth as the
161 /// element type of 'type'. 'type' must be a vector or tensor with static
162 /// shape.
163 static DenseElementsAttr get(ShapedType type, ArrayRef<APInt> values);
164
165 /// Constructs a dense complex elements attribute from an array of APInt
166 /// values. Each APInt value is expected to have the same bitwidth as the
167 /// element type of 'type'. 'type' must be a vector or tensor with static
168 /// shape.
169 static DenseElementsAttr get(ShapedType type,
170 ArrayRef<std::complex<APInt>> values);
171
172 /// Constructs a dense float elements attribute from an array of APFloat
173 /// values. Each APFloat value is expected to have the same bitwidth as the
174 /// element type of 'type'. 'type' must be a vector or tensor with static
175 /// shape.
176 static DenseElementsAttr get(ShapedType type, ArrayRef<APFloat> values);
177
178 /// Constructs a dense complex elements attribute from an array of APFloat
179 /// values. Each APFloat value is expected to have the same bitwidth as the
180 /// element type of 'type'. 'type' must be a vector or tensor with static
181 /// shape.
182 static DenseElementsAttr get(ShapedType type,
183 ArrayRef<std::complex<APFloat>> values);
184
185 /// Construct a dense elements attribute for an initializer_list of values.
186 /// Each value is expected to be the same bitwidth of the element type of
187 /// 'type'. 'type' must be a vector or tensor with static shape.
188 template <typename T>
189 static DenseElementsAttr get(const ShapedType &type,
190 const std::initializer_list<T> &list) {
191 return get(type, ArrayRef<T>(list));
192 }
193
194 /// Construct a dense elements attribute from a raw buffer representing the
195 /// data for this attribute. Users are encouraged to use one of the
196 /// constructors above, which provide more safeties. However, this
197 /// constructor is useful for tools which may want to interop and can
198 /// follow the precise definition.
199 ///
200 /// The format of the raw buffer is a densely packed array of values that
201 /// can be bitcast to the storage format of the element type specified.
202 /// Types that are not byte aligned will be rounded up to the next byte.
203 static DenseElementsAttr getFromRawBuffer(ShapedType type,
204 ArrayRef<char> rawBuffer);
205
206 /// Returns true if the given buffer is a valid raw buffer for the given type.
207 static bool isValidRawBuffer(ShapedType type, ArrayRef<char> rawBuffer);
208
209 //===--------------------------------------------------------------------===//
210 // Iterators
211 //===--------------------------------------------------------------------===//
212
213 /// The iterator range over the given iterator type T.
214 template <typename IteratorT>
216
217 /// The iterator for the given element type T.
218 template <typename T, typename AttrT = DenseElementsAttr>
219 using iterator = decltype(std::declval<AttrT>().template value_begin<T>());
220 /// The iterator range over the given element T.
221 template <typename T, typename AttrT = DenseElementsAttr>
223 decltype(std::declval<AttrT>().template getValues<T>());
224
225 /// A utility iterator that allows walking over the internal Attribute values
226 /// of a DenseElementsAttr.
227 class AttributeElementIterator
228 : public llvm::indexed_accessor_iterator<AttributeElementIterator,
229 const void *, Attribute,
231 public:
232 /// Accesses the Attribute value at this iterator position.
233 Attribute operator*() const;
234
235 private:
236 friend DenseElementsAttr;
237
238 /// Constructs a new iterator.
239 AttributeElementIterator(DenseElementsAttr attr, size_t index);
240 };
241
242 /// Iterator for walking raw element values of the specified type 'T', which
243 /// may be any c++ data type matching the stored representation: int32_t,
244 /// float, etc.
245 template <typename T>
246 class ElementIterator
247 : public detail::DenseElementIndexedIteratorImpl<ElementIterator<T>,
248 const T> {
249 public:
250 /// Accesses the raw value at this iterator position.
251 const T &operator*() const {
252 return reinterpret_cast<const T *>(this->getData())[this->getDataIndex()];
253 }
254
255 private:
256 friend DenseElementsAttr;
257
258 /// Constructs a new iterator.
259 ElementIterator(const char *data, bool isSplat, size_t dataIndex)
261 data, isSplat, dataIndex) {}
262 };
263
264 /// A utility iterator that allows walking over the internal bool values.
265 class BoolElementIterator
266 : public detail::DenseElementIndexedIteratorImpl<BoolElementIterator,
267 bool, bool, bool> {
268 public:
269 /// Accesses the bool value at this iterator position.
270 bool operator*() const;
271
272 private:
273 friend DenseElementsAttr;
274
275 /// Constructs a new iterator.
276 BoolElementIterator(DenseElementsAttr attr, size_t dataIndex);
277 };
278
279 /// A utility iterator that allows walking over the internal raw APInt values.
280 class IntElementIterator
281 : public detail::DenseElementIndexedIteratorImpl<IntElementIterator,
282 APInt, APInt, APInt> {
283 public:
284 /// Accesses the raw APInt value at this iterator position.
285 APInt operator*() const;
286
287 private:
288 friend DenseElementsAttr;
289
290 /// Constructs a new iterator.
291 IntElementIterator(DenseElementsAttr attr, size_t dataIndex);
292
293 /// The bitwidth of the element type.
294 size_t bitWidth;
295 };
296
297 /// A utility iterator that allows walking over the internal raw complex APInt
298 /// values.
299 class ComplexIntElementIterator
301 ComplexIntElementIterator, std::complex<APInt>, std::complex<APInt>,
302 std::complex<APInt>> {
303 public:
304 /// Accesses the raw std::complex<APInt> value at this iterator position.
305 std::complex<APInt> operator*() const;
306
307 private:
308 friend DenseElementsAttr;
309
310 /// Constructs a new iterator.
311 ComplexIntElementIterator(DenseElementsAttr attr, size_t dataIndex);
312
313 /// The bitwidth of the element type.
314 size_t bitWidth;
315 };
316
317 /// Iterator for walking over APFloat values.
318 class FloatElementIterator final
319 : public llvm::mapped_iterator_base<FloatElementIterator,
320 IntElementIterator, APFloat> {
321 public:
322 /// Map the element to the iterator result type.
323 APFloat mapElement(const APInt &value) const {
324 return APFloat(*smt, value);
325 }
326
327 private:
328 friend DenseElementsAttr;
329
330 /// Initializes the float element iterator to the specified iterator.
331 FloatElementIterator(const llvm::fltSemantics &smt, IntElementIterator it)
332 : BaseT(it), smt(&smt) {}
333
334 /// The float semantics to use when constructing the APFloat.
335 const llvm::fltSemantics *smt;
336 };
337
338 /// Iterator for walking over complex APFloat values.
339 class ComplexFloatElementIterator final
340 : public llvm::mapped_iterator_base<ComplexFloatElementIterator,
341 ComplexIntElementIterator,
342 std::complex<APFloat>> {
343 public:
344 /// Map the element to the iterator result type.
345 std::complex<APFloat> mapElement(const std::complex<APInt> &value) const {
346 return {APFloat(*smt, value.real()), APFloat(*smt, value.imag())};
347 }
348
349 private:
350 friend DenseElementsAttr;
351
352 /// Initializes the float element iterator to the specified iterator.
353 ComplexFloatElementIterator(const llvm::fltSemantics &smt,
355 : BaseT(it), smt(&smt) {}
356
357 /// The float semantics to use when constructing the APFloat.
358 const llvm::fltSemantics *smt;
359 };
360
361 //===--------------------------------------------------------------------===//
362 // Value Querying
363 //===--------------------------------------------------------------------===//
364
365 /// Returns true if this attribute corresponds to a splat, i.e. if all element
366 /// values are the same.
367 bool isSplat() const;
368
369 /// Return the splat value for this attribute. This asserts that the attribute
370 /// corresponds to a splat.
371 template <typename T>
372 std::enable_if_t<!std::is_base_of<Attribute, T>::value ||
373 std::is_same<Attribute, T>::value,
374 T>
376 assert(isSplat() && "expected the attribute to be a splat");
377 return *value_begin<T>();
378 }
379 /// Return the splat value for derived attribute element types.
380 template <typename T>
381 std::enable_if_t<std::is_base_of<Attribute, T>::value &&
382 !std::is_same<Attribute, T>::value,
383 T>
385 return llvm::cast<T>(getSplatValue<Attribute>());
386 }
387
388 /// Try to get an iterator of the given type to the start of the held element
389 /// values. Return failure if the type cannot be iterated.
390 template <typename T>
391 auto try_value_begin() const {
392 auto range = tryGetValues<T>();
393 using iterator = decltype(range->begin());
394 return failed(range) ? FailureOr<iterator>(failure()) : range->begin();
395 }
396
397 /// Try to get an iterator of the given type to the end of the held element
398 /// values. Return failure if the type cannot be iterated.
399 template <typename T>
400 auto try_value_end() const {
401 auto range = tryGetValues<T>();
402 using iterator = decltype(range->begin());
403 return failed(range) ? FailureOr<iterator>(failure()) : range->end();
404 }
405
406 /// Return the held element values as a range of the given type.
407 template <typename T>
408 auto getValues() const {
409 auto range = tryGetValues<T>();
410 assert(succeeded(range) && "element type cannot be iterated");
411 return std::move(*range);
412 }
413
414 /// Get an iterator of the given type to the start of the held element values.
415 template <typename T>
416 auto value_begin() const {
417 return getValues<T>().begin();
418 }
419
420 /// Get an iterator of the given type to the end of the held element values.
421 template <typename T>
422 auto value_end() const {
423 return getValues<T>().end();
424 }
425
426 /// Try to get the held element values as a range of integer or floating-point
427 /// values.
428 template <typename T>
430 std::enable_if_t<(!std::is_same<T, bool>::value &&
431 std::numeric_limits<T>::is_integer) ||
433 template <typename T, typename = IntFloatValueTemplateCheckT<T>>
434 FailureOr<iterator_range_impl<ElementIterator<T>>> tryGetValues() const {
435 if (!isValidIntOrFloat(sizeof(T), std::numeric_limits<T>::is_integer,
436 std::numeric_limits<T>::is_signed))
437 return failure();
438 const char *rawData = getRawData().data();
439 bool splat = isSplat();
441 getType(), ElementIterator<T>(rawData, splat, 0),
442 ElementIterator<T>(rawData, splat, getNumElements()));
443 }
444
445 /// Try to get the held element values as a range of std::complex.
446 template <typename T, typename ElementT>
448 std::enable_if_t<detail::is_complex_t<T>::value &&
449 (std::numeric_limits<ElementT>::is_integer ||
451 template <typename T, typename ElementT = typename T::value_type,
453 FailureOr<iterator_range_impl<ElementIterator<T>>> tryGetValues() const {
454 if (!isValidComplex(sizeof(T), std::numeric_limits<ElementT>::is_integer,
455 std::numeric_limits<ElementT>::is_signed))
456 return failure();
457 const char *rawData = getRawData().data();
458 bool splat = isSplat();
460 getType(), ElementIterator<T>(rawData, splat, 0),
461 ElementIterator<T>(rawData, splat, getNumElements()));
462 }
463
464 /// Try to get the held element values as a range of StringRef.
465 template <typename T>
467 std::enable_if_t<std::is_same<T, StringRef>::value>;
468 template <typename T, typename = StringRefValueTemplateCheckT<T>>
469 FailureOr<iterator_range_impl<ElementIterator<StringRef>>>
470 tryGetValues() const {
471 auto stringRefs = getRawStringData();
472 const char *ptr = reinterpret_cast<const char *>(stringRefs.data());
473 bool splat = isSplat();
477 }
478
479 /// Try to get the held element values as a range of Attributes.
480 template <typename T>
482 std::enable_if_t<std::is_same<T, Attribute>::value>;
483 template <typename T, typename = AttributeValueTemplateCheckT<T>>
484 FailureOr<iterator_range_impl<AttributeElementIterator>>
490
491 /// Try to get the held element values a range of T, where T is a derived
492 /// attribute type.
493 template <typename T>
495 std::enable_if_t<std::is_base_of<Attribute, T>::value &&
496 !std::is_same<Attribute, T>::value>;
497 template <typename T>
499 : public llvm::mapped_iterator_base<DerivedAttributeElementIterator<T>,
500 AttributeElementIterator, T> {
501 using llvm::mapped_iterator_base<DerivedAttributeElementIterator<T>,
503 T>::mapped_iterator_base;
504
505 /// Map the element to the iterator result type.
506 T mapElement(Attribute attr) const { return llvm::cast<T>(attr); }
507 };
508 template <typename T, typename = DerivedAttrValueTemplateCheckT<T>>
509 FailureOr<iterator_range_impl<DerivedAttributeElementIterator<T>>>
510 tryGetValues() const {
511 using DerivedIterT = DerivedAttributeElementIterator<T>;
513 getType(), DerivedIterT(value_begin<Attribute>()),
514 DerivedIterT(value_end<Attribute>()));
515 }
516
517 /// Try to get the held element values as a range of bool. The element type of
518 /// this attribute must be of integer type of bitwidth 1.
519 template <typename T>
521 std::enable_if_t<std::is_same<T, bool>::value>;
522 template <typename T, typename = BoolValueTemplateCheckT<T>>
523 FailureOr<iterator_range_impl<BoolElementIterator>> tryGetValues() const {
524 if (!isValidBool())
525 return failure();
527 getType(), BoolElementIterator(*this, 0),
529 }
530
531 /// Try to get the held element values as a range of APInts. The element type
532 /// of this attribute must be of integer type.
533 template <typename T>
535 std::enable_if_t<std::is_same<T, APInt>::value>;
536 template <typename T, typename = APIntValueTemplateCheckT<T>>
537 FailureOr<iterator_range_impl<IntElementIterator>> tryGetValues() const {
538 if (!getElementType().isIntOrIndex())
539 return failure();
541 raw_int_end());
542 }
543
544 /// Try to get the held element values as a range of complex APInts. The
545 /// element type of this attribute must be a complex of integer type.
546 template <typename T>
548 std::enable_if_t<std::is_same<T, std::complex<APInt>>::value>;
549 template <typename T, typename = ComplexAPIntValueTemplateCheckT<T>>
550 FailureOr<iterator_range_impl<ComplexIntElementIterator>>
551 tryGetValues() const {
552 return tryGetComplexIntValues();
553 }
554
555 /// Try to get the held element values as a range of APFloat. The element type
556 /// of this attribute must be of float type.
557 template <typename T>
559 std::enable_if_t<std::is_same<T, APFloat>::value>;
560 template <typename T, typename = APFloatValueTemplateCheckT<T>>
561 FailureOr<iterator_range_impl<FloatElementIterator>> tryGetValues() const {
562 return tryGetFloatValues();
563 }
564
565 /// Try to get the held element values as a range of complex APFloat. The
566 /// element type of this attribute must be a complex of float type.
567 template <typename T>
569 std::enable_if_t<std::is_same<T, std::complex<APFloat>>::value>;
570 template <typename T, typename = ComplexAPFloatValueTemplateCheckT<T>>
571 FailureOr<iterator_range_impl<ComplexFloatElementIterator>>
572 tryGetValues() const {
574 }
575
576 /// Return the raw storage data held by this attribute. Users should generally
577 /// not use this directly, as the internal storage format is not always in the
578 /// form the user might expect.
580
581 /// Return the raw StringRef data held by this attribute.
583
584 /// Return the type of this ElementsAttr, guaranteed to be a vector or tensor
585 /// with static shape.
586 ShapedType getType() const;
587
588 /// Return the element type of this DenseElementsAttr.
589 Type getElementType() const;
590
591 /// Returns the number of elements held by this attribute.
592 int64_t getNumElements() const;
593
594 /// Returns the number of elements held by this attribute.
595 int64_t size() const { return getNumElements(); }
596
597 /// Returns if the number of elements held by this attribute is 0.
598 bool empty() const { return size() == 0; }
599
600 //===--------------------------------------------------------------------===//
601 // Mutation Utilities
602 //===--------------------------------------------------------------------===//
603
604 /// Return a new DenseElementsAttr that has the same data as the current
605 /// attribute, but has been reshaped to 'newType'. The new type must have the
606 /// same total number of elements as well as element type.
607 DenseElementsAttr reshape(ShapedType newType);
608
609 /// Return a new DenseElementsAttr that has the same data as the current
610 /// attribute, but with a different shape for a splat type. The new type must
611 /// have the same element type.
612 DenseElementsAttr resizeSplat(ShapedType newType);
613
614 /// Return a new DenseElementsAttr that has the same data as the current
615 /// attribute, but has bitcast elements to 'newElType'. The new type must have
616 /// the same bitwidth as the current element type.
617 DenseElementsAttr bitcast(Type newElType);
618
619 /// Generates a new DenseElementsAttr by mapping each int value to a new
620 /// underlying APInt. The new values can represent either an integer or float.
621 /// This underlying type must be an DenseIntElementsAttr.
622 DenseElementsAttr mapValues(Type newElementType,
623 function_ref<APInt(const APInt &)> mapping) const;
624
625 /// Generates a new DenseElementsAttr by mapping each float value to a new
626 /// underlying APInt. the new values can represent either an integer or float.
627 /// This underlying type must be an DenseFPElementsAttr.
629 mapValues(Type newElementType,
630 function_ref<APInt(const APFloat &)> mapping) const;
631
632protected:
633 /// Iterators to various elements that require out-of-line definition. These
634 /// are hidden from the user to encourage consistent use of the
635 /// getValues/value_begin/value_end API.
637 return IntElementIterator(*this, 0);
638 }
642 FailureOr<iterator_range_impl<ComplexIntElementIterator>>
644 FailureOr<iterator_range_impl<FloatElementIterator>>
645 tryGetFloatValues() const;
646 FailureOr<iterator_range_impl<ComplexFloatElementIterator>>
648
649 /// Overload of the raw 'get' method that asserts that the given type is of
650 /// complex type. This method is used to verify type invariants that the
651 /// templatized 'get' method cannot.
652 static DenseElementsAttr getRawComplex(ShapedType type, ArrayRef<char> data,
653 int64_t dataEltSize, bool isInt,
654 bool isSigned);
655
656 /// Overload of the raw 'get' method that asserts that the given type is of
657 /// integer or floating-point type. This method is used to verify type
658 /// invariants that the templatized 'get' method cannot.
659 static DenseElementsAttr getRawIntOrFloat(ShapedType type,
660 ArrayRef<char> data,
661 int64_t dataEltSize, bool isInt,
662 bool isSigned);
663
664 /// Check the information for a C++ data type, check if this type is valid for
665 /// the current attribute. This method is used to verify specific type
666 /// invariants that the templatized 'getValues' method cannot.
667 bool isValidBool() const { return getElementType().isInteger(1); }
668 bool isValidIntOrFloat(int64_t dataEltSize, bool isInt, bool isSigned) const;
669 bool isValidComplex(int64_t dataEltSize, bool isInt, bool isSigned) const;
670};
671
672/// An attribute that represents a reference to a splat vector or tensor
673/// constant, meaning all of the elements have the same value.
675public:
676 using DenseElementsAttr::DenseElementsAttr;
677
678 /// Method for support type inquiry through isa, cast and dyn_cast.
679 static bool classof(Attribute attr) {
680 auto denseAttr = llvm::dyn_cast<DenseElementsAttr>(attr);
681 return denseAttr && denseAttr.isSplat();
682 }
683};
684
685//===----------------------------------------------------------------------===//
686// DenseResourceElementsAttr
687//===----------------------------------------------------------------------===//
688
690
691} // namespace mlir
692
693//===----------------------------------------------------------------------===//
694// Tablegen Attribute Declarations
695//===----------------------------------------------------------------------===//
696
697#define GET_ATTRDEF_CLASSES
698#include "mlir/IR/BuiltinAttributes.h.inc"
699
700//===----------------------------------------------------------------------===//
701// C++ Attribute Declarations
702//===----------------------------------------------------------------------===//
703
704namespace mlir {
705/// DenseIntOrFPElementsAttr was renamed to DenseTypedElementsAttr. This alias
706/// is provided for backwards compatibility. It will be removed in the future.
707using DenseIntOrFPElementsAttr [[deprecated(
708 "DenseIntOrFPElementsAttr has been renamed to DenseTypedElementsAttr")]] =
710
711//===----------------------------------------------------------------------===//
712// DenseArrayAttr
713//===----------------------------------------------------------------------===//
714
715namespace detail {
716/// Base class for DenseArrayAttr that is instantiated and specialized for each
717/// supported element type below.
718template <typename T>
719class DenseArrayAttrImpl : public DenseArrayAttr {
720public:
721 using DenseArrayAttr::DenseArrayAttr;
722
723 /// Implicit conversion to ArrayRef<T>.
724 operator ArrayRef<T>() const;
725 ArrayRef<T> asArrayRef() const { return ArrayRef<T>{*this}; }
726
727 /// Random access to elements.
728 T operator[](std::size_t index) const { return asArrayRef()[index]; }
729
730 /// Builder from ArrayRef<T>.
732
733 /// Print the short form `[42, 100, -1]` without any type prefix.
734 void print(AsmPrinter &printer) const;
735 void print(raw_ostream &os) const;
736 /// Print the short form `42, 100, -1` without any braces or type prefix.
738
739 /// Parse the short form `[42, 100, -1]` without any type prefix.
740 static Attribute parse(AsmParser &parser, Type type);
741
742 /// Parse the short form `42, 100, -1` without any type prefix or braces.
744
745 /// Support for isa<>/cast<>.
746 static bool classof(Attribute attr);
747};
748
749extern template class DenseArrayAttrImpl<bool>;
750extern template class DenseArrayAttrImpl<int8_t>;
751extern template class DenseArrayAttrImpl<int16_t>;
752extern template class DenseArrayAttrImpl<int32_t>;
753extern template class DenseArrayAttrImpl<int64_t>;
754extern template class DenseArrayAttrImpl<float>;
755extern template class DenseArrayAttrImpl<double>;
756} // namespace detail
757
758// Public name for all the supported DenseArrayAttr
766
767//===----------------------------------------------------------------------===//
768// DenseResourceElementsAttr
769//===----------------------------------------------------------------------===//
770
771namespace detail {
772/// Base class for DenseResourceElementsAttr that is instantiated and
773/// specialized for each supported element type below.
774template <typename T>
775class DenseResourceElementsAttrBase : public DenseResourceElementsAttr {
776public:
777 using DenseResourceElementsAttr::DenseResourceElementsAttr;
778
779 /// A builder that inserts a new resource using the provided blob. The handle
780 /// of the inserted blob is used when building the attribute. The provided
781 /// `blobName` is used as a hint for the key of the new handle for the `blob`
782 /// resource, but may be changed if necessary to ensure uniqueness during
783 /// insertion.
785 get(ShapedType type, StringRef blobName, AsmResourceBlob blob);
786
787 /// Return the data of this attribute as an ArrayRef<T> if it is present,
788 /// returns std::nullopt otherwise.
789 std::optional<ArrayRef<T>> tryGetAsArrayRef() const;
790
791 /// Support for isa<>/cast<>.
792 static bool classof(Attribute attr);
793};
794
795extern template class DenseResourceElementsAttrBase<bool>;
796extern template class DenseResourceElementsAttrBase<int8_t>;
797extern template class DenseResourceElementsAttrBase<int16_t>;
798extern template class DenseResourceElementsAttrBase<int32_t>;
799extern template class DenseResourceElementsAttrBase<int64_t>;
800extern template class DenseResourceElementsAttrBase<uint8_t>;
801extern template class DenseResourceElementsAttrBase<uint16_t>;
802extern template class DenseResourceElementsAttrBase<uint32_t>;
803extern template class DenseResourceElementsAttrBase<uint64_t>;
804extern template class DenseResourceElementsAttrBase<float>;
805extern template class DenseResourceElementsAttrBase<double>;
806} // namespace detail
807
808// Public names for all the supported DenseResourceElementsAttr.
809
832
833//===----------------------------------------------------------------------===//
834// BoolAttr
835//===----------------------------------------------------------------------===//
836
837/// Special case of IntegerAttr to represent boolean integers, i.e., signless i1
838/// integers.
839class BoolAttr : public Attribute {
840public:
843
844 static BoolAttr get(MLIRContext *context, bool value);
845
846 /// Enable conversion to IntegerAttr and its interfaces. This uses conversion
847 /// vs. inheritance to avoid bringing in all of IntegerAttrs methods.
848 operator IntegerAttr() const { return IntegerAttr(impl); }
849 operator TypedAttr() const { return IntegerAttr(impl); }
850
851 /// Return the boolean value of this attribute.
852 bool getValue() const;
853
854 /// Methods for support type inquiry through isa, cast, and dyn_cast.
855 static bool classof(Attribute attr);
856};
857
858//===----------------------------------------------------------------------===//
859// FlatSymbolRefAttr
860//===----------------------------------------------------------------------===//
861
862/// A symbol reference with a reference path containing a single element. This
863/// is used to refer to an operation within the current symbol table.
864class FlatSymbolRefAttr : public SymbolRefAttr {
865public:
866 using SymbolRefAttr::SymbolRefAttr;
867 using ValueType = StringRef;
868
869 /// Construct a symbol reference for the given value name.
870 static FlatSymbolRefAttr get(StringAttr value) {
871 return SymbolRefAttr::get(value);
872 }
873 static FlatSymbolRefAttr get(MLIRContext *ctx, StringRef value) {
874 return SymbolRefAttr::get(ctx, value);
875 }
876
877 /// Convenience getter for building a SymbolRefAttr based on an operation
878 /// that implements the SymbolTrait.
880 return SymbolRefAttr::get(symbol);
881 }
882
883 /// Returns the name of the held symbol reference as a StringAttr.
884 StringAttr getAttr() const { return getRootReference(); }
885
886 /// Returns the name of the held symbol reference.
887 StringRef getValue() const { return getAttr().getValue(); }
888
889 /// Methods for support type inquiry through isa, cast, and dyn_cast.
890 static bool classof(Attribute attr) {
891 SymbolRefAttr refAttr = llvm::dyn_cast<SymbolRefAttr>(attr);
892 return refAttr && refAttr.getNestedReferences().empty();
893 }
894
895private:
896 using SymbolRefAttr::get;
897 using SymbolRefAttr::getNestedReferences;
898};
899
900//===----------------------------------------------------------------------===//
901// DenseFPElementsAttr
902//===----------------------------------------------------------------------===//
903
904/// An attribute that represents a reference to a dense float vector or tensor
905/// object. Each element is stored as a double.
907public:
909
910 using DenseTypedElementsAttr::DenseTypedElementsAttr;
911
912 /// Get an instance of a DenseFPElementsAttr with the given arguments. This
913 /// simply wraps the DenseElementsAttr::get calls.
914 template <typename Arg>
915 static DenseFPElementsAttr get(const ShapedType &type, Arg &&arg) {
916 return llvm::cast<DenseFPElementsAttr>(
918 }
919 template <typename T>
920 static DenseFPElementsAttr get(const ShapedType &type,
921 const std::initializer_list<T> &list) {
922 return llvm::cast<DenseFPElementsAttr>(DenseElementsAttr::get(type, list));
923 }
924
925 /// Generates a new DenseElementsAttr by mapping each value attribute, and
926 /// constructing the DenseElementsAttr given the new element type.
928 mapValues(Type newElementType,
929 function_ref<APInt(const APFloat &)> mapping) const;
930
931 /// Iterator access to the float element values.
932 iterator begin() const { return tryGetFloatValues()->begin(); }
933 iterator end() const { return tryGetFloatValues()->end(); }
934
935 /// Method for supporting type inquiry through isa, cast and dyn_cast.
936 static bool classof(Attribute attr);
937};
938
939//===----------------------------------------------------------------------===//
940// DenseIntElementsAttr
941//===----------------------------------------------------------------------===//
942
943/// An attribute that represents a reference to a dense integer vector or tensor
944/// object.
946public:
947 /// DenseIntElementsAttr iterates on APInt, so we can use the raw element
948 /// iterator directly.
950
951 using DenseTypedElementsAttr::DenseTypedElementsAttr;
952
953 /// Get an instance of a DenseIntElementsAttr with the given arguments. This
954 /// simply wraps the DenseElementsAttr::get calls.
955 template <typename Arg>
956 static DenseIntElementsAttr get(const ShapedType &type, Arg &&arg) {
957 return llvm::cast<DenseIntElementsAttr>(
959 }
960 template <typename T>
961 static DenseIntElementsAttr get(const ShapedType &type,
962 const std::initializer_list<T> &list) {
963 return llvm::cast<DenseIntElementsAttr>(DenseElementsAttr::get(type, list));
964 }
965
966 /// Generates a new DenseElementsAttr by mapping each value attribute, and
967 /// constructing the DenseElementsAttr given the new element type.
968 DenseElementsAttr mapValues(Type newElementType,
969 function_ref<APInt(const APInt &)> mapping) const;
970
971 /// Iterator access to the integer element values.
972 iterator begin() const { return raw_int_begin(); }
973 iterator end() const { return raw_int_end(); }
974
975 /// Method for supporting type inquiry through isa, cast and dyn_cast.
976 static bool classof(Attribute attr);
977};
978
979//===----------------------------------------------------------------------===//
980// SparseElementsAttr
981//===----------------------------------------------------------------------===//
982
983template <typename T>
984auto SparseElementsAttr::try_value_begin_impl(OverloadToken<T>) const
985 -> FailureOr<iterator<T>> {
986 auto zeroValue = getZeroValue<T>();
987 auto valueIt = getValues().try_value_begin<T>();
988 if (failed(valueIt))
989 return failure();
990 const SmallVector<ptrdiff_t> flatSparseIndices(getFlattenedSparseIndices());
991 std::function<T(ptrdiff_t)> mapFn =
992 [flatSparseIndices{flatSparseIndices}, valueIt{std::move(*valueIt)},
993 zeroValue{std::move(zeroValue)}](ptrdiff_t index) {
994 // Try to map the current index to one of the sparse indices.
995 for (unsigned i = 0, e = flatSparseIndices.size(); i != e; ++i)
996 if (flatSparseIndices[i] == index)
997 return *std::next(valueIt, i);
998 // Otherwise, return the zero value.
999 return zeroValue;
1000 };
1001 return iterator<T>(llvm::seq<ptrdiff_t>(0, getNumElements()).begin(), mapFn);
1002}
1003
1004//===----------------------------------------------------------------------===//
1005// DistinctAttr
1006//===----------------------------------------------------------------------===//
1007
1008namespace detail {
1009struct DistinctAttrStorage;
1011} // namespace detail
1012
1013/// An attribute that associates a referenced attribute with a unique
1014/// identifier. Every call to the create function allocates a new distinct
1015/// attribute instance. The address of the attribute instance serves as a
1016/// temporary identifier. Similar to the names of SSA values, the final
1017/// identifiers are generated during pretty printing. This delayed numbering
1018/// ensures the printed identifiers are deterministic even if multiple distinct
1019/// attribute instances are created in-parallel.
1020///
1021/// Examples:
1022///
1023/// #distinct = distinct[0]<42.0 : f32>
1024/// #distinct1 = distinct[1]<42.0 : f32>
1025/// #distinct2 = distinct[2]<array<i32: 10, 42>>
1026///
1027/// NOTE: The distinct attribute cannot be defined using ODS since it uses a
1028/// custom distinct attribute uniquer that cannot be set from ODS.
1030 : public detail::StorageUserBase<DistinctAttr, Attribute,
1031 detail::DistinctAttrStorage,
1032 detail::DistinctAttributeUniquer> {
1033public:
1034 using Base::Base;
1035
1036 /// Returns the referenced attribute.
1038
1039 /// Creates a distinct attribute that associates a referenced attribute with a
1040 /// unique identifier.
1041 static DistinctAttr create(Attribute referencedAttr);
1042
1043 static constexpr StringLiteral name = "builtin.distinct";
1044};
1045
1046//===----------------------------------------------------------------------===//
1047// StringAttr
1048//===----------------------------------------------------------------------===//
1049
1050/// Define comparisons for StringAttr against nullptr and itself to avoid the
1051/// StringRef overloads from being chosen when not desirable.
1052inline bool operator==(StringAttr lhs, std::nullptr_t) { return !lhs; }
1053inline bool operator!=(StringAttr lhs, std::nullptr_t) {
1054 return static_cast<bool>(lhs);
1055}
1056inline bool operator==(StringAttr lhs, StringAttr rhs) {
1057 return (Attribute)lhs == (Attribute)rhs;
1058}
1059inline bool operator!=(StringAttr lhs, StringAttr rhs) { return !(lhs == rhs); }
1060
1061/// Allow direct comparison with StringRef.
1062inline bool operator==(StringAttr lhs, StringRef rhs) {
1063 return lhs.getValue() == rhs;
1064}
1065inline bool operator!=(StringAttr lhs, StringRef rhs) { return !(lhs == rhs); }
1066inline bool operator==(StringRef lhs, StringAttr rhs) {
1067 return rhs.getValue() == lhs;
1068}
1069inline bool operator!=(StringRef lhs, StringAttr rhs) { return !(lhs == rhs); }
1070
1071} // namespace mlir
1072
1073//===----------------------------------------------------------------------===//
1074// Attribute Utilities
1075//===----------------------------------------------------------------------===//
1076
1077namespace mlir {
1078
1079/// Given a list of strides (in which ShapedType::kDynamic
1080/// represents a dynamic value), return the single result AffineMap which
1081/// represents the linearized strided layout map. Dimensions correspond to the
1082/// offset followed by the strides in order. Symbols are inserted for each
1083/// dynamic dimension in order. A stride is always positive.
1084///
1085/// Examples:
1086/// =========
1087///
1088/// 1. For offset: 0 strides: ?, ?, 1 return
1089/// (i, j, k)[M, N]->(M * i + N * j + k)
1090///
1091/// 2. For offset: 3 strides: 32, ?, 16 return
1092/// (i, j, k)[M]->(3 + 32 * i + M * j + 16 * k)
1093///
1094/// 3. For offset: ? strides: ?, ?, ? return
1095/// (i, j, k)[off, M, N, P]->(off + M * i + N * j + P * k)
1097 MLIRContext *context);
1098
1099} // namespace mlir
1100
1101namespace llvm {
1102
1103template <>
1104struct DenseMapInfo<mlir::StringAttr> : public DenseMapInfo<mlir::Attribute> {
1105 static mlir::StringAttr getEmptyKey() {
1106 const void *pointer = llvm::DenseMapInfo<const void *>::getEmptyKey();
1107 return mlir::StringAttr::getFromOpaquePointer(pointer);
1108 }
1109 static mlir::StringAttr getTombstoneKey() {
1111 return mlir::StringAttr::getFromOpaquePointer(pointer);
1112 }
1113};
1114template <>
1115struct PointerLikeTypeTraits<mlir::StringAttr>
1116 : public PointerLikeTypeTraits<mlir::Attribute> {
1117 static inline mlir::StringAttr getFromVoidPointer(void *p) {
1118 return mlir::StringAttr::getFromOpaquePointer(p);
1119 }
1120};
1121
1122template <>
1123struct PointerLikeTypeTraits<mlir::IntegerAttr>
1124 : public PointerLikeTypeTraits<mlir::Attribute> {
1125 static inline mlir::IntegerAttr getFromVoidPointer(void *p) {
1126 return mlir::IntegerAttr::getFromOpaquePointer(p);
1127 }
1128};
1129
1130template <>
1131struct PointerLikeTypeTraits<mlir::SymbolRefAttr>
1132 : public PointerLikeTypeTraits<mlir::Attribute> {
1133 static inline mlir::SymbolRefAttr getFromVoidPointer(void *ptr) {
1134 return mlir::SymbolRefAttr::getFromOpaquePointer(ptr);
1135 }
1136};
1137
1138} // namespace llvm
1139
1140#endif // MLIR_IR_BUILTINATTRIBUTES_H
lhs
static int64_t getNumElements(Type t)
Compute the total number of elements in the given type, also taking into account nested types.
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition AffineMap.h:46
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.
This class represents a processed binary blob of data.
Definition AsmState.h:91
Attributes are known-constant values of operations.
Definition Attributes.h:25
constexpr Attribute()=default
Special case of IntegerAttr to represent boolean integers, i.e., signless i1 integers.
constexpr Attribute()=default
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.
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...
FailureOr< iterator_range_impl< AttributeElementIterator > > tryGetValues() const
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.
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.
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.
bool isValidComplex(int64_t dataEltSize, bool isInt, bool isSigned) const
FailureOr< iterator_range_impl< ElementIterator< T > > > 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 ...
decltype(std::declval< AttrT >().template getValues< T >()) iterator_range
The iterator range over the given element T.
detail::ElementsAttrRange< IteratorT > iterator_range_impl
The iterator range over the given iterator type T.
int64_t getNumElements() const
Returns the number of elements held by 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 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.
static bool isValidRawBuffer(ShapedType type, ArrayRef< char > rawBuffer)
Returns true if the given buffer is a valid raw buffer for the given type.
int64_t size() const
Returns the number of elements held by this attribute.
std::enable_if_t< std::is_same< T, Attribute >::value > AttributeValueTemplateCheckT
Try to get the held element values as a range of Attributes.
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.
constexpr Attribute()=default
FailureOr< iterator_range_impl< ElementIterator< T > > > tryGetValues() const
static DenseElementsAttr get(const ShapedType &type, ArrayRef< T > values)
Constructs a dense integer elements attribute from an array of integer or floating-point values.
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< std::is_same< T, std::complex< APFloat > >::value > ComplexAPFloatValueTemplateCheckT
Try to get the held element values as a range of complex APFloat.
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.
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.
Type getElementType() const
Return the element type of this DenseElementsAttr.
FailureOr< iterator_range_impl< ComplexFloatElementIterator > > tryGetComplexFloatValues() const
std::enable_if_t< std::is_same< T, StringRef >::value > StringRefValueTemplateCheckT
Try to get the held element values as a range of StringRef.
IntElementIterator raw_int_end() 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.
static DenseElementsAttr get(ShapedType type, ArrayRef< Attribute > values)
Constructs a dense elements attribute from an array of element values.
std::enable_if_t< std::is_same< T, APFloat >::value > APFloatValueTemplateCheckT
Try to get the held element values as a range of APFloat.
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
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.
std::enable_if_t< std::is_same< T, bool >::value > BoolValueTemplateCheckT
Try to get the held element values as a range of bool.
bool empty() const
Returns if the number of elements held by this attribute is 0.
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_base_of< Attribute, T >::value &&!std::is_same< Attribute, T >::value, T > getSplatValue() const
Return the splat value for derived attribute element types.
bool isValidIntOrFloat(int64_t dataEltSize, bool isInt, bool isSigned) const
FailureOr< iterator_range_impl< ElementIterator< StringRef > > > tryGetValues() const
FailureOr< iterator_range_impl< IntElementIterator > > tryGetValues() const
DenseElementsAttr reshape(ShapedType newType)
Return a new DenseElementsAttr that has the same data as the current attribute, but has been reshaped...
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.
std::enable_if_t< std::is_same< T, APInt >::value > APIntValueTemplateCheckT
Try to get the held element values as a range of APInts.
FailureOr< iterator_range_impl< BoolElementIterator > > tryGetValues() const
FailureOr< iterator_range_impl< DerivedAttributeElementIterator< T > > > tryGetValues() const
FailureOr< iterator_range_impl< ComplexFloatElementIterator > > tryGetValues() const
FailureOr< iterator_range_impl< ComplexIntElementIterator > > tryGetValues() const
FailureOr< iterator_range_impl< FloatElementIterator > > tryGetValues() const
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)
DenseElementsAttr::FloatElementIterator iterator
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)
DenseElementsAttr::IntElementIterator iterator
DenseIntElementsAttr iterates on APInt, so we can use the raw element iterator directly.
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.
An attribute that associates a referenced attribute with a unique identifier.
static constexpr StringLiteral name
static DistinctAttr create(Attribute referencedAttr)
Creates a distinct attribute that associates a referenced attribute with a unique identifier.
Attribute getReferencedAttr() const
Returns the referenced attribute.
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.
An integer set representing a conjunction of one or more affine equalities and inequalities.
Definition IntegerSet.h:44
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
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...
Definition Types.h:74
bool isInteger() const
Return true if this is an integer type (with the specified width).
Definition Types.cpp:58
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<>.
void print(raw_ostream &os) 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...
ptrdiff_t getDataIndex() const
Return the current index for this iterator, adjusted for the case of a splat.
const char * getData() const
Return the data base pointer.
DenseElementIndexedIteratorImpl(const char *data, bool isSplat, size_t dataIndex)
Base class for DenseResourceElementsAttr that is instantiated and specialized for each supported elem...
static bool classof(Attribute attr)
Support for isa<>/cast<>.
static DenseResourceElementsAttrBase< T > get(ShapedType type, StringRef blobName, AsmResourceBlob blob)
A builder that inserts a new resource using the provided blob.
std::optional< ArrayRef< T > > tryGetAsArrayRef() const
Return the data of this attribute as an ArrayRef<T> if it is present, returns std::nullopt otherwise.
A specialized attribute uniquer for distinct attributes that always allocates since the distinct attr...
This class provides iterator utilities for an ElementsAttr range.
Utility class for implementing users of storage classes uniqued by a StorageUniquer.
StorageUserBase< ConcreteT, BaseT, StorageT, UniquerT, Traits... > Base
Utility declarations for the concrete attribute class.
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
AttrTypeReplacer.
std::pair< const char *, bool > DenseIterPtrAndSplat
Pair of raw pointer and a boolean flag of whether the pointer holds a splat,.
Include the generated interface declarations.
detail::DenseArrayAttrImpl< int64_t > DenseI64ArrayAttr
detail::DenseResourceElementsAttrBase< uint64_t > DenseUI64ResourceElementsAttr
detail::DenseResourceElementsAttrBase< int32_t > DenseI32ResourceElementsAttr
detail::DenseResourceElementsAttrBase< uint32_t > DenseUI32ResourceElementsAttr
detail::DenseArrayAttrImpl< int8_t > DenseI8ArrayAttr
DialectResourceBlobHandle< BuiltinDialect > DenseResourceElementsHandle
detail::DenseResourceElementsAttrBase< float > DenseF32ResourceElementsAttr
bool operator==(StringAttr lhs, std::nullptr_t)
Define comparisons for StringAttr against nullptr and itself to avoid the StringRef overloads from be...
detail::DenseResourceElementsAttrBase< bool > DenseBoolResourceElementsAttr
bool operator!=(RegionBranchPoint lhs, RegionBranchPoint rhs)
detail::DenseResourceElementsAttrBase< int16_t > DenseI16ResourceElementsAttr
detail::DenseResourceElementsAttrBase< int64_t > DenseI64ResourceElementsAttr
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr
detail::DenseResourceElementsAttrBase< uint16_t > DenseUI16ResourceElementsAttr
AffineExpr operator*(int64_t val, AffineExpr expr)
Definition AffineExpr.h:252
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...
detail::DenseArrayAttrImpl< double > DenseF64ArrayAttr
detail::DenseArrayAttrImpl< bool > DenseBoolArrayAttr
detail::DenseResourceElementsAttrBase< int8_t > DenseI8ResourceElementsAttr
detail::DenseArrayAttrImpl< float > DenseF32ArrayAttr
detail::DenseResourceElementsAttrBase< uint8_t > DenseUI8ResourceElementsAttr
detail::DenseArrayAttrImpl< int16_t > DenseI16ArrayAttr
llvm::function_ref< Fn > function_ref
Definition LLVM.h:147
detail::DenseResourceElementsAttrBase< double > DenseF64ResourceElementsAttr
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.
An attribute representing a reference to a dense vector or tensor object containing strings.
An attribute representing a reference to a dense vector or tensor object.
An attribute to store a distinct reference to another attribute.
Type trait detector that checks if a given type T is a complex type.