Skip to content

cmd/compile: inlining blocks integer range optimization #74915

Closed

Description

@mundaym

Consider the following code:

func gte4(x uint64) bool {
	return 4 <= x
}

func lt20(x uint64) bool {
	return x < 20
}

func example(x uint64, t, f int) int {
	if gte4(x) && lt20(x) {
		return t
	}
	return f
}

When compiled this produces:

00007 CMP $4, R0
00008 BLO 14
00009 NOP
00010 CMP $20, R0
00011 BHS 14
00012 MOVD R1, R0
00013 RET
00014 MOVD R2, R0
00015 RET

If, however, I manually inline the gte4 and lt20 functions the result is more optimal:

00006 SUB $4, R0, R3
00007 CMP $16, R3
00008 BHS 11
00009 MOVD R1, R0
00010 RET
00011 MOVD R2, R0
00012 RET

After a bit of investigation it turns out this is because the InlMark op has a memory argument which currently prevents speculative execution of any block containing it. That in turn blocks the integer range check optimisation. I will send a fix.

Activity

  1. self-assigned this
    on Aug 6, 2025
  2. added theissue type on Aug 6, 2025
  3. gopherbot commented on Aug 6, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/693795 mentions this issue: cmd/compile: allow InlMark operations to be speculatively executed

  4. added
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    on Aug 8, 2025
  5. added this to the Go1.26 milestone on Aug 8, 2025
  6. locked and limited conversation to collaborators on Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

FixPendingIssues that have a fix which has not yet been reviewed or submitted.FrozenDueToAgePerformancecompiler/runtimeIssues related to the Go compiler and/or runtime.

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions