[llvm] [Transforms][IPO] Add remarks for ArgumentPromotion and DeadArgumentE… (PR #105740)

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 29 22:02:26 PDT 2024


https://github.com/yonghong-song updated https://github.com/llvm/llvm-project/pull/105740

>From b7f5d6137e2899b9a9bc93bb41ed19eff7db66df Mon Sep 17 00:00:00 2001
From: Yonghong Song <yonghong.song at linux.dev>
Date: Thu, 22 Aug 2024 14:35:11 -0700
Subject: [PATCH] [Transforms][IPO] Add remarks for ArgumentPromotion and
 DeadArgumentElimination

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] https://github.com/llvm/llvm-project/issues/104678
---
 llvm/lib/Transforms/IPO/ArgumentPromotion.cpp     | 15 +++++++++++++++
 .../Transforms/IPO/DeadArgumentElimination.cpp    | 13 +++++++++++++
 .../ArgumentPromotion/2008-02-01-ReturnAttrs.ll   | 12 +++++++++++-
 .../DeadArgElim/2007-12-20-ParamAttrs.ll          | 11 ++++++++++-
 4 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp b/llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
index 452fff7898d0ea..1f9b546ed29996 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)
+               << "eliminating argument " << ore::NV("ArgName", I->getName())
+               << "(" << ore::NV("ArgIndex", ArgNo) << ")";
+      });
+
       NewArgIndices.push_back((unsigned)-1);
     } else {
       const auto &ArgParts = ArgsToPromote.find(&*I)->second;
@@ -147,6 +155,13 @@ doPromotion(Function *F, FunctionAnalysisManager &FAM,
         ArgAttrVec.push_back(AttributeSet());
       }
       ++NumArgumentsPromoted;
+      ORE.emit([&]() {
+        return OptimizationRemark(DEBUG_TYPE, "ArgumentPromoted", F)
+               << "promoting argument " << ore::NV("ArgName", I->getName())
+               << "(" << ore::NV("ArgIndex", ArgNo) << ")"
+               << " to pass by value";
+      });
+
       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..d1548592b1ce26 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)
+               << "eliminating argument " << ore::NV("ArgName", I->getName())
+               << "(" << ore::NV("ArgIndex", ArgI) << ")";
+      });
       LLVM_DEBUG(dbgs() << "DeadArgumentEliminationPass - Removing argument "
                         << ArgI << " (" << I->getName() << ") from "
                         << F->getName() << "\n");
@@ -810,6 +818,11 @@ bool DeadArgumentEliminationPass::removeDeadStuffFromFunction(Function *F) {
         NewRetIdxs[Ri] = RetTypes.size() - 1;
       } else {
         ++NumRetValsEliminated;
+
+        ORE.emit([&]() {
+          return OptimizationRemark(DEBUG_TYPE, "ReturnValueRemoved", F)
+                 << "removing return value " << std::to_string(Ri);
+        });
         LLVM_DEBUG(
             dbgs() << "DeadArgumentEliminationPass - Removing return value "
                    << Ri << " from " << F->getName() << "\n");
diff --git a/llvm/test/Transforms/ArgumentPromotion/2008-02-01-ReturnAttrs.ll b/llvm/test/Transforms/ArgumentPromotion/2008-02-01-ReturnAttrs.ll
index 95d8915a15be4f..daa4e1fb757d21 100644
--- a/llvm/test/Transforms/ArgumentPromotion/2008-02-01-ReturnAttrs.ll
+++ b/llvm/test/Transforms/ArgumentPromotion/2008-02-01-ReturnAttrs.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --scrub-attributes
-; RUN: opt < %s -passes=argpromotion -S | FileCheck %s
+; RUN: opt < %s -passes=argpromotion -S -pass-remarks-output=%t | FileCheck %s
+; RUN: cat %t | FileCheck -check-prefix=REMARK %s
 
 define internal i32 @deref(ptr %x) nounwind {
 ; CHECK-LABEL: define {{[^@]+}}@deref
@@ -7,6 +8,15 @@ define internal i32 @deref(ptr %x) nounwind {
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    ret i32 [[X_0_VAL]]
 ;
+; REMARK-LABEL: Function: deref
+; REMARK:       Args:
+; REMARK-NEXT:    - String:   'promoting argument '
+; REMARK-NEXT:    - ArgName:  x
+; REMARK-NEXT:    - String:   '('
+; REMARK-NEXT:    - ArgIndex: '0'
+; REMARK-NEXT:    - String:   ')'
+; REMARK-NEXT:    - String:   ' to pass by value'
+;
 entry:
   %temp2 = load i32, ptr %x, align 4
   ret i32 %temp2
diff --git a/llvm/test/Transforms/DeadArgElim/2007-12-20-ParamAttrs.ll b/llvm/test/Transforms/DeadArgElim/2007-12-20-ParamAttrs.ll
index 8298021df18668..c3f7d7df90bc4f 100644
--- a/llvm/test/Transforms/DeadArgElim/2007-12-20-ParamAttrs.ll
+++ b/llvm/test/Transforms/DeadArgElim/2007-12-20-ParamAttrs.ll
@@ -1,10 +1,19 @@
-; RUN: opt < %s -passes=deadargelim -S | FileCheck %s
+; RUN: opt < %s -passes=deadargelim -S -pass-remarks-output=%t | FileCheck %s
+; RUN: cat %t | FileCheck -check-prefix=REMARK %s
 
 %struct = type { }
 
 @g = global i8 0
 
 ; CHECK: define internal void @foo(i8 signext %y) [[NUW:#[0-9]+]]
+;
+; REMARK-LABEL: Function: foo
+; REMARK:       Args:
+; REMARK-NEXT:    - String:   'eliminating argument '
+; REMARK-NEXT:    - ArgName:  p
+; REMARK-NEXT:    - String:   '('
+; REMARK-NEXT:    - ArgIndex: '0'
+; REMARK-NEXT:    - String:   ')'
 
 define internal zeroext i8 @foo(ptr inreg %p, i8 signext %y, ... )  nounwind {
   store i8 %y, ptr @g



More information about the llvm-commits mailing list