[PATCH] D78810: [InstCombine] Check max alignment before adding attr on aligned_alloc

Uday Bondhugula via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 24 08:04:33 PDT 2020


bondhugula created this revision.
bondhugula added reviewers: clin1, jdoerfert.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

In InstCombineCalls, check maximum alignment before adding the aligment
attribute on aligned_alloc. Fixes 45654.
https://bugs.llvm.org/show_bug.cgi?id=45654


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D78810

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
  llvm/test/Transforms/InstCombine/deref-alloc-fns.ll


Index: llvm/test/Transforms/InstCombine/deref-alloc-fns.ll
===================================================================
--- llvm/test/Transforms/InstCombine/deref-alloc-fns.ll
+++ llvm/test/Transforms/InstCombine/deref-alloc-fns.ll
@@ -38,19 +38,24 @@
   ret i8* %call
 }
 
-declare noalias i8* @foo(i8*, i8*, i8*)
+declare noalias i8* @escape(i8*, i8*, i8*, i8*)
 
 define noalias i8* @aligned_alloc_dynamic_args(i64 %align, i64 %size) {
 ; CHECK-LABEL: @aligned_alloc_dynamic_args(
 ; CHECK-NEXT:    tail call noalias dereferenceable_or_null(1024) i8* @aligned_alloc(i64 %{{.*}}, i64 1024)
 ; CHECK-NEXT:    tail call noalias i8* @aligned_alloc(i64 0, i64 1024)
 ; CHECK-NEXT:    tail call noalias i8* @aligned_alloc(i64 32, i64 %{{.*}})
+; CHECK-NEXT:    tail call noalias dereferenceable_or_null(4096) i8* @aligned_alloc(i64 9223372036854775807, i64 4096)
 ;
+  ; No alignment attribute will be added in these cases.
   %call = tail call noalias i8* @aligned_alloc(i64 %align, i64 1024)
   %call_1 = tail call noalias i8* @aligned_alloc(i64 0, i64 1024)
   %call_2 = tail call noalias i8* @aligned_alloc(i64 32, i64 %size)
+  ; Alignment is more than the maximum alignment allowed.
+  ; 9223372036854775807 is 2^63 - 1.
+  %call_3 = tail call noalias i8* @aligned_alloc(i64 9223372036854775807, i64 4096)
 
-  call i8* @foo(i8* %call, i8* %call_1, i8* %call_2)
+  call i8* @escape(i8* %call, i8* %call_1, i8* %call_2, i8* %call_3)
   ret i8* %call
 }
 
Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -4466,10 +4466,12 @@
     Call.addAttribute(AttributeList::ReturnIndex,
                       Attribute::getWithDereferenceableOrNullBytes(
                           Call.getContext(), Op1C->getZExtValue()));
-    // Add alignment attribute if alignment is a power of two constant.
+    // Add an alignment attribute if the alignment is a power of two constant
+    // less than the maximum alignment.
     if (Op0C) {
       uint64_t AlignmentVal = Op0C->getZExtValue();
-      if (llvm::isPowerOf2_64(AlignmentVal))
+      if (isPowerOf2_64(AlignmentVal) &&
+          Op0C->getValue().ule(Value::MaximumAlignment))
         Call.addAttribute(AttributeList::ReturnIndex,
                           Attribute::getWithAlignment(Call.getContext(),
                                                       Align(AlignmentVal)));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78810.259887.patch
Type: text/x-patch
Size: 2536 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200424/d335925b/attachment.bin>


More information about the llvm-commits mailing list