[llvm-dev] Help with pass manager

Lorenzo Laneve via llvm-dev llvm-dev at lists.llvm.org
Wed Mar 23 15:44:51 PDT 2016


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<TargetMachine> 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;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160323/6a9db3e1/attachment.html>


More information about the llvm-dev mailing list