How to properly insert a function call using LLVM? - llvm

I'm trying to set up a pass that will insert a couple of global variables as well as a couple of function calls at the beginning of main. However, I believe I have an issue with setting up the function description correctly. My code compiles, but when I try to run the pass I get this error:
void llvm::CallInst::init(llvm::Value*, llvm::ArrayRef<llvm::Value*>, const llvm::Twine&):
Assertion `(Args.size() == FTy->getNumParams() || (FTy->isVarArg() && Args.size() > FTy-
>getNumParams())) && "Calling a function with bad signature!"' failed.
Here is the code I have written so far:
#include "llvm/IR/Instructions.h"
#include "llvm/Pass.h"
#include "llvm/Support/InstIterator.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Module.h"
using namespace llvm;
namespace{
struct fun_insert : public FunctionPass{
static char ID;
fun_insert():FunctionPass(ID){}
virtual bool runOnFunction(Function &F){
if (F.getName() == "main"){
for (inst_iterator I = inst_begin(F), E= inst_end(F); I != E; ++I){
Instruction *inst = &*I;
if(dyn_cast<AllocaInst> (inst)){
errs() << "test" << "\n";
GlobalVariable *virtAddr = new GlobalVariable(*F.getParent(),
Type::getInt8PtrTy(F.getContext()), false,
GlobalValue::ExternalLinkage, 0, "virt_addr");
virtAddr->setAlignment(4);
Module *M = F.getParent();
Constant *c = M->getOrInsertFunction("open",
IntegerType::get(F.getContext(),32),
PointerType::get(Type::getInt8PtrTy(F.getContext(), 8),8),
IntegerType::get(F.getContext(),32), NULL);
Function *open = cast<Function>(c);
//ConstantInt *a = ConstantInt::get(M->getContext(), APInt(32, 9437184));
IRBuilder<> builder(inst);
Value *strPtr = builder.CreateGlobalStringPtr("/dev/mem", ".str");
ConstantInt *a = builder.getInt32(9437184);
//CallInst *openRet = builder.CreateCall2(open, strPtr, a, "open");
builder.CreateCall2(open, strPtr, a, "open");
}
errs() << *inst <<"\n";
}
}
return false;
}
};
}
char fun_insert::ID=0;
static RegisterPass<fun_insert>
X("fun_insert", "Insert Function Test", false, false);
Thanks for any help!

I believe I have resolved the issue. The issue did lie with the function declaration and call. I adjusted the code as follows:
Constant *c = M->getOrInsertFunction("open",
FunctionType::getInt32Ty(F.getContext()),
Type::getInt8PtrTy(F.getContext()),
Type::getInt32Ty(F.getContext()),
NULL);
Function *open = cast<Function>(c);
IRBuilder<> builder(inst);
Value *strPtr = builder.CreateGlobalStringPtr("/dev/mem", ".str");
ConstantInt *a = builder.getInt32(9437184);
builder.CreateCall2(open,strPtr,a);

Related

How to call sigaction from C++

I know how to use it in C (with signal.h), but the <csignal> library is provided in C++ and I want to know if it includes sigaction? I tried running it but it said not found. I was wondering if I did something wrong?
#include <iostream>
#include <string>
#include <cstdio>
#include <csignal>
namespace {
volatile bool quitok = false;
void handle_break(int a) {
if (a == SIGINT) quitok = true;
}
std::sigaction sigbreak;
sigbreak.sa_handler = &handle_break;
sigbreak.sa_mask = 0;
sigbreak.sa_flags = 0;
if (std::sigaction(SIGINT, &sigbreak, NULL) != 0) std::perror("sigaction");
}
int main () {
std::string line = "";
while (!::quitok) {
std::getline(std::cin, line);
std::cout << line << std::endl;
}
}
But for some reason it doesn't work.
EDIT:
By "doesn't work", I mean the compiler fails and says there's no std::sigaction function or struct.
sigaction is C POSIX isn't it?
sigaction is in POSIX, not the C++ standard, and it's in the global namespace.
You'll also need the struct keyword to differentiate between sigaction, the struct,
and sigaction, the function.
Finally, the initialization code will need to be in a function -- you can't have it
in file scope.
#include <cstdio>
#include <signal.h>
namespace {
volatile sig_atomic_t quitok = false;
void handle_break(int a) {
if (a == SIGINT) quitok = true;
}
}
int main () {
struct sigaction sigbreak;
sigbreak.sa_handler = &handle_break;
sigemptyset(&sigbreak.sa_mask);
sigbreak.sa_flags = 0;
if (sigaction(SIGINT, &sigbreak, NULL) != 0) std::perror("sigaction");
//...
}

LLVM: simple example of a just-in-time compilation

I'm learning LLVM and trying to compile a simple function:
int sum(int a, int b) {
return a+b;
};
on the fly.
So here's the code I have so far:
#include <string>
#include <vector>
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Verifier.h"
using namespace llvm;
static LLVMContext &Context = getGlobalContext();
static std::unique_ptr<Module> MyModule = make_unique<Module>("my compiler", Context);
Function *createFunc(IRBuilder<> &Builder, std::string Name) {
std::vector<Type*> Integers(2, Builder.getInt32Ty());
auto *funcType = FunctionType::get(Builder.getInt32Ty(), Integers, false);
auto *fooFunc = Function::Create(funcType, Function::ExternalLinkage, Name, MyModule.get());
return fooFunc;
};
int main(int argc, char* argv[]) {
static IRBuilder<> Builder(Context);
auto *fooFunc = createFunc(Builder, "sum");
auto *entry = BasicBlock::Create(Context, "entry", fooFunc);
Builder.SetInsertPoint(entry);
// Fill the function body
auto args = fooFunc->arg_begin();
Value *arg1 = &(*args);
args = std::next(args);
Value *arg2 = &(*args);
auto *sum = Builder.CreateAdd(arg1, arg2, "tmp");
Builder.CreateRet(sum);
verifyFunction(*fooFunc);
// TODO: compile and run it
MyModule->dump();
return 0;
}
This compiles and when I run it I get the expected output:
; ModuleID = 'my compiler'
define i32 #sum(i32, i32) {
entry:
%tmp = add i32 %0, %1
ret i32 %tmp
}
just like in the tutorial.
But now I want to compile this function and run it from C++. I'm looking for the easiest way to do something like that:
auto compiledStuff = ...;
auto compiledFn = (int (*)(int, int))compiledStuff;
auto result = compiledFn(3, 8);
I've been digging through the official Kaleidoscope tutorial but the JIT tutorial is really complicated and seems to focus on optimizations and laziness while I still can't figure out how to easily compile a module and call a function from it.
Any help?
So, I've digged through the KaleidoscopeJIT and retrieved the most important pieces. First of all note that I'm using llvm-4.0. I've had lots of issues by not realizing how really incompatible 4.0 and lower versions are.
The code works with C++11. I'm using clang++-4.0 with following compilation flags:
llvm-config-4.0 --cxxflags --ldflags --system-libs --libs core engine
And now the entire code:
#include <string>
#include <vector>
#include <iostream>
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Verifier.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
#include "llvm/ExecutionEngine/RuntimeDyld.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
#include "llvm/ExecutionEngine/Orc/LambdaResolver.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Mangler.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/TargetSelect.h"
using namespace llvm;
typedef orc::ObjectLinkingLayer<> ObjLayerT;
typedef orc::IRCompileLayer<ObjLayerT> CompileLayerT;
static LLVMContext Context;
static auto MyModule = make_unique<Module>("my compiler", Context);
Function *createFunc(IRBuilder<> &Builder, std::string Name) {
std::vector<Type*> Integers(2, Builder.getInt32Ty());
auto *funcType = FunctionType::get(Builder.getInt32Ty(), Integers, false);
auto *fooFunc = Function::Create(funcType, Function::ExternalLinkage, Name, MyModule.get());
return fooFunc;
};
void updateBody(Function *fooFunc, IRBuilder<> &Builder) {
auto *entry = BasicBlock::Create(Context, "entry", fooFunc);
Builder.SetInsertPoint(entry);
auto args = fooFunc->arg_begin();
Value *arg1 = &(*args);
args = std::next(args);
Value *arg2 = &(*args);
auto *sum = Builder.CreateAdd(arg1, arg2, "tmp");
Builder.CreateRet(sum);
};
int main(int argc, char* argv[]) {
// Prepare the module
static IRBuilder<> Builder(Context);
auto *fooFunc = createFunc(Builder, "sum");
updateBody(fooFunc, Builder);
verifyFunction(*fooFunc);
// Initilaze native target
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();
// Prepare jit layer
ObjLayerT ObjectLayer;
std::unique_ptr<TargetMachine> TM(EngineBuilder().selectTarget());
DataLayout DL(TM->createDataLayout());
CompileLayerT CompileLayer(ObjectLayer, orc::SimpleCompiler(*TM));
auto Resolver = orc::createLambdaResolver(
[&](const std::string &Name) {
if (auto Sym = CompileLayer.findSymbol(Name, false))
return Sym;
return JITSymbol(nullptr);
},
[](const std::string &S) { return nullptr; }
);
// Add MyModule to the jit layer
std::vector<std::unique_ptr<Module>> Modules;
Modules.push_back(std::move(MyModule));
CompileLayer.addModuleSet(
std::move(Modules),
make_unique<SectionMemoryManager>(),
std::move(Resolver)
);
// Retrieve the foo symbol
std::string MangledName;
raw_string_ostream MangledNameStream(MangledName);
Mangler::getNameWithPrefix(MangledNameStream, "sum", DL);
auto Sym = CompileLayer.findSymbol(MangledNameStream.str(), true);
// Cast to function
auto func = (int(*)(int, int))Sym.getAddress();
// Try it
std::cout << func(5, 7) << std::endl;
return 0;
}
I'm not sure if all includes are needed but anyway it works like a charm. Although I'm looking forward for any comment on how to improve it. :)
Using the provided KaleidoscopeJIT.h this is pretty straightforward (I'm using LLVM 4.0.0 in this example):
// Your existing includes here.
#include "llvm/Support/TargetSelect.h" // For InitializeNativeTarget() etc.
#include "KaleidoscopeJIT.h"
int main() {
// Your existing main body here.
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
InitializeNativeTargetAsmParser();
orc::KaleidoscopeJIT jit;
MyModule->setDataLayout(jit.getTargetMachine().createDataLayout());
auto moduleHandle = jit.addModule(std::move(MyModule)); // JIT-compile MyModule.
auto symbol = jit.findSymbol("sum"); // Get the compiled sum function.
auto sumFunc = (int(*)(int, int)) symbol.getAddress(); // Cast it.
auto result = sumFunc(42, 42); // Call it.
assert(result == 84); // VoilĂ .
}

opencv_NormalizeHist.cpp:47:34: error: 'NormalizeHist' was not declared in this scope

I have written the code to access opencv(Mainly used for image processing) function from Scilab(numerical computation software). When I run bilder gateway file, I am encountering below error.
opencv_NormalizeHist.cpp:47:34: error: 'NormalizeHist' was not declared in this scope.
#include <numeric>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
extern "C"
{
#include "api_scilab.h"
#include "Scierror.h"
#include "BOOL.h"
#include <localization.h>
#include "../common.h"
int opencv_NormalizeHist(char *fname, unsigned long fname_len)
{
SciErr sciErr;
int iRows=0,iCols=0;
int *piAddr2 = NULL;
//checking input argument
CheckInputArgument(pvApiCtx, 2, 2);
CheckOutputArgument(pvApiCtx, 0, 0) ;
//Define histogram
Mat hist;
retrieveImage(hist,1);
double *factor;
//for value of factor
sciErr = getVarAddressFromPosition(pvApiCtx,2,&piAddr2);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr2, &iRows, &iCols ,&factor);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
NormalizeHist(hist, factor[0]); //Normalizing hist
string tempstring = type2str(hist.type());
char *checker;`enter code herek
checker = (char *)malloc(tempstring.size() + 1);
memcpy(checker, tempstring.c_str(), tempstring.size() + 1);
returnImage(checker,hist,1); //here, remove the temp as a parameter as it is not needed, and instead add 1 as the third parameter. 1 denotes that the first output argument will be this variable
free(checker); //free memory taken up by checker, i missed this earlier
//Assigning the list as the Output Variable
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
//Returning the Output Variables as arguments to the Scilab environment
ReturnArguments(pvApiCtx);
return 0;
}
/* ==================================================================== */
}

LLVM error accessing loopinfo in function pass

I'm trying to get loop information from IR by writing a function pass. So I followed some examples and wrote like following. I'm not very familiar with writing passes and pass managers.
#include <iostream>
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Function.h"
#include "llvm/BasicBlock.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Support/IRReader.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Pass.h"
#include "llvm/PassManager.h"
using namespace llvm;
namespace {
class DetectLoop: public FunctionPass {
public:
DetectLoop() : FunctionPass(ID) {}
static char ID;
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<LoopInfo>();//I'm not sure if it's correct here *****1*****
}
virtual bool runOnFunction(Function &F) {
if (!F.isDeclaration())
LoopInfo &li = getAnalysis<LoopInfo>(F);//*****2*****
for (Function::iterator I = F.begin(); I != F.end(); I++) {
BasicBlock *BB = I;
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
Instruction &I = *BI++;
//did noting inside
}
}
return false;
}
};
}
char DetectLoop::ID = 0;
int main(int argc, char** argv)
{
if (argc < 2) {
errs() << "Expected an argument - IR file name\n";
exit(1);
}
SMDiagnostic Err;
std::cout<<argv[1]<<std::endl;
Module *Mod = ParseIRFile(argv[1], Err, getGlobalContext());
Err.Print(argv[0], errs());
if (Mod) {
PassManager PM;
PM.add(new DetectLoop());
PM.add(new LoopInfo());//*****3*****
PM.run(*Mod);
}
else {
std::cout << "Mod is null" << std::endl;
}
return 0;
}
While I was running this program, it just showed me segmentation error(core dumped),
but when I commented out addRequired the error msg I got was
IRparser: PassManager.cpp:1200: virtual llvm::Pass* llvm::PMDataManager::getOnTheFlyPass
(llvm::Pass*, llvm::AnalysisID, llvm::Function&): Assertion `0 && "Unable to find on the fly pass"' failed.
Stack dump:
0. Running pass 'Function Pass Manager' on module '../../testcase/forloop1.ll'.
1. Running pass 'Unnamed pass: implement Pass::getPassName()' on function '#main'
Aborted (core dumped)
I have marked 3 places I'm not sure which is correct or not. Can anyone help me with that?
If you use it in a Module:
LoopInfo &li = getAnalysis<LoopInfo>(F)
If you use it in a function:
LoopInfo &li = getAnalysis<LoopInfo>()
I had this question before. After searching several answers, I found the solution.
You should change the location of the two statements below:
PM.add(new DetectLoop());
PM.add(new LoopInfo());//*****3*****
Because LoopInfo Pass must be registed before your own pass.

failing to parse C++ using llvm and clang

I'm writing a little tool with llvm to parse C and C++ code, but I can't seem to get it to successfully parse C++ at all. I'm probably missing something obvious.
This is what I have so far:
#include <iostream>
#include "llvm/Support/Host.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Frontend/FrontendOptions.h"
#include "clang/Frontend/LangStandard.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/DirectoryLookup.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/Parse/ParseAST.h"
class MyASTConsumer : public clang::ASTConsumer {
public:
bool HandleTopLevelDecl(clang::DeclGroupRef d);
virtual ~MyASTConsumer() { }
};
bool MyASTConsumer::HandleTopLevelDecl(clang::DeclGroupRef d)
{
for(auto ii = d.begin(); ii != d.end(); ii++)
{
printf("decl type: %s\n", (*ii)->getDeclKindName());
auto namedDecl = llvm::dyn_cast<clang::NamedDecl>(*ii);
if(namedDecl)
{
printf("name: %s\n", namedDecl->getName().data());
}
}
return true;
}
int main(int, char **argv)
{
using clang::CompilerInstance;
using clang::TargetOptions;
using clang::TargetInfo;
using clang::FileEntry;
using clang::DiagnosticOptions;
using clang::TextDiagnosticPrinter;
using clang::SrcMgr::CharacteristicKind;
using clang::StringRef;
using clang::DirectoryLookup;
using llvm::MemoryBuffer;
using clang::LangOptions;
using clang::FrontendOptions;
using clang::LangStandard;
using clang::CompilerInvocation;
using clang::InitializePreprocessor;
using clang::Preprocessor;
using clang::PreprocessorOptions;
using clang::HeaderSearch;
using clang::HeaderSearchOptions;
CompilerInstance ci;
DiagnosticOptions diagnosticOptions;
ci.createDiagnostics();
CompilerInvocation *invocation = new CompilerInvocation;
LangOptions &langOpts = ci.getLangOpts();
langOpts.RTTI = 1;
langOpts.Bool = 1;
langOpts.CPlusPlus11 = 1;
langOpts.GNUKeywords = 1;
langOpts.CXXExceptions = 1;
langOpts.POSIXThreads = 1;
langOpts.SpellChecking = 1;
invocation->setLangDefaults(langOpts, clang::IK_CXX, LangStandard::lang_gnucxx11);
ci.setInvocation(invocation);
llvm::IntrusiveRefCntPtr<TargetOptions> pto( new TargetOptions() );
pto->Triple = llvm::sys::getDefaultTargetTriple();
llvm::IntrusiveRefCntPtr<TargetInfo> pti(TargetInfo::CreateTargetInfo(ci.getDiagnostics(), pto.getPtr()));
ci.setTarget(pti.getPtr());
ci.createFileManager();
auto &fileManager = ci.getFileManager();
ci.createSourceManager(fileManager);
llvm::IntrusiveRefCntPtr<HeaderSearchOptions> headerSearchOpts( new HeaderSearchOptions() );
ci.createPreprocessor();
auto &pp = ci.getPreprocessor();
pp.setPredefines(builtinMacros);
HeaderSearch &headerSearch = pp.getHeaderSearchInfo();
for(auto &inc: builtinIncludePaths)
{
auto dirEntry = fileManager.getDirectory(StringRef(inc), true);
DirectoryLookup dirLookup(dirEntry, CharacteristicKind::C_System, false);
headerSearch.AddSearchPath (dirLookup, true);
}
MyASTConsumer *astConsumer = new MyASTConsumer();
ci.setASTConsumer(astConsumer);
ci.createASTContext();
const FileEntry *pFile = fileManager.getFile(argv[1]);
auto &sourceManager = ci.getSourceManager();
sourceManager.createMainFileID(pFile);
ci.getDiagnosticClient().BeginSourceFile(
ci.getLangOpts(),
&pp
);
clang::ParseAST(pp, astConsumer, ci.getASTContext());
ci.getDiagnosticClient().EndSourceFile();
return 0;
}
It does parse C just fine, but it errors out on the namespace keyword and extern "C" { blocks. So far I'm stumped. If anyone has a clue, something I'm missing, please share.
I believe I figured out the problem. You should always call setInvocation on the compiler instance before calling a lot of the methods on the compiler instance, as it actually just proxies to the invocation.
I moved the setInvocation call to right after the CompilerInvocation object is created, and things now work.