[PATCH] D132495: [BOLT] Improve recognition of unknown control flow

Amir Ayupov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 23 15:01:20 PDT 2022


Amir updated this revision to Diff 454978.
Amir added a comment.

Verify the set of internal references against jump table targets.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132495/new/

https://reviews.llvm.org/D132495

Files:
  bolt/lib/Core/BinaryFunction.cpp


Index: bolt/lib/Core/BinaryFunction.cpp
===================================================================
--- bolt/lib/Core/BinaryFunction.cpp
+++ bolt/lib/Core/BinaryFunction.cpp
@@ -1842,6 +1842,34 @@
   if (HasInternalLabelReference)
     return false;
 
+  // Validate that all references to function internals from data are claimed by
+  // recognized jump tables. Register externally referenced blocks as entry
+  // points otherwise.
+  if (!HasUnknownControlFlow && hasInternalReference()) {
+    SmallPtrSet<MCSymbol *, 4> JTTargets;
+    for (const JumpTable *JT : llvm::make_second_range(JumpTables))
+      JTTargets.insert(JT->Entries.begin(), JT->Entries.end());
+    for (uint64_t Destination : ExternallyReferencedOffsets) {
+      if (BinaryBasicBlock *BB = getBasicBlockAtOffset(Destination)) {
+        bool Found = JTTargets.contains(BB->getLabel());
+        if (!Found) {
+          if (opts::Verbosity >= 1) {
+            outs() << "BOLT-WARNING: registering externally referenced "
+                   << BB->getName() << " as a secondary entry point in "
+                   << *this << ".\n";
+          }
+          addEntryPoint(*BB);
+        }
+      } else {
+        outs() << "BOLT-WARNING: external reference to offset "
+               << formatv("{0:x}", Destination)
+               << " does not point to a valid instruction in " << *this
+               << ". Ignoring the function.";
+        return false;
+      }
+    }
+  }
+
   // If there's only one jump table, and one indirect jump, and no other
   // references, then we should be able to derive the jump table even if we
   // fail to match the pattern.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132495.454978.patch
Type: text/x-patch
Size: 1657 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220823/de52dba8/attachment.bin>


More information about the llvm-commits mailing list