ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
35.23k stars 2.57k forks source link

error: LLVM ERROR: Unsupported expression in static initializer #17856

Open minneelyyyy opened 1 year ago

minneelyyyy commented 1 year ago

Zig Version

0.11.0

Steps to Reproduce and Observed Behavior

the code is at https://github.com/girlbuzz/minos and if you simply build with zig build -Darch=x86 then you should get an error message that looks like this:

zig build-exe kernel.elf Debug x86-freestanding-none: error: LLVM ERROR: Unsupported expression in static initializer: or (i48 shl (i48 zext (i32 ptrtoint (ptr @arch.x86.gdt.gdt to i32) to i48), i48 16), i48 39)

zig build-exe kernel.elf Debug x86-freestanding-none: error: the following command terminated unexpectedly:
/home/minneelyyyy/.local/share/zig-linux-x86_64-0.11.0/zig build-exe -ofmt=elf /home/minneelyyyy/Code/minos/src/main.zig --cache-dir /home/minneelyyyy/Code/minos/zig-cache --global-cache-dir /home/minneelyyyy/.cache/zig --name kernel.elf -mcmodel kernel -target x86-freestanding-none -mcpu pentium4 --script /home/minneelyyyy/Code/minos/src/linker.ld --listen=-
Build Summary: 0/3 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install kernel.elf transitive failure
   └─ zig build-exe kernel.elf Debug x86-freestanding-none failure
error: the following build command failed with exit code 1:
/home/minneelyyyy/Code/minos/zig-cache/o/8ac0241d7c4a9777356f43fc94562246/build /home/minneelyyyy/.local/share/zig-linux-x86_64-0.11.0/zig /home/minneelyyyy/Code/minos /home/minneelyyyy/Code/minos/zig-cache /home/minneelyyyy/.cache/zig -Darch=x86

the related code seems to be this little line of code in src/arch/x86/gdt.zig

const gdtd = GDTDescriptor{
    .size = @sizeOf(@TypeOf(gdt)) - 1,
    .offset = &gdt,
};

when i set .offset to something like @ptrFromInt(0x1000) then it doesn't produce an error.

I was also able to reproduce the issue with just this code

minneelyyyy@DESKTOP-QJI9JDV:~$ zig build-exe b.zig
LLVM Emit Object... LLVM ERROR: Unsupported expression in static initializer: or (i80 shl (i80 zext (i64 ptrtoint (ptr @b.s to i64) to i80), i80 16), i80 3)
Aborted
minneelyyyy@DESKTOP-QJI9JDV:~$ cat b.zig
const std = @import("std");

const StringHolder = packed struct {
        length: u16,
        ptr: [*]const u8
};

const s = [_]u8 {'a', 'b', 'c'};

const sh = StringHolder{
        .length = 3,
        .ptr = &s
};

pub fn main() !void {
        // to ensure that sh is compiled in
        std.log.info("{}\n", .{sh.length});
}

Expected Behavior

Ideally this code would just compile, or if this isn't allowed for some reason, there should be a better error message produced by Zig itself and not an LLVM error.

nektro commented 1 year ago

0.12.0-dev.1390+94cee4fb2

LLVM Emit Object... LLVM ERROR: Unsupported expression in static initializer: or (i48 shl (i48 zext (i32 ptrtoint (ptr @arch.x86.gdt.gdt to i32) to i48), i48 16), i48 39)

Thread 1 "zig" received signal SIGABRT, Aborted.
0x00007fffea4a1adc in __pthread_kill_implementation () from /nix/store/dg8mpqqykmw9c7l0bgzzb5znkymlbfjw-glibc-2.37-8/lib/libc.so.6
(gdb) bt
#0  0x00007fffea4a1adc in __pthread_kill_implementation () from /nix/store/dg8mpqqykmw9c7l0bgzzb5znkymlbfjw-glibc-2.37-8/lib/libc.so.6
#1  0x00007fffea452cb6 in raise () from /nix/store/dg8mpqqykmw9c7l0bgzzb5znkymlbfjw-glibc-2.37-8/lib/libc.so.6
#2  0x00007fffea43c8ba in abort () from /nix/store/dg8mpqqykmw9c7l0bgzzb5znkymlbfjw-glibc-2.37-8/lib/libc.so.6
#3  0x00007fffeafca215 in llvm::report_fatal_error (Reason=..., GenCrashDiag=GenCrashDiag@entry=true) at /home/meghan/src/llvm-17/llvm/lib/Support/ErrorHandling.cpp:123
#4  0x00007fffebded011 in llvm::AsmPrinter::lowerConstant (this=0x368e280, CV=0x357c070) at /home/meghan/src/llvm-17/llvm/include/llvm/ADT/Twine.h:233
#5  0x00007fffebdf3df6 in emitGlobalConstantImpl (DL=..., CV=CV@entry=0x357c070, AP=..., BaseCV=0x353a450, BaseCV@entry=0x0, Offset=Offset@entry=0, AliasList=AliasList@entry=0x0)
    at /home/meghan/src/llvm-17/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp:3584
#6  0x00007fffebdf4aa8 in llvm::AsmPrinter::emitGlobalConstant (this=0x368e280, DL=..., CV=0x357c070, AliasList=0x0) at /home/meghan/src/llvm-17/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp:3600
#7  0x00007fffebdf72cd in llvm::AsmPrinter::emitGlobalVariable (this=0x368e280, GV=<optimized out>) at /home/meghan/src/llvm-17/llvm/include/llvm/IR/GlobalValue.h:653
#8  0x00007fffebdf175d in llvm::AsmPrinter::doFinalization (this=0x368e280, M=...) at /home/meghan/src/llvm-17/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp:2165
#9  0x00007fffeb3c8f04 in llvm::FPPassManager::doFinalization (this=0x363f4a0, M=...) at /home/meghan/src/llvm-17/llvm/lib/IR/LegacyPassManager.cpp:1499
#10 0x00007fffeb3d4f56 in (anonymous namespace)::MPPassManager::runOnModule (M=..., this=<optimized out>) at /home/meghan/src/llvm-17/llvm/lib/IR/LegacyPassManager.cpp:1586
#11 llvm::legacy::PassManagerImpl::run (this=0x35cc150, M=...) at /home/meghan/src/llvm-17/llvm/lib/IR/LegacyPassManager.cpp:535
#12 0x0000000002f2e77b in ZigLLVMTargetMachineEmitToFile (targ_machine_ref=<optimized out>, module_ref=0x34fda80, error_message=0x7ffffffeb200, is_debug=<optimized out>, is_small=<optimized out>, time_report=<optimized out>, tsan=false, lto=false, 
    asm_filename=0x0, bin_filename=0x3642f00 "kernel.elf.o", llvm_ir_filename=0x0, bitcode_filename=<optimized out>) at /home/meghan/src/zig/src/zig_llvm.cpp:357
#13 0x000000000113239d in codegen.llvm.Object.flushModule (self=0x34fa3a0, comp=0x34f78b0, prog_node=0x7fffffff1338) at /home/meghan/src/zig/src/codegen/llvm.zig:1291
#14 0x0000000001152a26 in link.Elf.flushModule (self=0x34f9950, comp=0x34f78b0, prog_node=0x7fffffff1338) at /home/meghan/src/zig/src/link/Elf.zig:862
#15 0x0000000001148347 in link.Elf.linkWithLLD (self=0x34f9950, comp=0x34f78b0, prog_node=0x7fffffff1338) at /home/meghan/src/zig/src/link/Elf.zig:1936
#16 0x0000000000e3dc4f in link.Elf.flush (self=0x34f9950, comp=0x34f78b0, prog_node=0x7fffffff1338) at /home/meghan/src/zig/src/link/Elf.zig:848
#17 0x0000000000b1371d in link.File.flush (base=0x34f9b40, comp=0x34f78b0, prog_node=0x7fffffff1338) at link.zig:819
#18 0x0000000000b12f76 in Compilation.flush (comp=0x34f78b0, prog_node=0x7fffffff1338) at Compilation.zig:2393
#19 0x0000000000b56d56 in Compilation.update (comp=0x34f78b0, main_progress_node=0x7fffffff1338) at Compilation.zig:2350
#20 0x0000000000b85753 in main.updateModule (comp=0x34f78b0) at main.zig:4226
#21 0x0000000000ba7bf3 in main.buildOutputType (gpa=..., arena=..., all_args=..., arg_mode=...) at main.zig:3627
#22 0x00000000009b92b0 in main.mainArgs (gpa=..., arena=..., args=...) at main.zig:271
#23 0x00000000009b6596 in main.main () at main.zig:215
tadeokondrak commented 5 months ago

I think this is relevant: https://github.com/llvm/llvm-project/blob/a03d06a736fd8921c8f40637667d6af9c2c76505/llvm/test/CodeGen/X86/ptrtoint-constexpr-invalid.ll#L3-L6