Skip to content

[Transforms][IPO] Add remarks for ArgumentPromotion and DeadArgumentE… #105740

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
Aug 31, 2024

Conversation

yonghong-song
Copy link
Contributor

@yonghong-song yonghong-song commented Aug 22, 2024

…limination

ArgumentPromotion and DeadArgumentElimination passes may change function signature. This makes bpf tracing difficult since users either not aware of signature change or need to poke into IR or assembly to understand the function signature change.

This patch enabled to emit some remarks so if recompiling with -foptimization-record-file=, users can check remarks to see what kind of signature changes for a particular function. The following are some examples for implemented remarks:

  Pass:            deadargelim
  Name:            ReturnValueRemoved
  DebugLoc:        { File: 'bpf-next/net/mptcp/protocol.c', Line: 572, Column: 0 }
  Function:        mptcp_check_data_fin
  Args:
    - String:          'removing return value '
    - String:          '0'

  Pass:            deadargelim
  Name:            ArgumentRemoved
  DebugLoc:        { File: 'bpf-next/kernel/bpf/syscall.c', Line: 1670, Column: 0 }
  Function:        map_delete_elem
  Args:
      - String:          'eliminating argument '
      - ArgName:         uattr.coerce0
      - String:          '('
      - ArgIndex:        '1'
      - String:          ')'

  Pass:            argpromotion
  Name:            ArgumentPromoted
  DebugLoc:        { File: 'bpf-next/net/mptcp/protocol.h', Line: 570, Column: 0 }
  Function:        mptcp_subflow_ctx
  Args:
    - String:          'promoting argument '
    - ArgName:         sk
    - String:          '('
    - ArgIndex:        '0'
    - String:          ')'
    - String:          ' to pass by value'

[1] #104678

@llvmbot
Copy link
Member

llvmbot commented Aug 22, 2024

@llvm/pr-subscribers-llvm-transforms

Author: None (yonghong-song)

Changes

…limination

ArgumentPromotion and DeadArgumentElimination passes may change function signature. This makes bpf tracing difficult since users either not aware of signature change or need to poke into IR or assembly to understand the function signature change.

This patch enabled to emit some remarks so if recompiling with -foptimization-record-file=<file>, users can check remarks to see what kind of signature changes for a particular function. The following are some examples for implemented remarks:

  Pass:            deadargelim
  Name:            ReturnValueRemoved
  DebugLoc:        { File: 'bpf-next/net/mptcp/protocol.c', Line: 572, Column: 0 }
  Function:        mptcp_check_data_fin
  Args:
    - Function:        mptcp_check_data_fin
      DebugLoc:        { File: 'bpf-next/net/mptcp/protocol.c', Line: 572,
                         Column: 0 }
    - String:          ' removing return value '
    - String:          '0'

  Pass:            deadargelim
  Name:            ArgumentRemoved
  DebugLoc:        { File: 'bpf-next/kernel/bpf/syscall.c', Line: 1670, Column: 0 }
  Function:        map_delete_elem
  Args:
    - Function:        map_delete_elem
      DebugLoc:        { File: 'bpf-next/kernel/bpf/syscall.c', Line: 1670,
                         Column: 0 }
    - String:          ' removing argument '
    - String:          '1'
    - String:          ' ('
    - String:          uattr.coerce0
    - String:          ')'

  Pass:            argpromotion
  Name:            ArgumentPromoted
  DebugLoc:        { File: 'bpf-next/net/mptcp/protocol.h', Line: 570, Column: 0 }
  Function:        mptcp_subflow_ctx
  Args:
    - Function:        mptcp_subflow_ctx
      DebugLoc:        { File: 'bpf-next/net/mptcp/protocol.h', Line: 570,
                         Column: 0 }
    - String:          ' promoting argument '
    - String:          '0'
    - String:          ' ('
    - String:          sk
    - String:          ')'

[1] #104678


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/IPO/ArgumentPromotion.cpp (+14)
  • (modified) llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp (+14)
diff --git a/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp b/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
index 452fff7898d0ea..9369d2be74bb7d 100644
--- a/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
+++ b/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
@@ -42,6 +42,7 @@
 #include "llvm/Analysis/CallGraph.h"
 #include "llvm/Analysis/Loads.h"
 #include "llvm/Analysis/MemoryLocation.h"
+#include "llvm/Analysis/OptimizationRemarkEmitter.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/Analysis/ValueTracking.h"
 #include "llvm/IR/Argument.h"
@@ -126,6 +127,7 @@ doPromotion(Function *F, FunctionAnalysisManager &FAM,
   // arguments.
   SmallVector<unsigned> NewArgIndices;
   AttributeList PAL = F->getAttributes();
+  OptimizationRemarkEmitter ORE(F);
 
   // First, determine the new argument list
   unsigned ArgNo = 0, NewArgNo = 0;
@@ -139,6 +141,12 @@ doPromotion(Function *F, FunctionAnalysisManager &FAM,
     } else if (I->use_empty()) {
       // Dead argument (which are always marked as promotable)
       ++NumArgumentsDead;
+      ORE.emit([&]() {
+        return OptimizationRemark(DEBUG_TYPE, "ArgumentRemoved", F)
+               << ore::NV("Function", F) << " removing argument "
+               << std::to_string(ArgNo) << " (" << I->getName() << ")";
+      });
+
       NewArgIndices.push_back((unsigned)-1);
     } else {
       const auto &ArgParts = ArgsToPromote.find(&*I)->second;
@@ -147,6 +155,12 @@ doPromotion(Function *F, FunctionAnalysisManager &FAM,
         ArgAttrVec.push_back(AttributeSet());
       }
       ++NumArgumentsPromoted;
+      ORE.emit([&]() {
+        return OptimizationRemark(DEBUG_TYPE, "ArgumentPromoted", F)
+               << ore::NV("Function", F) << " promoting argument "
+               << std::to_string(ArgNo) << " (" << I->getName() << ")";
+      });
+
       NewArgIndices.push_back((unsigned)-1);
       NewArgNo += ArgParts.size();
     }
diff --git a/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp b/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
index f5a7ab26a49e96..bb71993995cf18 100644
--- a/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
+++ b/llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp
@@ -19,6 +19,7 @@
 #include "llvm/Transforms/IPO/DeadArgumentElimination.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
+#include "llvm/Analysis/OptimizationRemarkEmitter.h"
 #include "llvm/IR/Argument.h"
 #include "llvm/IR/AttributeMask.h"
 #include "llvm/IR/Attributes.h"
@@ -748,6 +749,7 @@ bool DeadArgumentEliminationPass::removeDeadStuffFromFunction(Function *F) {
   // Set up to build a new list of parameter attributes.
   SmallVector<AttributeSet, 8> ArgAttrVec;
   const AttributeList &PAL = F->getAttributes();
+  OptimizationRemarkEmitter ORE(F);
 
   // Remember which arguments are still alive.
   SmallVector<bool, 10> ArgAlive(FTy->getNumParams(), false);
@@ -765,6 +767,12 @@ bool DeadArgumentEliminationPass::removeDeadStuffFromFunction(Function *F) {
       HasLiveReturnedArg |= PAL.hasParamAttr(ArgI, Attribute::Returned);
     } else {
       ++NumArgumentsEliminated;
+
+      ORE.emit([&]() {
+        return OptimizationRemark(DEBUG_TYPE, "ArgumentRemoved", F)
+               << ore::NV("Function", F) << " removing argument "
+               << std::to_string(ArgI) << " (" << I->getName() << ")";
+      });
       LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Removing argument "
                         << ArgI << " (" << I->getName() << ") from "
                         << F->getName() << "\n");
@@ -810,6 +818,12 @@ bool DeadArgumentEliminationPass::removeDeadStuffFromFunction(Function *F) {
         NewRetIdxs[Ri] = RetTypes.size() - 1;
       } else {
         ++NumRetValsEliminated;
+
+        ORE.emit([&]() {
+          return OptimizationRemark(DEBUG_TYPE, "ReturnValueRemoved", F)
+                 << ore::NV("Function", F) << " removing return value "
+                 << std::to_string(Ri);
+        });
         LLVM_DEBUG(
             dbgs() << "DeadArgumentEliminationPass - Removing return value "
                    << Ri << " from " << F->getName() << "\n");

@yonghong-song
Copy link
Contributor Author

cc @4ast @eddyz87 @anakryiko

@4ast
Copy link
Member

4ast commented Aug 22, 2024

That should be pretty useful. Is there a tool that can parse it programmatically or, so far, it's for human consumption only?

@yonghong-song
Copy link
Contributor Author

That should be pretty useful. Is there a tool that can parse it programmatically or, so far, it's for human consumption only?

Alexei, I know there is a meta internal tool to parse remarks to construct an inlining-decision tree. Maybe upstream have some tools to parse those remarks as well. I am not sure.

@@ -139,6 +141,12 @@ doPromotion(Function *F, FunctionAnalysisManager &FAM,
} else if (I->use_empty()) {
// Dead argument (which are always marked as promotable)
++NumArgumentsDead;
ORE.emit([&]() {
return OptimizationRemark(DEBUG_TYPE, "ArgumentRemoved", F)
<< ore::NV("Function", F) << " removing argument "
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: function name is printed twice, first for OptimizationRemark, next for NV, is it really necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion. The second function name is not needed. Will fix.

Copy link
Contributor

@david-xl david-xl left a comment

Choose a reason for hiding this comment

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

Also add a test case?

@yonghong-song
Copy link
Contributor Author

Also add a test case?

Looks like you are okay with the format? I will add a test case in the next revision.

@david-xl
Copy link
Contributor

Also add a test case?

Looks like you are okay with the format? I will add a test case in the next revision.

Adding a case will help get a better feel of the format too.

@yonghong-song
Copy link
Contributor Author

Also add a test case?

Looks like you are okay with the format? I will add a test case in the next revision.

Adding a case will help get a better feel of the format too.

@david-xl I just added two remark tests corresponding to optimization passes. Please take a look.

Copy link
Contributor

@david-xl david-xl left a comment

Choose a reason for hiding this comment

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

bike shedding on the format. Is it better to show the parameter name first in the message?

foo: eliminating argument 'x' (index 0)
foo: promoting argument 'p'(index 1) to pass by value

…limination

ArgumentPromotion and DeadArgumentElimination passes may change
function signature. This makes bpf tracing difficult since
users either not aware of signature change or need to poke
into IR or assembly to understand the function signature change.

This patch enabled to emit some remarks so if recompiling
with -foptimization-record-file=<file>, users can check remarks
to see what kind of signature changes for a particular function.
The following are some examples for implemented remarks:

  Pass:            deadargelim
  Name:            ReturnValueRemoved
  DebugLoc:        { File: 'bpf-next/net/mptcp/protocol.c', Line: 572, Column: 0 }
  Function:        mptcp_check_data_fin
  Args:
    - String:          'removing return value '
    - String:          '0'

  Pass:            deadargelim
  Name:            ArgumentRemoved
  DebugLoc:        { File: 'bpf-next/kernel/bpf/syscall.c', Line: 1670, Column: 0 }
  Function:        map_delete_elem
  Args:
    - String:          'eliminating argument '
    - ArgName:         uattr.coerce0
    - String:          '('
    - ArgIndex:        '1'
    - String:          ')'

  Pass:            argpromotion
  Name:            ArgumentPromoted
  DebugLoc:        { File: 'bpf-next/net/mptcp/protocol.h', Line: 570, Column: 0 }
  Function:        mptcp_subflow_ctx
  Args:
  - String:          'promoting argument '
  - ArgName:         sk
  - String:          '('
  - ArgIndex:        '0'
  - String:          ')'
  - String:          ' to pass by value'

  [1] llvm#104678
@yonghong-song
Copy link
Contributor Author

@david-xl I made some changes to the remark format based on your suggestion. Could you take a look?

Copy link
Contributor

@david-xl david-xl left a comment

Choose a reason for hiding this comment

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

LGTM (you may want to wait a few days in case there are other comments)

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.

Looks reasonable to me.

@yonghong-song yonghong-song merged commit 470f55f into llvm:main Aug 31, 2024
8 checks passed
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.

6 participants