Skip to content

Commit 802c702

Browse files
committed
wire: 优化赋值操作,去除 Retain 指令
1 parent d0919ff commit 802c702

4 files changed

Lines changed: 99 additions & 72 deletions

File tree

internal/wire/function.go

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,8 @@ func (f *Function) EndBody() {
166166

167167
param.name = "$" + param.name
168168
fb.emit(param)
169-
if hasChunk {
170-
param.init = NewRetain(NewGet(&np, np.pos), np.pos)
171-
} else {
172-
param.init = NewGet(&np, np.pos)
173-
}
169+
170+
param.init = NewGet(&np, np.pos)
174171

175172
f.params[i] = &np
176173
}
@@ -309,9 +306,6 @@ func rc_stmt(s Stmt, inloop bool, d *Block) {
309306
case *Alloc:
310307
s.init = rc_expr(s.init, inloop, false, false, d, &pre)
311308
d.Stmts = append(d.Stmts, pre...)
312-
if s.init != nil && rtimp.hasChunk(s.init.Type()) && !s.init.retained() {
313-
s.init = NewRetain(s.init, s.init.Pos())
314-
}
315309
d.emit(s)
316310

317311
case *Get:
@@ -322,33 +316,42 @@ func rc_stmt(s Stmt, inloop bool, d *Block) {
322316
s.Lhs[i] = rc_expr(s.Lhs[i], inloop, true, false, d, &pre)
323317
}
324318

325-
for i := range s.Rhs {
326-
s.Rhs[i] = rc_expr(s.Rhs[i], inloop, false, false, d, &pre)
327-
if rtimp.hasChunk(s.Rhs[i].Type()) && !s.Rhs[i].retained() {
328-
s.Rhs[i] = NewRetain(s.Rhs[i], s.Rhs[i].Pos())
319+
rhNeedRetain := make([]bool, len(s.Lhs))
320+
if len(s.Lhs) == len(s.Rhs) {
321+
for i := range s.Rhs {
322+
s.Rhs[i] = rc_expr(s.Rhs[i], inloop, false, false, d, &pre)
323+
if rtimp.hasChunk(s.Rhs[i].Type()) && !s.Rhs[i].retained() {
324+
rhNeedRetain[i] = true
325+
}
326+
}
327+
} else {
328+
// rh 是元组
329+
notRetained := rtimp.hasChunk(s.Rhs[0].Type()) && !s.Rhs[0].retained()
330+
for i := range rhNeedRetain {
331+
rhNeedRetain[i] = notRetained
329332
}
330333
}
331334

332335
d.Stmts = append(d.Stmts, pre...)
333336

334-
allAssignable := true
335-
assignable := make([]bool, len(s.Lhs))
337+
allLhsAssignable := true
338+
lhAssignable := make([]bool, len(s.Lhs))
336339
lhs := make([]Var, len(s.Lhs))
337340
for i, lh := range s.Lhs {
338341
if lh == nil {
339342
lhs[i] = nil
340-
assignable[i] = true
343+
lhAssignable[i] = true
341344
continue
342345
}
343346

344347
if v, ok := lh.(Var); ok && v.Kind() == Register {
345348
lhs[i] = v
346-
assignable[i] = true
349+
lhAssignable[i] = true
347350
continue
348351
}
349352

350-
allAssignable = false
351-
assignable[i] = false
353+
allLhsAssignable = false
354+
lhAssignable[i] = false
352355
if get, ok := lh.(*Get); ok {
353356
if v, ok := get.Loc.(Var); ok && v.Kind() == Register {
354357
lhs[i] = v
@@ -361,8 +364,10 @@ func rc_stmt(s Stmt, inloop bool, d *Block) {
361364
lhs[i] = loc
362365
}
363366

364-
if allAssignable {
365-
d.emit(NewAssignN(lhs, s.Rhs, s.pos))
367+
if allLhsAssignable {
368+
assign := NewAssignN(lhs, s.Rhs, s.pos)
369+
copy(assign.needRetain, rhNeedRetain)
370+
d.emit(assign)
366371
} else {
367372
rhs := make([]Expr, len(s.Lhs))
368373

@@ -386,31 +391,41 @@ func rc_stmt(s Stmt, inloop bool, d *Block) {
386391
loc := lhs[i]
387392
rh := rhs[i]
388393

389-
if assignable[i] {
390-
d.emit(NewAssign(loc, rh, s.pos))
394+
if lhAssignable[i] {
395+
assign := NewAssign(loc, rh, s.pos)
396+
assign.needRetain[0] = rhNeedRetain[i]
397+
d.emit(assign)
391398
continue
392399
}
393400

394401
if v, ok := rh.(Var); ok {
395-
d.emit(NewStore(loc, v, s.pos))
402+
store := NewStore(loc, v, s.pos)
403+
store.needRetain = rhNeedRetain[i]
404+
d.emit(store)
396405
continue
397406
}
398407

399408
if get, ok := rh.(*Get); ok {
400409
if v, ok := get.Loc.(Var); ok && v.Kind() == Register {
401-
d.emit(NewStore(loc, v, s.pos))
410+
store := NewStore(loc, v, s.pos)
411+
store.needRetain = rhNeedRetain[i]
412+
d.emit(store)
402413
continue
403414
}
404415
}
405416

406417
if c, ok := rh.(*Const); ok {
407-
d.emit(NewStore(loc, c, s.pos))
418+
store := NewStore(loc, c, s.pos)
419+
store.needRetain = false
420+
d.emit(store)
408421
continue
409422
}
410423

411424
imv := NewImv(d.newTempVarName(), rh, s.pos)
412425
d.emit(imv)
413-
d.emit(NewStore(loc, imv, s.pos))
426+
store := NewStore(loc, imv, s.pos)
427+
store.needRetain = rhNeedRetain[i]
428+
d.emit(store)
414429
}
415430
}
416431

@@ -436,8 +451,8 @@ func rc_stmt(s Stmt, inloop bool, d *Block) {
436451
rc_block(s.Body, true, l.Body)
437452
rc_block(s.Post, true, l.Post)
438453

439-
case *Retain:
440-
d.emit(s)
454+
//case *Retain:
455+
// d.emit(s)
441456

442457
case *Drop:
443458
d.emit(s)
@@ -556,7 +571,7 @@ func (f *Function) autoDrop(b *Block) {
556571
f.autoDrop(s.Body)
557572
f.autoDrop(s.Post)
558573

559-
case *Get, *Set, *Assign, *Store, *Br, *Return, *Unop, *Biop, *Retain, *Drop:
574+
case *Get, *Set, *Assign, *Store, *Br, *Return, *Unop, *Biop, *Drop:
560575
continue
561576

562577
default:
@@ -621,8 +636,8 @@ func (f *Function) allocVR_stmt(s Stmt, inloop bool) {
621636
case *Drop:
622637
f.dropVR_tank(s.X.Tank())
623638

624-
case *Retain:
625-
panic("Retain should not be here")
639+
//case *Retain:
640+
// panic("Retain should not be here")
626641

627642
case *Br, *Return:
628643

@@ -691,8 +706,8 @@ func (f *Function) allocVR_expr(e Expr, inloop bool) {
691706
// f.allocVR_expr(e.X, inloop)
692707
// f.allocVR_var(e.Imv, inloop)
693708

694-
case *Retain:
695-
f.allocVR_expr(e.X, inloop)
709+
//case *Retain:
710+
// f.allocVR_expr(e.X, inloop)
696711

697712
default:
698713
panic(fmt.Sprintf("Todo: %s", e.(Stmt).String()))
@@ -865,8 +880,8 @@ func getReplace_expr(e Expr) (ret Expr) {
865880
e.X = getReplace_expr(e.X)
866881
e.Y = getReplace_expr(e.Y)
867882

868-
case *Retain:
869-
e.X = getReplace_expr(e.X)
883+
//case *Retain:
884+
// e.X = getReplace_expr(e.X)
870885

871886
case *Call:
872887
var call_common *CallCommon

internal/wire/ins_expr_unop_biop_retain.go renamed to internal/wire/ins_expr_unop_biop.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -167,30 +167,30 @@ Combo: 组合指令,将多个指令组合成一个指令,实现了 Expr 接
167167
// return v
168168
//}
169169

170-
/**************************************
171-
Retain: Retain 指令,引用计数 +1,Retain 指令实现了 Expr,返回 X 本身
172-
**************************************/
173-
174-
type Retain struct {
175-
aStmt
176-
X Expr
177-
}
178-
179-
func (i *Retain) Name() string { return i.String() }
180-
func (i *Retain) Type() Type { return i.X.Type() }
181-
func (i *Retain) retained() bool { panic("") }
182-
func (i *Retain) String() string { return fmt.Sprintf("retain(%s)", i.X.Name()) }
183-
184-
// 生成一条 Retain 指令
185-
func NewRetain(x Expr, pos int) *Retain {
186-
if x == nil {
187-
panic("x is nil")
188-
}
189-
v := &Retain{X: x}
190-
v.Stringer = v
191-
v.pos = pos
192-
return v
193-
}
170+
///**************************************
171+
//Retain: Retain 指令,引用计数 +1,Retain 指令实现了 Expr,返回 X 本身
172+
//**************************************/
173+
//
174+
//type Retain struct {
175+
// aStmt
176+
// X Expr
177+
//}
178+
//
179+
//func (i *Retain) Name() string { return i.String() }
180+
//func (i *Retain) Type() Type { return i.X.Type() }
181+
//func (i *Retain) retained() bool { panic("") }
182+
//func (i *Retain) String() string { return fmt.Sprintf("retain(%s)", i.X.Name()) }
183+
//
184+
//// 生成一条 Retain 指令
185+
//func NewRetain(x Expr, pos int) *Retain {
186+
// if x == nil {
187+
// panic("x is nil")
188+
// }
189+
// v := &Retain{X: x}
190+
// v.Stringer = v
191+
// v.pos = pos
192+
// return v
193+
//}
194194

195195
/**************************************
196196
DupRef: DupRef 指令,引用复制,DupRef 指令实现了 Expr,返回 X

internal/wire/ins_stmt_block.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ func exprContainsVar(expr Expr, v Var) bool {
308308
case *Biop:
309309
return exprContainsVar(e.X, v) || exprContainsVar(e.Y, v)
310310

311-
case *Retain:
312-
return exprContainsVar(e.X, v)
311+
//case *Retain:
312+
// return exprContainsVar(e.X, v)
313313

314314
//case *Combo:
315315
// for _, stmt := range e.Stmts {

internal/wire/ins_stmt_set_assign_store.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,35 @@ Assign: Assign 指令,将 Rhs 赋值给 Lhs
8989
- Assgin 支持多赋值
9090
- Lh 必须为 Register 型变量
9191
- Rh 可能为元组(Tuple),此时 Rhs 的长度应为 1,Lhs 的长度应为元组长度
92-
- 向 nil 的 Lh 赋值是合法的,这等价于向匿名变量 _ 赋值,此时若 Rh 已 retain,应触发 release
92+
- 向 nil 的 Lh 赋值是合法的,这等价于向匿名变量 _ 赋值
9393
**************************************/
9494

9595
type Assign struct {
9696
aStmt
97-
Lhs []Var
98-
Rhs []Expr
97+
Lhs []Var
98+
needRetain []bool
99+
Rhs []Expr
99100
}
100101

101102
func (i *Assign) String() string {
102103
var sb strings.Builder
103104

104-
for i, lh := range i.Lhs {
105-
if i > 0 {
105+
for j, lh := range i.Lhs {
106+
if j > 0 {
106107
sb.WriteString(", ")
107108
}
108109

109110
if lh == nil {
110111
sb.WriteRune('_')
111-
continue
112+
if !i.needRetain[j] {
113+
sb.WriteString("↓")
114+
}
115+
} else {
116+
sb.WriteString(lh.Name())
117+
if i.needRetain[j] && rtimp.hasChunk(lh.Type()) {
118+
sb.WriteString("↑")
119+
}
112120
}
113-
114-
sb.WriteString(lh.Name())
115121
}
116122

117123
sb.WriteString(" = ")
@@ -134,6 +140,7 @@ func NewAssignN(lhs []Var, rhs []Expr, pos int) *Assign {
134140
v.Stringer = v
135141
v.Lhs = lhs
136142
v.Rhs = rhs
143+
v.needRetain = make([]bool, len(lhs))
137144
v.pos = pos
138145
return v
139146
}
@@ -160,12 +167,17 @@ Store: Store 指令,将 Val 存储到 Loc 指定的位置
160167

161168
type Store struct {
162169
aStmt
163-
Loc Var
164-
Val Expr
170+
Loc Var
171+
needRetain bool
172+
Val Expr
165173
}
166174

167175
func (i *Store) String() string {
168-
return fmt.Sprintf("store(%s, %s)", i.Loc.Name(), i.Val.Name())
176+
if i.needRetain && rtimp.hasChunk(i.Val.Type()) {
177+
return fmt.Sprintf("store(%s↑, %s)", i.Loc.Name(), i.Val.Name())
178+
} else {
179+
return fmt.Sprintf("store(%s, %s)", i.Loc.Name(), i.Val.Name())
180+
}
169181
}
170182

171183
func NewStore(loc Var, val Expr, pos int) *Store {

0 commit comments

Comments
 (0)