aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/spirv.zig
AgeCommit message (Collapse)Author
2023-05-20spirv: fix some (Ptr)AccessChain usesRobin Voetter
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-20spirv: dont use OpIAddCarryRobin Voetter
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-20spirv: use intInfo instead of arithmeticTypeInfo in airIntCastRobin Voetter
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-20spirv: implement pointer comparison in for air cmpRobin Voetter
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-20spirv: lower get_union_tagAli Chraghi
2023-05-15spirv: lower ptrtoint & ignore dbg_inline instructionsAli Chraghi
2023-05-15spirv: implement arithmeticTypeInfo for Enum (`@intToEnum`)Ali Chraghi
2023-05-15spirv: lower float_to_int and int_to_floatAli Chraghi
2023-05-11spirv: lower store_safe, trunc and trapAli Chraghi
2023-05-11spirv: make decl deps a hash map instead of an arraylistRobin Voetter
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-11spirv: lower wrap_optionalRobin Voetter
This implements the wrap_optional AIR instruction.
2023-05-11spirv: lower air optional_payloadRobin Voetter
Implements lowering the AIR tag optional_payload. Also fixes an issue with lowering constant ints.
2023-05-11spirv: lower air is_null, is_non_nullRobin Voetter
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-11spirv: fix invalid code generated by br-with-valueRobin Voetter
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-11spirv: lower air wrap_errunion_errRobin Voetter
2023-05-11spirv: lower air unwrap_error_union_errRobin Voetter
2023-05-11spirv: lower air tryRobin Voetter
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-11spirv: fix OpFunctionCall parameters interleaving with instsRobin Voetter
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-04-20Begin integrating new liveness analysis into remaining backendsmlugg
2023-04-09spirv: Do not generate the Alignment attribute on pointers for nowRobin Voetter
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-09spirv: allow global, constant address spacesRobin Voetter
These should actually just work fine in SPIR-V. They are mapped to CrossWorkgroup and UniformConstant respectively.
2023-04-09spirv: export functions with .Kernel callconv as entry pointRobin Voetter
Exported functions which have the .Kernel calling convention are now exported as entry point. This also works with @export.
2023-04-09spirv: emit interface variables for entry pointsRobin Voetter
Also actually implement generating the OpEntryPoint instructions.
2023-04-09spirv: add decl dependencies for functions alsoRobin Voetter
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-09spirv: temporarily emit test kernelsRobin Voetter
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-09spirv: implement error set and error unionsRobin Voetter
Implements lowering types and constants for error sets and error unions.
2023-04-09spirv: improve linking globalsRobin Voetter
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-09spirv: overhaul constant loweringRobin Voetter
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-09spirv: union types/constantsRobin Voetter
Implements lowering union types and constants in the SPIR-V backend.
2023-04-09spirv: optional constantsRobin Voetter
Implements lowering optional constants in the SPIR-V backend.
2023-04-09spirv: improve genConstant usageRobin Voetter
This little wrapper function allocates a result-id for us, so that we don't have to do that.
2023-04-09spirv: slice constantsRobin Voetter
Implements lowering slice constants in the SPIR-V backend
2023-04-09spirv: string literalsRobin Voetter
Implements lowering string literal constants in the SPIR-V backend
2023-04-09spirv: optional typesRobin Voetter
Implements lowering optional types in the SPIR-V backend.
2023-04-09spirv: generic global pointersRobin Voetter
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-09spirv: initial decl_ref pointer generationRobin Voetter
Starts lowering decl_ref Pointer constants.
2023-04-09spirv: generate code directly in updateFunc/updateDeclRobin Voetter
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-09spirv: start lowering non-function declsRobin Voetter
Start to lower decls which are not functions. These generate an OpVariable instruction by which they can be used later on.
2023-04-09spirv: convert bools on load/storeRobin Voetter
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-09spirv: make locals generic pointersRobin Voetter
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-09spirv: introduce type/value representationsRobin Voetter
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-09spirv: more fixes and improvementsRobin Voetter
- Formatting. - Improve `decorate` helper function to generate a decoration for a result-id. - Reorder some functions in a more logical way
2023-04-09spirv: make IdResultType and IdRef weak aliases of IdResultRobin Voetter
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-09spirv: some fixes and improvementsRobin Voetter
- 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-09spirv: switch_br loweringRobin Voetter
Implements lowering switch statements in the SPIR-V backend.
2023-04-09spirv: left shiftRobin Voetter
Implements the AIR left_shift operation for the SPIR-V backend.
2023-04-09spirv: struct field ptr index, ptr elem ptrRobin Voetter
Implements the ptr_elem_ptr and struct_field_ptr_index_* AIR instructions for the SPIR-V backend.
2023-04-09spirv: div, rem, intcast, some strange integer maskingRobin Voetter
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-09spirv: (some) array and struct constantsRobin Voetter
Starts implementing constant lowering for some array and struct constants. In particular, TODO are packed structs.
2023-04-09spirv: enum values, struct_field_val, ret_ptr, ret_loadRobin Voetter
Implements lowering for enum constants, as well as the struct_field_val, ret_ptr, and ret_load AIR instructions.