mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-08 21:13:39 +00:00
compiler: refactor runtime.alloc call
This refactor makes no other changes.
This commit is contained in:
committed by
Ron Evans
parent
5f66260c3a
commit
7ffbcbc666
+2
-13
@@ -196,16 +196,6 @@ func newBuilder(c *compilerContext, irbuilder llvm.Builder, f *ssa.Function) *bu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the runtime.alloc function variant.
|
|
||||||
// This is normally just "alloc", but is "alloc_noheap" if the //go:noheap
|
|
||||||
// pragma is used.
|
|
||||||
func (b *builder) allocFunc() string {
|
|
||||||
if b.info.noheap {
|
|
||||||
return "alloc_noheap"
|
|
||||||
}
|
|
||||||
return "alloc"
|
|
||||||
}
|
|
||||||
|
|
||||||
type blockInfo struct {
|
type blockInfo struct {
|
||||||
// entry is the LLVM basic block corresponding to the start of this *ssa.Block.
|
// entry is the LLVM basic block corresponding to the start of this *ssa.Block.
|
||||||
entry llvm.BasicBlock
|
entry llvm.BasicBlock
|
||||||
@@ -2197,9 +2187,8 @@ func (b *builder) createExpr(expr ssa.Value) (llvm.Value, error) {
|
|||||||
}
|
}
|
||||||
sizeValue := llvm.ConstInt(b.uintptrType, size, false)
|
sizeValue := llvm.ConstInt(b.uintptrType, size, false)
|
||||||
layoutValue := b.createObjectLayout(typ, expr.Pos())
|
layoutValue := b.createObjectLayout(typ, expr.Pos())
|
||||||
buf := b.createRuntimeCall(b.allocFunc(), []llvm.Value{sizeValue, layoutValue}, expr.Comment)
|
|
||||||
align := b.targetData.ABITypeAlignment(typ)
|
align := b.targetData.ABITypeAlignment(typ)
|
||||||
buf.AddCallSiteAttribute(0, b.ctx.CreateEnumAttribute(llvm.AttributeKindID("align"), uint64(align)))
|
buf := b.createAlloc(sizeValue, layoutValue, align, expr.Comment)
|
||||||
return buf, nil
|
return buf, nil
|
||||||
} else {
|
} else {
|
||||||
buf := llvmutil.CreateEntryBlockAlloca(b.Builder, typ, expr.Comment)
|
buf := llvmutil.CreateEntryBlockAlloca(b.Builder, typ, expr.Comment)
|
||||||
@@ -2429,7 +2418,7 @@ func (b *builder) createExpr(expr ssa.Value) (llvm.Value, error) {
|
|||||||
}
|
}
|
||||||
sliceSize := b.CreateBinOp(llvm.Mul, elemSizeValue, sliceCapCast, "makeslice.cap")
|
sliceSize := b.CreateBinOp(llvm.Mul, elemSizeValue, sliceCapCast, "makeslice.cap")
|
||||||
layoutValue := b.createObjectLayout(llvmElemType, expr.Pos())
|
layoutValue := b.createObjectLayout(llvmElemType, expr.Pos())
|
||||||
slicePtr := b.createRuntimeCall(b.allocFunc(), []llvm.Value{sliceSize, layoutValue}, "makeslice.buf")
|
slicePtr := b.createAlloc(sliceSize, layoutValue, 0, "makeslice.buf")
|
||||||
slicePtr.AddCallSiteAttribute(0, b.ctx.CreateEnumAttribute(llvm.AttributeKindID("align"), uint64(elemAlign)))
|
slicePtr.AddCallSiteAttribute(0, b.ctx.CreateEnumAttribute(llvm.AttributeKindID("align"), uint64(elemAlign)))
|
||||||
|
|
||||||
// Extend or truncate if necessary. This is safe as we've already done
|
// Extend or truncate if necessary. This is safe as we've already done
|
||||||
|
|||||||
+1
-1
@@ -505,7 +505,7 @@ func (b *builder) createDefer(instr *ssa.Defer) {
|
|||||||
size := b.targetData.TypeAllocSize(deferredCallType)
|
size := b.targetData.TypeAllocSize(deferredCallType)
|
||||||
sizeValue := llvm.ConstInt(b.uintptrType, size, false)
|
sizeValue := llvm.ConstInt(b.uintptrType, size, false)
|
||||||
nilPtr := llvm.ConstNull(b.dataPtrType)
|
nilPtr := llvm.ConstNull(b.dataPtrType)
|
||||||
alloca = b.createRuntimeCall(b.allocFunc(), []llvm.Value{sizeValue, nilPtr}, "defer.alloc.call")
|
alloca = b.createAlloc(sizeValue, nilPtr, 0, "defer.alloc.call")
|
||||||
}
|
}
|
||||||
if b.NeedsStackObjects {
|
if b.NeedsStackObjects {
|
||||||
b.trackPointer(alloca)
|
b.trackPointer(alloca)
|
||||||
|
|||||||
@@ -10,6 +10,25 @@ import (
|
|||||||
"tinygo.org/x/go-llvm"
|
"tinygo.org/x/go-llvm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Heap-allocate a buffer of the given size. This will typically call
|
||||||
|
// runtime.alloc.
|
||||||
|
func (b *builder) createAlloc(sizeValue, layoutValue llvm.Value, align int, comment string) llvm.Value {
|
||||||
|
// Normally allocate using "runtime.alloc", but use "runtime.alloc_noheap"
|
||||||
|
// if the //go:noheap pragma is used.
|
||||||
|
allocFunc := "alloc"
|
||||||
|
if b.info.noheap {
|
||||||
|
allocFunc = "alloc_noheap"
|
||||||
|
}
|
||||||
|
|
||||||
|
call := b.createRuntimeCall(allocFunc, []llvm.Value{sizeValue, layoutValue}, comment)
|
||||||
|
if align != 0 {
|
||||||
|
// TODO: make sure all callsites set the correct alignment.
|
||||||
|
call.AddCallSiteAttribute(0, b.ctx.CreateEnumAttribute(llvm.AttributeKindID("align"), uint64(align)))
|
||||||
|
}
|
||||||
|
|
||||||
|
return call
|
||||||
|
}
|
||||||
|
|
||||||
// trackExpr inserts pointer tracking intrinsics for the GC if the expression is
|
// trackExpr inserts pointer tracking intrinsics for the GC if the expression is
|
||||||
// one of the expressions that need this.
|
// one of the expressions that need this.
|
||||||
func (b *builder) trackExpr(expr ssa.Value, value llvm.Value) {
|
func (b *builder) trackExpr(expr ssa.Value, value llvm.Value) {
|
||||||
|
|||||||
+1
-5
@@ -129,11 +129,7 @@ func (b *builder) emitPointerPack(values []llvm.Value) llvm.Value {
|
|||||||
// Packed data is bigger than a pointer, so allocate it on the heap.
|
// Packed data is bigger than a pointer, so allocate it on the heap.
|
||||||
sizeValue := llvm.ConstInt(b.uintptrType, size, false)
|
sizeValue := llvm.ConstInt(b.uintptrType, size, false)
|
||||||
align := b.targetData.ABITypeAlignment(packedType)
|
align := b.targetData.ABITypeAlignment(packedType)
|
||||||
packedAlloc := b.createRuntimeCall(b.allocFunc(), []llvm.Value{
|
packedAlloc := b.createAlloc(sizeValue, llvm.ConstNull(b.dataPtrType), align, "")
|
||||||
sizeValue,
|
|
||||||
llvm.ConstNull(b.dataPtrType),
|
|
||||||
}, "")
|
|
||||||
packedAlloc.AddCallSiteAttribute(0, b.ctx.CreateEnumAttribute(llvm.AttributeKindID("align"), uint64(align)))
|
|
||||||
if b.NeedsStackObjects {
|
if b.NeedsStackObjects {
|
||||||
b.trackPointer(packedAlloc)
|
b.trackPointer(packedAlloc)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user