Commit graph

17075 commits

Author SHA1 Message Date
Andrew Kelley
80a9b8f326 compiler: delete powerpc backend stub
nobody is currently working on this
2025-07-02 14:35:13 -07:00
Alex Rønne Petersen
edf785db0f
Merge pull request #24302 from alexrp/riscv
Native RISC-V bootstrap and test fixes
2025-07-02 04:15:10 +02:00
Ivan Stepanov
ee6d19480d spirv: fix signed overflow detection for safe subtraction
The overflow check for safe signed subtraction was using the formula (rhs < 0) == (lhs > result). This logic is flawed and incorrectly reports an overflow when the right-hand side is zero.
For the expression 42 - 0, this check evaluated to (0 < 0) == (42 > 42), which is false == false, resulting in true. This caused the generated SPIR-V to incorrectly branch to an OpUnreachable instruction, preventing the result from being stored.

Fixes #24281.
2025-07-01 19:26:21 +02:00
Alex Rønne Petersen
07114e6bc6
llvm: Disable the machine outliner pass on RISC-V 2025-07-01 18:16:40 +02:00
Alex Rønne Petersen
aa7b32d781
llvm: Fix alignment of by-ref ptr_elem_val and slice_elem_val 2025-07-01 18:16:36 +02:00
Andrew Kelley
d9742acc23 Sema: detect one-possible-value types after function calls
produces better Air for backends
2025-06-29 17:53:52 -07:00
Andrew Kelley
7999374b21 Sema: correct OPV for optional empty error set
prevents crashes in backends; improves codegen; provides more
comptime-ness.
2025-06-29 17:20:23 -07:00
Alex Rønne Petersen
e837765956
zig rc: Fix include directory detection when cross-compiling from certain host archs (#24288)
* resinator: Only preprocess when the input is an .rc file

* resinator: Fix include directory detection when cross-compiling from certain host archs

Previously, resinator would use the host arch as the target arch when looking for windows-gnu include directories. However, Zig only thinks it can provide a libc for targets specified in the `std.zig.target.available_libcs` array, which only includes a few for windows-gnu. Therefore, when cross-compiling from a host architecture that doesn't have a windows-gnu target in the available_libcs list, resinator would fail to detect the MinGW include directories.

Now, the custom option `/:target` is passed to `zig rc` which is intended for the COFF object file target, but can be re-used for the include directory target as well. For the include directory target, resinator will convert the MachineType to the relevant arch, or fail if there is no  equivalent arch/no support for detecting the includes for the MachineType (currently 64-bit Itanium and EBC).

Fixes the `windows_resources` standalone test failing when the host is, for example, `riscv64-linux`.
2025-06-29 09:59:38 +02:00
Ryan Liptak
d3363b7a61 Replace unreachable with error return for non-standard Windows targets
This new error mirrors the error returned from the Elf struct (error.UnsupportedElfArchitecture):

0adcfd60f4/src/link/Lld.zig (L112)

Before:

```
$ zig build-exe -target riscv64-windows-gnu main.zig
thread 543087 panic: reached unreachable code
```

After:

```
$ zig build-exe -target riscv64-windows-gnu main.zig
error: unable to create compilation: UnsupportedCoffArchitecture
```

Closes #24287
2025-06-29 08:32:19 +02:00
Ryan Liptak
09f4e2d9d1 resinator: Fix include directory detection when cross-compiling from certain host archs
Previously, resinator would use the host arch as the target arch when looking for windows-gnu include directories. However, Zig only thinks it can provide a libc for targets specified in the `std.zig.target.available_libcs` array, which only includes a few for windows-gnu. Therefore, when cross-compiling from a host architecture that doesn't have a windows-gnu target in the available_libcs list, resinator would fail to detect the MinGW include directories.

Now, the custom option `/:target` is passed to `zig rc` which is intended for the COFF object file target, but can be re-used for the include directory target as well. For the include directory target, resinator will convert the MachineType to the relevant arch, or fail if there is no  equivalent arch/no support for detecting the includes for the MachineType (currently 64-bit Itanium and EBC).

Fixes the `windows_resources` standalone test failing when the host is, for example, `riscv64-linux`.
2025-06-28 19:52:18 -07:00
Ali Cheraghi
1df79ab895 remove spirv cpu arch 2025-06-23 06:03:03 +02:00
Jacob Young
c71bb0f2b6 x86_64: fix pair live-out tracking
Closes #24226
2025-06-23 05:58:14 +02:00
David Rubin
3034d1e377 minor grammatical mistake 2025-06-22 20:00:17 +02:00
Jacob Young
1f98c98fff x86_64: increase passing test coverage on windows
Now that codegen has no references to linker state this is much easier.

Closes #24153
2025-06-19 18:41:12 -04:00
Jacob Young
ed37a1a33c coff: add hack to build a compiler-rt dynamic library
This is not meant to be a long-term solution, but it's the easiest thing
to get working quickly at the moment. The main intention of this hack is
to allow more tests to be enabled. By the time the coff linker is far
enough along to be enabled by default, this will no longer be required.
2025-06-19 18:41:12 -04:00
Jacob Young
e92b129063 Compilation: fix use after free
Closes #23967
2025-06-19 18:41:11 -04:00
Jacob Young
917640810e Target: pass and use locals by pointer instead of by value
This struct is larger than 256 bytes and code that copies it
consistently shows up in profiles of the compiler.
2025-06-19 11:45:06 -04:00
mlugg
8aab222ffb Compilation: add missing link file options to cache manifest
Also add a standalone test which covers the `-fentry` case. It does this
by performing two reproducible compilations which are identical other
than having different entry points, and checking whether the emitted
binaries are identical (they should *not* be).

Resolves: #23869
2025-06-17 15:33:50 +01:00
Matthew Lugg
561fdd0ed3
Merge pull request #24188 from mlugg/intfromfloat-safety
Absorb std.math.big.rational logic into std.math.big.int; fix `@intFromFloat` safety check
2025-06-17 11:02:03 +01:00
Ali Cheraghi
872f68c9cb
rename spirv backend name
`stage2_spirv64` -> `stage2_spirv`
2025-06-16 13:22:19 +03:30
Ali Cheraghi
c126d6554f
build: add spirv to dev env 2025-06-16 01:09:07 +03:30
mlugg
d90068db5a Sema: tiny refactor
There will be more call sites to `preparePanicId` as we transition away
from safety checks in Sema towards safety checked instructions; it's
silly for them to all have this clunky usage.
2025-06-15 14:15:18 -04:00
mlugg
6ffa285fc3 compiler: fix @intFromFloat safety check
This safety check was completely broken; it triggered unchecked illegal
behavior *in order to implement the safety check*. You definitely can't
do that! Instead, we must explicitly check the boundaries. This is a
tiny bit fiddly, because we need to make sure we do floating-point
rounding in the correct direction, and also handle the fact that the
operation truncates so the boundary works differently for min vs max.

Instead of implementing this safety check in Sema, there are now
dedicated AIR instructions for safety-checked intfromfloat (two
instructions; which one is used depends on the float mode). Currently,
no backend directly implements them; instead, a `Legalize.Feature` is
added which expands the safety check, and this feature is enabled for
all backends we currently test, including the LLVM backend.

The `u0` case is still handled in Sema, because Sema needs to check for
that anyway due to the comptime-known result. The old safety check here
was also completely broken and has therefore been rewritten. In that
case, we just check for 'abs(input) < 1.0'.

I've added a bunch of test coverage for the boundary cases of
`@intFromFloat`, both for successes (in `test/behavior/cast.zig`) and
failures (in `test/cases/safety/`).

Resolves: #24161
2025-06-15 14:15:18 -04:00
Jacob Young
6b41beb370 big.int: implement float conversions
These conversion routines accept a `round` argument to control how the
result is rounded and return whether the result is exact. Most callers
wanted this functionality and had hacks around it being missing.

Also delete `std.math.big.rational` because it was only being used for
float conversion, and using rationals for that is a lot more complex
than necessary. It also required an allocator, whereas the new integer
routines only need to be passed enough memory to store the result.
2025-06-15 14:15:18 -04:00
Jacob Young
6e72026e3b Legalize: make the feature set comptime-known in zig1
This allows legalizations to be added that aren't used by zig1 without
affecting the size of zig1.
2025-06-15 11:42:03 -04:00
Matthew Lugg
095c956c5c
Merge pull request #24171 from mlugg/atomic-order-derp
compiler: fix races in link queue
2025-06-14 02:10:20 +01:00
Loris Cro
180e8442af
zig init: simplify templating logic (#24170)
and also rename `advancedPrint` to `bufferedPrint` in the zig init templates

These are left overs from my previous changes to zig init.

The new templating system removes LITNAME because the new restrictions on package names make it redundant with NAME, and the use of underscores for marking templated identifiers lets us template variable names while still keeping zig fmt happy.
2025-06-13 22:31:29 +00:00
mlugg
55b7187429
link: fix obvious race condition
Did you know that allocators reuse addresses? If not, then don't feel
bad, because apparently I don't either! This dumb mistake was probably
responsible for the CI failures on `master` yesterday.
2025-06-13 22:05:03 +01:00
mlugg
121d620443
compiler: fix atomic orderings
I messed up atomic orderings on this variable because they changed in a
local refactor at some point. We need to always release on the store and
acquire on the loads so that a linker thread observing `.ready` sees the
stored MIR.
2025-06-13 19:05:44 +01:00
Andrew Kelley
dd75e7bcb1 Sema: add missing error and test for bool not on vector of ints 2025-06-13 05:59:25 -04:00
Andrew Kelley
dcdb4422b8
Merge pull request #24124 from mlugg/better-backend-pipeline-2
compiler: threaded codegen (and more goodies)
2025-06-12 20:46:36 -04:00
Daniel Kongsgaard
5e3c0b7af7
Allow more operators on bool vectors (#24131)
* Sema: allow binary operations and boolean not on vectors of bool

* langref: Clarify use of operators on vectors (`and` and `or` not allowed)

closes #24093
2025-06-12 15:16:23 -07:00
mlugg
43d01ff69f
x86_64.Lower: replace slow stringToEnum call
Looking at a compilation of 'test/behavior/x86_64/unary.zig' in
callgrind showed that a full 30% of the compiler runtime was spent in
this `stringToEnum` call, so optimizing it was low-hanging fruit.

We tried replacing it with nested `switch` statements using
`inline else`, but that generated too much code; it didn't emit huge
binaries or anything, but LLVM used a *ridiculous* amount of memory
compiling it in some cases. The core problem here is that only a small
subset of the cases are actually used (the rest fell through to an
"error" path), but that subset is computed at comptime, so we must rely
on the optimizer to eliminate the thousands of redundant cases. This
would be solved by #21507.

Instead, we pre-compute a lookup table at comptime. This table is pretty
big (I guess a couple hundred k?), but only the "valid" subset of
entries will be accessed in practice (unless a bug in the backend is
hit), so it's not too awful on the cache; and it performs much better
than the old `std.meta.stringToEnum` call.
2025-06-12 18:40:01 +01:00
mlugg
71baa5e769
compiler: improve progress output
Update the estimated total items for the codegen and link progress nodes
earlier. Rather than waiting for the main thread to dispatch the tasks,
we can add the item to the estimated total as soon as we queue the main
task. The only difference is we need to complete it even in error cases.
2025-06-12 17:51:31 +01:00
mlugg
5bb5aaf932
compiler: don't queue too much AIR/MIR
Without this cap, unlucky scheduling and/or details of what pipeline
stages perform best on the host machine could cause many gigabytes of
MIR to be stuck in the queue. At a certain point, pause the main thread
until some of the functions in flight have been processed.
2025-06-12 17:51:31 +01:00
mlugg
f9a670d46d
Compilation: prevent zig1 depending on fd_readdir
This isn't really coherent to model as a `Feature`; this makes sense
because of zig1's specific environment. As such, I opted to check
`dev.env` directly.
2025-06-12 17:51:31 +01:00
mlugg
d7afd797cc
Zcu: handle unreferenced test_functions correctly
Previously, `PerThread.populateTestFunctions` was analyzing the
`test_functions` declaration if it hadn't already been analyzed, so that
it could then populate it. However, the logic for doing this wasn't
actually correct, because it didn't trigger the necessary type
resolution. I could have tried to fix this, but there's actually a
simpler solution! If the `test_functions` declaration isn't referenced
or has a compile error, then we simply don't need to update it; either
it's unreferenced so its value doesn't matter, or we're going to get a
compile error anyway. Either way, we can just give up early. This avoids
doing semantic analysis after `performAllTheWork` finishes.

Also, get rid of the "Code Generation" progress node while updating the
test decl: this is a linking task.
2025-06-12 17:51:30 +01:00
mlugg
ff89a98c50
link.Queue: release safety lock before releasing mutex after stopping 2025-06-12 17:51:30 +01:00
mlugg
1b27369acb
cli: correctly error for missing output directories 2025-06-12 17:51:30 +01:00
mlugg
7f2f107a1e
Zcu: SPIR-V also doesn't generate MIR (yet) 2025-06-12 17:51:30 +01:00
mlugg
22e961070d
link: fix goff and xcoff flush 2025-06-12 17:51:30 +01:00
Jacob Young
afa07f723f
x86_64: implement coff relocations 2025-06-12 17:51:30 +01:00
Jacob Young
746137034e
Sema: fix union layout logic to match struct layout logic 2025-06-12 17:51:30 +01:00
Jacob Young
d312dfc1f2
codegen: make threadlocal logic consistent 2025-06-12 17:51:29 +01:00
Jacob Young
56119699bf
x86_64: fix dbg_var_ptr types in debug info 2025-06-12 17:51:29 +01:00
mlugg
89a6c732e5
Zcu: fix deleteExport crash with LLVM backend 2025-06-12 17:51:29 +01:00
mlugg
e28b699cbf
libs: fix caching behavior
glibc, freebsd, and netbsd all do caching manually, because of the fact
that they emit multiple files which they want to cache as a block.
Therefore, the individual sub-compilation on a cache miss should be
using `CacheMode.none` so that we can specify the output paths for each
sub-compilation as being in the shared output directory.
2025-06-12 17:51:29 +01:00
mlugg
ac745edbbd
compiler: estimate totals for "Code Generation" and "Linking" progress nodes 2025-06-12 13:55:41 +01:00
mlugg
db5d85b8c8
compiler: improve progress output
* "Flush" nodes ("LLVM Emit Object", "ELF Flush") appear under "Linking"

* "Code Generation" disappears when all analysis and codegen is done

* We only show one node under "Semantic Analysis" to accurately convey
  that analysis isn't happening in parallel, but rather that we're
  pausing one task to do another
2025-06-12 13:55:41 +01:00
Jacob Young
ba53b14028
x86_64: remove linker references from codegen 2025-06-12 13:55:41 +01:00