Skip to content

Commit 4d15af1

Browse files
[LLVM][LoopVectorize] Fix SIGFPE crash in getPredBlockCostDivisor
When computing block frequency division in getPredBlockCostDivisor, the function could perform division by zero if BBFreq equals 0. This caused a floating point exception crash when processing blocks with zero block frequency information. Add a guard check to return 1 when BBFreq is 0, avoiding the undefined behavior while maintaining correct cost model behavior. A divisor of 1 indicates the block is executed at the same frequency as the header, which is a safe default when frequency information is unavailable. Fixes issue #172049
1 parent f3c1645 commit 4d15af1

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2907,6 +2907,10 @@ uint64_t LoopVectorizationCostModel::getPredBlockCostDivisor(
29072907
uint64_t BBFreq = getBFI().getBlockFreq(BB).getFrequency();
29082908
assert(HeaderFreq >= BBFreq &&
29092909
"Header has smaller block freq than dominated BB?");
2910+
// Guard against division by zero when BBFreq is 0.
2911+
// In such cases, return 1 to avoid undefined behavior.
2912+
if (BBFreq == 0)
2913+
return 1;
29102914
return std::round((double)HeaderFreq / BBFreq);
29112915
}
29122916

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; Test case for crash with Floating point Exception in loop-vectorize pass
2+
; This test verifies that the loop vectorizer does not crash with SIGFPE
3+
; when processing blocks with zero block frequency.
4+
; See issue #172049
5+
6+
; RUN: opt -passes=loop-vectorize -S %s
7+
8+
; ModuleID = 'reduced.ll'
9+
source_filename = "reduced.ll"
10+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128-ni:1-p2:32:8:8:32-ni:2"
11+
target triple = "x86_64-unknown-linux-gnu"
12+
13+
define ptr addrspace(1) @wombat() gc "statepoint-example" {
14+
bb:
15+
br label %bb2
16+
17+
bb1:
18+
ret ptr addrspace(1) null
19+
20+
bb2:
21+
%phi = phi i64 [ %add, %bb6 ], [ 0, %bb ]
22+
br i1 false, label %bb3, label %bb6
23+
24+
bb3:
25+
br i1 false, label %bb4, label %bb5, !prof !0
26+
27+
bb4:
28+
br label %bb6
29+
30+
bb5:
31+
br label %bb6
32+
33+
bb6:
34+
%add = add i64 %phi, 1
35+
%icmp = icmp eq i64 %phi, 0
36+
br i1 %icmp, label %bb2, label %bb1
37+
}
38+
39+
!0 = !{!"branch_weights", i32 1, i32 0}

0 commit comments

Comments
 (0)