aboutsummaryrefslogtreecommitdiff
path: root/src
AgeCommit message (Collapse)Author
2025-01-02Sema: correctly label `block_comptime` for restoring error return trace indexmlugg
Resolves: #22384
2025-01-01Merge pull request #22379 from mlugg/incremental-fixesMatthew Lugg
incremental: bugfixes
2025-01-01Sema: fix invalid coercion `*[n:x]T` -> `*[m]T` for `n != m`mlugg
The change in `Sema.coerceExtra` is just to avoid an unhelpful error message, covered by the added test case. Resolves: #22373
2025-01-01incremental: fix errors not being deleted upon re-analysismlugg
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.
2024-12-31link/Elf.zig: set stack size and build-id for dynamic libraries.Jan200101
2024-12-31Sema: add doc comments for comptime reason typesmlugg
2024-12-31Sema: remove some incorrect calls to `requireRuntimeBlock`mlugg
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-31compiler: ensure local `const`s in comptime scope are comptime-knownmlugg
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-31compiler: ensure result of `block_comptime` is comptime-knownmlugg
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-29InternPool: fix leak when the last namespace bucket is fullJacob Young
2024-12-29Value: implement `orderAgainstZeroInner` for errorsDavid Rubin
2024-12-27llvm: fix UB in metadata printerDavid Rubin
2024-12-26InternPool: fix segfault in `rehashTrackedInsts`mlugg
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-24compiler: analyze type and value of global declaration separatelymlugg
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-24compiler: remove Caumlugg
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-23Zir: refactor `declaration` instruction representationmlugg
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-23Merge pull request #22280 from jacobly0/stage2-ppAndrew Kelley
lldb: add more stage2 pretty printers
2024-12-23Merge pull request #22225 from alexrp/libc-linux-os-versionAlex Rønne Petersen
Attach minimum Linux versions to provided libcs + incorporate ABI in `VersionRange.default()`
2024-12-22compiler: Print more information when failing to provide libc.Alex Rønne Petersen
2024-12-20lldb: add pretty printer for cau and nav indicesJacob Young
2024-12-20lldb: add pretty printer for intern pool indicesJacob Young
2024-12-19Dwarf: remove redundant debug infoJacob Young
2024-12-19Module: keep frame pointer in ReleaseSmall on x86wooster0
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-19Zcu: allow `node_offset_var_decl_*` source location for function declarationsmlugg
2024-12-19compiler: disallow `align` etc annotations on comptime-only globalsmlugg
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-18compiler: disallow `callconv` etc from depending on function parametersmlugg
Resolves: #22261
2024-12-18compiler: move `RuntimeIndex` to `Sema`mlugg
Just a small refactor.
2024-12-17Zir: store declaration column number so Dwarf doesn't need to load the ASTmlugg
Resolves: #21227
2024-12-17InternPool: fix typoJacob Young
2024-12-17Dwarf: deleting bad code until I remember what it doesJacob Young
2024-12-17Dwarf: fix data races by reading from ZIRJacob Young
2024-12-17Merge pull request #22252 from jacobly0/dwarf-deduped-structsAndrew Kelley
Dwarf: preserve deduped struct navs
2024-12-17Merge pull request #22251 from alexrp/remove-cudaAndrew Kelley
`zig cc`: Remove broken CUDA C/C++ support.
2024-12-16Merge pull request #22250 from mlugg/zon-astMatthew Lugg
compiler: introduce ZonGen and make `ast-check` run it for ZON inputs
2024-12-16Dwarf: include comptime-only values in debug infoJacob Young
2024-12-16InternPool: we have pointer subtraction now!Jacob Young
2024-12-16Dwarf: preserve deduped struct navsJacob Young
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-16compiler: introduce ZonGen and make `ast-check` run it for ZON inputsmlugg
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-16std.c.darwin.posix_spawn: fix signaturemlugg
And change corresponding signature in `DarwinPosixSpawn`.
2024-12-16compiler: add some missing `const`smlugg
The previous commit exposed some missing `const` qualifiers in a few places. These mutable slices could have been used to store invalid values into memory!
2024-12-16Sema: disallow unsafe in-memory coercionsmlugg
The error messages here aren't amazing yet, but this is an improvement on status quo, because the current behavior allows false negative compile errors, so effectively miscompiles. Resolves: #15874
2024-12-16Merge pull request #22245 from mlugg/zir-no-doc-commentsMatthew Lugg
compiler: remove doc comments from Zir
2024-12-16mingw: Fix CFLAGS for winpthreads.Alex Rønne Petersen
It should not use the CRT ones, and it also needs a few flags of its own that I forgot to add in #22156. Follow-up fix for #10989.
2024-12-15Merge pull request #22233 from jacobly0/fix-relocsAndrew Kelley
Elf.Atom: fix truncated dyn abs relocs
2024-12-15compiler: remove doc comments from Zirmlugg
This code was left over from the legacy Autodoc implementation. No component of the compiler pipeline actually requires doc comments, so it is a waste of time and space to store them in ZIR.
2024-12-15Sema: disallow runtime stores to pointers with comptime-only element typesmlugg
2024-12-15Sema: do not allow coercing undefined to opaque typesmlugg
2024-12-15zig cc: Remove broken CUDA C/C++ support.Alex Rønne Petersen
2024-12-14Elf: fix shdr size getting out of sync with the actual sizeJacob Young
2024-12-14Elf.Atom: fix truncated dyn abs relocsJacob Young