Skip to content

cmd/compile: bad instruction chosen for pcln table for expressionless switch statements head #81705

Closed

Description

@aarzilli

With go version 1.27.0, compiling with -gcflags='all=-N -l' this program:

package main

import "fmt"

func f1() bool {
	fmt.Println("f1")
	return false
}

func f2() bool {
	fmt.Println("f2")
	return true
}

func f3() bool {
	fmt.Println("f3")
	return false
}

func main() {
	switch {
	case f1():
	case f2():
	case f3():
	}
}

Produces this assembly (I removed some irrelevant instructions and added labels in an attempt to make the problem clearer):

	test.go:22	S	0x4b648e	CALL main.f1(SB)  >>>
	test.go:22		0x4b6493	MOVB AL, 0x7(SP)
	test.go:22		0x4b6497	TESTL AL, AL
	test.go:22		0x4b6499	JNE 0x4b649d
	test.go:22		0x4b649b	JMP case_call_f2
	test.go:22	S	0x4b649d	JMP 0x4b64a0
	test.go:22		0x4b649f	NOPL
	test.go:22	S	0x4b64a0	JMP switch_exit

case_call_f2:

	test.go:23	S	0x4b64a2	CALL main.f2(SB)  >>>
	test.go:23		0x4b64a7	MOVB AL, 0x6(SP)
	test.go:23		0x4b64ab	TESTL AL, AL
	test.go:23		0x4b64ad	JNE 0x4b64b1
	test.go:23		0x4b64af	JMP case_call_f3
	test.go:23		0x4b64b3	JMP switch_exit

case_call_f3:

	test.go:24	S	0x4b64b5	CALL main.f3(SB)  >>>
	test.go:24		0x4b64ba	MOVB AL, 0x5(SP)
	test.go:24		0x4b64be	NOPW
	test.go:24		0x4b64c0	TESTL AL, AL
	test.go:24		0x4b64c2	JNE 0x4b64c6
	test.go:24		0x4b64c4	JMP switch_statement_head
	test.go:24	S	0x4b64c6	JMP 0x4b64c8
	test.go:24		0x4b64c8	JMP switch_exit

switch_statement_head:

	test.go:21	S	0x4b64ca	JMP switch_exit

switch_exit:

	test.go:26	S	0x4b64cc	ADDQ $0x8, SP
	test.go:26		0x4b64d0	POPQ BP
	test.go:26		0x4b64d1	RET

The instruction chose for the head of the switch statement, 0x4b64ca, is only executed if all three cases fail and even them only after they've been executed. That means that a breakpoint placed on that line will appear to be skipped often, an effect that is particularly jarring inside a loop.

cc @dr2chase

Activity

  1. thepudds commented on Sep 23, 2026

    @thepudds
    Member

    Hi @aarzilli, thanks for reporting this.

    To your knowledge, is this a new-ish problem, or maybe one that might have been around for a while, or maybe you are unsure of whether it's new?

    (A simple scripted step-back using objdump got to roughly Go 1.16, but maybe that's wrong. Haven't looked carefully, and didn't try with a debugger.)

  2. aarzilli commented on Sep 23, 2026

    @aarzilli
    ContributorAuthor

    I hadn't checked. Both 1.25 and 1.26 seem to behave the same, you could be right.

  3. thepudds commented on Sep 23, 2026

    @thepudds
    Member

    FWIW, an objdump based bisect landed on 59b8916 from 2020:

      first BAD:   59b8916d48 | 2020-12-03 16:22 | [dev.regabi] cmd/compile: handle OCONVNOP better in ssa
      GOOD parent: 00e5727790 | 2020-12-03 08:30 | [dev.regabi] cmd/compile: remove okAs
    

    (That was a simple-ish bisect script so maybe wrong, and also maybe that commit is just jostling the behavior of the reproducer. A quick look suggests OCONVNOP could plausibly be involved, but not sure.)

  4. subhramit commented on Sep 25, 2026

    @subhramit
    Contributor

    Hi, I could reproduce this.
    I spent some time looking through this, and I think I found the root cause. Will elaborate soon, and possibly send a fix.

  5. subhramit commented on Sep 25, 2026

    @subhramit
    Contributor

    So there are two parts to the problem.

    First, walkSwitchExpr turns an expressionless switch into a switch whose tag is the constant true, and only copies non-constant tags to a temporary. That copy normally gives the switch line an instruction before case dispatch. The constant true needs no evaluation, so an expressionless switch has no such instruction.

    Second, when a switch has no explicit default, walk creates an implicit OBREAK for the path where no case matches and deliberately marks it PosNotStmt:

    if defaultGoto == nil {
    br := ir.NewBranchStmt(base.Pos, ir.OBREAK, nil)
    br.SetPos(br.Pos().WithNotStmt())
    defaultGoto = br
    }
    s.Emit(w, &sw.Compiled)
    sw.Compiled.Append(defaultGoto)
    sw.Compiled.Append(body.Take()...)
    w.walkStmtList(sw.Compiled)

    Ssagen currently marks every lowered OBREAK or OCONTINUE block as a statement with s.lastPos.WithIsStmt(), discarding walk's marker.

    Together, these behaviors mean there is no instruction on the switch line before the cases, while the implicit no-match jump after the cases is incorrectly marked as the switch statement. A breakpoint on the switch line therefore stops only after all cases fail, and does not stop there if a case matches.

    Now coming to the fix, I filed #81756 (https://go-review.googlesource.com/c/go/+/838905).

    With -N, ssagen now ends the current block at the source switch or select position before lowering the case dispatch. The block ends in a jump on the switch line, giving the statement an instruction that executes before any case is evaluated.

    Independently of -N, ssagen now also preserves PosNotStmt when lowering a generated break or continue, removing the incorrect late switch-line stop from the no-match path in optimized builds as well.

    The preheader is limited to -N, so optimized builds do not get an additional CFG block. It is also limited to source statements, so internal switches synthesized while lowering type switches do not acquire debugger stops of their own.

    Verification

    I added TestDebugLines_81705 using the existing debug-lines test framework. Before the fix, the statement-line order for the regression program was:

    13, 14, 15, 16, 12
    

    Line 12 is the expressionless switch line, so it appeared only after both cases had been evaluated and failed.

    After the fix, the order is:

    12, 13, 14, 15, 16
    

    The generated dump now begins case dispatch with a statement-marked jump on line 12:

    b1   00003 (+12)  JMP   4
    v18  00004 (+13)  MOVL  $1, AX
    

    The final no-match jump is no longer marked as the switch statement:

    b7   00023 (-15) JMP   24
    

    cc @aarzilli @thepudds @dr2chase

    Update - as asked by @randall77 on Gerrit, have made the fix unconditional.

  6. gopherbot commented on Sep 25, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/838905 mentions this issue: cmd/compile: fix switch head position with -N

  7. added a commit that references this issue on Sep 28, 2026
    2a2f428
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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