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

builtin.Target.x86.cpu._i386 compiler crash #4371

Closed
jzck opened this issue Feb 2, 2020 · 2 comments
Closed

builtin.Target.x86.cpu._i386 compiler crash #4371

jzck opened this issue Feb 2, 2020 · 2 comments

Comments

@jzck
Copy link
Sponsor

jzck commented Feb 2, 2020

Hello, I'm migrating my project to master to prepare for 0.6.0.

I'd like to build my project with the least possible cpu features enabled, the default pentium4 has sse2 which my project doesn't support right now.

from lib/std/target.zig line 478

.i386 => &x86.cpu.pentium4

So with the following change to build.zig:

kernel.setTheTarget(builtin.Target{
        .Cross = std.Target.Cross{
            .arch = builtin.Arch.i386,
            .os = builtin.Os.freestanding,
            .abi = builtin.Abi.none,
            .cpu_features = builtin.CpuFeatures.initFromCpu(
                builtin.Arch.i386,
                &builtin.Target.x86.cpu._i386,
                // &builtin.Target.x86.cpu.pentium4,
            ),
        },
    });

The compiler crashes:

Build Dependencies...c [2] Semantic Analysis [147/227] LLVM ERROR: Cannot select: 0x56550b94f848: i32 = X86ISD::SAHF 0x56550b854568, isnan.zig:8:14 @[ c.zig:527:30 @[ c.zig:399:24 ] ]
  0x56550b854568: i8 = truncate 0x56550b8552d0, isnan.zig:8:14 @[ c.zig:527:30 @[ c.zig:399:24 ] ]
    0x56550b8552d0: i32 = srl 0x56550b855198, Constant:i8<8>, isnan.zig:8:14 @[ c.zig:527:30 @[ c.zig:399:24 ] ]
      0x56550b855198: i32 = any_extend 0x56550b854d20, isnan.zig:8:14 @[ c.zig:527:30 @[ c.zig:399:24 ] ]
        0x56550b854d20: i16 = X86ISD::FNSTSW16r 0x56550b94f8b0, isnan.zig:8:14 @[ c.zig:527:30 @[ c.zig:399:24 ] ]
          0x56550b94f8b0: i16 = truncate 0x56550b854b80, isnan.zig:8:14 @[ c.zig:527:30 @[ c.zig:399:24 ] ]
            0x56550b854b80: i32 = X86ISD::CMP 0x56550b8549e0, 0x56550b8549e0, isnan.zig:8:14 @[ c.zig:527:30 @[ c.zig:399:24 ] ]
              0x56550b8549e0: f32,ch = CopyFromReg 0x5655049d07e8, Register:f32 %58, isnan.zig:8:14 @[ c.zig:527:30 @[
c.zig:399:24 ] ]
                0x56550b854500: f32 = Register %58
              0x56550b8549e0: f32,ch = CopyFromReg 0x5655049d07e8, Register:f32 %58, isnan.zig:8:14 @[ c.zig:527:30 @[
c.zig:399:24 ] ]
                0x56550b854500: f32 = Register %58
      0x56550b94f918: i8 = Constant<8>
In function: fmodf
kernel...The following command exited with error code 1:
/home/jack/documents/zig/build/zig build-exe /home/jack/documents/kernel-zig/src/main.zig --c-source /home/jack/documents/kernel-zig/src/arch/x86/start.s --c-source /home/jack/documents/kernel-zig/src/arch/x86/gdt.s --c-source /home/jack/documents/kernel-zig/src/arch/x86/isr.s --c-source /home/jack/documents/kernel-zig/src/arch/x86/paging.s --c-source /home/jack/documents/kernel-zig/src/arch/x86/switch_tasks.s --cache-dir /home/jack/documents/kernel-zig/zig-cache --name kernel -target i386-freestanding-none -target-cpu _i386 --linker-script /home/jack/documents/kernel-zig/src/arch/x86/linker.ld --pkg-begin kernel /home/jack/documents/kernel-zig/src/index.zig --pkg-end --pkg-begin x86 /home/jack/documents/kernel-zig/src/arch/x86/index.zig --pkg-end --cache on

I've pushed that code to a separate branch here: https://github.com/jzck/kernel-zig/tree/0.6.0 if you want to reproduce.

Am I using the new CpuFeature interface wrong or is this a bug?

@LemonBoy
Copy link
Contributor

LemonBoy commented Feb 2, 2020

Am I using the new CpuFeature interface wrong or is this a bug?

Neither of them! Running the code trough a LLVM build with the assertions enabled gives some more hints:

llvm::SDValue llvm::X86TargetLowering::ConvertCmpIfNecessary(llvm::SDValue, llvm::SelectionDAG&) const: Assertion `Subtarget.hasLAHFSAHF() && "Target doesn't support SAHF or FCOMI?"' failed.

You've chosen a target so feature-poor that this particular combination of instructions cannot be lowered (or nobody was bothered enough to implement it).
You can easily solve the problem by picking another base such as i686 (it's just i386 plus cx8 and cmov, the latter helps to sidestep this problem (check out LLVM's ConvertCmpIfNecessary for more info)).

@jzck
Copy link
Sponsor Author

jzck commented Feb 3, 2020

Thanks! i686 works great. Maybe that Cpu should be tagged to produce a friendly warning that LLVM doesn't support it?

Closing now that my problem is solved.
Kepp up the good work :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants