Hello, I'm trying to catch assembly output and do some formatting for its presentation while being executed. I face a problem, which is clear from the error, although I have no clue on how to do it otherwise. If generating the code for its asm output and then for jit execution I get: ********** static llvm::MachineFunction& llvm::MachineFunction::construct(const llvm::Function*, const llvm::TargetMachine&): Assertion `Fn->getAnnotation(MF_AID) == 0 && "Object already exists for this function!"' failed. ********** The code I use: /// The asm code output std::string Err; const llvm::TargetMachineRegistry::entry* MArch llvm::TargetMachineRegistry::getClosestStaticTargetForModule(*_module,Err); std::string FeaturesStr; std::auto_ptr<llvm::TargetMachine>target(MArch->CtorFn(*_module,FeaturesStr)); assert(target.get() && "Could not allocate target machine!"); llvm::TargetMachine &Target = *target.get(); llvm::FunctionPassManager Passes(new llvm::ExistingModuleProvider(_module)); Passes.add(new llvm::TargetData(*Target.getTargetData())); std::ostringstream os; Target.addPassesToEmitFile(Passes,os,llvm::TargetMachine::AssemblyFile, true); Passes.doInitialization(); for (llvm::Module::iterator I = _module->begin(), E=_module->end(); I != E; ++I) if (!I->isDeclaration()) { Passes.run(*I); } Passes.doFinalization(); /// The execution llvm::Function *inputFunction = _module->getFunction("function"); std::vector<llvm::GenericValue> args(0); llvm::GenericValue result; result = _executionEngine->runFunction( inputFunction, args ); **** Thanks, -- .alvaro.castro.castilla.
Álvaro Castro Castilla
2008-May-29 16:29 UTC
[LLVMdev] [solved] Asm output while executing
Hi, Well maybe it was pretty obvious for some people, but it took me some hours. I must point out that I was using llvm 2.2, not svn. An API change affected me: addPassesToEmitFile function only accepted FunctionPassManager, now it works with PassManagerBase, which I needed in order to use the same Pass Manager than my other Passes I'm working on. So I updated to svn and here I put the code I use: ************* std::string Err; const llvm::TargetMachineRegistry::entry* _arch llvm::TargetMachineRegistry::getClosestTargetForJIT(Err); std::string FeaturesStr; llvm::TargetMachine* target(_arch->CtorFn(*_module, FeaturesStr)); assert(target && "Could not allocate target machine!"); target->addPassesToEmitFile(*_passManager, os, llvm::TargetMachine::AssemblyFile, true); target->addPassesToEmitFileFinish(*_passManager, 0, true); _passManager->run(*_module); std::cout << os.str() << std::endl; os.str(""); llvm::Function *inputFunction = _module->getFunction("function"); std::vector<llvm::GenericValue> args(0); llvm::GenericValue result; result = _executionEngine->runFunction( inputFunction, args ); ************* Thanks anyway, I hope this is useful for someone. -- .alvaro.castro.castilla. 2008/5/28 Álvaro Castro Castilla <alvaro.castro.castilla at gmail.com>:> Hello, > > I'm trying to catch assembly output and do some formatting for its > presentation while being executed. > I face a problem, which is clear from the error, although I have no > clue on how to do it otherwise. > If generating the code for its asm output and then for jit execution I get: > > ********** > static llvm::MachineFunction& llvm::MachineFunction::construct(const > llvm::Function*, const llvm::TargetMachine&): Assertion > `Fn->getAnnotation(MF_AID) == 0 && "Object already exists for this > function!"' failed. > ********** > > The code I use: > > > /// The asm code output > > std::string Err; > const llvm::TargetMachineRegistry::entry* MArch > llvm::TargetMachineRegistry::getClosestStaticTargetForModule(*_module,Err); > > > std::string FeaturesStr; > std::auto_ptr<llvm::TargetMachine>target(MArch->CtorFn(*_module,FeaturesStr)); > assert(target.get() && "Could not allocate target machine!"); > llvm::TargetMachine &Target = *target.get(); > > llvm::FunctionPassManager Passes(new > llvm::ExistingModuleProvider(_module)); > Passes.add(new llvm::TargetData(*Target.getTargetData())); > > std::ostringstream os; > Target.addPassesToEmitFile(Passes,os,llvm::TargetMachine::AssemblyFile, > true); > > Passes.doInitialization(); > for (llvm::Module::iterator I = _module->begin(), > E=_module->end(); I != E; ++I) > if (!I->isDeclaration()) > { > Passes.run(*I); > } > Passes.doFinalization(); > > /// The execution > > llvm::Function *inputFunction = _module->getFunction("function"); > std::vector<llvm::GenericValue> args(0); > llvm::GenericValue result; > result = _executionEngine->runFunction( inputFunction, args ); > > **** > > Thanks, > > > -- > > .alvaro.castro.castilla. >