16860 Commits

Author SHA1 Message Date
Alex Rønne Petersen
0cbff2ff7f mingw: Remove libscrnsav(e,w) support.
This defines a WinMain() function that can be potentially problematic when it
isn't wanted. If we add back support for this library in the future, it should
be built separately from mingw32.lib and on demand.
2025-05-30 01:03:47 +02:00
Alex Rønne Petersen
7c7627b18a
compiler: Use new merged header paths for glibc and NetBSD libc. 2025-05-30 00:15:37 +02:00
Jacob Young
c04be630d9 Legalize: introduce a new pass before liveness
Each target can opt into different sets of legalize features.
By performing these transformations before liveness, instructions
that become unreferenced will have up-to-date liveness information.
2025-05-29 03:57:48 -04:00
Jacob Young
9f9e7e398f x86_64: implement strict float @reduce(.Max) 2025-05-28 15:10:22 -04:00
Jacob Young
f5a1aaf2fc x86_64: implement strict float @reduce(.Min) 2025-05-28 15:10:22 -04:00
Jacob Young
d29ea656b0 x86_64: implement optimized float @reduce(.Max) 2025-05-28 15:10:22 -04:00
Jacob Young
114a4eaf5c x86_64: implement optimized float @reduce(.Min) 2025-05-28 15:10:22 -04:00
Jacob Young
8bacf3e757 x86_64: implement integer @reduce(.Max) 2025-05-28 15:10:22 -04:00
Jacob Young
3fd3358f37 x86_64: implement integer @reduce(.Min) 2025-05-28 15:10:22 -04:00
Jacob Young
7d727ed7df x86_64: implement strict float @reduce(.Mul) 2025-05-28 15:10:22 -04:00
Jacob Young
d1785cf61c x86_64: implement strict float @reduce(.Add) 2025-05-28 15:10:22 -04:00
Jacob Young
a4a1ebdeed x86_64: implement optimized float @reduce(.Mul) 2025-05-28 15:10:22 -04:00
Jacob Young
612f5784cf x86_64: implement optimized float @reduce(.Add) 2025-05-28 15:10:22 -04:00
mlugg
7c31f9d4ff Sema: don't emit AIR reduce for single-element vectors
This is equivalent to `array_elem_val`, and doing that conversion in
Sema (seems reasonable since it's just a simple branch) is much easier
for the self-hosted x86_64 backend then actually handling this case.
2025-05-28 15:10:22 -04:00
Jacob Young
7bfdb7f26d x86_64: implement integer @reduce(.Mul) 2025-05-28 15:10:22 -04:00
Jacob Young
1f6f8b0ffe x86_64: implement integer @reduce(.Add) 2025-05-28 15:10:22 -04:00
Jacob Young
d69f4c48fc x86_64: rewrite bitwise @reduce 2025-05-28 15:10:22 -04:00
mlugg
92c63126e8 compiler: tlv pointers are not comptime-known
Pointers to thread-local variables do not have their addresses known
until runtime, so it is nonsensical for them to be comptime-known. There
was logic in the compiler which was essentially attempting to treat them
as not being comptime-known despite the pointer being an interned value.
This was a bit of a mess, the check was frequent enough to actually show
up in compiler profiles, and it was very awkward for backends to deal
with, because they had to grapple with the fact that a "constant" they
were lowering might actually require runtime operations.

So, instead, do not consider these pointers to be comptime-known in
*any* way. Never intern such a pointer; instead, when the address of a
threadlocal is taken, emit an AIR instruction which computes the pointer
at runtime. This avoids lots of special handling for TLVs across
basically all codegen backends; of all somewhat-functional backends, the
only one which wasn't improved by this change was the LLVM backend,
because LLVM pretends this complexity around threadlocals doesn't exist.

This change simplifies Sema and codegen, avoids a potential source of
bugs, and potentially improves Sema performance very slightly by
avoiding a non-trivial check on a hot path.
2025-05-27 19:23:11 +01:00
David Rubin
3ed9155f10 Sema: simplify comptime @intFromPtr logic 2025-05-27 02:40:58 +01:00
Matthew Lugg
ef35c3d5fe
Merge pull request #23986 from mlugg/incremental-stuff
incremental: bugfix (and a debugging feature that helped me do that bugfix)
2025-05-25 18:02:16 +01:00
Robin Voetter
dc6ffc28b5
Merge pull request #23815 from alichraghi/master
spirv: unroll all vector operations
2025-05-25 12:36:53 +02:00
mlugg
3d8e760552
Zcu: fix nav_ty dependency on nav_val
In the case where a declaration has no type annotation, the interaction
between resolution of `nav_ty` and `nav_val` is a little fiddly because
of the fact that resolving `nav_val` actually implicitly resolves the
type as well. This means `nav_ty` never gets an opporunity to mark its
dependency on the `nav_val`. So, `ensureNavValUpToDate` needs to be the
one to do it. It can't do it too early, though; otherwise, our marking
of dependees as out-of-date/up-to-date will go wrong.

Resolves: #23959
2025-05-25 05:50:26 +01:00
mlugg
aeed5f9ebd
compiler: introduce incremental debug server
In a compiler built with debug extensions, pass `--debug-incremental` to
spawn the "incremental debug server". This is a TCP server exposing a
REPL which allows querying a bunch of compiler state, some of which is
stored only when that flag is passed. Eventually, this will probably
move into `std.zig.Server`/`std.zig.Client`, but this is easier to work
with right now. The easiest way to interact with the server is `telnet`.
2025-05-25 04:43:43 +01:00
mlugg
a5861fcddd Module: name builtin module root directory correctly
37a9a4e accidentally turned paths `b/[hash]/` into `b[hash]/` in the
global cache. This doesn't technically break anything, but it pollutes
the global cache directory. Sorry about that one!
2025-05-21 15:20:23 +01:00
mlugg
3416452d56 compiler: fix ZIR hash not including compiler version
This was an unintentional regression in 23c8175 which meant that
backwards-incompatible ZIR changes would have caused compiler crashes if
old caches were present.
2025-05-21 11:11:28 +01:00
Ali Cheraghi
8fa54eb798 spirv: error when execution mode is set more than once 2025-05-21 13:01:21 +03:30
Ali Cheraghi
9209f4b16a spirv: recognize builtin extern vars 2025-05-21 13:01:21 +03:30
Ali Cheraghi
dacd70fbe4 spirv: super basic composite int support 2025-05-21 13:01:20 +03:30
Ali Cheraghi
0901328f12 spirv: write error value in an storage buffer 2025-05-21 12:57:40 +03:30
Ali Cheraghi
fca5f3602d spirv: unroll all vector operations 2025-05-21 12:57:40 +03:30
Alex Rønne Petersen
7c9035f635 link.Elf: Don't require linking libc for dynamic linker path to take effect.
Closes #23813.
2025-05-21 06:08:50 +02:00
David
55848363fd
libc: implement common abs for various integer sizes (#23893)
* libc: implement common `abs` for various integer sizes

* libc: move imaxabs to inttypes.zig and don't use cInclude

* libc: delete `fabs` c implementations because already implemented in compiler_rt

* libc: export functions depending on the target libc

Previously all the functions that were exported were handled equally,
though some may exist and some not inside the same file. Moving the
checks inside the file allows handling different functions differently

* remove empty ifs in inttypes

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>

* remove empty ifs in stdlib

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>

* libc: use `@abs` for the absolute value calculation

---------

Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>
2025-05-21 00:57:38 +02:00
Alex Rønne Petersen
a63f7875f4 compiler: Fix build break due to #23836 being merged before #23913. 2025-05-20 13:22:11 +02:00
Alex Rønne Petersen
999777e73a compiler: Scaffold stage2_powerpc backend.
Nothing interesting here; literally just the bare minimum so I can work on this
on and off in a branch without worrying about merge conflicts in the non-backend
code.
2025-05-20 10:23:16 +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
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
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
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
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