aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/llvm
AgeCommit message (Collapse)Author
2025-08-01build system: replace fuzzing UI with build UI, add time reportmlugg
This commit replaces the "fuzzer" UI, previously accessed with the `--fuzz` and `--port` flags, with a more interesting web UI which allows more interactions with the Zig build system. Most notably, it allows accessing the data emitted by a new "time report" system, which allows users to see which parts of Zig programs take the longest to compile. The option to expose the web UI is `--webui`. By default, it will listen on `[::1]` on a random port, but any IPv6 or IPv4 address can be specified with e.g. `--webui=[::1]:8000` or `--webui=127.0.0.1:8000`. The options `--fuzz` and `--time-report` both imply `--webui` if not given. Currently, `--webui` is incompatible with `--watch`; specifying both will cause `zig build` to exit with a fatal error. When the web UI is enabled, the build runner spawns the web server as soon as the configure phase completes. The frontend code consists of one HTML file, one JavaScript file, two CSS files, and a few Zig source files which are built into a WASM blob on-demand -- this is all very similar to the old fuzzer UI. Also inherited from the fuzzer UI is that the build system communicates with web clients over a WebSocket connection. When the build finishes, if `--webui` was passed (i.e. if the web server is running), the build runner does not terminate; it continues running to serve web requests, allowing interactive control of the build system. In the web interface is an overall "status" indicating whether a build is currently running, and also a list of all steps in this build. There are visual indicators (colors and spinners) for in-progress, succeeded, and failed steps. There is a "Rebuild" button which will cause the build system to reset the state of every step (note that this does not affect caching) and evaluate the step graph again. If `--time-report` is passed to `zig build`, a new section of the interface becomes visible, which associates every build step with a "time report". For most steps, this is just a simple "time taken" value. However, for `Compile` steps, the compiler communicates with the build system to provide it with much more interesting information: time taken for various pipeline phases, with a per-declaration and per-file breakdown, sorted by slowest declarations/files first. This feature is still in its early stages: the data can be a little tricky to understand, and there is no way to, for instance, sort by different properties, or filter to certain files. However, it has already given us some interesting statistics, and can be useful for spotting, for instance, particularly complex and slow compile-time logic. Additionally, if a compilation uses LLVM, its time report includes the "LLVM pass timing" information, which was previously accessible with the (now removed) `-ftime-report` compiler flag. To make time reports more useful, ZIR and compilation caches are ignored by the Zig compiler when they are enabled -- in other words, `Compile` steps *always* run, even if their result should be cached. This means that the flag can be used to analyze a project's compile time without having to repeatedly clear cache directory, for instance. However, when using `-fincremental`, updates other than the first will only show you the statistics for what changed on that particular update. Notably, this gives us a fairly nice way to see exactly which declarations were re-analyzed by an incremental update. If `--fuzz` is passed to `zig build`, another section of the web interface becomes visible, this time exposing the fuzzer. This is quite similar to the fuzzer UI this commit replaces, with only a few cosmetic tweaks. The interface is closer than before to supporting multiple fuzz steps at a time (in line with the overall strategy for this build UI, the goal will be for all of the fuzz steps to be accessible in the same interface), but still doesn't actually support it. The fuzzer UI looks quite different under the hood: as a result, various bugs are fixed, although other bugs remain. For instance, viewing the source code of any file other than the root of the main module is completely broken (as on master) due to some bogus file-to-module assignment logic in the fuzzer UI. Implementation notes: * The `lib/build-web/` directory holds the client side of the web UI. * The general server logic is in `std.Build.WebServer`. * Fuzzing-specific logic is in `std.Build.Fuzz`. * `std.Build.abi` is the new home of `std.Build.Fuzz.abi`, since it now relates to the build system web UI in general. * The build runner now has an **actual** general-purpose allocator, because thanks to `--watch` and `--webui`, the process can be arbitrarily long-lived. The gpa is `std.heap.DebugAllocator`, but the arena remains backed by `std.heap.page_allocator` for efficiency. I fixed several crashes caused by conflation of `gpa` and `arena` in the build runner and `std.Build`, but there may still be some I have missed. * The I/O logic in `std.Build.WebServer` is pretty gnarly; there are a *lot* of threads involved. I anticipate this situation improving significantly once the `std.Io` interface (with concurrency support) is introduced.
2025-07-07llvm: Use emulated TLS when appropriate for the targetAlex Rønne Petersen
Closes #24236.
2025-07-01llvm: Disable the machine outliner pass on RISC-VAlex Rønne Petersen
2025-04-09compiler: Allow using LLVM's SPIR-V backend.Alex Rønne Petersen
2025-04-04compiler: Updates for LLVM/Clang 20 API changes.Alex Rønne Petersen
2025-02-27Move the compiler's LLVM bitcode builder to std.zig.llvm.Alex Rønne Petersen
2025-02-22llvm: Use inline variants of memcpy/memset intrinsics when using -fno-builtin.Alex Rønne Petersen
This is a correctness issue: When -fno-builtin is used, we must assume that we could be compiling the memcpy/memset implementations, so generating calls to them is problematic.
2025-02-22llvm.Builder: Update some intrinsic definitions for LLVM 19.Alex Rønne Petersen
2025-02-03compiler,std: implement ZON supportMason Remaley
This commit allows using ZON (Zig Object Notation) in a few ways. * `@import` can be used to load ZON at comptime and convert it to a normal Zig value. In this case, `@import` must have a result type. * `std.zon.parse` can be used to parse ZON at runtime, akin to the parsing logic in `std.json`. * `std.zon.stringify` can be used to convert arbitrary data structures to ZON at runtime, again akin to `std.json`.
2025-01-16all: update to `std.builtin.Type.{Pointer,Array,StructField}` field renamesmlugg
2024-12-27llvm: fix UB in metadata printerDavid Rubin
2024-11-03Merge pull request #21599 from alexrp/thumb-portingAlex Rønne Petersen
2024-11-03llvm: Update the list of address spaces for LLVM 19.Alex Rønne Petersen
Mainly affects amdgcn.
2024-11-02llvm: Remove extraneous commas for branch hint metadata in textual IR output.Alex Rønne Petersen
2024-10-31zig_llvm: Reduce our exposure to LLVM API breakage.Alex Rønne Petersen
LLVM recently introduced new Triple::ArchType members in 19.1.3 which broke our static assertions in zig_llvm.cpp. When implementing a fix for that, I realized that we don't even need a lot of the stuff we have in zig_llvm.(cpp,h) anymore. This commit trims the interface down considerably.
2024-10-23Merge pull request #21758 from kcbanner/dll_storage_classAndrew Kelley
Add `is_dll_import` to @extern, to support `__declspec(dllimport)` with the MSVC ABI
2024-10-22Cause a compilation error to occur if using @extern with is_dll_import in a ↵kcbanner
comptime scope. Add a note about thread local / dll import being the cause.
2024-10-19compiler: introduce new `CallingConvention`mlugg
This commit begins implementing accepted proposal #21209 by making `std.builtin.CallingConvention` a tagged union. The stage1 dance here is a little convoluted. This commit introduces the new type as `NewCallingConvention`, keeping the old `CallingConvention` around. The compiler uses `std.builtin.NewCallingConvention` exclusively, but when fetching the type from `std` when running the compiler (e.g. with `getBuiltinType`), the name `CallingConvention` is used. This allows a prior build of Zig to be used to build this commit. The next commit will update `zig1.wasm`, and then the compiler and standard library can be updated to completely replace `CallingConvention` with `NewCallingConvention`. The second half of #21209 is to remove `@setAlignStack`, which will be implemented in another commit after updating `zig1.wasm`.
2024-09-19zig_llvm: Update to LLVM 19.Alex Rønne Petersen
2024-09-12Replace deprecated default initializations with decl literalsLinus Groh
2024-09-04Merge pull request #21257 from mlugg/computed-goto-3Andrew Kelley
compiler: implement labeled switch/continue
2024-09-01Builder: add `indirectbr` llvm instructionJacob Young
2024-09-01LLVM: Remove cpp bindings for setPICLevel, setPIELevel and setCodeModelantlilja
2024-09-01LLVM: Emit module flags through Builder instead of LLVM APIantlilja
2024-08-30Merge pull request #21224 from alexrp/mips-gnu-fixesAndrew Kelley
Fix MIPS PIC level and work around an LLVM bug for `mips(el)-linux-gnueabi(hf)`
2024-08-30llvm: Pass EmitOptions to libzigcpp by pointer.Alex Rønne Petersen
Passing it by value means that bringup on new architectures is harder for no real benefit. Passing it by pointer allows to get the compiler running without needing to figure out the C calling convention details first. This manifested in practice on LoongArch, for example.
2024-08-29compiler: avoid field/decl name conflictsmlugg
Most of the required renames here are net wins for readaibility, I'd say. The ones in `arch` are a little more verbose, but I think better. I didn't bother renaming the non-conflicting functions in `arch/arm/bits.zig` and `arch/aarch64/bits.zig`, since these backends are pretty bit-rotted anyway AIUI.
2024-08-28llvm.Builder: revert adding !nosanitize APIAndrew Kelley
It's not actually useful after all.
2024-08-28llvm.Builder: add !nosanitize APIAndrew Kelley
see #20992 Co-authored-by: Jacob Young <jacobly0@users.noreply.github.com>
2024-08-28std: update `std.builtin.Type` fields to follow naming conventionsmlugg
The compiler actually doesn't need any functional changes for this: Sema does reification based on the tag indices of `std.builtin.Type` already! So, no zig1.wasm update is necessary. This change is necessary to disallow name clashes between fields and decls on a type, which is a prerequisite of #9938.
2024-08-28llvm: Disable FastISel on MIPS as a workaround for #21215.Alex Rønne Petersen
Until llvm/llvm-project#106231 trickles down.
2024-08-28llvm: Set PIC level 1 for MIPS.Alex Rønne Petersen
For hysterical raisins, MIPS always uses 1, regardless of `-fpic` vs `-fPIC`.
2024-08-27compiler: implement `@branchHint`, replacing `@setCold`mlugg
Implements the accepted proposal to introduce `@branchHint`. This builtin is permitted as the first statement of a block if that block is the direct body of any of the following: * a function (*not* a `test`) * either branch of an `if` * the RHS of a `catch` or `orelse` * a `switch` prong * an `or` or `and` expression It lowers to the ZIR instruction `extended(branch_hint(...))`. When Sema encounters this instruction, it sets `sema.branch_hint` appropriately, and `zirCondBr` etc are expected to reset this value as necessary. The state is on `Sema` rather than `Block` to make it automatically propagate up non-conditional blocks without special handling. If `@panic` is reached, the branch hint is set to `.cold` if none was already set; similarly, error branches get a hint of `.unlikely` if no hint is explicitly provided. If a condition is comptime-known, `cold` hints from the taken branch are allowed to propagate up, but other hints are discarded. This is because a `likely`/`unlikely` hint just indicates the direction this branch is likely to go, which is redundant information when the branch is known at comptime; but `cold` hints indicate that control flow is unlikely to ever reach this branch, meaning if the branch is always taken from its parent, then the parent is also unlikely to ever be reached. This branch information is stored in AIR `cond_br` and `switch_br`. In addition, `try` and `try_ptr` instructions have variants `try_cold` and `try_ptr_cold` which indicate that the error case is cold (rather than just unlikely); this is reachable through e.g. `errdefer unreachable` or `errdefer @panic("")`. A new API `unwrapSwitch` is introduced to `Air` to make it more convenient to access `switch_br` instructions. In time, I plan to update all AIR instructions to be accessed via an `unwrap` method which returns a convenient tagged union a la `InternPool.indexToKey`. The LLVM backend lowers branch hints for conditional branches and switches as follows: * If any branch is marked `unpredictable`, the instruction is marked `!unpredictable`. * Any branch which is marked as `cold` gets a `llvm.assume(i1 true) [ "cold"() ]` call to mark the code path cold. * If any branch is marked `likely` or `unlikely`, branch weight metadata is attached with `!prof`. Likely branches get a weight of 2000, and unlikely branches a weight of 1. In `switch` statements, un-annotated branches get a weight of 1000 as a "middle ground" hint, since there could be likely *and* unlikely *and* un-annotated branches. For functions, a `cold` hint corresponds to the `cold` function attribute, and other hints are currently ignored -- as far as I can tell LLVM doesn't really have a way to lower them. (Ideally, we would want the branch hint given in the function to propagate to call sites.) The compiler and standard library do not yet use this new builtin. Resolves: #21148
2024-08-27llvm.Builder: add support for more instruction metadataAndrew Kelley
mlugg: this is cherry-picked from Andrew's nosanitize branch (with Jacob's fixes squashed in) since I needed this for `unpredictable` and `prof` metadata. The nosanitize-specific changes are reverted in the next commit. Co-authored-by: Jacob Young <jacobly0@users.noreply.github.com>
2024-08-13nvptx: add implementations for GPU builtinsRobin Voetter
2024-07-23LLVM: more fine-grained sancov emit optionsAndrew Kelley
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-22initial support for integrated fuzzingAndrew Kelley
* 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-21Revert "Merge pull request #20380 from tau-dev/master"Andrew Kelley
This reverts commit 397be0c9cc8156d38d1487a4c80210007033cbd0, reversing changes made to 18d412ab2fb7bda92f7bfbdf732849bbcd066c33. Caused test failures in master branch.
2024-07-19llvm: add pass-by-reference info to debug typesTau
Without this data, debugger expressions try to pass structs by-value, which mostly just crashes. Also: mark enums as enum classes to prevent the enumerators from shadowing other identifiers.
2024-07-19llvm: Do not generate static member definitionsTau
They were not helping LLDB and actively throwing off GDB. Also: clean up some llvm.Builder and llvm.ir definitions that are no longer necessary.
2024-07-19llvm: encode variables as DW_TAG_imported_declarationTau
Now we get working global variable lookup in GDB! LLDB still re-mangles, and it looks like we can't do much about that for now. Also: translate non-owning type declarations into typedefs.
2024-07-19llvm: set precise scopes on namespace types and variablesTau
This will allow accessing non-local declarations from debuggers, which, AFAICT, was impossible before. Getting scopes right already works for type declarations and functions, but will need some fiddling for variables: For those, I tried imitating what Clang does for static member variables, but LLDB tries to re-mangle those and then fails at lookup, while GDB outright crashes. Hopefully I can find some other dwarven incantation to do the right thing.
2024-07-13Builder: fix llvm ir syntaxJacob Young
2024-07-03LLVM Builder: Pass correct argument to ensureUnusedMetadataCapacityantlilja
The trail_len was being multiplied by the size of the type before
2024-06-05LLVM backend: loongarch64 supportAndrew Kelley
2024-05-08add a debug subcommand for printing LLVM integer type alignmentAndrew Kelley
Useful when debugging why upgrading from LLVM 17 to 18 caused C ABI regressions. Turns out LLVM 18 does the following insane thing: ```diff -[nix-shell:~/dev/zig/build-llvm17]$ stage4/bin/zig llvm-ints i386-linux-musl +[nix-shell:~/src/zig/build-llvm18]$ stage4/bin/zig llvm-ints i386-linux-musl LLVMABIAlignmentOfType(i1) == 1 LLVMABIAlignmentOfType(i8) == 1 LLVMABIAlignmentOfType(i16) == 2 LLVMABIAlignmentOfType(i32) == 4 LLVMABIAlignmentOfType(i64) == 4 -LLVMABIAlignmentOfType(i128) == 4 -LLVMABIAlignmentOfType(i256) == 4 +LLVMABIAlignmentOfType(i128) == 16 +LLVMABIAlignmentOfType(i256) == 16 ```
2024-05-08add detect-cpu subcommand for debugging CPU featuresAndrew Kelley
This brings back `detectNativeCpuWithLLVM` so that we can troubleshoot during LLVM upgrades. closes #19793
2024-05-08update for LLVM 18 new target dataAndrew Kelley
New OSs: * XROS * Serenity * Vulkan Removed OSs: * Ananas * CloudABI * Minix * Contiki New CPUs: * spirv The removed stuff is removed from LLVM but not Zig.
2024-04-25LLVM: Remove deprecated or soon to be deprecated constant expressionsantlilja
2024-04-06Builder: fix encoding big integers in bitcodeJacob Young
Closes #19543