Skip to content

cmd/compile: schedule block with predicted branch - #170

Open
ctk-1998 wants to merge 1 commit into
go1.26.5-zte-devfrom
blockpredict
Open

ctk-1998 wants to merge 1 commit into
go1.26.5-zte-devfrom
blockpredict

Conversation

@ctk-1998

@ctk-1998 ctk-1998 commented Sep 11, 2026

Copy link
Copy Markdown
Collaborator

Related Issue(s) & Descriptions

Original pr: https://atomgit.com/openeuler/golang/pull/85, authored by Aleksey Markin.

Port the predicted-branch block layout optimization from the Kunpeng fork. Basic blocks are now laid out along the statically predicted hot path: once layout follows a likely successor, it keeps following single-successor blocks so the whole hot path stays fall-through and error handling is pushed into the cold tail.

  • ir: add IfStmt.UnLikely, mutually exclusive with Likely
  • walk: heuristic prediction of error handling patterns, enabled with -d=blockpredict>=2; "err == nil" is likely, "err != nil" is unlikely, and a body/else returning nil vs non-nil error hints at the hot side
  • ssagen: map Likely/UnLikely to BranchLikely/BranchUnlikely
  • ssa: extend layoutOrder to follow the trace through single-successor blocks after a predicted edge (-d=blockpredict>=1)
  • port the two tests that came with the patch: ssa/layout_test.go (asserts the predicted-path block order) and test/errorlikely_test.go (end-to-end assembly order)
  • newly add some benchmarks in test/blockpredict_test.go to quantify the benefit

Tests

Benchmarks on sg2044:

goos: linux
goarch: riscv64
pkg: cmd/compile/internal/test
                            │  default(0) │            blockpredict=1        │         blockpredict=2          │
                            │   sec/op    │   sec/op     vs base              │   sec/op     vs base              │
ErrorCheckChain/errrate-0     192.8n ± 1%   195.6n ± 1%  +1.48% (p=0.002 n=6)   192.8n ± 0%       ~ (p=0.491 n=6)
ErrorCheckChain/errrate-10    891.4n ± 0%   867.5n ± 0%  -2.67% (p=0.002 n=6)   837.4n ± 2%  -6.05% (p=0.002 n=6)
ErrorCheckChain/errrate-50    868.5n ± 1%   832.1n ± 2%  -4.20% (p=0.002 n=6)   818.8n ± 1%  -5.73% (p=0.002 n=6)
ErrorCheckChain/errrate-100   854.2n ± 1%   824.6n ± 0%  -3.47% (p=0.002 n=6)   798.6n ± 1%  -6.51% (p=0.002 n=6)
MiddlewareChain/errrate-0     88.97n ± 0%   93.81n ± 0%  +5.44% (p=0.002 n=6)   89.38n ± 0%  +0.47% (p=0.004 n=6)
MiddlewareChain/errrate-10    1.929µ ± 1%   1.939µ ± 2%       ~ (p=0.121 n=6)   1.941µ ± 1%       ~ (p=0.093 n=6)
MiddlewareChain/errrate-50    9.260µ ± 1%   9.305µ ± 1%       ~ (p=0.180 n=6)   9.282µ ± 2%       ~ (p=0.589 n=6)
MiddlewareChain/errrate-100   18.28µ ± 0%   18.44µ ± 1%  +0.91% (p=0.004 n=6)   18.43µ ± 1%       ~ (p=0.065 n=6)
NoErrorBaseline/rate-0        190.6n ± 0%   192.2n ± 1%  +0.87% (p=0.002 n=6)   191.0n ± 2%       ~ (p=0.320 n=6)
NoErrorBaseline/rate-50       241.7n ± 0%   242.7n ± 0%       ~ (p=0.128 n=6)   243.5n ± 1%  +0.77% (p=0.004 n=6)
geomean                       837.9n        837.4n       -0.06%                 824.8n       -1.56%

Checklist

  • Tests were added or are not required
  • Documentation was added or is not required

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant