[llvm] [Instrumentation] Fix EdgeCounts vector size in SetBranchWeights (PR #99064)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 16 10:00:15 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-pgo

@llvm/pr-subscribers-llvm-transforms

Author: Avi Kivity (avikivity)

<details>
<summary>Changes</summary>

SetBranchWeights() calculates the size of the EdgeCounts vector using OutEdges.Size(), but this is an under-estimate with coroutines.

Use the number of successors, as the vector will be indexed by the result of the GetSuccessorNumber() function.

This crashes in 18.1, but somehow doesn't in main. Still, it looks like the fix is applicable to both.

Fixes #<!-- -->97962

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


1 Files Affected:

- (modified) llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp (+3-1) 


``````````diff
diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
index 35b1bbf21be97..d32d00700bcb3 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -1624,7 +1624,9 @@ void PGOUseFunc::setBranchWeights() {
 
     // We have a non-zero Branch BB.
     unsigned Size = BBCountInfo.OutEdges.size();
-    SmallVector<uint64_t, 2> EdgeCounts(Size, 0);
+    unsigned SuccessorCount = BB.getTerminator()->getNumSuccessors();
+
+    SmallVector<uint64_t, 2> EdgeCounts(SuccessorCount, 0);
     uint64_t MaxCount = 0;
     for (unsigned s = 0; s < Size; s++) {
       const PGOUseEdge *E = BBCountInfo.OutEdges[s];

``````````

</details>


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


More information about the llvm-commits mailing list