aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/llvm.zig
AgeCommit message (Collapse)Author
2022-05-31stage2: introduce support for noaliasAndrew Kelley
Not implemented yet is enhancements to coerceInMemory to account for noalias parameters. Related to #11498.
2022-05-31LLVM: integrate param attrs with iterateParamTypesAndrew Kelley
This moves some logic from resolveLlvmFunction to updateFunc and takes advantage of the iteration we already do that takes into account C ABI lowering, making LLVM parameter attributes accurate for C ABI functions as well as our own unspecified calling convention. Related to #11498.
2022-05-31LLVM: add readonly, nonnull, align attributes to pointer paramsAndrew Kelley
2022-05-31LLVM: elide some loads when loweringAndrew Kelley
Generally, the load instruction may need to make a copy of an isByRef=true value, such as in the case of the following code: ```zig pub fn swap(comptime T: type, a: *T, b: *T) void { const tmp = a.*; a.* = b.*; b.* = tmp; } ``` However, it only needs to do so if there are any instructions which can possibly write to memory. When calling functions with isByRef=true parameters, the AIR code that is generated looks like loads followed directly by call. This allows for a peephole optimization when lowering loads: if the load instruction operates on an isByRef=true type and dies before any side effects occur, then we can safely lower the load as a no-op that returns its operand. This is one out of three changes I intend to make to address #11498. However I will put these changes in separate branches and merge them separately so that we can have three independent points on the perf charts.
2022-05-31LLVM: omit memset of 0xaa bytes in unsafe optimization modesAndrew Kelley
This is one out of three changes I intend to make to address #11498. However I will put these changes in separate branches and merge them separately so that we can have three independent points on the perf charts.
2022-05-25stage2: implement runtime array multiplicationAndrew Kelley
Additionally: * Sema: fix array cat/mul not setting the sentinel value - This required an LLVM backend enhancement to the handling of the AIR instruction aggregate_init that likely needs to be propagated to the other backends. * Sema: report integer overflow of array concatenation in a proper compile error instead of crashing. * Sema: fix not using proper pointer address space for array cat/mul
2022-05-24stage2: treat `error{}!void` as a zero-bit typeAndrew Kelley
2022-05-24LLVM: rename two functionsAndrew Kelley
llvmType -> lowerType genTypedValue -> lowerValue
2022-05-24stage2: make `?anyerror` represented the same as `anyerror`Andrew Kelley
I was able to get the backend implementation working on LLVM and the C backend, but I'm going to ask for some help on the other backends.
2022-05-24stage2: fixes for error union semanticsAndrew Kelley
* Sema: avoid unnecessary safety checks when an error set is empty. * Sema: make zirErrorToInt handle comptime errors that are represented as integers. * Sema: make empty error sets properly integrate with typeHasOnePossibleValue. * Type: correct the ABI alignment and size of error unions which have both zero-bit error set and zero-bit payload. The previous code did not account for the fact that we still need to store a bit for whether there is an error. * LLVM: lower error unions possibly with the payload first or with the error code first, depending on alignment. Previously it always put the error code first and used a padding array. * LLVM: lower functions which have an empty error set as the return type the same as anyerror, so that they can be used where fn()anyerror function pointers are expected. In such functions, Zig will lower ret to returning zero instead of void. As a result, one more behavior test is passing.
2022-05-24stage2: string literal interningAndrew Kelley
This is a temporary addition to stage2 in order to match stage1 behavior, however the end-game once the lang spec is settled will be to use a global InternPool for comptime memoized objects, making this behavior consistent across all types, not only string literals. Or, we might decide to not guarantee string literals to have equal comptime pointers, in which case this commit can be reverted.
2022-05-22Bump support macOS versions; clean up allocs in llvm.targetTripleJakub Konka
2022-05-22Return an error when macOS ABI is not {none, simulator, macabi}Jakub Konka
2022-05-22stage2: append min version to target triple when lowering to LLVMJakub Konka
2022-05-21LLVM: fix calling convention lowering involving pointersAndrew Kelley
The previous commit caused LLVM module verification failure because we attemped to bitcast LLVM pointers to i64 parameters. This is exactly what we want, however it's technically not allowed according to LLVM's type system. It could have been fixed trivially by using ptrtoint instead of bitcast in the case of pointers, however, out of concern for inttoptr being problematic for the optimizer, I put in special code to detect when a given parameter can be treated as its actual type rather than an integer type. This makes Zig's output LLVM IR closer to what Clang outputs.
2022-05-20LLVM: rework calling convention loweringAndrew Kelley
The previous implementation of calling conventions was hacky and broken. This commit reworks lowerFnParamTy into iterateParamTypes which returns enum tags indicating how to handle each parameter. This is then used in the three places that matter: * lowering a function type to llvm type * converting function parameters to the canonical type representation (with respect to isByRef). * converting canonical type representation to function arguments at callsites (again with respect to isByRef). As a result, we are one step closer to the C ABI tests passing. Before this commit, attempting to build them crashed the compiler. I isolated the broken function and verified that it now is lowered correctly. I will keep working on this one piece at a time until all the C ABI tests pass, and then I will enable all of them in the CI.
2022-05-17stage2: fix pointer arithmetic result typeAndrew Kelley
This makes it so the result of doing pointer arithmetic creates a new pointer type that has adjusted alignment.
2022-05-17LLVM: support mixing extern and export with the same symbol nameAndrew Kelley
2022-05-16Sema: do not call `returnError` when returning payload of error unionVeikka Tuominen
2022-05-16stage2: enable error return tracing on llvm backendVeikka Tuominen
2022-05-16stage2: disable error return tracing on unsupported targetsVeikka Tuominen
2022-05-16stage2: implement error return tracesVeikka Tuominen
2022-05-16LLVM: correctly pad result tuple of `airOverflow`William Sengir
2022-05-16stage2: vectorize shl_with_overflow in LLVM backendWilliam Sengir
2022-05-16stage2: fix {add,sub,mul}_with_overflow vectorization in LLVM backendWilliam Sengir
2022-05-13target: Rename sparcv9 -> sparc64Koakuma
Rename all references of sparcv9 to sparc64, to make Zig align more with other projects. Also, added new function to convert glibc arch name to Zig arch name, since it refers to the architecture as sparcv9. This is based on the suggestion by @kubkon in PR 11847. (https://github.com/ziglang/zig/pull/11487#pullrequestreview-963761757)
2022-05-10Add Visibility field to ExportOptions.Takeshi Yoneda
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-05-08Add Win64 calling conventionHannes Bredberg
Closes ziglang/zig#11585
2022-05-06LLVM: rework the previous commitAndrew Kelley
Idiomatic Zig, use const instead of var, simplify the logic.
2022-05-05stage2,llvm: handle softfloats in @intToFloat and @floatToIntJakub Konka
If the hw doesn't have support for exotic floating-point types such as `f80`, we lower the call to a compiler-rt function call instead. I've added a behavior test specifically targeting this use case which now passes on `aarch64-macos`. Additionally, this commit makes it possible to successfully build stage3 on `aarch64-macos`. We can print the compiler's help message, however, building with it needs a little bit more love still.
2022-05-04LLVM: fix C ABI for windowsAndrew Kelley
* sret logic needed a check for hasRuntimeBits() * lower f128 on windows targets with the "sse" class rather than "memory". For reference, clang emits a compile error when __float128 is used with the MSVC ABI, saying that this type is not supported. The docs for the x64 calling convention have both of these sentences: - "Any argument that doesn't fit in 8 bytes, or isn't 1, 2, 4, or 8 bytes, must be passed by reference." - "All floating point operations are done using the 16 XMM registers." * For i128, however, it is clear that the Windows calling convention wants such an object to be passed by reference. I fixed the LLVM lowering for function parameters to make this work.
2022-05-04LLVM: fix x86_64 sysv C ABI for extern structs with sub-64 bit integersAndrew Kelley
2022-05-04stage2: implement global assemblyAndrew Kelley
So far it's supported by the LLVM backend only. I recommend for the other backends to wait for the resolution of #10761 before adding support for this feature.
2022-05-04stage2: improve `@sizeOf` and `@alignOf` integersAndrew Kelley
Prior to this commit, the logic for ABI size and ABI alignment for integers was naive and incorrect. This results in wasted hardware as well as undefined behavior in the LLVM backend when we memset an incorrect number of bytes to 0xaa due to disagreeing with LLVM about the ABI size of integers. This commit introduces a "max int align" value which is different per Target. This value is used to derive the ABI size and alignment of all integers. This commit makes an interesting change from stage1, which treats 128-bit integers as 16-bytes aligned for x86_64-linux. stage1 is incorrect. The maximum integer alignment on this system is only 8 bytes. This change breaks the behavior test called "128-bit cmpxchg" because on that target, 128-bit cmpxchg does require a 16-bytes aligned pointer to a 128 bit integer. However, this alignment property does not belong on *all* 128 bit integers - only on the pointer type in the `@cmpxchg` builtin function prototype. The user can then use an alignment override annotation on a 128-bit integer variable or struct field to obtain such a pointer.
2022-05-02LLVM: set module PIC, PIE, and CodeModelAndrew Kelley
Some simple code from stage1 ported over to stage2.
2022-05-02LLVM: insert workaround for aarch64-windows f16 CodeView crashAndrew Kelley
2022-05-02stage2: improve inline asm stage1 compatibilityAndrew Kelley
* outputs can have names and be referenced with template replacements the same as inputs. * fix print_air.zig not decoding correctly. * LLVM backend: use a table for template names for simplicity
2022-04-27add new builtin function `@tan`Andrew Kelley
The reason for having `@tan` is that we already have `@sin` and `@cos` because some targets have machine code instructions for them, but in the case that the implementation needs to go into compiler-rt, sin, cos, and tan all share a common dependency which includes a table of data. To avoid duplicating this table of data, we promote tan to become a builtin alongside sin and cos. ZIR: The tag enum is at capacity so this commit moves `field_call_bind_named` to be `extended`. I measured this as one of the least used tags in the zig codebase. Fix libc math suffix for `f32` being wrong in both stage1 and stage2. stage1: add missing libc prefix for float functions.
2022-04-27stage2: fix recent LLVM backend codeAndrew Kelley
* std.math.snan: fix compilation error. Also make it and nan inline. * LLVM: use a proper enum type for float op instead of enum literal. Also various cleanups. * LLVM: use LLVMBuildVectorSplat for vector splat AIR instruction. - also the bindings had parameter order wrong * LLVM: additionally handle f16 lowering. For now all targets report OK but I think we will need to add some exceptions to this list.
2022-04-27stage2: Manually lower softfloat ops when neededCody Tapscott
Updates stage2 to manually lower softfloat operations for all unary floating point operations and arithmetic. Softfloat support still needs to be added for conversion operators (float<->float and int<->float)
2022-04-21LLVM: C calling convention lowering fixesAndrew Kelley
For parameters and return types of functions with the C calling convention, the LLVM backend now has a special lowering for the function type that makes the function adhere to the C ABI. The AIR instruction lowerings for call, ret, and ret_load are adjusted to bitcast the real type to the ABI type if necessary. More work on this will need to be done, however, this improvement is enough that stage3 now passes all the same behavior tests that stage2 passes - notably, translate-c no longer has a segfault due to C ABI issues with Zig's Clang C API wrapper.
2022-04-20stage2: use indexes for Decl objectsAndrew Kelley
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-17stage2: expose progress bar API to linker backendsAndrew Kelley
This gives us insight as to what is happening when we are waiting for things such as LLVM emit object and LLD linking.
2022-04-15stage2: fix bugs preventing stage2 from building stage3 with LLVMVeikka Tuominen
2022-04-15stage2 llvm: fix optional pointers to zero bit payloadsVeikka Tuominen
2022-04-15stage2 llvm: handle dollar signs in asm templateVeikka Tuominen
2022-04-14stage2: progress towards stage3Andrew Kelley
* The `@bitCast` workaround is removed in favor of `@ptrCast` properly doing element casting for slice element types. This required an enhancement both to stage1 and stage2. * stage1 incorrectly accepts `.{}` instead of `{}`. stage2 code that abused this is fixed. * Make some parameters comptime to support functions in switch expressions (as opposed to making them function pointers). * Avoid relying on local temporaries being mutable. * Workarounds for when stage1 and stage2 disagree on function pointer types. * Workaround recursive formatting bug with a `@panic("TODO")`. * Remove unreachable `else` prongs for some inferred error sets. All in effort towards #89.
2022-04-02stage2 llvm: properly align error union payloadVeikka Tuominen
2022-03-31stage2: handle assembly input namesVeikka Tuominen
2022-03-29stage2: implement `@intToError` with safetyAndrew Kelley
This commit introduces a new AIR instruction `cmp_lt_errors_len`. It's specific to this use case for two reasons: * The total number of errors is not stable during semantic analysis; it can only be reliably checked when flush() is called. So the backend that is lowering the instruction must emit a relocation of some kind and then populate it during flush(). * The fewer AIR instructions in memory, the better for compiler performance, so we squish complex meanings into AIR tags without hesitation. The instruction is implemented only in the LLVM backend so far. It does this by creating a simple function which is gutted and re-populated with each flush(). AstGen now uses ResultLoc.coerced_ty for `@intToError` and Sema does the coercion.