From 0c38f72156b0a74be293579c165477ced9fe60d5 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Thu, 26 Jan 2023 13:25:06 -0800 Subject: [PATCH] Move procid to dynamic facts render Based on cl/504066914, this generates the proper procid offsets at build time. Package facts needs an update to properly skip underscore imports, which don't have facts for some reason (because they are unnamed?). Drop use of runtime.getprocid given that these generated code is pretty solid. PiperOrigin-RevId: 504926257 --- pkg/procid/BUILD | 22 +++++++++++++++++++--- pkg/procid/procid.go | 10 +++++++--- pkg/procid/procid_amd64.s | 19 ++++++++----------- pkg/procid/procid_arm64.s | 19 ++++++++----------- tools/nogo/facts/facts.go | 10 ++++++++++ 5 files changed, 52 insertions(+), 28 deletions(-) diff --git a/pkg/procid/BUILD b/pkg/procid/BUILD index 2838b5aca..9124b5b05 100644 --- a/pkg/procid/BUILD +++ b/pkg/procid/BUILD @@ -1,13 +1,29 @@ -load("//tools:defs.bzl", "go_library", "go_test") +load("//tools:defs.bzl", "arch_genrule", "go_library", "go_test", "select_arch") +load("//tools/nogo:defs.bzl", "nogo_facts") package(licenses = ["notice"]) +nogo_facts( + name = "procid_impl", + srcs = ["procid.go"], + output = "procid_impl.s", + template = select_arch( + amd64 = "procid_amd64.s", + arm64 = "procid_arm64.s", + ), +) + +arch_genrule( + name = "procid_impl_arch", + src = ":procid_impl", + template = "procid_impl_%s.s", +) + go_library( name = "procid", srcs = [ "procid.go", - "procid_amd64.s", - "procid_arm64.s", + ":procid_impl_arch", ], visibility = ["//visibility:public"], ) diff --git a/pkg/procid/procid.go b/pkg/procid/procid.go index e0d42819d..55a16a80a 100644 --- a/pkg/procid/procid.go +++ b/pkg/procid/procid.go @@ -12,12 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build go1.1 -// +build go1.1 - // Package procid provides a way to get the current system thread identifier. package procid +import ( + "runtime" +) + +// Dummy references for facts. +const _ = runtime.Compiler + // Current returns the current system thread identifier. // // Precondition: This should only be called with the runtime OS thread locked. diff --git a/pkg/procid/procid_amd64.s b/pkg/procid/procid_amd64.s index 54a2f6162..7173ddf1b 100644 --- a/pkg/procid/procid_amd64.s +++ b/pkg/procid/procid_amd64.s @@ -12,22 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build amd64 && go1.8 && !go1.21 && go1.1 -// +build amd64,go1.8,!go1.21,go1.1 - -// //go:linkname directives type-checked by checklinkname. Any other -// non-linkname assumptions outside the Go 1 compatibility guarantee should -// have an accompanied vet check or version guard build tag. +//go:build amd64 +// +build amd64 #include "textflag.h" +#define M_OFFSET {{ .import.runtime.g.m.Offset }} +#define PROCID_OFFSET {{ .import.runtime.m.procid.Offset }} + TEXT ·Current(SB),NOSPLIT,$0-8 - // The offset specified here is the m_procid offset for Go1.8+. - // Changes to this offset should be caught by the tests, and major - // version changes require an explicit tag change above. + // procid is in getg().m.procid. MOVQ TLS, AX MOVQ 0(AX)(TLS*1), AX - MOVQ 48(AX), AX // g_m (may change in future versions) - MOVQ 72(AX), AX // m_procid (may change in future versions) + MOVQ M_OFFSET(AX), AX // gp.m + MOVQ PROCID_OFFSET(AX), AX // mp.procid MOVQ AX, ret+0(FP) RET diff --git a/pkg/procid/procid_arm64.s b/pkg/procid/procid_arm64.s index 78736df39..22e23ef5b 100644 --- a/pkg/procid/procid_arm64.s +++ b/pkg/procid/procid_arm64.s @@ -12,21 +12,18 @@ // See the License for the specific language governing permissions and // limitations under the License. -//go:build arm64 && go1.8 && !go1.21 && go1.1 -// +build arm64,go1.8,!go1.21,go1.1 - -// //go:linkname directives type-checked by checklinkname. Any other -// non-linkname assumptions outside the Go 1 compatibility guarantee should -// have an accompanied vet check or version guard build tag. +//go:build arm64 +// +build arm64 #include "textflag.h" +#define M_OFFSET {{ .import.runtime.g.m.Offset }} +#define PROCID_OFFSET {{ .import.runtime.m.procid.Offset }} + TEXT ·Current(SB),NOSPLIT,$0-8 - // The offset specified here is the m_procid offset for Go1.8+. - // Changes to this offset should be caught by the tests, and major - // version changes require an explicit tag change above. + // procid is in getg().m.procid. MOVD g, R0 // g - MOVD 48(R0), R0 // g_m (may change in future versions) - MOVD 72(R0), R0 // m_procid (may change in future versions) + MOVD M_OFFSET(R0), R0 // gp.m + MOVD PROCID_OFFSET(R0), R0 // mp.procid MOVD R0, ret+0(FP) RET diff --git a/tools/nogo/facts/facts.go b/tools/nogo/facts/facts.go index 7de3a1edc..2578431d5 100644 --- a/tools/nogo/facts/facts.go +++ b/tools/nogo/facts/facts.go @@ -230,6 +230,13 @@ func (b *Bundle) Package(pkg *types.Package) (*Package, error) { return facts, nil } + if b.reader == nil { + // Nothing available. + // + // N.B. some bundles contain only cached packages. + return nil, nil + } + // Find based on the reader. for _, f := range b.reader.File { if f.Name != pkg.Path() { @@ -368,6 +375,9 @@ func Resolve(pkg *types.Package, localFacts *Package, allFacts *Bundle, allFactN if err != nil { return nil, err } + if importFacts == nil { + continue + } r.walkScope(append(names, "import", importPkg.Name()), importPkg.Scope(), importFacts, allFactNames) } return r, nil