Skip to content

cmd/compile: internal compiler error in midway rewriter on 'for range N' inside SIMD-using function #81264

Closed

Description

@YoshikiShibata

Go version

go version go1.28-devel_da7c67f595 Fri Aug 28 14:42:09 2026 -0700 darwin/arm64

(gotip at commit da7c67f595, built with the simd experiment.)

Output of go env (relevant)

GOOS=darwin
GOARCH=arm64
GOEXPERIMENT=simd

What did you do?

Minimal reproducer (build with GOEXPERIMENT=simd):

//go:build goexperiment.simd

package p

import "simd"

func f() {
    var _ simd.Float32s
    for range 1 {
    }
}
GOEXPERIMENT=simd go build ./...

What did you expect to see?

A successful build.

What did you see instead?

<unknown line number>: internal compiler error: panic: runtime error: invalid memory address or nil pointer dereference

goroutine 1 [running]:
...
cmd/compile/internal/midway.(*DeepCopier).CopySimpleStmt(...)
    cmd/compile/internal/midway/deepcopy.go:525 +0x358
cmd/compile/internal/midway.(*DeepCopier).CopyStmt(...)
    cmd/compile/internal/midway/deepcopy.go:469 +0x230
cmd/compile/internal/midway.(*DeepCopier).CopyBlockStmt(...)
    cmd/compile/internal/midway/deepcopy.go:600 +0xa4
cmd/compile/internal/midway.(*DeepCopier).CopyFuncDecl(...)
    cmd/compile/internal/midway/deepcopy.go:288 +0x2cc
cmd/compile/internal/midway.(*Rewriter).Rewrite(...)
    cmd/compile/internal/midway/rewrite.go:186 +0x8c
cmd/compile/internal/midway.RewriteWrapper(...)
    cmd/compile/internal/midway/rewrite.go:540 +0x2ec

Analysis

The crash happens in CopySimpleStmt's *syntax.RangeClause handling at deepcopy.go:522-525:

} else {
    newS.Lhs = c.CopyExpr(s.Lhs)     // returns nil when s.Lhs is nil
}
newS.Lhs.SetPos(s.Lhs.Pos())         // panics: nil.SetPos() (and nil.Pos())

For the Go 1.22 integer-range form for range N (no LHS variable, not even _), s.Lhs is nil, so newS.Lhs becomes nil and both newS.Lhs.SetPos(...) and s.Lhs.Pos() on line 525 dereference nil.

Trigger matrix

Code inside function Result
for range 1 {} in a function that references a simd.* type crash
for i := range 1 { _ = i } in the same function ok
for j := 0; j < 1; j++ {} in the same function ok
for range 1 {} in a package that uses no SIMD types ok

So the trigger is: (a) the enclosing function is rewritten by the midway pass — any use of a SIMD type is sufficient — AND (b) the loop is an integer-range with no LHS.

Suggested fix

Guard both the copy and the SetPos on s.Lhs != nil:

if s.Lhs != nil {
    // existing dispatch on ListExpr / Name / else
    newS.Lhs.SetPos(s.Lhs.Pos())
}

Activity

  1. cuonglm commented on Sep 1, 2026

    @cuonglm
    Member

    It seems to me we could just remove that newS.Lhs.SetPos(s.Lhs.Pos()) entirely, since both c.CopyName and c.CopyExpr return the copied node with position already set.

  2. self-assigned this
    on Sep 1, 2026
  3. gopherbot commented on Sep 1, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/825524 mentions this issue: cmd/compile: fix ICE with for range clause in midway pass

  4. added a commit that references this issue on Sep 1, 2026
    898e190
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions