Skip to content

[CostModel] Mark ssa_copy as free #75294

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

Merged
merged 1 commit into from
Dec 13, 2023
Merged

Conversation

davemgreen
Copy link
Collaborator

These are intrinsics are only used ephemerally and be should be given a zero cost.

@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Dec 13, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 13, 2023

@llvm/pr-subscribers-llvm-analysis

Author: David Green (davemgreen)

Changes

These are intrinsics are only used ephemerally and be should be given a zero cost.


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

2 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/BasicTTIImpl.h (+2)
  • (modified) llvm/test/Analysis/CostModel/ARM/intrinsic-cost-kinds.ll (+12-12)
diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
index e05ce2890a08c8..33db350a12102a 100644
--- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h
+++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h
@@ -1510,6 +1510,8 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
     Intrinsic::ID IID = ICA.getID();
     if (Function::isTargetIntrinsic(IID))
       return TargetTransformInfo::TCC_Basic;
+    if (IID == Intrinsic::ssa_copy)
+      return TargetTransformInfo::TCC_Free;
 
     if (ICA.isTypeBasedOnly())
       return getTypeBasedIntrinsicInstrCost(ICA, CostKind);
diff --git a/llvm/test/Analysis/CostModel/ARM/intrinsic-cost-kinds.ll b/llvm/test/Analysis/CostModel/ARM/intrinsic-cost-kinds.ll
index 9a9193b36de029..d58f3348e41fed 100644
--- a/llvm/test/Analysis/CostModel/ARM/intrinsic-cost-kinds.ll
+++ b/llvm/test/Analysis/CostModel/ARM/intrinsic-cost-kinds.ll
@@ -341,27 +341,27 @@ define void @memcpy(ptr %a, ptr %b, i32 %c) {
 define void @ssa_copy() {
   ; CHECK: %{{.*}} = llvm.intr.ssa.copy %{{.*}} : f32
 ; THRU-LABEL: 'ssa_copy'
-; THRU-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %i = call i32 @llvm.ssa.copy.i32(i32 undef)
-; THRU-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %f = call float @llvm.ssa.copy.f32(float undef)
-; THRU-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %p = call ptr @llvm.ssa.copy.p0(ptr undef)
+; THRU-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %i = call i32 @llvm.ssa.copy.i32(i32 undef)
+; THRU-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %f = call float @llvm.ssa.copy.f32(float undef)
+; THRU-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %p = call ptr @llvm.ssa.copy.p0(ptr undef)
 ; THRU-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: ret void
 ;
 ; LATE-LABEL: 'ssa_copy'
-; LATE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %i = call i32 @llvm.ssa.copy.i32(i32 undef)
-; LATE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %f = call float @llvm.ssa.copy.f32(float undef)
-; LATE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %p = call ptr @llvm.ssa.copy.p0(ptr undef)
+; LATE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %i = call i32 @llvm.ssa.copy.i32(i32 undef)
+; LATE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %f = call float @llvm.ssa.copy.f32(float undef)
+; LATE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %p = call ptr @llvm.ssa.copy.p0(ptr undef)
 ; LATE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: ret void
 ;
 ; SIZE-LABEL: 'ssa_copy'
-; SIZE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %i = call i32 @llvm.ssa.copy.i32(i32 undef)
-; SIZE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %f = call float @llvm.ssa.copy.f32(float undef)
-; SIZE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %p = call ptr @llvm.ssa.copy.p0(ptr undef)
+; SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %i = call i32 @llvm.ssa.copy.i32(i32 undef)
+; SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %f = call float @llvm.ssa.copy.f32(float undef)
+; SIZE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %p = call ptr @llvm.ssa.copy.p0(ptr undef)
 ; SIZE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: ret void
 ;
 ; SIZE_LATE-LABEL: 'ssa_copy'
-; SIZE_LATE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %i = call i32 @llvm.ssa.copy.i32(i32 undef)
-; SIZE_LATE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %f = call float @llvm.ssa.copy.f32(float undef)
-; SIZE_LATE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: %p = call ptr @llvm.ssa.copy.p0(ptr undef)
+; SIZE_LATE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %i = call i32 @llvm.ssa.copy.i32(i32 undef)
+; SIZE_LATE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %f = call float @llvm.ssa.copy.f32(float undef)
+; SIZE_LATE-NEXT:  Cost Model: Found an estimated cost of 0 for instruction: %p = call ptr @llvm.ssa.copy.p0(ptr undef)
 ; SIZE_LATE-NEXT:  Cost Model: Found an estimated cost of 1 for instruction: ret void
 ;
   %i = call i32 @llvm.ssa.copy.i32(i32 undef)

@fhahn
Copy link
Contributor

fhahn commented Dec 13, 2023

I am curious if you encountered cases where those intrinsics are still around after the middle-end optimization pipeline. I think all users of those intrinsics should clean them up, but there might be some places that got missed.

@davemgreen
Copy link
Collaborator Author

No the costing is done during a pass that adds and removes them. This could be handled specially in that pass, but it is more general fix to give a zero cost to everywhere.

@@ -1510,6 +1510,8 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
Intrinsic::ID IID = ICA.getID();
if (Function::isTargetIntrinsic(IID))
return TargetTransformInfo::TCC_Basic;
if (IID == Intrinsic::ssa_copy)
return TargetTransformInfo::TCC_Free;
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be in TargetTransformInfoImpl.h.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah I see. That is better, thanks.

These are intrinsics are only used ephemerally and be should be given a zero cost.
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@davemgreen davemgreen merged commit 7433120 into llvm:main Dec 13, 2023
@davemgreen davemgreen deleted the gh-ssacopyfree branch December 13, 2023 11:24
hazzlim added a commit that referenced this pull request Nov 4, 2024
Look through ssa_copy intrinsic calls when computing codesize bonus for
a specialization.

Also remove redundant logic to skip computing codesize bonus for
ssa_copy intrinsics, now these are considered zero-cost by TTI (in PR
#75294).
PhilippRados pushed a commit to PhilippRados/llvm-project that referenced this pull request Nov 6, 2024
…14247)

Look through ssa_copy intrinsic calls when computing codesize bonus for
a specialization.

Also remove redundant logic to skip computing codesize bonus for
ssa_copy intrinsics, now these are considered zero-cost by TTI (in PR
llvm#75294).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants