diff --git a/compiler/gengo/gengo.go b/compiler/gengo/gengo.go index 99b6f91..93fb74b 100644 --- a/compiler/gengo/gengo.go +++ b/compiler/gengo/gengo.go @@ -939,7 +939,9 @@ func (g *Generator) emitFuncDecl(fn *ast.FuncDecl) { // so reads can be constant-propagated at emit time. g.constLocals = collectConstLocals(fn) - // Emit LOCAL initializers + // Emit LOCAL initializers. LOCALs that were const-propagated have + // their reads substituted inline, so the init store has no live + // reader — skip it (dead-store elimination). localIdx := nParams + 1 // 1-based, params come first for _, d := range fn.Decls { vd, ok := d.(*ast.VarDecl) @@ -948,8 +950,10 @@ func (g *Generator) emitFuncDecl(fn *ast.FuncDecl) { } for _, v := range vd.Vars { if v.Init != nil { - g.emitExpr(v.Init) - g.writeln(fmt.Sprintf("t.PopLocalFast(%d)", localIdx)) + if _, isConst := g.constLocals[strings.ToUpper(v.Name)]; !isConst { + g.emitExpr(v.Init) + g.writeln(fmt.Sprintf("t.PopLocalFast(%d)", localIdx)) + } } localIdx++ } @@ -1330,8 +1334,10 @@ func (g *Generator) emitMidVarDecl(s *ast.VarDecl, locals localMap) { locals[strings.ToUpper(v.Name)] = idx } if v.Init != nil { - g.emitExpr(v.Init) - g.writeln(fmt.Sprintf("t.PopLocalFast(%d)", idx)) + if _, isConst := g.constLocals[strings.ToUpper(v.Name)]; !isConst { + g.emitExpr(v.Init) + g.writeln(fmt.Sprintf("t.PopLocalFast(%d)", idx)) + } } } }