Skip to content

Commit ee40aef

Browse files
authored
Merge pull request #568 from Xilinx/jrickert.DenseElementsAttrBitStorageSize
Allow to use the DenseElementsAttr get methods to be used with types …
2 parents 7f76ec9 + 11a5e38 commit ee40aef

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

mlir/include/mlir/IR/BuiltinAttributes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ class DenseElementsAttr : public Attribute {
112112
static DenseElementsAttr get(ShapedType type, ArrayRef<Attribute> values);
113113

114114
/// 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.
115+
/// or floating-point values. Each value is expected to be the same as the
116+
/// storage bitwidth of the element type of 'type'. 'type' must be a vector or
117+
/// tensor with static shape.
118118
template <typename T,
119119
typename = std::enable_if_t<std::numeric_limits<T>::is_integer ||
120120
is_valid_cpp_fp_type<T>::value>>

mlir/lib/IR/BuiltinAttributes.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,13 +1104,15 @@ bool DenseElementsAttr::isValidRawBuffer(ShapedType type,
11041104
/// invariants that the templatized 'getValues' method cannot.
11051105
static bool isValidIntOrFloat(Type type, int64_t dataEltSize, bool isInt,
11061106
bool isSigned) {
1107-
// Make sure that the data element size is the same as the type element width.
1108-
auto denseEltBitWidth = getDenseElementBitWidth(type);
1109-
auto dataSize = static_cast<size_t>(dataEltSize * CHAR_BIT);
1110-
if (denseEltBitWidth != dataSize) {
1111-
LLVM_DEBUG(llvm::dbgs() << "expected dense element bit width "
1112-
<< denseEltBitWidth << " to match data size "
1113-
<< dataSize << " for type " << type << "\n");
1107+
// Make sure that the data element size is the same as the type element
1108+
// storage width.
1109+
const size_t denseEltStorageBitWidth = getDenseElementStorageWidth(type);
1110+
const size_t dataSizeBitWidth = static_cast<size_t>(dataEltSize * CHAR_BIT);
1111+
if (denseEltStorageBitWidth != dataSizeBitWidth) {
1112+
LLVM_DEBUG(llvm::dbgs()
1113+
<< "expected dense element bit width " << denseEltStorageBitWidth
1114+
<< " to match data size " << dataSizeBitWidth << " for type "
1115+
<< type << "\n");
11141116
return false;
11151117
}
11161118

mlir/unittests/IR/AttributeTest.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,4 +570,26 @@ TEST(NonSplattedDenseElementAttrTest, GetNonSplatRawDataI16) {
570570
.getNonSplatRawData<uint16_t>(),
571571
expected);
572572
}
573+
574+
TEST(NonSplattedDenseElementAttrTest, GetFromRawI7) {
575+
constexpr std::size_t numberOfElements = 6;
576+
static constexpr std::array<int8_t, numberOfElements> rawValues = {1, 2, 3,
577+
4, 5, 6};
578+
579+
mlir::MLIRContext context;
580+
mlir::OpBuilder b(&context);
581+
582+
auto values = mlir::DenseElementsAttr::get(
583+
mlir::RankedTensorType::get({numberOfElements}, b.getIntegerType(7)),
584+
ArrayRef<int8_t>(rawValues));
585+
auto fromRaw = mlir::DenseIntOrFPElementsAttr::getFromRawBuffer(
586+
values.getType(), values.getRawData());
587+
588+
EXPECT_EQ(values, fromRaw);
589+
EXPECT_EQ(fromRaw.getElementType(), b.getIntegerType(7));
590+
EXPECT_EQ(fromRaw.getNumElements(), numberOfElements);
591+
for (auto [fr, e] : llvm::zip_equal(fromRaw.getValues<int8_t>(), rawValues)) {
592+
EXPECT_EQ(fr, e);
593+
}
594+
}
573595
} // namespace

0 commit comments

Comments
 (0)