Why does verifyFunction dislike this?

I am programmatically building some functions in intermediate representation, and trying to verify them, but the verifier always reports that there is a problem, and I can’t see why. Minimal test case:

#ifdef _MSC_VER
#pragma warning(disable : 4141)
#pragma warning(disable : 4530)
#pragma warning(disable : 4624)
#endif

#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Verifier.h>
using namespace llvm;

int main(int argc, char **argv) {
LLVMContext context;
IRBuilder<> builder(context);
Module module(“”, context);

// Function
auto rty = Type::getInt32Ty(context);
SmallVector<Type *, 1> pty;
auto ty = FunctionType::get(rty, pty, false);
auto f = Function::Create(ty, GlobalValue::CommonLinkage, “f”, module);

// Entry block
auto entry = BasicBlock::Create(context, “entry”, f);
builder.SetInsertPoint(entry);

// return 0
auto val = ConstantInt::getSigned(rty, 0);
builder.CreateRet(val);

// Check
f->dump();
outs() << verifyFunction(*f) << ‘\n’;
return 0;
}

Output:

define common i32 @f() {
entry:
ret i32 0
}

1

So the verifier says there is a problem, but I don’t see anywhere the problem could be. What am I missing?

Hi Russell.

Disclaimer: I haven’t created IR from scratch nor have I used IRBuilder or Verifier before.

There is a second parameter in verifyFunction which is the output stream it should write error messages to in case errors occur.

Just from looking at the output I see that f is not mangled, maybe that could be a problem?

Try Mangler::getNameWithPrefix(…) from “llvm/IR/Manger.h”

Regards,

Viktor

Ah, that led to a solution, thanks! Now the verifier reports the specific problem: functions may not have common linkage. When I change it to external linkage, the verifier is happy.