From 68e6fa4f6852b4ef0fe61789618c093f4e2185c9 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Thu, 31 Dec 2020 23:45:36 -0800 Subject: [dev.regabi] cmd/compile: fix package-initialization order This CL fixes package initialization order by creating the init task before the general deadcode-removal pass. It also changes noder to emit zero-initialization assignments (i.e., OAS with nil RHS) for package-block variables, so that initOrder can tell the variables still need initialization. To allow this, we need to also extend the static-init code to recognize zero-initialization assignments. This doesn't pass toolstash -cmp, because it reorders some package initialization routines. Fixes #43444. Change-Id: I0da7996a62c85e15e97ce965298127e075390a7e Reviewed-on: https://go-review.googlesource.com/c/go/+/280976 Trust: Matthew Dempsky Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/staticinit/sched.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/cmd/compile/internal/staticinit') diff --git a/src/cmd/compile/internal/staticinit/sched.go b/src/cmd/compile/internal/staticinit/sched.go index 1b0af1b05d..8e4ce55954 100644 --- a/src/cmd/compile/internal/staticinit/sched.go +++ b/src/cmd/compile/internal/staticinit/sched.go @@ -86,17 +86,22 @@ func (s *Schedule) staticcopy(l *ir.Name, loff int64, rn *ir.Name, typ *types.Ty if rn.Class_ != ir.PEXTERN || rn.Sym().Pkg != types.LocalPkg { return false } - if rn.Defn == nil { // probably zeroed but perhaps supplied externally and of unknown value - return false - } if rn.Defn.Op() != ir.OAS { return false } if rn.Type().IsString() { // perhaps overwritten by cmd/link -X (#34675) return false } + if rn.Embed != nil { + return false + } orig := rn r := rn.Defn.(*ir.AssignStmt).Y + if r == nil { + // No explicit initialization value. Probably zeroed but perhaps + // supplied externally and of unknown value. + return false + } for r.Op() == ir.OCONVNOP && !types.Identical(r.Type(), typ) { r = r.(*ir.ConvExpr).X @@ -185,6 +190,11 @@ func (s *Schedule) staticcopy(l *ir.Name, loff int64, rn *ir.Name, typ *types.Ty } func (s *Schedule) StaticAssign(l *ir.Name, loff int64, r ir.Node, typ *types.Type) bool { + if r == nil { + // No explicit initialization value. Either zero or supplied + // externally. + return true + } for r.Op() == ir.OCONVNOP { r = r.(*ir.ConvExpr).X } -- cgit v1.3