Skip to content

Crash during codegen for target OS PS3 #26020

Description

@e820

Zig Version

0.16.0-dev.1446+947301105

Steps to Reproduce and Observed Behavior

I'd like to preface this with the fact that this is not an issue that can be triggered with the current upstream compiler. I was experimenting with the powerpc64-ps3(-ilp32) target and extended lib/std/Target.zig:cTypeBitSize with the following prong to get rid of the initial panic when using the target:

.ps3 => switch (c_type) {
    .char => return 8,
    .short, .ushort => return 16,
    .int, .uint, .float, .long, .ulong => return 32,
    .longlong, .ulonglong, .double => return 64,
    .longdouble => return 128,
},

Which matches the clang behavior. Running the patched compiler however crashes due to a segfault:

0x00007fffeb3d1765 in llvm::MachineRegisterInfo::addRegOperandToUseList(llvm::MachineOperand*) () from /usr/lib/libLLVM.so.21.1
#0  0x00007fffeb3d1765 in llvm::MachineRegisterInfo::addRegOperandToUseList(llvm::MachineOperand*) () from /usr/lib/libLLVM.so.21.1
#1  0x00007fffeb31d1f0 in llvm::MachineInstr::addOperand(llvm::MachineFunction&, llvm::MachineOperand const&) () from /usr/lib/libLLVM.so.21.1
#2  0x00007fffef1c3c29 in ?? () from /usr/lib/libLLVM.so.21.1
#3  0x00007fffef1cae7f in ?? () from /usr/lib/libLLVM.so.21.1
#4  0x00007fffeb64de68 in llvm::TargetInstrInfo::lowerCopy(llvm::MachineInstr*, llvm::TargetRegisterInfo const*) const () from /usr/lib/libLLVM.so.21.1
#5  0x00007fffeb184650 in ?? () from /usr/lib/libLLVM.so.21.1
#6  0x00007fffeb18472c in ?? () from /usr/lib/libLLVM.so.21.1
#7  0x00007fffeb310b75 in llvm::MachineFunctionPass::runOnFunction(llvm::Function&) () from /usr/lib/libLLVM.so.21.1
#8  0x00007fffeaf050a3 in llvm::FPPassManager::runOnFunction(llvm::Function&) () from /usr/lib/libLLVM.so.21.1
#9  0x00007fffeaf05283 in llvm::FPPassManager::runOnModule(llvm::Module&) () from /usr/lib/libLLVM.so.21.1
#10 0x00007fffeaf045a2 in llvm::legacy::PassManagerImpl::run(llvm::Module&) () from /usr/lib/libLLVM.so.21.1
#11 0x00000000064c0c40 in ZigLLVMTargetMachineEmitToFile (targ_machine_ref=0x7fff85482050, module_ref=0x7fff841c6cc0, error_message=0x7fff8c41a640, options=0x7fff8c41a790)
    at /home/lauree/Developer/zig/src/zig_llvm.cpp:411
#12 0x00000000021fb976 in codegen.llvm.Object.emit (o=0x7fff84002ff8, pt=..., options=...)
#13 0x0000000002033a5c in Compilation.flush (comp=0x7fff84000d20, arena=..., tid=main)
#14 0x0000000001f2788c in Compilation.update (comp=0x7fff84000d20, main_progress_node=...)
#15 0x00000000033aa806 in Compilation.updateSubCompilation (parent_comp=0x6f6b300, sub_comp=0x7fff84000d20, misc_task=compiler_rt, prog_node=...)
#16 0x0000000002d162b4 in Compilation.buildOutputFromZig (comp=0x6f6b300, src_basename=..., root_name=..., output_mode=Lib, link_mode=static, misc_task_tag=compiler_rt, prog_node=..., options=...,
    out=0x6f6b950)
#17 0x0000000002883344 in Compilation.buildRt (comp=0x6f6b300, root_source_name=..., root_name=..., output_mode=Lib, link_mode=static, misc_task=compiler_rt, prog_node=..., options=..., out=0x6f6b950)
#18 0x0000000002364211 in Thread.WaitGroup.spawnManager__anon_207123.Manager.run (wg_inner=0x6f6b6c0, args_inner=...)
#19 0x0000000002d1494b in Thread.callFn__anon_326024 (f=<optimized out>, args=...)
#20 0x0000000002882d22 in Thread.PosixThreadImpl.spawn__anon_254921.Instance.entryFn (raw_arg=...)
#21 0x00007ffff7c9698b in start_thread (arg=<optimized out>) at pthread_create.c:448
#22 0x00007ffff7d1a9cc in __GI___clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78

I realize this is probably not a priority issue, given that the target isn't even strictly supported in the compiler, but I thought I'd put it out there and then see if I can get to the bottom of it when I get the time.

Expected Behavior

No segfault.

Activity

  1. added
    bugObserved behavior contradicts documented or intended behavior
    on Nov 23, 2025
  2. alexrp commented on Nov 23, 2025

    @alexrp
    Member

    Have you built LLVM with either -DCMAKE_BUILD_TYPE=Debug or -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLLVM_ENABLE_ASSERTIONS=ON? (Recommend the latter as Debug LLVM is very slow.) If not, I strongly suggest doing that before even trying to investigate this.

  3. e820 commented on Nov 23, 2025

    @e820
    Author

    Yeah, trying to compile the LLVM IR emitted by Zig using a debug build of llc aborts with the following assertion failure:

    llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp:1354: void {anonymous}::SelectionDAGLegalize::LegalizeOp(llvm::SDNode*): Assertion `(Res.getValueType() == Node->getValueType(0) || Node->getValueType(0) == MVT::Glue) && "Type mismatch for custom legalized operation"' failed.
    

    Considering ISEL is very much outside of my area of expertise I doubt I'll be able to make much sense of this, but I'll see what I can do.

  4. alexrp commented on Nov 23, 2025

    @alexrp
    Member

    I suggest grabbing the LLVM IR that's causing that assertion to get hit and running it through llvm-reduce. By the end of that, it should be much easier to determine what the root cause is.

  5. e820 commented on Nov 23, 2025

    @e820
    Author

    llvm-reduce leaves me with the following:

    target datalayout = "E-m:e-p:32:32-Fi64-i64:64-i128:128-n32:64"
    target triple = "ppc64-unknown-lv2-unknown"
    
    @__anon_1820 = external constant [7 x i8]
    
    define { ptr, i64 } @__ubsan_handle_add_overflow() {
    Entry:
      ret { ptr, i64 } { ptr @__anon_1820, i64 6 }
    }

    Fwiw, running release llc on the reduced IR crashes with a different error from the one initially observed, but maybe they're related:

    LLVM ERROR: Cannot select: 0x55a235378dd0: i64 = any_extend 0x55a2353790e0
      0x55a2353790e0: i64,ch = PPCISD::TOC_ENTRY<(load (s64) from got)> TargetGlobalAddress:i32<ptr @__anon_1820> 0, Register:i64 $x2
    In function: __ubsan_handle_add_overflow
    
  6. alexrp commented on Nov 23, 2025

    @alexrp
    Member

    Well, I think you're out of luck here; it doesn't look like this target is well-tested in LLVM at all.

    ❯ bat test.c
    int x;
    int main() { return x; }
    ❯ clang --target=powerpc64-lv2 test.c -c
    clang: /home/alexrp/Source/llvm-project/llvm/lib/Target/PowerPC/PPCFastISel.cpp:2047: llvm::Register {anonymous}::PPCFastISel::PPCMaterializeGV(const llvm::GlobalValue*, llvm::MVT): Assertion `VT == MVT::i64 && "Non-address!"' failed.
    
  7. e820 commented on Nov 24, 2025

    @e820
    Author

    Interestingly your example doesn't reproduce for me, tried it with a debug clang 21 as well as on compiler explorer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions