[PATCH] D121641: MemoryBuiltins: also check function definition for allocalign

Augie Fackler via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 6 07:41:29 PDT 2022


durin42 edited the summary of this revision.
durin42 updated this revision to Diff 420847.
Herald added a subscriber: dexonsmith.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121641/new/

https://reviews.llvm.org/D121641

Files:
  llvm/include/llvm/IR/InstrTypes.h
  llvm/lib/Analysis/MemoryBuiltins.cpp
  llvm/lib/IR/Instructions.cpp
  llvm/test/Transforms/InstCombine/InferAlignAttribute.ll


Index: llvm/test/Transforms/InstCombine/InferAlignAttribute.ll
===================================================================
--- llvm/test/Transforms/InstCombine/InferAlignAttribute.ll
+++ llvm/test/Transforms/InstCombine/InferAlignAttribute.ll
@@ -14,11 +14,10 @@
   ret i8* %call
 }
 
-; BUG: we don't check the declaration, only the callsite. This will be fixed in the next change.
 define i8* @widen_align_from_allocalign() {
 ; CHECK-LABEL: @widen_align_from_allocalign(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[CALL:%.*]] = tail call align 16 i8* @my_aligned_alloc(i32 noundef 320, i32 noundef 64)
+; CHECK-NEXT:    [[CALL:%.*]] = tail call align 64 i8* @my_aligned_alloc(i32 noundef 320, i32 noundef 64)
 ; CHECK-NEXT:    ret i8* [[CALL]]
 ;
 entry:
@@ -27,10 +26,12 @@
   ret i8* %call
 }
 
+; BUG: we shouldn't narrow this alignment since we already had a stronger
+; constraint, but we do.
 define i8* @dont_narrow_align_from_allocalign() {
 ; CHECK-LABEL: @dont_narrow_align_from_allocalign(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[CALL:%.*]] = tail call align 16 i8* @my_aligned_alloc(i32 noundef 320, i32 noundef 8)
+; CHECK-NEXT:    [[CALL:%.*]] = tail call align 8 i8* @my_aligned_alloc(i32 noundef 320, i32 noundef 8)
 ; CHECK-NEXT:    ret i8* [[CALL]]
 ;
 entry:
@@ -59,7 +60,7 @@
 ; CHECK-INLINE-NEXT:    ret i8* [[CALL_I]]
 ;
 ; CHECK-NOINLINE-LABEL: @allocalign_disappears(
-; CHECK-NOINLINE-NEXT:    [[CALL:%.*]] = tail call i8* @my_aligned_alloc_3(i32 42, i32 128)
+; CHECK-NOINLINE-NEXT:    [[CALL:%.*]] = tail call align 128 i8* @my_aligned_alloc_3(i32 42, i32 128)
 ; CHECK-NOINLINE-NEXT:    ret i8* [[CALL]]
 ;
   %call = tail call i8* @my_aligned_alloc_3(i32 42, i32 128)
Index: llvm/lib/IR/Instructions.cpp
===================================================================
--- llvm/lib/IR/Instructions.cpp
+++ llvm/lib/IR/Instructions.cpp
@@ -337,6 +337,18 @@
   return nullptr;
 }
 
+Value *CallBase::getAllocAlignArgOperand() const {
+  unsigned Index;
+
+  if (Attrs.hasAttrSomewhere(Attribute::AllocAlign, &Index))
+    return getArgOperand(Index - AttributeList::FirstArgIndex);
+  if (const Function *F = getCalledFunction())
+    if (F->getAttributes().hasAttrSomewhere(Attribute::AllocAlign, &Index))
+      return getArgOperand(Index - AttributeList::FirstArgIndex);
+
+  return nullptr;
+}
+
 /// Determine whether the argument or parameter has the given attribute.
 bool CallBase::paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
   assert(ArgNo < arg_size() && "Param index out of bounds!");
Index: llvm/lib/Analysis/MemoryBuiltins.cpp
===================================================================
--- llvm/lib/Analysis/MemoryBuiltins.cpp
+++ llvm/lib/Analysis/MemoryBuiltins.cpp
@@ -338,12 +338,7 @@
   if (FnData.hasValue() && FnData->AlignParam >= 0) {
     return V->getOperand(FnData->AlignParam);
   }
-  unsigned AllocAlignParam;
-  if (V->getAttributes().hasAttrSomewhere(Attribute::AllocAlign,
-                                          &AllocAlignParam)) {
-    return V->getOperand(AllocAlignParam-1);
-  }
-  return nullptr;
+  return V->getAllocAlignArgOperand();
 }
 
 /// When we're compiling N-bit code, and the user uses parameters that are
Index: llvm/include/llvm/IR/InstrTypes.h
===================================================================
--- llvm/include/llvm/IR/InstrTypes.h
+++ llvm/include/llvm/IR/InstrTypes.h
@@ -1828,6 +1828,10 @@
   /// operand value. Otherwise, return nullptr.
   Value *getReturnedArgOperand() const;
 
+  /// If one of the arguments has the 'allocalign' attribute, returns its
+  /// operand value. Otherwise, return nullptr.
+  Value *getAllocAlignArgOperand() const;
+ 
   /// Return true if the call should not be treated as a call to a
   /// builtin.
   bool isNoBuiltin() const {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121641.420847.patch
Type: text/x-patch
Size: 3815 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220406/08d84877/attachment.bin>


More information about the llvm-commits mailing list