107 Commits

Author SHA1 Message Date
mlugg
6f09a7041e
Begin integrating new liveness analysis into remaining backends 2023-04-20 20:49:34 +01:00
Robin Voetter
979b410258
spirv: Do not generate the Alignment attribute on pointers for now
It seems that some implementations may have problems with these right now,
like Intel and Rusticl. In theory, these attributes should be superficial
on the pointer type, as alignment guarantees are also added via the
alignment option of the OpLoad and OpStore instructions. Therefore, get rid
of them for now.
2023-04-09 01:51:55 +02:00
Robin Voetter
45b5f46770
spirv: allow global, constant address spaces
These should actually just work fine in SPIR-V. They are mapped to CrossWorkgroup
and UniformConstant respectively.
2023-04-09 01:51:54 +02:00
Robin Voetter
e389f524c9
spirv: export functions with .Kernel callconv as entry point
Exported functions which have the .Kernel calling convention are now exported
as entry point. This also works with @export.
2023-04-09 01:51:54 +02:00
Robin Voetter
3f2025f59e
spirv: emit interface variables for entry points
Also actually implement generating the OpEntryPoint instructions.
2023-04-09 01:51:54 +02:00
Robin Voetter
405f7298ac
spirv: add decl dependencies for functions also
Entry points need to be attributed with a complete list of
global variables that they use. To that end, the global dependencies
mechanism is extended to also allow functions - when flushing the
module, the list of dependencies is examined to generate this
list of global variable result-ids.
2023-04-09 01:51:53 +02:00
Robin Voetter
efe7fae6af
spirv: temporarily emit test kernels
SPIR-V cannot represent function pointers without extensions
that no vendor implements. For the time being, generate a test
kernel for each error, so that we can at least run SOME tests.

In the future we may be able to emulate function pointers in some
way, but that is not today.
2023-04-09 01:51:53 +02:00
Robin Voetter
719d47d823
spirv: implement error set and error unions
Implements lowering types and constants for error sets and error unions.
2023-04-09 01:51:53 +02:00
Robin Voetter
8bbfbfc956
spirv: improve linking globals
SPIR-V globals must be emitted in order, so that any
declaration precedes usage. Zig, however, generates globals in
random order. To this end we keep for each global a list of
dependencies and perform a topological sort when flushing the
module.
2023-04-09 01:51:53 +02:00
Robin Voetter
80b8435569
spirv: overhaul constant lowering
Lowering constants is currently not really compatible with unions. In
this commit, constant lowering is drastically overhauled: instead of
playing nice and generating SPIR-V constant representations for everything
directly, we're just going to treat globals as an untyped bag of bytes (
or rather, SPIR-V 32-bit words), which we cast to the desired type at
usage. This is similar to how Rust generates constants in its LLVm backend.
2023-04-09 01:51:53 +02:00
Robin Voetter
764f19034d
spirv: union types/constants
Implements lowering union types and constants in the SPIR-V backend.
2023-04-09 01:51:53 +02:00
Robin Voetter
75abe3b43b
spirv: optional constants
Implements lowering optional constants in the SPIR-V backend.
2023-04-09 01:51:53 +02:00
Robin Voetter
0c53fea42c
spirv: improve genConstant usage
This little wrapper function allocates a result-id for us,
so that we don't have to do that.
2023-04-09 01:51:52 +02:00
Robin Voetter
12a3a8b100
spirv: slice constants
Implements lowering slice constants in the SPIR-V backend
2023-04-09 01:51:52 +02:00
Robin Voetter
e788dfa142
spirv: string literals
Implements lowering string literal constants in the SPIR-V backend
2023-04-09 01:51:52 +02:00
Robin Voetter
27833004db
spirv: optional types
Implements lowering optional types in the SPIR-V backend.
2023-04-09 01:51:52 +02:00
Robin Voetter
3c7f93aa69
spirv: generic global pointers
Similar to function locals, taking the address of a global that does
not have an explicit address space assigned to it should result
in a generic pointer, not a global pointer. Also similar to function
locals, they cannot be generated into the generic storage class, and
so are generated into the global storage class and then cast to a
generic pointer, using OpSpecConstantOp. Note that using
OpSpecConstantOp results is only allowed by a hand full of other
OpSpecConstant instructions - which is why we generate constant
structs using OpSpecConstantComposite: These may use OpVariable
and OpSpecConstantOp results, while OpConstantComposite may not.
2023-04-09 01:51:52 +02:00
Robin Voetter
fbe5f0c345
spirv: initial decl_ref pointer generation
Starts lowering decl_ref Pointer constants.
2023-04-09 01:51:52 +02:00
Robin Voetter
c9db6e43af
spirv: generate code directly in updateFunc/updateDecl
This cloneAir/cloneLiveness idea used to ignore Zig's internals
has proven buggy. Instead, just generate the code directly from
updateFunc and updateDecl as the other backends do, but pretend
that Zig is not an incremental compiler. The SPIR-V backend will
for the time being not support this.
2023-04-09 01:51:51 +02:00
Robin Voetter
34b98ee372
spirv: start lowering non-function decls
Start to lower decls which are not functions. These generate
an OpVariable instruction by which they can be used later on.
2023-04-09 01:51:51 +02:00
Robin Voetter
f1229e0f00
spirv: convert bools on load/store
Bools have a different immediate representation and memory
representation - which means that they must be converted every time
a bool is loaded from or stored into memory.
2023-04-09 01:51:51 +02:00
Robin Voetter
caf8461af8
spirv: make locals generic pointers
Taking the address of a local variable should result in a generic
pointer - too much code breaks if we do not do this. We cannot
lower locals into the generic storage class directly though, so
instead, lower the variables into the Function storage class
implicitly, and convert the pointer to a generic pointer.

Also Correct OpInboundsAccessChain generation (we only need
the one index).
2023-04-09 01:51:51 +02:00
Robin Voetter
2a8e784989
spirv: introduce type/value representations
There are two main ways in which a value can be stored: "Direct", as it
will be operated on as an immediate value, and "indirect", as it is stored
in memory. Some types need a different representation here: Bools, for
example, are opaque in SPIR-V, and so these need to have a different
representation in memory. The bool operations are not easily interchangable
with integer operations, though, so they need to be OpTypeBool as
immediate value.
2023-04-09 01:51:51 +02:00
Robin Voetter
8a00ec162c
spirv: more fixes and improvements
- Formatting.
- Improve `decorate` helper function to generate a decoration for a result-id.
- Reorder some functions in a more logical way
2023-04-09 01:51:51 +02:00
Robin Voetter
700dee34d5
spirv: make IdResultType and IdRef weak aliases of IdResult
Previously they were strong aliases, but as these types are used quite
intermittendly it resulted in a lot of toRef() calls. Removing them
improves readability a bit.
2023-04-09 01:51:51 +02:00
Robin Voetter
0c2526b18e
spirv: some fixes and improvements
- Adds the Int8. Int16, Int64 and GenericPointer capabilities.
  TODO: This should integrate with the feature system.
- Default some struct fields of SPIR-V types so that we dont
  need to type them all the time.
- Store struct field name's in SPIR-V types, and generate the
  OpMemberName decoration if they are non-null.
- Also add the field names to the actual SPIR-V types.
- Generate OpName for functions.
2023-04-09 01:51:50 +02:00
Robin Voetter
e443b1bed7
spirv: switch_br lowering
Implements lowering switch statements in the SPIR-V backend.
2023-04-09 01:51:50 +02:00
Robin Voetter
205d928b24
spirv: left shift
Implements the AIR left_shift operation for the SPIR-V backend.
2023-04-09 01:51:50 +02:00
Robin Voetter
ea97966c9e
spirv: struct field ptr index, ptr elem ptr
Implements the ptr_elem_ptr and struct_field_ptr_index_* AIR instructions
for the SPIR-V backend.
2023-04-09 01:51:50 +02:00
Robin Voetter
8608d6e235
spirv: div, rem, intcast, some strange integer masking
Implements the div-family and intcast AIR instructions, and starts
implementing a mechanism for masking the value of 'strange' integers
before they are used in an operation that does not hold under modulo.
2023-04-09 01:51:50 +02:00
Robin Voetter
23e210c38f
spirv: (some) array and struct constants
Starts implementing constant lowering for some array and struct constants.
In particular, TODO are packed structs.
2023-04-09 01:51:50 +02:00
Robin Voetter
bca6f2901a
spirv: enum values, struct_field_val, ret_ptr, ret_load
Implements lowering for enum constants, as well as the struct_field_val,
ret_ptr, and ret_load AIR instructions.
2023-04-09 01:51:49 +02:00
Robin Voetter
6146abee1e
spirv: add_with_overflow
Implements lowering for the add_with_overflow AIR instructions. Also implements
a helper function, simpleStructType, to quickly generate a SPIR-V structure type
without having to do the whole allocation dance.
2023-04-09 01:51:49 +02:00
Robin Voetter
c23d668c79
spirv: slice operations
This commit adds support for SPIR-V code generation for the following AIR
instructions:
- slice_ptr
- slice_len
- slice_elem_ptr
- slice_elem_val
2023-04-09 01:51:49 +02:00
Robin Voetter
3c5ab4dd3d
spirv: add liveness checks
When a result of a pure instruction is not used, it also does not need to
be generated. The other backends already implement these checks, they were
ignored in SPIR-V up until now. New instructions added in the future should
have these be implemented from the start.
2023-04-09 01:51:49 +02:00
Robin Voetter
39016948f0
spirv: slice types
Implements type lowering for slices.
2023-04-09 01:51:49 +02:00
Robin Voetter
3f92eaceb6
spirv: array, structs, bitcast, call
Implements type lowering for arrays and structs, and implements instruction
lowering for bitcast and call. Bitcast currently naively maps to the OpBitcast
instruction - this is only valid for some primitive types, and should be
improved to work with composites.
2023-04-09 01:51:49 +02:00
Robin Voetter
3eafe3033e
spirv: improve storage efficiency for integer and float types
In practice there are only a few variations of these types allowed, so it
kind-of makes sense to write them all out. Because the types are hashed this
does not actually save all that many bytes in the long run, though. Perhaps
some of these types should be pre-registered?
2023-04-09 01:51:48 +02:00
Robin Voetter
a60308f87c
spirv: enum type
This gives the spir-v backend the power to emit enum types. These
are simply lowered to their backing integer type.
2023-04-09 01:51:48 +02:00
Robin Voetter
df5577c28b
spirv: allow more calling conventions
This allows the Zig calling convention and makes way for a Kernel
calling convention in the future. Any future checks on calling
conventions should be placed in Sema.zig.
2023-04-09 01:51:48 +02:00
Andrew Kelley
aeaef8c0ff update std lib and compiler sources to new for loop syntax 2023-02-18 19:17:21 -07:00
Jakub Konka
e0f3975fc8 link: make SpirV atoms fully owned by the linker 2023-02-01 16:01:43 +01:00
Maciej 'vesim' Kuliński
2b9478ce12 Sema: implement AVR address spaces
Co-authored-by: Veikka Tuominen <git@vexu.eu>
2023-01-04 01:26:50 +02:00
Veikka Tuominen
9d93b2ccf1 Eliminate BoundFn type from the language
Closes #9484
2022-12-09 20:37:18 -07:00
Veikka Tuominen
0e38cc16d5 Sema: fix comparisons between lazy and runtime values
Closes #12498
2022-12-03 00:09:23 +02:00
Robin Voetter
bd6a571093
fmt 2022-11-23 19:17:58 +01:00
Robin Voetter
074ba69ba6
spirv: assembler
spirv: introduce SpvModule.Fn to generate function code into

spirv: assembler error message setup

spirv: runtime spec info

spirv: inline assembly tokenizer

spirv: inline assembly lhs result/opcode parsing

spirv: forgot to fmt

spirv: tokenize opcodes and assigned result-ids

spirv: operand parsing setup

spirv: assembler string literals

spirv: assembler integer literals

spirv: assembler value enums

spirv: assembler bit masks

spirv: update assembler to new asm air format

spirv: target 1.5 for now

Current vulkan sdk version (1.3.204) ships spirv tools targetting 1.5,
and so these do not work with binaries targetting 1.6 yet. In the
future, this version number should be decided by the target.

spirv: store operands in flat arraylist.

Instead of having dedicated Operand variants for variadic operands,
just flatten them and store them in the normal inst.operands list.
This is a little simpler, but is not easily decodable in the operand
data representation.

spirv: parse variadic assembly operands

spirv: improve assembler result-id tokenization

spirv: begin instruction processing

spirv: only remove decl if it was actually allocated

spirv: work around weird miscompilation

Seems like there are problems with switch in anonymous struct literals.

spirv: begin resolving some types in assembler

spirv: improve instruction processing

spirv: rename some types + process OpTypeInt

spirv: process OpTypeVector

spirv: process OpTypeMatrix and OpTypeSampler

spirv: add opcode class to spec, remove @exclude'd instructions

spirv: process more type instructions

spirv: OpTypeFunction

spirv: OpTypeOpaque

spirv: parse LiteralContextDependentNumber operands

spirv: emit assembly instruction into right section

spirv: parse OpPhi parameters

spirv: inline assembly inputs

spirv: also copy air types

spirv: inline assembly outputs

spirv: spir-v address spaces

spirv: basic vector constants/types and shuffle

spirv: assembler OpTypeImage

spirv: some stuff

spirv: remove spirv address spaces for now
2022-11-23 19:17:58 +01:00
Andrew Kelley
bac132bc8f introduce std.debug.Trace
And use it to debug a LazySrcLoc in stage2 that is set to a bogus value.

The actual fix in this commit is:

```diff
-        try sema.emitBackwardBranch(&child_block, call_src);
+        try sema.emitBackwardBranch(block, call_src);
```
2022-06-09 15:37:16 -07:00
Isaac Freund
6f4343b61a std: replace usage of std.meta.bitCount() with @bitSizeOf() 2022-04-27 11:10:52 +02:00
Andrew Kelley
f7596ae942 stage2: use indexes for Decl objects
Rather than allocating Decl objects with an Allocator, we instead allocate
them with a SegmentedList. This provides four advantages:
 * Stable memory so that one thread can access a Decl object while another
   thread allocates additional Decl objects from this list.
 * It allows us to use u32 indexes to reference Decl objects rather than
   pointers, saving memory in Type, Value, and dependency sets.
 * Using integers to reference Decl objects rather than pointers makes
   serialization trivial.
 * It provides a unique integer to be used for anonymous symbol names,
   avoiding multi-threaded contention on an atomic counter.
2022-04-20 17:37:35 -07:00