LLVM llvm::Target::createTargetMachine() errors with "Invalid architecture value"

I’ve checked the target triple’s architecture and it says its x86_64 but when I pass it to createTargetMachine the switch case fails and errors saying “Invalid architecture value”

Here’s the code:

llvm::InitializeAllTargetInfos();
    llvm::InitializeAllTargets();
    llvm::InitializeAllTargetMCs();
    llvm::InitializeAllAsmParsers();
    llvm::InitializeAllAsmPrinters();

    std::string error;
    const llvm::Target* target = llvm::TargetRegistry::lookupTarget(llvm::sys::getDefaultTargetTriple(), error);
    llvm::Triple triple(llvm::sys::getDefaultTargetTriple());
    llvm::errs() << triple.getArch();

    if (!target) {
        std::cout << "Target creation failed" << std::endl;
    }

    llvm::TargetOptions opts;
    auto targetMachine = target->createTargetMachine(llvm::sys::getDefaultTargetTriple(), "generic", "", opts, llvm::Optional<llvm::Reloc::Model>());

And here’s the valgrind dump:

==17233==    at 0x571018B: raise (raise.c:51)
==17233==    by 0x56EF858: abort (abort.c:79)
==17233==    by 0x7C7FC3: llvm::llvm_unreachable_internal(char const*, char const*, unsigned int) (ErrorHandling.cpp:209)
==17233==    by 0x80C9E0: getArchPointerBitWidth(llvm::Triple::ArchType) (Triple.cpp:1338)
==17233==    by 0x80CA06: llvm::Triple::isArch64Bit() const (Triple.cpp:1342)
==17233==    by 0xD62F4E: llvm::X86_MC::ParseX86Triple[abi:cxx11](llvm::Triple const&) (X86MCTargetDesc.cpp:49)
==17233==    by 0xD631DE: llvm::X86_MC::createX86MCSubtargetInfo(llvm::Triple const&, llvm::StringRef, llvm::StringRef) (X86MCTargetDesc.cpp:290)
==17233==    by 0x3EE6B9: llvm::Target::createTargetMachine(llvm::StringRef, llvm::StringRef, llvm::StringRef, llvm::TargetOptions const&, llvm::Optional<llvm::Reloc::Model>, llvm::Optional<llvm::CodeModel::Model>, llvm::CodeGenOpt::Level, bool) const (in /home/jack/Projects/blast/build/blast)
==17233==    by 0x3ED346: CodegenContext::Codegen(void*) (in /home/jack/Projects/blast/build/blast)
==17233==    by 0x3ED152: CodegenContext::Codegen(void*) (in /home/jack/Projects/blast/build/blast)
==17233==    by 0x3F20B4: Editor::Update() (in /home/jack/Projects/blast/build/blast)
==17233==    by 0x3F61D7: main (in /home/jack/Projects/blast/build/blast)

Something is really strange here, can you try to modify the code at Triple.cpp:1342 to print what kind of enum value you get here?
Also maybe try to build and test with ASAN.

You likely should exit here by the way.

I’ll try but won’t that need a full recompile of LLVM?

Yea I will eventually have an error handling system but bc its a text editor with compiler and stuff I don’t want it to crash

@mehdi_amini Added what you asked, here’s the output, seems like there’s an odd bit where it goes to 0?
35
35
35
35
35
0
35
35
35
35
35
35

But what did it print before crashing?

Also: did you run with ASAN?

This was the full message and the valgrind traceback

35
35
35
35
35
0
35
35
35
35
35
35
blast: /media/jack/32A07C604A5709A8/Jack/git/llvm/llvm/include/llvm/Support/Casting.h:104: static bool llvm::isa_impl_cl<To, const From*>::doit(const From*) [with To = llvm::UnreachableInst; From = llvm::Instruction]: Assertion `Val && "isa<> used on a null pointer"' failed.
==25050== 
==25050== Process terminating with default action of signal 6 (SIGABRT)
==25050==    at 0x50B618B: raise (raise.c:51)
==25050==    by 0x5095858: abort (abort.c:79)
==25050==    by 0x5095728: __assert_fail_base.cold (assert.c:92)
==25050==    by 0x50A6F35: __assert_fail (assert.c:101)
==25050==    by 0x4FFB55: llvm::isa_impl_cl<llvm::UnreachableInst, llvm::Instruction const*>::doit(llvm::Instruction const*) (Casting.h:104)
==25050==    by 0x4FED91: llvm::isa_impl_wrap<llvm::UnreachableInst, llvm::Instruction const*, llvm::Instruction const*>::doit(llvm::Instruction const* const&) (Casting.h:131)
==25050==    by 0x4FDB19: llvm::isa_impl_wrap<llvm::UnreachableInst, llvm::Instruction const* const, llvm::Instruction const*>::doit(llvm::Instruction const* const&) (Casting.h:122)
==25050==    by 0x4FC063: bool llvm::isa<llvm::UnreachableInst, llvm::Instruction const*>(llvm::Instruction const* const&) (Casting.h:143)
==25050==    by 0x1C4FA92: llvm::BranchProbabilityInfo::getInitialEstimatedBlockWeight(llvm::BasicBlock const*) (BranchProbabilityInfo.cpp:729)
==25050==    by 0x1C4FDBC: llvm::BranchProbabilityInfo::computeEestimateBlockWeight(llvm::Function const&, llvm::DominatorTree*, llvm::PostDominatorTree*) (BranchProbabilityInfo.cpp:767)
==25050==    by 0x1C5232B: llvm::BranchProbabilityInfo::calculate(llvm::Function const&, llvm::LoopInfo const&, llvm::TargetLibraryInfo const*, llvm::DominatorTree*, llvm::PostDominatorTree*) (BranchProbabilityInfo.cpp:1257)
==25050==    by 0x1C5278F: llvm::BranchProbabilityInfoWrapperPass::runOnFunction(llvm::Function&) (BranchProbabilityInfo.cpp:1310

What’s ASAN?

Right now your error message changed though: the printing does not help when the error isn’t the same…

Also this isn’t a Valgrind failure but an assertion in the code. We’d need a reproducer to figure out what’s happening here, but it went beyond you createTargetMachine call.

ASAN standard for Address Sanitizer, if you know Valgrind then it does kind of the same thing. For LLVM the CMake option to enable it is -DLLVM_USE_SANITIZER=Address, the clang flag is -fsanitize=address. See also UBSAN: UndefinedBehaviorSanitizer — Clang 13 documentation

Yea I think the error message changed because I switched from LLVM 14.0.0git to LLVM 13.0.1, forgot to mention in the message

So I’ve done a bit more debugging and its now crashing on the actual codegen pass (specifically llvm::LegacyPassManager.run(llvm::Module* module)), it looks like its casting an instruction to llvm::UnreachableInst and crashing on that (I have literally no clue what that means)

It’s crashing on a nullptr instruction, this shouldn’t happen, at least assuming the verifier passes before you run the pass manager: double-checking is the IR you’re providing to the pass manager valid?
Can you dump it if it isn’t too large?

I think the verify is fine, it’s not crashing when I verify it now but I’m not checking the return value. How do I dump the ir?

Module.dump(); (most entities should have a “dump” method).

@"Hello, World!" = private unnamed_addr constant [14 x i8] c"Hello, World!\00", align 1
@"Hello, World!.1" = private unnamed_addr constant [14 x i8] c"Hello, World!\00", align 1

define void @main() {
entry:
  %puts = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @"Hello, World!", i32 0, i32 0))
}

declare i32 @puts(i8* %0)

I forgot the return statement.