Skip to content

Commit 08992fd

Browse files
committed
fix test/nilptr3.go
func f4(x *[10]int) { ... _ = x[9] // ERROR "generated nil check" for { if x[9] != 0 { // ERROR "removed nil check" break } } } Loop rotation duplicates conditional test Values into loop gaurd block: ... loopGuard: v6 (+159) = NilCheck <*[10]int> v5 v1 v15 (159) = OffPtr <*int> [72] v6 v9 (159) = Load <int> v15 v1 v7 (159) = Neq64 <bool> v9 v13 If v7 → b5 loopHeader loopHeader: v8 (+159) = NilCheck <*[10]int> v5 v1 v11 (159) = OffPtr <*int> [72] v8 v12 (159) = Load <int> v11 v1 v14 (159) = Neq64 <bool> v12 v13 Plain → b3 So we need to add a loop nilcheckelim pass after all other loop opts to remove the v6 NilCheck in the loop guard, but that's not the whole story. The code _ = x[9] is expected to generate a nilcheck, buttighten pass will move the LoweredNilCheck from the loop entry b1 to the loop guard. At that point, there happens to be a CMPQconstload in the loop guard that meets the conditions for late nilcheckelim, resulting in the elimination of the LoweredNilCheck as well. Therefore, we also need to modify the test to no longer expect the generated code to contain a nilcheck.
1 parent f3ec1ea commit 08992fd

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ var passes = [...]pass{
492492
{name: "loop sccp", fn: sccp, disabled: !EnableLoopOpts}, // optimize loop guard conditional test
493493
{name: "loop opt", fn: opt, disabled: !EnableLoopOpts}, // further optimize loop guard conditional test
494494
{name: "loop deadcode late", fn: deadcode, disabled: !EnableLoopOpts}, // remove dead loop guard to simplify cfg
495+
{name: "loop nilcheckelim", fn: nilcheckelim, disabled: !EnableLoopOpts}, // remove duplicated nil check in loop guard
495496
{name: "writebarrier", fn: writebarrier, required: true}, // expand write barrier ops
496497
{name: "insert resched checks", fn: insertLoopReschedChecks,
497498
disabled: !buildcfg.Experiment.PreemptibleLoops}, // insert resched checks in loops.

test/nilptr3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func f4(x *[10]int) {
155155
// and the offset is small enough that if x is nil, the address will still be
156156
// in the first unmapped page of memory.
157157

158-
_ = x[9] // ERROR "generated nil check" // bug: would like to remove this check (but nilcheck and load are in different blocks)
158+
_ = x[9] // ERROR "removed nil check"
159159

160160
for {
161161
if x[9] != 0 { // ERROR "removed nil check"

0 commit comments

Comments
 (0)