[llvm] [AMDGPU][llvm-split] Fix division by zero (PR #98888)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 15 04:42:36 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: Fraser Cormack (frasercrmck)

<details>
<summary>Changes</summary>

An empty module, or one containing only declarations, would result in a division by a zero cost.

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


2 Files Affected:

- (modified) llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp (+3-2) 
- (added) llvm/test/tools/llvm-split/AMDGPU/declarations.ll (+15) 


``````````diff
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp b/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp
index 3e5d83b8e3fb1..f75961f6eaa77 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp
@@ -256,11 +256,12 @@ calculateFunctionCosts(SplitModuleLogger &SML, GetTTIFn GetTTI, Module &M,
   }
 
   CostType FnCost = (ModuleCost - KernelCost);
+  CostType ModuleCostOr1 = ModuleCost ? ModuleCost : 1;
   SML << "=> Total Module Cost: " << ModuleCost << '\n'
       << "  => KernelCost: " << KernelCost << " ("
-      << format("%0.2f", (float(KernelCost) / ModuleCost) * 100) << "%)\n"
+      << format("%0.2f", (float(KernelCost) / ModuleCostOr1) * 100) << "%)\n"
       << "  => FnsCost: " << FnCost << " ("
-      << format("%0.2f", (float(FnCost) / ModuleCost) * 100) << "%)\n";
+      << format("%0.2f", (float(FnCost) / ModuleCostOr1) * 100) << "%)\n";
 
   return ModuleCost;
 }
diff --git a/llvm/test/tools/llvm-split/AMDGPU/declarations.ll b/llvm/test/tools/llvm-split/AMDGPU/declarations.ll
new file mode 100644
index 0000000000000..f579056e914aa
--- /dev/null
+++ b/llvm/test/tools/llvm-split/AMDGPU/declarations.ll
@@ -0,0 +1,15 @@
+; RUN: llvm-split -o %t %s -j 2 -mtriple amdgcn-amd-amdhsa
+; RUN: llvm-dis -o - %t0 | FileCheck --check-prefix=CHECK0 %s
+; RUN: llvm-dis -o - %t1 | FileCheck --check-prefix=CHECK1 %s
+
+; Check that all declarations are put into each partition.
+
+; CHECK0: declare void @A
+; CHECK0: declare void @B
+
+; CHECK1: declare void @A
+; CHECK1: declare void @B
+
+declare void @A()
+
+declare void @B()

``````````

</details>


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


More information about the llvm-commits mailing list