Skip to content

Commit a17ffca

Browse files
committed
fix signless integer
1 parent cfacea7 commit a17ffca

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

lib/Conversion/SCFToCalyx/SCFToCalyx.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -998,8 +998,8 @@ static LogicalResult buildAllocOp(ComponentLoweringState &componentState,
998998
componentState.registerMemoryInterface(allocOp.getResult(),
999999
calyx::MemoryInterface(memoryOp));
10001000

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");
10031003
bool isFloat = !memtype.getElementType().isInteger();
10041004

10051005
auto shape = allocOp.getType().getShape();
@@ -1031,12 +1031,12 @@ static LogicalResult buildAllocOp(ComponentLoweringState &componentState,
10311031
assert((isa<mlir::FloatAttr, mlir::IntegerAttr>(attr)) &&
10321032
"memory attributes must be float or int");
10331033
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());
10361036
} else {
10371037
auto intAttr = dyn_cast<mlir::IntegerAttr>(attr);
10381038
int64_t value = intAttr.getInt();
1039-
flattenedVals[sizeCount++] = static_cast<uint64_t>(value);
1039+
flattenedVals[sizeCount++] = bit_cast<uint64_t>(value);
10401040
}
10411041
}
10421042

@@ -1053,20 +1053,19 @@ static LogicalResult buildAllocOp(ComponentLoweringState &componentState,
10531053
}
10541054
}
10551055

1056-
bool isSigned = false;
1056+
bool isSignlessOrUnsigned = memtype.getElementType().isSignlessInteger() ||
1057+
memtype.getElementType().isUnsignedInteger();
10571058
for (size_t i = 0; i < flattenedVals.size(); ++i) {
10581059
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;
10621060
llvm::json::Value value = 0;
10631061
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);
10671065
} 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();
10701069
}
10711070
result[i] = std::move(value);
10721071
}
@@ -1077,7 +1076,8 @@ static LogicalResult buildAllocOp(ComponentLoweringState &componentState,
10771076
std::string numType =
10781077
memtype.getElementType().isInteger() ? "bitnum" : "ieee754_float";
10791078

1080-
componentState.setFormat(memoryOp.getName(), numType, isSigned, width);
1079+
componentState.setFormat(memoryOp.getName(), numType,
1080+
/*isSigned=*/!isSignlessOrUnsigned, width);
10811081

10821082
return success();
10831083
}

test/Conversion/SCFToCalyx/write_memory.mlir

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// CHECK: 0
99
// CHECK: ],
1010
// CHECK: "format": {
11-
// CHECK: "is_signed": false,
11+
// CHECK: "is_signed": true,
1212
// CHECK: "numeric_type": "ieee754_float",
1313
// CHECK: "width": 32
1414
// CHECK: }
@@ -19,7 +19,7 @@
1919
// CHECK: 0
2020
// CHECK: ],
2121
// CHECK: "format": {
22-
// CHECK: "is_signed": false,
22+
// CHECK: "is_signed": true,
2323
// CHECK: "numeric_type": "ieee754_float",
2424
// CHECK: "width": 32
2525
// CHECK: }
@@ -29,17 +29,17 @@
2929
// CHECK: "data": [
3030
// CHECK: 43,
3131
// CHECK: 8,
32-
// CHECK: -39,
33-
// CHECK: -19,
32+
// CHECK: 4294967257,
33+
// CHECK: 4294967277,
3434
// CHECK: 70,
35-
// CHECK: -64,
36-
// CHECK: -7,
37-
// CHECK: -27,
38-
// CHECK: -57,
35+
// CHECK: 4294967232,
36+
// CHECK: 4294967289,
37+
// CHECK: 4294967269,
38+
// CHECK: 4294967239,
3939
// CHECK: 5
4040
// CHECK: ],
4141
// CHECK: "format": {
42-
// CHECK: "is_signed": true,
42+
// CHECK: "is_signed": false,
4343
// CHECK: "numeric_type": "bitnum",
4444
// CHECK: "width": 32
4545
// CHECK: }

0 commit comments

Comments
 (0)