[llvm] [Propeller] Make BBID containing information about MBB's size to detect ir drift (PR #79545)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 25 19:47:53 PST 2024


https://github.com/lifengxiang1025 created https://github.com/llvm/llvm-project/pull/79545

Now Propeller only detect code drift when using pgo together. This patch aims to make BBID containing information about MBB's size to detect ir drift.

>From a10a127094bc5061813ea90d065849c7b19d16da Mon Sep 17 00:00:00 2001
From: lifengxiang <lifengxiang.1025 at bytedance.com>
Date: Fri, 26 Jan 2024 11:41:27 +0800
Subject: [PATCH] [Propeller] Make BBID containing information about MBB's size
 to detect ir drift

---
 llvm/lib/CodeGen/BasicBlockSections.cpp | 7 +++++++
 llvm/lib/CodeGen/MachineFunction.cpp    | 6 +++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/BasicBlockSections.cpp b/llvm/lib/CodeGen/BasicBlockSections.cpp
index 94b5a503fbd0f3..430b8864207612 100644
--- a/llvm/lib/CodeGen/BasicBlockSections.cpp
+++ b/llvm/lib/CodeGen/BasicBlockSections.cpp
@@ -310,7 +310,14 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) {
             .getClusterInfoForFunction(MF.getName());
     if (!HasProfile)
       return false;
+    DenseSet<UniqueBBID> FuncUniqueBBID;
+    for (auto &MBB : MF) {
+      FuncUniqueBBID.insert(MBB.getBBID().value());
+    }
     for (auto &BBClusterInfo : ClusterInfo) {
+      if (!FuncUniqueBBID.count(BBClusterInfo.BBID)) {
+        return false;
+      }
       FuncClusterInfo.try_emplace(BBClusterInfo.BBID, BBClusterInfo);
     }
   }
diff --git a/llvm/lib/CodeGen/MachineFunction.cpp b/llvm/lib/CodeGen/MachineFunction.cpp
index 57af571ed9bfd5..2fc760ef233e72 100644
--- a/llvm/lib/CodeGen/MachineFunction.cpp
+++ b/llvm/lib/CodeGen/MachineFunction.cpp
@@ -468,7 +468,11 @@ MachineFunction::CreateMachineBasicBlock(const BasicBlock *BB,
   // blocks.
   if (Target.getBBSectionsType() == BasicBlockSection::Labels ||
       Target.getBBSectionsType() == BasicBlockSection::List)
-    MBB->setBBID(BBID.has_value() ? *BBID : UniqueBBID{NextBBID++, 0});
+    MBB->setBBID(BBID.has_value()
+                     ? *BBID
+                     : UniqueBBID{((NextBBID++ & 0x0000ffff) << 32) |
+                                      (MBB->size() & 0x0000ffff),
+                                  0});
   return MBB;
 }
 



More information about the llvm-commits mailing list