Skip to content

Commit d36bc7d

Browse files
committed
runtime: split PCs out of scase
Per-case PCs are only needed for race detector builds, so this allows skipping allocating stack space for them for non-race builds. It's possible to arrange the PCs and order arrays consecutively in memory so that we could just reuse the order0 pointer to identify both. However, there's more risk of that silently going wrong, so this commit passes them as separate arguments for now. We can revisit this in the future. Updates #40410. Change-Id: I8468bc25749e559891cb0cb007d1cc4a40fdd0f8 Reviewed-on: https://go-review.googlesource.com/c/go/+/245124 Reviewed-by: Keith Randall <khr@golang.org>
1 parent 8a984e8 commit d36bc7d

File tree

4 files changed

+153
-118
lines changed

4 files changed

+153
-118
lines changed

src/cmd/compile/internal/gc/builtin.go

Lines changed: 100 additions & 99 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/compile/internal/gc/builtin/runtime.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ func selectnbsend(hchan chan<- any, elem *any) bool
169169
func selectnbrecv(elem *any, hchan <-chan any) bool
170170
func selectnbrecv2(elem *any, received *bool, hchan <-chan any) bool
171171

172-
func selectsetpc(cas *byte)
173-
func selectgo(cas0 *byte, order0 *byte, ncases int) (int, bool)
172+
func selectsetpc(pc *uintptr)
173+
func selectgo(cas0 *byte, order0 *byte, pc0 *uintptr, ncases int) (int, bool)
174174
func block()
175175

176176
func makeslice(typ *byte, len int, cap int) unsafe.Pointer

src/cmd/compile/internal/gc/select.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ func walkselectcases(cases *Nodes) []*Node {
271271
r = typecheck(r, ctxStmt)
272272
init = append(init, r)
273273

274+
var pc0, pcs *Node
275+
if flag_race {
276+
pcs = temp(types.NewArray(types.Types[TUINTPTR], int64(n)))
277+
pc0 = typecheck(nod(OADDR, nod(OINDEX, pcs, nodintconst(0)), nil), ctxExpr)
278+
} else {
279+
pc0 = nodnil()
280+
}
281+
274282
// register cases
275283
for i, cas := range cases.Slice() {
276284
setlineno(cas)
@@ -324,8 +332,8 @@ func walkselectcases(cases *Nodes) []*Node {
324332

325333
// TODO(mdempsky): There should be a cleaner way to
326334
// handle this.
327-
if instrumenting {
328-
r = mkcall("selectsetpc", nil, nil, bytePtrToIndex(selv, int64(i)))
335+
if flag_race {
336+
r = mkcall("selectsetpc", nil, nil, nod(OADDR, nod(OINDEX, pcs, nodintconst(int64(i))), nil))
329337
init = append(init, r)
330338
}
331339
}
@@ -337,13 +345,16 @@ func walkselectcases(cases *Nodes) []*Node {
337345
r = nod(OAS2, nil, nil)
338346
r.List.Set2(chosen, recvOK)
339347
fn := syslook("selectgo")
340-
r.Rlist.Set1(mkcall1(fn, fn.Type.Results(), nil, bytePtrToIndex(selv, 0), bytePtrToIndex(order, 0), nodintconst(int64(n))))
348+
r.Rlist.Set1(mkcall1(fn, fn.Type.Results(), nil, bytePtrToIndex(selv, 0), bytePtrToIndex(order, 0), pc0, nodintconst(int64(n))))
341349
r = typecheck(r, ctxStmt)
342350
init = append(init, r)
343351

344352
// selv and order are no longer alive after selectgo.
345353
init = append(init, nod(OVARKILL, selv, nil))
346354
init = append(init, nod(OVARKILL, order, nil))
355+
if flag_race {
356+
init = append(init, nod(OVARKILL, pcs, nil))
357+
}
347358

348359
// dispatch cases
349360
for i, cas := range cases.Slice() {
@@ -385,7 +396,6 @@ func scasetype() *types.Type {
385396
namedfield("c", types.Types[TUNSAFEPTR]),
386397
namedfield("elem", types.Types[TUNSAFEPTR]),
387398
namedfield("kind", types.Types[TUINT16]),
388-
namedfield("pc", types.Types[TUINTPTR]),
389399
})
390400
scase.SetNoalg(true)
391401
}

0 commit comments

Comments
 (0)