Help with pass manager

Sorry in advance for the stupid question, i still don’t understand some concepts like passes.
I took a piece of code from llc, and I used it to write a function that creates an object (or assembly) file from an IR module.
It compiles without any problems. But program crashes when it reaches add() method of the pass manager.
Can you help me figuring out what’s the problem please? here is my function

int moduleToObjectFile(llvm::Module *module, std::string &srcname, llvm::LLVMContext &Context) {
SMDiagnostic error;
Triple moduletriple = Triple(module->getTargetTriple());

if (moduletriple.getTriple().empty())
moduletriple.setTriple(sys::getDefaultTargetTriple());

std::string lookuperror;
const Target *moduletarget = TargetRegistry::lookupTarget(MArch, moduletriple, lookuperror);

if (!moduletarget) {
errs() << lookuperror;
return 1;
}

std::string cpuname = getCPUStr(),
ftrlist = getFeaturesStr();

CodeGenOpt::Level OLvl = CodeGenOpt::Default;
switch (‘2’) {
default:
errs() << “invalid optimization level.\n”;
return 1;
case ’ ': break;
case ‘0’: OLvl = CodeGenOpt::None; break;
case ‘1’: OLvl = CodeGenOpt::Less; break;
case ‘2’: OLvl = CodeGenOpt::Default; break;
case ‘3’: OLvl = CodeGenOpt::Aggressive; break;
}

TargetOptions targetopts = InitTargetOptionsFromCodeGenFlags();

std::unique_ptr tmachine(moduletarget->createTargetMachine(moduletriple.getTriple(), cpuname, ftrlist, targetopts, Reloc::Default, CodeModel::Default, OLvl));

assert(tmachine && “Could not allocate target machine!”);

assert(module && “Should have exited if we didn’t have a module!”);
if (FloatABIForCalls != FloatABI::Default)
targetopts.FloatABIType = FloatABIForCalls;

std::unique_ptr<tool_output_file> objoutstream = getOutputFileStream(module, srcname);
if (!objoutstream) return 1;

legacy::PassManager passmanager;

TargetLibraryInfoImpl TLII(moduletriple);

TargetLibraryInfoWrapperPass *tliwp = new TargetLibraryInfoWrapperPass(TLII);

passmanager.add(tliwp);

module->setDataLayout(tmachine->createDataLayout());

setFunctionAttributes(cpuname, ftrlist, *module);

if (RelaxAll.getNumOccurrences() > 0 &&
FileType != TargetMachine::CGFT_ObjectFile)
errs() << “warning: ignoring -mc-relax-all because filetype != obj”;

{
raw_pwrite_stream *outstream = &objoutstream->os();

SmallVector<char, 0> filebuf;
std::unique_ptr<raw_svector_ostream> BOS;
if ((FileType != TargetMachine::CGFT_AssemblyFile && !objoutstream->os().supportsSeeking())) {
BOS = make_unique<raw_svector_ostream>(filebuf);
outstream = BOS.get();
}

AnalysisID StartBeforeID = nullptr;
AnalysisID StartAfterID = nullptr;
AnalysisID StopAfterID = nullptr;
const PassRegistry *PR = PassRegistry::getPassRegistry();
if (!RunPass.empty()) {
if (!StartAfter.empty() || !StopAfter.empty()) {
errs() << “start-after and/or stop-after passes are redundant when run-pass is specified.\n”;
return 1;
}
const PassInfo *PI = PR->getPassInfo(RunPass);
if (!PI) {
errs() << “run-pass pass is not registered.\n”;
return 1;
}
StopAfterID = StartBeforeID = PI->getTypeInfo();
} else {
if (!StartAfter.empty()) {
const PassInfo *PI = PR->getPassInfo(StartAfter);
if (!PI) {
errs() << “start-after pass is not registered.\n”;
return 1;
}
StartAfterID = PI->getTypeInfo();
}
if (!StopAfter.empty()) {
const PassInfo *PI = PR->getPassInfo(StopAfter);
if (!PI) {
errs() << “stop-after pass is not registered.\n”;
return 1;
}
StopAfterID = PI->getTypeInfo();
}
}

if (tmachine->addPassesToEmitFile(passmanager, *outstream, FileType, false, StartBeforeID, StartAfterID, StopAfterID)) {
errs() << “target does not support generation of this file type!\n”;
return 1;
}

passmanager.run(*module);

if (BOS) {
objoutstream->os() << filebuf;
}
}

objoutstream->keep();

return 0;
}

Assuming you are talking about this line:

passmanager.add(tliwp);

I don’t see anything obviously wrong.
Are you hitting an assertion or a pure crash? (if LLVM not built with assertions, please rebuild).
What does your debugger gives you as a stracktrace?

The stack trace:
llvm::PMTopLevelManager::addImmutablePass(llvm::ImmutablePass*)
llvm::PMTopLevelManager::schedulePass(llvm::Pass*)
moduleToObjectFile(llvm::Module*,std::string&,llvm::LLVMContext&)

Sometimes it doesn’t crash because TargetRegistry::lookupTarget() returns an error which says it doesn’t support the current target

The stack trace:
llvm::PMTopLevelManager::addImmutablePass(llvm::ImmutablePass*)
llvm::PMTopLevelManager::schedulePass(llvm::Pass*)
moduleToObjectFile(llvm::Module*,std::string&,llvm::LLVMContext&)

Without mapping to line numbers this is not very helpful: moduleToObjectFile never calls schedulePass.

Also you didn’t answer my previous question about the crash.

Sometimes it doesn’t crash because TargetRegistry::lookupTarget() returns an error which says it doesn’t support the current target

Do you mean that running it multiple time does not always produce the same behavior in the call to lookupTarget?
That’s unexpected and I’d be worried about my program.

Sorry, that’s a pure crash I think, assertions are not triggered.
Xcode doesn’t map those 2 functions to line numbers because they’re in precompiled libraries

This code path is not likely to crash usually. Did you build LLVM yourself? Which version are you using and can you reduce the test case to be “minimal” so that someone can reproduce?
(for instance you don’t need a module to create a pass manager)

I’m using LLVM 3.8.0, and no, it’s the precompiled version
That’s why it doesn’t give me enough info for debug

Update:
Sorry my bad. I built llvm and tried it with debugging version.
It was an assertion (IR/LegacyPassManager.cpp:764) saying that it expects all immutable passes to be initialized.

You may want to try adding this code (copy/pasted from llc.cpp):

// Initialize targets first, so that --version shows registered targets.
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmPrinters();
InitializeAllAsmParsers();

// Initialize codegen and IR passes used by llc so that the -print-after,
// -print-before, and -stop-after options work.
PassRegistry *Registry = PassRegistry::getPassRegistry();
initializeCore(*Registry);
initializeCodeGen(*Registry);
initializeLoopStrengthReducePass(*Registry);
initializeLowerIntrinsicsPass(*Registry);
initializeUnreachableBlockElimPass(*Registry);

Those lines of code are in a function that is called before calling the moduleToObjectFile() function

So we come back to my earlier comment: can you produce a one-file, < 100 lines that reproduce the issue?

The problems happens because PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) returns nullptr in PMTopLevelManager::addImmutablePass(ImmutablePass *P).
This because PassRegistry::getPassRegistry()->getPassInfo(AID) call in it returns nullptr as well.
Should I probably register the pass I want to add with PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) ?
I didn’t do it because llc doesn’t do it either.

Here is the assertion:

Assertion failed: (PassInf && “Expected all immutable passes to be initialized”), function addImmutablePass, file llvm-3.8.0.src/lib/IR/LegacyPassManager.cpp, line 764.

Here is the complete stack trace, which says where to find the assert:

in llvm::PMTopLevelManager::addImmutablePass(llvm::ImmutablePass*) at llvm-3.8.0.src/lib/IR/LegacyPassManager.cpp:764
in llvm::PMTopLevelManager::schedulePass(llvm::Pass*) at llvm-3.8.0.src/lib/IR/LegacyPassManager.cpp:697
in llvm::legacy::PassManagerImpl::add(llvm::Pass*) at llvm-3.8.0.src/lib/IR/LegacyPassManager.cpp:410
in llvm::legacy::PassManager::add(llvm::Pass*) at llvm-3.8.0.src/lib/IR/LegacyPassManager.cpp:1755
in moduleToObjectFile(llvm::Module*, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >&, llvm::LLVMContext&) at Compiler/builders/llc.cpp:189

Passes all need to be initialized before they are added into a pass manager.

Already resolved thanks!
And I did, it was just a bug in the library that I already filed in bugzilla