transform: fix incorrect alignment of heap-to-stack transform

It assumed the maximum alignment was equal to sizeof(void*), which is
definitely not the case. So this only worked more or less by accident
previously.

It now uses the alignment as specified by the frontend, or else
`unsafe.Alignof(complex128)` which is typically the maximum alignment of
a given platform (though this shouldn't really happen in practice: the
optimizer should keep the 'align' attribute in place).
This commit is contained in:
Ayke van Laethem
2024-06-25 20:14:05 +02:00
committed by Ron Evans
parent 571447c7c1
commit e6caa3fe9e
5 changed files with 62 additions and 31 deletions
+23 -8
View File
@@ -7,7 +7,7 @@ declare nonnull ptr @runtime.alloc(i32, ptr)
; Test allocating a single int (i32) that should be allocated on the stack.
define void @testInt() {
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
store i32 5, ptr %alloc
ret void
}
@@ -15,7 +15,7 @@ define void @testInt() {
; Test allocating an array of 3 i16 values that should be allocated on the
; stack.
define i16 @testArray() {
%alloc = call ptr @runtime.alloc(i32 6, ptr null)
%alloc = call align 2 ptr @runtime.alloc(i32 6, ptr null)
%alloc.1 = getelementptr i16, ptr %alloc, i32 1
store i16 5, ptr %alloc.1
%alloc.2 = getelementptr i16, ptr %alloc, i32 2
@@ -23,30 +23,45 @@ define i16 @testArray() {
ret i16 %val
}
; Test allocating objects with an unknown alignment.
define void @testUnknownAlign() {
%alloc32 = call ptr @runtime.alloc(i32 32, ptr null)
store i8 5, ptr %alloc32
%alloc24 = call ptr @runtime.alloc(i32 24, ptr null)
store i16 5, ptr %alloc24
%alloc12 = call ptr @runtime.alloc(i32 12, ptr null)
store i16 5, ptr %alloc12
%alloc6 = call ptr @runtime.alloc(i32 6, ptr null)
store i16 5, ptr %alloc6
%alloc3 = call ptr @runtime.alloc(i32 3, ptr null)
store i16 5, ptr %alloc3
ret void
}
; Call a function that will let the pointer escape, so the heap-to-stack
; transform shouldn't be applied.
define void @testEscapingCall() {
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%val = call ptr @escapeIntPtr(ptr %alloc)
ret void
}
define void @testEscapingCall2() {
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%val = call ptr @escapeIntPtrSometimes(ptr %alloc, ptr %alloc)
ret void
}
; Call a function that doesn't let the pointer escape.
define void @testNonEscapingCall() {
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%val = call ptr @noescapeIntPtr(ptr %alloc)
ret void
}
; Return the allocated value, which lets it escape.
define ptr @testEscapingReturn() {
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
ret ptr %alloc
}
@@ -55,7 +70,7 @@ define void @testNonEscapingLoop() {
entry:
br label %loop
loop:
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%ptr = call ptr @noescapeIntPtr(ptr %alloc)
%result = icmp eq ptr null, %ptr
br i1 %result, label %loop, label %end
@@ -65,7 +80,7 @@ end:
; Test a zero-sized allocation.
define void @testZeroSizedAlloc() {
%alloc = call ptr @runtime.alloc(i32 0, ptr null)
%alloc = call align 1 ptr @runtime.alloc(i32 0, ptr null)
%ptr = call ptr @noescapeIntPtr(ptr %alloc)
ret void
}
+22 -3
View File
@@ -22,14 +22,33 @@ define i16 @testArray() {
ret i16 %val
}
define void @testUnknownAlign() {
%stackalloc4 = alloca [32 x i8], align 8
%stackalloc3 = alloca [24 x i8], align 8
%stackalloc2 = alloca [12 x i8], align 8
%stackalloc1 = alloca [6 x i8], align 8
%stackalloc = alloca [3 x i8], align 8
store [32 x i8] zeroinitializer, ptr %stackalloc4, align 8
store i8 5, ptr %stackalloc4, align 1
store [24 x i8] zeroinitializer, ptr %stackalloc3, align 8
store i16 5, ptr %stackalloc3, align 2
store [12 x i8] zeroinitializer, ptr %stackalloc2, align 8
store i16 5, ptr %stackalloc2, align 2
store [6 x i8] zeroinitializer, ptr %stackalloc1, align 8
store i16 5, ptr %stackalloc1, align 2
store [3 x i8] zeroinitializer, ptr %stackalloc, align 8
store i16 5, ptr %stackalloc, align 2
ret void
}
define void @testEscapingCall() {
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%val = call ptr @escapeIntPtr(ptr %alloc)
ret void
}
define void @testEscapingCall2() {
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
%val = call ptr @escapeIntPtrSometimes(ptr %alloc, ptr %alloc)
ret void
}
@@ -42,7 +61,7 @@ define void @testNonEscapingCall() {
}
define ptr @testEscapingReturn() {
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
ret ptr %alloc
}