1216 Commits

Author SHA1 Message Date
Robin Voetter
2f28713bd7
spirv: pointer bitcasting 2023-05-20 17:30:22 +02:00
Robin Voetter
4203d099be
spirv: lower integer pointer constants 2023-05-20 17:30:22 +02:00
Robin Voetter
64f99f36a6
spirv: ptr_add
Implements the ptr_add air tag for spirv.

The implementation for slices is probably wrong, but there seems to be no test for this...
2023-05-20 17:30:22 +02:00
Robin Voetter
3c14438a93
spirv: use extractField more
reduce some code duplication
2023-05-20 17:30:21 +02:00
Robin Voetter
6c05557072
spirv: fix some (Ptr)AccessChain uses
The first dereference of PtrAccessChain returns a pointer of the same type
as the base pointer, in contrast to AccessChain, where the first dereference
returns a pointer of the dereferenced type of the base pointer.
2023-05-20 17:30:21 +02:00
Robin Voetter
0ba0d8fecb
spirv: dont use OpIAddCarry
This instruction is not really working well in the LLVM SPIRV translator,
as it is not implemented.

This commit also intruces the constructStruct helper function to initialize
structs at runtime. This is ALSO buggy in the translator, and we must work
around OpCompositeConstruct not working when some of the constituents are
runtime-known only.

Some other improvements are made:
- improved variable() so that it is more useful and no longer requires the
  address space. It always puts values in the Function address space,
  and returns a pointer to the Generic address space
- adds a boolToInt utility function
2023-05-20 17:30:21 +02:00
Robin Voetter
7d519b3383
spirv: use intInfo instead of arithmeticTypeInfo in airIntCast
This ensures that we can also cast enums and error sets here. In the future
this function will need to be changed to support composite and strange
integers, but that is fine.
2023-05-20 17:30:21 +02:00
Robin Voetter
6e3770e970
spirv: implement pointer comparison in for air cmp
It turns out that the Khronos LLVM SPIRV translator does not support OpPtrEqual.
Therefore, this instruction is emitted using a series of conversions.

This commit breaks intToEnum, because enum was removed from the arithmetic type
info. The enum should be converted to an int before this function is called.
2023-05-20 17:30:21 +02:00
Ali Chraghi
6fca3f8b72 spirv: lower ptrtoint & ignore dbg_inline instructions 2023-05-15 22:04:42 +03:30
Ali Chraghi
f8de4db873 spirv: implement arithmeticTypeInfo for Enum (@intToEnum) 2023-05-15 14:22:40 +03:30
Ali Chraghi
9c550721e4 spirv: lower float_to_int and int_to_float 2023-05-15 03:04:17 +03:30
Andrew Kelley
88d275199c
Merge pull request #15240 from Snektron/spirv-basic
spirv: attempting to get the 'basic' behavior tests running
2023-05-12 14:46:20 -07:00
shwqf
e46d7a3699 Optimize access of array member in a structure. 2023-05-12 09:04:03 +03:00
Ali Chraghi
d61dc3a5a2
spirv: lower store_safe, trunc and trap 2023-05-11 20:31:52 +02:00
Robin Voetter
e26d8d0604
spirv: make decl deps a hash map instead of an arraylist
The same declaration can be added to the dependency set multiple
times, and in this case we still need to emit it once. By making
this list a hash map instead, we can do that quite easily.

This commit also introduces some additional debug logging regarding
decls.
2023-05-11 20:31:51 +02:00
Robin Voetter
d961b11cde
spirv: lower wrap_optional
This implements the wrap_optional AIR instruction.
2023-05-11 20:31:51 +02:00
Robin Voetter
6c16465d45
spirv: lower air optional_payload
Implements lowering the AIR tag optional_payload.

Also fixes an issue with lowering constant ints.
2023-05-11 20:31:51 +02:00
Robin Voetter
83ab1ba8fd
spirv: lower air is_null, is_non_null
Implements AIR lowering for is_null and is_non_null tags.

Additionally this cleans up and centralizes the logic to convert from 'direct'
representation to 'indirect' representation and vice-versa. The related functions,
as well as the functions that use it, are all moved near eachother so that the
conversion logic remains in a central place. Extracting/inserting fields
and loading/storing pointers should go through these functions.
2023-05-11 20:31:51 +02:00
Robin Voetter
435a5660ce
spirv: fix invalid code generated by br-with-value
The result-id and result-type-id of the OpPhi used to merge the
break values was not properly emitted, as some of the operands
were not written out. This caused an invalid spir-v module.
2023-05-11 20:31:51 +02:00
Robin Voetter
f62735d98c
spirv: lower air wrap_errunion_err 2023-05-11 20:31:51 +02:00
Robin Voetter
78c44d63da
spirv: lower air unwrap_error_union_err 2023-05-11 20:31:50 +02:00
Robin Voetter
0bae2caaf3
spirv: lower air try
Implements code generation for the try air tag. This commit also adds
a utility `errorUnionLayout` function that helps keeping the layout
of a spir-v error union consistent.
2023-05-11 20:31:50 +02:00
Robin Voetter
dfecf89d06
spirv: fix OpFunctionCall parameters interleaving with insts
resolve() is now able to emit instructions. If usage of this function is
interleaved with calls to emitRaw() and writeOperand(), then an
instruction may get inserted between operands, causing an invalid module.
The solution here is to just perform a temporary allocation.
2023-05-11 20:31:50 +02:00
Robin Voetter
5d896a6cc6
spirv: fix use-after-realloc in resolveType()
The pointer to a slot in a hash map was fetched before a recursive call.
If the hash map's size changed during the recursive call, this would write
to an invalid pointer.

The solution is to use an index instead of a pointer. Note that care must be
taken that resolved types (from the type_cahce) must not be accessed, as they
might be incomplete during this operation.
2023-05-11 20:31:50 +02:00
Andrew Kelley
5569e6b49d
Merge pull request #15639 from jacobly0/signed-mod
llvm/cbe: fix signed `@mod`/`@divFloor` computations
2023-05-11 08:36:33 -07:00
Jacob Young
c5b96c7447 llvm: fix @max/@min of unsupported float types
Closes #15611
2023-05-10 15:16:50 -04:00
Jacob Young
2e6a6d7564 llvm/cbe: fix signed @mod/@divFloor computations
Closes #15636
2023-05-10 15:11:52 -04:00
Jacob Young
31a13ce2e3 cbe: use Assignment in airSplat 2023-05-10 15:11:52 -04:00
Jacob Young
d71a43ec2c cbe: fix typos 2023-05-06 10:27:39 -07:00
Andrew Kelley
cb9e8b0d01 C backend: take advantage of Assignment abstraction in airMemset 2023-04-28 13:24:43 -07:00
Andrew Kelley
73d3fb9883 C backend: fix ptr comparison of array ptrs when one is null-terminated 2023-04-28 13:24:43 -07:00
Andrew Kelley
6261c13731 update codebase to use @memset and @memcpy 2023-04-28 13:24:43 -07:00
Andrew Kelley
36df2a83fc C backend: fix memset for structs and arrays 2023-04-28 13:24:42 -07:00
Andrew Kelley
9295355985 LLVM backend: optimize memset with comptime-known element
When the element is comptime-known, we can check if it has a repeated
byte representation. In this case, `@memset` can be lowered with the
LLVM intrinsic rather than with a loop.
2023-04-28 13:24:42 -07:00
Andrew Kelley
51adbf472b llvm backend: fix memset with byref element value 2023-04-28 13:24:42 -07:00
Andrew Kelley
00b690540e llvm backend: fix lowering of memset
The bitcast of ABI size 1 elements was problematic for some types.
2023-04-28 13:24:42 -07:00
Andrew Kelley
afbcad9939
Merge pull request #15452 from mlugg/zig-cbe-opt
CBE: minor optimizations to output source
2023-04-26 17:09:04 -07:00
Andrew Kelley
3c66850e42
Merge pull request #15278 from ziglang/memcpy-memset
change semantics of `@memcpy` and `@memset`
2023-04-26 10:01:54 -07:00
mlugg
2e23ddbe7b
CBE: minor optimizations to output source 2023-04-26 01:46:09 +01:00
Andrew Kelley
badad16f88 C backend: fix lowering comparison when array ptr meets ptr
Pointer comparisons were triggering `-Wcompare-distinct-pointer-types`
before this fix, which adds `(void*)` casts if the lhs type and rhs type
do not match pointer sizeness.
2023-04-25 11:23:41 -07:00
Andrew Kelley
792bbfa301 Sema: fix memcpy alias safety incorrect math
Previously it was not multiplying by the element ABI size. Now, it uses
ptr_add instructions which do math based on the element type.
2023-04-25 11:23:41 -07:00
Andrew Kelley
5378fdffdc stage2: introduce store_safe AIR instruction
store:
The value to store may be undefined, in which case the destination
memory region has undefined bytes after this instruction is
evaluated. In such case ignoring this instruction is legal
lowering.

store_safe:
Same as `store`, except if the value to store is undefined, the
memory region should be filled with 0xaa bytes, and any other
safety metadata such as Valgrind integrations should be notified of
this memory region being undefined.
2023-04-25 11:23:41 -07:00
Andrew Kelley
d604553ee0 C backend: use ++ instead of += for airMemset
It does the same thing but has fewer bytes in the output.
2023-04-25 11:23:41 -07:00
Andrew Kelley
0f65cc9275 C backend: fix memset for loop lowering
Previously, this code casted the array pointer to u8 pointer, but I
removed that in a different commit. This commit restores the cast, but
instead of hard-coding u8, it uses the destination element pointer,
since memset now supports arbitrary element types.
2023-04-25 11:23:41 -07:00
Andrew Kelley
057c950093 LLVM backend: support non-byte-sized memset
Also introduce memset_safe AIR tag and support it in C backend and LLVM
backend.
2023-04-25 11:23:41 -07:00
Andrew Kelley
92186b8c13 C backend: implement new memcpy and inttoptr semantics 2023-04-25 11:23:40 -07:00
Andrew Kelley
edb5e493e6 update @memcpy to require equal src and dest lens
* Sema: upgrade operands to array pointers if possible when emitting
   AIR.
 * Implement safety checks for length mismatch and aliasing.
 * AIR: make ptrtoint support slice operands. Implement in LLVM backend.
 * C backend: implement new `@memset` semantics. `@memcpy` is not done
   yet.
2023-04-25 11:23:40 -07:00
Andrew Kelley
a5c910adb6 change semantics of @memcpy and @memset
Now they use slices or array pointers with any element type instead of
requiring byte pointers.

This is a breaking enhancement to the language.

The safety check for overlapping pointers will be implemented in a
future commit.

closes #14040
2023-04-25 11:23:40 -07:00
Jacob Young
a1fcb51692 cbe: fix mutability issues with builtin test_functions 2023-04-25 13:42:10 -04:00
Jacob Young
f1782c07a9 cbe: implement @extern 2023-04-25 13:42:10 -04:00