Skip to content

Commit

Permalink
ssa: reuses slice on basicBlock.loopNestingForestChildren (#2232)
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake authored Jun 5, 2024
1 parent 8dd9d5a commit 506c922
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
9 changes: 6 additions & 3 deletions internal/engine/wazevo/ssa/basic_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type (

// loopNestingForestChildren holds the children of this block in the loop nesting forest.
// Non-empty if and only if this block is a loop header (i.e. loopHeader=true)
loopNestingForestChildren []BasicBlock
loopNestingForestChildren wazevoapi.VarLength[BasicBlock]

// reversePostOrder is used to sort all the blocks in the function in reverse post order.
// This is used in builder.LayoutBlocks.
Expand All @@ -128,6 +128,9 @@ type (
}
)

// basicBlockVarLengthNil is the default nil value for basicBlock.loopNestingForestChildren.
var basicBlockVarLengthNil = wazevoapi.NewNilVarLength[BasicBlock]()

const basicBlockIDReturnBlock = 0xffffffff

// Name implements BasicBlock.Name.
Expand Down Expand Up @@ -271,7 +274,7 @@ func resetBasicBlock(bb *basicBlock) {
bb.unknownValues = bb.unknownValues[:0]
bb.lastDefinitions = wazevoapi.ResetMap(bb.lastDefinitions)
bb.reversePostOrder = -1
bb.loopNestingForestChildren = bb.loopNestingForestChildren[:0]
bb.loopNestingForestChildren = basicBlockVarLengthNil
bb.loopHeader = false
bb.sibling = nil
bb.child = nil
Expand Down Expand Up @@ -364,7 +367,7 @@ func (bb *basicBlock) String() string {

// LoopNestingForestChildren implements BasicBlock.LoopNestingForestChildren.
func (bb *basicBlock) LoopNestingForestChildren() []BasicBlock {
return bb.loopNestingForestChildren
return bb.loopNestingForestChildren.View()
}

// LoopHeader implements BasicBlock.LoopHeader.
Expand Down
4 changes: 4 additions & 0 deletions internal/engine/wazevo/ssa/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func NewBuilder() Builder {
return &builder{
instructionsPool: wazevoapi.NewPool[Instruction](resetInstruction),
basicBlocksPool: wazevoapi.NewPool[basicBlock](resetBasicBlock),
varLengthBasicBlockPool: wazevoapi.NewVarLengthPool[BasicBlock](),
varLengthPool: wazevoapi.NewVarLengthPool[Value](),
valueAnnotations: make(map[ValueID]string),
signatures: make(map[SignatureID]*Signature),
Expand Down Expand Up @@ -177,6 +178,8 @@ type builder struct {
dominators []*basicBlock
sparseTree dominatorSparseTree

varLengthBasicBlockPool wazevoapi.VarLengthPool[BasicBlock]

// loopNestingForestRoots are the roots of the loop nesting forest.
loopNestingForestRoots []BasicBlock

Expand Down Expand Up @@ -219,6 +222,7 @@ func (b *builder) Init(s *Signature) {
b.instructionsPool.Reset()
b.basicBlocksPool.Reset()
b.varLengthPool.Reset()
b.varLengthBasicBlockPool.Reset()
b.donePreBlockLayoutPasses = false
b.doneBlockLayout = false
b.donePostBlockLayoutPasses = false
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/wazevo/ssa/pass_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func passBuildLoopNestingForest(b *builder) {
b.loopNestingForestRoots = append(b.loopNestingForestRoots, blk)
} else if n == ent {
} else if n.loopHeader {
n.loopNestingForestChildren = append(n.loopNestingForestChildren, blk)
n.loopNestingForestChildren = n.loopNestingForestChildren.Append(&b.varLengthBasicBlockPool, blk)
}
}

Expand All @@ -193,7 +193,7 @@ func passBuildLoopNestingForest(b *builder) {

func printLoopNestingForest(root *basicBlock, depth int) {
fmt.Println(strings.Repeat("\t", depth), "loop nesting forest root:", root.ID())
for _, child := range root.loopNestingForestChildren {
for _, child := range root.loopNestingForestChildren.View() {
fmt.Println(strings.Repeat("\t", depth+1), "child:", child.ID())
if child.LoopHeader() {
printLoopNestingForest(child.(*basicBlock), depth+2)
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/wazevo/ssa/pass_cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ func TestBuildLoopNestingForest(t *testing.T) {
for expBlkID, blk := range blocks {
expChildren := tc.expLNF.children[expBlkID]
var actualChildren []BasicBlockID
for _, child := range blk.loopNestingForestChildren {
for _, child := range blk.loopNestingForestChildren.View() {
actualChildren = append(actualChildren, child.(*basicBlock).id)
}
sort.Slice(actualChildren, func(i, j int) bool {
Expand Down

0 comments on commit 506c922

Please sign in to comment.