Skip to content

Commit e99d21a

Browse files
committed
[llvm][clang][modules] Fix test failure on big-endian bots
After 6fb08d8,`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.
1 parent 0e8e782 commit e99d21a

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,13 +1147,9 @@ ASTFileSignature ASTWriter::backpatchSignature() {
11471147
// For implicit modules, write the hash of the PCM as its signature.
11481148

11491149
auto BackpatchSignatureAt = [&](const ASTFileSignature &S, uint64_t BitNo) {
1150-
using WordT = unsigned;
1151-
std::array<WordT, sizeof(ASTFileSignature) / sizeof(WordT)> Words;
1152-
static_assert(sizeof(Words) == sizeof(S));
1153-
std::memcpy(Words.data(), S.data(), sizeof(ASTFileSignature));
1154-
for (WordT Word : Words) {
1155-
Stream.BackpatchWord(BitNo, Word);
1156-
BitNo += sizeof(WordT) * 8;
1150+
for (uint8_t Byte : S) {
1151+
Stream.BackpatchByte(BitNo, Byte);
1152+
BitNo += 8;
11571153
}
11581154
};
11591155

llvm/include/llvm/Bitstream/BitstreamWriter.h

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,20 @@ class BitstreamWriter {
129129
// Basic Primitives for emitting bits to the stream.
130130
//===--------------------------------------------------------------------===//
131131

132-
/// Backpatch a 32-bit word in the output at the given bit offset
133-
/// with the specified value.
134-
void BackpatchWord(uint64_t BitNo, unsigned NewWord) {
132+
/// Backpatch a byte in the output at the given bit offset with the specified
133+
/// value.
134+
void BackpatchByte(uint64_t BitNo, uint8_t NewByte) {
135135
using namespace llvm::support;
136136
uint64_t ByteNo = BitNo / 8;
137137
uint64_t StartBit = BitNo & 7;
138138
uint64_t NumOfFlushedBytes = GetNumOfFlushedBytes();
139139

140140
if (ByteNo >= NumOfFlushedBytes) {
141-
assert((!endian::readAtBitAlignment<uint32_t, little, unaligned>(
141+
assert((!endian::readAtBitAlignment<uint8_t, little, unaligned>(
142142
&Out[ByteNo - NumOfFlushedBytes], StartBit)) &&
143143
"Expected to be patching over 0-value placeholders");
144-
endian::writeAtBitAlignment<uint32_t, little, unaligned>(
145-
&Out[ByteNo - NumOfFlushedBytes], NewWord, StartBit);
144+
endian::writeAtBitAlignment<uint8_t, little, unaligned>(
145+
&Out[ByteNo - NumOfFlushedBytes], NewByte, StartBit);
146146
return;
147147
}
148148

@@ -151,8 +151,8 @@ class BitstreamWriter {
151151
uint64_t CurPos = FS->tell();
152152

153153
// Copy data to update into Bytes from the file FS and the buffer Out.
154-
char Bytes[9]; // Use one more byte to silence a warning from Visual C++.
155-
size_t BytesNum = StartBit ? 8 : 4;
154+
char Bytes[3]; // Use one more byte to silence a warning from Visual C++.
155+
size_t BytesNum = StartBit ? 2 : 1;
156156
size_t BytesFromDisk = std::min(static_cast<uint64_t>(BytesNum), NumOfFlushedBytes - ByteNo);
157157
size_t BytesFromBuffer = BytesNum - BytesFromDisk;
158158

@@ -170,14 +170,14 @@ class BitstreamWriter {
170170
assert(BytesRead >= 0 && static_cast<size_t>(BytesRead) == BytesFromDisk);
171171
for (size_t i = 0; i < BytesFromBuffer; ++i)
172172
Bytes[BytesFromDisk + i] = Out[i];
173-
assert((!endian::readAtBitAlignment<uint32_t, little, unaligned>(
173+
assert((!endian::readAtBitAlignment<uint8_t, little, unaligned>(
174174
Bytes, StartBit)) &&
175175
"Expected to be patching over 0-value placeholders");
176176
}
177177

178178
// Update Bytes in terms of bit offset and value.
179-
endian::writeAtBitAlignment<uint32_t, little, unaligned>(Bytes, NewWord,
180-
StartBit);
179+
endian::writeAtBitAlignment<uint8_t, little, unaligned>(Bytes, NewByte,
180+
StartBit);
181181

182182
// Copy updated data back to the file FS and the buffer Out.
183183
FS->seek(ByteNo);
@@ -189,6 +189,16 @@ class BitstreamWriter {
189189
FS->seek(CurPos);
190190
}
191191

192+
void BackpatchHalfWord(uint64_t BitNo, uint16_t Val) {
193+
BackpatchByte(BitNo, (uint8_t)Val);
194+
BackpatchByte(BitNo + 8, (uint8_t)(Val >> 8));
195+
}
196+
197+
void BackpatchWord(uint64_t BitNo, unsigned Val) {
198+
BackpatchHalfWord(BitNo, (uint16_t)Val);
199+
BackpatchHalfWord(BitNo + 16, (uint16_t)(Val >> 16));
200+
}
201+
192202
void BackpatchWord64(uint64_t BitNo, uint64_t Val) {
193203
BackpatchWord(BitNo, (uint32_t)Val);
194204
BackpatchWord(BitNo + 32, (uint32_t)(Val >> 32));

0 commit comments

Comments
 (0)