aboutsummaryrefslogtreecommitdiff
path: root/src/arch/wasm/CodeGen.zig
AgeCommit message (Collapse)Author
2023-05-31wasm: `memcpy` support elem abi-size > 1Luuk de Gram
Previously it was incorrectly assumed that all memcopy's generated by the `memcpy` AIR instruction had an element size of 1 byte. However, this would result in miscompilations for pointer's to arrays where the element size of the array was larger than 1 byte. We now corectly calculate this size.
2023-05-26std.Target adjustmentsVeikka Tuominen
* move `ptrBitWidth` from Arch to Target since it needs to know about the abi * double isn't always 8 bits * AVR uses 1-byte alignment for everything in GCC
2023-05-26wasm backend: emit a TODO error rather than miscompileAndrew Kelley
2023-05-19wasm: aggregate_init - ensure zeroed result localLuuk de Gram
When initializing a packed struct, we must ensure the result local is zero'd. Previously we would do this by ensuring a new local is allocated. Although a local is always zero by default, it meant that if such an initialization was being done inside a loop, it would re- use that very same local that could potentially still hold a different value. Because this value is `or`'d with the value, it would result in a miscompilation. By manually setting this result to 0, we guarantee the correct behavior.
2023-05-19wasm: fix `div_trunc` for floatsLuuk de Gram
For floats we would previously only do the division, but not the truncation for floats. This would result in incorrect values being returned.
2023-05-19wasm: support `memset` for elem abi size > 1Luuk de Gram
Previously we incorrectly assumed all memset's to have its element abi-size be 1 byte. This would set the region of memory incorrectly. We now have a more efficient loop, as well as support any element type by re-using the `store` function for each element and moving the pointer by 1 element.
2023-05-19wasm: implement `shl` for big integersLuuk de Gram
2023-05-19wasm: memset - correctly load the ptr for slicesLuuk de Gram
Previously we would use the address of the slice itself, which would result in miscompilations and accidently setting the memory region of the slice itself, rather than based on the `ptr` field.
2023-05-19wasm: correctly use elem type when loweringLuuk de Gram
Previously when lowering a value of `elem_ptr` we would multiply the abisize of the parent type by the index, rather than the element type. This would result in an invalid pointer way beyond the correct pointer. We now also pass the current offset to each recursive call to ensure we do not miss inner offsets.
2023-05-19wasm: fix return `ret_load` with zero-size typeLuuk de Gram
When we have a `ret_load` instruction with a zero-sized type which was not an error, we would not emit any instruction. This resulted in no `return` instruction and also not correctly resetting the global stack_pointer. This commit also enables the regular test runner for the WebAssembly backend.
2023-05-19wasm: fix double free of localsLuuk de Gram
A copy was being made of a WValue variable, which meant the call to `free` would insert the local that was being held by said WValue was appended to the free list twice. This led to the same local being reused even though it wasn't free and would lead to it being over- written by a new value.
2023-05-19wasm: simplify merging of branchesLuuk de Gram
Rather than adding all values that were generated in the child branch, we simply discard them as outer branches cannot refer to values produced from an inner branch.
2023-05-19wasm: add `dead` tag to `WValue`Luuk de Gram
This new tag is used for freed locals that are not allowed to have any remaining references pointing to it. This new tag allows us to easily identify liveness bugs. Previously we would set the entire region to `undefined` which would incorrectly set the tag to `function_index`, making codegen think it was a valid `WValue` while it wasn't.
2023-05-19wasm: more liveness fixesLuuk de Gram
2023-05-19wasm: fix liveness bugsLuuk de Gram
Make sure to increase the reference count for `intcast` when the operand doesn't require any casting of the respective WebAssembly type. Function arguments have a reserved slot, and therefore cannot be re-used arbitrarily
2023-05-19wasm: fix miscompilation for shiftingLuuk de Gram
This fix ensures that when we are shifting left or right, both operands have the same WebAssembly type. e.g. it's not possible to shift a 64 bit integer and 32 bit integer together and will fail WebAssembly's validator. By first coercing the values to the same type, we ensure we satisfy the validator.
2023-05-19wasm: implement `@mulWithOverflow` for big intsLuuk de Gram
Currently we only support exact 128 bit *unsigned* integers
2023-05-19wasm: implement `@addWithOverflow` for 64bit intsLuuk de Gram
2023-05-19wasm: implement mul, shl and xor for big intsLuuk de Gram
Uses compiler-rt for multiplication and shifting left, while lowers it down using regular instructions for xor.
2023-05-19wasm: implement `@frameAddress`Luuk de Gram
2023-04-27Merge pull request #15474 from Luukdegram/wasm-atomicsAndrew Kelley
wasm: implement atomic instructions
2023-04-26wasm: support pointers in `cmpxchg`Luuk de Gram
2023-04-26wasm: implement atomic storesLuuk de Gram
2023-04-26wasm: implement `@fence`Luuk de Gram
Uses the `atomic.fence` instruction for multi-thread-enabled builds where the `atomics` feature is enabled for the wasm32 target. In all other cases, this lowers to a nop.
2023-04-26wasm: implement `@atomicRmw`Luuk de Gram
Implements the lowering of the `@atomicRmw` builtin. Uses the atomic opcodes when the cpu feature `atomics` is enabled. Otherwise lowers it to regular instructions. For the operations that do not lower to a direct atomic opcode, we use a loop in combiantion with a cmpxchg to ensure the swapping of values is doing atomically.
2023-04-26wasm: implement `@atomicLoad`Luuk de Gram
Uses the atomic instructions when cpu feature is enabled, otherwise lowers it down to a regular load.
2023-04-26wasm: use atomic feature for `@cmpxchg` when enabledLuuk de Gram
When the user passes the cpu feature `atomics` to the target triple, the backend will lower the AIR instruction using opcodes from the atomics feature instead of manually lowering it.
2023-04-26wasm: implement `cmpxchg{weak/strong}`Luuk de Gram
2023-04-25stage2: introduce store_safe AIR instructionAndrew Kelley
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-25wasm backend: fix airMemset with slicesAndrew Kelley
2023-04-25LLVM backend: support non-byte-sized memsetAndrew Kelley
Also introduce memset_safe AIR tag and support it in C backend and LLVM backend.
2023-04-25wasm backend: implement new memcpy/memset and ptrtoint semanticsAndrew Kelley
2023-04-22wasm: store `__zig_lt_errors_len` in linear dataLuuk de Gram
Rather than using a function call to verify if an error fits within the global error set's length, we now store the error set' size in the .rodata segment of the linear memory and load that value onto the stack to check with the integer value.
2023-04-22wasm: implement `error_set_has_value`Luuk de Gram
This implements the safety check for error casts. The instruction generates a jump table with 2 possibilities. The operand is used as an index into the jump table. For cases where the value does not exist within the error set, it will generate a jump to the 'false' block. For cases where it does exist, it will generate a jump to the 'true' block. By calculating the highest and lowest value we can keep the jump table smaller, as it doesn't need to contain an index into the entire error set.
2023-04-22wasm: implement `cmp_lt_errors_len` instructionLuuk de Gram
Creates a global undefined symbol when this instruction is called. The linker will then resolve it as a lazy symbol, ensuring it is only generated when the symbol was created. In `flush` it will then generate the function as only then, all errors are known and we can generate the function body. This logic allows us to re-use the same functionality of linker-synthetic-functions.
2023-04-20wasm: integrate new Liveness behaviourLuuk de Gram
Uses the new liveness behaviour. This also removes useless calls to `processDeath` on branches that were just initialized. Branch consolidation and processing deaths on branches inside `condbr` is still a TODO, just like before. This also skips var_args on other native backends as they do not support this feature yet.
2023-04-20Liveness: control flow analysismlugg
This is a partial rewrite of Liveness, so has some other notable changes: - A proper multi-pass system to prevent code duplication - Better logging - Minor bugfixes
2023-04-12wasm: make tagName null-terminatedLuuk de Gram
2023-04-12wasm: generate unnamed constant for tagLuuk de Gram
2023-04-12wasm: generate function to get tag nameLuuk de Gram
When we lower the instruction for `@tagName` we generate a new function if it doesn't exist yet for that decl. This function creates an if-else chain to determine which value was provided. Each tag generates a constant that contains its name as the value. For each tag we generate a case where the pointer of the string is stored in the result slice. The length of the tagname is comptime-known, therefore will be stored in the slice directly without having it being part of the tagname symbol. In the future this can use a jump table instead of an if-else chain, similar to the `switch` instruction.
2023-04-07Fix 32-bit compile errorsAuguste Rame
2023-04-07Handle compile time case for vector element access using lane accessAuguste Rame
2023-04-07Finish shuffle, fix arrayElemVal for vectorsAuguste Rame
2023-04-07Make airShuffle work for unrolledAuguste Rame
2023-04-07Merge pull request #14668 from Techatrix/wasm-floatopsLuuk de Gram
wasm: implement float operations with compiler-rt
2023-04-07Merge pull request #15195 from mlugg/fix/liveness-loop-defer-deathsAndrew Kelley
Liveness: defer deaths of externally-scoped instructions in loop bodies
2023-04-07Make self-hosted wasm @returnAddress return 0Auguste Rame
2023-04-07Liveness: defer deaths of externally-scoped instructions in loop bodiesmlugg
2023-03-30new builtins: @workItemId, @workGroupId, @workGroupSizeRobin Voetter
* @workItemId returns the index of the work item in a work group for a dimension. * @workGroupId returns the index of the work group in the kernel dispatch for a dimension. * @workGroupSize returns the size of the work group for a dimension. These builtins are mainly useful for GPU backends. They are currently only implemented for the AMDGCN LLVM backend.
2023-03-28link: pass expected lib name as hint in getGlobalSymbol()Jakub Konka