16860 Commits

Author SHA1 Message Date
mlugg
137787edbb Sema: fix incorrect type in optional_payload instruction
Resolves: #22417
2025-01-05 19:38:19 +00:00
Travis Lange
82e7f23c49 Added support for thin lto 2025-01-05 18:08:11 +01:00
Matthew Lugg
41786fabfc
Merge pull request #22412 from mlugg/line-number-incremental
incremental: debug line number updates
2025-01-05 11:48:58 +00:00
mlugg
b039a8b615 compiler: slightly simplify builtin decl memoization
Rather than `Zcu.BuiltinDecl.Memoized` being a struct with fields, it
can instead just be an array, indexed by the enum. This allows runtime
indexing, avoiding a few now-unnecessary `inline` switch cases.
2025-01-05 05:52:02 +00:00
Jacob Young
dde3116e50
Dwarf: implement new incremental line number update API 2025-01-05 02:20:56 +00:00
mlugg
065e10c95c
link: new incremental line number update API 2025-01-05 02:20:56 +00:00
mlugg
f01029c4af
incremental: new AnalUnit to group dependencies on std.builtin decls
This commit reworks how values like the panic handler function are
memoized during a compiler invocation. Previously, the value was
resolved by whichever analysis requested it first, and cached on `Zcu`.
This is problematic for incremental compilation, as after the initial
resolution, no dependencies are marked by users of this memoized state.
This is arguably acceptable for `std.builtin`, but it's definitely not
acceptable for the panic handler/messages, because those can be set by
the user (`std.builtin.Panic` checks `@import("root").Panic`).

So, here we introduce a new kind of `AnalUnit`, called `memoized_state`.
There are 3 such units:
* `.{ .memoized_state = .va_list }` resolves the type `std.builtin.VaList`
* `.{ .memoized_state = .panic }` resolves `std.Panic`
* `.{ .memoized_state = .main }` resolves everything else we want

These units essentially "bundle" the resolution of their corresponding
declarations, storing the results into fields on `Zcu`. This way, when,
for instance, a function wants to call the panic handler, it simply runs
`ensureMemoizedStateResolved`, registering one dependency, and pulls the
values from the `Zcu`. This "bundling" minimizes dependency edges. The 3
units are separated to allow them to act independently: for instance,
the panic handler can use `std.builtin.Type` without triggering a
dependency loop.
2025-01-04 07:51:19 +00:00
mlugg
fd62912787
incremental: correctly handle losing file root struct_decl inst 2025-01-04 05:44:29 +00:00
mlugg
f818098971
incremental: correctly return error.AnalysisFail when type structure changes
`Zcu.PerThead.ensureTypeUpToDate` is set up in such a way that it only
returns the updated type the first time it is called. In general, that's
okay; however, the exception is that we want the function to continue
returning `error.AnalysisFail` when the type has been lost, or its
number of captures changed.

Therefore, the check for this case now happens before the up-to-date
success return.

For simplicity, the number of captures is now handled by intentionally
losing the instruction in `Zcu.mapOldZirToNew`, since there is nothing
to gain from tracking a type when old instances of it can never be
reused.
2025-01-04 05:44:29 +00:00
mlugg
814491f599
Zcu: fix crash when incremental re-analysis of type annotation yields same result 2025-01-04 05:44:29 +00:00
mlugg
b4da8eef2a Zir: split up start and end of range in for_len
The old lowering was kind of neat, but it unintentionally allowed the
syntax `for (123) |_| { ... }`, and there wasn't really a way to fix
that. So, instead, we include both the start and the end of the range in
the `for_len` instruction (each operand to `for` now has *two* entries
in this multi-op instruction). This slightly increases the size of ZIR
for loops of predominantly indexables, but the difference is small
enough that it's not worth complicating ZIR to try and fix it.
2025-01-03 22:28:37 +00:00
mlugg
252c203101 Sema: correctly label block_comptime for restoring error return trace index
Resolves: #22384
2025-01-02 16:26:50 +00:00
Matthew Lugg
0585ed0641
Merge pull request #22379 from mlugg/incremental-fixes
incremental: bugfixes
2025-01-01 21:49:02 +00:00
mlugg
5333d2443a Sema: fix invalid coercion *[n:x]T -> *[m]T for n != m
The change in `Sema.coerceExtra` is just to avoid an unhelpful error
message, covered by the added test case.

Resolves: #22373
2025-01-01 16:20:40 +00:00
mlugg
f0d5e0df4d
incremental: fix errors not being deleted upon re-analysis
Previously, logic in `Compilation.getAllErrorsAlloc` was corrupting the
`failed_analysis` hashmap. This meant that on updates after the initial
update, attempts to remove entries from this map (because the `AnalUnit`
in question is being re-analyzed) silently failed. This resulted in
compile errors from earlier updates wrongly getting "stuck", i.e. never
being removed.

This commit also adds a few log calls which helped me to find this bug.
2025-01-01 15:49:37 +00:00
Jan200101
d02c2c76fc link/Elf.zig: set stack size and build-id for dynamic libraries. 2024-12-31 17:09:20 +01:00
mlugg
106df881d3
Sema: add doc comments for comptime reason types 2024-12-31 09:56:21 +00:00
mlugg
7e82398cfe
Sema: remove some incorrect calls to requireRuntimeBlock
Most calls to `requireRuntimeBlock` in Sema are not correct. This
function doesn't deal with all of them, but it does deal with ones which
have, in combination with the past few commits, introduced real-world
regressions.

Related: #22353
2024-12-31 09:55:03 +00:00
mlugg
9a70eeeac5
compiler: ensure local consts in comptime scope are comptime-known
This fixes a bug which exposed a compiler implementation detail (ZIR
alloc elision). Previously, `const` declarations with a runtime-known
value in a comptime scope were permitted only if AstGen was able to
elide the alloc in ZIR, since the error was reported by storing to the
comptime alloc.

This just adds a new instruction to also emit this error when the alloc
is elided.
2024-12-31 09:55:03 +00:00
mlugg
6026a5f217
compiler: ensure result of block_comptime is comptime-known
To avoid this PR regressing error messages, most of the work here has
gone towards improving error notes for why code was comptime-evaluated.
ZIR `block_comptime` now stores a "comptime reason", the enum for which
is also used by Sema. There are two types in Sema:

* `ComptimeReason` represents the reason we started evaluating something
  at comptime.
* `BlockComptimeReason` represents the reason a given block is evaluated
  at comptime; it's either a `ComptimeReason` with an attached source
  location, or it's because we're in a function which was called at
  comptime (and that function's `Block` should be consulted for the
  "parent" reason).

Every `Block` stores a `?BlockComptimeReason`. The old `is_comptime`
field is replaced with a trivial `isComptime()` method which returns
whether that reason is non-`null`.

Lastly, the handling for `block_comptime` has been simplified. It was
previously going through an unnecessary runtime-handling path; now, it
is a trivial sub block exited through a `break_inline` instruction.

Resolves: #22296
2024-12-31 09:55:03 +00:00
Jacob Young
ec60156f18 InternPool: fix leak when the last namespace bucket is full 2024-12-29 15:28:40 -05:00
David Rubin
b99dbb6fb5 Value: implement orderAgainstZeroInner for errors 2024-12-29 13:36:52 -05:00
David Rubin
5ee2816f62 llvm: fix UB in metadata printer 2024-12-27 15:38:41 -05:00
mlugg
42dac40b3f InternPool: fix segfault in rehashTrackedInsts
The `.empty` map in a shard is weird: it claims to have capacity 1, but
you're not actually allowed to actually use that capacity. That's fine
for the normal insertion algorithm, because it always resizes to a
higher capacity when inserting the initial element. However,
`rehashTrackedInsts` was not aware of this caveat, so sometimes tried to
store to the single element of the `empty` map.

This system exists to avoid an extra branch in the main resizing logic
(since `new_cap = old_cap * 2` only works if the capacity is never
non-zero). However, it's fine for `rehashTrackedInsts` to have an extra
branch to handle this case, since it's literally called once per update.
2024-12-26 02:19:02 +00:00
mlugg
3afda4322c
compiler: analyze type and value of global declaration separately
This commit separates semantic analysis of the annotated type vs value
of a global declaration, therefore allowing recursive and mutually
recursive values to be declared.

Every `Nav` which undergoes analysis now has *two* corresponding
`AnalUnit`s: `.{ .nav_val = n }` and `.{ .nav_ty = n }`. The `nav_val`
unit is responsible for *fully resolving* the `Nav`: determining its
value, linksection, addrspace, etc. The `nav_ty` unit, on the other
hand, resolves only the information necessary to construct a *pointer*
to the `Nav`: its type, addrspace, etc. (It does also analyze its
linksection, but that could be moved to `nav_val` I think; it doesn't
make any difference).

Analyzing a `nav_ty` for a declaration with no type annotation will just
mark a dependency on the `nav_val`, analyze it, and finish. Conversely,
analyzing a `nav_val` for a declaration *with* a type annotation will
first mark a dependency on the `nav_ty` and analyze it, using this as
the result type when evaluating the value body.

The `nav_val` and `nav_ty` units always have references to one another:
so, if a `Nav`'s type is referenced, its value implicitly is too, and
vice versa. However, these dependencies are trivial, so, to save memory,
are only known implicitly by logic in `resolveReferences`.

In general, analyzing ZIR `decl_val` will only analyze `nav_ty` of the
corresponding `Nav`. There are two exceptions to this. If the
declaration is an `extern` declaration, then we immediately ensure the
`Nav` value is resolved (which doesn't actually require any more
analysis, since such a declaration has no value body anyway).
Additionally, if the resolved type has type tag `.@"fn"`, we again
immediately resolve the `Nav` value. The latter restriction is in place
for two reasons:

* Functions are special, in that their externs are allowed to trivially
  alias; i.e. with a declaration `extern fn foo(...)`, you can write
  `const bar = foo;`. This is not allowed for non-function externs, and
  it means that function types are the only place where it is possible
  for a declaration `Nav` to have a `.@"extern"` value without actually
  being declared `extern`. We need to identify this situation
  immediately so that the `decl_ref` can create a pointer to the *real*
  extern `Nav`, not this alias.
* In certain situations, such as taking a pointer to a `Nav`, Sema needs
  to queue analysis of a runtime function if the value is a function. To
  do this, the function value needs to be known, so we need to resolve
  the value immediately upon `&foo` where `foo` is a function.

This restriction is simple to codify into the eventual language
specification, and doesn't limit the utility of this feature in
practice.

A consequence of this commit is that codegen and linking logic needs to
be more careful when looking at `Nav`s. In general:

* When `updateNav` or `updateFunc` is called, it is safe to assume that
  the `Nav` being updated (the owner `Nav` for `updateFunc`) is fully
  resolved.
* Any `Nav` whose value is/will be an `@"extern"` or a function is fully
  resolved; see `Nav.getExtern` for a helper for a common case here.
* Any other `Nav` may only have its type resolved.

This didn't seem to be too tricky to satisfy in any of the existing
codegen/linker backends.

Resolves: #131
2024-12-24 02:18:41 +00:00
mlugg
40aafcd6a8
compiler: remove Cau
The `Cau` abstraction originated from noting that one of the two primary
roles of the legacy `Decl` type was to be the subject of comptime
semantic analysis. However, the data stored in `Cau` has always had some
level of redundancy. While preparing for #131, I went to remove that
redundany, and realised that `Cau` now had exactly one field: `owner`.

This led me to conclude that `Cau` is, in fact, an unnecessary level of
abstraction over what are in reality *fundamentally different* kinds of
analysis unit (`AnalUnit`). Types, `Nav` vals, and `comptime`
declarations are all analyzed in different ways, and trying to treat
them as the same thing is counterproductive!

So, these 3 cases are now different alternatives in `AnalUnit`. To avoid
stealing bits from `InternPool`-based IDs, which are already a little
starved for bits due to the sharding datastructures, `AnalUnit` is
expanded to 64 bits (30 of which are currently unused). This doesn't
impact memory usage too much by default, because we don't store
`AnalUnit`s all too often; however, we do store them a lot under
`-fincremental`, so a non-trivial bump to peak RSS can be observed
there. This will be improved in the future when I made
`InternPool.DepEntry` less memory-inefficient.

`Zcu.PerThread.ensureCauAnalyzed` is split into 3 functions, for each of
the 3 new types of `AnalUnit`. The new logic is much easier to
understand, because it avoids conflating the logic of these
fundamentally different cases.
2024-12-24 02:18:41 +00:00
mlugg
18362ebe13
Zir: refactor declaration instruction representation
The new representation is often more compact. It is also more
straightforward to understand: for instance, `extern` is represented on
the `declaration` instruction itself rather than using a special
instruction. The same applies to `var`, making both of these far more
compact.

This commit also separates the type and value bodies of a `declaration`
instruction. This is a prerequisite for #131.

In general, `declaration` now directly encodes details of the syntax
form used, and the embedded ZIR bodies are for actual expressions. The
only exception to this is functions, where ZIR is effectively designed
as if we had #1717. `extern fn` declarations are modeled as
`extern const` with a function type, and normal `fn` definitions are
modeled as `const` with a `func{,_fancy,_inferred}` instruction. This
may change in the future, but improving on this was out of scope for
this commit.
2024-12-23 21:09:17 +00:00
Andrew Kelley
af5e731729
Merge pull request #22280 from jacobly0/stage2-pp
lldb: add more stage2 pretty printers
2024-12-23 15:55:03 -05:00
Alex Rønne Petersen
f06ca14cb5
Merge pull request #22225 from alexrp/libc-linux-os-version
Attach minimum Linux versions to provided libcs + incorporate ABI in `VersionRange.default()`
2024-12-23 19:42:53 +01:00
Alex Rønne Petersen
130310972d
compiler: Print more information when failing to provide libc. 2024-12-22 22:06:00 +01:00
Jacob Young
5776d8f270 lldb: add pretty printer for cau and nav indices 2024-12-20 22:51:20 -05:00
Jacob Young
5c76e08f49 lldb: add pretty printer for intern pool indices 2024-12-20 22:51:20 -05:00
Jacob Young
06206479a9 Dwarf: remove redundant debug info 2024-12-19 17:10:03 -05:00
wooster0
0ff0bdb4a7 Module: keep frame pointer in ReleaseSmall on x86
On x86 and x86_64 keeping the frame pointer usually reduces binary size, even for simple programs:

```
~$ cat x.zig
pub fn main() void {
    @import("std").debug.print("hello", .{});
}
~$ zig build-exe x.zig -target x86_64-linux -OReleaseSmall -fno-omit-frame-pointer && wc -c x
5168 x
~$ zig build-exe x.zig -target x86_64-linux -OReleaseSmall -fomit-frame-pointer && wc -c x
5216 x
```
```
~$ cat x.zig
pub fn main() void {
    @import("std").debug.print("hello", .{});
}
~$ zig build-exe x.zig -target x86-linux -OReleaseSmall -fno-omit-frame-pointer && wc -c x
3400 x
~$ zig build-exe x.zig -target x86-linux -OReleaseSmall -fomit-frame-pointer && wc -c x
3556 x
```

A bigger benchmark is the Zig compiler:

With no changes to anything on master branch:
```
$ zig build -Dno-lib -Dno-langref --zig-lib-dir lib -Doptimize=ReleaseSmall
$ wc -c zig-out/bin/zig
10698792 zig-out/bin/zig
```
Adding `.omit_frame_pointer = false` in `addCompilerStep` in `build.zig`:
```
$ zig build -Dno-lib -Dno-langref --zig-lib-dir lib -Doptimize=ReleaseSmall
$ wc -c zig-out/bin/zig
10155744 zig-out/bin/zig
```
2024-12-19 20:12:34 +01:00
mlugg
58b8b1ac2a
Zcu: allow node_offset_var_decl_* source location for function declarations 2024-12-19 03:21:56 +00:00
mlugg
eac87ea8d6
compiler: disallow align etc annotations on comptime-only globals
This includes function aliases, but not function declarations.

Also, re-introduce a target check for function alignment which was
inadvertently removed in the prior commit.
2024-12-19 03:21:56 +00:00
mlugg
7408679234
compiler: disallow callconv etc from depending on function parameters
Resolves: #22261
2024-12-18 23:06:35 +00:00
mlugg
242bb44695
compiler: move RuntimeIndex to Sema
Just a small refactor.
2024-12-18 20:34:10 +00:00
mlugg
98f63990d5
Zir: store declaration column number so Dwarf doesn't need to load the AST
Resolves: #21227
2024-12-17 22:52:30 +00:00
Jacob Young
737154fcd8 InternPool: fix typo 2024-12-17 17:26:55 -05:00
Jacob Young
14f7900ac5 Dwarf: deleting bad code until I remember what it does 2024-12-17 17:26:55 -05:00
Jacob Young
2bb05f0ee7 Dwarf: fix data races by reading from ZIR 2024-12-17 16:37:04 -05:00
Andrew Kelley
debba652a3
Merge pull request #22252 from jacobly0/dwarf-deduped-structs
Dwarf: preserve deduped struct navs
2024-12-17 01:26:09 -05:00
Andrew Kelley
e5c188e59a
Merge pull request #22251 from alexrp/remove-cuda
`zig cc`: Remove broken CUDA C/C++ support.
2024-12-17 01:24:46 -05:00
Matthew Lugg
7e8be21363
Merge pull request #22250 from mlugg/zon-ast
compiler: introduce ZonGen and make `ast-check` run it for ZON inputs
2024-12-16 23:01:24 +00:00
Jacob Young
8c0628d0e2 Dwarf: include comptime-only values in debug info 2024-12-16 17:25:52 -05:00
Jacob Young
1983adb8ae InternPool: we have pointer subtraction now! 2024-12-16 15:11:23 -05:00
Jacob Young
74ba5762ec Dwarf: preserve deduped struct navs
Previously, if multiple navs owned the same type due to being the same
zir node and having the same captures, they would overwrite each other.
Now, the navs codegenned later emit a decl alias to the first one.
2024-12-16 15:11:23 -05:00
mlugg
c7485d73ac
compiler: introduce ZonGen and make ast-check run it for ZON inputs
Currently, `zig ast-check` fails on ZON files, because it tries to
interpret the file as Zig source code. This commit introduces a new
verification pass, `std.zig.ZonGen`, which applies to an AST in ZON
mode.

Like `AstGen`, this pass also converts the AST into a more helpful
format. Rather than a sequence of instructions like `Zir`, the output
format of `ZonGen` is a new datastructure called `Zoir`. This type is
essentially a simpler form of AST, containing only the information
required for consumers of ZON. It is also far more compact than
`std.zig.Ast`, with the size generally being comparable to the size of
the well-formatted source file.

The emitted `Zoir` is currently not used aside from the `-t` option to
`ast-check` which causes it to be dumped to stdout. However, in future,
it can be used for comptime `@import` of ZON files, as well as for
simpler handling of files like `build.zig.zon`, and even by other parts
of the Zig Standard Library.

Resolves: #22078
2024-12-16 17:02:35 +00:00
mlugg
512cb22d83
std.c.darwin.posix_spawn: fix signature
And change corresponding signature in `DarwinPosixSpawn`.
2024-12-16 14:53:54 +00:00