Skip to content

Commit

Permalink
Revert "Skip copying param-originated locals to return registers if p…
Browse files Browse the repository at this point in the history
…ossible"

This reverts commit 93448cd.

The optimization was speculatively added as a peephole optimization for
a specific pattern. However, based on the profiling result, it seems
that the optimization does not provide a significant performance
improvement...
  • Loading branch information
kateinoigakukun committed Aug 24, 2024
1 parent a69f4c1 commit 76e8c34
Showing 1 changed file with 6 additions and 42 deletions.
48 changes: 6 additions & 42 deletions Sources/WasmKit/Translator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,8 @@ struct StackLayout {
return Instruction.Register(index) - paramResultBase
}

func isParamerterLocal(_ index: LocalIndex) -> Bool {
return index < type.parameters.count
}

func localReg(_ index: LocalIndex) -> Instruction.Register {
if isParamerterLocal(index) {
if index < type.parameters.count {
return paramReg(Int(index))
} else {
return Instruction.Register(index) - Instruction.Register(type.parameters.count)
Expand Down Expand Up @@ -385,7 +381,7 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
enum ValueSource {
case register(Instruction.Register)
case local(LocalIndex)

func intoRegister(layout: StackLayout) -> Instruction.Register {
switch self {
case .register(let register):
Expand All @@ -402,7 +398,7 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
private(set) var maxHeight: Int = 0
var height: Int { values.count }
let stackRegBase: Instruction.Register

init(stackRegBase: Instruction.Register) {
self.stackRegBase = stackRegBase
}
Expand Down Expand Up @@ -766,43 +762,11 @@ struct InstructionTranslator<Context: TranslatorContext>: InstructionVisitor {
}

private mutating func visitReturnLike() throws {
var copies: [(source: Instruction.Register, dest: Instruction.Register)] = []
preserveAllLocalsOnStack()
for (index, resultType) in self.type.results.enumerated().reversed() {
let result = try valueStack.pop(resultType)
let source = result.intoRegister(layout: stackLayout)
let dest = returnReg(index)
switch try valueStack.pop(resultType) {
case .local(let localIndex):
let source = localReg(localIndex)
if stackLayout.isParamerterLocal(localIndex) {
if source == dest {
// If the local is a parameter and the return value is placed in
// the same position, no need to copy.
// e.g. `(func (param i32) (result i32) (local.get 0))` is
// translated to no-op
continue
}
// For local variables originated from params, we need
// two copies to avoid the value being overwritten
// because parameters and results share the same stack
// space.

// 1.Copy the local variable to a temporary register
// first to save the value
let tmpReg = valueStack.stackRegBase + Instruction.Register(valueStack.height)
emitCopyStack(from: source, to: tmpReg)
// 2. Then schedule the copy from the temporary register
// to the return register
copies.append((tmpReg, dest))
} else {
// For local variables not originated from params, we
// can directly copy the value to the return register
copies.append((source, dest))
}
case .register(let register):
copies.append((register, dest))
}
}
// Emit copy operations targeting the return registers
for (source, dest) in copies {
emitCopyStack(from: source, to: dest)
}
}
Expand Down

0 comments on commit 76e8c34

Please sign in to comment.