Skip to content

Commit 075f754

Browse files
authored
[AMDGPU][llvm-split] Fix division by zero (#98888)
An empty module, or one containing only declarations, would result in a division by a zero cost.
1 parent a1dfe15 commit 075f754

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,12 @@ calculateFunctionCosts(SplitModuleLogger &SML, GetTTIFn GetTTI, Module &M,
256256
}
257257

258258
CostType FnCost = (ModuleCost - KernelCost);
259+
CostType ModuleCostOr1 = ModuleCost ? ModuleCost : 1;
259260
SML << "=> Total Module Cost: " << ModuleCost << '\n'
260261
<< " => KernelCost: " << KernelCost << " ("
261-
<< format("%0.2f", (float(KernelCost) / ModuleCost) * 100) << "%)\n"
262+
<< format("%0.2f", (float(KernelCost) / ModuleCostOr1) * 100) << "%)\n"
262263
<< " => FnsCost: " << FnCost << " ("
263-
<< format("%0.2f", (float(FnCost) / ModuleCost) * 100) << "%)\n";
264+
<< format("%0.2f", (float(FnCost) / ModuleCostOr1) * 100) << "%)\n";
264265

265266
return ModuleCost;
266267
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; RUN: llvm-split -o %t %s -j 2 -mtriple amdgcn-amd-amdhsa
2+
; RUN: llvm-dis -o - %t0 | FileCheck --check-prefix=CHECK0 %s
3+
; RUN: llvm-dis -o - %t1 | FileCheck --check-prefix=CHECK1 %s
4+
5+
; Check that all declarations are put into each partition.
6+
7+
; CHECK0: declare void @A
8+
; CHECK0: declare void @B
9+
10+
; CHECK1: declare void @A
11+
; CHECK1: declare void @B
12+
13+
declare void @A()
14+
15+
declare void @B()

0 commit comments

Comments
 (0)