[llvm-dev] Help with pass manager

165 views
Skip to first unread message

Lorenzo Laneve via llvm-dev

unread,
Mar 23, 2016, 6:45:05 PM3/23/16
to llvm...@lists.llvm.org
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::Nonebreak;
        case '1': OLvl = CodeGenOpt::Lessbreak;
        case '2': OLvl = CodeGenOpt::Defaultbreak;
        case '3': OLvl = CodeGenOpt::Aggressivebreak;
    }
    
    TargetOptions targetopts = InitTargetOptionsFromCodeGenFlags();
    
    std::unique_ptr<TargetMachine> tmachine(moduletarget->createTargetMachine(moduletriple.getTriple(), cpuname, ftrlist, targetopts, Reloc::DefaultCodeModel::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<char0> 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, FileTypefalse, 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;
}

Mehdi Amini via llvm-dev

unread,
Mar 23, 2016, 8:14:19 PM3/23/16
to Lorenzo Laneve, llvm...@lists.llvm.org
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?

-- 
Mehdi



_______________________________________________
LLVM Developers mailing list
llvm...@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

Lorenzo Laneve via llvm-dev

unread,
Mar 23, 2016, 8:41:49 PM3/23/16
to Mehdi Amini, llvm...@lists.llvm.org
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

Mehdi Amini via llvm-dev

unread,
Mar 23, 2016, 8:44:34 PM3/23/16
to Lorenzo Laneve, llvm...@lists.llvm.org
On Mar 23, 2016, at 5:41 PM, Lorenzo Laneve <lore...@icloud.com> wrote:

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.

-- 
Mehdi

Lorenzo Laneve via llvm-dev

unread,
Mar 23, 2016, 8:51:12 PM3/23/16
to Mehdi Amini, llvm...@lists.llvm.org
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

Mehdi Amini via llvm-dev

unread,
Mar 23, 2016, 8:53:41 PM3/23/16
to Lorenzo Laneve, llvm...@lists.llvm.org
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)

-- 
Mehdi

Lorenzo Laneve via llvm-dev

unread,
Mar 23, 2016, 9:01:20 PM3/23/16
to Mehdi Amini, llvm...@lists.llvm.org
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

Lorenzo Laneve via llvm-dev

unread,
Mar 24, 2016, 1:00:02 PM3/24/16
to Mehdi Amini, llvm...@lists.llvm.org
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.

Mehdi Amini via llvm-dev

unread,
Mar 24, 2016, 1:07:51 PM3/24/16
to Lorenzo Laneve, llvm...@lists.llvm.org
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);


--
Mehdi

Lorenzo Laneve via llvm-dev

unread,
Mar 24, 2016, 1:16:54 PM3/24/16
to Mehdi Amini, llvm...@lists.llvm.org
Those lines of code are in a function that is called before calling the moduleToObjectFile() function

Mehdi Amini via llvm-dev

unread,
Mar 24, 2016, 1:21:22 PM3/24/16
to Lorenzo Laneve, llvm...@lists.llvm.org
So we come back to my earlier comment: can you produce a one-file, < 100 lines that reproduce the issue?

--
Mehdi

Lorenzo Laneve via llvm-dev

unread,
Mar 24, 2016, 1:41:18 PM3/24/16
to Mehdi Amini, llvm...@lists.llvm.org
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<char>, std::__1::allocator<char> >&, llvm::LLVMContext&) at Compiler/builders/llc.cpp:189

ChrisBieneman via llvm-dev

unread,
Mar 30, 2016, 11:31:31 AM3/30/16
to Lorenzo Laneve, llvm...@lists.llvm.org
Passes all need to be initialized before they are added into a pass manager.

Are you calling TargetLibraryInfoWrapperPass::initializePass anywhere?

-Chris

Lorenzo Laneve via llvm-dev

unread,
Mar 30, 2016, 11:33:12 AM3/30/16
to ChrisBieneman, llvm...@lists.llvm.org
Already resolved thanks!
And I did, it was just a bug in the library that I already filed in bugzilla
Reply all
Reply to author
Forward
0 new messages