686 Commits

Author SHA1 Message Date
mlugg
f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -07:00
Andrew Kelley
5288929ffd sema.addConstant: remove type parameter
Now that InternPool migration is finished, all values have types. So
only the value parameter is required.
2023-06-23 21:59:42 -07:00
Jacob Young
96cdd51c14 Type: delete legacy allocation functions 2023-06-20 14:02:09 -04:00
Andrew Kelley
a72d634b73
Merge pull request #16046 from BratishkaErik/issue-6128
Renaming `@xtoy` to `@YfromX`
2023-06-19 22:36:24 -07:00
Jacob Young
8fcc28d302 Module: add support for multiple global asm blocks per decl
Closes #16076
2023-06-19 13:12:04 -07:00
Eric Joldasov
50339f595a all: zig fmt and rename "@XToY" to "@YFromX"
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2023-06-19 12:34:42 -07:00
Motiejus Jakštys
d41111d7ef mem: rename align*Generic to mem.align*
Anecdote 1: The generic version is way more popular than the non-generic
one in Zig codebase:

     git grep -w alignForward | wc -l
    56
     git grep -w alignForwardGeneric | wc -l
    149

     git grep -w alignBackward | wc -l
    6
     git grep -w alignBackwardGeneric | wc -l
    15

Anecdote 2: In my project (turbonss) that does much arithmetic and
alignment I exclusively use the Generic functions.

Anecdote 3: we used only the Generic versions in the Macho Man's linker
workshop.
2023-06-17 12:49:13 -07:00
mlugg
11d0dfb882 Sema: fix @intToPtr of zero value to optional pointer
Calling into coercion logic here is a little opaque, and more to the
point wholly unnecessary. Instead, the (very short) logic is now
implemented directly in Sema.

Resolves: #16033
2023-06-15 02:43:06 -07:00
mlugg
c9531eb833
Sema: rewrite peer type resolution
The existing logic for peer type resolution was quite convoluted and
buggy. This rewrite makes it much more resilient, readable, and
extensible. The algorithm works by first iterating over the types to
select a "strategy", then applying that strategy, possibly applying peer
resolution recursively.

Several new tests have been added to cover cases which the old logic did
not correctly handle.

Resolves: #15138
Resolves: #15644
Resolves: #15693
Resolves: #15709
Resolves: #15752
2023-06-13 21:48:18 +01:00
mlugg
42dc7539c5
Fix bad source locations in switch capture errors
To do this, I expanded SwitchProngSrc a bit. Several of the tags there
aren't actually used by any current errors, but they're there for
consistency and if we ever need them.

Also delete a now-redundant test and fix another.
2023-06-13 12:55:27 +01:00
mlugg
00609e7edb
Eliminate switch_capture and switch_capture_ref ZIR tags
These tags are unnecessary, as this information can be more efficiently
encoded within the switch_block instruction itself. We also use a neat
little trick to avoid needing a dummy instruction (like is used for
errdefer captures): since the switch_block itself cannot otherwise be
referenced within a prong, we can repurpose its index within prongs to
refer to the captured value.
2023-06-13 12:53:18 +01:00
Jacob Young
d37ebfcf23 InternPool: avoid as many slices pointing to string_bytes as possible
These are frequently invalidated whenever a string is interned, so avoid
creating pointers to `string_bytes` wherever possible.  This is an
attempt to fix random CI failures.
2023-06-11 23:45:09 -07:00
mlugg
2a6b91874a stage2: pass most test cases under InternPool
All but 2 test cases now pass (tested on x86_64 Linux, native only). The
remaining two signify an issue requiring a larger refactor, which I will
do in a separate commit.

Notable changes:
* Fix uninitialized memory when allocating objects from free lists
* Implement TypedValue printing for pointers
* Fix some TypedValue printing logic
* Work around non-existence of InternPool.remove implementation
2023-06-10 20:51:10 -07:00
Jacob Young
e23b0a01e6 InternPool: fix yet more key lifetime issues 2023-06-10 20:47:59 -07:00
mlugg
0fd52cdc5e InternPool: avoid aggregate null bytes storage
This is a workaround for InternPool currently not handling
non-null-terminated strings. It avoids using the `bytes` storage for
aggregates if there are any null bytes.

In the future this should be changed so that the `bytes` storage can be
used regardless of whether there are any null bytes. This is important
for use cases such as `@embedFile`.

However, this fixes a bug for now, and after this commit, stage2
self-hosts again.

mlugg: stage5 passes all enabled behavior tests on my system.

Commit message edited by Andrew Kelley <andrew@ziglang.org>
2023-06-10 20:47:59 -07:00
Jacob Young
da24ea7f36 Sema: rewrite monomorphed_funcs usage
In an effort to delete `Value.hashUncoerced`, generic instantiation has
been redesigned.  Instead of just storing instantiations in
`monomorphed_funcs`, partially instantiated generic argument types are
also cached.  This isn't quite the single `getOrPut` that it used to be,
but one `get` per generic argument plus one get for the instantiation,
with an equal number of `put`s per unique instantiation isn't bad.
2023-06-10 20:47:59 -07:00
Andrew Kelley
35550c840b Module: fix populateTestFunctions UAF 2023-06-10 20:47:59 -07:00
Andrew Kelley
f1c900c72e compiler: avoid use of undefined memory
InternPool is nice in some ways but it also comes with its own set of
footguns. This commit fixes 5 instances. I see quite a few Valgrind
warnings remaining when running the behavior tests.

Perhaps the solution is to have stringToSlice return a struct with start
and length as indexes, which has a format function?
2023-06-10 20:47:59 -07:00
Andrew Kelley
69b7b91092 compiler: eliminate Decl.value_arena and Sema.perm_arena
The main motivation for this commit is eliminating Decl.value_arena.
Everything else is dominoes.

Decl.name used to be stored in the GPA, now it is stored in InternPool.
It ended up being simpler to migrate other strings to be interned as
well, such as struct field names, union field names, and a few others.
This ended up requiring a big diff, sorry about that. But the changes
are pretty nice, we finally start to take advantage of InternPool's
existence.

global_error_set and error_name_list are simplified. Now it is a single
ArrayHashMap(NullTerminatedString, void) and the index is the error tag
value.

Module.tmp_hack_arena is re-introduced (it was removed in
eeff407941560ce8eb5b737b2436dfa93cfd3a0c) in order to deal with
comptime_args, optimized_order, and struct and union fields. After
structs and unions get moved into InternPool properly, tmp_hack_arena
can be deleted again.
2023-06-10 20:47:58 -07:00
mlugg
e0179640d5 Sema: intern values of mutable decls after analysis
This is necessary with the upcoming removal of Decl.value_arena to
prevent UAF of these values.
2023-06-10 20:47:58 -07:00
mlugg
9b48fc2833 Allocate capture scopes in gpa instead of Decl.value_arena
This eliminates the last major use of value_arena, in preparation to
remove it.
2023-06-10 20:47:58 -07:00
Andrew Kelley
bb526426e7 InternPool: remove memoized_decl
This is neither a type nor a value. Simplifies `addStrLit` as well as
the many places that switch on `InternPool.Key`.

This is a partial revert of bec29b9e498e08202679aa29a45dab2a06a69a1e.
2023-06-10 20:47:58 -07:00
Andrew Kelley
82f6f164a1 InternPool: improve hashing performance
Key.PtrType is now an extern struct so that hashing it can be done by
reinterpreting bytes directly. It also uses the same representation for
type_pointer Tag encoding and the Key. Accessing pointer attributes now
requires packed struct access, however, many operations are now a copy
of a u32 rather than several independent fields.

This function moves the top two most used Key variants - pointer types
and pointer values - to use a single-shot hash function that branches
for small keys instead of calling memcpy.

As a result, perf against merge-base went from 1.17x ± 0.04 slower to
1.12x ± 0.04 slower. After the pointer value hashing was changed, total
CPU instructions spent in memcpy went from 4.40% to 4.08%, and after
additionally improving pointer type hashing, it further decreased to
3.72%.
2023-06-10 20:47:57 -07:00
Andrew Kelley
90a877f462 InternPool: pass by const pointer
The Zig language allows the compiler to make this optimization
automatically. We should definitely make the compiler do that, and
revert this commit. However, that will not happen in this branch, and I
want to continue to explore achieving performance parity with
merge-base. So, this commit changes all InternPool parameters to be
passed by const pointer rather than by value.

I measured a 1.03x ± 0.03 speedup vs the previous commit compiling the
(set of passing) behavior tests. Against merge-base, this commit is
1.17x ± 0.04 slower, which is an improvement from the previous
measurement of 1.22x ± 0.02.

Related issue: #13510
Related issue: #14129
Related issue: #15688
2023-06-10 20:47:57 -07:00
Jacob Young
27f1ad8afd Module: add allowzero canonicalization to pointer types 2023-06-10 20:47:57 -07:00
Jacob Young
5580a69d71 cbe: fix InternPool regressions 2023-06-10 20:47:56 -07:00
Jacob Young
4f70863a55 InternPool: fix various pointer issues 2023-06-10 20:47:56 -07:00
Jacob Young
3269256965 behavior: fix more compiler crashes 2023-06-10 20:47:56 -07:00
Jacob Young
3064d2aa7b behavior: additional llvm fixes 2023-06-10 20:47:56 -07:00
Jacob Young
3b6ca1d35b Module: move memoized data to the intern pool
This avoids memory management bugs with the previous implementation.
2023-06-10 20:47:56 -07:00
Jacob Young
2d5bc01469 behavior: get more test cases passing with llvm 2023-06-10 20:47:56 -07:00
Andrew Kelley
8011faa004 compiler: fix populateTestFunctions InternPool usage 2023-06-10 20:47:56 -07:00
Jacob Young
1dc01f1140 InternPool: fix build-exe and compiler-rt crashes 2023-06-10 20:47:55 -07:00
Jacob Young
9cd0ca9f48 Module: rename functions to make ownership checks explicit
This makes the difference between `decl.getOwnedFunction` and
`decl.val.getFunction` more clear when reading the code.
2023-06-10 20:47:55 -07:00
Jacob Young
abded5cbb0 TypedValue: implement more prints 2023-06-10 20:47:55 -07:00
Jacob Young
f2c716187c InternPool: fix more crashes 2023-06-10 20:47:55 -07:00
Andrew Kelley
66c4396854 AIR: eliminate the values array 2023-06-10 20:47:55 -07:00
Jacob Young
9a738c0be5 Module: intern the values of decls when they are marked alive
I'm not sure if this is the right place for this to happen, and
it should become obsolete when comptime mutation is rewritten
and the remaining legacy value tags are remove, so keeping this
as a separate revertable commit.
2023-06-10 20:47:55 -07:00
Jacob Young
70cc68e999 Air: remove constant tag
Some uses have been moved to their own tag, the rest use interned.

Also, finish porting comptime mutation to be more InternPool aware.
2023-06-10 20:47:55 -07:00
Jacob Young
1a4626d2cf InternPool: remove more legacy values
Reinstate some tags that will be needed for comptime init.
2023-06-10 20:47:54 -07:00
Jacob Young
6e0de1d116 InternPool: port most of value tags 2023-06-10 20:47:54 -07:00
Andrew Kelley
01ca841f12 Sema: improve the types_to_resolve mechanism
Store `InternPool.Index` as the key instead which means that an AIR
instruction no longer needs to be burned to store the type, and also
that we can use AutoArrayHashMap instead of an ArrayList, which avoids
storing duplicates into the set, potentially saving CPU time.
2023-06-10 20:47:54 -07:00
Jacob Young
a6fcf469fc Value: remove legacy type values 2023-06-10 20:47:54 -07:00
Jacob Young
dfd91abfe1 InternPool: add more pointer values 2023-06-10 20:47:54 -07:00
Andrew Kelley
7e19c95668 Sema: move inferred_alloc_const/mut_type to InternPool
Now, all types are migrated to use `InternPool`. The `Type.Tag` enum is
deleted in this commit.
2023-06-10 20:47:54 -07:00
Andrew Kelley
65d65f5dda Module: remove tmp_hack_arena
This was only needed when pointers were not fully migrated to InternPool
yet.
2023-06-10 20:47:53 -07:00
Jacob Young
115c089562 Value: add intern and unintern to facilitate code conversion
This allows some code (like struct initializers) to use interned types
while other code (such as comptime mutation) continues to use legacy
types.

With these changes, an `zig build-obj empty.zig` gets to a crash on
missing interned error union types.
2023-06-10 20:47:53 -07:00
Andrew Kelley
9ff514b6a3 compiler: move error union types and error set types to InternPool
One change worth noting in this commit is that `module.global_error_set`
is no longer kept strictly up-to-date. The previous code reserved
integer error values when dealing with error set types, but this is no
longer needed because the integer values are not needed for semantic
analysis unless `@errorToInt` or `@intToError` are used and therefore
may be assigned lazily.
2023-06-10 20:47:53 -07:00
Andrew Kelley
7bf91fc79a compiler: eliminate legacy Type.Tag.pointer
Now pointer types are stored only in InternPool.
2023-06-10 20:47:53 -07:00
Andrew Kelley
f21ca3da19 compiler: move anyframe->T to InternPool
Also I moved `anyframe` from being represented by `SimpleType` to being
represented by the `none` tag of `anyframe_type` because most code wants
to handle these two types together.
2023-06-10 20:47:53 -07:00