aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/noder
AgeCommit message (Collapse)Author
2025-05-20internal/pkgbits: rename RelocEnt to RefTableEntryMark Freeman
Change-Id: I9b1c9a0499ad3444e8cb3e4be187f9fab816c90c Reviewed-on: https://go-review.googlesource.com/c/go/+/674159 Reviewed-by: Robert Griesemer <gri@google.com> Auto-Submit: Mark Freeman <mark@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-05-20cmd/compile/internal/noder: mark Ref[T] as a primitiveMark Freeman
Like Sync, Ref[T] is also used to define things like StringRef. Change-Id: I9e10234504ee4dd03907bb058a6f3ae7e6a287ca Reviewed-on: https://go-review.googlesource.com/c/go/+/674157 Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Bypass: Mark Freeman <mark@golang.org> Auto-Submit: Mark Freeman <mark@golang.org>
2025-05-19cmd/compile/internal/noder: mark Sync as a primitiveMark Freeman
Sync is used in the definition of primitives and documented by pkgbits. It's not much help to also document it here. Change-Id: I18bd0c7816f8249483550a1f0af7c76b9cfe09fb Reviewed-on: https://go-review.googlesource.com/c/go/+/674156 Auto-Submit: Mark Freeman <mark@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Bypass: Mark Freeman <mark@golang.org>
2025-05-19cmd/compile/internal/noder: document SectionPkgMark Freeman
The package section holds package stubs, which are a package (path, name) pair and a series of declared imports. Change-Id: If2a260c5e0a3522851be9808de46a3f128902002 Reviewed-on: https://go-review.googlesource.com/c/go/+/674175 Auto-Submit: Mark Freeman <mark@golang.org> TryBot-Bypass: Mark Freeman <mark@golang.org> Reviewed-by: Robert Griesemer <gri@google.com>
2025-05-19cmd/compile/internal/noder: format grammarMark Freeman
This just wraps column width to 72 and indents production definitions so they are easier to distinguish from prose. Change-Id: I386b122b4f617db4b182ebb549fbee4f35a0122c Reviewed-on: https://go-review.googlesource.com/c/go/+/673536 TryBot-Bypass: Mark Freeman <mark@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> Auto-Submit: Mark Freeman <mark@golang.org>
2025-05-19cmd/compile/internal/noder: document the PosBase sectionMark Freeman
Positions mostly borrow their representation from package syntax. Of note, constants (such as the zero value for positions) are not encoded directly. Rather, a flag typically signals such values. Change-Id: I6b4bafc6e96bb21902dd2d6e164031e7dd5aabdd Reviewed-on: https://go-review.googlesource.com/c/go/+/673535 TryBot-Bypass: Mark Freeman <mark@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> Auto-Submit: Mark Freeman <mark@golang.org>
2025-05-19std: pass bytes.Buffer and strings.Builder by pointerAlan Donovan
This CL fixes a number of (all true positive) findings of vet's copylock analyzer patched to treat the Bu{ff,uild}er types as non-copyable after first use. This does require imposing an additional indirection between noder.writer and Encoder since the field is embedded by value but its constructor now returns a pointer. Updates golang/go#25907 Updates golang/go#47276 Change-Id: I0b4d77ac12bcecadf06a91709e695365da10766c Reviewed-on: https://go-review.googlesource.com/c/go/+/635339 Reviewed-by: Robert Findley <rfindley@google.com> Commit-Queue: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Alan Donovan <adonovan@google.com>
2025-05-14internal/pkgbits, cmd/compile/internal/noder: document string sectionMark Freeman
To understand this change, we begin with a short description of the UIR file format. Every file is a header followed by a series of sections. Each section has a kind, which determines the type of elements it contains. An element is just a collection of one or more primitives, as defined by package pkgbits. Strings have their own section. Elements in the string section contain only string primitives. To use a string, elements in other sections encode a reference to the string section. To illustrate, consider a simple file which exports nothing at all. package p In the meta section, there is an element representing a package stub. In that package stub, a string ("p") represents both the path and name of the package. Again, these are encoded as references. To manage references, every element begins with a reference table. Instead of writing the bytes for "p" directly, the package stub encodes an index in this reference table. At that index, a pair of numbers is stored, indicating: 1. which section 2. which element index within the section Effectively, elements always use *2* layers of indirection; first to the reference table, then to the bytes themselves. With some minor hand-waving, an encoding for the above package is given below, with (S)ections, (E)lements and (P)rimitives denoted. + Header | + Section Ends // each section has 1 element | | + 1 // String is elements [0, 1) | | + 2 // Meta is elements [1, 2) | + Element Ends | | + 1 // "p" is bytes [0, 1) | | + 6 // stub is bytes [1, 6) + Payload | + (S) String | | + (E) String | | | + (P) String { byte } 0x70 // "p" | + (S) Meta | | + (E) Package Stub | | | + Reference Table | | | | + (P) Entry Count uvarint 1 // there is a single entry | | | | + (P) 0th Section uvarint 0 // to String, 0th section | | | | + (P) 0th Index uvarint 0 // to 0th element in String | | | + Internals | | | | + (P) Path uvarint 0 // 0th entry in table | | | | + (P) Name uvarint 0 // 0th entry in table Note that string elements do not have reference tables like other elements. They behave more like a primitive. As this is a bit complicated and getting into details of the UIR file format, we omit some details in the documentation here. The structure will become clearer as we continue documenting. Change-Id: I12a5ce9a34251c5358a20f2f2c4d0f9bd497f4d0 Reviewed-on: https://go-review.googlesource.com/c/go/+/671997 Reviewed-by: Robert Griesemer <gri@google.com> Auto-Submit: Mark Freeman <mark@golang.org> TryBot-Bypass: Mark Freeman <mark@golang.org>
2025-05-12internal/pkgbits: rename Reloc* to Section*Mark Freeman
This is a basic refactoring. This enumeration refers primarily to the different sections of a UIR file, so this naming is a bit more direct. Change-Id: Ib70ab054e97effaabc035450d246ae4354da8075 Reviewed-on: https://go-review.googlesource.com/c/go/+/671935 Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Mark Freeman <mark@golang.org>
2025-05-09cmd/compile/internal/noder: begin documenting meta sectionMark Freeman
Meta is the most fundamental section. To flesh this out, we discuss references. Primitives are briefly mentioned by pointing to pkgbits, where they will be defined using a similar grammar. Change-Id: I7abd899f38fad4cc5caf87ebfc7aa1b1985b17d4 Reviewed-on: https://go-review.googlesource.com/c/go/+/671176 Auto-Submit: Mark Freeman <mark@golang.org> TryBot-Bypass: Mark Freeman <mark@golang.org> Reviewed-by: Robert Griesemer <gri@google.com>
2025-05-08cmd/compile/internal/noder: begin a formal UIR grammar.Mark Freeman
The UIR export data format can be reasonably expressed using EBNF. The noder owns the definition of the export data format, so this seems like a reasonable place to put this. Change-Id: I0205ab29a3c5e57d670d7fd3164a8bd604ab8e59 Reviewed-on: https://go-review.googlesource.com/c/go/+/670616 Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Bypass: Mark Freeman <mark@golang.org> Auto-Submit: Mark Freeman <mark@golang.org>
2025-05-06pkgbits: replace references to RelocKind with SectionKindMark Freeman
Change-Id: Id194a42645d1da6440558bf12dc252f347072f86 Reviewed-on: https://go-review.googlesource.com/c/go/+/670175 Auto-Submit: Mark Freeman <mark@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Mark Freeman <mark@golang.org> Reviewed-by: Robert Griesemer <gri@google.com>
2025-03-11cmd/compile: Enable inlining of tail callsAlexander Musman
Enable inlining tail calls and do not limit emitting tail calls only to the non-inlineable methods when generating wrappers. This change produces additional code size reduction. Code size difference measured with this change (tried for x86_64): etcd binary: .text section size: 10613393 -> 10593841 (0.18%) total binary size: 33450787 -> 33424307 (0.07%) compile binary: .text section size: 10171025 -> 10126545 (0.43%) total binary size: 28241012 -> 28192628 (0.17%) cockroach binary: .text section size: 83947260 -> 83694140 (0.3%) total binary size: 263799808 -> 263534160 (0.1%) Change-Id: I694f83cb838e64bd4c51f05b7b9f2bf0193bb551 Reviewed-on: https://go-review.googlesource.com/c/go/+/650455 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Keith Randall <khr@golang.org> Auto-Submit: Keith Randall <khr@golang.org>
2025-02-15std: add //go:fix inline directives to some deprecated functionsAlan Donovan
In particular, we apply it only to functions where it is always a code improvement to inline the call. We also apply it to some constants. In a few cases this may introduce a panic statement at the caller, which is debatable, but making the potential for panic evident is the purpose of the deprecation. The gofix analyzer in gopls v0.18 will show a diagnostic for calls to the annotated functions, and will offer to inline the call. The new //go:fix annotation needs a special exemption in the pragma check in the compiler. Updates #32816 Change-Id: I43bf15648ac12251734109eb7102394f8a76d55e Reviewed-on: https://go-review.googlesource.com/c/go/+/648995 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Commit-Queue: Alan Donovan <adonovan@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org> Auto-Submit: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-11-22internal/exportdata, cmd/compile/internal/noder: merge export data handlingTim King
Unify how go/types, types2, and noder read in unified export data from GC-created files. This splits FindExportData into smaller pieces for improved code sharing. - FindPackageDefinition finds the package definition file in the ar archive. - ReadObjectHeaders reads the object headers. - ReadExportDataHeader reads the export data format header. There is a new convenience wrapper ReadUnified that combines all of these. This documents the expected archive contents. Updates noder and the importers to use these. This also adjusts when end-of-section marker ("\n$$\n") checking happens. Change-Id: Iec2179b0a1ae7f69eb12d077018f731116a77f13 Reviewed-on: https://go-review.googlesource.com/c/go/+/628155 Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Commit-Queue: Tim King <taking@google.com>
2024-11-12cmd/compile/internal/noder,go/internal/gcimporter: return an error if not an ↵Tim King
archive file Return an error from FindExportData variants if the contents are not an archive file. Change-Id: I2fa8d3553638ef1de6a03e2ce46341f00ed6965f Reviewed-on: https://go-review.googlesource.com/c/go/+/626697 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Griesemer <gri@google.com> Commit-Queue: Tim King <taking@google.com>
2024-11-08cmd/compile/internal/noder: replace recompile library error messagesTim King
Replaces 'recompile library' error messages with the more accurate 'recompile package' globally. Change-Id: I7247964c76f1fcb94feda37c78bdfb8a1b1a6492 Reviewed-on: https://go-review.googlesource.com/c/go/+/626696 Reviewed-by: Alan Donovan <adonovan@google.com> Commit-Queue: Tim King <taking@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-11-04cmd/compile: fix mis-compilation with labeled fallthroughCuong Manh Le
A fallthrough statement can be a labeled fallthrough per Go spec. However, the hasFallthrough function is not considering this case, causing mis-compilation. Fixing this by un-wrapping (possible nested) labeled fallthrough statements if any. Fixes #70173 Change-Id: Ic93d4fb75ff02703a32dfc63c3e84a8b7f78c261 Reviewed-on: https://go-review.googlesource.com/c/go/+/624717 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Youlin Feng <fengyoulin@live.com> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-11-04cmd/compile: fix inlining name mangling for blank labelCuong Manh Le
Fixes #70175 Change-Id: I13767d951455854b03ad6707ff9292cfe9097ee9 Reviewed-on: https://go-review.googlesource.com/c/go/+/624377 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Keith Randall <khr@golang.org> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Keith Randall <khr@google.com> Auto-Submit: Keith Randall <khr@golang.org>
2024-10-07cmd/compile: avoid dynamic type when possibleCuong Manh Le
If the expression type is a single compile-time known type, use that type instead of the dynamic one, so the later passes of the compiler could skip un-necessary runtime calls. Thanks Youlin Feng for writing the original test case. Change-Id: I3f65ab90f041474a9731338a82136c1d394c1773 Reviewed-on: https://go-review.googlesource.com/c/go/+/616975 Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2024-09-11cmd/compile: remove types.Type rparams fieldCuong Manh Le
This field is present during the initial development of generic support inside compiler, and indicating whether a type is fully instantiated is the solely purpose at this moment. Further, its name is also confused, and there have been a TODO to chose a better name for it. Instead, just using a bit to track whether a type is fully instantiated, then this rparams field can be removed to simplify the code. Change-Id: Ia29c6dd5792487c440b83b0f3b77bd60917c2019 Reviewed-on: https://go-review.googlesource.com/c/go/+/611255 Reviewed-by: Tim King <taking@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@google.com> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Keith Randall <khr@golang.org>
2024-09-09cmd/compile: emit tail call wrappers when possibleamusman
Use OTAILCALL in wrapper if the receiver and method are both pointers and it is not going to be inlined, similar to how it is done in reflectdata.methodWrapper. Currently tail call may be used for functions with identical argument types. This change updates wrappers where both wrapper and the wrapped method's receiver are pointers. In this case, we have the same signature for the wrapper and the wrapped method (modulo the receiver's pointed-to types), and do not need any local variables in the generated wrapper (on stack) because the arguments are immediately passed to the wrapped method in place (without need to move some value passed to other register or to change any argument/return passed through stack). Thus, the wrapper does not need its own stack frame. This applies to promoted methods, e.g. when we have some struct type U with an embedded type *T and construct a wrapper like func (recv *U) M(arg int) bool { return recv.T.M(i) } See also test/abi/method_wrapper.go for a running example. Code size difference measured with this change (tried for x86_64): etcd binary: .text section size: 21472251 -> 21432350 (0.2%) total binary size: 32226640 -> 32191136 (0.1%) compile binary: .text section size: 17419073 -> 17413929 (0.03%) total binary size: 26744743 -> 26737567 (0.03%) Change-Id: I9bbe730568f6def21a8e61118a6b6f503d98049c Reviewed-on: https://go-review.googlesource.com/c/go/+/578235 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com>
2024-09-05cmd/compile: use slices.{Sort,SortFunc}Cuong Manh Le
Now that we're bootstrapping from a toolchain that has the slices package. Updates #64751 Change-Id: I2e63d95577d058670d3dc75bd45d6e050c6f0e25 Reviewed-on: https://go-review.googlesource.com/c/go/+/610601 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-09-04cmd: do not use notsha256Cuong Manh Le
CL 402595 used notsha256 to prevent the compiler from depending on cgo-based implementations of sha1 and sha256. However, since CL 454836, cmd is built with CGO_ENABLED=0, which will disable boringcrypto. Thus all usages of notsha256 is not necessary anymore. Updates #51940 Updates #64751 Change-Id: I503090f7a2efb5723e8a79523b143dc7cdb4edd0 Reviewed-on: https://go-review.googlesource.com/c/go/+/610596 Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Keith Randall <khr@golang.org>
2024-08-28cmd/compile/internal/noder: write V2 bitstream aliastypeparams=1Tim King
Enables V2 unified IR bitstreams when GOEXPERIMENT aliastypeparams are enabled. Allows pkgbits.NewPkgEncoder to set the output version. Reenables support for writing V0 streams. Updates #68778 Updates #68526 Change-Id: I590c494d81ab7db148232ceaba52229068d1e986 Reviewed-on: https://go-review.googlesource.com/c/go/+/608595 Reviewed-by: David Chase <drchase@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2024-08-26cmd/compile: deprecate derived info needed fieldCuong Manh Le
This field is unused since shape-based stenciling was added for Unified IR (CL 421821). The derived types information is now explicitly using derived-type dictionaries (CL 331829). This CL follows the pattern used in CL 606035. Updates #68778 Change-Id: Ie784b6443c0a651854bfbcebb8a5166b1481408b Reviewed-on: https://go-review.googlesource.com/c/go/+/608216 Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Tim King <taking@google.com>
2024-08-23cmd/compile/internal: write type parameters for aliasesTim King
Writes the field for type parameter names for aliases when the bitstream is >= V2. This is a no-op at the moment as the writer is hardwired to V1. Updates #68778 Change-Id: I5887e3608239b9a6a47e3cc21cacb75b84e1d186 Reviewed-on: https://go-review.googlesource.com/c/go/+/607235 Reviewed-by: David Chase <drchase@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2024-08-20cmd/compile: deprecate has init and derived func instanceTim King
Removes 'has init' and 'derived func instance' fields from unified IR starting with V2. This should be a no-op at the moment as the writer is hardwired to create V1. Updates #68778 Change-Id: I84a606cbc27cd6d8bd6eee2aff44c89f4aa7413c Reviewed-on: https://go-review.googlesource.com/c/go/+/606035 Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: David Chase <drchase@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-08-20cmd/compile/internal/noder: reduce clutter a bit (cosmetic changes)Robert Griesemer
- introduce index alias - inline the two short tables in stmt.go (removes a TODO) - move assert out of stencil.go and remove that file (we can always re-introduce it) Also, replace two if's with a simpler switch. Change-Id: I25c3104164574999dd9826dee6166dd8a8488908 Reviewed-on: https://go-review.googlesource.com/c/go/+/607236 Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Tim King <taking@google.com>
2024-08-09cmd/compile: add basic wasmexport supportCherry Mui
This CL adds a compiler directive go:wasmexport, which applies to a Go function and makes it an exported function of the Wasm module being built, so it can be called directly from the host. As proposed in #65199, parameter and result types are limited to 32-bit and 64-bit integers and floats, and there can be at most one result. As the Go and Wasm calling conventions are different, for a wasmexport function we generate a wrapper function does the ABI conversion at compile time. Currently this CL only adds basic support. In particular, - it only supports executable mode, i.e. the Go wasm module calls into the host via wasmimport, which then calls back to Go via wasmexport. Library (c-shared) mode is not implemented yet. - only supports wasip1, not js. - if the exported function unwinds stacks (goroutine switch, stack growth, etc.), it probably doesn't work. TODO: support stack unwinding, c-shared mode, js. For #65199. Change-Id: Id1777c2d44f7d51942c1caed3173c0a82f120cc4 Reviewed-on: https://go-review.googlesource.com/c/go/+/603055 Reviewed-by: Than McIntosh <thanm@golang.org> Reviewed-by: Randy Reddig <randy.reddig@fastly.com> Reviewed-by: David Chase <drchase@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-07-31cmd/compile: add comment for the context on mismatch import pathCuong Manh Le
Follow up suggestion in CL 596396. Updates #54542 Change-Id: I47bf66684bb8397dc1cfbc4479e2279e59a40cfb Reviewed-on: https://go-review.googlesource.com/c/go/+/596515 Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2024-07-24cmd/compile: restore not-in-heap check for map/channel typeCuong Manh Le
CL 388538 removed unused -G=0 node types. However, the code for checking not-in-heap types for map and channel type was also removed, which is likely not intentional. This CL restores the check, porting removed code to noder. Updates #54846 Change-Id: I2995836b90e36d2684197fefc9829fddfffe8585 Reviewed-on: https://go-review.googlesource.com/c/go/+/597535 Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Keith Randall <khr@google.com>
2024-07-23cmd/compile: prevent un-necessary wrapping in switch statementCuong Manh Le
Follow up discussion in CL 594575. The wrapping in "any" is only necessary if either casType or tagType is an interface, as "==" in this situation is implemented by upconverting to an interface anyway. Change-Id: I73da771d25685a23eec612ac696965c892db4764 Reviewed-on: https://go-review.googlesource.com/c/go/+/596555 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com>
2024-07-23runtime,internal: move runtime/internal/sys to internal/runtime/sysDavid Chase
Cleanup and friction reduction For #65355. Change-Id: Ia14c9dc584a529a35b97801dd3e95b9acc99a511 Reviewed-on: https://go-review.googlesource.com/c/go/+/600436 Reviewed-by: Keith Randall <khr@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@golang.org>
2024-07-22cmd/compile: retire "IsHiddenClosure" and "IsDeadcodeClosure"Cuong Manh Le
Since CL 522318, all closures are now hidden. Thus this CL removes all codes that worries about hidden vs non-hidden closures. Change-Id: I1ea124168c76cedbfc4053d2f150937a382aa330 Reviewed-on: https://go-review.googlesource.com/c/go/+/523275 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Than McIntosh <thanm@google.com>
2024-07-16cmd/compile: fix recursive generic interface instantiationCuong Manh Le
When shapifying recursive instantiated types, the compiler ends up leaving the type as-is if it already has been a shape type. However, if both of type arguments are interfaces, and one of them is a recursive one, it ends up being shaped as-is, while the other is shaped to its underlying, causing mismatch in function signature. Fixing this by shapifying an interface type as-is, if it is fully instantiated and already been a shape type. Fixes #65362 Fixes #66663 Change-Id: I839d266e0443b41238b1b7362aca09adc0177362 Reviewed-on: https://go-review.googlesource.com/c/go/+/559656 Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Keith Randall <khr@golang.org>
2024-07-03cmd/compile: fix mis-compilation when switching over channelsCuong Manh Le
CL 418101 changes Unified IR writer to force mixed tag/case to have common type, emitting the implicit conversion if any of the case values are not assignable to the tag value's type. However, the Go spec definition of equality is non-transitive for channels stored in interfaces, causing incorrect behavior with channel values comparison. To fix it, don't emit the implicit conversions if tag type is channel. Fixes #67190 Change-Id: I9a29d9ce3c7978f0689e9502ba6f15660c763d16 Reviewed-on: https://go-review.googlesource.com/c/go/+/594575 Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Keith Randall <khr@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Than McIntosh <thanm@google.com>
2024-07-03cmd/compile: emit error message on mismatch import pathCuong Manh Le
Fixes #54542 Change-Id: I16cfb84fc54892923106d0a6f0b3ba810886d077 Reviewed-on: https://go-review.googlesource.com/c/go/+/596396 Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Than McIntosh <thanm@google.com>
2024-06-20cmd/compile: support generic alias typeCuong Manh Le
Type parameters on aliases are now allowed after #46477 accepted. Updates #46477 Fixes #68054 Change-Id: Ic2e3b6f960a898163f47666e3a6bfe43b8cc22e2 Reviewed-on: https://go-review.googlesource.com/c/go/+/593715 Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Robert Griesemer <gri@google.com>
2024-06-12cmd/compile/internal: add a PGO devirt post-lookup cleanup hookThan McIntosh
The PGO-based devirtualization helper pgoir.addIndirectEdges makes a series of calls into the unified IR reader to import functions that would not normally be imported but may be the target of a hot indirect call from the current package. This importing primarily targets at non-generic functions and methods, but as part of the process we can encounter types that have methods (including generic methods) whose bodies need to be read in. When the reader encounters an inlinable func of this sort, it may (depending on the context) decide not to read the body right away, but instead adds the func to a list ("todoBodies") to be read in later on in a more convenient context. In the bug in question, a hot method lookup takes place in pgoir.addIndirectEdges, and as part of the import process we wind up with a type T with method M that is in this partially created state, and in addition T gets added to the unified IR's list of types that may need method wrappers. During wrapper generation we create a new wrapper "(*T).M" whose body has a call to "T.M", then farther on down the pike during escape analysis we try to analyze the two functions; this causes a crash due to "T.M" being in partially constructed state. As a fix, add a new "PostLookupCleanup" hook (in the unified IR reader) that pgoir.addIndirectEdges can invoke that takes care of reading in the bodies of any functions that have been added to the "todoBodies" list. [Note: creating a test case for this problem is proving to be very tricky; a new test will be added in a subsequent patch]. Fixes #67746. Change-Id: Ibc47ee79e08a55421728d35341df80a865231cff Reviewed-on: https://go-review.googlesource.com/c/go/+/591075 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Michael Pratt <mpratt@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-05-23runtime: move exit hooks into internal/runtime/exithookRuss Cox
This removes a //go:linkname usage in the coverage implementation. For #67401. Change-Id: I0602172c7e372a84465160dbf46d9fa371582fff Reviewed-on: https://go-review.googlesource.com/c/go/+/586259 Auto-Submit: Russ Cox <rsc@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2024-05-21cmd/compile: for rangefunc, add checks and tests, fix panic interactionsDavid Chase
Modify rangefunc #next protocol to make it more robust Extra-terrible nests of rangefunc iterators caused the prior implementation to misbehave non-locally (in outer loops). Add more rangefunc exit flag tests, parallel and tricky This tests the assertion that a rangefunc iterator running in parallel can trigger the race detector if any of the parallel goroutines attempts an early exit. It also verifies that if everything else is carefully written, that it does NOT trigger the race detector if all the parts run time completion. Another test tries to rerun a yield function within a loop, so that any per-line shared checking would be fooled. Added all the use-of-body/yield-function checking. These checks handle pathological cases that would cause rangefunc for loops to behave in surprising ways (compared to "regular" for loops). For example, a rangefunc iterator might defer-recover a panic thrown in the syntactic body of a loop; this notices the fault and panics with an explanation Modified closure naming to ID rangefunc bodies Add a "-range<N>" suffix to the name of any closure generated for a rangefunc loop body, as provided in Alessandro Arzilli's CL (which is merged into this one). Fix return values for panicky range functions This removes the delayed implementation of "return x" by ensuring that return values (in rangefunc-return-containing functions) always have names and translating the "return x" into "#rv1 = x" where #rv1 is the synthesized name of the first result. Updates #61405. Change-Id: I933299ecce04ceabcf1c0c2de8e610b2ecd1cfd8 Reviewed-on: https://go-review.googlesource.com/c/go/+/584596 Reviewed-by: Matthew Dempsky <mdempsky@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Tim King <taking@google.com>
2024-05-20cmd/compile/internal/syntax: implement Pos.FileBase method (cleanup)Robert Griesemer
Factor out file base computation into a method. Change-Id: Ia6de100459b6df2919f2320872890320aa88866d Reviewed-on: https://go-review.googlesource.com/c/go/+/586156 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Griesemer <gri@google.com> Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
2024-05-20cmd/compile: initialize posBaseMap correctlyRobert Griesemer
The posBaseMap is used to identify a file's syntax tree node given a source position. The position is mapped to the file base which is then used to look up the file node in posBaseMap. When posBaseMap is initialized, the file position base is not the file base if there's a line directive before the package clause. This can happen in cgo-generated files, for instance due to an import "C" declaration. If the wrong file position base is used during initialization, looking up a file given a position will not find the file. If a version error occurs and the corresponding file is not found, the old code panicked with a null pointer exception. Make sure to consistently initialize the posBaseMap by factoring out the code computing the file base from a given position. While at it, check for a nil file pointer. This should not happen anymore, but don't crash if it happens (at the cost of a slightly less informative error message). Fixes #67141. Change-Id: I4a6af88699c32ad01fffce124b06bb7f9e06f43d Reviewed-on: https://go-review.googlesource.com/c/go/+/586238 Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Robert Griesemer <gri@google.com>
2024-05-15cmd/compile: disallow linkname referring to instantiationsCherry Mui
Linknaming an instantiated generic symbol isn't particularly useful: it doesn't guarantee the instantiation exists, and the instantiated symbol name may be subject to change. Checked with a large code corpus, currently there is no occurrance of linkname to an instantiated generic symbol (or symbol with a bracket in its name). This also suggests that it is not very useful. Linkname is already an unsafe mechanism. We don't need to allow it to do more unsafe things without justification. Change-Id: Ifaa20c98166b28a9d7dc3290c013c2b5bb7682e7 Reviewed-on: https://go-review.googlesource.com/c/go/+/585458 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Than McIntosh <thanm@google.com>
2024-05-15cmd/compile/internal/noder: enable type aliases in type checkerMatthew Dempsky
This CL fixes an initialization loop during IR construction, that stems from IR lacking first-class support for aliases. As a workaround, we avoid publishing alias declarations until the RHS type expression has been constructed. Thanks to gri@ for investigating while I was out. Fixes #66873. Change-Id: I11e0d96ea6c357c295da47f44b6ec408edef89b7 Reviewed-on: https://go-review.googlesource.com/c/go/+/585399 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com> Auto-Submit: Matthew Dempsky <mdempsky@google.com>
2024-05-10cmd/compile, cmd/link: disallow linkname of some newly added internal functionsCherry Mui
Go API is defined through exported symbols. When a package is imported, the compiler ensures that only exported symbols can be accessed, and the go command ensures that internal packages cannot be imported. This ensures API integrity. But there is a hole: using linkname, one can access internal or non-exported symbols. Linkname is a mechanism to give access of a symbol to a package without adding it to the public API. It is intended for coupled packages to share some implementation details, or to break circular dependencies, and both "push" (definition) and "pull" (reference) sides are controlled, so they can be updated in sync. Nevertheless, it is abused as a mechanism to reach into internal details of other packages uncontrolled by the user, notably the runtime. As the other package evolves, the code often breaks, because the linknamed symbol may no longer exist, or change its signature or semantics. This CL adds a mechanism to enforce the integrity of linknames. Generally, "push" linkname is allowed, as the package defining the symbol explicitly opt in for access outside of the package. "Pull" linkname is checked and only allowed in some circumstances. Given that there are existing code that use "pull"-only linkname to access other package's internals, disallowing it completely is too much a change at this point in the release cycle. For a start, implement a hard-coded blocklist, which contains some newly added internal functions that, if used inappropriately, may break memory safety or runtime integrity. All blocked symbols are newly added in Go 1.23. So existing code that builds with Go 1.22 will continue to build. For the implementation, when compiling a package, we mark linknamed symbols in the current package with an attribute. At link time, marked linknamed symbols are checked against the blocklist. Care is taken so it distinguishes a linkname reference in the current package vs. a reference of a linkname from another package and propagated to the current package (e.g. through inlining or instantiation). Symbol references in assembly code are similar to linknames, and are treated similarly. Change-Id: I8067efe29c122740cd4f1effd2dec2d839147d5d Reviewed-on: https://go-review.googlesource.com/c/go/+/584598 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Than McIntosh <thanm@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
2024-05-07go/types, types: represent any using AliasRob Findley
When GODEBUG=gotypesalias=1 is set, use an actual Alias type to represent any, rather than a legacy alias representation. This makes any consistent with other interface aliases, and will eventually make obsolete the various workarounds for formatting any as 'any' rather than 'interface{}'. Since any is a global in the Universe scope, we must hijack Scope.Lookup to select the correct representation. Of course, this also means that we can't support type checking concurrently while mutating gotypesalias (or, in the case of types2, Config.EnableAlias). Some care is taken to ensure that the type checker panics in the event of this type of misuse. For now, we must still support the legacy representation of any, and the existing workarounds that look for a distinguished any pointer. This is done by ensuring that both representations have the same underlying pointer, and by updating workarounds to consider Underlying. Fixes golang/go#66921 Change-Id: I81db7e8e15317b7a6ed3b406545db15a2fc42f57 Reviewed-on: https://go-review.googlesource.com/c/go/+/580355 Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-04-24cmd/compile: bail PGO method lookup on interface typesMichael Pratt
Interface types don't have concrete method implementations, so it does not make sense to attempt a lookup. An interface method would not normally appear in a PGO profile as it has no symbol in the final binary. However it can appear if the method was concrete when the profile was collected and it has since been refactored to an interface method in the code being compiled. The guards here (OTYPE, !Alias, !IsInterface) now match noder.linker.relocObj, which does a similar iteration of all methods. Fixes #67016. Change-Id: I858c58929c890ac0b2019fbd7c99f683ab63f8bb Reviewed-on: https://go-review.googlesource.com/c/go/+/581436 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2024-04-18go/types, types2: use types2.Config flag to control Alias node creationRobert Griesemer
Move Checker.enableAlias to Config.EnableAlias (for types2) and Config._EnableAlias (for go/types), and adjust all uses. Use Config.EnableAlias to control Alias creation for types2 and with that remove dependencies on the gotypesalias GODEBUG setting and problems during bootstrap. The only client is the compiler and there we simply use the desired configuration; it is undesirable for the compiler to be dependent on gotypesalias. Use the gotypesalias GODEBUG setting to control Config._EnableAlias for go/types (similar to before). Adjust some related code. We plan to remove gotypesalias eventually which will remove some of the new discrepancies between types2 and go/types again. Fixes #66874. Change-Id: Id7cc4805e7ea0697e0d023c7f510867e59a24871 Reviewed-on: https://go-review.googlesource.com/c/go/+/579935 Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com> Auto-Submit: Robert Griesemer <gri@google.com>