[llvm] 2d037f5 - [Inliner] Use "best" ret attribute when propagating attributes during inlining

Noah Goldstein via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 3 14:12:43 PDT 2023


Author: Noah Goldstein
Date: 2023-10-03T16:12:16-05:00
New Revision: 2d037f5aed34291a078e90afa1c09b9482d9a242

URL: https://github.com/llvm/llvm-project/commit/2d037f5aed34291a078e90afa1c09b9482d9a242
DIFF: https://github.com/llvm/llvm-project/commit/2d037f5aed34291a078e90afa1c09b9482d9a242.diff

LOG: [Inliner] Use "best" ret attribute when propagating attributes during inlining

For attributes assosiated with a value (like `dereferenceable(N)`)
instead of always using the attribute from the to-be inlined caller,
it should keep using the value at existing callsites that have the
attribute if the value is higher (provides more information).

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/InlineFunction.cpp
    llvm/test/Transforms/Inline/ret_attr_align_and_noundef.ll
    llvm/test/Transforms/Inline/ret_attr_update.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp
index 548f949277952ca..0b7b3de23613ccb 100644
--- a/llvm/lib/Transforms/Utils/InlineFunction.cpp
+++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp
@@ -1417,6 +1417,11 @@ static void AddReturnAttributes(CallBase &CB, ValueToValueMapTy &VMap) {
     // existing attribute value (i.e. attributes such as dereferenceable,
     // dereferenceable_or_null etc). See AttrBuilder::merge for more details.
     AttributeList AL = NewRetVal->getAttributes();
+    if (ValidUB.getDereferenceableBytes() < AL.getRetDereferenceableBytes())
+      ValidUB.removeAttribute(Attribute::Dereferenceable);
+    if (ValidUB.getDereferenceableOrNullBytes() <
+        AL.getRetDereferenceableOrNullBytes())
+      ValidUB.removeAttribute(Attribute::DereferenceableOrNull);
     AttributeList NewAL = AL.addRetAttributes(Context, ValidUB);
     // Attributes that may generate poison returns are a bit tricky. If we
     // propagate them, other uses of the callsite might have their behavior

diff  --git a/llvm/test/Transforms/Inline/ret_attr_align_and_noundef.ll b/llvm/test/Transforms/Inline/ret_attr_align_and_noundef.ll
index ddb94ef3fe4e070..574cbc835ef7ae9 100644
--- a/llvm/test/Transforms/Inline/ret_attr_align_and_noundef.ll
+++ b/llvm/test/Transforms/Inline/ret_attr_align_and_noundef.ll
@@ -132,7 +132,7 @@ define ptr @callee6() {
 
 define ptr @caller6_fail() {
 ; CHECK-LABEL: define ptr @caller6_fail() {
-; CHECK-NEXT:    [[R_I:%.*]] = call dereferenceable(8) ptr @foo()
+; CHECK-NEXT:    [[R_I:%.*]] = call dereferenceable(16) ptr @foo()
 ; CHECK-NEXT:    ret ptr [[R_I]]
 ;
   %r = call dereferenceable(8) ptr @callee6()
@@ -159,7 +159,7 @@ define ptr @callee7() {
 
 define ptr @caller7_fail() {
 ; CHECK-LABEL: define ptr @caller7_fail() {
-; CHECK-NEXT:    [[R_I:%.*]] = call dereferenceable_or_null(8) ptr @foo()
+; CHECK-NEXT:    [[R_I:%.*]] = call dereferenceable_or_null(16) ptr @foo()
 ; CHECK-NEXT:    ret ptr [[R_I]]
 ;
   %r = call dereferenceable_or_null(8) ptr @callee7()

diff  --git a/llvm/test/Transforms/Inline/ret_attr_update.ll b/llvm/test/Transforms/Inline/ret_attr_update.ll
index bba7147ad00467e..7004b5795125d14 100644
--- a/llvm/test/Transforms/Inline/ret_attr_update.ll
+++ b/llvm/test/Transforms/Inline/ret_attr_update.ll
@@ -153,7 +153,7 @@ define internal ptr @callee6(ptr %p) alwaysinline {
 define ptr @test6(ptr %ptr, i64 %x) {
 ; CHECK-LABEL: @test6(
 ; CHECK-NEXT:    [[GEP:%.*]] = getelementptr inbounds i8, ptr [[PTR:%.*]], i64 [[X:%.*]]
-; CHECK-NEXT:    [[R_I:%.*]] = call dereferenceable_or_null(12) ptr @foo(ptr [[GEP]])
+; CHECK-NEXT:    [[R_I:%.*]] = call dereferenceable_or_null(16) ptr @foo(ptr [[GEP]])
 ; CHECK-NEXT:    [[V_I:%.*]] = call ptr @baz(ptr [[GEP]])
 ; CHECK-NEXT:    ret ptr [[R_I]]
 ;


        


More information about the llvm-commits mailing list