@@ -998,8 +998,8 @@ static LogicalResult buildAllocOp(ComponentLoweringState &componentState,
998
998
componentState.registerMemoryInterface (allocOp.getResult (),
999
999
calyx::MemoryInterface (memoryOp));
1000
1000
1001
- assert ( memtype.getElementTypeBitWidth () <= 64 &&
1002
- " element bitwidth should not exceed 64" );
1001
+ unsigned elmTyBitWidth = memtype.getElementTypeBitWidth ();
1002
+ assert (elmTyBitWidth <= 64 && " element bitwidth should not exceed 64" );
1003
1003
bool isFloat = !memtype.getElementType ().isInteger ();
1004
1004
1005
1005
auto shape = allocOp.getType ().getShape ();
@@ -1031,12 +1031,12 @@ static LogicalResult buildAllocOp(ComponentLoweringState &componentState,
1031
1031
assert ((isa<mlir::FloatAttr, mlir::IntegerAttr>(attr)) &&
1032
1032
" memory attributes must be float or int" );
1033
1033
if (auto fltAttr = dyn_cast<mlir::FloatAttr>(attr)) {
1034
- double value = fltAttr. getValueAsDouble ();
1035
- std::memcpy (&flattenedVals[sizeCount++], &value, sizeof ( double ));
1034
+ flattenedVals[sizeCount++] =
1035
+ bit_cast< uint64_t >(fltAttr. getValueAsDouble ( ));
1036
1036
} else {
1037
1037
auto intAttr = dyn_cast<mlir::IntegerAttr>(attr);
1038
1038
int64_t value = intAttr.getInt ();
1039
- flattenedVals[sizeCount++] = static_cast <uint64_t >(value);
1039
+ flattenedVals[sizeCount++] = bit_cast <uint64_t >(value);
1040
1040
}
1041
1041
}
1042
1042
@@ -1053,20 +1053,19 @@ static LogicalResult buildAllocOp(ComponentLoweringState &componentState,
1053
1053
}
1054
1054
}
1055
1055
1056
- bool isSigned = false ;
1056
+ bool isSignlessOrUnsigned = memtype.getElementType ().isSignlessInteger () ||
1057
+ memtype.getElementType ().isUnsignedInteger ();
1057
1058
for (size_t i = 0 ; i < flattenedVals.size (); ++i) {
1058
1059
uint64_t bitValue = flattenedVals[i];
1059
- // checks whether the MSB of the `uint64_t` type `bitValue` is set
1060
- if (bitValue & (1ULL << 63 ))
1061
- isSigned = true ;
1062
1060
llvm::json::Value value = 0 ;
1063
1061
if (isFloat) {
1064
- double floatValue;
1065
- std::memcpy (&floatValue, &bitValue, sizeof ( double ));
1066
- value = floatValue ;
1062
+ // we cast to ` double` and let downstream calyx to deal with the actual
1063
+ // value's precision handling
1064
+ value = bit_cast< double >(bitValue) ;
1067
1065
} else {
1068
- int64_t intValue = static_cast <int64_t >(bitValue);
1069
- value = intValue;
1066
+ APInt apInt (/* numBits=*/ elmTyBitWidth, bitValue);
1067
+ value =
1068
+ isSignlessOrUnsigned ? apInt.getZExtValue () : apInt.getSExtValue ();
1070
1069
}
1071
1070
result[i] = std::move (value);
1072
1071
}
@@ -1077,7 +1076,8 @@ static LogicalResult buildAllocOp(ComponentLoweringState &componentState,
1077
1076
std::string numType =
1078
1077
memtype.getElementType ().isInteger () ? " bitnum" : " ieee754_float" ;
1079
1078
1080
- componentState.setFormat (memoryOp.getName (), numType, isSigned, width);
1079
+ componentState.setFormat (memoryOp.getName (), numType,
1080
+ /* isSigned=*/ !isSignlessOrUnsigned, width);
1081
1081
1082
1082
return success ();
1083
1083
}
0 commit comments