Skip to content

Commit 16f7431

Browse files
committed
cmd/compile: randomize compilation order when race-enabled
There's been one failure on the race builder so far, before we started sorting functions by length. The race detector can only detect actual races, and ordering functions by length might reduce the odds of catching some kinds of races. Give it more to chew on. Updates golang#20144 Change-Id: I0206ac182cb98b70a729dea9703ecb0fef54d2d0
1 parent 220e0e0 commit 16f7431

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !race
6+
7+
package gc
8+
9+
const raceEnabled = false

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"cmd/internal/src"
1414
"cmd/internal/sys"
1515
"fmt"
16+
"math/rand"
1617
"sort"
1718
"sync"
1819
)
@@ -253,12 +254,22 @@ func compileSSA(fn *Node, worker int) {
253254
// and waits for them to complete.
254255
func compileFunctions() {
255256
if len(compilequeue) != 0 {
256-
// Compile the longest functions first,
257-
// since they're most likely to be the slowest.
258-
// This helps avoid stragglers.
259-
obj.SortSlice(compilequeue, func(i, j int) bool {
260-
return compilequeue[i].Nbody.Len() > compilequeue[j].Nbody.Len()
261-
})
257+
if raceEnabled {
258+
// Randomize compilation order to try to shake out races.
259+
tmp := make([]*Node, len(compilequeue))
260+
perm := rand.Perm(len(compilequeue))
261+
for i, v := range perm {
262+
tmp[v] = compilequeue[i]
263+
}
264+
copy(compilequeue, tmp)
265+
} else {
266+
// Compile the longest functions first,
267+
// since they're most likely to be the slowest.
268+
// This helps avoid stragglers.
269+
obj.SortSlice(compilequeue, func(i, j int) bool {
270+
return compilequeue[i].Nbody.Len() > compilequeue[j].Nbody.Len()
271+
})
272+
}
262273
var wg sync.WaitGroup
263274
c := make(chan *Node)
264275
for i := 0; i < nBackendWorkers; i++ {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build race
6+
7+
package gc
8+
9+
const raceEnabled = true

0 commit comments

Comments
 (0)