Add support for building a DenseIntElementsAttr with ArrayRef<int64_t> values.

PiperOrigin-RevId: 239616595
This commit is contained in:
River Riddle 2019-03-21 10:03:40 -07:00 committed by jpienaar
parent a19f22163d
commit f0b38058b1
4 changed files with 35 additions and 6 deletions

View File

@ -428,12 +428,18 @@ public:
using DenseElementsAttr::getValues;
using DenseElementsAttr::ImplType;
// 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'.
/// 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'.
static DenseIntElementsAttr get(VectorOrTensorType type,
ArrayRef<APInt> values);
/// Constructs a dense integer elements attribute from an array of integer
/// values. Each value is expected to be within the bitwidth of the element
/// type of 'type'.
static DenseIntElementsAttr get(VectorOrTensorType type,
ArrayRef<int64_t> values);
/// Gets the integer value of each of the dense elements.
void getValues(SmallVectorImpl<APInt> &values) const;

View File

@ -114,6 +114,8 @@ public:
ArrayRef<char> data);
ElementsAttr getDenseElementsAttr(VectorOrTensorType type,
ArrayRef<Attribute> values);
ElementsAttr getDenseIntElementsAttr(VectorOrTensorType type,
ArrayRef<int64_t> values);
ElementsAttr getSparseElementsAttr(VectorOrTensorType type,
DenseIntElementsAttr indices,
DenseElementsAttr values);

View File

@ -356,14 +356,30 @@ APInt DenseElementsAttr::readBits(const char *rawData, size_t bitPos,
/// DenseIntElementsAttr
// 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'.
/// 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'.
DenseIntElementsAttr DenseIntElementsAttr::get(VectorOrTensorType type,
ArrayRef<APInt> values) {
return DenseElementsAttr::get(type, values).cast<DenseIntElementsAttr>();
}
/// Constructs a dense integer elements attribute from an array of integer
/// values. Each value is expected to be within the bitwidth of the element
/// type of 'type'.
DenseIntElementsAttr DenseIntElementsAttr::get(VectorOrTensorType type,
ArrayRef<int64_t> values) {
auto eltType = type.getElementType();
size_t bitWidth = eltType.isBF16() ? 64 : eltType.getIntOrFloatBitWidth();
// Convert the raw integer values to APInt.
SmallVector<APInt, 8> apIntValues;
apIntValues.reserve(values.size());
for (auto value : values)
apIntValues.emplace_back(APInt(bitWidth, value));
return get(type, apIntValues);
}
void DenseIntElementsAttr::getValues(SmallVectorImpl<APInt> &values) const {
// Simply return the raw integer values.
getRawValues(values);

View File

@ -185,6 +185,11 @@ ElementsAttr Builder::getDenseElementsAttr(VectorOrTensorType type,
return DenseElementsAttr::get(type, values);
}
ElementsAttr Builder::getDenseIntElementsAttr(VectorOrTensorType type,
ArrayRef<int64_t> values) {
return DenseIntElementsAttr::get(type, values);
}
ElementsAttr Builder::getSparseElementsAttr(VectorOrTensorType type,
DenseIntElementsAttr indices,
DenseElementsAttr values) {