After analyzing function body, check call instructions and determine
whether it is an async function or not.
LLVM backend: support lowering trivial async functions
* AstGen: suspend blocks are always void
* add new AIR instructions: suspend_begin and suspend_end
* Module: after a function's body is analyzed, conclude that it is not
async if it wasn't proven async during analysis.
* LLVM: implement lowering of suspend_begin and suspend_end.
There is a lot to do in this branch. I started a branch-local TODO list
in the BRANCH_TODO file. All of these tasks should be finished before
merging into master.
It's based on a runtime alloca based on the frame size. This allows us
to lower async function calls before lowering the callee, and allows
updating callees without updating the callers.
Sema: add `std.debug.Trace` integration for `unneeded`. This helps us
debug when `LazySrcLoc.unneeded` was incorrectly used.
This is for async calls that also act as an alloca. This helps avoid
unnecessarily complicated machinery for the simple case of
`var a = async b();`.
The `call_async` instruction has a frame pointer and returns void
always, which will be used for the other form: `a = async b();`.
This is to be used when the source code looks like this:
var a = async b();
The instruction acts both as an alloc as well as a function call using
that alloc as the result location. This avoids multiple ZIR instructions
as well as complicated "inferred pointer" semantic analysis for a common
case.
AstGen is not yet updated to emit this new instruction.
This implements the semantics as discussed in today's compiler meeting,
where the alignment of pointers to fields of default-layout unions
cannot exceed the field's alignment.
Resolves: #15878
emmalloc.c does a fair amount of type punning in order to access
the size of memory regions and traverse them.
Unfortunately, that can lead to unwanted optimizations.
This simple test case currently triggers a memory fault:
int main(void) {
char * volatile p = malloc(1);
p = realloc(p, 12);
p = malloc(1);
printf("%p\n", p);
}
Work around this by adding "-fno-strict-aliasing" when compiling
that file.
.res files are compiled Windows resource files that get linked into executables/libraries. The linker knows what to do with them, but previously you had to trick Zig into thinking it was an object file (by renaming it to have the .obj extension, for example).
After this commit, the following works:
zig build-exe main.zig resource.res
or, in build.zig:
exe.addObjectFile("resource.res");
Closes#6488
This actually used to be how it worked in stage1, and there was this
issue to change it: #2649
So this commit is a reversal to that idea. One motivation for that issue
was avoiding emitting the panic handler in compilations that do not have
any calls to panic. This commit only resolves the panic handler in the
event of a safety check function being emitted, so it does not have that
flaw.
The other reason given in that issue was for optimizations that elide
safety checks. It's yet to be determined whether that was a good idea or
not; this can get re-explored when we start adding optimization passes
to AIR.
This commit adds these AIR instructions, which are only emitted if
`backendSupportsFeature(.safety_checked_arithmetic)` is true:
* add_safe
* sub_safe
* mul_safe
It removes these nonsensical AIR instructions:
* addwrap_optimized
* subwrap_optimized
* mulwrap_optimized
The safety-checked arithmetic functions push the burden of invoking the
panic handler into the backend. This makes for a messier compiler
implementation, but it reduces the amount of AIR instructions emitted by
Sema, which reduces time spent in the secondary bottleneck of the
compiler. It also generates more compact LLVM IR, reducing time spent in
the primary bottleneck of the compiler.
Finally, it eliminates 1 stack allocation per safety-check which was
being used to store the resulting tuple. These allocations were going to
be annoying when combined with suspension points.
Needed due to the breaking changes to casting builtins, which are used
by the compiler when building itself.
Note from Andrew: I re-ran update-zig1 on my PC and replaced this
commit.
Signed-off-by: Andrew Kelley <andrew@ziglang.org>