Skip to content

Commit

Permalink
[Attributor] Add initial support for vectors in AAPointerInfo
Browse files Browse the repository at this point in the history
While full support requires more work (see TODOs), this allows us to
handle vector writes with a single constant value properly. For now,
we can handle the same constant values stored to all elements if
everything is of a fixed size.
  • Loading branch information
jdoerfert committed Jan 23, 2023
1 parent 538cd2e commit f341807
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
35 changes: 33 additions & 2 deletions llvm/lib/Transforms/IPO/AttributorAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1352,10 +1352,41 @@ struct AAPointerInfoFloating : public AAPointerInfoImpl {

// Make a strictly ascending list of offsets as required by addAccess()
llvm::sort(Offsets);
auto Last = std::unique(Offsets.begin(), Offsets.end());
auto *Last = std::unique(Offsets.begin(), Offsets.end());
Offsets.erase(Last, Offsets.end());

Changed = Changed | addAccess(A, {Offsets, Size}, I, Content, Kind, &Ty);
VectorType *VT = dyn_cast<VectorType>(&Ty);
if (!VT || VT->getElementCount().isScalable() ||
!Content.value_or(nullptr) || !isa<Constant>(*Content) ||
(*Content)->getType() != VT ||
DL.getTypeStoreSize(VT->getElementType()).isScalable()) {
Changed = Changed | addAccess(A, {Offsets, Size}, I, Content, Kind, &Ty);
} else {
// Handle vector stores with constant content element-wise.
// TODO: We could look for the elements or create instructions
// representing them.
// TODO: We need to push the Content into the range abstraction
// (AA::RangeTy) to allow different content values for different
// ranges. ranges. Hence, support vectors storing different values.
Type *ElementType = VT->getElementType();
int64_t ElementSize = DL.getTypeStoreSize(ElementType).getFixedValue();
auto *ConstContent = cast<Constant>(*Content);
Type *Int32Ty = Type::getInt32Ty(ElementType->getContext());
SmallVector<int64_t> ElementOffsets(Offsets.begin(), Offsets.end());

for (int i = 0, e = VT->getElementCount().getFixedValue(); i != e; ++i) {
Value *ElementContent = ConstantExpr::getExtractElement(
ConstContent, ConstantInt::get(Int32Ty, i));

// Add the element access.
Changed = Changed | addAccess(A, {ElementOffsets, ElementSize}, I,
ElementContent, Kind, ElementType);

// Advance the offsets for the next element.
for (auto &ElementOffset : ElementOffsets)
ElementOffset += ElementSize;
}
}
return true;
};

Expand Down
63 changes: 63 additions & 0 deletions llvm/test/Transforms/Attributor/value-simplify-pointer-info-vec.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-attributes --check-globals
; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=3 -S < %s | FileCheck %s --check-prefixes=CHECK,TUNIT
; RUN: opt -aa-pipeline=basic-aa -passes=attributor-cgscc -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,CGSCC
;
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

define i32 @vec_write_0() {
; CHECK: Function Attrs: nofree norecurse nosync nounwind willreturn memory(none)
; CHECK-LABEL: define {{[^@]+}}@vec_write_0
; CHECK-SAME: () #[[ATTR0:[0-9]+]] {
; CHECK-NEXT: ret i32 0
;
%a = alloca <2 x i32>
store <2 x i32> <i32 0, i32 0>, ptr %a
%l1 = load i32, ptr %a
%g = getelementptr i32, ptr %a, i64 1
%l2 = load i32, ptr %g
%add = add i32 %l1, %l2
ret i32 %add
}

define i32 @vec_write_1() {
; CHECK: Function Attrs: nofree norecurse nosync nounwind willreturn memory(none)
; CHECK-LABEL: define {{[^@]+}}@vec_write_1
; CHECK-SAME: () #[[ATTR0]] {
; CHECK-NEXT: ret i32 10
;
%a = alloca <2 x i32>
store <2 x i32> <i32 5, i32 5>, ptr %a
%l1B = load i32, ptr %a
%g = getelementptr i32, ptr %a, i64 1
%l2B = load i32, ptr %g
%add = add i32 %l1B, %l2B
ret i32 %add
}

; TODO: We should support this.
define i32 @vec_write_2() {
; CHECK: Function Attrs: nofree norecurse nosync nounwind willreturn memory(none)
; CHECK-LABEL: define {{[^@]+}}@vec_write_2
; CHECK-SAME: () #[[ATTR0]] {
; CHECK-NEXT: [[A:%.*]] = alloca <2 x i32>, align 8
; CHECK-NEXT: store <2 x i32> <i32 3, i32 5>, ptr [[A]], align 8
; CHECK-NEXT: [[L1:%.*]] = load i32, ptr [[A]], align 8
; CHECK-NEXT: [[G:%.*]] = getelementptr i32, ptr [[A]], i64 1
; CHECK-NEXT: [[L2:%.*]] = load i32, ptr [[G]], align 4
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[L1]], [[L2]]
; CHECK-NEXT: ret i32 [[ADD]]
;
%a = alloca <2 x i32>
store <2 x i32> <i32 3, i32 5>, ptr %a
%l1 = load i32, ptr %a
%g = getelementptr i32, ptr %a, i64 1
%l2 = load i32, ptr %g
%add = add i32 %l1, %l2
ret i32 %add
}
;.
; CHECK: attributes #[[ATTR0]] = { nofree norecurse nosync nounwind willreturn memory(none) }
;.
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
; CGSCC: {{.*}}
; TUNIT: {{.*}}

0 comments on commit f341807

Please sign in to comment.