[llvm] [BOLT] Add support for safe-icf (PR #116275)

Maksim Panchenko via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 10 14:56:08 PST 2024


================
@@ -340,6 +354,113 @@ typedef std::unordered_map<BinaryFunction *, std::vector<BinaryFunction *>,
 
 namespace llvm {
 namespace bolt {
+/// Scans symbol table and creates a bit vector of memory addresses of vtables.
+static void processSymbolTable(const BinaryContext &BC,
+                               llvm::BitVector &BitVector) {
+  for (auto &[Address, Data] : BC.getBinaryData()) {
+    // Filter out all symbols that are not vtables.
+    if (!Data->getName().starts_with("_ZTV"))
+      continue;
+    for (uint64_t I = Address / 8, End = I + (Data->getSize() / 8); I < End;
+         ++I)
+      BitVector.set(I);
+  }
+}
+Error IdenticalCodeFolding::processDataRelocations(
+    BinaryContext &BC, const SectionRef &SecRefRelData,
+    const llvm::BitVector &BitVector, const bool HasAddressTaken) {
+  for (const RelocationRef &Rel : SecRefRelData.relocations()) {
+    if (BitVector.test(Rel.getOffset() / 8))
+      continue;
+    symbol_iterator SymbolIter = Rel.getSymbol();
+    const ObjectFile *OwningObj = Rel.getObject();
+    assert(SymbolIter != OwningObj->symbol_end() &&
+           "relocation Symbol expected");
+    const SymbolRef &Symbol = *SymbolIter;
+    const uint64_t SymbolAddress = cantFail(Symbol.getAddress());
+    const ELFObjectFileBase *ELFObj = dyn_cast<ELFObjectFileBase>(OwningObj);
+    if (!ELFObj)
+      return createFatalBOLTError(
+          Twine("BOLT-ERROR: Only ELFObjectFileBase is supported"));
+    const int64_t Addend = getRelocationAddend(ELFObj, Rel);
+    BinaryFunction *BF = BC.getBinaryFunctionAtAddress(SymbolAddress + Addend);
+    if (!BF)
+      continue;
+    BF->setHasAddressTaken(HasAddressTaken);
+  }
+  return Error::success();
+}
+
+Error IdenticalCodeFolding::markFunctionsUnsafeToFold(BinaryContext &BC) {
+  if (!BC.isX86())
+    BC.outs() << "BOLT-WARNING: Safe ICF is only supported for x86\n";
----------------
maksfb wrote:

nit:
```suggestion
    BC.errs() << "BOLT-WARNING: safe ICF is only supported for x86\n";
```

https://github.com/llvm/llvm-project/pull/116275


More information about the llvm-commits mailing list