xref: /llvm-project-15.0.7/lld/MachO/ICF.cpp (revision cf838ebf)
1 //===- ICF.cpp ------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ICF.h"
10 #include "ConcatOutputSection.h"
11 #include "InputSection.h"
12 #include "Symbols.h"
13 #include "UnwindInfoSection.h"
14 
15 #include "llvm/Support/Parallel.h"
16 #include "llvm/Support/TimeProfiler.h"
17 
18 #include <atomic>
19 
20 using namespace llvm;
21 using namespace lld;
22 using namespace lld::macho;
23 
24 class ICF {
25 public:
26   ICF(std::vector<ConcatInputSection *> &inputs);
27 
28   void run();
29   void segregate(size_t begin, size_t end,
30                  std::function<bool(const ConcatInputSection *,
31                                     const ConcatInputSection *)>
32                      equals);
33   size_t findBoundary(size_t begin, size_t end);
34   void forEachClassRange(size_t begin, size_t end,
35                          std::function<void(size_t, size_t)> func);
36   void forEachClass(std::function<void(size_t, size_t)> func);
37 
38   // ICF needs a copy of the inputs vector because its equivalence-class
39   // segregation algorithm destroys the proper sequence.
40   std::vector<ConcatInputSection *> icfInputs;
41 };
42 
43 ICF::ICF(std::vector<ConcatInputSection *> &inputs) {
44   icfInputs.assign(inputs.begin(), inputs.end());
45 }
46 
47 // ICF = Identical Code Folding
48 //
49 // We only fold __TEXT,__text, so this is really "code" folding, and not
50 // "COMDAT" folding. String and scalar constant literals are deduplicated
51 // elsewhere.
52 //
53 // Summary of segments & sections:
54 //
55 // The __TEXT segment is readonly at the MMU. Some sections are already
56 // deduplicated elsewhere (__TEXT,__cstring & __TEXT,__literal*) and some are
57 // synthetic and inherently free of duplicates (__TEXT,__stubs &
58 // __TEXT,__unwind_info). Note that we don't yet run ICF on __TEXT,__const,
59 // because doing so induces many test failures.
60 //
61 // The __LINKEDIT segment is readonly at the MMU, yet entirely synthetic, and
62 // thus ineligible for ICF.
63 //
64 // The __DATA_CONST segment is read/write at the MMU, but is logically const to
65 // the application after dyld applies fixups to pointer data. We currently
66 // fold only the __DATA_CONST,__cfstring section.
67 //
68 // The __DATA segment is read/write at the MMU, and as application-writeable
69 // data, none of its sections are eligible for ICF.
70 //
71 // Please see the large block comment in lld/ELF/ICF.cpp for an explanation
72 // of the segregation algorithm.
73 //
74 // FIXME(gkm): implement keep-unique attributes
75 // FIXME(gkm): implement address-significance tables for MachO object files
76 
77 static unsigned icfPass = 0;
78 static std::atomic<bool> icfRepeat{false};
79 
80 // Compare "non-moving" parts of two ConcatInputSections, namely everything
81 // except references to other ConcatInputSections.
82 static bool equalsConstant(const ConcatInputSection *ia,
83                            const ConcatInputSection *ib) {
84   // We can only fold within the same OutputSection.
85   if (ia->parent != ib->parent)
86     return false;
87   if (ia->data.size() != ib->data.size())
88     return false;
89   if (ia->data != ib->data)
90     return false;
91   if (ia->relocs.size() != ib->relocs.size())
92     return false;
93   auto f = [](const Reloc &ra, const Reloc &rb) {
94     if (ra.type != rb.type)
95       return false;
96     if (ra.pcrel != rb.pcrel)
97       return false;
98     if (ra.length != rb.length)
99       return false;
100     if (ra.offset != rb.offset)
101       return false;
102     if (ra.addend != rb.addend)
103       return false;
104     if (ra.referent.is<Symbol *>() != rb.referent.is<Symbol *>())
105       return false;
106 
107     InputSection *isecA, *isecB;
108 
109     uint64_t valueA = 0;
110     uint64_t valueB = 0;
111     if (ra.referent.is<Symbol *>()) {
112       const auto *sa = ra.referent.get<Symbol *>();
113       const auto *sb = rb.referent.get<Symbol *>();
114       if (sa->kind() != sb->kind())
115         return false;
116       if (!isa<Defined>(sa)) {
117         assert(isa<DylibSymbol>(sa));
118         return sa == sb;
119       }
120       const auto *da = cast<Defined>(sa);
121       const auto *db = cast<Defined>(sb);
122       if (!da->isec || !db->isec) {
123         assert(da->isAbsolute() && db->isAbsolute());
124         return da->value == db->value;
125       }
126       isecA = da->isec;
127       valueA = da->value;
128       isecB = db->isec;
129       valueB = db->value;
130     } else {
131       isecA = ra.referent.get<InputSection *>();
132       isecB = rb.referent.get<InputSection *>();
133     }
134 
135     if (isecA->parent != isecB->parent)
136       return false;
137     // Sections with identical parents should be of the same kind.
138     assert(isecA->kind() == isecB->kind());
139     // We will compare ConcatInputSection contents in equalsVariable.
140     if (isa<ConcatInputSection>(isecA))
141       return true;
142     // Else we have two literal sections. References to them are equal iff their
143     // offsets in the output section are equal.
144     return isecA->getOffset(valueA + ra.addend) ==
145            isecB->getOffset(valueB + rb.addend);
146   };
147   return std::equal(ia->relocs.begin(), ia->relocs.end(), ib->relocs.begin(),
148                     f);
149 }
150 
151 // Compare the "moving" parts of two ConcatInputSections -- i.e. everything not
152 // handled by equalsConstant().
153 static bool equalsVariable(const ConcatInputSection *ia,
154                            const ConcatInputSection *ib) {
155   assert(ia->relocs.size() == ib->relocs.size());
156   auto f = [](const Reloc &ra, const Reloc &rb) {
157     // We already filtered out mismatching values/addends in equalsConstant.
158     if (ra.referent == rb.referent)
159       return true;
160     const ConcatInputSection *isecA, *isecB;
161     if (ra.referent.is<Symbol *>()) {
162       // Matching DylibSymbols are already filtered out by the
163       // identical-referent check above. Non-matching DylibSymbols were filtered
164       // out in equalsConstant(). So we can safely cast to Defined here.
165       const auto *da = cast<Defined>(ra.referent.get<Symbol *>());
166       const auto *db = cast<Defined>(rb.referent.get<Symbol *>());
167       if (da->isAbsolute())
168         return true;
169       isecA = dyn_cast<ConcatInputSection>(da->isec);
170       if (!isecA)
171         return true; // literal sections were checked in equalsConstant.
172       isecB = cast<ConcatInputSection>(db->isec);
173     } else {
174       const auto *sa = ra.referent.get<InputSection *>();
175       const auto *sb = rb.referent.get<InputSection *>();
176       isecA = dyn_cast<ConcatInputSection>(sa);
177       if (!isecA)
178         return true;
179       isecB = cast<ConcatInputSection>(sb);
180     }
181     return isecA->icfEqClass[icfPass % 2] == isecB->icfEqClass[icfPass % 2];
182   };
183   return std::equal(ia->relocs.begin(), ia->relocs.end(), ib->relocs.begin(),
184                     f);
185 }
186 
187 // Find the first InputSection after BEGIN whose equivalence class differs
188 size_t ICF::findBoundary(size_t begin, size_t end) {
189   uint64_t beginHash = icfInputs[begin]->icfEqClass[icfPass % 2];
190   for (size_t i = begin + 1; i < end; ++i)
191     if (beginHash != icfInputs[i]->icfEqClass[icfPass % 2])
192       return i;
193   return end;
194 }
195 
196 // Invoke FUNC on subranges with matching equivalence class
197 void ICF::forEachClassRange(size_t begin, size_t end,
198                             std::function<void(size_t, size_t)> func) {
199   while (begin < end) {
200     size_t mid = findBoundary(begin, end);
201     func(begin, mid);
202     begin = mid;
203   }
204 }
205 
206 // Split icfInputs into shards, then parallelize invocation of FUNC on subranges
207 // with matching equivalence class
208 void ICF::forEachClass(std::function<void(size_t, size_t)> func) {
209   // Only use threads when the benefits outweigh the overhead.
210   const size_t threadingThreshold = 1024;
211   if (icfInputs.size() < threadingThreshold) {
212     forEachClassRange(0, icfInputs.size(), func);
213     ++icfPass;
214     return;
215   }
216 
217   // Shard into non-overlapping intervals, and call FUNC in parallel.  The
218   // sharding must be completed before any calls to FUNC are made so that FUNC
219   // can modify the InputSection in its shard without causing data races.
220   const size_t shards = 256;
221   size_t step = icfInputs.size() / shards;
222   size_t boundaries[shards + 1];
223   boundaries[0] = 0;
224   boundaries[shards] = icfInputs.size();
225   parallelForEachN(1, shards, [&](size_t i) {
226     boundaries[i] = findBoundary((i - 1) * step, icfInputs.size());
227   });
228   parallelForEachN(1, shards + 1, [&](size_t i) {
229     if (boundaries[i - 1] < boundaries[i]) {
230       forEachClassRange(boundaries[i - 1], boundaries[i], func);
231     }
232   });
233   ++icfPass;
234 }
235 
236 void ICF::run() {
237   // Into each origin-section hash, combine all reloc referent section hashes.
238   for (icfPass = 0; icfPass < 2; ++icfPass) {
239     parallelForEach(icfInputs, [&](ConcatInputSection *isec) {
240       uint64_t hash = isec->icfEqClass[icfPass % 2];
241       for (const Reloc &r : isec->relocs) {
242         if (auto *sym = r.referent.dyn_cast<Symbol *>()) {
243           if (auto *dylibSym = dyn_cast<DylibSymbol>(sym))
244             hash += dylibSym->stubsHelperIndex;
245           else if (auto *defined = dyn_cast<Defined>(sym)) {
246             if (defined->isec) {
247               if (auto isec = dyn_cast<ConcatInputSection>(defined->isec))
248                 hash += defined->value + isec->icfEqClass[icfPass % 2];
249               else
250                 hash += defined->isec->kind() +
251                         defined->isec->getOffset(defined->value);
252             } else {
253               hash += defined->value;
254             }
255           } else if (!isa<Undefined>(sym))
256             llvm_unreachable("foldIdenticalSections symbol kind");
257         }
258       }
259       // Set MSB to 1 to avoid collisions with non-hashed classes.
260       isec->icfEqClass[(icfPass + 1) % 2] = hash | (1ull << 63);
261     });
262   }
263 
264   llvm::stable_sort(
265       icfInputs, [](const ConcatInputSection *a, const ConcatInputSection *b) {
266         return a->icfEqClass[0] < b->icfEqClass[0];
267       });
268   forEachClass(
269       [&](size_t begin, size_t end) { segregate(begin, end, equalsConstant); });
270 
271   // Split equivalence groups by comparing relocations until convergence
272   do {
273     icfRepeat = false;
274     forEachClass([&](size_t begin, size_t end) {
275       segregate(begin, end, equalsVariable);
276     });
277   } while (icfRepeat);
278   log("ICF needed " + Twine(icfPass) + " iterations");
279 
280   // Fold sections within equivalence classes
281   forEachClass([&](size_t begin, size_t end) {
282     if (end - begin < 2)
283       return;
284     ConcatInputSection *beginIsec = icfInputs[begin];
285     for (size_t i = begin + 1; i < end; ++i)
286       beginIsec->foldIdentical(icfInputs[i]);
287   });
288 }
289 
290 // Split an equivalence class into smaller classes.
291 void ICF::segregate(
292     size_t begin, size_t end,
293     std::function<bool(const ConcatInputSection *, const ConcatInputSection *)>
294         equals) {
295   while (begin < end) {
296     // Divide [begin, end) into two. Let mid be the start index of the
297     // second group.
298     auto bound = std::stable_partition(icfInputs.begin() + begin + 1,
299                                        icfInputs.begin() + end,
300                                        [&](ConcatInputSection *isec) {
301                                          return equals(icfInputs[begin], isec);
302                                        });
303     size_t mid = bound - icfInputs.begin();
304 
305     // Split [begin, end) into [begin, mid) and [mid, end). We use mid as an
306     // equivalence class ID because every group ends with a unique index.
307     for (size_t i = begin; i < mid; ++i)
308       icfInputs[i]->icfEqClass[(icfPass + 1) % 2] = mid;
309 
310     // If we created a group, we need to iterate the main loop again.
311     if (mid != end)
312       icfRepeat = true;
313 
314     begin = mid;
315   }
316 }
317 
318 void macho::foldIdenticalSections() {
319   TimeTraceScope timeScope("Fold Identical Code Sections");
320   // The ICF equivalence-class segregation algorithm relies on pre-computed
321   // hashes of InputSection::data for the ConcatOutputSection::inputs and all
322   // sections referenced by their relocs. We could recursively traverse the
323   // relocs to find every referenced InputSection, but that precludes easy
324   // parallelization. Therefore, we hash every InputSection here where we have
325   // them all accessible as simple vectors.
326 
327   // If an InputSection is ineligible for ICF, we give it a unique ID to force
328   // it into an unfoldable singleton equivalence class.  Begin the unique-ID
329   // space at inputSections.size(), so that it will never intersect with
330   // equivalence-class IDs which begin at 0. Since hashes & unique IDs never
331   // coexist with equivalence-class IDs, this is not necessary, but might help
332   // someone keep the numbers straight in case we ever need to debug the
333   // ICF::segregate()
334   std::vector<ConcatInputSection *> hashable;
335   uint64_t icfUniqueID = inputSections.size();
336   for (ConcatInputSection *isec : inputSections) {
337     // FIXME: consider non-code __text sections as hashable?
338     bool isHashable = (isCodeSection(isec) || isCfStringSection(isec)) &&
339                       !isec->shouldOmitFromOutput() && isec->isHashableForICF();
340     // ICF can't fold functions with unwind info
341     if (isHashable)
342       for (Defined *d : isec->symbols)
343         if (d->compactUnwind) {
344           isHashable = false;
345           break;
346         }
347 
348     if (isHashable)
349       hashable.push_back(isec);
350     else
351       isec->icfEqClass[0] = ++icfUniqueID;
352   }
353   parallelForEach(hashable,
354                   [](ConcatInputSection *isec) { isec->hashForICF(); });
355   // Now that every input section is either hashed or marked as unique, run the
356   // segregation algorithm to detect foldable subsections.
357   ICF(hashable).run();
358 }
359