Skip to content

Commit 8988914

Browse files
authored
[lld-macho] Improve ICF thunk folding logic (#131186)
Refactor to add some early return logic to `applySafeThunksToRange` so that we completely skip irrelevant ranges. Also add a check for `isCodeSection` to ensure we only apply branch thunks to code section (they don't make sense for anything else). Currently this isn't an issue since there are no `keepUnique` non-code sections - but this is not a hard restriction and may be implemented in the future, so we should be able to handle (i.e. avoid) this scenario.
1 parent 4818623 commit 8988914

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lld/MachO/ICF.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,25 @@ static Symbol *getThunkTargetSymbol(ConcatInputSection *isec) {
297297
// direct branch thunk rather than containing a full copy of the actual function
298298
// body.
299299
void ICF::applySafeThunksToRange(size_t begin, size_t end) {
300+
// When creating a unique ICF thunk, use the first section as the section that
301+
// all thunks will branch to.
302+
ConcatInputSection *masterIsec = icfInputs[begin];
303+
304+
// If the first section is not address significant, sorting guarantees that
305+
// there are no address significant functions. So we can skip this range.
306+
if (!masterIsec->keepUnique)
307+
return;
308+
309+
// Skip anything that is not a code section.
310+
if (!isCodeSection(masterIsec))
311+
return;
312+
300313
// If the functions we're dealing with are smaller than the thunk size, then
301314
// just leave them all as-is - creating thunks would be a net loss.
302315
uint32_t thunkSize = target->getICFSafeThunkSize();
303-
if (icfInputs[begin]->data.size() <= thunkSize)
316+
if (masterIsec->data.size() <= thunkSize)
304317
return;
305318

306-
// When creating a unique ICF thunk, use the first section as the section that
307-
// all thunks will branch to.
308-
ConcatInputSection *masterIsec = icfInputs[begin];
309-
310319
// Get the symbol that all thunks will branch to.
311320
Symbol *masterSym = getThunkTargetSymbol(masterIsec);
312321

0 commit comments

Comments
 (0)