[llvm] [BOLT] Add CustomOffset flag for optimizing kernel (PR #98153)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 9 06:10:33 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-bolt

Author: None (lifengxiang1025)

<details>
<summary>Changes</summary>

When I use BOLT to optimize kernel, I found there exists one offset between `vmlinux` and `/proc/kallsyms`. And the address that perf script output matches `/proc/kallsyms` and dismatches `vmlinux`.

---
Full diff: https://github.com/llvm/llvm-project/pull/98153.diff


2 Files Affected:

- (modified) bolt/include/bolt/Profile/DataAggregator.h (+10-4) 
- (modified) bolt/lib/Profile/DataAggregator.cpp (+10-6) 


``````````diff
diff --git a/bolt/include/bolt/Profile/DataAggregator.h b/bolt/include/bolt/Profile/DataAggregator.h
index 6453b3070ceb8..b77b25598f4c2 100644
--- a/bolt/include/bolt/Profile/DataAggregator.h
+++ b/bolt/include/bolt/Profile/DataAggregator.h
@@ -426,7 +426,12 @@ class DataAggregator : public DataReader {
   /// the base load address. External addresses, i.e. addresses that do not
   /// correspond to the binary allocated address space, are adjusted to avoid
   /// conflicts.
-  void adjustAddress(uint64_t &Address, const MMapInfo &MMI) const {
+  void adjustAddress(uint64_t &Address, const MMapInfo &MMI,
+                     int64_t CustomOffset) const {
+    if (CustomOffset) {
+      Address += CustomOffset;
+      return;
+    }
     if (Address >= MMI.MMapAddress && Address < MMI.MMapAddress + MMI.Size) {
       Address -= MMI.BaseAddress;
     } else if (Address < MMI.Size) {
@@ -436,9 +441,10 @@ class DataAggregator : public DataReader {
   }
 
   /// Adjust addresses in \p LBR entry.
-  void adjustLBR(LBREntry &LBR, const MMapInfo &MMI) const {
-    adjustAddress(LBR.From, MMI);
-    adjustAddress(LBR.To, MMI);
+  void adjustLBR(LBREntry &LBR, const MMapInfo &MMI,
+                 int64_t CustomOffset) const {
+    adjustAddress(LBR.From, MMI, CustomOffset);
+    adjustAddress(LBR.To, MMI, CustomOffset);
   }
 
   /// Ignore kernel/user transition LBR if requested
diff --git a/bolt/lib/Profile/DataAggregator.cpp b/bolt/lib/Profile/DataAggregator.cpp
index ce6ec0a04ac16..e2143582de1f7 100644
--- a/bolt/lib/Profile/DataAggregator.cpp
+++ b/bolt/lib/Profile/DataAggregator.cpp
@@ -110,6 +110,10 @@ static cl::opt<bool> WriteAutoFDOData(
     "autofdo", cl::desc("generate autofdo textual data instead of bolt data"),
     cl::cat(AggregatorCategory));
 
+static cl::opt<long long> CustomOffset("custom_offset", cl::init(0),
+                                       cl::desc("address plus custom_offset"),
+                                       cl::Optional, cl::Hidden,
+                                       cl::cat(AggregatorCategory));
 } // namespace opts
 
 namespace {
@@ -1112,8 +1116,8 @@ ErrorOr<DataAggregator::PerfBranchSample> DataAggregator::parseBranchSample() {
     LBREntry LBR = LBRRes.get();
     if (ignoreKernelInterrupt(LBR))
       continue;
-    if (!BC->HasFixedLoadAddress)
-      adjustLBR(LBR, MMapInfoIter->second);
+    if (!BC->HasFixedLoadAddress || CustomOffset)
+      adjustLBR(LBR, MMapInfoIter->second, CustomOffset);
     Res.LBR.push_back(LBR);
   }
 
@@ -1154,8 +1158,8 @@ ErrorOr<DataAggregator::PerfBasicSample> DataAggregator::parseBasicSample() {
   }
 
   uint64_t Address = *AddrRes;
-  if (!BC->HasFixedLoadAddress)
-    adjustAddress(Address, MMapInfoIter->second);
+  if (!BC->HasFixedLoadAddress || CustomOffset)
+    adjustAddress(Address, MMapInfoIter->second, CustomOffset);
 
   return PerfBasicSample{Event.get(), Address};
 }
@@ -1209,8 +1213,8 @@ ErrorOr<DataAggregator::PerfMemSample> DataAggregator::parseMemSample() {
   }
 
   uint64_t Address = *AddrRes;
-  if (!BC->HasFixedLoadAddress)
-    adjustAddress(Address, MMapInfoIter->second);
+  if (!BC->HasFixedLoadAddress || CustomOffset)
+    adjustAddress(Address, MMapInfoIter->second, CustomOffset);
 
   return PerfMemSample{PCRes.get(), Address};
 }

``````````

</details>


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


More information about the llvm-commits mailing list