Skip to content
Open
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
15 changes: 11 additions & 4 deletions go/ir/lift.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,12 +995,18 @@ func replaceAll(x, y Value) {
var rands []*Value
pxrefs := x.Referrers()
pyrefs := y.Referrers()
for _, instr := range *pxrefs {
refs := *pxrefs
// Referrers follow operand order, so repeated operands produce adjacent entries.
for i := 0; i < len(refs); {
instr := refs[i]
end := i + 1
for end < len(refs) && refs[end] == instr {
end++
}

switch instr := instr.(type) {
case *CompositeValue:
// Special case CompositeValue because it might have very large lists of operands
//
// OPT(dh): this loop is still expensive for large composite values
for i, rand := range instr.Values {
if rand == x {
instr.Values[i] = y
Expand All @@ -1017,8 +1023,9 @@ func replaceAll(x, y Value) {
}
}
if pyrefs != nil {
*pyrefs = append(*pyrefs, instr) // dups ok
*pyrefs = append(*pyrefs, refs[i:end]...) // dups ok
}
i = end
}
*pxrefs = nil // x is now unreferenced
}
Expand Down
56 changes: 56 additions & 0 deletions go/ir/lift_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ir

import (
"fmt"
"testing"
)

func TestReplaceAllRepeatedCompositeValue(t *testing.T) {
const size = 32

x := &Alloc{}
y := &Alloc{}
instr := &CompositeValue{Values: make([]Value, size)}
for i := range instr.Values {
instr.Values[i] = x
x.referrers = append(x.referrers, instr)
}

replaceAll(x, y)

for i, value := range instr.Values {
if value != y {
t.Fatalf("operand %d: got %p, want %p", i, value, y)
}
}
if len(x.referrers) != 0 {
t.Fatalf("old referrers: got %d, want 0", len(x.referrers))
}
if len(y.referrers) != size {
t.Fatalf("new referrers: got %d, want %d", len(y.referrers), size)
}
}

func BenchmarkReplaceAllRepeatedCompositeValue(b *testing.B) {
for _, size := range []int{16, 64, 256, 1024} {
b.Run(fmt.Sprintf("operands=%d", size), func(b *testing.B) {
x := &Alloc{}
y := &Alloc{}
instr := &CompositeValue{Values: make([]Value, size)}
refs := make([]Instruction, size)
for i := range refs {
refs[i] = instr
}
x.referrers = refs
for i := range instr.Values {
instr.Values[i] = x
}

b.ReportAllocs()
for b.Loop() {
replaceAll(x, y)
x, y = y, x
}
})
}
}