717 Commits

Author SHA1 Message Date
Andrew Kelley
070e3ea37d LLVM: insert debug logging when LLVM ABI size is wrong 2022-07-14 22:26:11 -07:00
Andrew Kelley
35e7011124 LLVM: implement signext/zeroext attributes
For calling convention ABI purposes, integer attributes and return
values need to have an LLVM attribute signext or zeroext added
sometimes. This commit implements that logic.

It also implements a proof-of-concept of moving the F16T type from
being a compiler_rt hack to being how the compiler lowers f16 in
functions that need to match certain calling conventions.

Closes #12054
2022-07-13 11:14:46 -07:00
Andrew Kelley
8324a93f2e LLVM: always add some clobbers for some architectures
For some targets, Clang unconditionally adds some clobbers to all inline assembly.
While this is probably not strictly necessary, if we don't follow Clang's lead
here then we may risk tripping LLVM bugs since anything not used by Clang tends
to be buggy and regress often.
2022-07-12 18:38:11 -07:00
Andrew Kelley
0d164f9a25 LLVM: broaden aarch64-windows f16 debug variable workaround
LLVM does not properly handle debug info for f16 on the aarch64-windows
target, causing "fatal error: unknown codeview register H1". The
previous workaround checked only for f16 but was still vulnerable if a
type was a byval struct or tuple which had an f16 field in it.

Now I have filed an upstream issue (see
https://github.com/llvm/llvm-project/issues/56484) and broadened the
workaround to always skip debug values for this target.
2022-07-12 18:38:11 -07:00
Cody Tapscott
2eaef84ebe stage2 llvm: Use unpacked struct for unions and arrays
Our lowerings for various LLVM types assume that we can anticipate the
alignment/layout that LLVM will generate. Among other things, this
requires that we keep the alignment of our lowered LLVM types
synchronized with their expected alignment in Zig.

 - Arrays were using packed struct types, which is seems to be
   incorrect since array elements are supposed to be self-aligned.

 - Unions were using packed struct types for their payload, which causes
   layout divergence between what stage2 expects and what LLVM generates

Consider this lowered union type:
```llvm
%Value = type { <{ i64, [8 x i8] }>, i1, [7 x i8] } ; 24 bytes, align(1)
%ErrorUnion = type { %Value, i16 } ; 26 bytes, align(2)
```

Zig expects Value to be align(8) and, by extension, for ErrorUnion to be
size 32.
2022-07-12 21:34:20 -04:00
Cody Tapscott
2b5215436c stage2: Lower libcalls on Windows x86-64 correctly
This change is the Zig counterpart to https://reviews.llvm.org/D110413

Same as the prior commit, but for stage2
2022-07-10 20:52:29 -07:00
Andrew Kelley
6a3a0fe7ae Merge remote-tracking branch 'origin/master' into llvm14 2022-07-07 18:25:01 -07:00
Andrew Kelley
3a03872af7 LLVM: more robust implementation of C ABI for multiple_llvm_ints
The previous code here was potentially more optimal for some cases,
however, I never tested the perf, so it might not actually matter. This
code handles more cases. We can go back and re-evaluate that other
implementation if it seems worthwhile in the future.
2022-07-07 18:24:34 -07:00
Andrew Kelley
8d6011361f LLVM: handle byref combined with multiple_llvm_ints 2022-07-07 18:24:34 -07:00
Andrew Kelley
2ee864ca5e CLI: add support for -fno-builtin 2022-07-05 15:21:20 -07:00
Andrew Kelley
fbd6c88321 Merge remote-tracking branch 'origin/master' into llvm14 2022-07-04 17:22:36 -07:00
Andrew Kelley
ecb4293afe stage2: disable unit tests when building with stage1
They trip LLVM assertions and spirv is not needed to bootstrap. Not
important for these tests to pass with stage1.
2022-07-04 16:20:33 -07:00
Andrew Kelley
15bdba410a update build scripts to LLD and LLVM 14 libraries 2022-07-03 18:41:43 -07:00
Andrew Kelley
15f111a085 LLVM: update lowering of saturating shift-left
LLVM 14 makes it so that a RHS of saturating shift left produces a
poison value if the value is greater than the number of bits of the LHS.
Zig now emits code that will check if this is the case and select a
saturated LHS value in such case, matching Zig semantics.
2022-07-03 13:07:23 -07:00
Andrew Kelley
b1873f2074 LLVM: update inline asm to LLVM14 semantics
This is the equivalent of d19290e603833a197bc8bfc8315561ec77291225
applied to stage2 instead of stage1.
2022-07-03 11:37:31 -07:00
Andrew Kelley
3ab5912eee LLD: the driver functions now return "false" to mean "error" 2022-07-01 22:13:30 -07:00
Andrew Kelley
1fa4a58d2c stage1: update to LLVM 14 API 2022-07-01 19:19:13 -07:00
Andrew Kelley
c89dd15e1b Merge remote-tracking branch 'origin/master' into llvm14 2022-07-01 15:52:54 -07:00
Andrew Kelley
d3542be875 LLVM: be sure to never pass align(0) attribute
This can happen with pointers to zero-bit types.

This commit fixes an LLVM assertion being tripped.
2022-06-30 18:33:02 -07:00
Andrew Kelley
c030ec1884 LLVM: use unnamed struct llvm type for unions when necessary
The constant value lowering for unions was missing a check for whether
the payload was itself an unnamed struct. Lowerings of other types
already handle this case.

closes #11971
2022-06-30 18:33:02 -07:00
Andrew Kelley
1951051e3d
Merge pull request #11942 from Vexu/stage2-compile-errors
Move passing stage1 compile error tests to stage2
2022-06-30 18:39:46 -04:00
Andrew Kelley
77a334451f
Merge pull request #11967 from ziglang/runtime-float-negation
stage2: lower float negation explicitly + modify hash/eql logic for floats
2022-06-30 17:34:05 -04:00
Andrew Kelley
b3b96b5e28 LLVM: lower float negation with xor a constant
Rather than a compiler-rt call in the case that LLVM does not support
lowering the fneg instruction.
2022-06-30 11:48:37 -07:00
Veikka Tuominen
4f200eda9f stage2 llvm: ensure @tagName functions are unique 2022-06-30 21:47:25 +03:00
Andrew Kelley
6bc6e47b15 stage2: lower float negation explicitly
Rather than lowering float negation as `0.0 - x`.

 * Add AIR instruction for float negation.
 * Add compiler-rt functions for f128, f80 negation

closes #11853
2022-06-30 00:02:00 -07:00
Andrew Kelley
c248af3bdc LLVM: fix lowering of untagged union types
The LLVM backend was calculating the amount of padding solely based
on the payload size. However, in the case where there is no union
tag, this fails to take into account alignment.

Closes #11857
2022-06-30 02:43:05 -04:00
Andrew Kelley
3c1daf951c LLVM: fix invalid IR on @returnAddress of wasm/bpf
see #11946
2022-06-27 17:12:45 -07:00
Andrew Kelley
8d8a5f9733 LLVM: support calls to varargs functions
closes #11944
2022-06-27 16:02:41 -07:00
Veikka Tuominen
38a1222c87 std.crypto: fix invalid pass by value 2022-06-20 15:11:22 +03:00
Andrew Kelley
13f02c30e6 stage2: fix some inline asm incompatibilities with stage1 2022-06-12 14:46:05 -07:00
Veikka Tuominen
0333ff4476 stage2: make error{} the same size as anyerror
Having `error{}` be a zero bit type causes issues when it interracts
with empty inferred error sets which are the same size as `anyerror`.
2022-06-11 23:49:33 +03:00
Veikka Tuominen
0f820d0bdf stage2: improve debugging tools
llvm: dump failed module when -femit-llvm-ir set
print_air:
 * print fully qualified name
 * use Type.fmt and Value.fmtValue, fmtDebug is useless

TypedValue
 * handle anon structs and tuples
 * fix bugs
2022-06-11 11:02:56 +03:00
Andrew Kelley
c1eb6c30e8
Merge pull request #11835 from ziglang/stage2-behavior
stage2: fix handling of aggregates with mixed comptime-only fields
2022-06-11 00:27:41 -04:00
Andrew Kelley
32c90cb553 stage2: fix handling of aggregates with mixed comptime-only fields 2022-06-09 19:23:36 -07: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
Andrew Kelley
3e30ba3f20 stage2: better codegen for byte-aligned packed struct fields
* Sema: handle overaligned packed struct field pointers
 * LLVM: handle byte-aligned packed struct field pointers
2022-06-07 21:05:40 -07:00
Veikka Tuominen
6de9eea7bc stage2 llvm: fix float/int conversion compiler-rt calls 2022-06-07 21:27:06 +03:00
Veikka Tuominen
fbd7e4506f stage2: implement asm with multiple outputs 2022-06-07 21:27:06 +03:00
Andrew Kelley
e9fc58eab7 LLVM: handle extern function name collisions
Zig allows multiple extern functions with the same name, and the
backends have to handle this possibility.

For LLVM, we keep a sparse map of collisions, and then resolve them in
flushModule(). This introduces some technical debt that will have to be
resolved when adding incremental compilation support to the LLVM
backend.
2022-06-07 00:47:10 -04:00
Andrew Kelley
d2f31d315e C backend: implement try instruction 2022-06-05 10:37:08 +02:00
Andrew Kelley
ef885a78d6 stage2: implement the new "try" ZIR/AIR instruction
Implements semantic analysis for the new try/try_inline ZIR
instruction. Adds the new try/try_ptr AIR instructions and implements
them for the LLVM backend.

Fixes not calling rvalue() for tryExpr in AstGen.

This is part of an effort to implement #11772.
2022-06-05 10:37:08 +02:00
Veikka Tuominen
8f45e81c84 stage2: ignore asm inputs named _
This is a hacky solution but the entire asm syntax is supposed to be
reworked anyways.
2022-06-03 15:10:37 +03:00
Andrew Kelley
de14fba247 LLVM: convert two ArrayLists into a MultiArrayList 2022-06-01 11:53:47 -07:00
Andrew Kelley
8c0f4e6f54 LLVM: add target-cpu and target-features fn attributes 2022-05-31 22:13:24 -07:00
Andrew Kelley
2f9533f639 LLVM: pass slices as ptr/len combo
LLVM optimization passes handle this better, and it allows Zig to
specify pointer parameter attributes such as readonly, nonnull, noalias,
and alignment.

closes #561
2022-05-31 18:25:57 -07:00
Andrew Kelley
356a865b87 stage2: introduce support for noalias
Not implemented yet is enhancements to coerceInMemory to account for
noalias parameters.

Related to #11498.
2022-05-31 17:38:42 -07:00
Andrew Kelley
ec919c3c9b LLVM: integrate param attrs with iterateParamTypes
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-31 15:32:15 -07:00
Andrew Kelley
43311e19f4 LLVM: add readonly, nonnull, align attributes to pointer params 2022-05-31 15:16:38 -07:00
Andrew Kelley
d410693dad LLVM: elide some loads when lowering
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-31 14:17:54 -04:00
Andrew Kelley
83beed09e1 LLVM: omit memset of 0xaa bytes in unsafe optimization modes
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-31 00:39:14 -07:00