Skip to content

Commit 0ee0bb1

Browse files
committed
cmd/compile: make runtime/internal/sys.NotInHeap intrinsic
So next CL can get rid of go:notinheap pragma. Updates #46731 Change-Id: Ib2e2f2d381767e11cec10f76261b516188ddaa6a Reviewed-on: https://go-review.googlesource.com/c/go/+/422814 Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: David Chase <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 0cf996a commit 0ee0bb1

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

src/cmd/compile/internal/types/size.go

+12
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ func calcStructOffset(errtype *Type, t *Type, o int64, flag int) int64 {
184184
}
185185

186186
CalcSize(f.Type)
187+
// If type T contains a field F marked as not-in-heap,
188+
// then T must also be a not-in-heap type. Otherwise,
189+
// you could heap allocate T and then get a pointer F,
190+
// which would be a heap pointer to a not-in-heap type.
191+
if f.Type.NotInHeap() {
192+
t.SetNotInHeap(true)
193+
}
187194
if int32(f.Type.align) > maxalign {
188195
maxalign = int32(f.Type.align)
189196
}
@@ -391,6 +398,7 @@ func CalcSize(t *Type) {
391398
}
392399

393400
CalcSize(t.Elem())
401+
t.SetNotInHeap(t.Elem().NotInHeap())
394402
if t.Elem().width != 0 {
395403
cap := (uint64(MaxWidth) - 1) / uint64(t.Elem().width)
396404
if uint64(t.NumElem()) > cap {
@@ -412,6 +420,10 @@ func CalcSize(t *Type) {
412420
if t.IsFuncArgStruct() {
413421
base.Fatalf("CalcSize fn struct %v", t)
414422
}
423+
// Recognize and mark runtime/internal/sys.nih as not-in-heap.
424+
if sym := t.Sym(); sym != nil && sym.Pkg.Path == "runtime/internal/sys" && sym.Name == "nih" {
425+
t.SetNotInHeap(true)
426+
}
415427
w = calcStructOffset(t, t, 0, 1)
416428

417429
// make fake type to check later to

src/cmd/compile/internal/types/type.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,6 @@ func NewArray(elem *Type, bound int64) *Type {
625625
}
626626
t := newType(TARRAY)
627627
t.extra = &Array{Elem: elem, Bound: bound}
628-
t.SetNotInHeap(elem.NotInHeap())
629628
if elem.HasTParam() {
630629
t.SetHasTParam(true)
631630
}
@@ -1061,17 +1060,6 @@ func (t *Type) SetFields(fields []*Field) {
10611060
base.Fatalf("SetFields of %v: width previously calculated", t)
10621061
}
10631062
t.wantEtype(TSTRUCT)
1064-
for _, f := range fields {
1065-
// If type T contains a field F with a go:notinheap
1066-
// type, then T must also be go:notinheap. Otherwise,
1067-
// you could heap allocate T and then get a pointer F,
1068-
// which would be a heap pointer to a go:notinheap
1069-
// type.
1070-
if f.Type != nil && f.Type.NotInHeap() {
1071-
t.SetNotInHeap(true)
1072-
break
1073-
}
1074-
}
10751063
t.Fields().Set(fields)
10761064
}
10771065

@@ -1676,7 +1664,7 @@ func (t *Type) IsUntyped() bool {
16761664
}
16771665

16781666
// HasPointers reports whether t contains a heap pointer.
1679-
// Note that this function ignores pointers to go:notinheap types.
1667+
// Note that this function ignores pointers to not-in-heap types.
16801668
func (t *Type) HasPointers() bool {
16811669
return PtrDataSize(t) > 0
16821670
}

src/runtime/internal/sys/nih.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
package sys
66

7-
// TODO: make this as a compiler intrinsic type, and remove go:notinheap
8-
//
9-
//go:notinheap
7+
// NOTE: keep in sync with cmd/compile/internal/types.CalcSize
8+
// to make the compiler recognize this as an intrinsic type.
109
type nih struct{}
1110

1211
// NotInHeap is a type must never be allocated from the GC'd heap or on the stack,

0 commit comments

Comments
 (0)