1789 Commits

Author SHA1 Message Date
Pyry Kovanen
4163126c01 llvm: always include debug information for global variables 2024-05-09 16:02:03 -07:00
Jakub Konka
d3ba541034 codegen/llvm: handle missing Apple targets 2024-05-09 22:00:17 +02:00
Jakub Konka
2e1fc0dd14 handle visionos target OS tag in the compiler
* rename .xros to .visionos as agreed in the tracking issue
* add support for VisionOS platform in the MachO linker
2024-05-09 15:04:15 +02:00
Andrew Kelley
e9efed9ed1 LLVM: zeroext/signext does happen on macos
Fixes a regression introduced in 3ce7fee9dd8bbb6f56e47758a9a8ada028400c71.
2024-05-08 19:37:29 -07:00
Andrew Kelley
88ada2121f LLVM: fix x86 and x86_64 datalayout string calculation
now it matches clang again
2024-05-08 19:37:29 -07:00
Andrew Kelley
708894cf99 add a debug subcommand for printing LLVM integer type alignment
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-08 19:37:29 -07:00
Andrew Kelley
6730b366a0 LLVM backend: no more signext on aarch64
Clang doesn't do it, so Zig must not do it in order to match the C ABI.
2024-05-08 19:37:29 -07:00
Andrew Kelley
6986d2aca9 x86_64 sysv C ABI: fix f128 param and return types
Clang 17 passed struct{f128} parameters using rdi and rax, while Clang
18 matches GCC 13.2 behavior, passing them using xmm0.

This commit makes Zig's LLVM backend match Clang 18 and GCC 13.2. The
commit deletes a hack in x86_64/abi.zig which miscategorized f128 as
"memory" which obviously disagreed with the spec.
2024-05-08 19:37:29 -07:00
Andrew Kelley
b7799ef322 std.Target.maxIntAlignment: move to compiler implementation
This should not be a public API, and the x86 backend does not support
the value 16.
2024-05-08 19:37:29 -07:00
Andrew Kelley
78002dbe47 add detect-cpu subcommand for debugging CPU features
This brings back `detectNativeCpuWithLLVM` so that we can troubleshoot
during LLVM upgrades.

closes #19793
2024-05-08 19:37:29 -07:00
Andrew Kelley
65bea9ac07 LLVM 18 update: avoid passing vectors sometimes
LLVM now refuses to lower arguments and return values on x86 targets
when the total vector bit size is >= 512.

This code detects such a situation and uses byref instead of byval.
2024-05-08 19:37:28 -07:00
Andrew Kelley
d34fae26d5 LLVM 18 std lib updates and fixes
* some manual fixes to generated CPU features code. In the future it
  would be nice to make the script do those automatically.

* add to various target OS switches. Some of the values I was unsure of
  and added TODO panics, for example in the case of spirv CPU arch.
2024-05-08 19:37:28 -07:00
Andrew Kelley
243ae3a6cb update for LLVM 18 new target data
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-05-08 19:37:28 -07:00
mlugg
db890dbae7 InternPool: eliminate var_args_param_type
This was a "fake" type used to handle C varargs parameters, much like
generic poison. In fact, it is treated identically to generic poison in
all cases other than one (the final coercion of a call argument), which
is trivially special-cased. Thus, it makes sense to remove this special
tag and instead use `generic_poison_type` in its place. This fixes
several bugs in Sema related to missing handling of this tag.

Resolves: #19781
2024-05-04 22:03:56 +01:00
Anton Lilja
20b9b54e6b
LLVM: Fix panic when using tagged union backed by enum with negative values 2024-05-02 14:15:44 +00:00
Jacob Young
e8dd79ca67 cbe: fix ub with integer @abs 2024-04-30 22:09:54 -07:00
Andrew Kelley
1c9bb6a79d C backend: avoid memcpy when len=0
As of Clang 18, calling memcpy() with a misaligned pointer trips UBSAN,
even if the length is zero. This unfortunately includes any call to
`@memcpy` when source or destination are undefined and the length is
zero.

This patch makes the C backend avoid calling memcpy when the length is
zero, thereby avoiding undefined behavior.

A zig1.wasm update will be needed in the llvm18 branch to activate this
code.
2024-04-30 10:00:53 -07:00
Julian
4303400e47
Sema+llvm: properly implement Interrupt callconv
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2024-04-28 22:43:00 +00:00
antlilja
c231d94960 LLVM: Remove deprecated or soon to be deprecated constant expressions 2024-04-25 22:58:47 -07:00
Travis Staloch
8af59d1f98 ComptimeStringMap: return a regular struct and optimize
this patch renames ComptimeStringMap to StaticStringMap, makes it
accept only a single type parameter, and return a known struct type
instead of an anonymous struct.  initial motivation for these changes
was to reduce the 'very long type names' issue described here
https://github.com/ziglang/zig/pull/19682.

this breaks the previous API.  users will now need to write:
`const map = std.StaticStringMap(T).initComptime(kvs_list);`

* move `kvs_list` param from type param to an `initComptime()` param
* new public methods
  * `keys()`, `values()` helpers
  * `init(allocator)`, `deinit(allocator)` for runtime data
  * `getLongestPrefix(str)`, `getLongestPrefixIndex(str)` - i'm not sure
     these belong but have left in for now incase they are deemed useful
* performance notes:
  * i posted some benchmarking results here:
    https://github.com/travisstaloch/comptime-string-map-revised/issues/1
  * i noticed a speedup reducing the size of the struct from 48 to 32
    bytes and thus use u32s instead of usize for all length fields
  * i noticed speedup storing KVs as a struct of arrays
  * latest benchmark shows these wall_time improvements for
    debug/safe/small/fast builds: -6.6% / -10.2% / -19.1% / -8.9%. full
    output in link above.
2024-04-22 15:31:41 -07:00
Jacob Young
5d745d94fb x86_64: fix C abi for unions
Closes #19721
2024-04-22 15:24:29 -07:00
Jacob Young
ebce190321 llvm: fix debug info when running tests 2024-04-19 12:13:29 -07:00
mlugg
d0e74ffe52
compiler: rework comptime pointer representation and access
We've got a big one here! This commit reworks how we represent pointers
in the InternPool, and rewrites the logic for loading and storing from
them at comptime.

Firstly, the pointer representation. Previously, pointers were
represented in a highly structured manner: pointers to fields, array
elements, etc, were explicitly represented. This works well for simple
cases, but is quite difficult to handle in the cases of unusual
reinterpretations, pointer casts, offsets, etc. Therefore, pointers are
now represented in a more "flat" manner. For types without well-defined
layouts -- such as comptime-only types, automatic-layout aggregates, and
so on -- we still use this "hierarchical" structure. However, for types
with well-defined layouts, we use a byte offset associated with the
pointer. This allows the comptime pointer access logic to deal with
reinterpreted pointers far more gracefully, because the "base address"
of a pointer -- for instance a `field` -- is a single value which
pointer accesses cannot exceed since the parent has undefined layout.
This strategy is also more useful to most backends -- see the updated
logic in `codegen.zig` and `codegen/llvm.zig`. For backends which do
prefer a chain of field and elements accesses for lowering pointer
values, such as SPIR-V, there is a helpful function in `Value` which
creates a strategy to derive a pointer value using ideally only field
and element accesses. This is actually more correct than the previous
logic, since it correctly handles pointer casts which, after the dust
has settled, end up referring exactly to an aggregate field or array
element.

In terms of the pointer access code, it has been rewritten from the
ground up. The old logic had become rather a mess of special cases being
added whenever bugs were hit, and was still riddled with bugs. The new
logic was written to handle the "difficult" cases correctly, the most
notable of which is restructuring of a comptime-only array (for
instance, converting a `[3][2]comptime_int` to a `[2][3]comptime_int`.
Currently, the logic for loading and storing work somewhat differently,
but a future change will likely improve the loading logic to bring it
more in line with the store strategy. As far as I can tell, the rewrite
has fixed all bugs exposed by #19414.

As a part of this, the comptime bitcast logic has also been rewritten.
Previously, bitcasts simply worked by serializing the entire value into
an in-memory buffer, then deserializing it. This strategy has two key
weaknesses: pointers, and undefined values. Representations of these
values at comptime cannot be easily serialized/deserialized whilst
preserving data, which means many bitcasts would become runtime-known if
pointers were involved, or would turn `undefined` values into `0xAA`.
The new logic works by "flattening" the datastructure to be cast into a
sequence of bit-packed atomic values, and then "unflattening" it; using
serialization when necessary, but with special handling for `undefined`
values and for pointers which align in virtual memory. The resulting
code is definitely slower -- more on this later -- but it is correct.

The pointer access and bitcast logic required some helper functions and
types which are not generally useful elsewhere, so I opted to split them
into separate files `Sema/comptime_ptr_access.zig` and
`Sema/bitcast.zig`, with simple re-exports in `Sema.zig` for their small
public APIs.

Whilst working on this branch, I caught various unrelated bugs with
transitive Sema errors, and with the handling of `undefined` values.
These bugs have been fixed, and corresponding behavior test added.

In terms of performance, I do anticipate that this commit will regress
performance somewhat, because the new pointer access and bitcast logic
is necessarily more complex. I have not yet taken performance
measurements, but will do shortly, and post the results in this PR. If
the performance regression is severe, I will do work to to optimize the
new logic before merge.

Resolves: #19452
Resolves: #19460
2024-04-17 13:41:25 +01:00
Jacob Young
f1c0f42cdd cbe: fix optional codegen
Also reduce ctype pool string memory usage, remove self assignments, and
enable more warnings.
2024-04-13 01:35:20 -04:00
Jacob Young
7611d90ba0 InternPool: remove slice from byte aggregate keys
This deletes a ton of lookups and avoids many UAF bugs.

Closes #19485
2024-04-08 13:24:08 -04:00
Jacob Young
23ee39116c cbe: fix struct field location computation 2024-04-08 13:20:13 -04:00
Jacob Young
d979df585d cbe: remove threadlocal variables in single threaded mode 2024-04-08 13:20:02 -04:00
Andrew Kelley
f45ba7d0c1
Merge pull request #19562 from Snektron/spirv-remove-cache
spirv: remove cache
2024-04-06 13:03:22 -07:00
Jacob Young
4e85536604 Builder: fix encoding big integers in bitcode
Closes #19543
2024-04-06 12:53:09 -07:00
antlilja
637b1d606d LLVM Builder: Emit binary op optional flags for exact and no wrap 2024-04-06 14:57:46 -04:00
Robin Voetter
3e388faecd
spirv: yeet cache 2024-04-06 13:37:40 +02:00
Robin Voetter
ef638502d4
spirv: remove cache usage from assembler 2024-04-06 13:37:40 +02:00
Robin Voetter
97a67762ba
spirv: remove cache usage for types 2024-04-06 13:37:39 +02:00
Robin Voetter
188922a544
spirv: remove cache usage for constants 2024-04-06 13:37:39 +02:00
Robin Voetter
42c7e752e1
spirv: id range helper
This allows us to more sanely allocate a continuous
range of result-ids, and avoids a bunch of nasty
casting code in a few places. Its currently not used
very often, but will be useful in the future.
2024-04-06 13:37:37 +02:00
Ali Chraghi
436f53f55d spirv: implement @mulWithOverflow 2024-04-06 09:01:46 +03:30
Ali Chraghi
9785014938 spirv: OpExtInstImport in assembler 2024-04-06 08:52:38 +03:30
Ali Chraghi
0f75143c62 spirv: implement @divFloor, @floor and @mod 2024-04-06 08:50:02 +03:30
Ali Chraghi
14e3718723 spirv: make behavior tests passing 2024-04-05 00:13:48 +03:30
Robin Voetter
d2be725e4b
Merge pull request #19490 from Snektron/spirv-dedup
spirv: deduplication pass
2024-04-01 09:51:04 +02:00
Jacob Young
fb192df4f2 cbe: fix uncovered bugs 2024-03-30 20:50:48 -04:00
Jacob Young
5a41704f7e cbe: rewrite CType
Closes #14904
2024-03-30 20:50:48 -04:00
Jacob Young
6f10b11658 cbe: fix bugs revealed by an upcoming commit
Closes #18023
2024-03-30 20:50:48 -04:00
Robin Voetter
12350f53bf
spirv: clz, ctz for opencl
This instruction seems common in compiler_rt.
2024-03-30 19:47:55 +01:00
mlugg
2a245e3b78
compiler: eliminate TypedValue
The only logic which remained in this file was the Value printing logic.
This has been moved into a new `print_value.zig`.
2024-03-26 13:48:07 +00:00
mlugg
a61def10c6
compiler: eliminate most usages of TypedValue 2024-03-26 13:48:07 +00:00
mlugg
0d8c7ae007
Zcu.Decl: replace typedValue with valueOrFail
Now that the legacy `Value` representation is eliminated, we can begin
to phase out the redundant `TypedValue` type.
2024-03-26 13:48:07 +00:00
mlugg
26a94e8481
Zcu: eliminate Decl.alive field
Legacy anon decls now have three uses:
* Type owner decls
* Function owner decls
* `@export` and `@extern`

Therefore, there are no longer any cases where we wish to explicitly
omit legacy anon decls from the binary. This means we can remove the
concept of an "alive" vs "dead" `Decl`, which also allows us to remove
the separate `anon_work_queue` in `Compilation`.
2024-03-26 13:48:06 +00:00
mlugg
884d957b6c
compiler: eliminate legacy Value representation
Good riddance!

Most of these changes are trivial. There's a fix for a minor bug this
exposed in `Value.readFromPackedMemory`, but aside from that, it's all
just things like changing `intern` calls to `toIntern`.
2024-03-26 13:48:06 +00:00
mlugg
c6f3e9d79c
Zcu.Decl: remove ty field
`Decl` can no longer store un-interned values, so this field is now
unnecessary. The type can instead be fetched with the new `typeOf`
helper method, which just gets the type of the Decl's `Value`.
2024-03-26 13:48:06 +00:00