[llvm] [CodeExtractor] Resolving the Inconsistency of Compiled Binary Files (PR #86464)

dong jianqiang via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 24 21:23:36 PDT 2024


https://github.com/dongjianqiang2 created https://github.com/llvm/llvm-project/pull/86464

When the compiler enables ALSR by default, binary inconsistency occurs when the extractCodeRegion function is called. The reason is that the sequence of traversing ExitBlocks containers of the SmallPtrSet type is changed due to randomization of BasicBlock pointers.

>From 3ba9122af72410ecb3c9723e5b239d3182a614a2 Mon Sep 17 00:00:00 2001
From: dong jianqiang <dongjianqiang2 at huawei.com>
Date: Mon, 25 Mar 2024 12:21:51 +0800
Subject: [PATCH] [CodeExtractor] Resolving the Inconsistency of Compiled
 Binary Files

When the compiler enables ALSR by default, binary inconsistency occurs
when the extractCodeRegion function is called. The reason is that the
sequence of traversing ExitBlocks containers of the SmallPtrSet type
is changed due to randomization of BasicBlock pointers.
---
 llvm/include/llvm/Transforms/Utils/CodeExtractor.h | 3 ++-
 llvm/lib/Transforms/Utils/CodeExtractor.cpp        | 4 ++--
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Utils/CodeExtractor.h b/llvm/include/llvm/Transforms/Utils/CodeExtractor.h
index 27b34ef023db72..7f21067e5487a3 100644
--- a/llvm/include/llvm/Transforms/Utils/CodeExtractor.h
+++ b/llvm/include/llvm/Transforms/Utils/CodeExtractor.h
@@ -18,6 +18,7 @@
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SetVector.h"
 #include <limits>
+#include <unordered_set>
 
 namespace llvm {
 
@@ -249,7 +250,7 @@ class CodeExtractorAnalysisCache {
                        Instruction *Addr, BasicBlock *ExitBlock) const;
 
     void severSplitPHINodesOfEntry(BasicBlock *&Header);
-    void severSplitPHINodesOfExits(const SmallPtrSetImpl<BasicBlock *> &Exits);
+    void severSplitPHINodesOfExits(const std::unordered_set<BasicBlock *> &Exits);
     void splitReturnBlocks();
 
     Function *constructFunction(const ValueSet &inputs,
diff --git a/llvm/lib/Transforms/Utils/CodeExtractor.cpp b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
index 3191751d92e176..0184c1c996c5a7 100644
--- a/llvm/lib/Transforms/Utils/CodeExtractor.cpp
+++ b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
@@ -745,7 +745,7 @@ void CodeExtractor::severSplitPHINodesOfEntry(BasicBlock *&Header) {
 /// and other with remaining incoming blocks; then first PHIs are placed in
 /// outlined region.
 void CodeExtractor::severSplitPHINodesOfExits(
-    const SmallPtrSetImpl<BasicBlock *> &Exits) {
+    const std::unordered_set<BasicBlock *> &Exits) {
   for (BasicBlock *ExitBB : Exits) {
     BasicBlock *NewBB = nullptr;
 
@@ -1751,7 +1751,7 @@ CodeExtractor::extractCodeRegion(const CodeExtractorAnalysisCache &CEAC,
   // Calculate the exit blocks for the extracted region and the total exit
   // weights for each of those blocks.
   DenseMap<BasicBlock *, BlockFrequency> ExitWeights;
-  SmallPtrSet<BasicBlock *, 1> ExitBlocks;
+  std::unordered_set<BasicBlock *> ExitBlocks;
   for (BasicBlock *Block : Blocks) {
     for (BasicBlock *Succ : successors(Block)) {
       if (!Blocks.count(Succ)) {



More information about the llvm-commits mailing list