-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathil.go
688 lines (611 loc) · 13.8 KB
/
il.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
//go:generate stringer -type=ILBlockType
package il
import (
"fmt"
"io"
"sync"
"sync/atomic"
)
type ILBlockType byte
const (
ILList ILBlockType = iota
ILLoop
ILDataPtrAdd
ILDataAdd
ILDataSet
ILRead
ILWrite
ILDataAddVector
ILDataAddLinVector // param is offset of vector
)
// ILBlock represents an Intermediate Language Block of instruction(s)
type ILBlock struct {
typ ILBlockType
param int64
inner []*ILBlock
vec []byte
}
func NewILBlock(typ ILBlockType) *ILBlock {
b := new(ILBlock)
b.typ = typ
if b.typ == ILList || b.typ == ILLoop {
b.inner = make([]*ILBlock, 0, 0)
}
return b
}
func (b *ILBlock) GetType() ILBlockType {
return b.typ
}
func (b *ILBlock) GetParam() int64 {
return b.param
}
func (b *ILBlock) GetVector() []byte {
var v = make([]byte, len(b.vec))
copy(v, b.vec)
return v
}
func (b *ILBlock) SetParam(param int64) {
b.param = param
}
func (b *ILBlock) ResetInner(size int) {
if size < 0 {
size = len(b.inner)
}
b.inner = make([]*ILBlock, 0, size)
}
func (b *ILBlock) Append(bs ...*ILBlock) {
b.inner = append(b.inner, bs...)
}
func (b *ILBlock) GetInner() []*ILBlock {
return b.inner
}
func (b *ILBlock) GetLast() *ILBlock {
return b.inner[len(b.inner)-1]
}
func (b *ILBlock) Dump(out io.Writer, indent int) {
const indentWidth = 4
fmt.Fprintf(out, "%*s--------------------------\n", indent*indentWidth, "")
if b == nil {
fmt.Fprintf(out, "%*s<nil>\n", indent*indentWidth, "")
return
}
fmt.Fprintf(out, "%*s| %-12v |", indent*indentWidth, "", b.typ)
switch b.typ {
case ILList, ILLoop:
case ILDataAdd, ILDataPtrAdd, ILDataSet:
fmt.Fprintf(out, " param=%v |", b.param)
case ILDataAddVector:
fmt.Fprintf(out, " vec=%v |", b.vec)
vc, oc := b.vectorCost()
fmt.Fprintf(out, " vcost=%d ocost=%d", vc, oc)
case ILDataAddLinVector:
fmt.Fprintf(out, " off=%v |", b.param)
fmt.Fprintf(out, " vec=%v |", b.vec)
vc, oc := b.vectorCost()
fmt.Fprintf(out, " vcost=%d ocost=%d", vc, oc)
}
fmt.Fprintf(out, "\n")
for _, ib := range b.inner {
ib.Dump(out, indent+1)
}
}
// Equal recursively checks if the implicit ILBlock is identical to
// ILBlock a.
func (b *ILBlock) Equal(a *ILBlock) bool {
if (b == nil && a != nil) || (b != nil && a == nil) {
return false
}
if b.typ != a.typ {
return false
}
// requiring param to always be equal (even for ILList) is pretty strict
if b.param != a.param {
return false
}
if len(b.inner) != len(a.inner) {
return false
}
for i := range b.inner {
if !b.inner[i].Equal(a.inner[i]) {
return false
}
}
return true
}
// Compress combines adjacent same type ILBlocks that have repeat parameters
//
// This is one case, where multiple Compress/Prune cycles are necessary.
// This can really only happen after a VectorBalance step.
//
// ILDataAdd -1
// ILDataPtrAdd 0
// ILDataAdd 1
//
// Can this happen more than once?
//
// ILDataAdd -1
// ILDataPtrAdd 0
// ILDataAdd 1
func (b *ILBlock) Compress() int {
var count int64
// base condition
if b.typ != ILList && b.typ != ILLoop {
return int(count)
}
var oldinner []*ILBlock
/* This step expands ILLists elements into the parent ILBlock */
oldinner = b.GetInner()
b.ResetInner(-1)
for _, ib := range oldinner {
if ib.typ == ILList {
b.Append(ib.inner...)
count += int64(len(ib.inner))
} else {
b.Append(ib)
}
}
/* This step combines similar consecutive ILBlock types */
var wg sync.WaitGroup
oldinner = b.GetInner()
b.ResetInner(-1)
var lastb *ILBlock
for _, ib := range oldinner {
switch ib.typ {
case ILList, ILLoop:
b.Append(ib)
wg.Add(1)
go func(wg *sync.WaitGroup, ib *ILBlock) {
c := ib.Compress()
atomic.AddInt64(&count, int64(c))
wg.Done()
}(&wg, ib)
lastb = nil
case ILDataPtrAdd, ILWrite:
/* Combine DataPtrAdds or WriteBs */
if lastb != nil && lastb.typ == ib.typ {
// combine with previous run
lastb.param += ib.param
atomic.AddInt64(&count, 1)
} else {
// start next run
b.Append(ib)
lastb = ib
}
case ILDataAdd:
/* Combine DataAdds, DataPtrAdds, and WriteBs */
if lastb != nil {
switch lastb.typ {
case ILDataAdd, ILDataSet:
// combine with previous DataAdd or DataSet
lastb.param += ib.param
atomic.AddInt64(&count, 1)
default:
b.Append(ib)
lastb = ib
}
} else {
// start next run
b.Append(ib)
lastb = ib
}
case ILDataSet:
/* Overrive a previous ILDataSet or ILDataAdd(interesting eh?) */
if lastb != nil {
switch lastb.typ {
case ILDataSet, ILDataAdd:
// combine with previous run
lastb.typ = ILDataSet // override a previous DataAdd
lastb.param = ib.param
atomic.AddInt64(&count, 1)
default:
b.Append(ib)
lastb = ib
}
} else {
// start next run
b.Append(ib)
lastb = ib
}
case ILDataAddVector:
fallthrough
case ILDataAddLinVector:
fallthrough
default:
b.Append(ib)
lastb = nil
}
}
wg.Wait()
return int(count)
}
// isPruneable uses a set of rules to determine id an ILBlock
// node is able to be removed.
func (b *ILBlock) isPruneable() bool {
if b == nil {
return true
}
switch b.typ {
case ILList:
if len(b.inner) == 0 {
return true
}
case ILDataPtrAdd, ILDataAdd, ILWrite:
if b.param == 0 {
return true
}
}
return false
}
// Prune removes all No-Operations that the Optimize step may have produced.
// For example dataadd(0) or dataptradd(0)
// This must be done depth first, to ensure that parent nodes will be pruned
// after leaf nodes.
// TODO: Parallelize
func (b *ILBlock) Prune() int {
var count int
if len(b.inner) == 0 {
return count
}
oldinner := b.inner
b.inner = make([]*ILBlock, 0)
for _, ib := range oldinner {
count += ib.Prune()
if !ib.isPruneable() {
b.Append(ib)
} else {
count++
}
}
return count
}
type voverlay struct {
ptrOff int
header *ILBlock
vec *ILBlock
footer *ILBlock
}
func (c *voverlay) dataadd(value byte) {
if c.ptrOff < 0 {
// must extend negatively
newLen := (-c.ptrOff) + len(c.vec.vec)
newV := make([]byte, newLen, newLen*2)
copy(newV[(-c.ptrOff):], c.vec.vec)
c.vec.vec = newV
// place back to 0
c.header.param += int64(c.ptrOff)
c.ptrOff = 0
c.footer.param = int64(c.ptrOff)
} else if c.ptrOff >= len(c.vec.vec) {
// must extend positive
if c.ptrOff < cap(c.vec.vec) {
c.vec.vec = c.vec.vec[:c.ptrOff+1]
} else {
newV := make([]byte, c.ptrOff+1, (c.ptrOff+1)*2)
copy(newV, c.vec.vec)
c.vec.vec = newV
}
}
c.vec.vec[c.ptrOff] += value
}
func (c *voverlay) dataptradd(delta int64) {
c.ptrOff += int(delta)
c.footer.param = int64(c.ptrOff)
}
func (b *ILBlock) vectorCost() (vcost, icost int) {
const datapaddCost = 1 + 1 + 1 // add, check <0, check readjust
const dataaddvecStaticCost = 1 + 1 + 1 // check readjust, slice, bound check
if b.typ != ILDataAddVector && b.typ != ILDataAddLinVector {
return -1, -1
}
// Vector Static Cost
vcost += dataaddvecStaticCost
// Vector Dynamic Cost
vcost += len(b.vec)
for _, v := range b.vec {
if v != 0 {
// Independent Dynamic Cost
icost += datapaddCost
}
}
return
}
func (b *ILBlock) Vectorize() int {
// for long blocks that don't print, aggregate their data deltas
// and dataptr moves into the following:
// * dataptr move
// * data vector deltas apply
// * dataptr move
var count int64
// base condition
if b.typ != ILList && b.typ != ILLoop {
return int(count)
}
var wg sync.WaitGroup
var oldinner []*ILBlock
// rip through inner ILBlocks
oldinner = b.GetInner()
b.ResetInner(-1)
var lastVec *voverlay
for _, ib := range oldinner {
switch ib.typ {
case ILList, ILLoop:
if lastVec != nil {
lastVec = nil
}
b.Append(ib)
wg.Add(1)
go func(wg *sync.WaitGroup, ib *ILBlock) {
c := ib.Vectorize()
atomic.AddInt64(&count, int64(c))
wg.Done()
}(&wg, ib)
case ILRead, ILWrite:
if lastVec != nil {
lastVec = nil
}
b.Append(ib)
case ILDataAdd:
if lastVec == nil {
lastVec = &voverlay{
header: &ILBlock{
typ: ILDataPtrAdd,
},
vec: &ILBlock{
typ: ILDataAddVector,
vec: make([]byte, 0),
},
footer: &ILBlock{
typ: ILDataPtrAdd,
},
}
b.Append(lastVec.header)
b.Append(lastVec.vec)
b.Append(lastVec.footer)
atomic.AddInt64(&count, 1)
}
lastVec.dataadd(byte(ib.param))
case ILDataPtrAdd:
if lastVec != nil {
lastVec.dataptradd(ib.param)
} else {
b.Append(ib)
}
case ILDataAddVector:
b.Append(ib)
}
}
wg.Wait()
return int(count)
}
// VectorBalance runs after Vectorizing and determines the runtime
// cost of keeping vectorized adds as compared to having independent operations.
// If the cost is higher to have vectorized operations, they are split up.
func (b *ILBlock) VectorBalance() int {
var count int64
// base condition
if b.typ != ILList && b.typ != ILLoop {
return int(count)
}
var wg sync.WaitGroup
// rip through inner ILBlocks
oldinner := b.inner
b.inner = make([]*ILBlock, 0)
for _, ib := range oldinner {
switch ib.typ {
case ILList, ILLoop:
b.Append(ib)
wg.Add(1)
go func(wg *sync.WaitGroup, ib *ILBlock) {
c := ib.VectorBalance()
atomic.AddInt64(&count, int64(c))
wg.Done()
}(&wg, ib)
case ILDataAddVector:
vcost, ocost := ib.vectorCost()
if vcost > ocost {
// Break It Up
for _, v := range ib.vec {
b.Append(&ILBlock{
typ: ILDataAdd,
param: int64(v),
})
b.Append(&ILBlock{
typ: ILDataPtrAdd,
param: 1,
})
}
// Add corrective dataptr. This should be the exact inverse
// data ptr value of the next ILBlock.
// This will be combined with original footer
// using an additional Optimize step and remove when
// after a Prune.
b.Append(&ILBlock{
typ: ILDataPtrAdd,
param: int64(-len(ib.vec)),
})
atomic.AddInt64(&count, 1)
} else {
b.Append(ib)
}
case ILRead, ILWrite, ILDataAdd, ILDataPtrAdd:
fallthrough
default:
b.Append(ib)
}
}
wg.Wait()
return int(count)
}
type PatternReplacer func(b *ILBlock) []*ILBlock
// Compatible with DataAdd and DataAddVector
func PatternReplaceZero(b *ILBlock) []*ILBlock {
if b.typ == ILLoop {
if len(b.inner) != 1 {
return nil
}
switch loop := b.inner[0]; loop.typ {
case ILDataAdd:
// data add with -1 (0xFF)
if loop.param != -1 {
return nil
}
case ILDataAddVector:
// vector with one element -1 (0xFF)
if len(loop.vec) != 1 || loop.vec[0] != 0xff {
return nil
}
default:
return nil
}
return []*ILBlock{
&ILBlock{
typ: ILDataSet,
param: 0,
},
}
} else if b.typ == ILDataAddLinVector {
if len(b.vec) != 1 || b.vec[0] != 0xFF || b.param != 0 {
return nil
}
return []*ILBlock{
&ILBlock{
typ: ILDataSet,
param: 0,
},
}
}
return nil
}
func PatternReplaceLinearVector(b *ILBlock) []*ILBlock {
if b.typ != ILLoop {
return nil
}
var addvec *ILBlock
var off int64
if len(b.inner) == 1 {
if b.inner[0].typ != ILDataAddVector {
return nil
}
addvec = b.inner[0]
} else if len(b.inner) == 3 {
if b.inner[0].typ != ILDataPtrAdd ||
b.inner[1].typ != ILDataAddVector ||
b.inner[2].typ != ILDataPtrAdd {
return nil
}
addvec = b.inner[1]
off = b.inner[0].param
if !(b.inner[0].param <= 0 && b.inner[2].param >= 0) {
return nil
}
if b.inner[0].param != -b.inner[2].param {
return nil
}
if len(addvec.vec) <= int(b.inner[2].param) || addvec.vec[b.inner[2].param] != 0xFF {
return nil
}
} else {
return nil
}
return []*ILBlock{
&ILBlock{
typ: ILDataAddLinVector,
param: off,
vec: addvec.vec,
},
}
}
func (b *ILBlock) PatternReplace(replacers ...PatternReplacer) int {
var count int64
// Try all the replacer. If one matches and returns
// a set of replacement instructions, wrap them in an ILList
// replace the current ILBlock.
for _, replacer := range replacers {
if rep := replacer(b); rep != nil {
atomic.AddInt64(&count, 1)
// wrap it in an ILList
b.typ = ILList
b.inner = rep
break
}
}
// rip through inner ILBlocks
var wg sync.WaitGroup
for _, ib := range b.GetInner() {
// Recursively search for sub-matches.
// We allow an already matched and replaced ILBlock to be searched
// again.
wg.Add(1)
go func(wg *sync.WaitGroup, ib *ILBlock) {
c := ib.PatternReplace(replacers...)
atomic.AddInt64(&count, int64(c))
wg.Done()
}(&wg, ib)
}
wg.Wait()
return int(count)
}
// BlockCount counts the total number of ILBlocks in the tree b
func (b *ILBlock) BlockCount() int {
var count int64 = 1
var wg sync.WaitGroup
for _, ib := range b.GetInner() {
switch ib.typ {
case ILList, ILLoop:
wg.Add(1)
go func(wg *sync.WaitGroup, ib *ILBlock) {
c := int64(ib.BlockCount())
atomic.AddInt64(&count, int64(c))
wg.Done()
}(&wg, ib)
default:
atomic.AddInt64(&count, 1)
}
}
wg.Wait()
return int(count)
}
// You can't really predict the max data depth, since
// you can use a loop to skip forward one at a time.
// For example, this program sets the first two cells
// to 1, rewinds the ptr back, has a loop find the last cell,
// and then moves two places past.
// +>+><<[>]>>
func (b *ILBlock) PredictMaxDataSize() int {
var deltaMax int64
var delta int64
// base condition
if b.typ == ILDataPtrAdd {
if b.param > 0 {
deltaMax = b.param
}
return int(deltaMax)
}
if b.typ != ILList && b.typ != ILLoop {
return int(deltaMax)
}
// var wg sync.WaitGroup
for _, ib := range b.inner {
switch ib.typ {
case ILDataPtrAdd:
delta += ib.param
if delta > deltaMax {
deltaMax = delta
}
case ILList, ILLoop:
// wg.Add(1)
// go func(wg *sync.WaitGroup, ib *ILBlock) {
dMax := int64(ib.PredictMaxDataSize()) + delta
if dMax > deltaMax {
deltaMax = dMax
}
// atomic.AddInt64(&delta, int64(c))
// wg.Done()
// }(&wg, ib)
case ILRead, ILWrite, ILDataAdd:
case ILDataAddVector:
}
}
// wg.Wait()
return int(deltaMax)
}