Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bit casting packed struct gives LLVM Error #991

Closed
Hejsil opened this Issue May 6, 2018 · 4 comments

Comments

Projects
None yet
5 participants
@Hejsil
Copy link
Member

Hejsil commented May 6, 2018

pub const LevelUpMove = packed struct {
    move_id: u9,
    level: u7,
};

test "" {
    var move = LevelUpMove { .move_id = 0, .level = 1 };
    var v = @bitCast(u16, move);
}
Invalid bitcast
  %2 = bitcast %LevelUpMove* %move to i16, !dbg !622
LLVM ERROR: Broken module found, compilation aborted!

@Hejsil Hejsil added the bug label May 6, 2018

@andrewrk andrewrk added this to the 0.3.0 milestone May 7, 2018

@tgschultz

This comment has been minimized.

Copy link
Contributor

tgschultz commented Jun 8, 2018

Ran into this recently, with the @bitCast going in the opposite direction:

@bitcast(PackedStruct, [16]u8);

I got a different error from LLVM and I'm logging it here just in case it helps at all.

LLVM ERROR: Cannot select: 0x1e1174dc2a8: i32 = bitcast FrameIndex:i64<1>
  0x1e117214c08: i64 = FrameIndex<1>
In function: BufferReader_peekReadRaw
@dbandstra

This comment has been minimized.

Copy link
Contributor

dbandstra commented Jun 14, 2018

I tried to bitcast a primitive value (e.g. u8) to a packed struct and got errors as well.

// stripped down example
const Attributes = packed struct {
  contents: u8,
};

test "asdf" {
  const byte: u8 = 0;
  const attributes = @bitCast(Attributes, byte);
}

This gives the compile error:

TODO buf_read_value_bytes struct type

If you change const byte: u8 = 0; to var byte: u8 = 0;, then the compile error becomes:

zig: /home/dbandstra/zig/src/codegen.cpp:1779: LLVMOpaqueValue* gen_assign_raw(CodeGen*, LLVMValueRef, TypeTableEntry*, LLVMValueRef): Assertion `LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMPointerTypeKind' failed.

The same errors happen if the struct is not packed.

@kevinw

This comment has been minimized.

Copy link

kevinw commented Jun 26, 2018

I hit a variant of this with @ptrCast here: https://github.com/kevinw/sokol-zig/tree/bug-llvm-cannot-select

const b_ptr = @ptrCast([*]const c.sg_buffer_desc, &buffer_desc);

The full LLVM traceback is in the README of that repository.

@kevinw

This comment has been minimized.

Copy link

kevinw commented Jun 26, 2018

I ran a zig debug build on the above error and got this assertion:

Assertion failed: (LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMPointerTypeKind), function gen_assign_raw, file /Users/kevin/src/zig/src/codegen.cpp, line 1769.

The backtrace in lldb:

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
  * frame #0: 0x00007fff59533b6e libsystem_kernel.dylib`__pthread_kill + 10
    frame #1: 0x00007fff596fe080 libsystem_pthread.dylib`pthread_kill + 333
    frame #2: 0x00007fff5948f1ae libsystem_c.dylib`abort + 127
    frame #3: 0x00007fff594571ac libsystem_c.dylib`__assert_rtn + 320
    frame #4: 0x00000001000479b2 zig`gen_assign_raw(g=0x000000010600b400, ptr=0x000000010945a3d8, ptr_type=0x000000010945e630, value=0x000000010945e5e0) at codegen.cpp:1769
    frame #5: 0x000000010003e1e0 zig`ir_render_decl_var(g=0x000000010600b400, executable=0x0000000109109f58, decl_var_instruction=0x000000010b3e7550) at codegen.cpp:2834
    frame #6: 0x000000010003d2b7 zig`ir_render_instruction(g=0x000000010600b400, executable=0x0000000109109f58, instruction=0x000000010b3e7550) at codegen.cpp:4767
    frame #7: 0x000000010003b9b2 zig`ir_render(g=0x000000010600b400, fn_entry=0x0000000109109e00) at codegen.cpp:4938
    frame #8: 0x000000010003349b zig`do_code_gen(g=0x000000010600b400) at codegen.cpp:5922
    frame #9: 0x0000000100031741 zig`codegen_build(g=0x000000010600b400) at codegen.cpp:7601
    frame #10: 0x00000001000c3fb4 zig`main(argc=25, argv=0x00007ffeefbff708) at main.cpp:903
    frame #11: 0x00007fff593e3015 libdyld.dylib`start + 1
    frame #12: 0x00007fff593e3015 libdyld.dylib`start + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.