ModelGeneratorContext::ModelGeneratorContext(libsbml::SBMLDocument const *doc,
    unsigned options) :
        ownedDoc(0),
        doc(0),
        symbols(new LLVMModelDataSymbols(doc->getModel(), options)),
        modelSymbols(new LLVMModelSymbols(getModel(), *symbols)),
        errString(new string()),
        context(0),
        executionEngine(0),
        module(0),
        builder(0),
        functionPassManager(0),
        options(options),
        moietyConverter(0)
{
    try
    {
        if (options & rr::ModelGenerator::CONSERVED_MOIETIES)
        {
            Log(Logger::LOG_NOTICE) << "performing conserved moiety conversion";

            moietyConverter = new rr::conservation::ConservedMoietyConverter();

            if (moietyConverter->setDocument(doc) != LIBSBML_OPERATION_SUCCESS)
            {
                throw_llvm_exception("error setting conserved moiety converter document");
            }

            if (moietyConverter->convert() != LIBSBML_OPERATION_SUCCESS)
            {
                throw_llvm_exception("error converting document to conserved moieties");
            }

            this->doc = moietyConverter->getDocument();

            SBMLWriter sw;
            char* convertedStr = sw.writeToString(doc);

            Log(Logger::LOG_INFORMATION) << "***************** Conserved Moiety Converted Document ***************";
            Log(Logger::LOG_INFORMATION) << convertedStr;
            Log(Logger::LOG_INFORMATION) << "*********************************************************************";

            delete convertedStr;
        }
        else
        {
            this->doc = doc;
        }

        symbols = new LLVMModelDataSymbols(doc->getModel(), options);

        modelSymbols = new LLVMModelSymbols(getModel(), *symbols);


        // initialize LLVM
        // TODO check result
        InitializeNativeTarget();

        context = new LLVMContext();
        // Make the module, which holds all the code.
        module = new Module("LLVM Module", *context);

        builder = new IRBuilder<>(*context);

        // engine take ownership of module
        EngineBuilder engineBuilder(module);

        //engineBuilder.setEngineKind(EngineKind::JIT);
        engineBuilder.setErrorStr(errString);
        executionEngine = engineBuilder.create();

        addGlobalMappings();

        createLibraryFunctions(module);

        ModelDataIRBuilder::createModelDataStructType(module, executionEngine, *symbols);

        initFunctionPassManager();

    }
    catch(const std::exception&)
    {
        cleanup();
        throw;
    }
}
ModelGeneratorContext::ModelGeneratorContext(std::string const &sbml,
    unsigned options) :
        ownedDoc(0),
        doc(0),
        symbols(0),
        modelSymbols(0),
        errString(new string()),
        context(0),
        executionEngine(0),
        module(0),
        builder(0),
        functionPassManager(0),
        options(options),
        moietyConverter(0)
{
    try
    {
        ownedDoc = checkedReadSBMLFromString(sbml.c_str());

        if (options & rr::ModelGenerator::CONSERVED_MOIETIES)
        {
            if ((rr::Config::getInt(rr::Config::ROADRUNNER_DISABLE_WARNINGS) &
                    rr::Config::ROADRUNNER_DISABLE_WARNINGS_CONSERVED_MOIETY) == 0)
            {
                Log(Logger::LOG_NOTICE) << "performing conserved moiety conversion";
            }

            // check if already conserved doc
            if (rr::conservation::ConservationExtension::isConservedMoietyDocument(ownedDoc))
            {
                doc = ownedDoc;
            }
            else
            {
                moietyConverter = new rr::conservation::ConservedMoietyConverter();

                if (moietyConverter->setDocument(ownedDoc) != LIBSBML_OPERATION_SUCCESS)
                {
                    throw_llvm_exception("error setting conserved moiety converter document");
                }

                if (moietyConverter->convert() != LIBSBML_OPERATION_SUCCESS)
                {
                    throw_llvm_exception("error converting document to conserved moieties");
                }

                doc = moietyConverter->getDocument();

                SBMLWriter sw;
                char* convertedStr = sw.writeToString(doc);

                Log(Logger::LOG_INFORMATION) << "***************** Conserved Moiety Converted Document ***************";
                Log(Logger::LOG_INFORMATION) << convertedStr;
                Log(Logger::LOG_INFORMATION) << "*********************************************************************";

                free(convertedStr);
            }
        }
        else
        {
            doc = ownedDoc;
        }

        symbols = new LLVMModelDataSymbols(doc->getModel(), options);

        modelSymbols = new LLVMModelSymbols(getModel(), *symbols);


        // initialize LLVM
        // TODO check result
        InitializeNativeTarget();

        context = new LLVMContext();
        // Make the module, which holds all the code.
        module = new Module("LLVM Module", *context);

        builder = new IRBuilder<>(*context);

        // engine take ownership of module
        EngineBuilder engineBuilder(module);

        engineBuilder.setErrorStr(errString);
        executionEngine = engineBuilder.create();

        addGlobalMappings();

        createLibraryFunctions(module);

        ModelDataIRBuilder::createModelDataStructType(module, executionEngine, *symbols);

        initFunctionPassManager();

    }
    catch(const std::exception&)
    {
        // we might have allocated memory in a failed ctor,
        // clean it up here.
        // destructors are not called on *this* class when exception is raised
        // in the ctor.
        cleanup();
        throw;
    }
}
Пример #3
0
JITEngine::JITEngine()
: m_llvmModule(0)
, m_llvmPassManager(0)
, m_llvmFuncPassManager(0)
, m_llvmEngine(0)
, m_eeerror("")
, m_irBuilder(new llvm::IRBuilder<>(llvm::getGlobalContext()))
{
    // Init llvm target
    llvm::InitializeNativeTarget();
    llvm::InitializeNativeTargetAsmPrinter();
    llvm::LLVMContext &llvmContext = llvm::getGlobalContext();
    m_llvmModule = new llvm::Module("JIT", llvmContext);

    // Create a Execution engine via the engine builder
    llvm::EngineBuilder engineBuilder(m_llvmModule);
    engineBuilder.setUseMCJIT(true); 
    //engineBuilder.setOptLevel(llvm::CodeGenOpt::Aggressive); // Test gdb
    engineBuilder.setErrorStr(&m_eeerror);
    engineBuilder.setAllocateGVsWithCode(true); // Global values
    m_llvmEngine = engineBuilder.create();

    m_llvmPassManager = new llvm::PassManager();
    m_llvmPassManager->add(new llvm::DataLayout(*m_llvmEngine->getDataLayout()));
    //m_llvmPassManager->add(llvm::createCFGSimplificationPass());     // Merge & remove BBs
    //m_llvmPassManager->add(llvm::createInstructionCombiningPass());  // Combine silly seq's
    //m_llvmPassManager->add(llvm::createTailCallEliminationPass());   // Eliminate tail calls
    //m_llvmPassManager->add(llvm::createCFGSimplificationPass());     // Merge & remove BBs
    //m_llvmPassManager->add(llvm::createReassociatePass());           // Reassociate expressions
    //m_llvmPassManager->add(llvm::createLoopRotatePass());            // Rotate Loop
    //m_llvmPassManager->add(llvm::createLICMPass());                  // Hoist loop invariants
    //m_llvmPassManager->add(llvm::createInstructionCombiningPass());
    //m_llvmPassManager->add(llvm::createIndVarSimplifyPass());        // Canonicalize indvars
    //m_llvmPassManager->add(llvm::createLoopIdiomPass());             // Recognize idioms like memset.
    //m_llvmPassManager->add(llvm::createLoopDeletionPass());          // Delete dead loops
    ////m_llvmPassManager->add(llvm::createLoopVectorizePass());
    //m_llvmPassManager->add(llvm::createFunctionInliningPass());
    ////m_llvmPassManager->add(llvm::createBBVectorizePass());
    //m_llvmPassManager->add(llvm::createInstructionCombiningPass());
    
    // Function pass manager
    m_llvmFuncPassManager = new llvm::FunctionPassManager(m_llvmModule);
    // Set up the optimizer pipeline.  Start with registering info about how the
    // target lays out data structures.
    m_llvmFuncPassManager->add(new llvm::DataLayout(*m_llvmEngine->getDataLayout()));
    // Promote allocas to registers.
    //m_llvmFuncPassManager->add(llvm::createPromoteMemoryToRegisterPass());
    //// Provide basic AliasAnalysis support for GVN.
    //m_llvmFuncPassManager->add(llvm::createBasicAliasAnalysisPass());
    //// Do simple "peephole" optimizations and bit-twiddling optzns.
    //m_llvmFuncPassManager->add(llvm::createInstructionCombiningPass());
    //// Reassociate expressions.
    //m_llvmFuncPassManager->add(llvm::createReassociatePass());
    //// Eliminate Common SubExpressions.
    //m_llvmFuncPassManager->add(llvm::createGVNPass());
    //// Simplify the control flow graph (deleting unreachable blocks, etc).
    //m_llvmFuncPassManager->add(llvm::createCFGSimplificationPass());

    //// Unroll small loops
    //m_llvmFuncPassManager->add(llvm::createLoopInstSimplifyPass());
    //m_llvmFuncPassManager->add(llvm::createLoopRotatePass());
    //// Recognize idioms like memset.
    //m_llvmFuncPassManager->add(llvm::createLoopIdiomPass());
    ////m_llvmFuncPassManager->add(llvm::createLoopVectorizePass());
    //m_llvmFuncPassManager->add(llvm::createLoopUnrollPass());
    ////m_llvmFuncPassManager->add(llvm::createBBVectorizePass());
    //m_llvmFuncPassManager->doInitialization();
    //m_nodeCompiler = new NodeCompiler();
}