File tree Expand file tree Collapse file tree 3 files changed +24
-11
lines changed Expand file tree Collapse file tree 3 files changed +24
-11
lines changed Original file line number Diff line number Diff line change @@ -560,6 +560,11 @@ class DenseElementsAttr : public ElementsAttr {
560
560
// / same total number of elements as well as element type.
561
561
DenseElementsAttr reshape (ShapedType newType);
562
562
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
+
563
568
// / Generates a new DenseElementsAttr by mapping each int value to a new
564
569
// / underlying APInt. The new values can represent either an integer or float.
565
570
// / This underlying type must be an DenseIntElementsAttr.
Original file line number Diff line number Diff line change @@ -512,16 +512,8 @@ OpFoldResult BitcastOp::fold(ArrayRef<Attribute> operands) {
512
512
513
513
Type resType = getResult ().getType ();
514
514
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 ());
525
517
526
518
APInt bits;
527
519
if (auto floatAttr = operand.dyn_cast <FloatAttr>())
Original file line number Diff line number Diff line change @@ -1024,14 +1024,30 @@ DenseElementsAttr DenseElementsAttr::reshape(ShapedType newType) {
1024
1024
if (curType == newType)
1025
1025
return *this ;
1026
1026
1027
- (void )curType;
1028
1027
assert (newType.getElementType () == curType.getElementType () &&
1029
1028
" expected the same element type" );
1030
1029
assert (newType.getNumElements () == curType.getNumElements () &&
1031
1030
" expected the same number of elements" );
1032
1031
return DenseIntOrFPElementsAttr::getRaw (newType, getRawData (), isSplat ());
1033
1032
}
1034
1033
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
+
1035
1051
DenseElementsAttr
1036
1052
DenseElementsAttr::mapValues (Type newElementType,
1037
1053
function_ref<APInt(const APInt &)> mapping) const {
You can’t perform that action at this time.
0 commit comments