Commit graph

2054 commits

Author SHA1 Message Date
Xavier Bouchoux
77dd64b5f4 Sema: fix coerceArrayLike() for vectors with padding
as explainded at https://llvm.org/docs/LangRef.html#vector-type :

"In general vector elements are laid out in memory in the same way as array types.
Such an analogy works fine as long as the vector elements are byte sized.
However, when the elements of the vector aren’t byte sized it gets a bit more complicated.
One way to describe the layout is by describing what happens when a vector such
as <N x iM> is bitcasted to an integer type with N*M bits, and then following the
rules for storing such an integer to memory."

"When <N*M> isn’t evenly divisible by the byte size the exact memory layout
is unspecified (just like it is for an integral type of the same size)."
2023-08-10 16:10:59 -07:00
Andrew Kelley
b820d5df79
Merge pull request #16747 from jacobly0/llvm-wo-libllvm
llvm: enable the backend even when not linked to llvm
2023-08-10 12:02:57 -07:00
mlugg
f2c8fa769a
Sema: refactor generic calls to interleave argument analysis and parameter type resolution
AstGen provides all function call arguments with a result location,
referenced through the call instruction index. The idea is that this
should be the parameter type, but for `anytype` parameters, we use
generic poison, which is required to be handled correctly.

Previously, generic instantiations and inline calls worked by evaluating
all args in advance, before resolving generic parameter types. This
means any generic parameter (not just `anytype` ones) had generic poison
result types. This caused missing result locations in some cases.

Additionally, the generic instantiation logic caused `zirParam` to
analyze the argument types a second time before coercion. This meant
that for nominal types (struct/enum/etc), a *new* type was created,
distinct to the result type which was previously forwarded to the
argument expression.

This commit fixes both of these issues. Generic parameter type
resolution is now interleaved with argument analysis, so that we don't
have unnecessary generic poison types, and generic instantiation logic
now handles parameters itself rather than falling through to the
standard zirParam logic, so avoids duplicating the types.

Resolves: #16566
Resolves: #16258
Resolves: #16753
2023-08-10 10:00:26 +01:00
mlugg
6917a8c258
AstGen: handle ty result location for struct and array init correctly
Well, this was a journey!

The original issue I was trying to fix is covered by the new behavior
test in array.zig: in essence, `ty` and `coerced_ty` result locations
were not correctly propagated.

While fixing this, I noticed a similar bug in struct inits: the type was
propagated to *fields* fine, but the actual struct init was
unnecessarily anonymous, which could lead to unnecessary copies. Note
that the behavior test added in struct.zig was already passing - the bug
here didn't change any easy-to-test behavior - but I figured I'd add it
anyway.

This is a little harder than it seems, because the result type may not
itself be an array/struct type: it could be an optional / error union
wrapper. A new ZIR instruction is introduced to unwrap these.

This is also made a little tricky by the fact that it's possible for
result types to be unknown at the time of semantic analysis (due to
`anytype` parameters), leading to generic poison. In these cases, we
must essentially downgrade to an anonymous initialization.

Fixing these issues exposed *another* bug, related to type resolution in
Sema. That issue is now tracked by #16603. As a temporary workaround for
this bug, a few result locations for builtin function operands have been
disabled in AstGen. This is technically a breaking change, but it's very
minor: I doubt it'll cause any breakage in the wild.
2023-08-09 19:46:55 +01:00
Jacob Young
66084b6c3f Sema: remove validateRunTimeType
This function does not seem to differ in any interesting way from
`!typeRequiresComptime`, other than the `is_extern` param which is only
used in one place, and some differences did not seem correct anyway.

My reasoning for changing opaque types to be comptime-only is that
`explainWhyTypeIsComptime` is quite happy to explain why they are. :D
2023-08-09 05:46:44 -04:00
Stevie Hryciw
2ad6ca581d Add behavior test for copying self-referential struct
Closes #6312. In the C++ implementation this caused a stack overflow
from infinite recursion during analysis.
2023-08-06 18:23:28 -07:00
Stevie Hryciw
6bba5a39e4 Add behavior test for ptrCast on function pointer
Closes #6280. In the C++ implementation this triggered a
compiler assertion.
2023-08-06 18:09:41 -07:00
Andrew Kelley
4f6013bf50 add behavior test for sub-aligned field access
The workaround in std.zig.Server remains because the C backend is not
passing the new test.

see #14904
2023-08-01 00:39:30 -07:00
Jacob Young
228c956377 std: finish cleanup up asm
This also required implementing the necessary syntax in the x86_64 backend.
2023-07-31 03:49:21 -04:00
Jacob Young
9831f27238 cbe: get behavior tests running on arm
Specifically without linking libc.
2023-07-31 01:58:10 -04:00
Jacob Young
817fa3af86 std: cleanup asm usage
After fixing some issues with inline assembly in the C backend, the std
cleanups have the side effect of making these functions compatible with
the backend, allowing it to be used on linux without linking libc.
2023-07-31 01:58:10 -04:00
Jacob Young
6f0a613b6f
Merge pull request #16611 from xxxbxxx/packed-struct
codegen: fix various packed struct issues

Closes #16609
Closes #15337
2023-07-29 23:49:14 -04:00
Evan Haas
9b5586ab7c add behavior test for vector bitcast at comptime
closes #8184
2023-07-29 18:08:47 -07:00
Jacob Young
dc8a80a191 llvm: support read-write output constraints in assembly
Closes #15227
2023-07-29 09:49:04 -07:00
AdamGoertz
796927b900
Allow zero-sized fields in extern structs (#16404)
This change allows the following types to appear in extern structs:
* Zero-bit integers
* void
* zero-sized structs and packed structs
* enums with zero-bit backing integers
* arrays of any length with zero-size elements
2023-07-29 12:45:01 -04:00
Xavier Bouchoux
8c367ef99a codegen: fix access to byte-aligned nested packed struct elems
When acessing a packed struct member via a byte aligned ptr (from the optimisation in Sema.structFieldPtrByIndex())
the codegen must apply the parent ptr packed_offset in addition to the field offset itself.

resolves https://github.com/ziglang/zig/issues/16609
2023-07-29 18:16:13 +02:00
Jacob Young
8d1805f81c behavior: add coverage for no longer reproducing issue
Closes #14305
2023-07-29 09:00:23 -07:00
Xavier Bouchoux
46abf20454 llvm: partial fix of store undefined to packed result location
prefer marking too few undefined bits, rather than too many that may overwrite nearby values.
partially resolves https://github.com/ziglang/zig/issues/15337
2023-07-29 11:56:27 +02:00
Jacob Young
125b453c58 llvm: fix SysV C abi for structs smaller than two eightbytes
Closes #16038
Closes #16288
2023-07-28 19:27:08 -04:00
Jacob Young
c80609dfec Sema: don't reorder tuple fields
This conflicts with anon structs which can be in-memory coercible but
are never reordered.

Closes #16242
2023-07-28 19:27:08 -04:00
Andrew Kelley
e66190025f frontend: make fn calls byval; fix false positive isNonErr
This commit does two things which seem unrelated at first, but,
together, solve a miscompilation, and potentially slightly speed up
compiler perf, at the expense of making #2765 trickier to implement in
the future.

Sema: avoid returning a false positive for whether an inferred error set
is comptime-known to be empty.

AstGen: mark function calls as not being interested in a result
location. This prevents the test case "ret_ptr doesn't cause own
inferred error set to be resolved" from being regressed. If we want to
accept and implement #2765 in the future, it will require solving this
problem a different way, but the principle of YAGNI tells us to go ahead
with this change.

Old ZIR looks like this:

  %97 = ret_ptr()
  %101 = store_node(%97, %100)
  %102 = load(%97)
  %103 = ret_is_non_err(%102)

New ZIR looks like this:

  %97 = ret_type()
  %101 = as_node(%97, %100)
  %102 = ret_is_non_err(%101)

closes #15669
2023-07-27 10:12:08 -07:00
Andrew Kelley
c3364b372f
Merge pull request #16572 from ziglang/stage1-test-coverage
add behavior test coverage for stage1 issues
2023-07-26 23:38:28 -07:00
Jacob Young
7201e69454 AstGen: fix missing deferred ref
Closes #16524
2023-07-26 22:19:30 -07:00
Andrew Kelley
d28f24d1d1 add behavior test for defer assign
closes #10591
2023-07-26 19:02:02 -07:00
Andrew Kelley
407d91f7a7 add behavior test for switch nested break
closes #10196
2023-07-26 19:02:02 -07:00
Andrew Kelley
9d3363fee9 add behavior test for bitcast packed struct twice
closes #9914
2023-07-26 19:02:02 -07:00
Andrew Kelley
508294e9be add behavior test for comptime ptrcast packed struct
closes #9912
2023-07-26 19:02:02 -07:00
Andrew Kelley
2936602566 add behavior test for union with 128 bit integer
closes #9871
2023-07-26 19:02:02 -07:00
Andrew Kelley
87961237cf add behavior test for tail calls
closes #9703
2023-07-26 19:02:02 -07:00
Andrew Kelley
00d6a4da49 add behavior test for comptime array load
closes #8487
2023-07-26 17:21:20 -07:00
Andrew Kelley
6cee98eb30 frontend: forbid packed and extern tuples 2023-07-25 21:45:33 -07:00
Xavier Bouchoux
5ab1854602 codegen/llvm: fix memset with vectors smaller than one byte
The special case to take advantage of llvm's intrinsic
was generating invalid llvm ir:

```
Invalid bitcast
  %60 = bitcast <2 x i2> %59 to i8, !dbg !3122
thread 145453 panic: LLVM module verification failed
```
2023-07-25 18:14:10 -07:00
Jacob G-W
3c08fe931a make @typeInfo not return private decls
fixes #10731
Thanks @nektro for previous work in #14878

This change creates a small breaking change:
It removes the `is_pub` field of a decl in `@typeInfo`
2023-07-25 16:19:08 -07:00
Andrew Kelley
cc6964c5dc InternPool: add func_coerced handling to funcIesResolved 2023-07-23 17:47:18 -07:00
Luuk de Gram
619140c0d2 wasm: correctly intcast signed integers
When a signed integer's bitsize is not 32 or 64, but the given
bitsize and wanted bitsize are either both represented by Wasm's i32
or i64, we must either sign extend or wrap the integer.
2023-07-22 02:12:07 +02:00
Andrew Kelley
c597ba32d9 Sema: fix return type of generic function is function pointer
also that's one less standalone test and one more behavior test.
2023-07-18 21:15:16 -07:00
antlilja
a0ec2266fe Update tests to new splat syntax 2023-07-12 15:35:57 -07:00
Ian Johnson
3a30f0fa50 Sema: resolve field type layout for anon struct type info
Closes #16148
2023-07-10 19:17:50 +03:00
Andrew Kelley
17890f6b8a
Merge pull request #15879 from r00ster91/more_stage1_test_coverage
More stage1 test coverage
2023-07-03 15:59:54 -07:00
r00ster91
2583a39fb6 behavior: test @bitCast of packed struct of bools
This seems to have resolved itself now.
Tested on x86_64 using debug optimize mode.

Closes #9851
2023-07-03 10:59:13 -07:00
Andrew Kelley
309aacfc89
Merge pull request #16284 from Snektron/spirv-internpool-fixes
SPIR-V InternPool aftermath damage control
2023-07-01 14:21:04 -07:00
Robin Voetter
073289d0da
spirv: disable new behavior tests that do not pass
Some new behavior tests have recently been added, and not all of these
pass with the SPIR-V backend.
2023-07-01 19:45:09 +02:00
Jacob Young
f571438fc0 Unrevert "Sema: preserve extern struct field alignment"
This unreverts commit 1a2468abfc.
2023-06-30 23:24:52 -04:00
Jacob Young
2282c27885 Remerge pull request #15995 from mlugg/fix/union-field-ptr-align
Sema: copy pointer alignment to union field pointers

This is an unrevert of 43c98dc115.
2023-06-30 23:23:26 -04:00
mlugg
730f2e0407 Sema: copy pointer alignment to struct field pointers 2023-06-30 23:23:03 -04:00
mlugg
5b594139d9 Sema: copy pointer alignment to union field pointers
This implements the semantics as discussed in today's compiler meeting,
where the alignment of pointers to fields of default-layout unions
cannot exceed the field's alignment.

Resolves: #15878
2023-06-30 23:23:01 -04:00
Jacob Young
43c98dc115 Revert "Merge pull request #15995 from mlugg/fix/union-field-ptr-align"
This reverts commit 40cf3f7ae5, reversing
changes made to d98147414d.
2023-06-29 00:23:19 -04:00
Jacob Young
1a2468abfc Revert "Sema: preserve extern struct field alignment"
This reverts commit 4620972d08.
2023-06-29 00:23:19 -04:00
Alex Kladov
4620972d08 Sema: preserve extern struct field alignment
In

    extern struct { x: u32, y: u16 }

we actually know that y's alignment is `@alignOf(u32)`, and not just
`@alignOf(u16)`.

closes: #16134
2023-06-28 16:36:32 +02:00
zooster
28f515acd7 behavior: test slicing array of zero-sized values
Closes #15343
2023-06-28 14:00:18 +03:00