965 Commits

Author SHA1 Message Date
mlugg
90116d92b0 Compilation: don't call resolveReferences unnecessarily
This function is slow and should only be called in compile error cases.
2024-08-17 18:50:10 -04:00
mlugg
84c2ebd6c6 frontend: incremental compilation progress
Another big commit, sorry! This commit makes all fixes necessary for
incremental updates of the compiler itself (specifically, adding a
breakpoint to `zirCompileLog`) to succeed, at least on the frontend.

The biggest change here is a reform to how types are handled. It works
like this:
* When a type is first created in `zirStructDecl` etc, its namespace is
  scanned. If the type requires resolution, an `interned` dependency is
  declared for the containing `AnalUnit`.
* `zirThis` also declared an `interned` dependency for its `AnalUnit` on
  the namespace's owner type.
* If the type's namespace changes, the surrounding source declaration
  changes hash, so `zirStructDecl` etc will be hit again. We check
  whether the namespace has been scanned this generation, and re-scan it
  if not.
* Namespace lookups also check whether the namespace in question
  requires a re-scan based on the generation. This is because there's no
  guarantee that the `zirStructDecl` is re-analyzed before the namespace
  lookup is re-analyzed.
* If a type's structure (essentially its fields) change, then the type's
  `Cau` is considered outdated. When the type is re-analyzed due to
  being outdated, or the `zirStructDecl` is re-analyzed by being
  transitively outdated, or a corresponding `zirThis` is re-analyzed by
  being transitively outdated, the struct type is recreated at a new
  `InternPool` index. The namespace's owner is updated (but not
  re-scanned, since that is handled by the mechanisms above), and the
  old type, while remaining a valid `Index`, is removed from the map
  metadata so it will never be found by lookups. `zirStructDecl` and
  `zirThis` store an `interned` dependency on the *new* type.
2024-08-17 18:50:10 -04:00
mlugg
978fe68a65 Compilation: actually do codegen on non-initial updates 2024-08-17 18:50:10 -04:00
mlugg
50960fac80 compiler: be more cautious about source locations
Two fixes here.

* Prevent a crash when sorting the list of analysis errors when some
  errors refer to lost source locations. These errors can be sorted
  anywhere in the list, because they are (in theory) guaranteed to never
  be emitted by the `resolveReferences` logic. This case occurs, for
  instance, when a declaration has compile errors in the initial update
  and is deleted in the second update.

* Prevent a crash when resolving the source location for `entire_file`
  errors for a non-existent file. This is the bug underlying #20954.

Resolves: #20954.
2024-08-17 18:50:10 -04:00
mlugg
4e5834a9f2 Compilation: don't queue std analysis twice when testing std 2024-08-17 18:50:10 -04:00
mlugg
aa6c1c40ec frontend: yet more incremental work 2024-08-17 18:50:10 -04:00
mlugg
6faa4cc7e6 Zcu: construct full reference graph
This commit updates `Zcu.resolveReferences` to traverse the graph of
`AnalUnit` references (starting from the 1-3 roots of analysis) in order
to determine which `AnalUnit`s are referenced in an update. Errors for
unreferenced entities are omitted from the error bundle. However, note
that unreferenced `Nav`s are not removed from the binary.
2024-08-17 18:50:10 -04:00
mlugg
895267c916 frontend: incremental progress
This commit makes more progress towards incremental compilation, fixing
some crashes in the frontend. Notably, it fixes the regressions introduced
by #20964. It also cleans up the "outdated file root" mechanism, by
virtue of deleting it: we now detect outdated file roots just after
updating ZIR refs, and re-scan their namespaces.
2024-08-17 18:50:10 -04:00
Jacob Young
ef11bc9899 Dwarf: rework self-hosted debug info from scratch
This is in preparation for incremental and actually being able to debug
executables built by the x86_64 backend.
2024-08-16 15:22:55 -04:00
Jakub Konka
90989be0e3
Merge pull request #21065 from ziglang/elf-zig-got
elf: replace .got.zig with a zig jump table
2024-08-16 21:19:44 +02:00
Igor Anić
72bcc3b7a5
std.tar: add writer (#19603)
Simplifies code in docs creation where we used `std.tar.output.Header`.
Writer uses that Header internally and provides higher level interface.
Updates checksum on write, handles long file names, allows setting mtime and file permission mode. Provides handy interface for passing `Dir.WalkerEntry`.
2024-08-15 22:05:53 -07:00
Jakub Konka
f968dd0cb1 comp: actually report codegen errors 2024-08-13 13:30:24 +02:00
Alex Rønne Petersen
d6f9972594 all: Handle spirv in addition to spirv(32,64) where applicable.
Some of this is arbitrary since spirv (as opposed to spirv32/spirv64) refers to
the version with logical memory layout, i.e. no 'real' pointers. This change at
least matches what clang does.
2024-08-12 16:08:28 -07:00
mlugg
548a087faf
compiler: split Decl into Nav and Cau
The type `Zcu.Decl` in the compiler is problematic: over time it has
gained many responsibilities. Every source declaration, container type,
generic instantiation, and `@extern` has a `Decl`. The functions of
these `Decl`s are in some cases entirely disjoint.

After careful analysis, I determined that the two main responsibilities
of `Decl` are as follows:
* A `Decl` acts as the "subject" of semantic analysis at comptime. A
  single unit of analysis is either a runtime function body, or a
  `Decl`. It registers incremental dependencies, tracks analysis errors,
  etc.
* A `Decl` acts as a "global variable": a pointer to it is consistent,
  and it may be lowered to a specific symbol by the codegen backend.

This commit eliminates `Decl` and introduces new types to model these
responsibilities: `Cau` (Comptime Analysis Unit) and `Nav` (Named
Addressable Value).

Every source declaration, and every container type requiring resolution
(so *not* including `opaque`), has a `Cau`. For a source declaration,
this `Cau` performs the resolution of its value. (When #131 is
implemented, it is unsolved whether type and value resolution will share
a `Cau` or have two distinct `Cau`s.) For a type, this `Cau` is the
context in which type resolution occurs.

Every non-`comptime` source declaration, every generic instantiation,
and every distinct `extern` has a `Nav`. These are sent to codegen/link:
the backends by definition do not care about `Cau`s.

This commit has some minor technically-breaking changes surrounding
`usingnamespace`. I don't think they'll impact anyone, since the changes
are fixes around semantics which were previously inconsistent (the
behavior changed depending on hashmap iteration order!).

Aside from that, this changeset has no significant user-facing changes.
Instead, it is an internal refactor which makes it easier to correctly
model the responsibilities of different objects, particularly regarding
incremental compilation. The performance impact should be negligible,
but I will take measurements before merging this work into `master`.

Co-authored-by: Jacob Young <jacobly0@users.noreply.github.com>
Co-authored-by: Jakub Konka <kubkon@jakubkonka.com>
2024-08-11 07:29:41 +01:00
sin-ack
2cfad20204 Compilation: Mark .c++ files as having C++ extension
Some projects, such as Cap'n Proto, use .c++ as their filenames. Without
this, compiling them fails because zig c++ will fall back to using the
linker.
2024-08-08 17:42:11 -07:00
Andrew Kelley
904fcda736 Compilation: fix -femit-docs 2024-08-07 00:48:32 -07:00
Andrew Kelley
ff503edc04 Compilation: fix not showing sub-errors for autodocs 2024-08-07 00:48:32 -07:00
Alex Rønne Petersen
27507ad971 compiler: Enable -Werror=date-time for C/C++ code in release builds.
We advertise reproducible builds for release modes, so let's help users achieve
that in C/C++ code. Users can still override this manually if they really want.
2024-07-29 11:27:38 -07:00
Alex Rønne Petersen
642cd730c8 link: Accept -Brepro linker option and pass it to LLD.
Enable it by default when building Zig code in release modes.

Contributes to #9432.
2024-07-28 14:37:03 +02:00
Jakub Konka
91c17979f1
Merge pull request #20807 from Rexicon226/riscv
riscv: more backend progress
2024-07-28 11:34:23 +02:00
David Rubin
a7498a6a53
comp: enable compilation of zig_libc 2024-07-26 14:02:48 -07:00
Andrew Kelley
208baa37ca frontend: add missed cache hash on --debug-rt
Makes adding --debug-rt correctly invalidate the cache for compiler_rt
and libfuzzer.
2024-07-26 08:42:40 -07:00
Andrew Kelley
a3c74aca99 add --debug-rt CLI arg to the compiler + bonus edits
The flag makes compiler_rt and libfuzzer be in debug mode.

Also:
* fuzzer: override debug logs and disable debug logs for frequently
  called functions
* std.Build.Fuzz: fix bug of rerunning the old unit test binary
* report errors from rebuilding the unit tests better
* link.Elf: additionally add tsan lib and fuzzer lib to the hash
2024-07-25 18:52:21 -07:00
Andrew Kelley
94f4f9c4ef add sub-compilation cache inputs to parents in whole mode
closes #20782
2024-07-24 19:40:54 -07:00
Andrew Kelley
3844b91db7 Compilation: build compiler_rt and fuzzer in parallel
With the rest of the pipeline.

Tracked by #9188
2024-07-24 18:24:00 -07:00
Andrew Kelley
26d2a7960e default "trace pc guard" coverage off
* Add -f(no-)sanitize-coverage-trace-pc-guard CLI flag which defaults to
  off. This value lowers to TracePCGuard = true (LLVM backend) and -Xclang
  -fsanitize-coverage-trace-pc-guard. These settings are not
  automatically included with -ffuzz.
* Add `Build.Step.Compile` flag for sanitize_coverage_trace_pc_guard
  with appropriate documentation.
* Add `zig cc` integration for the respective flags.
* Avoid crashing in ELF linker code when -ffuzz -femit-llvm-ir used
  together.
2024-07-23 17:30:53 -07:00
Andrew Kelley
33d4742456 LLVM: more fine-grained sancov emit options
Exposes sanitizer coverage flags to the target machine emit function.
Makes it easier to change sancov options without rebuilding the C++
files.

This also enables PCTable = true for sancov which is needed by AFL, and
adds the corresponding Clang flag.
2024-07-23 16:04:58 -07:00
Andrew Kelley
6f3e9939d0
Merge pull request #20725 from ziglang/fuzz
initial support for integrated fuzzing
2024-07-23 11:39:19 -07:00
Andrew Kelley
1cb994899d Compilation: fix regression in addCCArgs
`-fno-sanitize=function` must come after `-fsanitize=undefined` or it
has no effect.
2024-07-22 14:26:17 -07:00
Andrew Kelley
54b7e144b1 initial support for integrated fuzzing
* Add the `-ffuzz` and `-fno-fuzz` CLI arguments.
* Detect fuzz testing flags from zig cc.
* Set the correct clang flags when fuzz testing is requested. It can be
  combined with TSAN and UBSAN.
* Compilation: build fuzzer library when needed which is currently an
  empty zig file.
* Add optforfuzzing to every function in the llvm backend for modules
  that have requested fuzzing.
* In ZigLLVMTargetMachineEmitToFile, add the optimization passes for
  sanitizer coverage.
* std.mem.eql uses a naive implementation optimized for fuzzing when
  builtin.fuzz is true.

Tracked by #20702
2024-07-22 13:07:02 -07:00
Jakub Konka
cba3389d90 macho: redo input file parsing in prep for multithreading 2024-07-22 12:05:56 +02:00
Andrew Kelley
b5f3d12164
Merge pull request #20688 from ziglang/incr-test
introduce a new tool for testing incremental compilation
2024-07-20 13:04:49 -07:00
Jacob Young
2e65244cae dev: fix llvm backend checks 2024-07-20 07:43:40 -04:00
Andrew Kelley
eb4028bf30 add std.fmt.hex
converts an unsigned integer into an array
2024-07-20 01:06:29 -07:00
Jacob Young
4f742c4cfc dev: introduce dev environments that enable compiler feature sets 2024-07-19 22:35:33 -07:00
Andrew Kelley
8cb09db4e3 update some Module references to Zcu instead
I ended up reverting my real change, so here's a consolation prize
instead.
2024-07-15 19:05:55 -07:00
Andrew Kelley
60318a1e39 frontend: move updateZirRefs to be single-threaded
for simplicity's sake. This makes it O(M) instead of O(N*M) where N is
tracked insts and M is number of changed source files.
2024-07-15 18:54:41 -07:00
Andrew Kelley
716b128a24 frontend: add -fincremental, -fno-incremental flag
Remove --debug-incremental

This flag is also added to the build system. Importantly, this tells
Compile step whether or not to keep the compiler running between
rebuilds. It defaults off because it is currently crashing
zirUpdateRefs.
2024-07-14 21:18:09 -07:00
Andrew Kelley
abf8955951 make zig compiler processes live across rebuilds
Changes the `make` function signature to take an options struct, which
additionally includes `watch: bool`. I intentionally am not exposing
this information to configure phase logic.

Also adds global zig cache to the compiler cache prefixes.

Closes #20600
2024-07-14 19:51:16 -07:00
Andrew Kelley
d404d8a363
Merge pull request #20593 from jacobly0/more-races
InternPool: fix more races
2024-07-14 17:32:51 -07:00
Andrew Kelley
d8c1c9ea6e frontend: report correct paths for C objects
oops, the src_path field of CObject is not relative to the module
owner's root directory.
2024-07-13 02:13:03 -07:00
Jacob Young
ad55fb7a20 Compilation: restore saveState 2024-07-13 04:47:38 -04:00
Jacob Young
2ff49751aa Compilation: introduce work stages for better work distribution 2024-07-13 04:47:38 -04:00
Jacob Young
a1053e8e1d InternPool: add and use a mutate mutex for each list
This allows the mutate mutex to only be locked during actual grows,
which are rare. For the lists that didn't previously have a mutex, this
change has little effect since grows are rare and there is zero
contention on a mutex that is only ever locked by one thread.  This
change allows `extra` to be mutated without racing with a grow.
2024-07-13 04:47:38 -04:00
Andrew Kelley
67e3e4989d Compilation: fix rebase conflict 2024-07-12 00:18:29 -07:00
Andrew Kelley
5a34e6c3e6 frontend: add file system inputs for incremental cache mode
These are also used for whole cache mode in the case that any compile
errors are emitted.
2024-07-12 00:15:11 -07:00
Andrew Kelley
a3c20dffae integrate Compile steps with file watching
Updates the build runner to unconditionally require a zig lib directory
parameter. This parameter is needed in order to correctly understand
file system inputs from zig compiler subprocesses, since they will refer
to "the zig lib directory", and the build runner needs to place file
system watches on directories in there.

The build runner's fanotify file watching implementation now accounts
for when two or more Cache.Path instances compare unequal but ultimately
refer to the same directory in the file system.

Breaking change: std.Build no longer has a zig_lib_dir field. Instead,
there is the Graph zig_lib_directory field, and individual Compile steps
can still have their zig lib directories overridden. I think this is
unlikely to break anyone's build in practice.

The compiler now sends a "file_system_inputs" message to the build
runner which shares the full set of files that were added to the cache
system with the build system, so that the build runner can watch
properly and redo the Compile step. This is implemented for whole cache
mode but not yet for incremental cache mode.
2024-07-12 00:14:08 -07:00
Jacob Young
c79d3e4aab Compilation: fix leak 2024-07-10 22:05:52 -04:00
Jacob Young
c2316c5228 InternPool: make global_error_set thread-safe 2024-07-10 21:39:55 -04:00
Jacob Young
afa66fa392 InternPool: make tracked_insts thread-safe 2024-07-10 14:42:50 -04:00