wazeroir: removes allocations in wasmOpcodeSignature asymptotically (#1145)

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
Takeshi Yoneda
2023-02-20 16:52:21 -08:00
committed by GitHub
parent 9d8033df1b
commit f882996106
3 changed files with 84 additions and 16 deletions

View File

@@ -188,7 +188,8 @@ type compiler struct {
ensureTermination bool
// Pre-allocated bytes.Reader to be used in varous places.
br *bytes.Reader
br *bytes.Reader
funcTypeToSigs *funcTypeToIRSignatures
}
//lint:ignore U1000 for debugging only.
@@ -276,10 +277,18 @@ func CompileFunctions(enabledFeatures api.CoreFeatures, callFrameStackSizeInUint
tableTypes[i] = tables[i].Type
}
types := module.TypeSection
funcTypeToSigs := &funcTypeToIRSignatures{
indirectCalls: make([]*signature, len(types)),
directCalls: make([]*signature, len(types)),
wasmTypes: types,
}
var ret []*CompilationResult
for funcIndex := range module.FunctionSection {
typeID := module.FunctionSection[funcIndex]
sig := module.TypeSection[typeID]
sig := types[typeID]
code := module.CodeSection[funcIndex]
if code.GoFunc != nil {
// Assume the function might use memory if it has a parameter for the api.Module
@@ -300,8 +309,8 @@ func CompileFunctions(enabledFeatures api.CoreFeatures, callFrameStackSizeInUint
continue
}
r, err := compile(enabledFeatures, callFrameStackSizeInUint64, sig, code.Body,
code.LocalTypes, module.TypeSection, functions, globals, code.BodyOffsetInCodeSection,
module.DWARFLines != nil, ensureTermination)
code.LocalTypes, types, functions, globals, code.BodyOffsetInCodeSection,
module.DWARFLines != nil, ensureTermination, funcTypeToSigs)
if err != nil {
def := module.FunctionDefinitionSection[uint32(funcIndex)+module.ImportFuncCount()]
return nil, fmt.Errorf("failed to lower func[%s] to wazeroir: %w", def.DebugName(), err)
@@ -309,7 +318,7 @@ func CompileFunctions(enabledFeatures api.CoreFeatures, callFrameStackSizeInUint
r.IsHostFunction = code.IsHostFunction
r.Globals = globals
r.Functions = functions
r.Types = module.TypeSection
r.Types = types
r.HasMemory = hasMemory
r.HasTable = hasTable
r.HasDataInstances = hasDataInstances
@@ -335,6 +344,7 @@ func compile(enabledFeatures api.CoreFeatures,
bodyOffsetInCodeSection uint64,
needSourceOffset bool,
ensureTermination bool,
funcTypeToSigs *funcTypeToIRSignatures,
) (*CompilationResult, error) {
c := compiler{
enabledFeatures: enabledFeatures,
@@ -351,6 +361,7 @@ func compile(enabledFeatures api.CoreFeatures,
bodyOffsetInCodeSection: bodyOffsetInCodeSection,
ensureTermination: ensureTermination,
br: bytes.NewReader(nil),
funcTypeToSigs: funcTypeToSigs,
}
c.initializeStack()