张雨姗(ZHANG Yushan)-11310380 via llvm-dev
2018-Dec-28 16:38 UTC
[llvm-dev] [Instrumentation] Basic Block in function 'main' does not have terminator!
Hi all,
I want to add a new function to a module, which works as this:
void cmpr(int l, int r) {
if (i < l || i > r)
std::exit(0);
}
Here is my code:
// if required, instrument this function
BasicBlock * first = &func->getEntryBlock();
IRBuilder<> Builder(first);
CallInst* v = Builder.CreateCall2(getTargetInst(), vl, vr);
Value * getTargetInst() {// insert a new function to program
Constant *c = module->getOrInsertFunction("cmpr",
FunctionType::getVoidTy(*context),
Type::getInt64Ty(*context),
Type::getInt64Ty(*context), NULL);
f = &cast<Function>(*c);
// define arguments
Function::arg_iterator args = f->arg_begin();
Value* l = args++;
l->setName("l"); // lower bound
Value* r = args++;
r->setName("r"); // upper bound
BasicBlock *pb = BasicBlock::Create(*context, "entry", f);
BasicBlock *blk = BasicBlock::Create(*context, "cond_true", f);
BasicBlock *nblk = BasicBlock::Create(*context, "cond_false", f);
IRBuilder<> Builder(pb);
// i < l
Value *lower = Builder.CreateICmpSLT(vt, l);
// i > r
Value *upper = Builder.CreateICmpSGT(vt, r);
// l - i || i - r (l > i or i > r)
Value *both = Builder.CreateOr(lower, upper);
// if (l > i || i > r), exit(0)
BranchInst *br = Builder.CreateCondBr(both, blk, nblk);
Builder.SetInsertPoint(ret);
// cond_true: call exit(0)
IRBuilder<> blkBuilder(blk);
Value *one = ConstantInt::get(Type::getInt64Ty(*context), 0, true);
Value *blkv = module->getOrInsertFunction("std::exit",
FunctionType::getVoidTy(*context),
Type::getInt64Ty(*context), NULL);
blkBuilder.CreateCall(blkv, one);
return f;
}
However, I get this error (I tried to fix with adding "CreateRetVoid"
and other solutions from StackOverflow but didn't work ):Basic Block in
function 'main' does not have terminator!
label %entry
LLVM ERROR: Broken function found, compilation aborted!
Could anyone help me to fix this? I appreciate any possible help.
BTW, how could I call standard library in instrumented code?
Thanks,
Yushan
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20181229/b4a4f826/attachment.html>
Zhang via llvm-dev
2018-Dec-29 00:59 UTC
[llvm-dev] [Instrumentation] Basic Block in function 'main' does not have terminator!
First, you can't just call std::exit like that, you need to mangle its name
first. Google "C++ name mangling"
wrt the second part, can you try to print out the function after your transform?
I'm slightly lost in your code. ``blkBuilder.CreateCall `` does need a
following-up terminator, however since you mentioned you added it and still had
no luck, again, I suggest you print out the IR after your transform
Zhang
------------------ Original ------------------
From: "张雨姗(ZHANG Yushan)-11310380 via llvm-dev"<llvm-dev at
lists.llvm.org>;
Date: Sat, Dec 29, 2018 00:38 AM
To: "llvm-dev"<llvm-dev at lists.llvm.org>;
Subject: [llvm-dev] [Instrumentation] Basic Block in function 'main'
does not have terminator!
Hi all,
I want to add a new function to a module, which works as this:
void cmpr(int l, int r) {
if (i < l || i > r)
std::exit(0);
}
Here is my code:
// if required, instrument this function
BasicBlock * first = &func->getEntryBlock();
IRBuilder<> Builder(first);
CallInst* v = Builder.CreateCall2(getTargetInst(), vl, vr);
Value * getTargetInst() {// insert a new function to program
Constant *c = module->getOrInsertFunction("cmpr",
FunctionType::getVoidTy(*context),
Type::getInt64Ty(*context),
Type::getInt64Ty(*context), NULL);
f = &cast<Function>(*c);
// define arguments
Function::arg_iterator args = f->arg_begin();
Value* l = args++;
l->setName("l"); // lower bound
Value* r = args++;
r->setName("r"); // upper bound
BasicBlock *pb = BasicBlock::Create(*context, "entry", f);
BasicBlock *blk = BasicBlock::Create(*context, "cond_true", f);
BasicBlock *nblk = BasicBlock::Create(*context, "cond_false", f);
IRBuilder<> Builder(pb);
// i < l
Value *lower = Builder.CreateICmpSLT(vt, l);
// i > r
Value *upper = Builder.CreateICmpSGT(vt, r);
// l - i || i - r (l > i or i > r)
Value *both = Builder.CreateOr(lower, upper);
// if (l > i || i > r), exit(0)
BranchInst *br = Builder.CreateCondBr(both, blk, nblk);
Builder.SetInsertPoint(ret);
// cond_true: call exit(0)
IRBuilder<> blkBuilder(blk);
Value *one = ConstantInt::get(Type::getInt64Ty(*context), 0, true);
Value *blkv = module->getOrInsertFunction("std::exit",
FunctionType::getVoidTy(*context),
Type::getInt64Ty(*context), NULL);
blkBuilder.CreateCall(blkv, one);
return f;
}
However, I get this error (I tried to fix with adding "CreateRetVoid"
and other solutions from StackOverflow but didn't work ):Basic Block in
function 'main' does not have terminator!
label %entry
LLVM ERROR: Broken function found, compilation aborted!
Could anyone help me to fix this? I appreciate any possible help.
BTW, how could I call standard library in instrumented code?
Thanks,
Yushan
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20181229/83b49a3f/attachment.html>
Jeremy Lakeman via llvm-dev
2018-Dec-29 03:34 UTC
[llvm-dev] [Instrumentation] Basic Block in function 'main' does not have terminator!
Blocks cond_true & cond_false with both need terminators eg unreachable & ret void. But note that the error is complaining about the entry block of main. On Sat, 29 Dec 2018 at 03:08, 张雨姗(ZHANG Yushan)-11310380 via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi all, > > I want to add a new function to a module, which works as this: > > void cmpr(int l, int r) { > if (i < l || i > r) > std::exit(0); > } > > Here is my code: > > // if required, instrument this function > BasicBlock * first = &func->getEntryBlock(); > IRBuilder<> Builder(first); > CallInst* v = Builder.CreateCall2(*getTargetInst(), vl, vr*); > > > Value * *getTargetInst()* { > > // insert a new function to program > Constant *c = module->getOrInsertFunction("cmpr", > FunctionType::getVoidTy(*context), > Type::getInt64Ty(*context), > Type::getInt64Ty(*context), NULL); > f = &cast<Function>(*c); > > // define arguments > Function::arg_iterator args = f->arg_begin(); > Value* l = args++; > l->setName("l"); // lower bound > Value* r = args++; > r->setName("r"); // upper bound > > BasicBlock *pb = BasicBlock::Create(*context, "entry", f); > BasicBlock *blk = BasicBlock::Create(*context, "cond_true", f); > BasicBlock *nblk = BasicBlock::Create(*context, "cond_false", f); > IRBuilder<> Builder(pb); > > // i < l > Value *lower = Builder.CreateICmpSLT(vt, l); > // i > r > Value *upper = Builder.CreateICmpSGT(vt, r); > // l - i || i - r (l > i or i > r) > Value *both = Builder.CreateOr(lower, upper); > // if (l > i || i > r), exit(0) > BranchInst *br = Builder.CreateCondBr(both, blk, nblk); > Builder.SetInsertPoint(ret); > > // cond_true: call exit(0) > IRBuilder<> blkBuilder(blk); > Value *one = ConstantInt::get(Type::getInt64Ty(*context), 0, true); > Value *blkv = module->getOrInsertFunction("std::exit", > FunctionType::getVoidTy(*context), > Type::getInt64Ty(*context), NULL); > > blkBuilder.CreateCall(blkv, one); > > return f; > } > > However, I get this error (I tried to fix with adding "CreateRetVoid" and other solutions from StackOverflow but didn't work ): > > Basic Block in function 'main' does not have terminator! > label %entry > LLVM ERROR: Broken function found, compilation aborted! > > > Could anyone help me to fix this? I appreciate any possible help. > BTW, how could I call standard library in instrumented code? > > Thanks, > Yushan > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20181229/4fee3b80/attachment.html>