[PATCH] D149768: [ArgumentPromotion] Bail if any callers are minsize

Arthur Eubanks via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 3 09:59:36 PDT 2023


aeubanks created this revision.
aeubanks added a reviewer: nikic.
Herald added subscribers: hoy, ormris, StephenFan, hiraditya.
Herald added a project: All.
aeubanks requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Argument promotion mostly works on functions with more than one caller (otherwise the function would be inlined or is dead), so there's a good chance that performing this increases code size since we introduce loads at every call site. If any caller is marked minsize, bail.

We could compare the number of loads/stores removed from the function with the number of loads introduced in callers, but that's TODO.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149768

Files:
  llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
  llvm/test/Transforms/ArgumentPromotion/minsize.ll


Index: llvm/test/Transforms/ArgumentPromotion/minsize.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/ArgumentPromotion/minsize.ll
@@ -0,0 +1,63 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
+; RUN: opt -passes=argpromotion -S < %s | FileCheck %s
+
+define internal i32 @f1(ptr %p) {
+; CHECK-LABEL: define internal i32 @f1
+; CHECK-SAME: (i32 [[P_0_VAL:%.*]]) {
+; CHECK-NEXT:    ret i32 [[P_0_VAL]]
+;
+  %i = load i32, ptr %p
+  ret i32 %i
+}
+
+define i32 @g1(ptr %p) {
+; CHECK-LABEL: define i32 @g1
+; CHECK-SAME: (ptr [[P:%.*]]) {
+; CHECK-NEXT:    [[P_VAL:%.*]] = load i32, ptr [[P]], align 4
+; CHECK-NEXT:    [[I:%.*]] = call i32 @f1(i32 [[P_VAL]])
+; CHECK-NEXT:    ret i32 [[I]]
+;
+  %i = call i32 @f1(ptr %p)
+  ret i32 %i
+}
+
+define i32 @g2(ptr %p) {
+; CHECK-LABEL: define i32 @g2
+; CHECK-SAME: (ptr [[P:%.*]]) {
+; CHECK-NEXT:    [[P_VAL:%.*]] = load i32, ptr [[P]], align 4
+; CHECK-NEXT:    [[I:%.*]] = call i32 @f1(i32 [[P_VAL]])
+; CHECK-NEXT:    ret i32 [[I]]
+;
+  %i = call i32 @f1(ptr %p)
+  ret i32 %i
+}
+
+define internal i32 @f2(ptr %p) {
+; CHECK-LABEL: define internal i32 @f2
+; CHECK-SAME: (ptr [[P:%.*]]) {
+; CHECK-NEXT:    [[I:%.*]] = load i32, ptr [[P]], align 4
+; CHECK-NEXT:    ret i32 [[I]]
+;
+  %i = load i32, ptr %p
+  ret i32 %i
+}
+
+define i32 @h1(ptr %p) minsize {
+; CHECK-LABEL: define i32 @h1
+; CHECK-SAME: (ptr [[P:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:    [[I:%.*]] = call i32 @f2(ptr [[P]])
+; CHECK-NEXT:    ret i32 [[I]]
+;
+  %i = call i32 @f2(ptr %p)
+  ret i32 %i
+}
+
+define i32 @h2(ptr %p) {
+; CHECK-LABEL: define i32 @h2
+; CHECK-SAME: (ptr [[P:%.*]]) {
+; CHECK-NEXT:    [[I:%.*]] = call i32 @f2(ptr [[P]])
+; CHECK-NEXT:    ret i32 [[I]]
+;
+  %i = call i32 @f2(ptr %p)
+  ret i32 %i
+}
Index: llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
===================================================================
--- llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
+++ llvm/lib/Transforms/IPO/ArgumentPromotion.cpp
@@ -764,6 +764,15 @@
     if (CB->isMustTailCall())
       return nullptr;
 
+    // If the caller is marked minsize, this transformation may increase code
+    // size. We assume that there is more than one call to this function since
+    // otherwise this function would be inlined or is dead.
+    // TODO: compare the number of loads/stores removed from the function with
+    // the number of introduced loads in callees to see if this is profitable
+    // code-size-wise.
+    if (CB->getFunction()->hasMinSize())
+      return nullptr;
+
     if (CB->getFunction() == F)
       IsRecursive = true;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149768.519140.patch
Type: text/x-patch
Size: 2709 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230503/57c24c14/attachment-0001.bin>


More information about the llvm-commits mailing list