[clang] dd850f0 - [llvm][clang][modules] Fix test failure on big-endian bots

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 28 13:18:48 PDT 2023


Author: Jan Svoboda
Date: 2023-08-28T13:18:40-07:00
New Revision: dd850f0bae8f38b721e9ae6b8ba915dbf7d9bac7

URL: https://github.com/llvm/llvm-project/commit/dd850f0bae8f38b721e9ae6b8ba915dbf7d9bac7
DIFF: https://github.com/llvm/llvm-project/commit/dd850f0bae8f38b721e9ae6b8ba915dbf7d9bac7.diff

LOG: [llvm][clang][modules] Fix test failure on big-endian bots

After 6fb08d8f558a6f28db7835acdb88cab83aea2eb4,`clang/test/Modules/ModuleDebugInfoDwoId.cpp` started failing on a number of big-endian build bots (clang-ppc64be-linux-multistage, clang-ppc64be-linux-test-suite). This patch attempts to fix that by creating an API on `llvm::BitstreamWriter` that allows backpatching individual bytes. This API is then used from `clang::ASTWriter` to avoid endianness mismatch.

Added: 
    

Modified: 
    clang/lib/Serialization/ASTWriter.cpp
    llvm/include/llvm/Bitstream/BitstreamWriter.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index c9148b67c06ea9..8eac78408188d2 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -1148,13 +1148,9 @@ ASTFileSignature ASTWriter::backpatchSignature() {
   // For implicit modules, write the hash of the PCM as its signature.
 
   auto BackpatchSignatureAt = [&](const ASTFileSignature &S, uint64_t BitNo) {
-    using WordT = unsigned;
-    std::array<WordT, sizeof(ASTFileSignature) / sizeof(WordT)> Words;
-    static_assert(sizeof(Words) == sizeof(S));
-    std::memcpy(Words.data(), S.data(), sizeof(ASTFileSignature));
-    for (WordT Word : Words) {
-      Stream.BackpatchWord(BitNo, Word);
-      BitNo += sizeof(WordT) * 8;
+    for (uint8_t Byte : S) {
+      Stream.BackpatchByte(BitNo, Byte);
+      BitNo += 8;
     }
   };
 

diff  --git a/llvm/include/llvm/Bitstream/BitstreamWriter.h b/llvm/include/llvm/Bitstream/BitstreamWriter.h
index d802b73e97e0f5..423af4c2cc6c00 100644
--- a/llvm/include/llvm/Bitstream/BitstreamWriter.h
+++ b/llvm/include/llvm/Bitstream/BitstreamWriter.h
@@ -129,20 +129,20 @@ class BitstreamWriter {
   // Basic Primitives for emitting bits to the stream.
   //===--------------------------------------------------------------------===//
 
-  /// Backpatch a 32-bit word in the output at the given bit offset
-  /// with the specified value.
-  void BackpatchWord(uint64_t BitNo, unsigned NewWord) {
+  /// Backpatch a byte in the output at the given bit offset with the specified
+  /// value.
+  void BackpatchByte(uint64_t BitNo, uint8_t NewByte) {
     using namespace llvm::support;
     uint64_t ByteNo = BitNo / 8;
     uint64_t StartBit = BitNo & 7;
     uint64_t NumOfFlushedBytes = GetNumOfFlushedBytes();
 
     if (ByteNo >= NumOfFlushedBytes) {
-      assert((!endian::readAtBitAlignment<uint32_t, little, unaligned>(
+      assert((!endian::readAtBitAlignment<uint8_t, little, unaligned>(
                  &Out[ByteNo - NumOfFlushedBytes], StartBit)) &&
              "Expected to be patching over 0-value placeholders");
-      endian::writeAtBitAlignment<uint32_t, little, unaligned>(
-          &Out[ByteNo - NumOfFlushedBytes], NewWord, StartBit);
+      endian::writeAtBitAlignment<uint8_t, little, unaligned>(
+          &Out[ByteNo - NumOfFlushedBytes], NewByte, StartBit);
       return;
     }
 
@@ -151,8 +151,8 @@ class BitstreamWriter {
     uint64_t CurPos = FS->tell();
 
     // Copy data to update into Bytes from the file FS and the buffer Out.
-    char Bytes[9]; // Use one more byte to silence a warning from Visual C++.
-    size_t BytesNum = StartBit ? 8 : 4;
+    char Bytes[3]; // Use one more byte to silence a warning from Visual C++.
+    size_t BytesNum = StartBit ? 2 : 1;
     size_t BytesFromDisk = std::min(static_cast<uint64_t>(BytesNum), NumOfFlushedBytes - ByteNo);
     size_t BytesFromBuffer = BytesNum - BytesFromDisk;
 
@@ -170,14 +170,14 @@ class BitstreamWriter {
       assert(BytesRead >= 0 && static_cast<size_t>(BytesRead) == BytesFromDisk);
       for (size_t i = 0; i < BytesFromBuffer; ++i)
         Bytes[BytesFromDisk + i] = Out[i];
-      assert((!endian::readAtBitAlignment<uint32_t, little, unaligned>(
+      assert((!endian::readAtBitAlignment<uint8_t, little, unaligned>(
                  Bytes, StartBit)) &&
              "Expected to be patching over 0-value placeholders");
     }
 
     // Update Bytes in terms of bit offset and value.
-    endian::writeAtBitAlignment<uint32_t, little, unaligned>(Bytes, NewWord,
-                                                             StartBit);
+    endian::writeAtBitAlignment<uint8_t, little, unaligned>(Bytes, NewByte,
+                                                            StartBit);
 
     // Copy updated data back to the file FS and the buffer Out.
     FS->seek(ByteNo);
@@ -189,6 +189,16 @@ class BitstreamWriter {
     FS->seek(CurPos);
   }
 
+  void BackpatchHalfWord(uint64_t BitNo, uint16_t Val) {
+    BackpatchByte(BitNo, (uint8_t)Val);
+    BackpatchByte(BitNo + 8, (uint8_t)(Val >> 8));
+  }
+
+  void BackpatchWord(uint64_t BitNo, unsigned Val) {
+    BackpatchHalfWord(BitNo, (uint16_t)Val);
+    BackpatchHalfWord(BitNo + 16, (uint16_t)(Val >> 16));
+  }
+
   void BackpatchWord64(uint64_t BitNo, uint64_t Val) {
     BackpatchWord(BitNo, (uint32_t)Val);
     BackpatchWord(BitNo + 32, (uint32_t)(Val >> 32));


        


More information about the cfe-commits mailing list