profile
viewpoint

Ask questionsasync: Using switch result in expression leads to invalid llvm

This works:

        // Wait until the first byte is received
        var n = try serial.read(in_buf[0..1]);
        const r = serial.readUntilTimeout(in_buf[1..], 100) catch |err| switch (err) {
            error.Timeout => serial.rx_index,
            else => return err,
        };
        n += r;

but this doesn't

        // Wait until the first byte is received
        var n = try serial.read(in_buf[0..1]);

        // Read the rest (if any)
        n += serial.readUntilTimeout(in_buf[1..], 100) catch |err| switch (err) {
            error.Timeout => serial.rx_index,
            else => return err,
        };


the error is:

Semantic Analysis [174/623] broken LLVM module found: Instruction does not dominate all uses!
  %169 = load i32, i32* %51, align 4, !dbg !4213
  %168 = phi i32 [ %160, %SwitchEnd ], [ %169, %UnwrapErrOk ], !dbg !4213

Signature

pub fn readUntilTimeout(self: *UART, buffer: []u8, timeout: u32) !usize {

It happens even if I use else=> 0

ziglang/zig

Answer questions heidezomp

I've hit upon this error as well, but not involving async, just switching on an error in an expression. Here is my reduction:

const std = @import("std");
const debug = std.debug;
const mem = std.mem;

pub fn main() void {
    fatalError(error.Foo);
}   

fn fatalError(err: anyerror) void {
    debug.warn("{}\n", .{switch (err) {
        error.Foo => mem.span("foo"),
        else => mem.span("bar"),
    }});
}

The crash doesn't happen if I remove the calls to mem.span and just use "foo" and "bar", or if I define err inside fatalError instead of passing it as a parameter.

Compiler output:

heidezomp@falafel% zig build-exe switch-in-expression.zig
Code Generation [424/532] std.fmt.formatType...broken LLVM module found: Instruction does not dominate all uses!
  %3 = getelementptr inbounds %"struct:10:25", %"struct:10:25"* %1, i32 0, i32 0, !dbg !16666
  call fastcc void @std.mem.span.156(%"[]u8"* sret %3, [4 x i8]* @286), !dbg !16667
Instruction does not dominate all uses!
  %3 = getelementptr inbounds %"struct:10:25", %"struct:10:25"* %1, i32 0, i32 0, !dbg !16666
  %6 = bitcast %"[]u8"* %3 to i8*, !dbg !16667
Instruction does not dominate all uses!
  %3 = getelementptr inbounds %"struct:10:25", %"struct:10:25"* %1, i32 0, i32 0, !dbg !16666
  %7 = bitcast %"[]u8"* %3 to i8*, !dbg !16667

This is a bug in the Zig compiler.
Unable to dump stack trace: debug info stripped

(Note: I don't know why the debug info is stripped, as I'm compiling Zig with CMAKE_BUILD_TYPE=RelWithDebInfo; that should work, right?)

useful!
source:https://uonfu.com/
Github User Rank List