Skip to content

cmd/internal/obj: optimize wrapper method prologue for branch prediction #19042

@josharian

Description

@josharian

Wrapper functions (such as those found in CL 36809) have an extra prologue inserted during preprocessing. Quoting cmd/internal/obj/xk86/obj6.go (after CL 36833), what this does is:

		// if g._panic != nil && g._panic.argp == FP {
		//   g._panic.argp = bottom-of-frame
		// }
		//
		//	MOVQ g_panic(CX), BX
		//	TESTQ BX, BX
		//	JEQ end
		//	LEAQ (autoffset+8)(SP), DI
		//	CMPQ panic_argp(BX), DI
		//	JNE end
		//	MOVQ SP, panic_argp(BX)
		// end:
		//	NOP

These jumps are the wrong direction. Static branch prediction says forward jumps are not taken, but the first forward jump is almost always taken. Rearrange the code to fix that.

I have a CL, to be mailed momentarily, that does this. However, I am having a hard time writing a test for this, because I cannot find a way to trigger the rare (in-a-panic) cases. From reading the panic sources, this only happens when panicking inside a deferred statement (?), but even then I see the wrapper function in the traceback.

What exactly is this prologue for? How do I test that it is doing its job correctly?

cc @randall77 @minux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions