compiler: pass large aggregates by pointer

LLVM ComputeValueVTs recursively expands arrays and structs into one
value type per scalar leaf. SelectionDAG call lowering allocates data
structures proportional to this count, which makes very large values
exhaust memory or crash LLVM.

Count scalar leaves and use pointers for internal parameters and results
when the count exceeds 1024. A result pointer is the first parameter,
and aggregate parameters point to read-only memory. Exported function
types are unchanged.

Keep these SSA values in memory and copy them with memcpy when needed.
Handle calls, interfaces, maps, channels, selects, defers, goroutines,
phis, and multiple results. Update the expected compiler IR and re-enable
the native compress/flate tests.
This commit is contained in:
Jake Bailey
2026-07-14 06:22:02 -07:00
committed by Damian Gryski
parent 3b7c9e24f5
commit 9e7d89d4d5
14 changed files with 793 additions and 234 deletions
+17 -7
View File
@@ -517,6 +517,10 @@ func (p *lowerInterfacesPass) defineInterfaceMethodFunc(fn llvm.Value, itf *inte
context := fn.LastParam()
actualType := llvm.PrevParam(context)
returnType := fn.GlobalValueType().ReturnType()
resultOffset := 0
if fn.GetStringAttributeAtIndex(-1, "tinygo-indirect-result").GetStringValue() == "true" {
resultOffset = 1
}
context.SetName("context")
actualType.SetName("actualType")
fn.SetLinkage(llvm.InternalLinkage)
@@ -526,9 +530,9 @@ func (p *lowerInterfacesPass) defineInterfaceMethodFunc(fn llvm.Value, itf *inte
// Collect the params that will be passed to the functions to call.
// These params exclude the receiver (which may actually consist of multiple
// parts).
params := make([]llvm.Value, fn.ParamsCount()-3)
params := make([]llvm.Value, fn.ParamsCount()-3-resultOffset)
for i := range params {
params[i] = fn.Param(i + 1)
params[i] = fn.Param(i + 1 + resultOffset)
}
params = append(params,
llvm.Undef(p.ptrType),
@@ -570,14 +574,20 @@ func (p *lowerInterfacesPass) defineInterfaceMethodFunc(fn llvm.Value, itf *inte
function := typ.getMethod(signature).function
p.builder.SetInsertPointAtEnd(bb)
receiver := fn.FirstParam()
receiver := fn.Param(resultOffset)
paramTypes := []llvm.Type{receiver.Type()}
for _, param := range params {
paramTypes = append(paramTypes, param.Type())
callParams := make([]llvm.Value, 0, len(params)+2+resultOffset)
if resultOffset != 0 {
callParams = append(callParams, fn.FirstParam())
}
callParams = append(callParams, receiver)
callParams = append(callParams, params...)
paramTypes := make([]llvm.Type, len(callParams))
for i, param := range callParams {
paramTypes[i] = param.Type()
}
functionType := llvm.FunctionType(returnType, paramTypes, false)
retval := p.builder.CreateCall(functionType, function, append([]llvm.Value{receiver}, params...), "")
retval := p.builder.CreateCall(functionType, function, callParams, "")
if retval.Type().TypeKind() == llvm.VoidTypeKind {
p.builder.CreateRetVoid()
} else {