Skip to content

Commit ba48b15

Browse files
oontvooellishg
authored andcommitted
[lld-macho]Define a flag for adjusting slop scale (llvm#164295)
Co-authored-by: Ellis Hoag <[email protected]>
1 parent 19c981c commit ba48b15

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

lld/MachO/ConcatOutputSection.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ void TextOutputSection::finalize() {
306306
// contains several branch instructions in succession, then the distance
307307
// from the current position to the position where the thunks are inserted
308308
// grows. So leave room for a bunch of thunks.
309-
unsigned slop = 256 * thunkSize;
309+
unsigned slop = config->slopScale * thunkSize;
310310
while (finalIdx < endIdx) {
311311
uint64_t expectedNewSize =
312312
alignToPowerOf2(addr + size, inputs[finalIdx]->align) +
@@ -384,7 +384,9 @@ void TextOutputSection::finalize() {
384384
// above. If you hit this: For the current algorithm, just bumping up
385385
// slop above and trying again is probably simplest. (See also PR51578
386386
// comment 5).
387-
fatal(Twine(__FUNCTION__) + ": FIXME: thunk range overrun");
387+
fatal(Twine(__FUNCTION__) +
388+
": FIXME: thunk range overrun. Consider increasing the "
389+
"slop-scale with `--slop-scale=<unsigned_int>`.");
388390
}
389391
thunkInfo.isec =
390392
makeSyntheticInputSection(isec->getSegName(), isec->getName());

lld/MachO/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ struct Configuration {
224224
bool disableVerify;
225225
bool separateCstringLiteralSections;
226226
bool tailMergeStrings;
227+
unsigned slopScale = 256;
227228

228229
bool callGraphProfileSort = false;
229230
llvm::StringRef printSymbolOrder;

lld/MachO/Driver.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,14 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
20152015
OPT_no_separate_cstring_literal_sections, false);
20162016
config->tailMergeStrings =
20172017
args.hasFlag(OPT_tail_merge_strings, OPT_no_tail_merge_strings, false);
2018+
if (auto *arg = args.getLastArg(OPT_slop_scale_eq)) {
2019+
StringRef v(arg->getValue());
2020+
unsigned slop = 0;
2021+
if (!llvm::to_integer(v, slop))
2022+
error(arg->getSpelling() +
2023+
": expected a non-negative integer, but got '" + v + "'");
2024+
config->slopScale = slop;
2025+
}
20182026

20192027
auto IncompatWithCGSort = [&](StringRef firstArgStr) {
20202028
// Throw an error only if --call-graph-profile-sort is explicitly specified

lld/MachO/Options.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,14 @@ defm tail_merge_strings
10951095
: BB<"tail-merge-strings", "Enable string tail merging",
10961096
"Disable string tail merging to improve link-time performance">,
10971097
Group<grp_rare>;
1098+
def slop_scale_eq
1099+
: Joined<["--"], "slop_scale=">,
1100+
MetaVarName<"<unsigned_int>">,
1101+
HelpText<"Specify the slop scale. Default value is 256. If your binary "
1102+
"has too many consecutive branch instructions resulting in "
1103+
"thunk-range overrun, then you need to increase this value to a "
1104+
"higher value, such as 512 or 1024, etc">,
1105+
Group<grp_rare>;
10981106

10991107
def grp_deprecated : OptionGroup<"deprecated">, HelpText<"DEPRECATED">;
11001108

lld/test/MachO/set-slop-scale.s

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-darwin %s -o %t.o
3+
# RUN: %lld -o /dev/null %t.o --slop_scale=1
4+
# RUN: not %lld -o /dev/null %t.o --slop_scale=-1 2>&1 | FileCheck %s
5+
# CHECK: error: --slop_scale=: expected a non-negative integer, but got '-1'
6+
7+
.text
8+
.global _main
9+
_main:
10+
mov $0, %rax
11+
ret

0 commit comments

Comments
 (0)