Skip to content

Commit a4c3ed4

Browse files
committed
Correctly emit dwoIDs after ASTFileSignature refactoring (D81347)
D81347 changes the ASTFileSignature to be an array of 20 uint8_t instead of 5 uint32_t. However, it didn't update the code in ObjectFilePCHContainerOperations that creates the dwoID in the module from the ASTFileSignature (`Buffer->Signature` being the array subclass that is now `std::array<uint8_t, 20>` instead of `std::array<uint32_t, 5>`). ``` uint64_t Signature = [..] (uint64_t)Buffer->Signature[1] << 32 | Buffer->Signature[0] ``` This code works with the old ASTFileSignature (where two uint32_t are enough to fill the uint64_t), but after the patch this only took two bytes from the ASTFileSignature and only partly filled the Signature uint64_t. This caused that the dwoID in the module ref and the dwoID in the actual module no longer match (which in turns causes that LLDB keeps warning about the dwoID's not matching when debugging -gmodules-compiled binaries). This patch just unifies the logic for turning the ASTFileSignature into an uint64_t which makes the dwoID match again (and should prevent issues like that in the future). Reviewed By: aprantl, dang Differential Revision: https://reviews.llvm.org/D84013
1 parent 8881849 commit a4c3ed4

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

clang/include/clang/Basic/Module.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ struct ASTFileSignature : std::array<uint8_t, 20> {
6262

6363
explicit operator bool() const { return *this != BaseT({{0}}); }
6464

65+
/// Returns the value truncated to the size of an uint64_t.
66+
uint64_t truncatedValue() const {
67+
uint64_t Value = 0;
68+
static_assert(sizeof(*this) >= sizeof(uint64_t), "No need to truncate.");
69+
for (unsigned I = 0; I < sizeof(uint64_t); ++I)
70+
Value |= static_cast<uint64_t>((*this)[I]) << (I * 8);
71+
return Value;
72+
}
73+
6574
static ASTFileSignature create(StringRef Bytes) {
6675
return create(Bytes.bytes_begin(), Bytes.bytes_end());
6776
}

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,12 +2545,11 @@ llvm::DIModule *CGDebugInfo::getOrCreateModuleRef(ASTSourceDescriptor Mod,
25452545
// We use the lower 64 bits for debug info.
25462546

25472547
uint64_t Signature = 0;
2548-
if (const auto &ModSig = Mod.getSignature()) {
2549-
for (unsigned I = 0; I != sizeof(Signature); ++I)
2550-
Signature |= (uint64_t)ModSig[I] << (I * 8);
2551-
} else {
2548+
if (const auto &ModSig = Mod.getSignature())
2549+
Signature = ModSig.truncatedValue();
2550+
else
25522551
Signature = ~1ULL;
2553-
}
2552+
25542553
llvm::DIBuilder DIB(CGM.getModule());
25552554
SmallString<0> PCM;
25562555
if (!llvm::sys::path::is_absolute(Mod.getASTFile()))

clang/lib/CodeGen/ObjectFilePCHContainerOperations.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,10 @@ class PCHContainerGenerator : public ASTConsumer {
250250
// PCH files don't have a signature field in the control block,
251251
// but LLVM detects DWO CUs by looking for a non-zero DWO id.
252252
// We use the lower 64 bits for debug info.
253+
253254
uint64_t Signature =
254-
Buffer->Signature
255-
? (uint64_t)Buffer->Signature[1] << 32 | Buffer->Signature[0]
256-
: ~1ULL;
255+
Buffer->Signature ? Buffer->Signature.truncatedValue() : ~1ULL;
256+
257257
Builder->getModuleDebugInfo()->setDwoId(Signature);
258258

259259
// Finalize the Builder.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef DEBUG_DWO_ID_H
2+
#define DEBUG_DWO_ID_H
3+
struct Dummy {};
4+
#endif

clang/test/Modules/Inputs/module.map

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,10 @@ module DebugObjCImport {
357357
}
358358
}
359359

360+
module DebugDwoId {
361+
header "DebugDwoId.h"
362+
}
363+
360364
module ImportNameInDir {
361365
header "ImportNameInDir.h"
362366
export *
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Tests that dwoIds in modules match the dwoIDs in the main file.
2+
3+
// RUN: rm -rf %t.cache
4+
// RUN: %clang_cc1 -triple %itanium_abi_triple -x objective-c++ -std=c++11 -debugger-tuning=lldb -debug-info-kind=limited -fmodules -fmodule-format=obj -dwarf-ext-refs -fimplicit-module-maps -fmodules-cache-path=%t.cache %s -I %S/Inputs -emit-llvm -o %t.ll -mllvm -debug-only=pchcontainer &> %t.mod-out
5+
// RUN: cat %t.ll %t.mod-out | FileCheck %s
6+
// RUN: cat %t.ll | FileCheck --check-prefix=CHECK-REALIDS %s
7+
// RUN: cat %t.mod-out | FileCheck --check-prefix=CHECK-REALIDS %s
8+
9+
@import DebugDwoId;
10+
11+
Dummy d;
12+
13+
// Find the emitted dwoID for DebugInfoId and compare it against the one in the PCM.
14+
// CHECK: DebugDwoId-{{[A-Z0-9]+}}.pcm
15+
// CHECK-SAME: dwoId: [[DWOID:[0-9]+]]
16+
// CHECK: dwoId: [[DWOID]]
17+
// CHECK-NEXT: !DIFile(filename: "DebugDwoId"
18+
19+
// Make sure the dwo IDs are real IDs and not fallback values (~1ULL).
20+
// CHECK-REALIDS-NOT: dwoId: 18446744073709551615

0 commit comments

Comments
 (0)