Skip to content

Commit bac2042

Browse files
committed
compiler: consistently pass layout and alignment to createAlloc
1 parent 0922e3e commit bac2042

11 files changed

Lines changed: 20 additions & 22 deletions

compiler/compiler.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,8 +2415,7 @@ func (b *builder) createExpr(expr ssa.Value) (llvm.Value, error) {
24152415
}
24162416
sliceSize := b.CreateBinOp(llvm.Mul, elemSizeValue, sliceCapCast, "makeslice.cap")
24172417
layoutValue := b.createObjectLayout(llvmElemType, expr.Pos())
2418-
slicePtr := b.createAlloc(sliceSize, layoutValue, 0, "makeslice.buf")
2419-
slicePtr.AddCallSiteAttribute(0, b.ctx.CreateEnumAttribute(llvm.AttributeKindID("align"), uint64(elemAlign)))
2418+
slicePtr := b.createAlloc(sliceSize, layoutValue, elemAlign, "makeslice.buf")
24202419

24212420
// Extend or truncate if necessary. This is safe as we've already done
24222421
// the bounds check.

compiler/defer.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,9 @@ func (b *builder) createDefer(instr *ssa.Defer) {
504504
// This may be hit a variable number of times, so use a heap allocation.
505505
size := b.targetData.TypeAllocSize(deferredCallType)
506506
sizeValue := llvm.ConstInt(b.uintptrType, size, false)
507-
nilPtr := llvm.ConstNull(b.dataPtrType)
508-
alloca = b.createAlloc(sizeValue, nilPtr, 0, "defer.alloc.call")
507+
layoutValue := b.createObjectLayout(deferredCallType, instr.Pos())
508+
align := b.targetData.ABITypeAlignment(deferredCallType)
509+
alloca = b.createAlloc(sizeValue, layoutValue, align, "defer.alloc.call")
509510
}
510511
if b.NeedsStackObjects {
511512
b.trackPointer(alloca)

compiler/func.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (b *builder) parseMakeClosure(expr *ssa.MakeClosure) (llvm.Value, error) {
112112

113113
// Store the bound variables in a single object, allocating it on the heap
114114
// if necessary.
115-
context := b.emitPointerPack(boundVars)
115+
context := b.emitPointerPack(boundVars, expr.Pos())
116116

117117
// Create the closure.
118118
_, fn := b.getFunction(f)

compiler/gc.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ func (b *builder) createAlloc(sizeValue, layoutValue llvm.Value, align int, comm
2929

3030
// Make the runtime call.
3131
call := b.createRuntimeCall(allocFunc, []llvm.Value{sizeValue, layoutValue}, comment)
32-
if align != 0 {
33-
// TODO: make sure all callsites set the correct alignment.
34-
call.AddCallSiteAttribute(0, b.ctx.CreateEnumAttribute(llvm.AttributeKindID("align"), uint64(align)))
35-
}
32+
call.AddCallSiteAttribute(0, b.ctx.CreateEnumAttribute(llvm.AttributeKindID("align"), uint64(align)))
3633

3734
return call
3835
}

compiler/goroutine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (b *builder) createGo(instr *ssa.Go) {
100100
prefix = b.fn.RelString(nil)
101101
}
102102

103-
paramBundle := b.emitPointerPack(params)
103+
paramBundle := b.emitPointerPack(params, instr.Pos())
104104
var stackSize llvm.Value
105105
callee := b.createGoroutineStartWrapper(funcType, funcPtr, prefix, hasContext, false, instr.Pos())
106106
if b.AutomaticStackSize {

compiler/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const (
8484
//
8585
// An interface value is a {typecode, value} tuple named runtime._interface.
8686
func (b *builder) createMakeInterface(val llvm.Value, typ types.Type, pos token.Pos) llvm.Value {
87-
itfValue := b.emitPointerPack([]llvm.Value{val})
87+
itfValue := b.emitPointerPack([]llvm.Value{val}, pos)
8888
itfType := b.getTypeCode(typ)
8989
itf := llvm.Undef(b.getLLVMRuntimeType("_interface"))
9090
itf = b.CreateInsertValue(itf, itfType, 0, "")

compiler/llvm.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (b *builder) emitLifetimeEnd(ptr, size llvm.Value) {
5454
// pointer value directly. It returns the pointer with the packed data.
5555
// If the values are all constants, they are be stored in a constant global and
5656
// deduplicated.
57-
func (b *builder) emitPointerPack(values []llvm.Value) llvm.Value {
57+
func (b *builder) emitPointerPack(values []llvm.Value, pos token.Pos) llvm.Value {
5858
valueTypes := make([]llvm.Type, len(values))
5959
for i, value := range values {
6060
valueTypes[i] = value.Type()
@@ -128,8 +128,9 @@ func (b *builder) emitPointerPack(values []llvm.Value) llvm.Value {
128128

129129
// Packed data is bigger than a pointer, so allocate it on the heap.
130130
sizeValue := llvm.ConstInt(b.uintptrType, size, false)
131+
layoutValue := b.createObjectLayout(packedType, pos)
131132
align := b.targetData.ABITypeAlignment(packedType)
132-
packedAlloc := b.createAlloc(sizeValue, llvm.ConstNull(b.dataPtrType), align, "")
133+
packedAlloc := b.createAlloc(sizeValue, layoutValue, align, "")
133134
if b.NeedsStackObjects {
134135
b.trackPointer(packedAlloc)
135136
}

compiler/testdata/defer-cortex-m-qemu.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ entry:
278278

279279
for.body: ; preds = %for.body, %entry
280280
%defer.next = load ptr, ptr %deferPtr, align 4
281-
%defer.alloc.call = call dereferenceable(12) ptr @runtime.alloc(i32 12, ptr null, ptr undef) #4
281+
%defer.alloc.call = call align 4 dereferenceable(12) ptr @runtime.alloc(i32 12, ptr nonnull inttoptr (i32 135 to ptr), ptr undef) #4
282282
store i32 0, ptr %defer.alloc.call, align 4
283283
%defer.alloc.call.repack1 = getelementptr inbounds nuw i8, ptr %defer.alloc.call, i32 4
284284
store ptr %defer.next, ptr %defer.alloc.call.repack1, align 4
@@ -330,7 +330,7 @@ for.loop: ; preds = %for.body, %entry
330330

331331
for.body: ; preds = %for.loop
332332
%defer.next = load ptr, ptr %deferPtr, align 4
333-
%defer.alloc.call = call dereferenceable(12) ptr @runtime.alloc(i32 12, ptr null, ptr undef) #4
333+
%defer.alloc.call = call align 4 dereferenceable(12) ptr @runtime.alloc(i32 12, ptr nonnull inttoptr (i32 135 to ptr), ptr undef) #4
334334
store i32 0, ptr %defer.alloc.call, align 4
335335
%defer.alloc.call.repack13 = getelementptr inbounds nuw i8, ptr %defer.alloc.call, i32 4
336336
store ptr %defer.next, ptr %defer.alloc.call.repack13, align 4

compiler/testdata/gc.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ entry:
131131
define hidden %runtime._interface @main.makeInterface(double %v.r, double %v.i, ptr %context) unnamed_addr #1 {
132132
entry:
133133
%stackalloc = alloca i8, align 1
134-
%0 = call align 8 dereferenceable(16) ptr @runtime.alloc(i32 16, ptr null, ptr undef) #3
134+
%0 = call align 8 dereferenceable(16) ptr @runtime.alloc(i32 16, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #3
135135
call void @runtime.trackPointer(ptr nonnull %0, ptr nonnull %stackalloc, ptr undef) #3
136136
store double %v.r, ptr %0, align 8
137137
%.repack1 = getelementptr inbounds nuw i8, ptr %0, i32 8

compiler/testdata/goroutine-cortex-m-qemu-tasks.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ define hidden void @main.closureFunctionGoroutine(ptr %context) unnamed_addr #0
6060
entry:
6161
%n = call align 4 dereferenceable(4) ptr @runtime.alloc(i32 4, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #11
6262
store i32 3, ptr %n, align 4
63-
%0 = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr null, ptr undef) #11
63+
%0 = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr nonnull inttoptr (i32 133 to ptr), ptr undef) #11
6464
store i32 5, ptr %0, align 4
6565
%1 = getelementptr inbounds nuw i8, ptr %0, i32 4
6666
store ptr %n, ptr %1, align 4
@@ -102,7 +102,7 @@ declare void @runtime.printunlock(ptr) #1
102102
; Function Attrs: nounwind
103103
define hidden void @main.funcGoroutine(ptr %fn.context, ptr %fn.funcptr, ptr %context) unnamed_addr #0 {
104104
entry:
105-
%0 = call align 4 dereferenceable(12) ptr @runtime.alloc(i32 12, ptr null, ptr undef) #11
105+
%0 = call align 4 dereferenceable(12) ptr @runtime.alloc(i32 12, ptr nonnull inttoptr (i32 391 to ptr), ptr undef) #11
106106
store i32 5, ptr %0, align 4
107107
%1 = getelementptr inbounds nuw i8, ptr %0, i32 4
108108
store ptr %fn.context, ptr %1, align 4
@@ -157,7 +157,7 @@ declare void @runtime.chanClose(ptr dereferenceable_or_null(36), ptr) #1
157157
; Function Attrs: nounwind
158158
define hidden void @main.startInterfaceMethod(ptr %itf.typecode, ptr %itf.value, ptr %context) unnamed_addr #0 {
159159
entry:
160-
%0 = call align 4 dereferenceable(16) ptr @runtime.alloc(i32 16, ptr null, ptr undef) #11
160+
%0 = call align 4 dereferenceable(16) ptr @runtime.alloc(i32 16, ptr nonnull inttoptr (i32 713 to ptr), ptr undef) #11
161161
store ptr %itf.value, ptr %0, align 4
162162
%1 = getelementptr inbounds nuw i8, ptr %0, i32 4
163163
store ptr @"main$string", ptr %1, align 4

0 commit comments

Comments
 (0)