Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AMDGPU][SplitModule] Fix unintentional integer division #117586

Merged
merged 2 commits into from
Nov 27, 2024

Conversation

frasercrmck
Copy link
Contributor

@frasercrmck frasercrmck commented Nov 25, 2024

A static analysis tool warned that a division was always being performed
in integer division, so was either 0.0 or 1.0.

This doesn't seem intentional, so has been fixed to return a true ratio
using floating-point division. This in turn showed a bug where a
comparison against this ratio was incorrect.

@llvmbot
Copy link
Member

llvmbot commented Nov 25, 2024

@llvm/pr-subscribers-backend-amdgpu

Author: Fraser Cormack (frasercrmck)

Changes

A static analysis tool found that ModuleCost could be zero, so would perform divide by zero when being printed. Perhaps this is unreachable in practice, but the fix is straightforward enough and unlikely to be a performance concern.

The same tool warned that a division was always being performed in integer division, so was either 0.0 or 1.0. This doesn't seem intentional, so has been fixed to return a true ratio using floating-point division. This has a knock-on effect on how a test was splitting modules.


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

2 Files Affected:

  • (modified) llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp (+3-2)
  • (modified) llvm/test/tools/llvm-split/AMDGPU/large-kernels-merging.ll (+7-6)
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp b/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp
index 5d7aff1c5092cc..a7db7cdb890051 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUSplitModule.cpp
@@ -149,7 +149,8 @@ static constexpr unsigned InvalidPID = -1;
 /// \param Dem denominator
 /// \returns a printable object to print (Num/Dem) using "%0.2f".
 static auto formatRatioOf(CostType Num, CostType Dem) {
-  return format("%0.2f", (static_cast<double>(Num) / Dem) * 100);
+  CostType DemOr1 = Dem ? Dem : 1;
+  return format("%0.2f", (static_cast<double>(Num) / DemOr1) * 100);
 }
 
 /// Checks whether a given function is non-copyable.
@@ -1101,7 +1102,7 @@ void RecursiveSearchSplitting::pickPartition(unsigned Depth, unsigned Idx,
         // Check if the amount of code in common makes it worth it.
         assert(SimilarDepsCost && Entry.CostExcludingGraphEntryPoints);
         const double Ratio =
-            SimilarDepsCost / Entry.CostExcludingGraphEntryPoints;
+            (double)SimilarDepsCost / Entry.CostExcludingGraphEntryPoints;
         assert(Ratio >= 0.0 && Ratio <= 1.0);
         if (LargeFnOverlapForMerge > Ratio) {
           // For debug, just print "L", so we'll see "L3=P3" for instance, which
diff --git a/llvm/test/tools/llvm-split/AMDGPU/large-kernels-merging.ll b/llvm/test/tools/llvm-split/AMDGPU/large-kernels-merging.ll
index 807fb2e5f33cea..2810e9853bebe3 100644
--- a/llvm/test/tools/llvm-split/AMDGPU/large-kernels-merging.ll
+++ b/llvm/test/tools/llvm-split/AMDGPU/large-kernels-merging.ll
@@ -15,19 +15,20 @@
 ; Also check w/o large kernels processing to verify they are indeed handled
 ; differently.
 
-; P0 is empty
-; CHECK0: declare
+; CHECK0: define internal void @HelperC()
+; CHECK0: define amdgpu_kernel void @C
 
-; CHECK1: define internal void @HelperC()
-; CHECK1: define amdgpu_kernel void @C
+; CHECK1: define internal void @large2()
+; CHECK1: define internal void @large1()
+; CHECK1: define internal void @large0()
+; CHECK1: define internal void @HelperB()
+; CHECK1: define amdgpu_kernel void @B
 
 ; CHECK2: define internal void @large2()
 ; CHECK2: define internal void @large1()
 ; CHECK2: define internal void @large0()
 ; CHECK2: define internal void @HelperA()
-; CHECK2: define internal void @HelperB()
 ; CHECK2: define amdgpu_kernel void @A
-; CHECK2: define amdgpu_kernel void @B
 
 ; NOLARGEKERNELS-CHECK0: define internal void @HelperC()
 ; NOLARGEKERNELS-CHECK0: define amdgpu_kernel void @C

@arsenm
Copy link
Contributor

arsenm commented Nov 25, 2024

Description should be more specific

@frasercrmck frasercrmck force-pushed the amdgpu-fix-split-module-ratio branch from 9f271dd to 64efad2 Compare November 25, 2024 18:15
@frasercrmck frasercrmck changed the title [AMDGPU][SplitModule] Fix a couple of issues [AMDGPU][SplitModule] Fix unintentional integer division Nov 25, 2024
@frasercrmck frasercrmck force-pushed the amdgpu-fix-split-module-ratio branch from 64efad2 to 36267e1 Compare November 25, 2024 18:17
@frasercrmck
Copy link
Contributor Author

Description should be more specific

Yep, you're right, sorry. I've just split it into two PRs since we can't merge individual commits in the same PR

llvm/test/tools/llvm-split/AMDGPU/large-kernels-merging.ll Outdated Show resolved Hide resolved
@@ -1101,7 +1101,7 @@ void RecursiveSearchSplitting::pickPartition(unsigned Depth, unsigned Idx,
// Check if the amount of code in common makes it worth it.
assert(SimilarDepsCost && Entry.CostExcludingGraphEntryPoints);
const double Ratio =
SimilarDepsCost / Entry.CostExcludingGraphEntryPoints;
(double)SimilarDepsCost / Entry.CostExcludingGraphEntryPoints;
assert(Ratio >= 0.0 && Ratio <= 1.0);
if (LargeFnOverlapForMerge > Ratio) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be reversed, so Ratio > LargeFnOverlapForMerge

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep good spot, thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't reply inline with your other comment for some reason, but it does indeed reverse the changes to the test.

A static analysis tool warned that a division was always being performed
in integer division, so was either 0.0 or 1.0.

This doesn't seem intentional, so has been fixed to return a true ratio
using floating-point division. This in turn showed a bug where a
comparison against this ratio was incorrect.
@frasercrmck frasercrmck force-pushed the amdgpu-fix-split-module-ratio branch from 36267e1 to 70ce91d Compare November 26, 2024 10:30
@frasercrmck frasercrmck merged commit 345b331 into llvm:main Nov 27, 2024
8 checks passed
@frasercrmck frasercrmck deleted the amdgpu-fix-split-module-ratio branch November 27, 2024 10:18
searlmc1 pushed a commit to ROCm/llvm-project that referenced this pull request Dec 23, 2024
A static analysis tool warned that a division was always being performed
in integer division, so was either 0.0 or 1.0.

This doesn't seem intentional, so has been fixed to return a true ratio
using floating-point division. This in turn showed a bug where a
comparison against this ratio was incorrect.

Change-Id: Ia8b755339a7ddc2d3bdf5ba701bd5724b00488d2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants