33566 Commits

Author SHA1 Message Date
Alex Rønne Petersen
d000e53b29
Merge pull request #23923 from alexrp/compiler-rt-symbols
Use hidden visibility in compiler-rt and libzigc except when testing
2025-05-20 07:48:35 +02:00
Alex Rønne Petersen
b27c5fbbde
Merge pull request #23913 from alexrp/netbsd-libc
Support dynamically-linked NetBSD libc when cross-compiling
2025-05-20 07:46:08 +02:00
Matthew Lugg
23c817548b
Merge pull request #23836 from mlugg/incr-fixes
Incremental fixes, refactor `Zcu.File`
2025-05-20 03:25:19 +01:00
mlugg
f2077f57ae Sema: allow @ptrCast single-item pointer to slice
Also, rework this logic a little to make it simpler. The length of the
result slice is now computed in one place.
2025-05-19 19:26:12 +01:00
mlugg
ac8720f399 Zcu: fix memory leak
Bug introduced by d717c96, sorry!
2025-05-19 19:12:31 +01:00
tjog
ad1e09eab0 libfuzzer test: use proper cache dir logic 2025-05-19 04:03:16 +02:00
mlugg
07a5efd072 Sema: rewrite analyzeMinMax
I only wanted to fix a bug originally, but this logic was kind of a
rat's nest. But now... okay, it still *is*, but it's now a slightly more
navigable nest, with cute little signs occasionally, painted by adorable
rats desparately trying to follow the specification.

Hopefully #3806 comes along at some point to simplify this logic a
little.

Resolves: #23139
2025-05-19 00:27:01 +01:00
Alex Rønne Petersen
b0c10e2262
test: Enable x86-linux-musl with dynamic linkage in the module test matrix.
Building upstream musl for this target works now that we use hidden visibility
for compiler-rt symbols.
2025-05-19 00:22:22 +02:00
Alex Rønne Petersen
e882956ef6
std.leb128: Disable two tests on x86-linux-musl with dynamic linkage.
https://github.com/ziglang/zig/issues/23922
2025-05-19 00:22:19 +02:00
Alex Rønne Petersen
07c93cb103
compiler-rt: Add a comment explaining why we currently need weak linkage. 2025-05-19 00:22:03 +02:00
Alex Rønne Petersen
90fb2d9754
compiler-rt: Fix some exports to respect the common linkage and visibility. 2025-05-19 00:22:03 +02:00
Alex Rønne Petersen
092352ec63
compiler-rt, zigc: Use hidden visibility except when testing.
This prevents symbols from these libraries from polluting the dynamic symbol
tables of binaries built with Zig. The downside is that we no longer deduplicate
the symbols at run time due to weak linkage.

Closes #7935.
Closes #13303.
Closes #19342.
2025-05-19 00:22:03 +02:00
Alex Rønne Petersen
846571ce4e
build: Bump max_rss for test-behavior to 2488843878. 2025-05-19 00:22:03 +02:00
Andrew Kelley
b77e601342
Merge pull request #23834 from jacobly0/x86_64-rewrite
x86_64: finish rewriting scalar overflow and saturate operations
2025-05-18 14:36:33 -04:00
mlugg
37a9a4e0f1
compiler: refactor Zcu.File and path representation
This commit makes some big changes to how we track state for Zig source
files. In particular, it changes:

* How `File` tracks its path on-disk
* How AstGen discovers files
* How file-level errors are tracked
* How `builtin.zig` files and modules are created

The original motivation here was to address incremental compilation bugs
with the handling of files, such as #22696. To fix this, a few changes
are necessary.

Just like declarations may become unreferenced on an incremental update,
meaning we suppress analysis errors associated with them, it is also
possible for all imports of a file to be removed on an incremental
update, in which case file-level errors for that file should be
suppressed. As such, after AstGen, the compiler must traverse files
(starting from analysis roots) and discover the set of "live files" for
this update.

Additionally, the compiler's previous handling of retryable file errors
was not very good; the source location the error was reported as was
based only on the first discovered import of that file. This source
location also disappeared on future incremental updates. So, as a part
of the file traversal above, we also need to figure out the source
locations of imports which errors should be reported against.

Another observation I made is that the "file exists in multiple modules"
error was not implemented in a particularly good way (I get to say that
because I wrote it!). It was subject to races, where the order in which
different imports of a file were discovered affects both how errors are
printed, and which module the file is arbitrarily assigned, with the
latter in turn affecting which other files are considered for import.
The thing I realised here is that while the AstGen worker pool is
running, we cannot know for sure which module(s) a file is in; we could
always discover an import later which changes the answer.

So, here's how the AstGen workers have changed. We initially ensure that
`zcu.import_table` contains the root files for all modules in this Zcu,
even if we don't know any imports for them yet. Then, the AstGen
workers do not need to be aware of modules. Instead, they simply ignore
module imports, and only spin off more workers when they see a by-path
import.

During AstGen, we can't use module-root-relative paths, since we don't
know which modules files are in; but we don't want to unnecessarily use
absolute files either, because those are non-portable and can make
`error.NameTooLong` more likely. As such, I have introduced a new
abstraction, `Compilation.Path`. This type is a way of representing a
filesystem path which has a *canonical form*. The path is represented
relative to one of a few special directories: the lib directory, the
global cache directory, or the local cache directory. As a fallback, we
use absolute (or cwd-relative on WASI) paths. This is kind of similar to
`std.Build.Cache.Path` with a pre-defined list of possible
`std.Build.Cache.Directory`, but has stricter canonicalization rules
based on path resolution to make sure deduplicating files works
properly. A `Compilation.Path` can be trivially converted to a
`std.Build.Cache.Path` from a `Compilation`, but is smaller, has a
canonical form, and has a digest which will be consistent across
different compiler processes with the same lib and cache directories
(important when we serialize incremental compilation state in the
future). `Zcu.File` and `Zcu.EmbedFile` both contain a
`Compilation.Path`, which is used to access the file on-disk;
module-relative sub paths are used quite rarely (`EmbedFile` doesn't
even have one now for simplicity).

After the AstGen workers all complete, we know that any file which might
be imported is definitely in `import_table` and up-to-date. So, we
perform a single-threaded graph traversal; similar to what
`resolveReferences` plays for `AnalUnit`s, but for files instead. We
figure out which files are alive, and which module each file is in. If a
file turns out to be in multiple modules, we set a field on `Zcu` to
indicate this error. If a file is in a different module to a prior
update, we set a flag instructing `updateZirRefs` to invalidate all
dependencies on the file. This traversal also discovers "import errors";
these are errors associated with a specific `@import`. With Zig's
current design, there is only one possible error here: "import outside
of module root". This must be identified during this traversal instead
of during AstGen, because it depends on which module the file is in. I
tried also representing "module not found" errors in this same way, but
it turns out to be much more useful to report those in Sema, because of
use cases like optional dependencies where a module import is behind a
comptime-known build option.

For simplicity, `failed_files` now just maps to `?[]u8`, since the
source location is always the whole file. In fact, this allows removing
`LazySrcLoc.Offset.entire_file` completely, slightly simplifying some
error reporting logic. File-level errors are now directly built in the
`std.zig.ErrorBundle.Wip`. If the payload is not `null`, it is the
message for a retryable error (i.e. an error loading the source file),
and will be reported with a "file imported here" note pointing to the
import site discovered during the single-threaded file traversal.

The last piece of fallout here is how `Builtin` works. Rather than
constructing "builtin" modules when creating `Package.Module`s, they are
now constructed on-the-fly by `Zcu`. The map `Zcu.builtin_modules` maps
from digests to `*Package.Module`s. These digests are abstract hashes of
the `Builtin` value; i.e. all of the options which are placed into
"builtin.zig". During the file traversal, we populate `builtin_modules`
as needed, so that when we see this imports in Sema, we just grab the
relevant entry from this map. This eliminates a bunch of awkward state
tracking during construction of the module graph. It's also now clearer
exactly what options the builtin module has, since previously it
inherited some options arbitrarily from the first-created module with
that "builtin" module!

The user-visible effects of this commit are:
* retryable file errors are now consistently reported against the whole
  file, with a note pointing to a live import of that file
* some theoretical bugs where imports are wrongly considered distinct
  (when the import path moves out of the cwd and then back in) are fixed
* some consistency issues with how file-level errors are reported are
  fixed; these errors will now always be printed in the same order
  regardless of how the AstGen pass assigns file indices
* incremental updates do not print retryable file errors differently
  between updates or depending on file structure/contents
* incremental updates support files changing modules
* incremental updates support files becoming unreferenced

Resolves: #22696
2025-05-18 17:37:02 +01:00
mlugg
d32829e053
std.Build.Cache: change contract of addFilePostContents
This function was broken, because it took ownership of the buffer on
error *sometimes*, in a way which the caller could not tell. Rather than
trying to be clever, it's easier to just follow the same interface as
all other `addFilePost` methods, and not take ownership of the path.

This is a breaking change. The next commits will apply it to the
compiler, which is the only user of this function in the ziglang/zig
repository.
2025-05-18 17:10:04 +01:00
mlugg
a0792e743f
incr-check: normalize path separators in file names in errors 2025-05-18 17:10:04 +01:00
mlugg
ed7335ce57
incr-check: support basic modules
Allow specifying modules which the root module depends on. More complex
graphs cannot currently be specified.
2025-05-18 17:10:04 +01:00
mlugg
6d7c89cb40
build.zig: use correct module graph for compiler unit tests 2025-05-18 17:10:04 +01:00
mlugg
e26c326996
tests: remove incorrect import 2025-05-18 17:10:04 +01:00
Alex Rønne Petersen
74a3ae4927 start: Don't artificially limit some posixCallMainAndExit() logic to Linux.
This code applies to ~any POSIX OS where we don't link libc. For example, it'll
be useful for FreeBSD and NetBSD.

As part of this, move std.os.linux.pie to std.pie since there's really nothing
Linux-specific about what that file is doing.
2025-05-18 17:14:09 +02:00
Jacob Young
a4eabd3979 x86_64: implement vector_store_elem 2025-05-17 20:31:25 -04:00
Jacob Young
a3b0c242b0 x86_64: rewrite @splat 2025-05-17 18:00:17 -04:00
Jacob Young
58d2bd601e x86_64: rewrite scalar <<|
Closes #23035
2025-05-17 18:00:17 -04:00
Jacob Young
d3dfe61eaa x86_64: rewrite scalar *| 2025-05-17 18:00:17 -04:00
Alex Rønne Petersen
cd1eea0964
freebsd: Fix stub libraries containing versioned symbols that shouldn't be.
Closes #23911.
2025-05-17 20:13:02 +02:00
Alex Rønne Petersen
a97e417ab1
compiler: Support building NetBSD crt1.o/Scrt1.o and stub shared libraries.
Only works for NetBSD 10.1+. Note that we still default to targeting NetBSD 9.

Contributes to #2877.
2025-05-17 20:12:56 +02:00
Jacob Young
96e35b3652 x86_64: rewrite vector -| 2025-05-17 02:08:41 -04:00
Jacob Young
3529889cf3 x86_64: rewrite scalar -| 2025-05-17 02:08:41 -04:00
Jacob Young
4adb7eca6a debug: correctly detect missing entries in .eh_frame_hdr 2025-05-17 02:08:41 -04:00
Jacob Young
025611629f x86_64: implement @memmove 2025-05-17 02:08:41 -04:00
Jacob Young
6d68a494c8 x86_64: rewrite vector +| 2025-05-17 02:08:41 -04:00
Jacob Young
6dbf1c7682 x86_64: rewrite scalar +| 2025-05-17 02:08:41 -04:00
Jacob Young
932298679f x86_64: rewrite scalar @shlWithOverflow 2025-05-17 02:08:41 -04:00
Alex Rønne Petersen
c5e669ff76
compiler: Link libc by default when targeting NetBSD.
We don't yet have a direct syscall layer in std.os.netbsd.
2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
d29ba75c84
Compilation.Config: Default to dynamic linking with NetBSD libc. 2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
1a82cceb8c
std.Target: Factor arch/ABI into NetBSD minimum OS version selection.
Based on data in std.zig.target.
2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
a090ef007b
std.zig.target: Add NetBSD libc support. 2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
e20fb7071c
compiler: Define __NetBSD_Version__ when targeting NetBSD libc. 2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
c36d483ba0
libc: Add NetBSD libc abilists file.
Currently covers version 10.1.
2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
90911b39d5
update_netbsd_libc: Add tool for updating NetBSD libc startup code. 2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
1c342ca7c3
libc: Add NetBSD libc startup code from 10.1.
* sysident_assym.h was manually expanded.
    * The ELF_NOTE_MARCH_DESC and ELF_NOTE_MARCH_DESCSZ macros will be defined
      by the compiler.
* Legacy .init/.fini stuff was removed.
* GCJ nonsense was removed.
2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
35f8121194
std.zig.LibCDirs: Add NetBSD libc support. 2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
553cb3ed46
libc: Add NetBSD system and libc headers from 10.1.
sys/param.h was manually adjusted to not define __NetBSD_Version__ since it will
be defined by the compiler.
2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
4371e695ef
process_headers: Add NetBSD libc support. 2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
6028db7f29
std.Target: Add isNetBSDLibC() function. 2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
8a7d925d36
std.Target: Fix requiresLibC() for NetBSD. 2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
3d6f83b331
std.Target: Adjust DynamicLinker.standard() for NetBSD.
* powerpc64 does not appear to be a thing.
* riscv32/riscv64 have not had actual releases yet.
2025-05-17 04:41:27 +02:00
Alex Rønne Petersen
9af8e7c68e
std.c: Fill out some missing definitions for supported NetBSD libc targets.
This allows ubsan-rt to build.
2025-05-17 04:41:26 +02:00
Alex Rønne Petersen
d47cc5777d
test: Remove some nonsensical NetBSD targets from llvm_targets.
* mips64/mips64el on NetBSD are soft float; we have no support for this yet.
* powerpc64 does not appear to be a thing.
* riscv32/riscv64 have not seen official releases yet.
2025-05-17 04:41:26 +02:00