14197 Commits

Author SHA1 Message Date
antlilja
ec5a433fdf Fix FastMath packed struct
* Added missing legacy field (unused_algebra)
* Made struct correct size (u32 -> u8)
2024-02-21 16:24:59 +01:00
antlilja
7cb8813743 Added value_indices and valueIndex to Function
value_indices keeps track of the value index of
each instruction in the function (i.e skips
instruction which do not have a result)
2024-02-21 16:24:59 +01:00
antlilja
423c2c3a27 Made .block = false in WipFunction.hasResultWip 2024-02-21 16:24:59 +01:00
antlilja
c57b4e70b0 Builder: Add function_attributes_set 2024-02-21 16:24:59 +01:00
antlilja
52e8434022 Added opcode functions to Instruction/Constant.Tag 2024-02-21 16:24:59 +01:00
antlilja
ff76ba64d6 Added values to AtomicRmw.Operation enum fields 2024-02-21 16:24:59 +01:00
antlilja
6df8302be6 Fixed values in AtomicOrdering enum 2024-02-21 16:24:59 +01:00
antlilja
fd3b81f93b LLVM Builder: Add toLlvm helper to Alignment 2024-02-21 16:24:59 +01:00
antlilja
2801bf6400 LLVM Builder: Add strtab helper to String 2024-02-21 16:24:59 +01:00
antlilja
9ccd7158b9 LLVM Builder: Make Type.Simple reflect LLVM codes 2024-02-21 16:24:59 +01:00
antlilja
1d94e9ef83 LLVM: Make sure child types get added first
The LLVM bitcode requires all type references in
structs to be to earlier defined types.

We make sure types are ordered in the builder
itself in order to avoid having to iterate the
types multiple times and changing the values
of type indicies.
2024-02-21 16:24:59 +01:00
antlilja
8feae5d2d5 LLVM: Assign correct values to enum/union tags 2024-02-21 16:24:59 +01:00
garrison hinson-hasty
955fd65cb1
Sema: fix peer type resolution for arrays of coercible elements 2024-02-21 00:55:29 +00:00
Jacob Young
ec7293422d llvm: fix c abi for structs not passed in registers
Closes #18916
2024-02-20 23:36:47 +01:00
mlugg
65a87ff299 Liveness: do not elide safety-checked instructions
Resolves: #19012
2024-02-20 12:10:29 +00:00
Jacob Young
97290e0bfc cbe: make more use of InternPool.Index.Slice
Attempts to fix the current CI failures introduced by #18983.
2024-02-19 19:32:06 -08:00
mlugg
7461309b73 Sema: validate that runtime-known inferred alloc does not have comptime-only type
Resolves: #18997
2024-02-19 21:48:50 +00:00
Ian Johnson
80f3ef6e14 Package.Fetch: fix Git package fetching
This commit works around #18967 by adding an `AccumulatingReader`, which
accumulates data read from the underlying packfile, and by keeping track
of the position in the packfile and hash/checksum information separately
rather than using reader composition. That is, the packfile position and
hashes/checksums are updated with the accumulated read history data only
after we can determine what data has actually been used by the
decompressor rather than merely being buffered.

The only addition to the standard library APIs to support this change is
the `unreadBytes` function in `std.compress.flate.Inflate`, which allows
the user to determine how many bytes have been read only for buffering
and not used as part of compressed data.

These changes can be reverted if #18967 is resolved with a decompressor
that reads precisely only the number of bytes needed for decompression.
2024-02-19 13:43:32 -08:00
Jacob Young
57b2b3df52 Dwarf: use a user tag for padding 2024-02-18 14:11:03 +01:00
Jakub Konka
99584906bb elf: fix typo in resolving @"32" and @"32S" relocs on x86_64 2024-02-17 19:39:11 +01:00
Jakub Konka
57e71ce189 elf: @"64" is equivalent to GLOB_DAT on riscv 2024-02-17 19:39:11 +01:00
Jakub Konka
79e99c401c
Merge pull request #18973 from ziglang/elf-riscv
lib/std/elf: refactor relocation types + add RISCV relocs
2024-02-17 19:35:09 +01:00
Robin Voetter
dd4d320eb9
Merge pull request #18948 from alichraghi/vector
spirv: use extended instructions whenever possible
2024-02-17 14:01:07 +01:00
Jakub Konka
d1429a8fa9 lib/std/elf: refactor reloc enum values 2024-02-17 13:13:03 +01:00
Jakub Konka
509c7149b9 elf: fix formatting of relocs when reloc can be Zig specific 2024-02-17 12:35:16 +01:00
Jakub Konka
9542ee9bf8 elf: create Zig specific reloc type shared across ISAs 2024-02-17 11:46:01 +01:00
Jakub Konka
ace1a69a55 elf: add new R_RISCV_TLSDESC reloc type 2024-02-17 11:41:18 +01:00
Jakub Konka
975862aca9 elf: add riscv dynamic relocs 2024-02-17 11:29:06 +01:00
Michael Dusan
50330ec22a cbe: do not set execute bits on emitted file 2024-02-17 10:50:46 +01:00
Jakub Konka
601aa10b82 elf: add riscv64 dynamic relocs mapping 2024-02-17 09:04:06 +01:00
Jakub Konka
fc7dd3e285 elf: enable adding support for additional cpu archs 2024-02-16 21:54:43 +01:00
mlugg
5a957a3281
Sema: improved source location for @panic operand coercion error
Similar to the previous commit, errors coercing the panic message to
`[]const u8` now point at the operand to `@panic` rather than the actual
builtin call.
2024-02-16 12:04:00 +00:00
mlugg
e6cf3ce24c
Sema: correct source location for return value coercion errors
When coercing the operand of a `ret_node` etc instruction, the source
location for errors used to point to the entire `return` statement.
Instead, we now point to the operand, as would be expected if there was
an explicit `as_node` instruction (like there used to be).
2024-02-16 11:26:35 +00:00
mlugg
260c845355
Zir: make src_node of type declarations non-optional
Previously, the `src_node` field of `struct_decl`, `union_decl`,
`enum_decl`, and `opaque_decl` was optional, included in trailing data
only if a flag in `Small` was set. However, this was unnecessary logic:
AstGen always provided the source node. We can simplify a few bits of
logic by making this field non-optional, moving it into non-trailing
data.

There was one place where the field was actually omitted before: the
root struct of a file was at source node 0, so the node was
coincidentally elided. Therefore, this commit has a fixed cost of 4
bytes of ZIR per file.
2024-02-16 11:26:34 +00:00
mlugg
10784c7fc8
AstGen: migrate ty result locations to coerced_ty
In most cases where AstGen is coercing to a fixed type (such as `u29`,
`type`, `std.builtin.CallingConvention) we do not necessarily require an
explicit coercion instruction. Instead, Sema knows the type that is
required, and can perform the coercion after the fact. This means we can
use the `coerced_ty` result location kind, saving unnecessary coercion
instructions and therefore ZIR bytes.

This required a few enhancements to Sema to introduce missing coercions.
2024-02-16 11:26:34 +00:00
mlugg
434537213e
Sema: eliminate src field
`sema.src` is a failed experiment. It introduces complexity, and makes
often unwarranted assumptions about the existence of instructions
providing source locations, requiring an unreasonable amount of caution
in AstGen for correctness. Eliminating it simplifies the whole frontend.

This required adding source locations to a few instructions, but the
cost in ZIR bytes should be counteracted by the other work on this
branch.
2024-02-16 11:26:33 +00:00
mlugg
aba29f9789
AstGen: fix elision of unnecessary dbg_stmt instructions
AstGen has logic to elide leading `dbg_stmt` instructions when multiple
are emitted consecutively; however, it only applied in some cases. A
simple reshuffle here makes this logic apply universally, saving some
bytes in ZIR.
2024-02-16 11:26:33 +00:00
mlugg
2e27967a81
AstGen: avoid emitting multiple ret_type instructions
This is a small optimization to generated ZIR. In any function where the
return type is not a trivial Ref, we know it is almost certainly not
`void` (unless the user aliased it or did something else weird to fool
AstGen), and thus the return type is very likely to be required for
return value RLS at some point. Thus, we can just emit one `ret_type` at
the start of the function and use it throughout.

This sees a very small improvement in overall ZIR bytes.
2024-02-16 11:26:32 +00:00
Jacob Young
6f08e17229 InternPool: make more use of NullTerminatedString.Slice
This should avoid the random pointer invalidation crashes.

Closes #18954
2024-02-16 00:27:25 -08:00
Andrew Kelley
0183b44bb1 std.os.windows: add error.UnrecognizedVolume
Thanks to @matklad for finding this additional NTSTATUS possibility when
calling GetFinalPathNameByHandle.
2024-02-16 00:20:57 -08:00
Andrew Kelley
57d6f789de
Merge pull request #18923 from ianic/add_flate
add deflate implemented from first principles
2024-02-15 10:55:40 -08:00
Ali Chraghi
23b7075656 spirv: add capability to compile with x86_64 backend 2024-02-15 17:28:15 +03:30
Ali Chraghi
44c31194e3 spirv: use extended instructions whenever possible 2024-02-15 17:25:44 +03:30
Jacob Young
6fe90a913a llvm: fix lowering of recursive debug info
This change allows recursing over types that are currently being
resolved fully with a second pass of forward resolution.

Closes #16414
2024-02-15 02:46:18 -08:00
Jacob Young
cb3fe277bb x86_64: fix crash loading a packed value from a spilled pointer
Unblocks #18923
2024-02-14 18:28:20 +01:00
Igor Anić
d645114f7e add deflate implemented from first principles
Zig deflate compression/decompression implementation. It supports compression and decompression of gzip, zlib and raw deflate format.

Fixes #18062.

This PR replaces current compress/gzip and compress/zlib packages. Deflate package is renamed to flate. Flate is common name for deflate/inflate where deflate is compression and inflate decompression.

There are breaking change. Methods signatures are changed because of removal of the allocator, and I also unified API for all three namespaces (flate, gzip, zlib).

Currently I put old packages under v1 namespace they are still available as compress/v1/gzip, compress/v1/zlib, compress/v1/deflate. Idea is to give users of the current API little time to postpone analyzing what they had to change. Although that rises question when it is safe to remove that v1 namespace.

Here is current API in the compress package:

```Zig
// deflate
    fn compressor(allocator, writer, options) !Compressor(@TypeOf(writer))
    fn Compressor(comptime WriterType) type

    fn decompressor(allocator, reader, null) !Decompressor(@TypeOf(reader))
    fn Decompressor(comptime ReaderType: type) type

// gzip
    fn compress(allocator, writer, options) !Compress(@TypeOf(writer))
    fn Compress(comptime WriterType: type) type

    fn decompress(allocator, reader) !Decompress(@TypeOf(reader))
    fn Decompress(comptime ReaderType: type) type

// zlib
    fn compressStream(allocator, writer, options) !CompressStream(@TypeOf(writer))
    fn CompressStream(comptime WriterType: type) type

    fn decompressStream(allocator, reader) !DecompressStream(@TypeOf(reader))
    fn DecompressStream(comptime ReaderType: type) type

// xz
   fn decompress(allocator: Allocator, reader: anytype) !Decompress(@TypeOf(reader))
   fn Decompress(comptime ReaderType: type) type

// lzma
    fn decompress(allocator, reader) !Decompress(@TypeOf(reader))
    fn Decompress(comptime ReaderType: type) type

// lzma2
    fn decompress(allocator, reader, writer !void

// zstandard:
    fn DecompressStream(ReaderType, options) type
    fn decompressStream(allocator, reader) DecompressStream(@TypeOf(reader), .{})
    struct decompress
```

The proposed naming convention:
 - Compressor/Decompressor for functions which return type, like Reader/Writer/GeneralPurposeAllocator
 - compressor/compressor for functions which are initializers for that type, like reader/writer/allocator
 - compress/decompress for one shot operations, accepts reader/writer pair, like read/write/alloc

```Zig
/// Compress from reader and write compressed data to the writer.
fn compress(reader: anytype, writer: anytype, options: Options) !void

/// Create Compressor which outputs the writer.
fn compressor(writer: anytype, options: Options) !Compressor(@TypeOf(writer))

/// Compressor type
fn Compressor(comptime WriterType: type) type

/// Decompress from reader and write plain data to the writer.
fn decompress(reader: anytype, writer: anytype) !void

/// Create Decompressor which reads from reader.
fn decompressor(reader: anytype) Decompressor(@TypeOf(reader)

/// Decompressor type
fn Decompressor(comptime ReaderType: type) type

```

Comparing this implementation with the one we currently have in Zig's standard library (std).
Std is roughly 1.2-1.4 times slower in decompression, and 1.1-1.2 times slower in compression. Compressed sizes are pretty much same in both cases.
More resutls in [this](https://github.com/ianic/flate) repo.

This library uses static allocations for all structures, doesn't require allocator. That makes sense especially for deflate where all structures, internal buffers are allocated to the full size. Little less for inflate where we std version uses less memory by not preallocating to theoretical max size array which are usually not fully used.

For deflate this library allocates 395K while std 779K.
For inflate this library allocates 74.5K while std around 36K.

Inflate difference is because we here use 64K history instead of 32K in std.

If merged existing usage of compress gzip/zlib/deflate need some changes. Here is example with necessary changes in comments:

```Zig

const std = @import("std");

// To get this file:
// wget -nc -O war_and_peace.txt https://www.gutenberg.org/ebooks/2600.txt.utf-8
const data = @embedFile("war_and_peace.txt");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer std.debug.assert(gpa.deinit() == .ok);
    const allocator = gpa.allocator();

    try oldDeflate(allocator);
    try new(std.compress.flate, allocator);

    try oldZlib(allocator);
    try new(std.compress.zlib, allocator);

    try oldGzip(allocator);
    try new(std.compress.gzip, allocator);
}

pub fn new(comptime pkg: type, allocator: std.mem.Allocator) !void {
    var buf = std.ArrayList(u8).init(allocator);
    defer buf.deinit();

    // Compressor
    var cmp = try pkg.compressor(buf.writer(), .{});
    _ = try cmp.write(data);
    try cmp.finish();

    var fbs = std.io.fixedBufferStream(buf.items);
    // Decompressor
    var dcp = pkg.decompressor(fbs.reader());

    const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize));
    defer allocator.free(plain);
    try std.testing.expectEqualSlices(u8, data, plain);
}

pub fn oldDeflate(allocator: std.mem.Allocator) !void {
    const deflate = std.compress.v1.deflate;

    // Compressor
    var buf = std.ArrayList(u8).init(allocator);
    defer buf.deinit();
    // Remove allocator
    // Rename deflate -> flate
    var cmp = try deflate.compressor(allocator, buf.writer(), .{});
    _ = try cmp.write(data);
    try cmp.close(); // Rename to finish
    cmp.deinit(); // Remove

    // Decompressor
    var fbs = std.io.fixedBufferStream(buf.items);
    // Remove allocator and last param
    // Rename deflate -> flate
    // Remove try
    var dcp = try deflate.decompressor(allocator, fbs.reader(), null);
    defer dcp.deinit(); // Remove

    const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize));
    defer allocator.free(plain);
    try std.testing.expectEqualSlices(u8, data, plain);
}

pub fn oldZlib(allocator: std.mem.Allocator) !void {
    const zlib = std.compress.v1.zlib;

    var buf = std.ArrayList(u8).init(allocator);
    defer buf.deinit();

    // Compressor
    // Rename compressStream => compressor
    // Remove allocator
    var cmp = try zlib.compressStream(allocator, buf.writer(), .{});
    _ = try cmp.write(data);
    try cmp.finish();
    cmp.deinit(); // Remove

    var fbs = std.io.fixedBufferStream(buf.items);
    // Decompressor
    // decompressStream => decompressor
    // Remove allocator
    // Remove try
    var dcp = try zlib.decompressStream(allocator, fbs.reader());
    defer dcp.deinit(); // Remove

    const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize));
    defer allocator.free(plain);
    try std.testing.expectEqualSlices(u8, data, plain);
}

pub fn oldGzip(allocator: std.mem.Allocator) !void {
    const gzip = std.compress.v1.gzip;

    var buf = std.ArrayList(u8).init(allocator);
    defer buf.deinit();

    // Compressor
    // Rename compress => compressor
    // Remove allocator
    var cmp = try gzip.compress(allocator, buf.writer(), .{});
    _ = try cmp.write(data);
    try cmp.close(); // Rename to finisho
    cmp.deinit(); // Remove

    var fbs = std.io.fixedBufferStream(buf.items);
    // Decompressor
    // Rename decompress => decompressor
    // Remove allocator
    // Remove try
    var dcp = try gzip.decompress(allocator, fbs.reader());
    defer dcp.deinit(); // Remove

    const plain = try dcp.reader().readAllAlloc(allocator, std.math.maxInt(usize));
    defer allocator.free(plain);
    try std.testing.expectEqualSlices(u8, data, plain);
}

```
2024-02-14 18:28:20 +01:00
Loris Cro
a23ab331a2 autodoc: fix analysis of closure_get instructions 2024-02-14 15:35:00 +01:00
Jakub Konka
3f5593205d
Merge pull request #18918 from ziglang/elf-better-addr-mgmt
elf: store relative offsets in atom and symbol
2024-02-14 07:32:38 +01:00
Jakub Konka
9ec0cf23ca
Merge pull request #18905 from ziglang/elf-mem-pressure
elf: reduce memory pressure
2024-02-14 01:08:51 +01:00
Jakub Konka
216a5594f6 elf: use u32 for all section indexes 2024-02-13 20:33:08 +01:00