Skip to content

[Debian Linux, LLVMv17.0.6, C++] LLVM crashes when compiling to assembler #138037

@TheoW03

Description

@TheoW03

I have ran into this issue on 14 as well.
But I cant generate an assembler file without the LLVM crashing

This is the error I get

terminate called without an active exception
Aborted (core dumped)

and here is the code

 llvm::LLVMContext context;
    llvm::Module module("MyModule", context);

    llvm::IRBuilder<> builder(context);

// Initialize the target registry etc.llvm::InitializeAllTargets();

    llvm::InitializeAllTargetInfos();
    llvm::InitializeAllTargets();
    llvm::InitializeAllTargetMCs();
    llvm::InitializeAllAsmParsers();
    llvm::InitializeAllAsmPrinters();
    std::string Error;
    auto TargetTriple = llvm::sys::getDefaultTargetTriple();

    auto Target = llvm::TargetRegistry::lookupTarget(TargetTriple, Error);
    module.setTargetTriple(TargetTriple);
    if (output.print_llvm == 1)
    {
        module.print(llvm::outs(), nullptr);
    }

    if (!Target)
    {
        std::cout << Error << std::endl;
        return;
    }

    auto CPU = "generic";
    auto Features = "";

    llvm::TargetOptions opt;
    auto TargetMachine = Target->createTargetMachine(TargetTriple, CPU, Features, opt, llvm::Reloc::PIC_);
    module.setDataLayout(TargetMachine->createDataLayout());

     llvm::legacy::PassManager pass;
    
      llvm::raw_fd_ostream dest("file.s", EC, llvm::sys::fs::OF_None);
            if (EC)
            {
                llvm::errs() << "Could not open file: " << EC.message();
                return;
            }
            auto FileType = llvm::CodeGenFileType::CGFT_AssemblyFile;

            if (TargetMachine->addPassesToEmitFile(pass, dest, nullptr, FileType))
            {
                llvm::errs() << "TargetMachine can't emit a file of this type";
                return;
            }
            pass.run(module);

            dest.flush();

I know this is a LLVM error because it works with generating an IR file, compiled binary, and an unoptimized IR file.

Metadata

Metadata

Assignees

No one assigned

    Labels

    crashPrefer [crash-on-valid] or [crash-on-invalid]llvm:codegen

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions