12417 Commits

Author SHA1 Message Date
Jacob Young
44d8cf9331 wasm: address behavior test regressions 2023-06-10 20:51:10 -07:00
Jacob Young
dce80f67d4 Sema: fix crashes accessing undefined values 2023-06-10 20:51:10 -07:00
mlugg
7702af5eb2 Sema: fix int arithmetic overflow checks
Previously, these checks worked by performing the arithmetic operation,
then checking whether the result fit in the type in question. Since all
values are now typed, this approach was no longer valid, and was
tripping some assertions due to trying to store too-large values in
smaller types.

Now, `intAdd`, `intSub`, `intMul` and `intDiv` all check for overflow,
and if it happens, re-do the operation with the result being a
`comptime_int`, and reporting the error (and vector index) to the caller
so that the error can be reported.

After this change, all test cases are passing.
2023-06-10 20:51:10 -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
Andrew Kelley
ab86b20248 std.hash: improve small-key hashing in Wyhash
Instead of carrying an optimized version of wyhash in the compiler for
small keys, put it into the std lib where it belongs.

...except it does not match the official test cases. This will need to
be fixed before merging into master. This is an extremely
contributor-friendly task.

Related issue: #15916
2023-06-10 20:51:09 -07:00
Andrew Kelley
7c12e064c4 Sema: reword compile error about LLVM extensions and C import 2023-06-10 20:47:59 -07:00
Jacob Young
e23b0a01e6 InternPool: fix yet more key lifetime issues 2023-06-10 20:47:59 -07:00
Jacob Young
6a15fc87ad Sema: handle generic types when coercing functions in memory
This used to be handled by `Type.eql`, but that is now a single comparison.
2023-06-10 20:47:59 -07:00
Jacob Young
ad54f47b95 InternPool: optimize previous fix
Just because we can't dedup, doesn't mean we can't use `string_bytes`.
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
mlugg
0f80652efb Sema: remove leftover references to value_arena
Notably, there was a bug where the fields of reified structs and unions
were allocated into an arena which was leaked. These are now in the
Module.tmp_hack_arena.
2023-06-10 20:47:59 -07:00
Jacob Young
7a59cd2863 Sema: hack around UAF 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
Jacob Young
04e66e6b4d InternPool: add optional coercion 2023-06-10 20:47:59 -07:00
Jacob Young
fdfe730487 InternPool: fix more key lifetime issues 2023-06-10 20:47:59 -07:00
Jacob Young
bc3b56f957 llvm: fix undefined pointer type 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
Jacob Young
e8bcdca044 Sema: fix in-memory coercion during comptime load 2023-06-10 20:47:58 -07:00
Jacob Young
e2174428e8 wasm: implement missing case 2023-06-10 20:47:58 -07:00
Jacob Young
8299ddfe4f InternPool: fix more key lifetime issues
Reminder to look into deleting `get` and make keys less pointery and
more long lived.
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
Jacob Young
0777e98bfe Sema: disable repeated aggregate storage use with mismatching sentinel
The InternPool implementation was not written to support this, but
that could be changed and this check removed in the future.
2023-06-10 20:47:58 -07:00
Jacob Young
a3b3ac0ea4 llvm: fix lowering of lazy values
These really should not be making it to the backend, but that's a
problem for another time.
2023-06-10 20:47:58 -07:00
Jacob Young
123cfab984 codegen: fix doubled global sentinels 2023-06-10 20:47:58 -07:00
Jacob Young
828756ceeb InternPool: fix element pointer type computations 2023-06-10 20:47:58 -07:00
Jacob Young
08ae212772 InternPool: fix key for empty array with sentinel 2023-06-10 20:47:58 -07:00
Andrew Kelley
c82a04d35f InternPool: debug dump all the data 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
870e3843c5 Sema: elide comptime-checked slice safety
Before, Zig would emit a start<=end safety check for `foo[1..2]` even
though it was already checked at compile-time.
2023-06-10 20:47:58 -07:00
Jacob Young
b2391a7d44 Sema: remove opv status from arrays with sentinels
Being able to create a pointer to the non-opv sentinel means that these
types have to actually be stored.
2023-06-10 20:47:58 -07:00
Jacob Young
71c4077c35 Value: fix null test for c pointers 2023-06-10 20:47:57 -07:00
Jacob Young
26fac15f48 math.big.int: fix ctz of zero 2023-06-10 20:47:57 -07:00
Jacob Young
d019229c2c Sema: avoid invalided key access 2023-06-10 20:47:57 -07:00
Andrew Kelley
aed142ebaa InternPool: further optimize Key hashing
This is a continuation of 2f24228c758bc8a35d13379703bc1695008212b0.

This commit comes with smaller gains, but gains nonetheless. memcpy is
showing up as much less interesting in callgrind output for behavior
tests.

Current status: this branch is 1.15 ± 0.02 times slower than merge-base.
2023-06-10 20:47:57 -07:00
mlugg
a0d4ef0acf InternPool: add representation for value of empty enums and unions
This is a bit odd, because this value doesn't actually exist:
see #15909. This gets all the empty enum/union behavior tests passing.

Also adds an assertion to `Sema.analyzeBodyInner` which would have
helped figure out the issue here much more quickly.
2023-06-10 20:47:57 -07:00
Jacob Young
99531b0d52 Sema: make sentinel load through array pointer comptime known 2023-06-10 20:47:57 -07:00
Jacob Young
1430ac2fbb Type: fix @sizeOf(?anyerror) 2023-06-10 20:47:57 -07:00
Jacob Young
91fb45a51b Sema: fix comptime error set comparisons 2023-06-10 20:47:57 -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
6b81546454 Type: fix @typeName for undefined 2023-06-10 20:47:57 -07:00
Jacob Young
f673c98a7c Sema: fix sus overflow behavior in RangeSetUnhandledIterator
The old code assumed that `intAddScalar` could return a value outside
of the range of `ty`, which is problematic for many reasons.

The new code (ab)uses the InternPool for speed.
2023-06-10 20:47:57 -07:00
Jacob Young
d0cd1c89da Sema: port lazy value usage to be InternPool aware 2023-06-10 20:47:57 -07:00
Andrew Kelley
61978c8c94 InternPool: eliminate indexToKey call graph cycle
Recursion makes this hot function more difficult to profile and
optimize.

I measured a 1.05x speedup vs the previous commit with the (set of
passing) behavior tests.

This commit was the last in a series, and the main thing it needed to do
was make InternPool.typeOf not call indexToKey(). This required adding a
type field to the runtime_value encoding even though it is technically
redundant. This could have been avoided with a loop inside typeOf, but I
wanted to keep the machine code of that hot function as simple as
possible. The variable encoding is still responsible for a relatively
small slice of the InternPool data size.

I added a function that provides the payload type corresponding to the
InternPool.Tag type, which allows for some handy inline switch prongs.

Let's start moving the structs that are specific to InternPool.Tag into
the corresponding namespace. This will provide type safety if the
encoding of InternPool changes for these types later.
2023-06-10 20:47:57 -07:00
Andrew Kelley
66f83f27a2 InternPool: avoid indexToKey recursion for type_enum_auto
Recursion makes this hot function more difficult to profile and
optimize.

This commit adds the integer tag type to the type_enum_auto encoding
even though the integer tag type can be inferred based on the number of
fields of the enum. This avoids a call to getAssumeExists of the integer
tag type inside indexToKey.
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
a803e9cf48 Sema: fix vector comparison and interning of -0 2023-06-10 20:47:57 -07:00