15399 Commits

Author SHA1 Message Date
Andrew Kelley
e084c46ed6 fix invisible merge conflict 2024-08-30 14:55:28 -07:00
Andrew Kelley
e4e91a1314
Merge pull request #21224 from alexrp/mips-gnu-fixes
Fix MIPS PIC level and work around an LLVM bug for `mips(el)-linux-gnueabi(hf)`
2024-08-30 14:47:43 -07:00
Andrew Kelley
5d08b7f054
Merge pull request #21178 from alexrp/glibc-thumb
`std.zig.target`: Remove `thumb*-linux-gnueabi*` target triples.
2024-08-30 14:35:30 -07:00
Andrew Kelley
a4cc43c42b
Merge pull request #21174 from alexrp/win-arm
Change `arm-windows-gnu` to `thumb-windows-gnu`, plus some initial port work
2024-08-30 14:34:53 -07:00
Alex Rønne Petersen
5723fcaac1 llvm: Pass EmitOptions to libzigcpp by pointer.
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-30 11:02:12 -07:00
Jacob Young
a1a9d7440c Elf: fix reference from eh_frame_hdr to eh_frame 2024-08-30 09:24:32 -04:00
Jacob Young
7305184203 dwarf: share and use std.dwarf.EH_PE constants 2024-08-30 09:24:32 -04:00
Jacob Young
f0c0f697a6 Dwarf: fix zir inst index comparison not checking the file 2024-08-30 08:21:24 -04:00
mlugg
c62487da76
compiler: avoid field/decl name conflicts
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-29 23:43:52 +01:00
Andrew Kelley
c81219c573 LLVM: use @llvm.used instead of @llvm.compiler.used
because it marks the linker section, preventing garbage collection.

Also, name the members because that is required by this intrinsic.

Also, enable the StackDepth option in the sancov pass as a workaround
for https://github.com/llvm/llvm-project/pull/106464, otherwise, LLVM
enables TracePCGuard even though we explicitly disable it.
2024-08-28 18:07:13 -07:00
Andrew Kelley
9e11c4f60e LLVM: put sancov globals into llvm.compiler.used
This matches what LLVM's sancov pass does and is required so that
optimization passes do not delete the instrumentation.

However, this is currently triggering an error: "members of
llvm.compiler.used must be named" so the next commit will add names to
those globals.
2024-08-28 18:07:13 -07:00
Andrew Kelley
1bec824cad LLVM: disable inline 8-bit counters when using trace pc guard 2024-08-28 18:07:13 -07:00
Andrew Kelley
88bba4c154 LLVM: enable sancov pass partially
It's useful to have TraceCmp based on the results of LLVM optimizations,
while the code coverage bits were emitted by Zig manually, allowing more
careful correlation to points of interest in the source code.

This re-enables the sancov pass in `-ffuzz` mode, but only TraceCmp.

Notably, IndirectCalls is off, which needs to be implemented manually in
the LLVM backend, and StackDepth remains off, because it is not used by
libfuzzer or AFL either.

If stack depth is re-introduced, it can be done with better performance
characteristics by being function call graph aware, and only lowered in
call graph cycles, where its heuristic properties come in useful.

Fixes the fuzzing regression.
2024-08-28 18:07:13 -07:00
Andrew Kelley
b8d99a3323 implement code coverage instrumentation manually
instead of relying on the LLVM sancov pass. The LLVM pass is still
executed if trace_pc_guard is requested, disabled otherwise. The LLVM
backend emits the instrumentation directly.

It uses `__sancov_pcs1` symbol name instead of `__sancov_pcs` because
each element is 1 usize instead of 2.

AIR: add CoveragePoint to branch hints which indicates whether those
branches are interesting for code coverage purposes.

Update libfuzzer to use the new instrumentation. It's simplified since
we no longer need the constructor and the pcs are now in a continguous
list.

This is a regression in the fuzzing functionality because the
instrumentation for comparisons is no longer emitted, resulting in worse
fuzzer inputs generated. A future commit will add that instrumentation
back.
2024-08-28 18:07:13 -07:00
Andrew Kelley
43dc8db068 print_air: print cond_br branch hints 2024-08-28 18:07:13 -07:00
Andrew Kelley
a3d622bdd6 llvm.Builder: revert adding !nosanitize API
It's not actually useful after all.
2024-08-28 18:07:13 -07:00
Andrew Kelley
df52073681 llvm.Builder: add !nosanitize API
see #20992

Co-authored-by: Jacob Young <jacobly0@users.noreply.github.com>
2024-08-28 18:07:13 -07:00
mlugg
aaa7e73983
link.Dwarf: skip tests in updateComptimeNav
These won't live in the parent namespace as decls which causes problems
later in this function, and tests are guaranteed not to be referenced at
comptime anyway, so there's actually no need to run this logic.
2024-08-28 08:39:59 +01:00
mlugg
0fe3fd01dd
std: update std.builtin.Type fields to follow naming conventions
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-28 08:39:59 +01:00
Alex Rønne Petersen
e585103306
llvm: Disable FastISel on MIPS as a workaround for #21215.
Until llvm/llvm-project#106231 trickles down.
2024-08-28 06:19:01 +02:00
Alex Rønne Petersen
7d9edff11d
llvm: Set PIC level 1 for MIPS.
For hysterical raisins, MIPS always uses 1, regardless of `-fpic` vs `-fPIC`.
2024-08-28 06:18:06 +02:00
Alex Rønne Petersen
338730d2fa
link.Coff: Set default image base correctly for thumb. 2024-08-28 03:08:10 +02:00
Alex Rønne Petersen
6d15dc1ab8
mingw: Fix various target checks to check for thumb.
Also remove mentions of arm, armeb, thumbeb, and aarch64_be; none of these are
relevant for Windows.
2024-08-28 03:08:10 +02:00
Alex Rønne Petersen
694004ed40 glibc: Remove mentions of thumb.
We no longer expose `thumb*-linux-gnueabi*` target triples in std.zig.target.
2024-08-28 02:57:31 +02:00
Jacob Young
f289b82d0e Dwarf: implement .eh_frame 2024-08-27 03:55:56 -04:00
Jacob Young
26d4fd5276 Zcu: avoid trying to link failed container types and contained navs 2024-08-27 02:09:59 -04:00
Jacob Young
8c3f6c72c0 Dwarf: fix and test string format 2024-08-27 02:09:59 -04:00
mlugg
93cb44c805 translate-c: support GCC/Clang pointer subtraction extension
Pointer subtraction on `void *` or function pointers is UB by the C
spec, but is permitted by GCC and Clang as an extension. So, avoid
crashing translate-c in such cases, and follow the extension behavior --
there's nothing else that could really be intended.
2024-08-27 06:43:40 +01:00
Matthew Lugg
d3c6f7179c
Merge pull request #21214 from mlugg/branch-hint-and-export
Implement `@branchHint` and new `@export` usage
2024-08-27 06:10:56 +01:00
Andrew Kelley
d9147b91a6
Merge pull request #21212 from ziglang/elf-incr
elf: cleanups, cleanups, cleanups
2024-08-26 19:25:23 -07:00
mlugg
6808ce27bd
compiler,lib,test,langref: migrate @setCold to @branchHint 2024-08-27 00:44:35 +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
Jakub Konka
1254509d78 elf: make Atom.allocate and related ZigObject-independent 2024-08-26 22:19:51 +02: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
Jakub Konka
20240e9cd5 elf: store atom refs for rela sections until we can do better 2024-08-25 10:20:15 +02:00
Jakub Konka
1af0f4cd00 elf: fix setting sh_info and sh_link for manually managed rela sections 2024-08-25 10:20:15 +02:00