Skip to content

LDC crashes deep in LLVM #4759

Open
Open
@yanok

Description

@yanok

I see a crash trying to compile this code (already minified by dustmite):

import std;
struct notrace {}
struct string_tracetail {
    string value;
    alias value this;
}
class ReactorFiber{
string_tracetail funcName;
    @notrace setCallback(alias F)() {
        funcName = fullyQualifiedName!F;
    }

}

struct FiberHandle {
}
struct Reactor {
ReactorFiber allocFiber() {
    return new ReactorFiber;
    }

FiberHandle spawnFiber(alias F)() {
        auto fib = allocFiber;
        fib.setCallback!F;
        return FiberHandle();
    }

}


Reactor theReactor;
void closure(void delegate() ) {
}

unittest
{
    closure({
        static f() {}
        theReactor.spawnFiber!f;
    });
}

Compile command: ldc2 -c crash.d --unittest.

Stack trace:

0  libLLVM-17.so.1 0x00007f6ba4711bbf llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 63
1  libLLVM-17.so.1 0x00007f6ba470febb llvm::sys::RunSignalHandlers() + 43
2  libLLVM-17.so.1 0x00007f6ba47122c0
3  libc.so.6       0x00007f6ba34b7320
4  libLLVM-17.so.1 0x00007f6ba4f3a4aa llvm::SelectionDAG::getNode(unsigned int, llvm::SDLoc const&, llvm::EVT, llvm::SDValue, llvm::SDNodeFlags) + 74
5  libLLVM-17.so.1 0x00007f6ba4e5737b
6  libLLVM-17.so.1 0x00007f6ba4e5717b
7  libLLVM-17.so.1 0x00007f6ba4e73c96
8  libLLVM-17.so.1 0x00007f6ba4e7759d llvm::SelectionDAG::LegalizeTypes() + 1309
9  libLLVM-17.so.1 0x00007f6ba4f6f143 llvm::SelectionDAGISel::CodeGenAndEmitDAG() + 323
10 libLLVM-17.so.1 0x00007f6ba4f6e4d3 llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) + 4227
11 libLLVM-17.so.1 0x00007f6ba4f6c846 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) + 1862
12 libLLVM-17.so.1 0x00007f6ba7293016
13 libLLVM-17.so.1 0x00007f6ba4ad83ce llvm::MachineFunctionPass::runOnFunction(llvm::Function&) + 622
14 libLLVM-17.so.1 0x00007f6ba485b4c2 llvm::FPPassManager::runOnFunction(llvm::Function&) + 738
15 libLLVM-17.so.1 0x00007f6ba48613f4 llvm::FPPassManager::runOnModule(llvm::Module&) + 68
16 libLLVM-17.so.1 0x00007f6ba485bbae llvm::legacy::PassManagerImpl::run(llvm::Module&) + 1006
17 ldc2            0x00005647a7392f9b
18 ldc2            0x00005647a7393a88 writeModule(llvm::Module*, char const*) + 2328
19 ldc2            0x00005647a7390025 ldc::CodeGenerator::writeAndFreeLLModule(char const*) + 1253
20 ldc2            0x00005647a7390471 ldc::CodeGenerator::emit(Module*) + 177
21 ldc2            0x00005647a73681c0 codegenModules(Array<Module*>&) + 640
22 ldc2            0x00005647a71265d1 mars_mainBody(Param&, Array<char const*>&, Array<char const*>&) + 6289
23 ldc2            0x00005647a73668da cppmain() + 7082
24 ldc2            0x00005647a74f792d _D2rt6dmain212_d_run_main2UAAamPUQgZiZ6runAllMFZv + 77
25 ldc2            0x00005647a74f7747 _d_run_main2 + 407
26 ldc2            0x00005647a74f759d _d_run_main + 141
27 ldc2            0x00005647a6ff18ac main + 540
28 libc.so.6       0x00007f6ba349c1ca
29 libc.so.6       0x00007f6ba349c28b __libc_start_main + 139
30 ldc2            0x00005647a6ff3e45 _start + 37

The problem goes away if I move static f() {} definition out of the function body.

Checked on Ubuntu with

$ ldc2 --version
LDC - the LLVM D compiler (1.36.0):
  based on DMD v2.106.1 and LLVM 17.0.6
  built with LDC - the LLVM D compiler (1.36.0)
  Default target: x86_64-pc-linux-gnu
  Host CPU: skylake

and on M3 Mac with

LDC - the LLVM D compiler (1.39.0):
  based on DMD v2.109.1 and LLVM 18.1.8
  built with LDC - the LLVM D compiler (1.39.0)
  Default target: arm64-apple-darwin23.6.0
  Host CPU: apple-m1

Though on Mac I have to pass --mtripple=x86_64-pc-linux-gnu to trigger the crash.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions