[Mlir-commits] [mlir] 3a7be24 - [mlir] Support big endian in DenseElementsAttr
Mehdi Amini
llvmlistbot at llvm.org
Mon May 4 15:17:20 PDT 2020
Author: Haruki Imai
Date: 2020-05-04T22:17:05Z
New Revision: 3a7be241f252215e8a2cf26e8a301fd2a8269b58
URL: https://github.com/llvm/llvm-project/commit/3a7be241f252215e8a2cf26e8a301fd2a8269b58
DIFF: https://github.com/llvm/llvm-project/commit/3a7be241f252215e8a2cf26e8a301fd2a8269b58.diff
LOG: [mlir] Support big endian in DenseElementsAttr
This std::copy_n copies 8 byte data (APInt raw data) by 1 byte from the
beginning of char array. This is no problem in little endian, but the
data is not copied correctly in big endian because the data should be
copied from the end of the char array.
- Example of 4 byte data (such as float32)
Little endian (First 4 bytes):
Address | 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08
Data | 0xcd 0xcc 0x8c 0x3f 0x00 0x00 0x00 0x00
Big endian (Last 4 bytes):
Address | 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08
Data | 0x00 0x00 0x00 0x00 0x3f 0x8c 0xcc 0xcd
In general, when it copies N(N<8) byte data in big endian, the start
address should be incremented by (8 - N) bytes.
The original code has no problem when it includes 8 byte data(such as
double) even in big endian.
Differential Revision: https://reviews.llvm.org/D78076
Added:
Modified:
mlir/lib/IR/Attributes.cpp
Removed:
################################################################################
diff --git a/mlir/lib/IR/Attributes.cpp b/mlir/lib/IR/Attributes.cpp
index f7540f6feffd..5cb2d5a2ea84 100644
--- a/mlir/lib/IR/Attributes.cpp
+++ b/mlir/lib/IR/Attributes.cpp
@@ -16,6 +16,7 @@
#include "mlir/IR/Types.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/Twine.h"
+#include "llvm/Support/Endian.h"
using namespace mlir;
using namespace mlir::detail;
@@ -550,6 +551,29 @@ static bool getBit(const char *rawData, size_t bitPos) {
return (rawData[bitPos / CHAR_BIT] & (1 << (bitPos % CHAR_BIT))) != 0;
}
+/// 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<char *>(reinterpret_cast<const char *>(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();
@@ -560,9 +584,7 @@ static void writeBits(char *rawData, size_t bitPos, APInt value) {
// Otherwise, the bit position is guaranteed to be byte aligned.
assert((bitPos % CHAR_BIT) == 0 && "expected bitPos to be 8-bit aligned");
- std::copy_n(reinterpret_cast<const char *>(value.getRawData()),
- 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
@@ -575,9 +597,7 @@ static APInt readBits(const char *rawData, size_t bitPos, size_t bitWidth) {
// 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);
- std::copy_n(
- rawData + (bitPos / CHAR_BIT), llvm::divideCeil(bitWidth, CHAR_BIT),
- const_cast<char *>(reinterpret_cast<const char *>(result.getRawData())));
+ writeAPInt(rawData + (bitPos / CHAR_BIT), bitWidth, result);
return result;
}
More information about the Mlir-commits
mailing list