Skip to content

Commit ab962c7

Browse files
Adaptively determine the number of constant slots
1 parent 718c480 commit ab962c7

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

Sources/WasmKit/Execution/Function.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ struct WasmFunctionEntity {
189189
type: runtime.value.resolveType(type),
190190
locals: code.locals,
191191
functionIndex: index,
192+
codeSize: code.expression.count,
192193
intercepting: runtime.value.interceptor != nil
193194
)
194195
let iseq = try code.withValue { code in

Sources/WasmKit/Execution/Instances.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,18 @@ public struct Instance {
133133
target.write(" '\(name)'")
134134
}
135135
target.write(" ====\n")
136+
guard case .uncompiled(let code) = function.wasm.code else {
137+
fatalError("Already compiled!?")
138+
}
136139
try function.ensureCompiled(runtime: RuntimeRef(runtime))
137140
let (iseq, locals, _) = function.assumeCompiled()
138141

139142
// Print slot space information
140-
let stackLayout = StackLayout(type: runtime.funcTypeInterner.resolve(function.type), numberOfLocals: locals)
143+
let stackLayout = StackLayout(
144+
type: runtime.funcTypeInterner.resolve(function.type),
145+
numberOfLocals: locals,
146+
codeSize: code.expression.count
147+
)
141148
stackLayout.dump(to: &target, iseq: iseq)
142149

143150
var context = InstructionPrintingContext(

Sources/WasmKit/Translator.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,18 @@ struct FrameHeaderLayout {
214214

215215
struct StackLayout {
216216
let frameHeader: FrameHeaderLayout
217-
let constantSlotSize: Int = 8
217+
let constantSlotSize: Int
218218
let numberOfLocals: Int
219219

220220
var stackRegBase: VReg {
221221
return VReg(numberOfLocals + constantSlotSize)
222222
}
223223

224-
init(type: FunctionType, numberOfLocals: Int) {
224+
init(type: FunctionType, numberOfLocals: Int, codeSize: Int) {
225225
self.frameHeader = FrameHeaderLayout(type: type)
226226
self.numberOfLocals = numberOfLocals
227+
// The number of constant slots is determined by the code size
228+
self.constantSlotSize = max(codeSize / 20, 4)
227229
}
228230

229231
func localReg(_ index: LocalIndex) -> VReg {
@@ -780,6 +782,7 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
780782
type: FunctionType,
781783
locals: [WasmTypes.ValueType],
782784
functionIndex: FunctionIndex,
785+
codeSize: Int,
783786
intercepting: Bool
784787
) {
785788
self.allocator = allocator
@@ -788,7 +791,11 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
788791
self.module = module
789792
self.iseqBuilder = ISeqBuilder(runtimeConfiguration: runtimeConfiguration)
790793
self.controlStack = ControlStack()
791-
self.stackLayout = StackLayout(type: type, numberOfLocals: locals.count)
794+
self.stackLayout = StackLayout(
795+
type: type,
796+
numberOfLocals: locals.count,
797+
codeSize: codeSize
798+
)
792799
self.valueStack = ValueStack(stackLayout: stackLayout)
793800
self.locals = Locals(types: type.parameters + locals)
794801
self.functionIndex = functionIndex

0 commit comments

Comments
 (0)