You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: bad instruction chosen for pcln table for expressionless switch statements head #81705
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.
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.)
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:
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.
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:
Activity
thepudds commentedon Sep 23, 2026
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.)
gabyhelp commentedon Sep 23, 2026
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
aarzilli commentedon Sep 23, 2026
I hadn't checked. Both 1.25 and 1.26 seem to behave the same, you could be right.
thepudds commentedon Sep 23, 2026
FWIW, an objdump based bisect landed on 59b8916 from 2020:
(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.)
subhramit commentedon Sep 25, 2026
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.
subhramit commentedon Sep 25, 2026
So there are two parts to the problem.
First,
walkSwitchExprturns an expressionless switch into a switch whose tag is the constanttrue, and only copies non-constant tags to a temporary. That copy normally gives the switch line an instruction before case dispatch. The constanttrueneeds no evaluation, so an expressionless switch has no such instruction.Second, when a switch has no explicit default, walk creates an implicit
OBREAKfor the path where no case matches and deliberately marks itPosNotStmt:go/src/cmd/compile/internal/walk/switch.go
Lines 120 to 129 in 9904c2c
Ssagen currently marks every lowered
OBREAKorOCONTINUEblock as a statement withs.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 preservesPosNotStmtwhen lowering a generatedbreakorcontinue, 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_81705using the existing debug-lines test framework. Before the fix, the statement-line order for the regression program was:Line 12 is the expressionless
switchline, so it appeared only after both cases had been evaluated and failed.After the fix, the order is:
The generated dump now begins case dispatch with a statement-marked jump on line 12:
The final no-match jump is no longer marked as the switch statement:
cc @aarzilli @thepudds @dr2chase
Update - as asked by @randall77 on Gerrit, have made the fix unconditional.
gopherbot commentedon Sep 25, 2026
Change https://go.dev/cl/838905 mentions this issue:
cmd/compile: fix switch head position with -Ncmd/compile: fix switch statement positions