From bb0bbed610d86ba155f9c066c23038f7f34e2dbb Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Fri, 23 Jun 2023 09:17:53 +0200 Subject: Fix bytecode reader/writer on big-endian platforms This makes the bytecode reader/writer work on big-endian platforms. The only problem was related to encoding of multi-byte integers, where both reader and writer code make implicit assumptions about endianness of the host platform. This fixes the current test failures on s390x, and in addition allows to remove the UNSUPPORTED markers from all other bytecode-related test cases - they now also all pass on s390x. Also adding a GFAIL_SKIP to the MultiModuleWithResource unit test, as this still fails due to an unrelated endian bug regarding decoding of external resources. Differential Revision: https://reviews.llvm.org/D153567 Reviewed By: mehdi_amini, jpienaar, rriddle --- mlir/lib/Bytecode/Reader/BytecodeReader.cpp | 18 ++++++++++++------ mlir/lib/Bytecode/Writer/BytecodeWriter.cpp | 7 +++++-- 2 files changed, 17 insertions(+), 8 deletions(-) (limited to 'mlir/lib/Bytecode') diff --git a/mlir/lib/Bytecode/Reader/BytecodeReader.cpp b/mlir/lib/Bytecode/Reader/BytecodeReader.cpp index 177372c68046..5aa24ba873ec 100644 --- a/mlir/lib/Bytecode/Reader/BytecodeReader.cpp +++ b/mlir/lib/Bytecode/Reader/BytecodeReader.cpp @@ -6,8 +6,6 @@ // //===----------------------------------------------------------------------===// -// TODO: Support for big-endian architectures. - #include "mlir/Bytecode/BytecodeReader.h" #include "mlir/AsmParser/AsmParser.h" #include "mlir/Bytecode/BytecodeImplementation.h" @@ -27,6 +25,7 @@ #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" +#include "llvm/Support/Endian.h" #include "llvm/Support/MemoryBufferRef.h" #include "llvm/Support/SaveAndRestore.h" #include "llvm/Support/SourceMgr.h" @@ -203,8 +202,14 @@ public: // Handle the overwhelming uncommon case where the value required all 8 // bytes (i.e. a really really big number). In this case, the marker byte is // all zeros: `00000000`. - if (LLVM_UNLIKELY(result == 0)) - return parseBytes(sizeof(result), reinterpret_cast(&result)); + if (LLVM_UNLIKELY(result == 0)) { + llvm::support::ulittle64_t resultLE; + if (failed(parseBytes(sizeof(resultLE), + reinterpret_cast(&resultLE)))) + return failure(); + result = resultLE; + return success(); + } return parseMultiByteVarInt(result); } @@ -305,12 +310,13 @@ private: "unexpected number of trailing zeros in varint encoding"); // Parse in the remaining bytes of the value. - if (failed(parseBytes(numBytes, reinterpret_cast(&result) + 1))) + llvm::support::ulittle64_t resultLE(result); + if (failed(parseBytes(numBytes, reinterpret_cast(&resultLE) + 1))) return failure(); // Shift out the low-order bits that were used to mark how the value was // encoded. - result >>= (numBytes + 1); + result = resultLE >> (numBytes + 1); return success(); } diff --git a/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp b/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp index 03c7a53a34e5..936117aa2b8f 100644 --- a/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp +++ b/mlir/lib/Bytecode/Writer/BytecodeWriter.cpp @@ -21,6 +21,7 @@ #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Endian.h" #include #include #include @@ -538,7 +539,8 @@ void EncodingEmitter::emitMultiByteVarInt(uint64_t value) { if (LLVM_LIKELY(it >>= 7) == 0) { uint64_t encodedValue = (value << 1) | 0x1; encodedValue <<= (numBytes - 1); - emitBytes({reinterpret_cast(&encodedValue), numBytes}); + llvm::support::ulittle64_t encodedValueLE(encodedValue); + emitBytes({reinterpret_cast(&encodedValueLE), numBytes}); return; } } @@ -546,7 +548,8 @@ void EncodingEmitter::emitMultiByteVarInt(uint64_t value) { // If the value is too large to encode in a single byte, emit a special all // zero marker byte and splat the value directly. emitByte(0); - emitBytes({reinterpret_cast(&value), sizeof(value)}); + llvm::support::ulittle64_t valueLE(value); + emitBytes({reinterpret_cast(&valueLE), sizeof(valueLE)}); } //===----------------------------------------------------------------------===// -- cgit v1.2.3