Commit 8a6f1b1746 for golang
commit 8a6f1b1746b8b2e540a3f9ee1141b1fd7b6e5a80
Author: Mark Freeman <mark@golang.org>
Date: Wed Nov 25 12:26:21 2026 -0500
cmd/compile/internal/compare: implement the driver
The driver manages a set of workers. Each worker is assigned a shard of
the programs to generate. Every shard runs one OS process at a time and
is always in 1 of 4 states:
- done : the shard finished processing
- running: the shard is proceeding normally
- crashed: the last program generated by the shard crashed the process
- hanging: the last program generated by the shard hung the process
If a shard crashes or hangs, a new shard is created with the remaining
work from the old shard and assigned to the same worker. The driver
merges completed work from the workers until all trials are done.
Change-Id: Id71099fddf761af9fe501f8f272b2f69351f706e
Reviewed-on: https://go-review.googlesource.com/c/go/+/867663
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Mark Freeman <mark@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
diff --git a/src/cmd/compile/internal/contraire/driver.go b/src/cmd/compile/internal/contraire/driver.go
new file mode 100644
index 0000000000..24fa96d56e
--- /dev/null
+++ b/src/cmd/compile/internal/contraire/driver.go
@@ -0,0 +1,263 @@
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build darwin
+
+package contraire
+
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strconv"
+ "syscall"
+ "time"
+)
+
+// runDriver manages workers that generate and type-check programs. It returns a
+// summary of the workers' outputs (or an error).
+func runDriver(workDir string, trials, workers int, cfg *config, std bool) (*output, error) {
+ if trials < 1 {
+ return nil, errors.New("trials must be positive")
+ }
+ if workers < 1 {
+ return nil, errors.New("workers must be positive")
+ }
+ workers = min(workers, trials)
+
+ // The worker uses the same binary as the driver.
+ exe, err := os.Executable()
+ if err != nil {
+ return nil, fmt.Errorf("couldn't find executable: %v", err)
+ }
+
+ // Recreate the driver directory in case a prior run didn't exit cleanly.
+ driverDir := filepath.Join(workDir, "contraire")
+ if err := recreateDir(driverDir); err != nil {
+ return nil, err
+ }
+
+ // Forward config to workers if provided.
+ var cfgPath string
+ if cfg != nil {
+ data, err := json.Marshal(cfg)
+ if err != nil {
+ return nil, fmt.Errorf("couldn't encode config: %v", err)
+ }
+ cfgPath = filepath.Join(driverDir, "cfg.json")
+ if err := os.WriteFile(cfgPath, data, 0644); err != nil {
+ return nil, fmt.Errorf("couldn't write config to %q: %v", cfgPath, err)
+ }
+ }
+
+ // Create workers and assign initial shards to them.
+ active := make(map[int]*worker, workers)
+ for i := range workers {
+ active[i] = newWorker(i, driverDir, exe)
+ }
+ shardSize := trials / workers
+ remainder := trials % workers
+ for i, w := range active {
+ trials := shardSize
+ if i < remainder {
+ trials++
+ }
+ if err := w.newShard(trials, cfgPath, std); err != nil {
+ return nil, err
+ }
+ }
+
+ // Monitor workers and reassign shards as-needed.
+ ticker := time.NewTicker(100 * time.Millisecond)
+ defer ticker.Stop()
+
+ acc := &output{}
+ for len(active) > 0 {
+ <-ticker.C
+ for i, w := range active {
+ out, err := w.poll()
+ if err != nil {
+ return nil, err
+ }
+ if out != nil {
+ acc.merge(out)
+ trialsLeft := w.shard.size - out.TrialsDone
+ if trialsLeft == 0 {
+ delete(active, i)
+ } else {
+ if err := w.newShard(trialsLeft, cfgPath, std); err != nil {
+ return nil, err
+ }
+ }
+ }
+ }
+ }
+ return acc, nil
+}
+
+type worker struct {
+ id int // set in newWorker
+ driverDir string // set in newWorker
+ workerExe string // set in newWorker
+ outPath string // set in initDir
+ lockPath string // set in initDir
+ shard *shard // set in newShard
+}
+
+func newWorker(id int, driverDir, workerExe string) *worker {
+ return &worker{id: id, driverDir: driverDir, workerExe: workerExe}
+}
+
+func (w *worker) initDir() error {
+ workerDir := filepath.Join(w.driverDir, fmt.Sprintf("w-%d", w.id))
+ if err := recreateDir(workerDir); err != nil {
+ return err
+ }
+ w.outPath = filepath.Join(workerDir, "out.json")
+ data, err := json.Marshal(&output{})
+ if err != nil {
+ return fmt.Errorf("couldn't encode output: %v", err)
+ }
+ if err := os.WriteFile(w.outPath, data, 0644); err != nil {
+ return fmt.Errorf("couldn't write output to %q: %v", w.outPath, err)
+ }
+ w.lockPath = filepath.Join(workerDir, "out.lock")
+ if err := os.WriteFile(w.lockPath, nil, 0644); err != nil {
+ return fmt.Errorf("couldn't create lock file %q: %v", w.lockPath, err)
+ }
+ return nil
+}
+
+type shard struct {
+ *worker
+ size int // immutable
+ proc *os.Process // immutable
+ done <-chan error // immutable
+ trialsDone int // mutable
+}
+
+func (w *worker) newShard(trials int, cfgPath string, std bool) error {
+ if err := w.initDir(); err != nil {
+ return err
+ }
+ args := []string{
+ "-worker",
+ "-trials", strconv.Itoa(trials),
+ "-o", w.outPath,
+ "-l", w.lockPath,
+ }
+ if cfgPath != "" {
+ args = append(args, "-cfg", cfgPath)
+ }
+ if std {
+ args = append(args, "-std")
+ }
+ cmd := exec.Command(w.workerExe, args...)
+ if err := cmd.Start(); err != nil {
+ return fmt.Errorf("couldn't start process: %v", err)
+ }
+ done := make(chan error, 1)
+ go func() {
+ done <- cmd.Wait()
+ }()
+ w.shard = &shard{
+ worker: w,
+ size: trials,
+ proc: cmd.Process,
+ done: done,
+ trialsDone: -1, // Ensure the first trial gets the full deadline.
+ }
+ return nil
+}
+
+type shardState int
+
+const (
+ shardRunning shardState = iota
+ shardDone
+ shardCrashed
+ shardHanging
+)
+
+func (w *worker) poll() (*output, error) {
+ state, out, err := w.shard.poll()
+ if err != nil {
+ return nil, err
+ }
+ switch state {
+ case shardRunning:
+ return nil, nil
+ case shardDone:
+ // nothing to do
+ case shardCrashed:
+ out.TrialsDone++
+ out.Crash = out.Next
+ out.Next = nil
+ case shardHanging:
+ out.TrialsDone++
+ out.Hang = out.Next
+ out.Next = nil
+ }
+ return out, nil
+}
+
+func (s *shard) poll() (shardState, *output, error) {
+ f, err := os.Open(s.lockPath)
+ if err != nil {
+ return 0, nil, fmt.Errorf("couldn't open lock file %q: %v", s.lockPath, err)
+ }
+ defer f.Close()
+ if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
+ return 0, nil, fmt.Errorf("couldn't acquire lock file %q: %v", s.lockPath, err)
+ }
+ defer syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
+ // Read the worker's output and check for completion.
+ data, err := os.ReadFile(s.outPath)
+ if err != nil {
+ return 0, nil, fmt.Errorf("couldn't read output %q: %v", s.outPath, err)
+ }
+ out := &output{}
+ if err := json.Unmarshal(data, out); err != nil {
+ return 0, nil, fmt.Errorf("couldn't decode output %q: %v", s.outPath, err)
+ }
+ if out.TrialsDone == s.size {
+ return shardDone, out, nil
+ }
+ // If we're still in the startup phase, make sure it's alive.
+ if out.Next == nil {
+ select {
+ case err := <-s.done:
+ return 0, nil, fmt.Errorf("worker crashed during startup: %v", err)
+ default:
+ return shardRunning, nil, nil
+ }
+ }
+ // If we're in the steady state phase, make sure it's made progress.
+ if out.TrialsDone > s.trialsDone {
+ s.trialsDone = out.TrialsDone
+ return shardRunning, nil, nil
+ }
+ // If we didn't make progress, it's either crashed or hanging.
+ select {
+ case <-s.done:
+ return shardCrashed, out, nil
+ default:
+ s.proc.Signal(syscall.SIGQUIT)
+ <-s.done
+ return shardHanging, out, nil
+ }
+}
+
+func recreateDir(dir string) error {
+ if err := os.RemoveAll(dir); err != nil {
+ return fmt.Errorf("couldn't delete directory %q: %v", dir, err)
+ }
+ if err := os.Mkdir(dir, 0755); err != nil {
+ return fmt.Errorf("couldn't create directory %q: %v", dir, err)
+ }
+ return nil
+}