Skip to content

Commit e6caa3f

Browse files
aykevldeadprogram
authored andcommitted
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).
1 parent 571447c commit e6caa3f

File tree

5 files changed

+62
-31
lines changed

5 files changed

+62
-31
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
golang.org/x/sys v0.21.0
2222
golang.org/x/tools v0.22.1-0.20240621165957-db513b091504
2323
gopkg.in/yaml.v2 v2.4.0
24-
tinygo.org/x/go-llvm v0.0.0-20240518103902-697964f2a9dc
24+
tinygo.org/x/go-llvm v0.0.0-20240627184919-3b50c76783a8
2525
)
2626

2727
require (

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
106106
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
107107
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
108108
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
109-
tinygo.org/x/go-llvm v0.0.0-20240518103902-697964f2a9dc h1:TCzibFa4oLu+njEP3fnRUmZ+QQeb8BjtOwctgcjzL0k=
110-
tinygo.org/x/go-llvm v0.0.0-20240518103902-697964f2a9dc/go.mod h1:GFbusT2VTA4I+l4j80b17KFK+6whv69Wtny5U+T8RR0=
109+
tinygo.org/x/go-llvm v0.0.0-20240627184919-3b50c76783a8 h1:bLsZXRUBavt++CJlMN7sppNziqu3LyamESLhFJcpqFQ=
110+
tinygo.org/x/go-llvm v0.0.0-20240627184919-3b50c76783a8/go.mod h1:GFbusT2VTA4I+l4j80b17KFK+6whv69Wtny5U+T8RR0=

transform/allocs.go

+14-17
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ func OptimizeAllocs(mod llvm.Module, printAllocs *regexp.Regexp, maxStackAlloc u
2929

3030
targetData := llvm.NewTargetData(mod.DataLayout())
3131
defer targetData.Dispose()
32-
ptrType := llvm.PointerType(mod.Context().Int8Type(), 0)
33-
builder := mod.Context().NewBuilder()
32+
ctx := mod.Context()
33+
builder := ctx.NewBuilder()
3434
defer builder.Dispose()
3535

36+
// Determine the maximum alignment on this platform.
37+
complex128Type := ctx.StructType([]llvm.Type{ctx.DoubleType(), ctx.DoubleType()}, false)
38+
maxAlign := int64(targetData.ABITypeAlignment(complex128Type))
39+
3640
for _, heapalloc := range getUses(allocator) {
3741
logAllocs := printAllocs != nil && printAllocs.MatchString(heapalloc.InstructionParent().Parent().Name())
3842
if heapalloc.Operand(0).IsAConstantInt().IsNil() {
@@ -90,21 +94,14 @@ func OptimizeAllocs(mod llvm.Module, printAllocs *regexp.Regexp, maxStackAlloc u
9094
}
9195
// The pointer value does not escape.
9296

93-
// Determine the appropriate alignment of the alloca. The size of the
94-
// allocation gives us a hint what the alignment should be.
95-
var alignment int
96-
if size%2 != 0 {
97-
alignment = 1
98-
} else if size%4 != 0 {
99-
alignment = 2
100-
} else if size%8 != 0 {
101-
alignment = 4
102-
} else {
103-
alignment = 8
104-
}
105-
if pointerAlignment := targetData.ABITypeAlignment(ptrType); pointerAlignment < alignment {
106-
// Use min(alignment, alignof(void*)) as the alignment.
107-
alignment = pointerAlignment
97+
// Determine the appropriate alignment of the alloca.
98+
attr := heapalloc.GetCallSiteEnumAttribute(0, llvm.AttributeKindID("align"))
99+
alignment := int(maxAlign)
100+
if !attr.IsNil() {
101+
// 'align' return value attribute is set, so use it.
102+
// This is basically always the case, but to be sure we'll default
103+
// to maxAlign if it isn't.
104+
alignment = int(attr.GetEnumValue())
108105
}
109106

110107
// Insert alloca in the entry block. Do it here so that mem2reg can

transform/testdata/allocs.ll

+23-8
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,61 @@ declare nonnull ptr @runtime.alloc(i32, ptr)
77

88
; Test allocating a single int (i32) that should be allocated on the stack.
99
define void @testInt() {
10-
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
10+
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
1111
store i32 5, ptr %alloc
1212
ret void
1313
}
1414

1515
; Test allocating an array of 3 i16 values that should be allocated on the
1616
; stack.
1717
define i16 @testArray() {
18-
%alloc = call ptr @runtime.alloc(i32 6, ptr null)
18+
%alloc = call align 2 ptr @runtime.alloc(i32 6, ptr null)
1919
%alloc.1 = getelementptr i16, ptr %alloc, i32 1
2020
store i16 5, ptr %alloc.1
2121
%alloc.2 = getelementptr i16, ptr %alloc, i32 2
2222
%val = load i16, ptr %alloc.2
2323
ret i16 %val
2424
}
2525

26+
; Test allocating objects with an unknown alignment.
27+
define void @testUnknownAlign() {
28+
%alloc32 = call ptr @runtime.alloc(i32 32, ptr null)
29+
store i8 5, ptr %alloc32
30+
%alloc24 = call ptr @runtime.alloc(i32 24, ptr null)
31+
store i16 5, ptr %alloc24
32+
%alloc12 = call ptr @runtime.alloc(i32 12, ptr null)
33+
store i16 5, ptr %alloc12
34+
%alloc6 = call ptr @runtime.alloc(i32 6, ptr null)
35+
store i16 5, ptr %alloc6
36+
%alloc3 = call ptr @runtime.alloc(i32 3, ptr null)
37+
store i16 5, ptr %alloc3
38+
ret void
39+
}
40+
2641
; Call a function that will let the pointer escape, so the heap-to-stack
2742
; transform shouldn't be applied.
2843
define void @testEscapingCall() {
29-
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
44+
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
3045
%val = call ptr @escapeIntPtr(ptr %alloc)
3146
ret void
3247
}
3348

3449
define void @testEscapingCall2() {
35-
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
50+
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
3651
%val = call ptr @escapeIntPtrSometimes(ptr %alloc, ptr %alloc)
3752
ret void
3853
}
3954

4055
; Call a function that doesn't let the pointer escape.
4156
define void @testNonEscapingCall() {
42-
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
57+
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
4358
%val = call ptr @noescapeIntPtr(ptr %alloc)
4459
ret void
4560
}
4661

4762
; Return the allocated value, which lets it escape.
4863
define ptr @testEscapingReturn() {
49-
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
64+
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
5065
ret ptr %alloc
5166
}
5267

@@ -55,7 +70,7 @@ define void @testNonEscapingLoop() {
5570
entry:
5671
br label %loop
5772
loop:
58-
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
73+
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
5974
%ptr = call ptr @noescapeIntPtr(ptr %alloc)
6075
%result = icmp eq ptr null, %ptr
6176
br i1 %result, label %loop, label %end
@@ -65,7 +80,7 @@ end:
6580

6681
; Test a zero-sized allocation.
6782
define void @testZeroSizedAlloc() {
68-
%alloc = call ptr @runtime.alloc(i32 0, ptr null)
83+
%alloc = call align 1 ptr @runtime.alloc(i32 0, ptr null)
6984
%ptr = call ptr @noescapeIntPtr(ptr %alloc)
7085
ret void
7186
}

transform/testdata/allocs.out.ll

+22-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,33 @@ define i16 @testArray() {
2222
ret i16 %val
2323
}
2424

25+
define void @testUnknownAlign() {
26+
%stackalloc4 = alloca [32 x i8], align 8
27+
%stackalloc3 = alloca [24 x i8], align 8
28+
%stackalloc2 = alloca [12 x i8], align 8
29+
%stackalloc1 = alloca [6 x i8], align 8
30+
%stackalloc = alloca [3 x i8], align 8
31+
store [32 x i8] zeroinitializer, ptr %stackalloc4, align 8
32+
store i8 5, ptr %stackalloc4, align 1
33+
store [24 x i8] zeroinitializer, ptr %stackalloc3, align 8
34+
store i16 5, ptr %stackalloc3, align 2
35+
store [12 x i8] zeroinitializer, ptr %stackalloc2, align 8
36+
store i16 5, ptr %stackalloc2, align 2
37+
store [6 x i8] zeroinitializer, ptr %stackalloc1, align 8
38+
store i16 5, ptr %stackalloc1, align 2
39+
store [3 x i8] zeroinitializer, ptr %stackalloc, align 8
40+
store i16 5, ptr %stackalloc, align 2
41+
ret void
42+
}
43+
2544
define void @testEscapingCall() {
26-
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
45+
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
2746
%val = call ptr @escapeIntPtr(ptr %alloc)
2847
ret void
2948
}
3049

3150
define void @testEscapingCall2() {
32-
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
51+
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
3352
%val = call ptr @escapeIntPtrSometimes(ptr %alloc, ptr %alloc)
3453
ret void
3554
}
@@ -42,7 +61,7 @@ define void @testNonEscapingCall() {
4261
}
4362

4463
define ptr @testEscapingReturn() {
45-
%alloc = call ptr @runtime.alloc(i32 4, ptr null)
64+
%alloc = call align 4 ptr @runtime.alloc(i32 4, ptr null)
4665
ret ptr %alloc
4766
}
4867

0 commit comments

Comments
 (0)