Commit graph

17075 commits

Author SHA1 Message Date
Andrew Kelley
d3f4e9d4ac Compilation pipeline: repeat failed prelink tasks
and remove faulty assertion. When a prelink task fails, the
completed_prelink_tasks counter will not decrement.

A future improvement will be needed to make the pipeline fully robust
and handle failed prelink tasks, followed by updates in which those
tasks succeed, and compilation proceeds like normal.

Currently if a prelink task fails, the Compilation will be left in a
state unrecoverable by an incremental update.
2025-01-24 12:22:00 -08:00
mlugg
83991efe10
compiler: yet more panic handler changes
* `std.builtin.Panic` -> `std.builtin.panic`, because it is a namespace.
* `root.Panic` -> `root.panic` for the same reason. There are type
  checks so that we still allow the legacy `pub fn panic` strategy in
  the 0.14.0 release.
* `std.debug.SimplePanic` -> `std.debug.simple_panic`, same reason.
* `std.debug.NoPanic` -> `std.debug.no_panic`, same reason.
* `std.debug.FormattedPanic` is now a function `std.debug.FullPanic`
  which takes as input a `panicFn` and returns a namespace with all the
  panic functions. This handles the incredibly common case of just
  wanting to override how the message is printed, whilst keeping nice
  formatted panics.
* Remove `std.builtin.panic.messages`; now, every safety panic has its
  own function. This reduces binary bloat, as calls to these functions
  no longer need to prepare any arguments (aside from the error return
  trace).
* Remove some legacy declarations, since a zig1.wasm update has
  happened. Most of these were related to the panic handler, but a quick
  grep for "zig1" brought up a couple more results too.

Also, add some missing type checks to Sema.

Resolves: #22584

formatted -> full
2025-01-24 19:29:51 +00:00
Alex Rønne Petersen
41185d297f
Package.Module: Make create() fall back on options.global.root_optimize_mode.
As is done for root_strip and root_error_tracing.
2025-01-23 23:22:38 +00:00
Alex Rønne Petersen
ef4d7f01a5
compiler: Fix computation of Compilation.Config.any_unwind_tables.
This moves the default value logic to Package.Module.create() instead and makes
it so that Compilation.Config.any_unwind_tables is computed similarly to
any_sanitize_thread, any_fuzz, etc. It turns out that for any_unwind_tables, we
only actually care if unwind tables are enabled at all, not at what level.
2025-01-23 23:22:38 +00:00
Alex Rønne Petersen
b46a40ff1d
compiler: Handle --no-eh-frame-hdr as a regular zig build-* flag too.
For some reason we accepted --eh-frame-hdr, but not --no-eh-frame-hdr, despite
accepting the latter as a -Wl linker flag.
2025-01-23 23:22:38 +00:00
Alex Rønne Petersen
d916954bee
Merge pull request #22098 from alexrp/wasm-generic-baseline
`std.Target`: Use `lime1` as wasm baseline model and `mvp` as generic model
2025-01-23 18:41:11 +01:00
Alex Rønne Petersen
68c6a88770
link.Wasm.Feature: Make fromCpuFeature() and toCpuFeature() less cute.
This is more verbose, but at least we now get a compile error instead of UB when
a new feature is added to std.Target.wasm.Feature but not to link.Wasm.Feature.
2025-01-22 21:18:15 +01:00
Alex Rønne Petersen
ea1502974d
wasm: Add a nontrapping_bulk_memory_len0 feature.
This will mainly be used when targeting our wasm2c implementation which has no
problem with zero-length bulk memory operations, as a non-standard extension.
2025-01-22 20:56:28 +01:00
Matthew Lugg
0e815c652d
Merge pull request #22572 from jacobly0/new-error-trace
compiler: include error trace in all functions, implement for x86_64 backend
2025-01-22 16:48:27 +00:00
Jacob Young
d4b6a53327 x86_64: implement error return traces 2025-01-22 03:44:13 -05:00
mlugg
1bce01de97 compiler: pass error return traces everywhere 2025-01-22 02:22:56 -05:00
mlugg
e864c38cc3
Sema: fix crash when inline loop condition is not comptime-known 2025-01-22 04:18:43 +00:00
mlugg
8470b6ea37
Zcu: fix switch prong source location resolution
Resolves: #22343
2025-01-22 04:11:02 +00:00
Alex Rønne Petersen
280ced66eb
std.Target: Define and use lime1 as the baseline CPU model for WebAssembly.
See: https://github.com/WebAssembly/tool-conventions/pull/235

This is not *quite* using the same features as the spec'd lime1 model because
LLVM 19 doesn't have the level of feature granularity that we need for that.
This will be fixed once we upgrade to LLVM 20.

Part of #21818.
2025-01-22 03:01:05 +01:00
Alex Rønne Petersen
e800ea0fdd
wasm: Add a check for zero length around uses of memory.copy/memory.fill.
Apparently the WebAssembly spec requires these instructions to trap if the
computed memory access could be out of bounds, even if the length is zero.
Really a rather bizarre design choice.
2025-01-22 03:01:02 +01:00
Fabio Arnold
97b97ae620
Package fetch: add executable detection for Mach-O file headers (#21555) 2025-01-21 11:48:19 -08:00
Andrew Kelley
b31a2c9555
Merge pull request #22541 from ziglang/pipeline
Compilation pipeline: spawn Jobs earlier that produce linker inputs
2025-01-21 14:27:05 -05:00
Jacob Young
d652dd0658 x86_64: rewrite @abs for scalar floats 2025-01-21 06:39:24 -05:00
Alex Rønne Petersen
f1f269c6ee libc: Remove a bunch of code for architectures we don't actually support.
Namely:

* alpha
* hppa
* ia64
* microblaze
* nios2
* or1k
* s390
* sh
2025-01-21 09:30:16 +01:00
Andrew Kelley
874e17fe60 embrace the future slightly less
Turns out that even modern Debian aarch64 glibc libc_nonshared.a has
references to _init, meaning that the previous commit caused a
regression when trying to build any -lc executable on that target.

This commit backs out the changes to LibCInstallation.

There is still a fork in the road coming up when the self-hosted ELF
linker becomes load bearing on that target.
2025-01-20 20:59:52 -08:00
Andrew Kelley
f5485a52bc reject crti.o/crtn.o, embrace the future
crti.o/crtn.o is a legacy strategy for calling constructor functions
upon object loading that has been superseded by the
init_array/fini_array mechanism.

Zig code depends on neither, since the language intentionally has no way
to initialize data at runtime, but alas the Zig linker still must
support this feature since popular languages depend on it.

Anyway, the way it works is that crti.o has the machine code prelude of
two functions called _init and _fini, each in their own section with the
respective name. crtn.o has the machine code instructions comprising the
exitlude for each function. In between, objects use the .init and .fini
link section to populate the function body.

This function is then expected to be called upon object initialization
and deinitialization.

This mechanism is depended on by libc, for example musl and glibc, but
only for older ISAs. By the time the libcs gained support for newer
ISAs, they had moved on to the init_array/fini_array mechanism instead.

For the Zig linker, we are trying to move the linker towards
order-independent objects which is incompatible with the legacy
crti/crtn mechanism.

Therefore, this commit drops support entirely for crti/crtn mechanism,
which is necessary since the other commits in this branch make it
nondeterministic in which order the libc objects and the other link
inputs are sent to the linker.

The linker is still expected to produce a deterministic output, however,
by ignoring object input order for the purposes of symbol resolution.
2025-01-20 20:59:52 -08:00
Andrew Kelley
2391c460b1 fix build failure when llvm not available 2025-01-20 20:59:52 -08:00
Andrew Kelley
77af309cb6 Compilation: take advantage of @splat 2025-01-20 20:59:52 -08:00
Andrew Kelley
28da530271 Compilation pipeline: linker input producing Job representation
Move all the remaining Jobs that produce linker inputs to be spawned
earlier in the pipeline and registered with link_task_wait_group.
2025-01-20 20:59:52 -08:00
Andrew Kelley
966169fa68 compilation pipeline: do glibc jobs earlier 2025-01-20 20:59:52 -08:00
Andrew Kelley
ce00e91aa5 Compilation pipeline: do musl jobs earlier
This means doing more work in parallel which is already good, but it's
also a correctnes fix because we need link_task_wait_group.wait() to
ensure that no more linker inputs will be generated.
2025-01-20 20:59:52 -08:00
mlugg
0ec6b2dd88 compiler: simplify generic functions, fix issues with inline calls
The original motivation here was to fix regressions caused by #22414.
However, while working on this, I ended up discussing a language
simplification with Andrew, which changes things a little from how they
worked before #22414.

The main user-facing change here is that any reference to a prior
function parameter, even if potentially comptime-known at the usage
site or even not analyzed, now makes a function generic. This applies
even if the parameter being referenced is not a `comptime` parameter,
since it could still be populated when performing an inline call. This
is a breaking language change.

The detection of this is done in AstGen; when evaluating a parameter
type or return type, we track whether it referenced any prior parameter,
and if so, we mark this type as being "generic" in ZIR. This will cause
Sema to not evaluate it until the time of instantiation or inline call.

A lovely consequence of this from an implementation perspective is that
it eliminates the need for most of the "generic poison" system. In
particular, `error.GenericPoison` is now completely unnecessary, because
we identify generic expressions earlier in the pipeline; this simplifies
the compiler and avoids redundant work. This also entirely eliminates
the concept of the "generic poison value". The only remnant of this
system is the "generic poison type" (`Type.generic_poison` and
`InternPool.Index.generic_poison_type`). This type is used in two
places:

* During semantic analysis, to represent an unknown result type.
* When storing generic function types, to represent a generic parameter/return type.

It's possible that these use cases should instead use `.none`, but I
leave that investigation to a future adventurer.

One last thing. Prior to #22414, inline calls were a little inefficient,
because they re-evaluated even non-generic parameter types whenever they
were called. Changing this behavior is what ultimately led to #22538.
Well, because the new logic will mark a type expression as generic if
there is any change its resolved type could differ in an inline call,
this redundant work is unnecessary! So, this is another way in which the
new design reduces redundant work and complexity.

Resolves: #22494
Resolves: #22532
Resolves: #22538
2025-01-21 02:41:42 +00:00
mlugg
8bcb578507 Sema: fix is_non_null_ptr handling for runtime-known pointers
We can still often determine a comptime result based on the type, even
if the pointer is runtime-known.

Also, we previously used load -> is non null instead of AIR
`is_non_null_ptr` if the pointer is comptime-known, but that's a bad
heuristic. Instead, we should check for the pointer to be
comptime-known, *and* for the load to be comptime-known, and only in
that case should we call `Sema.analyzeIsNonNull`.

Resolves: #22556
2025-01-21 00:33:32 +00:00
Jacob Young
b9198b708f x86_64: rewrite @abs 2025-01-20 14:47:07 -05:00
Alex Rønne Petersen
d5db02728c
Merge pull request #22530 from alexrp/omit-unwind-tables
Fix building the standard library without CFI directives
2025-01-20 07:24:12 +01:00
Jacob Young
5cfcb01503 llvm: convert @divFloor and @mod to forms llvm will recognize
On x86_64, the `@divFloor` change is a strict improvement, and the
`@mod` change adds one zero latency instruction.  In return, once we
upgrade to LLVM 20, when the optimizer discovers one of these operations
has a power-of-two constant rhs, it will be able to optimize the entire
operation into an `ashr` or `and`, respectively.

                      #I   CPL   CPT
    old `@divFloor` |  8 | 15 | .143 |
    new `@divFloor` |  7 | 15 | .148 |
    old `@mod`      |  9 | 17 | .134 | (rip llvm
    new `@mod`      | 10 | 17 | .138 |  scheduler)
2025-01-19 22:10:39 -05:00
Alex Rønne Petersen
0ead0beb83
Merge pull request #22524 from alexrp/libunwind-exceptions
`libunwind`: Build C files with `-fexceptions`.
2025-01-19 06:46:24 +01:00
Jacob Young
8ee80d61f6 x86_64: add a bunch of instruction encodings
Closes #19773
2025-01-18 23:30:45 -05:00
Matthew Lugg
ebf782e944
Merge pull request #22531 from mlugg/various-fixes
incremental, Sema: minor fixes
2025-01-19 00:16:26 +00:00
Jacob Young
a446101677 x86_64: enable struct field reordering
The blocker for enabling this feature was my need to debug the emitted
assembly without debug info and having to manually inspect memory to
determine struct contents.  However, we now have debug info!

    (lldb) v -L foo bar
    0x00007fffffffda20: (repro.repro.Foo) foo = {
    0x00007fffffffda24:   .x = 12
    0x00007fffffffda20:   .y = 34
    }
    0x00007fffffffda28: (repro.repro.Bar) bar = {
    0x00007fffffffda28:   .x = 56
    0x00007fffffffda2c:   .y = 78
    }

Updates #21530
2025-01-18 16:14:51 -05:00
mlugg
3b6e5ba490
Sema: don't try to initialize global union pointer at comptime
Resolves: #19832
2025-01-18 14:30:06 +00:00
mlugg
f7b9f84df2
incremental: fix enum resolution bugs 2025-01-18 14:30:06 +00:00
Alex Rønne Petersen
566cc4f99d
compiler: Fix @import("builtin").unwind_tables logic. 2025-01-18 14:57:41 +01:00
Alex Rønne Petersen
332add7a65
libunwind: Build C files with -fexceptions.
See: https://github.com/llvm/llvm-project/pull/121819

This fixes LTO for libunwind, so also re-enable support for that.

Closes #12828.
2025-01-18 13:15:58 +01:00
Andrew Kelley
f7f6217df9 macho linker: adjust symbol priority
strong symbols always take precedence over weak symbols.
2025-01-17 15:12:01 -08:00
Andrew Kelley
e6dc85f1b4 remove memcpy and memmove from bundled libcs
These are provided instead by compiler_rt.

Part of #2879
2025-01-17 12:34:15 -08:00
Jacob Young
8c8dfb35f3 x86_64: fix crashes compiling the compiler and tests 2025-01-16 20:47:30 -05:00
Jacob Young
c3d33440f0 x86_64: pass more behavior tests 2025-01-16 20:47:30 -05:00
Jacob Young
666d76d85c x86_64: implement load and store 2025-01-16 20:47:30 -05:00
Jacob Young
6d1fc0f51c x86_64: implement aggregate access 2025-01-16 20:47:30 -05:00
Jacob Young
63730441d0 x86_64: implement union access 2025-01-16 20:47:30 -05:00
Jacob Young
5069f574f4 x86_64: remove pointless jump to epilogue 2025-01-16 20:47:30 -05:00
Jacob Young
3240adfa16 x86_64: implement pointer addition and subtraction 2025-01-16 20:47:30 -05:00
Jacob Young
0d9079f466 x86_64: implement element access 2025-01-16 20:47:30 -05:00
Jacob Young
870443f7fa x86_64: implement passing undefined as a call arg with the new cc 2025-01-16 20:47:30 -05:00