Skip to content

[InlineCost] Print inline cost for invoke call sites as well #114476

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
Nov 1, 2024

Conversation

mshockwave
Copy link
Member

Previously InlineCostAnnotationPrinter only prints inline cost for call instructions. I don't think there is any reason not to analyze invoke and its callee, and this patch adds such support.

Previously InlineCostAnnotationPrinter only prints inline cost for call
instructions. I don't think there is any reason not to analyze invoke
and its callee, and this patch adds such support.
@llvmbot llvmbot added llvm:analysis Includes value tracking, cost tables and constant folding llvm:transforms labels Oct 31, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 31, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Min-Yih Hsu (mshockwave)

Changes

Previously InlineCostAnnotationPrinter only prints inline cost for call instructions. I don't think there is any reason not to analyze invoke and its callee, and this patch adds such support.


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

2 Files Affected:

  • (modified) llvm/lib/Analysis/InlineCost.cpp (+4-4)
  • (modified) llvm/test/Transforms/Inline/inline-cost-annotation-pass.ll (+31)
diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
index dd9ac910456ade..22bb406c01a4ed 100644
--- a/llvm/lib/Analysis/InlineCost.cpp
+++ b/llvm/lib/Analysis/InlineCost.cpp
@@ -3257,16 +3257,16 @@ InlineCostAnnotationPrinterPass::run(Function &F,
   const InlineParams Params = llvm::getInlineParams();
   for (BasicBlock &BB : F) {
     for (Instruction &I : BB) {
-      if (CallInst *CI = dyn_cast<CallInst>(&I)) {
-        Function *CalledFunction = CI->getCalledFunction();
+      if (auto *CB = dyn_cast<CallBase>(&I)) {
+        Function *CalledFunction = CB->getCalledFunction();
         if (!CalledFunction || CalledFunction->isDeclaration())
           continue;
         OptimizationRemarkEmitter ORE(CalledFunction);
-        InlineCostCallAnalyzer ICCA(*CalledFunction, *CI, Params, TTI,
+        InlineCostCallAnalyzer ICCA(*CalledFunction, *CB, Params, TTI,
                                     GetAssumptionCache, nullptr, &PSI, &ORE);
         ICCA.analyze();
         OS << "      Analyzing call of " << CalledFunction->getName()
-           << "... (caller:" << CI->getCaller()->getName() << ")\n";
+           << "... (caller:" << CB->getCaller()->getName() << ")\n";
         ICCA.print(OS);
         OS << "\n";
       }
diff --git a/llvm/test/Transforms/Inline/inline-cost-annotation-pass.ll b/llvm/test/Transforms/Inline/inline-cost-annotation-pass.ll
index 5c07159822ca1c..f59a565ed34bff 100644
--- a/llvm/test/Transforms/Inline/inline-cost-annotation-pass.ll
+++ b/llvm/test/Transforms/Inline/inline-cost-annotation-pass.ll
@@ -33,3 +33,34 @@ define ptr @main() {
   %2 = call ptr @foo()
   ret ptr %1
 }
+
+; Make sure it also analyzes invoke call sites.
+
+; CHECK:       Analyzing call of g... (caller:f)
+; CHECK: define i32 @g(i32 %v) {
+; CHECK: ; cost before = {{.*}}, cost after = {{.*}}, threshold before = {{.*}}, threshold after = {{.*}}, cost delta = {{.*}}
+; CHECK:   %p = icmp ugt i32 %v, 35
+; CHECK: ; cost before = {{.*}}, cost after = {{.*}}, threshold before = {{.*}}, threshold after = {{.*}}, cost delta = {{.*}}
+; CHECK:   %r = select i1 %p, i32 %v, i32 7
+; CHECK: ; cost before = {{.*}}, cost after = {{.*}}, threshold before = {{.*}}, threshold after = {{.*}}, cost delta = {{.*}}
+; CHECK:   ret i32 %r
+; CHECK: }
+define i32 @g(i32 %v) {
+  %p = icmp ugt i32 %v, 35
+  %r = select i1 %p, i32 %v, i32 7
+  ret i32 %r
+}
+
+define void @f(i32 %v, ptr %dst) personality ptr @__gxx_personality_v0 {
+  %v1 = invoke i32 @g(i32 %v)
+          to label %bb1 unwind label %bb2
+bb1:
+  store i32 %v1, ptr %dst
+  ret void
+bb2:
+  %lpad.loopexit80 = landingpad { ptr, i32 }
+          cleanup
+  ret void
+}
+
+declare i32 @__gxx_personality_v0(...)

@llvmbot
Copy link
Member

llvmbot commented Oct 31, 2024

@llvm/pr-subscribers-llvm-analysis

Author: Min-Yih Hsu (mshockwave)

Changes

Previously InlineCostAnnotationPrinter only prints inline cost for call instructions. I don't think there is any reason not to analyze invoke and its callee, and this patch adds such support.


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

2 Files Affected:

  • (modified) llvm/lib/Analysis/InlineCost.cpp (+4-4)
  • (modified) llvm/test/Transforms/Inline/inline-cost-annotation-pass.ll (+31)
diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
index dd9ac910456ade..22bb406c01a4ed 100644
--- a/llvm/lib/Analysis/InlineCost.cpp
+++ b/llvm/lib/Analysis/InlineCost.cpp
@@ -3257,16 +3257,16 @@ InlineCostAnnotationPrinterPass::run(Function &F,
   const InlineParams Params = llvm::getInlineParams();
   for (BasicBlock &BB : F) {
     for (Instruction &I : BB) {
-      if (CallInst *CI = dyn_cast<CallInst>(&I)) {
-        Function *CalledFunction = CI->getCalledFunction();
+      if (auto *CB = dyn_cast<CallBase>(&I)) {
+        Function *CalledFunction = CB->getCalledFunction();
         if (!CalledFunction || CalledFunction->isDeclaration())
           continue;
         OptimizationRemarkEmitter ORE(CalledFunction);
-        InlineCostCallAnalyzer ICCA(*CalledFunction, *CI, Params, TTI,
+        InlineCostCallAnalyzer ICCA(*CalledFunction, *CB, Params, TTI,
                                     GetAssumptionCache, nullptr, &PSI, &ORE);
         ICCA.analyze();
         OS << "      Analyzing call of " << CalledFunction->getName()
-           << "... (caller:" << CI->getCaller()->getName() << ")\n";
+           << "... (caller:" << CB->getCaller()->getName() << ")\n";
         ICCA.print(OS);
         OS << "\n";
       }
diff --git a/llvm/test/Transforms/Inline/inline-cost-annotation-pass.ll b/llvm/test/Transforms/Inline/inline-cost-annotation-pass.ll
index 5c07159822ca1c..f59a565ed34bff 100644
--- a/llvm/test/Transforms/Inline/inline-cost-annotation-pass.ll
+++ b/llvm/test/Transforms/Inline/inline-cost-annotation-pass.ll
@@ -33,3 +33,34 @@ define ptr @main() {
   %2 = call ptr @foo()
   ret ptr %1
 }
+
+; Make sure it also analyzes invoke call sites.
+
+; CHECK:       Analyzing call of g... (caller:f)
+; CHECK: define i32 @g(i32 %v) {
+; CHECK: ; cost before = {{.*}}, cost after = {{.*}}, threshold before = {{.*}}, threshold after = {{.*}}, cost delta = {{.*}}
+; CHECK:   %p = icmp ugt i32 %v, 35
+; CHECK: ; cost before = {{.*}}, cost after = {{.*}}, threshold before = {{.*}}, threshold after = {{.*}}, cost delta = {{.*}}
+; CHECK:   %r = select i1 %p, i32 %v, i32 7
+; CHECK: ; cost before = {{.*}}, cost after = {{.*}}, threshold before = {{.*}}, threshold after = {{.*}}, cost delta = {{.*}}
+; CHECK:   ret i32 %r
+; CHECK: }
+define i32 @g(i32 %v) {
+  %p = icmp ugt i32 %v, 35
+  %r = select i1 %p, i32 %v, i32 7
+  ret i32 %r
+}
+
+define void @f(i32 %v, ptr %dst) personality ptr @__gxx_personality_v0 {
+  %v1 = invoke i32 @g(i32 %v)
+          to label %bb1 unwind label %bb2
+bb1:
+  store i32 %v1, ptr %dst
+  ret void
+bb2:
+  %lpad.loopexit80 = landingpad { ptr, i32 }
+          cleanup
+  ret void
+}
+
+declare i32 @__gxx_personality_v0(...)

@mshockwave mshockwave merged commit 64314de into llvm:main Nov 1, 2024
9 of 11 checks passed
@mshockwave mshockwave deleted the patch/print-invoke-inline-cost branch November 1, 2024 16:55
smallp-o-p pushed a commit to smallp-o-p/llvm-project that referenced this pull request Nov 3, 2024
…4476)

Previously InlineCostAnnotationPrinter only prints inline cost for call
instructions. I don't think there is any reason not to analyze invoke
and its callee, and this patch adds such support.
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
…4476)

Previously InlineCostAnnotationPrinter only prints inline cost for call
instructions. I don't think there is any reason not to analyze invoke
and its callee, and this patch adds such support.
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 llvm:transforms
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants