From de9a7260aca314778da7f768cd4d8687defc759a Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Tue, 12 Jul 2022 09:29:07 +0200 Subject: [PATCH] Read/write external resource alignment tag in little-endian https://reviews.llvm.org/D126446 added support for encoding binary blobs in MLIR assembly. To enable cross-architecture compatibility, these need to be encoded in little-endian format. This patch is a first step in that direction by reading and writing the alignment tag that those blobs are prefixed by in little-endian format. This fixes assertion failures in several test cases on big-endian platforms. The actual content of the blob is not yet handled here. Differential Revision: https://reviews.llvm.org/D129483 --- mlir/lib/IR/AsmPrinter.cpp | 3 ++- mlir/lib/Parser/Parser.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp index c3bc7ab..1e46b11 100644 --- a/mlir/lib/IR/AsmPrinter.cpp +++ b/mlir/lib/IR/AsmPrinter.cpp @@ -2685,8 +2685,9 @@ private: uint32_t dataAlignment) final { printFn(key, [&](raw_ostream &os) { // Store the blob in a hex string containing the alignment and the data. + llvm::support::ulittle32_t dataAlignmentLE(dataAlignment); os << "\"0x" - << llvm::toHex(StringRef(reinterpret_cast(&dataAlignment), + << llvm::toHex(StringRef(reinterpret_cast(&dataAlignmentLE), sizeof(dataAlignment))) << llvm::toHex(StringRef(data.data(), data.size())) << "\""; }); diff --git a/mlir/lib/Parser/Parser.cpp b/mlir/lib/Parser/Parser.cpp index e27f72c..bf015de 100644 --- a/mlir/lib/Parser/Parser.cpp +++ b/mlir/lib/Parser/Parser.cpp @@ -24,6 +24,7 @@ #include "llvm/ADT/ScopeExit.h" #include "llvm/ADT/StringSet.h" #include "llvm/ADT/bit.h" +#include "llvm/Support/Endian.h" #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/SourceMgr.h" #include @@ -2349,7 +2350,7 @@ public: "expected hex string blob for key '" + key + "' to encode alignment in first 4 bytes"); } - uint32_t align = 0; + llvm::support::ulittle32_t align; memcpy(&align, blobData->data(), sizeof(uint32_t)); // Get the data portion of the blob. -- 2.7.4