Verifier doesn't flag out-of-module constants, BC writing crashes
| Bugzilla Link | 8364 |
| Resolution | INVALID |
| Resolved on | May 01, 2020 02:13 |
| Version | 2.7 |
| OS | All |
| Reporter | LLVM Bugzilla Contributor |
| CC | @ekatz |
Extended Description
BitCode generation fails with "Unknown constant!" "UNREACHABLE executed at BitcodeWriter.cpp:895
Issue can be reproduced with the following: llvm::LLVMContext ctx; llvm::StringRef mn1 ("m1"); llvm::StringRef mn2 ("m2"); llvm::Module m1 (mn1, ctx); llvm::Module m2 (mn2, ctx); const llvm::Type * rt = llvm::Type::getVoidTy (ctx); const llvm::FunctionType * ft = llvm::FunctionType::get (rt, false); llvm::Function * f1 = llvm::Function::Create (ft, llvm::GlobalValue::ExternalLinkage); m1.getFunctionList ().push_back (f1); const llvm::IntegerType * it = llvm::IntegerType::get (ctx, 8); const llvm::PointerType * pt = llvm::PointerType::get (it, 0); const llvm::Constant * ce = llvm::ConstantExpr::getBitCast (f1, pt); llvm::Constant * v [1] = {(llvm::Constant *)ce}; const llvm::ArrayType * at = llvm::ArrayType::get (pt, 1); const llvm::Constant * a = llvm::ConstantArray::get (at, v, 1); const llvm::Type * gt = a->getRawType (); llvm::GlobalVariable * g = new llvm::GlobalVariable (gt, true, llvm::GlobalValue::ExternalLinkage, (llvm::Constant *)a); m2.getGlobalList ().push_back (g); std::string message; bool result = llvm::verifyModule (m2, llvm::ReturnStatusAction, &message);
The testcase is incorrect. It creates a function in module "m1", while referring it in module "m2", via a constant bitcast. The correct code should declare the same function as external in module "m2", and then reference it in the bitcast (keep the define of the function in "m1").
- Note that the function needs to be uniquely named for it to work.
Closing this bug, as this is not an issue.
I don't see how this can be considered resolved, since the verifier hasn't been beefed up as requested.
The best course of action for you to take is to create C code that's similar to what you want done and then generate a bitcode file from that. You can then disassemble that bitcode file to see the LLVM IR and then try to replicate that.
Does that make this example invalid? Whether I use get-or-insert for the functions or globals won't change that the validator doesn't flag a constant in one module referencing a function in a different module and that code generation aborts in an UNREACHABLE branch.
You probably want to use the Module::getOrInsertFunction and Module::getOrInsertGlobal methods instead of manually putting them into the Module's lists.