Skip to content

Commit e2c97d4

Browse files
committed
[MLIR] Add a bitcast method to DenseElementsAttr
This method bitcasts a DenseElementsAttr elementwise to one of the same shape with a different element type. Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D107612
1 parent fef39cc commit e2c97d4

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

mlir/include/mlir/IR/BuiltinAttributes.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,11 @@ class DenseElementsAttr : public ElementsAttr {
560560
/// same total number of elements as well as element type.
561561
DenseElementsAttr reshape(ShapedType newType);
562562

563+
/// Return a new DenseElementsAttr that has the same data as the current
564+
/// attribute, but has bitcast elements to 'newElType'. The new type must have
565+
/// the same bitwidth as the current element type.
566+
DenseElementsAttr bitcast(Type newElType);
567+
563568
/// Generates a new DenseElementsAttr by mapping each int value to a new
564569
/// underlying APInt. The new values can represent either an integer or float.
565570
/// This underlying type must be an DenseIntElementsAttr.

mlir/lib/Dialect/StandardOps/IR/Ops.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -512,16 +512,8 @@ OpFoldResult BitcastOp::fold(ArrayRef<Attribute> operands) {
512512

513513
Type resType = getResult().getType();
514514

515-
if (auto denseAttr = operand.dyn_cast<DenseFPElementsAttr>()) {
516-
Type elType = getElementTypeOrSelf(resType);
517-
return denseAttr.mapValues(
518-
elType, [](const APFloat &f) { return f.bitcastToAPInt(); });
519-
}
520-
if (auto denseAttr = operand.dyn_cast<DenseIntElementsAttr>()) {
521-
Type elType = getElementTypeOrSelf(resType);
522-
// mapValues does its own bitcast to the target type.
523-
return denseAttr.mapValues(elType, [](const APInt &i) { return i; });
524-
}
515+
if (auto denseAttr = operand.dyn_cast<DenseElementsAttr>())
516+
return denseAttr.bitcast(resType.cast<ShapedType>().getElementType());
525517

526518
APInt bits;
527519
if (auto floatAttr = operand.dyn_cast<FloatAttr>())

mlir/lib/IR/BuiltinAttributes.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,14 +1024,30 @@ DenseElementsAttr DenseElementsAttr::reshape(ShapedType newType) {
10241024
if (curType == newType)
10251025
return *this;
10261026

1027-
(void)curType;
10281027
assert(newType.getElementType() == curType.getElementType() &&
10291028
"expected the same element type");
10301029
assert(newType.getNumElements() == curType.getNumElements() &&
10311030
"expected the same number of elements");
10321031
return DenseIntOrFPElementsAttr::getRaw(newType, getRawData(), isSplat());
10331032
}
10341033

1034+
/// Return a new DenseElementsAttr that has the same data as the current
1035+
/// attribute, but has bitcast elements such that it is now 'newType'. The new
1036+
/// type must have the same shape and element types of the same bitwidth as the
1037+
/// current type.
1038+
DenseElementsAttr DenseElementsAttr::bitcast(Type newElType) {
1039+
ShapedType curType = getType();
1040+
Type curElType = curType.getElementType();
1041+
if (curElType == newElType)
1042+
return *this;
1043+
1044+
assert(getDenseElementBitWidth(newElType) ==
1045+
getDenseElementBitWidth(curElType) &&
1046+
"expected element types with the same bitwidth");
1047+
return DenseIntOrFPElementsAttr::getRaw(curType.clone(newElType),
1048+
getRawData(), isSplat());
1049+
}
1050+
10351051
DenseElementsAttr
10361052
DenseElementsAttr::mapValues(Type newElementType,
10371053
function_ref<APInt(const APInt &)> mapping) const {

0 commit comments

Comments
 (0)