Skip to content

Commit 7708bff

Browse files
committed
resolve comments
1 parent 463bd18 commit 7708bff

File tree

6 files changed

+39
-129
lines changed

6 files changed

+39
-129
lines changed

bolt/include/bolt/Core/DebugData.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ class DebugStrOffsetsWriter {
471471
return std::move(StrOffsetsBuffer);
472472
}
473473

474+
/// Returns strings of .debug_str_offsets.
474475
StringRef bufferStr() {
475476
return StringRef(reinterpret_cast<const char *>(StrOffsetsBuffer->data()),
476477
StrOffsetsBuffer->size());
@@ -512,6 +513,7 @@ class DebugStrWriter {
512513
return std::move(StrBuffer);
513514
}
514515

516+
/// Returns strings of .debug_str.
515517
StringRef bufferStr() {
516518
return StringRef(reinterpret_cast<const char *>(StrBuffer->data()),
517519
StrBuffer->size());

bolt/lib/Rewrite/DWARFRewriter.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,29 +1723,39 @@ StringRef getSectionName(const SectionRef &Section) {
17231723
return Name;
17241724
}
17251725

1726-
// Exctracts some appropriate slices of .debug_str.dwo from DWP.
1727-
// Updates the .debug_str_offets.dwo for CUs.
1728-
void UpdateStrAndStrOffsets(StringRef StrDWOContent,
1729-
StringRef StrOffsetsContent,
1730-
SmallVectorImpl<StringRef> &StrDWOOutData,
1731-
std::string &StrOffsetsOutData,
1732-
unsigned DwarfVersion, bool IsLittleEndian) {
1726+
/// Extracts the slice of the .debug_str.dwo section for a given CU from a DWP
1727+
/// file, based on the .debug_str_offsets.dwo section. This helps address DWO
1728+
/// bloat that may occur after updates.
1729+
///
1730+
/// A slice of .debug_str.dwo may be composed of several non-contiguous
1731+
/// fragments. These non-contiguous string views will be written out
1732+
/// sequentially, avoiding the copying overhead caused by assembling them.
1733+
///
1734+
/// The .debug_str_offsets for the first CU often does not need to be updated,
1735+
/// so copying is only performed when .debug_str_offsets requires updating.
1736+
static void UpdateStrAndStrOffsets(StringRef StrDWOContent,
1737+
StringRef StrOffsetsContent,
1738+
SmallVectorImpl<StringRef> &StrDWOOutData,
1739+
std::string &StrOffsetsOutData,
1740+
unsigned DwarfVersion, bool IsLittleEndian) {
17331741
const llvm::endianness Endian =
17341742
IsLittleEndian ? llvm::endianness::little : llvm::endianness::big;
1735-
// ignore DWARF64
17361743
const uint64_t HeaderOffset = (DwarfVersion >= 5) ? 8 : 0;
1737-
const uint64_t NumOffsets = (StrOffsetsContent.size() - HeaderOffset) / 4;
1744+
constexpr size_t SizeOfOffset = sizeof(int32_t);
1745+
const uint64_t NumOffsets =
1746+
(StrOffsetsContent.size() - HeaderOffset) / SizeOfOffset;
17381747

17391748
DataExtractor Extractor(StrOffsetsContent, IsLittleEndian, 0);
17401749
uint64_t ExtractionOffset = HeaderOffset;
17411750

17421751
using StringFragment = DWARFUnitIndex::Entry::SectionContribution;
1743-
auto getStringLength = [](StringRef Content, uint64_t Offset) -> uint64_t {
1752+
const auto getStringLength = [](StringRef Content,
1753+
uint64_t Offset) -> uint64_t {
17441754
size_t NullPos = Content.find('\0', Offset);
17451755
return (NullPos != StringRef::npos) ? (NullPos - Offset + 1) : 0;
17461756
};
1747-
auto isContiguous = [](const StringFragment &Fragment,
1748-
uint64_t NextOffset) -> bool {
1757+
const auto isContiguous = [](const StringFragment &Fragment,
1758+
uint64_t NextOffset) -> bool {
17491759
return NextOffset == Fragment.getOffset() + Fragment.getLength();
17501760
};
17511761
std::optional<StringFragment> CurrentFragment;
@@ -1754,26 +1764,26 @@ void UpdateStrAndStrOffsets(StringRef StrDWOContent,
17541764
const uint64_t StrOffset = Extractor.getU32(&ExtractionOffset);
17551765
const uint64_t StringLength = getStringLength(StrDWOContent, StrOffset);
17561766
if (!CurrentFragment) {
1757-
// first init
1767+
// First init.
17581768
CurrentFragment = StringFragment(StrOffset, StringLength);
17591769
} else {
17601770
if (isContiguous(*CurrentFragment, StrOffset)) {
1761-
// expand the current fragment
1771+
// Expanding the current fragment.
17621772
CurrentFragment->setLength(CurrentFragment->getLength() + StringLength);
17631773
} else {
1764-
// save the current fragment and start a new one
1774+
// Saving the current fragment and start a new one.
17651775
StrDWOOutData.push_back(StrDWOContent.substr(
17661776
CurrentFragment->getOffset(), CurrentFragment->getLength()));
17671777
CurrentFragment = StringFragment(StrOffset, StringLength);
17681778
}
17691779
}
17701780
if (AccumulatedStrLen != StrOffset) {
1771-
// update str offsets
1781+
// Updating str offsets.
17721782
if (StrOffsetsOutData.empty())
17731783
StrOffsetsOutData = StrOffsetsContent.str();
1774-
llvm::support::endian::write32(&StrOffsetsOutData[HeaderOffset + I * 4],
1775-
static_cast<uint32_t>(AccumulatedStrLen),
1776-
Endian);
1784+
llvm::support::endian::write32(
1785+
&StrOffsetsOutData[HeaderOffset + I * SizeOfOffset],
1786+
static_cast<uint32_t>(AccumulatedStrLen), Endian);
17771787
}
17781788
AccumulatedStrLen += StringLength;
17791789
}

bolt/test/X86/Inputs/dwarf4-str-split-dwarf.s

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#--- main.s
2-
# clang++ -g2 -gdwarf-4 -gsplit-dwarf=split -S main.cpp
2+
# clang++ -g2 -gdwarf-4 -gsplit-dwarf=split -gno-pubnames -S main.cpp
33
# extern int getReturn();
44
# int main() {
55
# return getReturn();
@@ -24,8 +24,6 @@ main: # @main
2424
.byte 23 # DW_FORM_sec_offset
2525
.byte 27 # DW_AT_comp_dir
2626
.byte 14 # DW_FORM_strp
27-
.ascii "\264B" # DW_AT_GNU_pubnames
28-
.byte 25 # DW_FORM_flag_present
2927
.ascii "\260B" # DW_AT_GNU_dwo_name
3028
.byte 14 # DW_FORM_strp
3129
.ascii "\261B" # DW_AT_GNU_dwo_id
@@ -49,7 +47,6 @@ main: # @main
4947
.byte 1 # Abbrev [1] 0xb:0x25 DW_TAG_compile_unit
5048
.long .Lline_table_start0 # DW_AT_stmt_list
5149
.long .Lskel_string0 # DW_AT_comp_dir
52-
# DW_AT_GNU_pubnames
5350
.long .Lskel_string1 # DW_AT_GNU_dwo_name
5451
.quad -9094791692727444213 # DW_AT_GNU_dwo_id
5552
.quad .Lfunc_begin0 # DW_AT_low_pc
@@ -158,36 +155,14 @@ main: # @main
158155
.section .debug_addr,"",@progbits
159156
.Laddr_table_base0:
160157
.quad .Lfunc_begin0
161-
.section .debug_gnu_pubnames,"",@progbits
162-
.long .LpubNames_end0-.LpubNames_start0 # Length of Public Names Info
163-
.LpubNames_start0:
164-
.short 2 # DWARF Version
165-
.long .Lcu_begin0 # Offset of Compilation Unit Info
166-
.long 48 # Compilation Unit Length
167-
.long 25 # DIE offset
168-
.byte 48 # Attributes: FUNCTION, EXTERNAL
169-
.asciz "main" # External Name
170-
.long 0 # End Mark
171-
.LpubNames_end0:
172-
.section .debug_gnu_pubtypes,"",@progbits
173-
.long .LpubTypes_end0-.LpubTypes_start0 # Length of Public Types Info
174-
.LpubTypes_start0:
175-
.short 2 # DWARF Version
176-
.long .Lcu_begin0 # Offset of Compilation Unit Info
177-
.long 48 # Compilation Unit Length
178-
.long 40 # DIE offset
179-
.byte 144 # Attributes: TYPE, STATIC
180-
.asciz "int" # External Name
181-
.long 0 # End Mark
182-
.LpubTypes_end0:
183158
.ident "clang version 22.0.0"
184159
.section ".note.GNU-stack","",@progbits
185160
.addrsig
186161
.addrsig_sym _Z9getReturnv
187162
.section .debug_line,"",@progbits
188163
.Lline_table_start0:
189164
#--- helper.s
190-
# clang++ -g2 -gdwarf-4 -gsplit-dwarf=split -S helper.cpp
165+
# clang++ -g2 -gdwarf-4 -gsplit-dwarf=split -gno-pubnames -S helper.cpp
191166
# int getReturn() {
192167
# return 0;
193168
# }
@@ -211,8 +186,6 @@ _Z9getReturnv: # @_Z9getReturnv
211186
.byte 23 # DW_FORM_sec_offset
212187
.byte 27 # DW_AT_comp_dir
213188
.byte 14 # DW_FORM_strp
214-
.ascii "\264B" # DW_AT_GNU_pubnames
215-
.byte 25 # DW_FORM_flag_present
216189
.ascii "\260B" # DW_AT_GNU_dwo_name
217190
.byte 14 # DW_FORM_strp
218191
.ascii "\261B" # DW_AT_GNU_dwo_id
@@ -236,7 +209,6 @@ _Z9getReturnv: # @_Z9getReturnv
236209
.byte 1 # Abbrev [1] 0xb:0x25 DW_TAG_compile_unit
237210
.long .Lline_table_start0 # DW_AT_stmt_list
238211
.long .Lskel_string0 # DW_AT_comp_dir
239-
# DW_AT_GNU_pubnames
240212
.long .Lskel_string1 # DW_AT_GNU_dwo_name
241213
.quad 5976014880088676049 # DW_AT_GNU_dwo_id
242214
.quad .Lfunc_begin0 # DW_AT_low_pc
@@ -351,28 +323,6 @@ _Z9getReturnv: # @_Z9getReturnv
351323
.section .debug_addr,"",@progbits
352324
.Laddr_table_base0:
353325
.quad .Lfunc_begin0
354-
.section .debug_gnu_pubnames,"",@progbits
355-
.long .LpubNames_end0-.LpubNames_start0 # Length of Public Names Info
356-
.LpubNames_start0:
357-
.short 2 # DWARF Version
358-
.long .Lcu_begin0 # Offset of Compilation Unit Info
359-
.long 48 # Compilation Unit Length
360-
.long 25 # DIE offset
361-
.byte 48 # Attributes: FUNCTION, EXTERNAL
362-
.asciz "getReturn" # External Name
363-
.long 0 # End Mark
364-
.LpubNames_end0:
365-
.section .debug_gnu_pubtypes,"",@progbits
366-
.long .LpubTypes_end0-.LpubTypes_start0 # Length of Public Types Info
367-
.LpubTypes_start0:
368-
.short 2 # DWARF Version
369-
.long .Lcu_begin0 # Offset of Compilation Unit Info
370-
.long 48 # Compilation Unit Length
371-
.long 41 # DIE offset
372-
.byte 144 # Attributes: TYPE, STATIC
373-
.asciz "int" # External Name
374-
.long 0 # End Mark
375-
.LpubTypes_end0:
376326
.ident "clang version 22.0.0"
377327
.section ".note.GNU-stack","",@progbits
378328
.addrsig

bolt/test/X86/Inputs/dwarf5-str-split-dwarf.s

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#--- main.s
2-
# clang++ -g2 -gdwarf-5 -gsplit-dwarf=split -S main.cpp
2+
# clang++ -g2 -gdwarf-5 -gsplit-dwarf=split -gno-pubnames -S main.cpp
33
# extern int getReturn();
44
# int main() {
55
# return getReturn();
@@ -26,8 +26,6 @@ main: # @main
2626
.byte 23 # DW_FORM_sec_offset
2727
.byte 27 # DW_AT_comp_dir
2828
.byte 37 # DW_FORM_strx1
29-
.ascii "\264B" # DW_AT_GNU_pubnames
30-
.byte 25 # DW_FORM_flag_present
3129
.byte 118 # DW_AT_dwo_name
3230
.byte 37 # DW_FORM_strx1
3331
.byte 17 # DW_AT_low_pc
@@ -52,7 +50,6 @@ main: # @main
5250
.long .Lline_table_start0 # DW_AT_stmt_list
5351
.long .Lstr_offsets_base0 # DW_AT_str_offsets_base
5452
.byte 0 # DW_AT_comp_dir
55-
# DW_AT_GNU_pubnames
5653
.byte 1 # DW_AT_dwo_name
5754
.byte 0 # DW_AT_low_pc
5855
.long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc
@@ -177,36 +174,14 @@ main: # @main
177174
.Laddr_table_base0:
178175
.quad .Lfunc_begin0
179176
.Ldebug_addr_end0:
180-
.section .debug_gnu_pubnames,"",@progbits
181-
.long .LpubNames_end0-.LpubNames_start0 # Length of Public Names Info
182-
.LpubNames_start0:
183-
.short 2 # DWARF Version
184-
.long .Lcu_begin0 # Offset of Compilation Unit Info
185-
.long 40 # Compilation Unit Length
186-
.long 26 # DIE offset
187-
.byte 48 # Attributes: FUNCTION, EXTERNAL
188-
.asciz "main" # External Name
189-
.long 0 # End Mark
190-
.LpubNames_end0:
191-
.section .debug_gnu_pubtypes,"",@progbits
192-
.long .LpubTypes_end0-.LpubTypes_start0 # Length of Public Types Info
193-
.LpubTypes_start0:
194-
.short 2 # DWARF Version
195-
.long .Lcu_begin0 # Offset of Compilation Unit Info
196-
.long 40 # Compilation Unit Length
197-
.long 41 # DIE offset
198-
.byte 144 # Attributes: TYPE, STATIC
199-
.asciz "int" # External Name
200-
.long 0 # End Mark
201-
.LpubTypes_end0:
202177
.ident "clang version 22.0.0"
203178
.section ".note.GNU-stack","",@progbits
204179
.addrsig
205180
.addrsig_sym _Z9getReturnv
206181
.section .debug_line,"",@progbits
207182
.Lline_table_start0:
208183
#--- helper.s
209-
# clang++ -g2 -gdwarf-5 -gsplit-dwarf=split -S helper.cpp
184+
# clang++ -g2 -gdwarf-5 -gsplit-dwarf=split -gno-pubnames -S helper.cpp
210185
# int getReturn() {
211186
# return 0;
212187
# }
@@ -232,8 +207,6 @@ _Z9getReturnv: # @_Z9getReturnv
232207
.byte 23 # DW_FORM_sec_offset
233208
.byte 27 # DW_AT_comp_dir
234209
.byte 37 # DW_FORM_strx1
235-
.ascii "\264B" # DW_AT_GNU_pubnames
236-
.byte 25 # DW_FORM_flag_present
237210
.byte 118 # DW_AT_dwo_name
238211
.byte 37 # DW_FORM_strx1
239212
.byte 17 # DW_AT_low_pc
@@ -258,7 +231,6 @@ _Z9getReturnv: # @_Z9getReturnv
258231
.long .Lline_table_start0 # DW_AT_stmt_list
259232
.long .Lstr_offsets_base0 # DW_AT_str_offsets_base
260233
.byte 0 # DW_AT_comp_dir
261-
# DW_AT_GNU_pubnames
262234
.byte 1 # DW_AT_dwo_name
263235
.byte 0 # DW_AT_low_pc
264236
.long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc
@@ -389,28 +361,6 @@ _Z9getReturnv: # @_Z9getReturnv
389361
.Laddr_table_base0:
390362
.quad .Lfunc_begin0
391363
.Ldebug_addr_end0:
392-
.section .debug_gnu_pubnames,"",@progbits
393-
.long .LpubNames_end0-.LpubNames_start0 # Length of Public Names Info
394-
.LpubNames_start0:
395-
.short 2 # DWARF Version
396-
.long .Lcu_begin0 # Offset of Compilation Unit Info
397-
.long 40 # Compilation Unit Length
398-
.long 26 # DIE offset
399-
.byte 48 # Attributes: FUNCTION, EXTERNAL
400-
.asciz "getReturn" # External Name
401-
.long 0 # End Mark
402-
.LpubNames_end0:
403-
.section .debug_gnu_pubtypes,"",@progbits
404-
.long .LpubTypes_end0-.LpubTypes_start0 # Length of Public Types Info
405-
.LpubTypes_start0:
406-
.short 2 # DWARF Version
407-
.long .Lcu_begin0 # Offset of Compilation Unit Info
408-
.long 40 # Compilation Unit Length
409-
.long 42 # DIE offset
410-
.byte 144 # Attributes: TYPE, STATIC
411-
.asciz "int" # External Name
412-
.long 0 # End Mark
413-
.LpubTypes_end0:
414364
.ident "clang version 22.0.0"
415365
.section ".note.GNU-stack","",@progbits
416366
.addrsig

bolt/test/X86/dwarf4-str-dwp-input-dwo-output.test

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
; RUN: llvm-dwarfdump --show-form --verbose --debug-str-offsets helper.dwo.dwo \
2121
; RUN: | FileCheck -check-prefix=BOLT-HELPER-STR-OFFSETS %s
2222

23-
;; For DWARF4.
24-
;; Test input into bolt a DWP file.
25-
;; Make sure output in the .dwo files has current str and str_offsets information.
23+
;; For DWARF4, this test checks that strings are split correctly from a combined
24+
;; section in DWP file, into appropriate .dwo files.
2625

2726
; PRE-BOLT-STR: 0x00000000: "main"
2827
; PRE-BOLT-STR: 0x00000005: "int"

bolt/test/X86/dwarf5-str-dwp-input-dwo-output.test

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
; RUN: llvm-dwarfdump --show-form --verbose --debug-str-offsets helper.dwo.dwo \
2121
; RUN: | FileCheck -check-prefix=BOLT-HELPER-STR-OFFSETS %s
2222

23-
;; For DWARF5.
24-
;; Test input into bolt a DWP file.
25-
;; Make sure output in the .dwo files has current str and str_offsets information.
23+
;; For DWARF5, this test checks that strings are split correctly from a combined
24+
;; section in DWP file, into appropriate .dwo files.
2625

2726
; PRE-BOLT-STR: 0x00000000: "main"
2827
; PRE-BOLT-STR: 0x00000005: "int"

0 commit comments

Comments
 (0)