aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/llvm.zig
AgeCommit message (Collapse)Author
2022-03-08LLVM: add debug info for parametersAndrew Kelley
2022-03-08LLVM: fix not handling dbg_stmt relative lineAndrew Kelley
Also make `namespaceToDebugScope` behave correctly for file-level structs. Instead of being inside their own scope, they use the file scope.
2022-03-08LLVM: add debug type lowering for ptr, slice, opaque, optionalAndrew Kelley
also fix issue with memoization and recursiveness.
2022-03-08LLVM: memoize debug types and add enum debug typesAndrew Kelley
2022-03-08LLVM: add DISubprogram and DIType lowering; handle dbg_stmtAndrew Kelley
2022-03-08LLVM: add compile unit to debug infoAndrew Kelley
This commit also adds a bunch of bindings for debug info.
2022-03-06stage2: rework `@mulAdd`Andrew Kelley
* mul_add AIR instruction: use `pl_op` instead of `ty_pl`. The type is always the same as the operand; no need to waste bytes redundantly storing the type. * AstGen: use coerced_ty for all the operands except for one which we use to communicate the type. * Sema: use the correct source location for requireRuntimeBlock in handling of `@mulAdd`. * native backends: handle liveness even for the functions that are TODO. * C backend: implement `@mulAdd`. It lowers to libc calls. * LLVM backend: make `@mulAdd` handle all float types. - improved fptrunc and fpext to handle f80 with compiler-rt calls. * Value.mulAdd: handle all float types and use the `@mulAdd` builtin. * behavior tests: revert the changes to testing `@mulAdd`. These changes broke the test coverage, making it only tested at compile-time. Improved f80 support: * std.math.fma handles f80 * move fma functions from freestanding libc to compiler-rt - add __fmax and fmal - make __fmax and fmaq only exported when they don't alias fmal. - make their linkage weak just like the rest of compiler-rt symbols. * removed `longDoubleIsF128` and replaced it with `longDoubleIs` which takes a type as a parameter. The implementation is now more accurate and handles more targets. Similarly, in stage2 the function CTypes.sizeInBits is more accurate for long double for more targets.
2022-03-06stage2: implement `@mulAdd` for scalar floatsJohn Schmidt
2022-03-06stage2: Fix wasm linker for llvm backendLuuk de Gram
This fixes 2 entrypoints within the self-hosted wasm linker that would be called for the llvm backend, whereas we should simply call into the llvm backend to perform such action. i.e. not allocate a decl index when we have an llvm object, and when flushing a module, we should be calling it on llvm's object, rather than have the wasm linker perform the operation. Also, this fixes the wasm intrinsics for wasm.memory.size and wasm.memory.grow. Lastly, this commit ensures that when an extern function is being resolved, we tell LLVM how to import such function.
2022-03-03stage2: cleanups to wasm memory intrinsicsAndrew Kelley
* AIR: use pl_op instead of ty_pl for wasm_memory_size. No need to store the type because the type is always `u32`. * AstGen: use coerced_ty for `@wasmMemorySize` and `@wasmMemoryGrow` and do the coercions in Sema. * Sema: use more accurate source locations for errors. * Provide more information in the compiler error message. * Codegen: use liveness data to avoid lowering unused `@wasmMemorySize`. * LLVM backend: add implementation - I wasn't able to test it because we are hitting a linker error for `-target wasm32-wasi -fLLVM`. * C backend: use `zig_unimplemented()` instead of silently doing wrong behavior for these builtins. * behavior tests: branch only on stage2_arch for inclusion of the wasm.zig file. We would change it to `builtin.cpu.arch` but that is causing a compiler crash on some backends.
2022-03-03Update behavior testsLuuk de Gram
2022-03-03wasm: Implement `@wasmMemoryGrow` builtinLuuk de Gram
Similarly to the other wasm builtin, this implements the grow variation where the memory index is a comptime known value. The operand as well as the result are runtime values. This also verifies during semantic analysis the target we're building for is wasm, or else emits a compilation error. This means that other backends do not have to handle this AIR instruction, other than the wasm and LLVM backends.
2022-03-03wasm: Implement `@wasmMemorySize()` builtinLuuk de Gram
This implements the `wasmMemorySize` builtin, in Sema and the Wasm backend. The Stage2 implementation differs from stage1 in the way that `index` must be a comptime value. The stage1 variant is incorrect, as the index is part of the instruction encoding, and therefore, cannot be a runtime value.
2022-03-03stage2 llvm: Lower 0-bit field-ptr as ptr-to-voidCody Tapscott
2022-03-03LLVM: fix lowering of unions and switchesAndrew Kelley
`Module.Union.getLayout` now additionally returns a `padding` field which tells how many bytes are between the final field end offset and the ending offset of the union. This is used by the LLVM backend to explicitly insert padding. LLVM backend: lowering of unions now inserts additional padding so that LLVM's internals will agree on the ABI size to match what ABI size zig wants unions to be. This is an alternative to calling LLVMABISizeOfType and LLVMABIAlignmentOfType which end up crashing when recursive struct definitions come into play. We no longer ever call these two functions and the bindings are deleted to avoid future footgun firings. LLVM backend: lowering of unions now represents untagged unions consistently. Before it was tripping an assertion. LLVM backend: switch cases call inttoptr on the case items and condition if necessary. Prevents tripping an LLVM assertion. After this commit, we are no longer tripping over any LLVM assertions.
2022-03-02LLVM: aggregate_init supports structsAndrew Kelley
in addition to tuples
2022-03-02stage2: implement `@extern`Veikka Tuominen
2022-03-01stage2: introduce anonymous struct literalsAndrew Kelley
2022-03-01LLVM: add extra padding to structs and tuples sometimesAndrew Kelley
* Sema: resolve type fully when emitting an alloc AIR instruction to avoid tripping assertion for checking struct field alignment. * LLVM backend: keep a reference to the LLVM target data alive during lowering so that we can ask LLVM what it thinks the ABI alignment and size of LLVM types are. We need this in order to lower tuples and structs so that we can put in extra padding bytes when Zig disagrees with LLVM about the size or alignment of something. * LLVM backend: make the LLVM struct type packed that contains the most aligned union field and the padding. This prevents the struct from being too big according to LLVM. In the future, we may want to consider instead emitting unions in a "flat" manner; putting the tag, most aligned union field, and padding all in the same struct field space. * LLVM backend: make structs with 2 or fewer fields return isByRef=false. This results in more efficient codegen. This required lowering of bitcast to sometimes store the struct into an alloca, ptrcast, and then load because LLVM does not allow bitcasting structs. * enable more passing behavior tests.
2022-03-01LLVM: fix tripping assertionsAndrew Kelley
Packed structs were tripping an LLVM assertion due to calling `LLVMConstZExt` from i16 to i16. Solved by using instead `LLVMConstZExtOrBitCast`. Unions were tripping an LLVM assertion due to a typo using the union llvm type to construct an integer value rather than the tag type.
2022-03-01LLVM: fix when sret and isByRef ret_ty disagreeAndrew Kelley
This can happen functions use the C ABI.
2022-02-28stage2: fix frame_address AIR instructionAndrew Kelley
Various places were assuming different union tags. Now it is consistently a no-op instruction, just like the similar instruction ret_addr.
2022-02-28stage2: implement `@frameAddress`Veikka Tuominen
2022-02-27stage2: fix bitcast to optional ptr in llvm backend; omit safety check for ↵Veikka Tuominen
intToPtr on optional ptr
2022-02-26stage2: implement `@unionInit`Andrew Kelley
The ZIR instruction `union_init_ptr` is renamed to `union_init`. I made it always use by-value semantics for now, not taking the time to invest in result location semantics, in case we decide to change the rules for unions. This way is much simpler. There is a new AIR instruction: union_init. This is for a comptime known tag, runtime-known field value. vector_init is renamed to aggregate_init, which solves a TODO comment.
2022-02-26stage2: various fixes to get one test passingVeikka Tuominen
* resolve error sets before merging them * implement tupleFieldPtr * make ret_ptr behave like alloc with zero sized types in llvm backend
2022-02-24stage2: implement fieldParentPtrVeikka Tuominen
2022-02-23stage2: integer-backed packed structsAndrew Kelley
This implements #10113 for the self-hosted compiler only. It removes the ability to override alignment of packed struct fields, and removes the ability to put pointers and arrays inside packed structs. After this commit, nearly all the behavior tests pass for the stage2 llvm backend that involve packed structs. I didn't implement the compile errors or compile error tests yet. I'm waiting until we have stage2 building itself and then I want to rework the compile error test harness with inspiration from Vexu's arocc test harness. At that point it should be a much nicer dev experience to work on compile errors.
2022-02-21Sema: fix comptime union initializationAndrew Kelley
The mechanism behind initializing a union's tag is a bit complicated, depending on whether the union is initialized at runtime, forced comptime, or implicit comptime. `coerce_result_ptr` now does not force a block to be a runtime context; instead of adding runtime instructions directly, it forwards analysis to the respective functions for initializing optionals and error unions. `validateUnionInit` now has logic to still emit a runtime `set_union_tag` instruction even if the union pointer is comptime-known, for the case of a pointer that is not comptime mutable, such as a variable or the result of `@intToPtr`. `validateStructInit` looks for a completely different pattern now; it now handles the possibility of the corresponding AIR instruction for the `field_ptr` to be missing or the corresponding `store` to be missing. See the new comment added to the function for more details. An equivalent change should probably be made to `validateArrayInit`. `analyzeOptionalPayloadPtr` and `analyzeErrUnionPayloadPtr` functions now emit a `optional_payload_ptr_set` or `errunion_payload_ptr_set` instruction respectively if `initializing` is true and the pointer value is not comptime-mutable. `storePtr2` now tries the comptime pointer store before checking if the element type has one possible value because the comptime pointer store can have side effects of setting a union tag, setting an optional payload non-null, or setting an error union to be non-error. The LLVM backend `lowerParentPtr` function is improved to take into account the differences in how the LLVM values are lowered depending on the Zig type. It now handles unions correctly as well as additionally handling optionals and error unions. In the LLVM backend, the instructions `optional_payload_ptr_set` and `errunion_payload_ptr_set` check liveness analysis and only do the side effects in the case the result of the instruction is unused. A few wasm and C backend test cases regressed, but they are due to TODOs in lowering of constants, so this is progress.
2022-02-21Merge pull request #10925 from Vexu/stage2Andrew Kelley
stage2: support anon init through error unions and optionals
2022-02-21enable Gpu address spaces (#10884)gwenzek
2022-02-19stage2: implement errunion_payload_ptr_setVeikka Tuominen
2022-02-19stage2: correct use of .unwrap_err_union_* in LLVM and C backendVeikka Tuominen
2022-02-19Merge pull request #10924 from ziglang/air-independence-dayAndrew Kelley
AIR independence day
2022-02-18stage2: make AIR not reference ZIR for inline assemblyAndrew Kelley
Instead it stores all the information it needs to into AIR. closes #10784
2022-02-18Add backend-specific skips for bitreverse, byteswap testsCody Tapscott
2022-02-18stage2: Implement `@bitReverse` and `@byteSwap`Cody Tapscott
This change implements the above built-ins for Sema and the LLVM backend. Other backends have had placeholders added for lowering.
2022-02-14LLVM backend: refactor LLVM bitcount ops (#10882)John Schmidt
Use `llvm.getIntrinsic` instead of `llvm.getNamedFunction`
2022-02-12stage2: implement `@popCount` for SIMD vectorsAndrew Kelley
2022-02-12LLVM backend: handle unnamed structs when lowering array valuesAndrew Kelley
LLVM doesn't support lowering union values, so we have to use unnamed structs to do it, which means any type that contains a union as an element, even if it is nested in another type, has to have a mechanism to detect when it can't be lowered normally and has to resort itself to an unnamed struct. This includes arrays.
2022-02-12LLVM backend: avoid creating invalid LLVM typesAndrew Kelley
Fixes assertions from creating i0 types which are not allowed in LLVM.
2022-02-12LLVM backend: call constPtrToInt instead of constBitCastAndrew Kelley
when appropriate. Avoids tripping an LLVM assertion.
2022-02-12LLVM backend: fix union with only 1 tag tripping llvm assertionAndrew Kelley
2022-02-12make f80 less hacky; lower as u80 on non-x86Andrew Kelley
Get rid of `std.math.F80Repr`. Instead of trying to match the memory layout of f80, we treat it as a value, same as the other floating point types. The functions `make_f80` and `break_f80` are introduced to compose an f80 value out of its parts, and the inverse operation. stage2 LLVM backend: fix pointer to zero length array tripping LLVM assertion. It now checks for when the element type is a zero-bit type and lowers such thing the same way that pointers to other zero-bit types are lowered. Both stage1 and stage2 LLVM backends are adjusted so that f80 is lowered as x86_fp80 on x86_64 and i386 architectures, and identical to a u80 on others. LLVM constants are lowered in a less hacky way now that #10860 is fixed, by using the expression `(exp << 64) | fraction` using llvm constants. Sema is improved to handle c_longdouble by recursively handling it correctly for whatever the float bit width is. In both stage1 and stage2.
2022-02-12stage1: fix f80 size and alignment on x86 and armAndrew Kelley
* F80Repr extern struct needs no explicit padding; let's match the target padding. * stage2: fix lowering of f80 constants. * stage1: decide ABI size and alignment of f80 based on alignment of u64. x86 has alignof u64 equal to 4 but arm has it as 8. * stage2: fix Value.floatReadFromMemory to use F80Repr
2022-02-12stage2: LLVM backend: adjust replaceAllUsesWith usageAndrew Kelley
replaceAllUsesWith requires the type to be unchanged. So we bitcast the new global to the old type and use that as the thing to replace old uses. Fixes an LLVM assertion found while troubleshooting #10837.
2022-02-10stage2: LLVM backend: make unnamed struct globalsAndrew Kelley
LLVM union globals have to be lowered as unnamed structs if the non-most-aligned field is the active tag. In this case it bubbles up so that structs containing unions have the same restriction. This fix needs to be applied to optionals and other callsites of createNamedStruct. The bug fixed in this commit was revealed in searching for the cause of #10837.
2022-02-09stage2: implement all builtin floatops for f{16,32,64}John Schmidt
- Merge `floatop.zig` and `floatop_stage1.zig` since most tests now pass on stage2. - Add more behavior tests for a bunch of functions.
2022-02-07stage2: implement @sqrt for f{16,32,64}John Schmidt
Support for f128, comptime_float, and c_longdouble require improvements to compiler_rt and will implemented in a later PR. Some of the code in this commit could be made more generic, for instance `llvm.airSqrt` could probably be `llvm.airUnaryMath`, but let's cross that bridge when we get to it.
2022-02-07Merge pull request #10803 from ziglang/decl-has-lib-nameAndrew Kelley
stage2: store externs lib name as part of decl