Skip to content

cmd/compile: PGO may cause changes to the execution timing of defer in rangefunc #77033

@wdvxdr1123

Description

@wdvxdr1123

Go version

gotip

What did you do?

package main

import (
	"testing"
)

var a int

func save(x int) {
	a = x
}

func foo() {
	for i := range yield1 {
		defer save(i)
	}
}

func yield1(yield func(int) bool) {
	yield(1)
}

func BenchmarkFoo(b *testing.B) {
	for i := 0; i < b.N; i++ {
		foo()
	}
	if a != 1 {
		b.Fatalf("a = %d; want 1", a)
	}
}

collect profile

go test -bench=Foo -cpuprofile=default.pgo

run pgo optimized bench

go test -pgo=default.pgo -bench=Foo

What did you see happen?

--- FAIL: BenchmarkFoo
    example_test.go:28: a = 0; want 1
FAIL
exit status 1
FAIL    tools   0.430s

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions