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

Asm block causes compiler crash #2320

Open
tyler569 opened this issue Apr 21, 2019 · 2 comments
Open

Asm block causes compiler crash #2320

tyler569 opened this issue Apr 21, 2019 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior upstream An issue with a third party project that Zig uses.
Milestone

Comments

@tyler569
Copy link
Contributor

Compiling the following source file:

const std = @import("std");

pub fn main() void {
    const foo: i64 = undefined;

    asm volatile (
        \\ mov $12, %[foo]
        :
        : [foo] "=m" (foo)
    );

    std.debug.warn("{}\n", foo);
}

with zig build-exe asm.zig causes the compiler to segfault.

This seems very similar to #1689, I get a similar crash deep in the LLVM weeds:

(gdb) bt
#0  0x00007ffff54c342c in llvm::SelectionDAGBuilder::visitInlineAsm(llvm::ImmutableCallSite) ()                                                                                      
#1  0x00007ffff54d4b51 in llvm::SelectionDAGBuilder::visitCall(llvm::CallInst const&) ()
#2  0x00007ffff54dc1f9 in llvm::SelectionDAGBuilder::visit(llvm::Instruction const&) ()
#3  0x00007ffff551ede2 in llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, false, false, void>, false, true>, llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, false, false, void>, false, true>, bool&) ()                                                                        
#4  0x00007ffff5525703 in llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) ()                                                                                      
#5  0x00007ffff5526a0e in llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) ()                                                                                    
#6  0x00007ffff45cf3c0 in (anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) ()                                                                    
#7  0x00007ffff5822ee5 in llvm::MachineFunctionPass::runOnFunction(llvm::Function&) [clone .part.43] ()                                                                              
#8  0x00007ffff6411029 in llvm::FPPassManager::runOnFunction(llvm::Function&) ()
#9  0x00007ffff6411081 in llvm::FPPassManager::runOnModule(llvm::Module&) ()
#10 0x00007ffff64103a2 in llvm::legacy::PassManagerImpl::run(llvm::Module&) ()
#11 0x00007ffff26cd787 in ZigLLVMTargetMachineEmitToFile ()
#12 0x00007ffff25ff98c in codegen_build_and_link(CodeGen*) ()
#13 0x00007ffff251c415 in main ()

The zig compiler survives if you emit LLVM, and lli doesn't like that asm block when it tries to run it:

$ lli asm.ll                                                                                      
lli: asm.ll:30372:13: error: invalid type for inline asm constraint string                                                                                                            
  call void asm sideeffect " mov $$12, $0", "=m"(i64 undef), !dbg !13268                                                                                                              
            ^

I don't currently have the ability to run with a LLVM build with asserts enabled, but I can try that later tonight if that will help.

@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label Apr 21, 2019
@andrewrk andrewrk added this to the 0.5.0 milestone Apr 21, 2019
@Sahnvour
Copy link
Contributor

I am not sure what you are trying to do, shouldn't your code be:

const std = @import("std");

pub fn main() void {
    var foo: i64 = undefined;

    asm volatile (
        \\ movq $12, %[foo]
        : [foo] "=m" (foo)
    );

    std.debug.warn("{}\n", foo);
}

?

Changes:

  • foo is const in your example
  • =m is an output but you listed it in the inputs
  • with the above change, llvm suggested to use movq instead

Granted, a lot of these could be caught by Zig.

@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Sep 27, 2019
@andrewrk andrewrk added stage1 The process of building from source via WebAssembly and the C backend. upstream An issue with a third party project that Zig uses. labels Feb 10, 2020
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Feb 10, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@nektro
Copy link
Contributor

nektro commented Oct 16, 2022

@Sahnvour's fixed version prints 12

@tyler569's original code after fixing the last line to std.debug.print("{}\n", .{foo}); still hits an llvm assertion

Failure value returned from cantFail wrapped call
number of input constraints does not match number of parameters
UNREACHABLE executed at /llvm/include/llvm/Support/Error.h:755!

Thread 1 "zig2" received signal SIGABRT, Aborted.
0x00007fffeb37cc1f in __pthread_kill_implementation () from /nix/store/scd5n7xsn0hh0lvhhnycr9gx0h8xfzsl-glibc-2.34-210/lib/libc.so.6
(gdb) bt
#0  0x00007fffeb37cc1f in __pthread_kill_implementation () from /nix/store/scd5n7xsn0hh0lvhhnycr9gx0h8xfzsl-glibc-2.34-210/lib/libc.so.6
#1  0x00007fffeb332042 in raise () from /nix/store/scd5n7xsn0hh0lvhhnycr9gx0h8xfzsl-glibc-2.34-210/lib/libc.so.6
#2  0x00007fffeb31d49c in abort () from /nix/store/scd5n7xsn0hh0lvhhnycr9gx0h8xfzsl-glibc-2.34-210/lib/libc.so.6
#3  0x00007fffec58e83a in llvm::llvm_unreachable_internal (msg=<optimized out>, file=file@entry=0x7ffff02a81a8 "/llvm/include/llvm/Support/Error.h", line=line@entry=755) at /llvm/lib/Support/ErrorHandling.cpp:212
#4  0x00007fffec7bb52c in llvm::cantFail (Msg=<optimized out>, Err=...) at /llvm/include/llvm/Support/Error.h:755
#5  llvm::InlineAsm::InlineAsm (this=0x2573df0, FTy=0x248e6a0, asmString=" mov $$12, $0", constraints=..., hasSideEffects=<optimized out>, isAlignStack=<optimized out>, asmDialect=llvm::InlineAsm::AD_ATT, canThrow=false) at /llvm/lib/IR/InlineAsm.cpp:39
#6  0x00007fffec7bced4 in llvm::InlineAsmKeyType::create (Ty=<optimized out>, this=0x7ffffffeebb0) at /llvm/lib/IR/ConstantsContext.h:429
#7  llvm::ConstantUniqueMap<llvm::InlineAsm>::create (this=0x23d2620, Ty=0x248d780, V=..., HashKey={...}) at /llvm/lib/IR/ConstantsContext.h:623
#8  0x00007fffec7bd21b in llvm::ConstantUniqueMap<llvm::InlineAsm>::getOrCreate (this=0x23d2620, Ty=0x248d780, V=...) at /llvm/lib/IR/ConstantsContext.h:642
#9  0x00007fffec7bb619 in llvm::InlineAsm::get (FTy=0x248e6a0, AsmString=..., Constraints=..., hasSideEffects=true, isAlignStack=false, asmDialect=llvm::InlineAsm::AD_ATT, canThrow=false) at /llvm/lib/IR/InlineAsm.cpp:50
#10 0x0000000000e2dbb6 in codegen.llvm.FuncGen.airAssembly (self=0x7fffffff1218, inst=5) at /zig/src/codegen/llvm.zig:6211
#11 0x0000000000e0a141 in codegen.llvm.FuncGen.genBody (self=0x7fffffff1218, body=...) at /zig/src/codegen/llvm.zig:4512
#12 0x0000000000e046cf in codegen.llvm.Object.updateFunc (o=0x23d1d40, module=0x23d0970, func=0x24700e0, air=..., liveness=...) at /zig/src/codegen/llvm.zig:1207
#13 0x0000000000b8f338 in link.Elf.updateFunc (self=0x23d1610, module=0x23d0970, func=0x24700e0, air=..., liveness=...) at /zig/src/link/Elf.zig:2411
#14 0x0000000000990d0a in link.File.updateFunc (base=0x23d1610, module=0x23d0970, func=0x24700e0, air=..., liveness=...) at /zig/src/link.zig:524
#15 0x000000000096ea41 in Module.ensureFuncBodyAnalyzed (mod=0x23d0970, func=0x24700e0) at /zig/src/Module.zig:4335
#16 0x000000000065a64d in Compilation.processOneJob (comp=0x23cf8c8, job=...) at /zig/src/Compilation.zig:3047
#17 0x00000000006483a0 in Compilation.performAllTheWork (comp=0x23cf8c8, main_progress_node=0x7fffffff31a8) at /zig/src/Compilation.zig:2985
#18 0x0000000000640a4e in Compilation.update (comp=0x23cf8c8) at /zig/src/Compilation.zig:2325
#19 0x00000000005cbfd0 in main.updateModule (gpa=..., comp=0x23cf8c8, hook=...) at /zig/src/main.zig:3318
#20 0x0000000000515d86 in main.buildOutputType (gpa=..., arena=..., all_args=..., arg_mode=...) at /zig/src/main.zig:3003
#21 0x00000000004aed60 in main.mainArgs (gpa=..., arena=..., args=...) at /zig/src/main.zig:238
#22 0x00000000004ae3d6 in main (argc=3, argv=0x7fffffffb788) at /zig/src/stage1.zig:56

@Vexu Vexu removed the stage1 The process of building from source via WebAssembly and the C backend. label Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior upstream An issue with a third party project that Zig uses.
Projects
None yet
Development

No branches or pull requests

5 participants