mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-18 19:44:00 +00:00
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:
committed by
Damian Gryski
parent
3b7c9e24f5
commit
9e7d89d4d5
+25
-3
@@ -79,13 +79,27 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value)
|
||||
return llvmFn.GlobalValueType(), llvmFn
|
||||
}
|
||||
|
||||
retType := c.getLLVMResultType(fn.Signature)
|
||||
retType, indirectResult := c.hasIndirectResult(fn.Signature)
|
||||
if info.exported {
|
||||
indirectResult = false
|
||||
}
|
||||
|
||||
var paramInfos []paramInfo
|
||||
if indirectResult {
|
||||
paramInfos = append(paramInfos, paramInfo{
|
||||
llvmType: c.dataPtrType,
|
||||
name: "return",
|
||||
elemSize: c.targetData.TypeAllocSize(retType),
|
||||
})
|
||||
retType = c.ctx.VoidType()
|
||||
}
|
||||
for _, param := range getParams(fn.Signature) {
|
||||
paramType := c.getLLVMType(param.Type())
|
||||
paramFragmentInfos := c.expandFormalParamType(paramType, param.Name(), param.Type())
|
||||
paramInfos = append(paramInfos, paramFragmentInfos...)
|
||||
if info.exported {
|
||||
paramInfos = append(paramInfos, c.expandDirectFormalParamType(paramType, param.Name(), param.Type())...)
|
||||
} else {
|
||||
paramInfos = append(paramInfos, c.expandFormalParamType(paramType, param.Name(), param.Type())...)
|
||||
}
|
||||
}
|
||||
|
||||
// Add an extra parameter as the function context. This context is used in
|
||||
@@ -95,12 +109,20 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value)
|
||||
}
|
||||
|
||||
var paramTypes []llvm.Type
|
||||
hasIndirectABI := indirectResult
|
||||
for _, info := range paramInfos {
|
||||
paramTypes = append(paramTypes, info.llvmType)
|
||||
hasIndirectABI = hasIndirectABI || info.flags¶mIsIndirect != 0
|
||||
}
|
||||
|
||||
fnType := llvm.FunctionType(retType, paramTypes, info.variadic)
|
||||
llvmFn = llvm.AddFunction(c.mod, info.linkName, fnType)
|
||||
if hasIndirectABI {
|
||||
// Argument promotion only rewrites functions whose uses are all direct
|
||||
// calls. Keep an address use so LLVM cannot reconstruct the large
|
||||
// aggregate signature that this ABI exists to avoid.
|
||||
llvmutil.AppendToGlobal(c.mod, "llvm.used", llvmFn)
|
||||
}
|
||||
if strings.HasPrefix(c.Triple, "wasm") {
|
||||
// C functions without prototypes like this:
|
||||
// void foo();
|
||||
|
||||
Reference in New Issue
Block a user