Commit Graph

30799 Commits

Author SHA1 Message Date
mlugg
a3a737e9a6
lib,test,tools,doc: update usages of @export 2024-08-27 00:44:35 +01:00
mlugg
22f65c3dd5
stage1: update zig1.wasm
This update implements both `@branchHint` and the new usage of `@export`
into zig1.

Signed-off-by: mlugg <mlugg@mlugg.co.uk>
2024-08-27 00:44:06 +01:00
mlugg
457c94d353
compiler: implement @branchHint, replacing @setCold
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-27 00:41:49 +01:00
Andrew Kelley
72e00805a6
llvm.Builder: add support for more instruction metadata
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-27 00:41:49 +01:00
mlugg
f2d7096bb9
compiler: make @export take a pointer
Resolves: #14911
2024-08-27 00:41:04 +01:00
Matthew Lugg
492cc2ef8d
Merge pull request #21208 from Rexicon226/pt-begone
Cleanup type resolution and finish `zcu` rename
2024-08-26 23:53:01 +01:00
David Rubin
61e8a6c008
remove some stale code
- Don't create an `inner_sema` in `unionFields`
- Remove assertions of the sema owner, when we literally just set it
2024-08-26 12:27:34 -07:00
David Rubin
f777b29832
fix up merge conflicts with master 2024-08-25 22:43:57 -07:00
David Rubin
ce92ccccc9
sema: resolve{Struct,Union}Inner don't throw away Semas
before this, calls to `resolveTypeFieldsStruct` (now renamed to the more correct `resolveStructFieldTypes`) would just throw away the sema that `resolveStructInner` created and create its own. There is no reason to do this, and we fix it to preserve the sema through it all.
2024-08-25 15:18:20 -07:00
David Rubin
472f3ac419
zcu: add some documentation around Zcu.Feature 2024-08-25 15:17:41 -07:00
David Rubin
1c1feba08e
remove mod aliases for Zcus 2024-08-25 15:17:40 -07:00
David Rubin
9868ed44b3
macho: remove deprecated Module usages 2024-08-25 15:17:23 -07:00
David Rubin
889a324e77
comp: remove an, unencouraged... pattern 2024-08-25 15:17:23 -07:00
David Rubin
863f74dcd2
comp: rename module to zcu 2024-08-25 15:17:21 -07:00
David Rubin
bb531759bc
coff: remove deprecated Module usages 2024-08-25 15:16:46 -07:00
David Rubin
8d2b495b2a
value: remove deprecated Module usages 2024-08-25 15:16:46 -07:00
David Rubin
80cd53d3bb
sema: clean-up {union,struct}FieldAlignment and friends
My main gripes with this design were that it was incorrectly namespaced, the naming was inconsistent and a bit wrong (`fooAlign` vs `fooAlignment`).

This commit moves all the logic from `PerThread.zig` to use the zcu + tid system that the previous couple commits introduce.
I've organized and merged the functions to be a bit more specific to their own purpose.

- `fieldAlignment` takes a struct or union type, an index, and a Zcu (or the Sema version which takes a Pt), and gives you the alignment of the field at the index.
- `structFieldAlignment` takes the field type itself, and provides the logic to handle special cases, such as externs.

A design goal I had in mind was to avoid using the word 'struct' in the function name, when it worked for things that aren't structs, such as unions.
2024-08-25 15:16:46 -07:00
David Rubin
b4bb64ce78
sema: rework type resolution to use Zcu when possible 2024-08-25 15:16:42 -07:00
Andrew Kelley
849c31a6cc
Merge pull request #21177 from alexrp/elf-coff-conv
`std.{coff,elf}`: Remove the `{MachineType,EM}.toTargetCpuArch()` functions.
2024-08-25 14:49:53 -07:00
mlugg
7d54c62c8a incremental: fix adding/removing aggregate fields
I don't recall why I put these checks here -- they aren't correct. We
can freely recreate a type even if its fields have changed, because we
are going to re-do all type resolution.

The only conditions for recreations are (a) the ZIR index must not be
lost and (b) the number of captures must be the same. These conditions
are permissible because if either is violated, we can guarantee that
analysis of a valid `zirStructDecl` (etc) will never reference this
type (since the ZIR index has just been tracked, and the captures have
just been created based on the ZIR).

Adds a corresponding test case.

Resolves: #21185
2024-08-24 20:30:52 +01:00
Jari Vetoniemi
2d7c26cc66 ElfDynLib: resolve lib from system paths
Implements the base that should usually work that is
- Check LD_LIBRARY_PATH if the binary is no setuid setgid binary
- Check /lib, /usr/lib, in that order

The missing parts are:
- DT_RPATH and DT_RUNPATH handling from the calling executable
- Reading /etc/ld.so.cache

For more details check man page of dlopen(3)
2024-08-24 05:32:53 -07:00
Prokop Randáček
9374725088
port cachegrind.h to zig (#19241)
* port cachegrind.h to zig
* import cachegrind.zig in valgrind.zig
* Avoid Redundant Names in Fully-Qualified Namespaces
2024-08-23 22:59:30 -07:00
Alex Rønne Petersen
ab69482a5d std.fs: Disable file operations on directories test on WASI.
https://github.com/ziglang/zig/issues/20747
2024-08-23 22:43:03 -07:00
Alex Rønne Petersen
cad69e2c29 std.Target.Query: Fix parse test on ABIs like gnueabi, gnuabi64, etc.
The `zigTriple()` implementation simply returns `gnu` when a glibc version is provided but a more specific ABI isn't.
2024-08-23 22:39:05 -07:00
Andrew Kelley
85747b266a Revert "Smaller memory footprint for BoundedArray (#16299)"
This reverts commit cb5a6be41a.

I deeply apologize for the churn.

This change is problematic given that we do not have ranged integers
(yet? see #3806).

In the meantime, this type needs to be `usize`, matching the length and
index types for all std lib data structures.

Users who want to save memory should not use heap-allocated BoundedArray
values, since it is inherently memory-inefficient. Use a different
memory layout instead.

If #3806 is accepted and implemented, the length value can become an
integer with the appropriate range, without the footgun. If that
proposal is not accepted, len type will remain a usize.
2024-08-23 22:30:10 -07:00
Andrew Kelley
d9e8671d96 fix merge conflict from previous commit 2024-08-23 21:46:36 -07:00
Eric Joldasov
03c297eea6 std.Build: call handleVerbose2 in runAllowFail
Ensures that all runned command are visible when using `--verbose` flag,
for example `pkg-config` from Step.Compile or `git describe` from build.zig.

Signed-off-by: Eric Joldasov <bratishkaerik@landless-city.net>
2024-08-23 21:45:32 -07:00
Andrew Kelley
9848318725 fix autodocs regression FTBFS
regressed in dffc8c44f9 since there is no
test coverage for the `zig std` command yet.

closes #21180
2024-08-23 19:23:38 -07:00
Alex Rønne Petersen
5dd2bb525d glibc: Define _IO_stdin_used in start code and reference it in stub asm.
This is necessary to inform the real, non-stub glibc that a program built with
Zig is using a modern `FILE` structure, i.e. glibc 2.1+. This is particularly
important on lesser-used architectures where the legacy code is poorly tested;
for example, glibc 2.40 introduced a regression for the legacy case in the
libio cleanup code, causing all Zig-compiled MIPS binaries to crash on exit.
2024-08-23 11:09:20 -07:00
Alex Rønne Petersen
fb6f5a30b2
link: Rename InvalidCpuArch error to InvalidMachineType. 2024-08-23 19:56:29 +02:00
Alex Rønne Petersen
a69f55a7cc
std.{coff,elf}: Remove the {MachineType,EM}.toTargetCpuArch() functions.
These are fundamentally incapable of producing accurate information for reasons
I've laid out in #20771. Since our only use of these functions is to check that
object files have the correct machine type, and since #21020 made
`std.Target.to{Coff,Elf}Machine()` more accurate, just switch these checks over
to that and compare the machine type tags instead.

Closes #20771.
2024-08-23 19:56:24 +02:00
bilaliscarioth
df6907f601
openbsd: adding EPERM for msync() against hitting a immutable region page (#18701) 2024-08-23 14:28:00 +00:00
Michał Drozd
206e5e4d7d
std.os.linux: Fix bunch of compilation errors (#21138)
* Correct layout of IntInfo according to https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-int

* Fix VFS errors

* Fix std.os.linux.sendmmsg

* Fix std.os.linux.sigismember. Add tests

* Fix futex2 functions
2024-08-23 14:21:19 +00:00
bilaliscarioth
dc72138b91 Update c.zig, openbsd don't have getcontext 2024-08-23 07:19:04 -07:00
Alex Rønne Petersen
3fb6e46f6e glibc: Pass -Qunused-arguments when building libc_nonshared.a.
For some platforms, the math-related flags are ignored and produce warnings.
There's nothing we can do about that, so just silence them.
2024-08-23 00:59:06 -07:00
Alex Rønne Petersen
945fc7064b glibc: Add missing xstatver.h for some linux architectures.
Specifically for alpha, arm, hppa, microblaze, and sh.

Closes #20054.
2024-08-23 00:58:16 -07:00
Andrew Kelley
60011d28d3
Merge pull request #21137 from Aransentin/af_packet
std.os.linux: Add support for AF_PACKET V3
2024-08-23 00:46:18 -07:00
Andrew Kelley
56119c2cbc
Merge pull request #21118 from alexrp/thread-porting
`std.Thread`: Support hexagon, sparc32, s390x in `freeAndExit()`
2024-08-23 00:41:50 -07:00
Jacob Young
c6286eee4b
Merge pull request #21170 from jacobly0/more-dwarf-cleanup
Dwarf: more cleanup
2024-08-22 23:20:53 -04:00
Andrew Kelley
ee84deda98
Merge pull request #21095 from alexrp/mips64-tests
Get `mips64(el)-linux` working and start testing it
2024-08-22 20:09:08 -07:00
Jacob Young
0a52914355 Elf: all dwarf relocs need to become linker relocs 2024-08-22 20:08:05 -04:00
Jacob Young
17171cdb07 Dwarf: fix big and zero enum values 2024-08-22 20:08:05 -04:00
Jacob Young
f47e1e148e Dwarf: add more childless special cases 2024-08-22 20:08:05 -04:00
Jacob Young
874ad98f7a Dwarf: fix treating big int limbs as signed 2024-08-22 20:08:04 -04:00
Jacob Young
4a132d4bce Type: fix inconsistency between zig fmt and @typeName 2024-08-22 20:08:04 -04:00
Jacob Young
cbaff43b2a Dwarf: add missing var args info on function decls 2024-08-22 19:53:04 -04:00
Andrew Kelley
c262061129 update readme 2024-08-22 14:06:01 -07:00
Andrew Kelley
205e17a73c
Merge pull request #21161 from jedisct1/mlkem-update
Update ML-KEM to the final specification
2024-08-22 12:54:40 -07:00
Josh Wolfe
febfcbd49d
std.json.WriteStream supports streaming long values directly to the underlying stream (#21155) 2024-08-22 08:26:14 -04:00
Jacob Young
31220b50b5 Dwarf: cleanup emitted debug info
* reduce iteration cost by not tracking unused entries
 * avoid emitting unused abbrevs to `.debug_abbrev`
 * get the compiler executable passing `llvm-dwarfdump --verify`
 * make it possible to skip `.debug_line` padding much more quickly
2024-08-22 08:44:08 +02:00