158 Commits

Author SHA1 Message Date
kcbanner
fb523c6283 sema: when guessing union alignment, save the result and check if the guess was correct 2023-10-31 01:35:58 +00:00
kcbanner
4d044ee7e0 sema: Add union alignment resolution
- Add resolveUnionAlignment, to resolve a union's alignment only, without triggering layout resolution.
- Update resolveUnionLayout to cache size, alignment, and padding. abiSizeAdvanced and abiAlignmentAdvanced
  now use this information instead of computing it each time.
2023-10-31 01:35:58 +00:00
Andrew Kelley
62f45b802c make Zir.Inst.Index typed
This commit starts by making Zir.Inst.Index a nonexhaustive enum rather
than a u32 alias for type safety purposes, and the rest of the changes
are needed to get everything compiling again.
2023-10-28 10:14:15 -07:00
mlugg
20bb81166f
InternPool: remove runtime_value representation
The main goal of this commit is to remove the `runtime_value` field from
`InternPool.Key` (and its associated representation), but there are a
few dominos. Specifically, this mostly eliminates the "maybe runtime"
concept from value resolution in Sema: so some resolution functions like
`resolveMaybeUndefValAllowVariablesMaybeRuntime` are gone. This required
a small change to struct/union/array initializers, to no longer
use `runtime_value` if a field was a `variable` - I'm not convinced this
case was even reachable, as `variable` should only ever exist as the
trivial value of a global runtime `var` decl.

Now, the only case in which a `Sema.resolveMaybeUndefVal`-esque function
can return the `variable` key is `resolveMaybeUndefValAllowVariables`,
which is directly called from `Sema.resolveInstValueAllowVariables`
(previously `Sema.resolveInstValue`), which is only used for resolving
the value of a Decl from `Module.semaDecl`.

While changing these functions, I also slightly reordered and
restructured some of them, and updated their doc comments.
2023-10-24 14:28:33 +01:00
Andrew Kelley
411fcd22ab frontend: rework @embedFile for incremental compilation
This feature was made to work with the legacy incremental compilation
mechanism which is being reworked.

This commit regresses the ability to update files used with `@embedFile`
while the compiler is running.

In exchange, we get these benefits:
* The embedded file contents are copied directly into InternPool rather
  than there being an extra allocation and memcpy.
* The EmbedFile struct, which represents a long-lived object, is made
  more serialization friendly.
* Eliminate the creation and use of a Decl as an anonymous decl.

When implementing the new incremental compilation mechanism,
functionality will need to be added back for handling `@embedFile`.
2023-10-23 17:24:58 -07:00
Andrew Kelley
7bab406c79 InternPool: store alignment of anon decls
Commit 5393e56500d499753dbc39704c0161b47d1e4d5c has a flaw pointed out
by @mlugg: the `ty` field of pointer values changes when comptime values
are pointer-casted. This commit introduces a new encoding which
additionally stores the "original pointer type" which is used to store
the alignment of the anonymous decl, and potentially other information
in the future such as section and pointer address space. However, this
new encoding is only used when the original pointer type differs from
the casted pointer type in a meaningful way.

I was able to make the LLVM backend and the C backend lower anonymous
decls with the appropriate alignment, however I will need some help
figuring out how to do this for the backends that lower anonymous decls
via src/codegen.zig and the wasm backend.
2023-10-21 21:38:41 -04:00
Andrew Kelley
bd1f96869e InternPool: fix incomplete hash of pointer values
There is this `common` prefix which was not getting passed in all union
tags of pointer vals, resumably making InternPool slower for pointer
values.
2023-10-20 20:02:35 -04:00
Robin Voetter
839a93a101
intern pool: fix float equality
We need to perform bitwise equality here, otherwise we get
two different entries for nan values.
2023-10-15 14:00:08 +02:00
Andrew Kelley
c0b5512544 compiler: start handling anonymous decls differently
Instead of explicitly creating a `Module.Decl` object for each anonymous
declaration, each `InternPool.Index` value is implicitly understood to
be an anonymous declaration when encountered by backend codegen.

The memory management strategy for these anonymous decls then becomes to
garbage collect them along with standard InternPool garbage.

In the interest of a smooth transition, this commit only implements this
new scheme for string literals and leaves all the previous mechanisms in
place.
2023-10-03 12:12:50 -07:00
Jacob Young
3bd1b9e15f x86_64: implement and test unary float builtins 2023-10-01 15:09:52 -04:00
Veikka Tuominen
f4c884617f
Merge pull request #17215 from kcbanner/read_from_memory_union
sema: add support for unions in readFromMemory and writeToMemory
2023-09-26 11:16:03 +03:00
Andrew Kelley
28ac9f8b70
Merge pull request #17253 from ziglang/MultiArrayList-0bit-struct
std.MultiArrayList: add test coverage for 0-bit structs
2023-09-25 02:33:32 -07:00
Andrew Kelley
eb072fa528
Merge pull request #17256 from ziglang/packed-bit-offsets
compiler: packed structs cache bit offsets
2023-09-24 19:42:06 -07:00
Andrew Kelley
ac6f9eb2ca InternPool: store_hash=false for FieldMap
This is something I wanted to do a long time ago but was blocked
by #10618 which is now solved.
2023-09-24 15:49:56 -07:00
Andrew Kelley
c08c0fc6ed revert "compiler: packed structs cache bit offsets"
This is mostly a revert of a7088fd9a3edb037f0f51bb402a3c557334634f3.
Measurement revealed the commit actually regressed performance.
2023-09-24 14:37:36 -07:00
Andrew Kelley
a7088fd9a3 compiler: packed structs cache bit offsets
Instead of linear search every time a packed struct field's bit or byte
offset is wanted, they are computed once during resolution of the packed
struct's backing int type, and stored in InternPool for O(1) lookup.

Closes #17178
2023-09-23 23:06:08 -07:00
mlugg
5fa260ba06
InternPool: do not append sentinel value twice when initializing aggregate of u8 2023-09-23 22:01:08 +01:00
kcbanner
f2a24b48e1 sema: rework the comptime representation of comptime unions
When the tag is not known, it's set to `.none`. In this case, the value is either an
array of bytes (for extern unions) or an integer (for packed unions).
2023-09-23 13:05:04 -04:00
Andrew Kelley
81b5df347a compiler: fix structFieldName crash for tuples
When struct types have no field names, the names are implicitly
understood to be strings corresponding to the field indexes in
declaration order. It used to be the case that a NullTerminatedString
would be stored for each field in this case, however, now, callers must
handle the possibility that there are no names stored at all. This
commit introduces `legacyStructFieldName`, a function to fake the
previous behavior. Probably something better could be done by reworking
all the callsites of this function.
2023-09-21 17:29:34 -07:00
Andrew Kelley
dca17d3603 Sema: fix struct alignment regressions 2023-09-21 14:48:40 -07:00
Andrew Kelley
51b1a2a6cb Alignment: min/minStrict max/maxStrict
Carve out a path forward for being a little bit more intentional about
handling "default" alignment values.
2023-09-21 14:48:40 -07:00
Andrew Kelley
2671aa9058 InternPool: fix nameIndex for tuples 2023-09-21 14:48:40 -07:00
mlugg
ada83fa557 compiler: get codegen of behavior tests working on at least one backend
We're hitting false compile errors, but this is progress!
2023-09-21 14:48:40 -07:00
Andrew Kelley
baea62a8ad fix regressions from this branch 2023-09-21 14:48:40 -07:00
Andrew Kelley
483b3a3344 InternPool: implement only_possible_value prong of indexToKey
for the new struct and packed struct encodings.
2023-09-21 14:48:40 -07:00
Andrew Kelley
b65f3d8826 InternPool: implement extraStructType 2023-09-21 14:48:40 -07:00
Andrew Kelley
fa1beba74f InternPool: implement getStructType
This also modifies AstGen so that struct types use 1 bit each from the
flags to communicate if there are nonzero inits, alignments, or comptime
fields. This allows adding a struct type to the InternPool without
looking ahead in memory to find out the answers to these questions,
which is easier for CPUs as well as for me, coding this logic right now.
2023-09-21 14:48:40 -07:00
Andrew Kelley
accd5701c2 compiler: move struct types into InternPool proper
Structs were previously using `SegmentedList` to be given indexes, but
were not actually backed by the InternPool arrays.

After this, the only remaining uses of `SegmentedList` in the compiler
are `Module.Decl` and `Module.Namespace`. Once those last two are
migrated to become backed by InternPool arrays as well, we can introduce
state serialization via writing these arrays to disk all at once.

Unfortunately there are a lot of source code locations that touch the
struct type API, so this commit is still work-in-progress. Once I get it
compiling and passing the test suite, I can provide some interesting
data points such as how it affected the InternPool memory size and
performance comparison against master branch.

I also couldn't resist migrating over a bunch of alignment API over to
use the log2 Alignment type rather than a mismash of u32 and u64 byte
units with 0 meaning something implicitly different and special at every
location. Turns out you can do all the math you need directly on the
log2 representation of alignments.
2023-09-21 14:48:40 -07:00
Andrew Kelley
cb6201715a InternPool: prevent anon struct UAF bugs with type safety
Instead of using actual slices for InternPool.Key.AnonStructType, this
commit changes to use Slice types instead, which store a
long-lived index rather than a pointer.

This is a follow-up to 7ef1eb1c27754cb0349fdc10db1f02ff2dddd99b.
2023-09-12 20:08:56 -04:00
Andrew Kelley
ada0010471 compiler: move unions into InternPool
There are a couple concepts here worth understanding:

Key.UnionType - This type is available *before* resolving the union's
fields. The enum tag type, number of fields, and field names, field
types, and field alignments are not available with this.

InternPool.UnionType - This one can be obtained from the above type with
`InternPool.loadUnionType` which asserts that the union's enum tag type
has been resolved. This one has all the information available.

Additionally:

* ZIR: Turn an unused bit into `any_aligned_fields` flag to help
  semantic analysis know whether a union has explicit alignment on any
  fields (usually not).
* Sema: delete `resolveTypeRequiresComptime` which had the same type
  signature and near-duplicate logic to `typeRequiresComptime`.
  - Make opaque types not report comptime-only (this was inconsistent
    between the two implementations of this function).
* Implement accepted proposal #12556 which is a breaking change.
2023-08-22 13:54:14 -07:00
Andrew Kelley
7ef1eb1c27 InternPool: safer enum API
The key changes in this commit are:

```diff
-        names: []const NullTerminatedString,
+        names: NullTerminatedString.Slice,
-        values: []const Index,
+        values: Index.Slice,
```

Which eliminates the slices from `InternPool.Key.EnumType` and replaces
them with structs that contain `start` and `len` indexes. This makes the
lifetime of `EnumType` change from expiring with updates to InternPool,
to expiring when the InternPool is garbage-collected, which is currently
never.

This is gearing up for a larger change I started working on locally
which moves union types into InternPool.

As a bonus, I fixed some unnecessary instances of `@as`.
2023-08-17 18:16:03 -07:00
mlugg
083ee8e0e2
InternPool: preserve indices of builtin types when resolved
Some builtin types have a special InternPool index (e.g.
`.type_info_type`) so that AstGen can refer to them before semantic
analysis. Unfortunately, this previously led to a second index existing
to refer to the type once it was resolved, complicating Sema by having
the concept of an "unresolved" type index.

This change makes Sema modify these InternPool indices in-place to
contain the expanded representation when resolved. The analysis of the
corresponding decls is caught in `Module.semaDecl`, and a field is set
on Sema telling it which index to place struct/union/enum types at. This
system could break if `std.builtin` contained complex decls which
evaluate multiple struct types, but this will be caught by the
assertions in `InternPool.resolveBuiltinType`.

The AstGen result types which were disabled in 6917a8c have been
re-enabled.

Resolves: #16603
2023-08-15 11:45:23 +01:00
kcbanner
a8a2f2b58b Add --verbose-generic-instances to provide visibility on the number of generic function instantiations 2023-07-26 02:20:29 -07:00
Andrew Kelley
d92cca9324 InternPool: add func_coerced handling to getFuncInstanceIes
Oops, there was a missing call to `unwrapCoercedFunc`.
2023-07-23 18:39:58 -07:00
Andrew Kelley
cc6964c5dc InternPool: add func_coerced handling to funcIesResolved 2023-07-23 17:47:18 -07:00
Andrew Kelley
a7f3c2eab4 InternPool: fix coerced func hash/eql same as uncoerced
Since the same Key.Func data structure is used for coerced function
bodies as well as uncoerced function bodies, there is danger of them
being hashed and equality-checked as the same. When that happens, the
type of a function body value might be wrong, causing lots of problems.
In this instance, it causes an assertion failure.

This commit fixes it by introducing an `uncoerced_ty` field which
is different than `ty` in the case of `func_coerced` and is used to
differentiate when doing hashing and equality checking.

I have a new behavior test to cover this bug, but it revealed *another*
bug at the same time, so I will fix it in the next commit and and add
the new test therein.
2023-07-23 17:47:18 -07:00
Andrew Kelley
9262b6076f Sema: fix generic function instances not respecting linksection 2023-07-18 21:46:30 -07:00
Andrew Kelley
e1935d4d16 InternPool: fix funcAnalysis through func_coerced 2023-07-18 19:02:06 -07:00
Andrew Kelley
9f61c29bfb InternPool: unwrap func_coerced when using it as generic_owner 2023-07-18 19:02:06 -07:00
Andrew Kelley
684aee3220 frontend: fixes for function regressions in this branch
* Introduce InternPool.Tag.func_coerced to handle the case of a
   function body coerced to a new type. `InternPool.getCoerced` is now
   implemented for function bodies in this branch.
 * implement resolution of ad-hoc inferred error sets in
   `Sema.analyzeCall`.
 * fix generic_owner being set wrong for child Sema bodies of param
   expressions.
 * fix `Sema.resolveInferredErrorSetTy` when passed `anyerror`.
2023-07-18 19:02:06 -07:00
Andrew Kelley
927f6ec8ca frontend: fix inferred error sets of comptime/inline calls
Previously, they shared function index with the owner decl, but that
would clobber the data stored for inferred error sets of runtime calls.

Now there is an adhoc_inferred_error_set_type which models the problem
much more correctly.
2023-07-18 19:02:06 -07:00
Andrew Kelley
82db06fa67 InternPool: fix regression in getting error sets
Missing call to `addStringsToMap`.
2023-07-18 19:02:06 -07:00
Andrew Kelley
b81d193021 InternPool: implement getFuncInstanceIes
and fix bug in equality checking for functions with inferred error sets.
2023-07-18 19:02:05 -07:00
Andrew Kelley
8f37b794a5 InternPool: fix debug info helpers for changes to functions
It still doesn't work correctly due to incorrect debug info for packed
structs.
2023-07-18 19:02:05 -07:00
Andrew Kelley
cbbb5cc2ec InternPool: implement getFuncInstance
This handles the case without an inferred error set. Still TODO is the
case with one.

Also fixes branchQuota returning a pointer to the wrong field.
2023-07-18 19:02:05 -07:00
Andrew Kelley
6d72f971af InternPool: implement getExternFunc 2023-07-18 19:02:05 -07:00
Andrew Kelley
8fd77395d6 InternPool: fix getErrorSetType
Before, it incorrectly passed an InternPool.Index where an extra array
index was expected (to the function which is renamed to `extraErrorSet`
in this commit).
2023-07-18 19:02:05 -07:00
Andrew Kelley
c193872c81 InternPool: implement indexToKey for func_instance and func_decl
Also delete incorrect frees an arena-allocated parameters.
2023-07-18 19:02:05 -07:00
Andrew Kelley
4a55fc6c53 InternPool: avoid false negatives for functions with inferred error sets
There is one case where function types may be inequal but we still want
to find the same function body instance in InternPool.

In the case of the functions having an inferred error set, the key used
to find an existing function body will necessarily have a unique
inferred error set type, because it refers to the function body
InternPool Index. To make this case work we omit the inferred error set
from the equality and hashing functions.
2023-07-18 19:02:05 -07:00
Andrew Kelley
f3dc53f6b5 compiler: rework inferred error sets
* move inferred error sets into InternPool.
   - they are now represented by pointing directly at the corresponding
     function body value.
 * inferred error set working memory is now in Sema and expires after
   the Sema for the function corresponding to the inferred error set is
   finished having its body analyzed.
 * error sets use a InternPool.Index.Slice rather than an actual slice
   to avoid lifetime issues.
2023-07-18 19:02:05 -07:00