Skip to content

Commit 7f1ff65

Browse files
committed
cmd/compile: insert scheduling checks on loop backedges
Loop breaking with a counter. Benchmarked (see comments), eyeball checked for sanity on popular loops. This code ought to handle loops in general, and properly inserts phi functions in cases where the earlier version might not have. Includes test, plus modifications to test/run.go to deal with timeout and killing looping test. Tests broken by the addition of extra code (branch frequency and live vars) for added checks turn the check insertion off. If GOEXPERIMENT=preemptibleloops, the compiler inserts reschedule checks on every backedge of every reducible loop. Alternately, specifying GO_GCFLAGS=-d=ssa/insert_resched_checks/on will enable it for a single compilation, but because the core Go libraries contain some loops that may run long, this is less likely to have the desired effect. This is intended as a tool to help in the study and diagnosis of GC and other latency problems, now that goal STW GC latency is on the order of 100 microseconds or less. Updates #17831. Updates #10958. Change-Id: I6206c163a5b0248e3f21eb4fc65f73a179e1f639 Reviewed-on: https://go-review.googlesource.com/33910 Run-TryBot: David Chase <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent f412bd3 commit 7f1ff65

File tree

13 files changed

+730
-8
lines changed

13 files changed

+730
-8
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var runtimeDecls = [...]struct {
1515
{"panicwrap", funcTag, 7},
1616
{"gopanic", funcTag, 9},
1717
{"gorecover", funcTag, 12},
18+
{"goschedguarded", funcTag, 5},
1819
{"printbool", funcTag, 14},
1920
{"printfloat", funcTag, 16},
2021
{"printint", funcTag, 18},

src/cmd/compile/internal/gc/builtin/runtime.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func panicwrap(string, string, string)
2121

2222
func gopanic(interface{})
2323
func gorecover(*int32) interface{}
24+
func goschedguarded()
2425

2526
func printbool(bool)
2627
func printfloat(float64)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ func buildssa(fn *Node) *ssa.Func {
6464
s.config = initssa()
6565
s.f = s.config.NewFunc()
6666
s.f.Name = name
67+
if fn.Func.Pragma&Nosplit != 0 {
68+
s.f.NoSplit = true
69+
}
6770
s.exitCode = fn.Func.Exit
6871
s.panics = map[funcLine]*ssa.Block{}
6972
s.config.DebugTest = s.config.DebugHashMatch("GOSSAHASH", name)

src/cmd/compile/internal/ssa/compile.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package ssa
66

77
import (
8+
"cmd/internal/obj"
89
"fmt"
910
"log"
1011
"os"
@@ -349,6 +350,8 @@ var passes = [...]pass{
349350
{name: "writebarrier", fn: writebarrier, required: true}, // expand write barrier ops
350351
{name: "fuse", fn: fuse},
351352
{name: "dse", fn: dse},
353+
{name: "insert resched checks", fn: insertLoopReschedChecks,
354+
disabled: obj.Preemptibleloops_enabled == 0}, // insert resched checks in loops.
352355
{name: "tighten", fn: tighten}, // move values closer to their uses
353356
{name: "lower", fn: lower, required: true},
354357
{name: "lowered cse", fn: cse},
@@ -378,7 +381,13 @@ type constraint struct {
378381
}
379382

380383
var passOrder = [...]constraint{
381-
// prove reliese on common-subexpression elimination for maximum benefits.
384+
// "insert resched checks" uses mem, better to clean out stores first.
385+
{"dse", "insert resched checks"},
386+
// insert resched checks adds new blocks containing generic instructions
387+
{"insert resched checks", "lower"},
388+
{"insert resched checks", "tighten"},
389+
390+
// prove relies on common-subexpression elimination for maximum benefits.
382391
{"generic cse", "prove"},
383392
// deadcode after prove to eliminate all new dead blocks.
384393
{"prove", "generic deadcode"},

src/cmd/compile/internal/ssa/func.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Func struct {
2424
vid idAlloc // value ID allocator
2525

2626
scheduled bool // Values in Blocks are in final order
27+
NoSplit bool // true if function is marked as nosplit. Used by schedule check pass.
2728

2829
// when register allocation is done, maps value ids to locations
2930
RegAlloc []Location

0 commit comments

Comments
 (0)