diff --git a/mlir/lib/IR/Attributes.cpp b/mlir/lib/IR/Attributes.cpp --- a/mlir/lib/IR/Attributes.cpp +++ b/mlir/lib/IR/Attributes.cpp @@ -524,15 +524,29 @@ return (rawData[bitPos / CHAR_BIT] & (1 << (bitPos % CHAR_BIT))) != 0; } -/// Get start position of actual data in `rawData`. Actual data is -/// stored in last `bitWidth`/`CHAR_BIT` bytes in big endian. -static const char *getDataStartPos(const char *dataPos, size_t bitWidth) { +/// Get start position of actual data in `value`. Actual data is +/// stored in last `bitWidth`/CHAR_BIT bytes in big endian. +static char *getAPIntDataPos(APInt &value, size_t bitWidth) { + char *dataPos = + const_cast(reinterpret_cast(value.getRawData())); if (llvm::support::endian::system_endianness() == llvm::support::endianness::big) dataPos = dataPos + 8 - llvm::divideCeil(bitWidth, CHAR_BIT); return dataPos; } +/// Read APInt `value` from appropriate position. +static void readAPInt(APInt &value, size_t bitWidth, char *outData) { + char *dataPos = getAPIntDataPos(value, bitWidth); + std::copy_n(dataPos, llvm::divideCeil(bitWidth, CHAR_BIT), outData); +} + +/// Write `inData` to appropriate position of APInt `value`. +static void writeAPInt(const char *inData, size_t bitWidth, APInt &value) { + char *dataPos = getAPIntDataPos(value, bitWidth); + std::copy_n(inData, llvm::divideCeil(bitWidth, CHAR_BIT), dataPos); +} + /// Writes value to the bit position `bitPos` in array `rawData`. static void writeBits(char *rawData, size_t bitPos, APInt value) { size_t bitWidth = value.getBitWidth(); @@ -543,10 +557,7 @@ // Otherwise, the bit position is guaranteed to be byte aligned. assert((bitPos % CHAR_BIT) == 0 && "expected bitPos to be 8-bit aligned"); - const char *dataPos = reinterpret_cast(value.getRawData()); - dataPos = getDataStartPos(dataPos, bitWidth); - std::copy_n(dataPos, llvm::divideCeil(bitWidth, CHAR_BIT), - rawData + (bitPos / CHAR_BIT)); + readAPInt(value, bitWidth, rawData + (bitPos / CHAR_BIT)); } /// Reads the next `bitWidth` bits from the bit position `bitPos` in array @@ -559,11 +570,7 @@ // Otherwise, the bit position must be 8-bit aligned. assert((bitPos % CHAR_BIT) == 0 && "expected bitPos to be 8-bit aligned"); APInt result(bitWidth, 0); - const char *dataPos = reinterpret_cast(result.getRawData()); - dataPos = getDataStartPos(dataPos, bitWidth); - std::copy_n(rawData + (bitPos / CHAR_BIT), - llvm::divideCeil(bitWidth, CHAR_BIT), - const_cast(dataPos)); + writeAPInt(rawData + (bitPos / CHAR_BIT), bitWidth, result); return result; }