Skip to content

Revert "IRGen: fix alignment for tail allocated arrays with elements which have a large alignment" #14656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions lib/IRGen/GenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,10 +739,9 @@ static llvm::Value *stackPromote(IRGenFunction &IGF,
return Alloca.getAddress();
}

std::pair<llvm::Value *, llvm::Value *>
irgen::appendSizeForTailAllocatedArrays(IRGenFunction &IGF,
llvm::Value *size, llvm::Value *alignMask,
TailArraysRef TailArrays) {
llvm::Value *irgen::appendSizeForTailAllocatedArrays(IRGenFunction &IGF,
llvm::Value *size,
TailArraysRef TailArrays) {
for (const auto &TailArray : TailArrays) {
SILType ElemTy = TailArray.first;
llvm::Value *Count = TailArray.second;
Expand All @@ -751,17 +750,16 @@ irgen::appendSizeForTailAllocatedArrays(IRGenFunction &IGF,

// Align up to the tail-allocated array.
llvm::Value *ElemStride = ElemTI.getStride(IGF, ElemTy);
llvm::Value *ElemAlignMask = ElemTI.getAlignmentMask(IGF, ElemTy);
size = IGF.Builder.CreateAdd(size, ElemAlignMask);
llvm::Value *InvertedMask = IGF.Builder.CreateNot(ElemAlignMask);
llvm::Value *AlignMask = ElemTI.getAlignmentMask(IGF, ElemTy);
size = IGF.Builder.CreateAdd(size, AlignMask);
llvm::Value *InvertedMask = IGF.Builder.CreateNot(AlignMask);
size = IGF.Builder.CreateAnd(size, InvertedMask);

// Add the size of the tail allocated array.
llvm::Value *AllocSize = IGF.Builder.CreateMul(ElemStride, Count);
size = IGF.Builder.CreateAdd(size, AllocSize);
alignMask = IGF.Builder.CreateOr(alignMask, ElemAlignMask);
}
return {size, alignMask};
return size;
}


Expand Down Expand Up @@ -802,8 +800,7 @@ llvm::Value *irgen::emitClassAllocation(IRGenFunction &IGF, SILType selfType,
val = IGF.emitInitStackObjectCall(metadata, val, "reference.new");
} else {
// Allocate the object on the heap.
std::tie(size, alignMask)
= appendSizeForTailAllocatedArrays(IGF, size, alignMask, TailArrays);
size = appendSizeForTailAllocatedArrays(IGF, size, TailArrays);
val = IGF.emitAllocObjectCall(metadata, size, alignMask, "reference.new");
StackAllocSize = -1;
}
Expand All @@ -826,8 +823,7 @@ llvm::Value *irgen::emitClassAllocationDynamic(IRGenFunction &IGF,
= emitClassResilientInstanceSizeAndAlignMask(IGF,
selfType.getClassOrBoundGenericClass(),
metadata);
std::tie(size, alignMask)
= appendSizeForTailAllocatedArrays(IGF, size, alignMask, TailArrays);
size = appendSizeForTailAllocatedArrays(IGF, size, TailArrays);

llvm::Value *val = IGF.emitAllocObjectCall(metadata, size, alignMask,
"reference.new");
Expand Down
10 changes: 4 additions & 6 deletions lib/IRGen/GenClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,10 @@ namespace irgen {
typedef llvm::ArrayRef<std::pair<SILType, llvm::Value *>> TailArraysRef;

/// Adds the size for tail allocated arrays to \p size and returns the new
/// size value. Also updades the alignment mask to represent the alignment of
/// the largest element.
std::pair<llvm::Value *, llvm::Value *>
appendSizeForTailAllocatedArrays(IRGenFunction &IGF,
llvm::Value *size, llvm::Value *alignMask,
TailArraysRef TailArrays);
/// size value.
llvm::Value *appendSizeForTailAllocatedArrays(IRGenFunction &IGF,
llvm::Value *size,
TailArraysRef TailArrays);

/// Emit an allocation of a class.
/// The \p StackAllocSize is an in- and out-parameter. The passed value
Expand Down
12 changes: 3 additions & 9 deletions test/IRGen/tail_alloc.sil
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ bb0(%c1 : $Builtin.Word, %c2 : $Builtin.Word):
// CHECK-NEXT: [[S3:%[0-9]+]] = and i64 [[S1]], [[S2]]
// CHECK-NEXT: [[S4:%[0-9]+]] = mul i64 %stride, %0
// CHECK-NEXT: [[S5:%[0-9]+]] = add i64 [[S3]], [[S4]]
// CHECK-NEXT: [[A:%[0-9]+]] = or i64 7, %flags.alignmentMask
// CHECK: call noalias %swift.refcounted* @swift_allocObject(%swift.type* %{{[0-9]+}}, i64 [[S5]], i64 [[A]])
// CHECK: call noalias %swift.refcounted* @swift_allocObject(%swift.type* %{{[0-9]+}}, i64 [[S5]], i64 7)
// CHECK: ret
sil @alloc_generic : $@convention(thin) <T> (Builtin.Word, @thick T.Type) -> @owned TestClass {
bb0(%0 : $Builtin.Word, %1 : $@thick T.Type):
Expand All @@ -111,15 +110,10 @@ bb0(%0 : $Builtin.Word, %1 : $@thick T.Type):
// CHECK-NEXT: [[INT_PTR:%[0-9]+]] = bitcast i8* [[SIZE_ADDR]] to i32*
// CHECK-NEXT: [[SIZE:%[0-9]+]] = load i32, i32* [[INT_PTR]]
// CHECK-NEXT: [[SIZE64:%[0-9]+]] = zext i32 [[SIZE]] to i64
// CHECK-NEXT: [[ALIGN_ADDR:%[0-9]+]] = getelementptr inbounds i8, i8* [[BYTE_PTR]], i32 52
// CHECK-NEXT: [[SHORT_PTR:%[0-9]+]] = bitcast i8* [[ALIGN_ADDR]] to i16*
// CHECK-NEXT: [[ALIGN:%[0-9]+]] = load i16, i16* [[SHORT_PTR]]
// CHECK-NEXT: [[ALIGN64:%[0-9]+]] = zext i16 [[ALIGN]] to i64
// CHECK-NEXT: [[ALIGN_TMP:%[0-9]+]] = add i64 [[SIZE64]], 3
// CHECK: [[ALIGN_TMP:%[0-9]+]] = add i64 [[SIZE64]], 3
// CHECK-NEXT: [[ALIGNED:%[0-9]+]] = and i64 [[ALIGN_TMP]], -4
// CHECK-NEXT: [[TOTAL_SIZE:%[0-9]+]] = add i64 [[ALIGNED]], 12
// CHECK-NEXT: [[TOTAL_ALIGN:%[0-9]+]] = or i64 [[ALIGN64]], 3
// CHECK-NEXT: [[O:%[0-9]+]] = call noalias %swift.refcounted* @swift_allocObject(%swift.type* %0, i64 [[TOTAL_SIZE]], i64 [[TOTAL_ALIGN]])
// CHECK-NEXT: [[O:%[0-9]+]] = call noalias %swift.refcounted* @swift_allocObject(%swift.type* %0, i64 [[TOTAL_SIZE]], i64 {{.*}})
// CHECK-NEXT: [[O2:%[0-9]+]] = bitcast %swift.refcounted* [[O]] to %{{.*TestClassC}}*
// CHECK-NEXT: ret %{{.*TestClassC}}* [[O2]]
sil @alloc_dynamic : $@convention(thin) (@thick TestClass.Type) -> @owned TestClass {
Expand Down