Skip to content

Commit f4936d0

Browse files
committed
cmd/compile: call fninit earlier
This allows the global initializers function to go through normal mid-end optimizations (e.g., inlining, escape analysis) like any other function. Updates #33485. Change-Id: I9bcfe98b8628d1aca09b4c238d8d3b74c69010a5 Reviewed-on: https://go-review.googlesource.com/c/go/+/254839 Reviewed-by: Keith Randall <[email protected]> Trust: Matthew Dempsky <[email protected]>
1 parent 2374105 commit f4936d0

File tree

3 files changed

+6
-10
lines changed

3 files changed

+6
-10
lines changed

src/cmd/compile/internal/gc/init.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func fninit(n []*Node) {
5959
Curfn = fn
6060
typecheckslice(nf, ctxStmt)
6161
Curfn = nil
62-
funccompile(fn)
62+
xtop = append(xtop, fn)
6363
fns = append(fns, initializers.Linksym())
6464
}
6565
if dummyInitFn.Func.Dcl != nil {
@@ -68,16 +68,14 @@ func fninit(n []*Node) {
6868
// something's weird if we get here.
6969
Fatalf("dummyInitFn still has declarations")
7070
}
71+
dummyInitFn = nil
7172

7273
// Record user init functions.
7374
for i := 0; i < renameinitgen; i++ {
7475
s := lookupN("init.", i)
7576
fn := asNode(s.Def).Name.Defn
7677
// Skip init functions with empty bodies.
77-
// noder.go doesn't allow external init functions, and
78-
// order.go has already removed any OEMPTY nodes, so
79-
// checking Len() == 0 is sufficient here.
80-
if fn.Nbody.Len() == 0 {
78+
if fn.Nbody.Len() == 1 && fn.Nbody.First().Op == OEMPTY {
8179
continue
8280
}
8381
fns = append(fns, s.Linksym())

src/cmd/compile/internal/gc/main.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ func Main(archInit func(*Arch)) {
642642
errorexit()
643643
}
644644

645+
fninit(xtop)
646+
645647
// Phase 4: Decide how to capture closed variables.
646648
// This needs to run before escape analysis,
647649
// because variables captured by value do not escape.
@@ -751,10 +753,6 @@ func Main(archInit func(*Arch)) {
751753
}
752754
timings.AddEvent(fcount, "funcs")
753755

754-
if nsavederrors+nerrors == 0 {
755-
fninit(xtop)
756-
}
757-
758756
compileFunctions()
759757

760758
if nowritebarrierrecCheck != nil {

test/inline.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func j(x int) int { // ERROR "can inline j"
5050
}
5151
}
5252

53-
var somethingWrong error = errors.New("something went wrong")
53+
var somethingWrong error = errors.New("something went wrong") // ERROR "can inline init" "inlining call to errors.New" "errors.errorString.* escapes to heap"
5454

5555
// local closures can be inlined
5656
func l(x, y int) (int, int, error) {

0 commit comments

Comments
 (0)