Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend: de-virtualize regalloc #2265

Merged
merged 9 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
354 changes: 177 additions & 177 deletions internal/engine/wazevo/backend/backend_test.go

Large diffs are not rendered by default.

26 changes: 12 additions & 14 deletions internal/engine/wazevo/backend/compiler_lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func (c *compiler) Lower() {
c.assignVirtualRegisters()
c.mach.SetCurrentABI(c.GetFunctionABI(c.ssaBuilder.Signature()))
c.mach.ExecutableContext().StartLoweringFunction(c.ssaBuilder.BlockIDMax())
c.mach.StartLoweringFunction(c.ssaBuilder.BlockIDMax())
c.lowerBlocks()
}

Expand All @@ -20,21 +20,19 @@ func (c *compiler) lowerBlocks() {
c.lowerBlock(blk)
}

ectx := c.mach.ExecutableContext()
// After lowering all blocks, we need to link adjacent blocks to layout one single instruction list.
var prev ssa.BasicBlock
for next := builder.BlockIteratorReversePostOrderBegin(); next != nil; next = builder.BlockIteratorReversePostOrderNext() {
if prev != nil {
ectx.LinkAdjacentBlocks(prev, next)
c.mach.LinkAdjacentBlocks(prev, next)
}
prev = next
}
}

func (c *compiler) lowerBlock(blk ssa.BasicBlock) {
mach := c.mach
ectx := mach.ExecutableContext()
ectx.StartBlock(blk)
mach.StartBlock(blk)

// We traverse the instructions in reverse order because we might want to lower multiple
// instructions together.
Expand Down Expand Up @@ -76,15 +74,15 @@ func (c *compiler) lowerBlock(blk ssa.BasicBlock) {
default:
mach.LowerInstr(cur)
}
ectx.FlushPendingInstructions()
mach.FlushPendingInstructions()
}

// Finally, if this is the entry block, we have to insert copies of arguments from the real location to the VReg.
if blk.EntryBlock() {
c.lowerFunctionArguments(blk)
}

ectx.EndBlock()
mach.EndBlock()
}

// lowerBranches is called right after StartBlock and before any LowerInstr call if
Expand All @@ -93,15 +91,15 @@ func (c *compiler) lowerBlock(blk ssa.BasicBlock) {
//
// See ssa.Instruction IsBranching, and the comment on ssa.BasicBlock.
func (c *compiler) lowerBranches(br0, br1 *ssa.Instruction) {
ectx := c.mach.ExecutableContext()
mach := c.mach

c.setCurrentGroupID(br0.GroupID())
c.mach.LowerSingleBranch(br0)
ectx.FlushPendingInstructions()
mach.FlushPendingInstructions()
if br1 != nil {
c.setCurrentGroupID(br1.GroupID())
c.mach.LowerConditionalBranch(br1)
ectx.FlushPendingInstructions()
mach.FlushPendingInstructions()
}

if br0.Opcode() == ssa.OpcodeJump {
Expand All @@ -119,11 +117,11 @@ func (c *compiler) lowerBranches(br0, br1 *ssa.Instruction) {
c.lowerBlockArguments(args, target)
}
}
ectx.FlushPendingInstructions()
mach.FlushPendingInstructions()
}

func (c *compiler) lowerFunctionArguments(entry ssa.BasicBlock) {
ectx := c.mach.ExecutableContext()
mach := c.mach

c.tmpVals = c.tmpVals[:0]
for i := 0; i < entry.Params(); i++ {
Expand All @@ -135,8 +133,8 @@ func (c *compiler) lowerFunctionArguments(entry ssa.BasicBlock) {
c.tmpVals = append(c.tmpVals, ssa.ValueInvalid)
}
}
c.mach.LowerParams(c.tmpVals)
ectx.FlushPendingInstructions()
mach.LowerParams(c.tmpVals)
mach.FlushPendingInstructions()
}

// lowerBlockArguments lowers how to pass arguments to the given successor block.
Expand Down
221 changes: 0 additions & 221 deletions internal/engine/wazevo/backend/executable_context.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func TestMachineCompileEntryPreamble(t *testing.T) {
} {
t.Run(tc.name, func(t *testing.T) {
_, _, m := newSetupWithMockContext()
m.ectx.RootInstr = m.compileEntryPreamble(tc.sig)
m.rootInstr = m.compileEntryPreamble(tc.sig)
require.Equal(t, tc.exp, m.Format())
})
}
Expand Down
11 changes: 4 additions & 7 deletions internal/engine/wazevo/backend/isa/amd64/abi_go_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var calleeSavedVRegs = []regalloc.VReg{

// CompileGoFunctionTrampoline implements backend.Machine.
func (m *machine) CompileGoFunctionTrampoline(exitCode wazevoapi.ExitCode, sig *ssa.Signature, needModuleContextPtr bool) []byte {
ectx := m.ectx
argBegin := 1 // Skips exec context by default.
if needModuleContextPtr {
argBegin++
Expand All @@ -25,7 +24,7 @@ func (m *machine) CompileGoFunctionTrampoline(exitCode wazevoapi.ExitCode, sig *
m.currentABI = abi

cur := m.allocateNop()
ectx.RootInstr = cur
m.rootInstr = cur

// Execution context is always the first argument.
execCtrPtr := raxVReg
Expand Down Expand Up @@ -272,7 +271,7 @@ func (m *machine) CompileGoFunctionTrampoline(exitCode wazevoapi.ExitCode, sig *
cur = m.revertRBPRSP(cur)
linkInstr(cur, m.allocateInstr().asRet())

m.encodeWithoutSSA(ectx.RootInstr)
m.encodeWithoutSSA(m.rootInstr)
return m.c.Buf()
}

Expand Down Expand Up @@ -347,10 +346,8 @@ var stackGrowSaveVRegs = []regalloc.VReg{

// CompileStackGrowCallSequence implements backend.Machine.
func (m *machine) CompileStackGrowCallSequence() []byte {
ectx := m.ectx

cur := m.allocateNop()
ectx.RootInstr = cur
m.rootInstr = cur

cur = m.setupRBPRSP(cur)

Expand Down Expand Up @@ -379,7 +376,7 @@ func (m *machine) CompileStackGrowCallSequence() []byte {
cur = m.revertRBPRSP(cur)
linkInstr(cur, m.allocateInstr().asRet())

m.encodeWithoutSSA(ectx.RootInstr)
m.encodeWithoutSSA(m.rootInstr)
return m.c.Buf()
}

Expand Down
Loading
Loading