Inconsistency between Interpreter and verifier when referencing a function in another module
Consider an execution engine having two modules:
; ModuleID = 'func1_module'
source_filename = "func1_module"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"
define i32 @func1() {
entry:
ret i32 42
}
and
; ModuleID = 'func2_module'
source_filename = "func2_module"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"
define i32 @func2() {
entry:
%0 = call i32 @func1()
ret i32 %0
}
declare i32 @func1()
When I try to execute @func1 with an interpreter engine, the following error happens:
LLVM ERROR: Tried to execute an unknown external function: func1.
When using a JIT engine, everything works as expected, and the execution returns 42.
A workaround I found is to use the Function object from the module func1_module when creating the call %0 = call i32 @func1(), but with this method, if I run verifyModule on func2_module, it fails with the following message:
Referencing function in another module!
%0 = call i32 @func1()
; ModuleID = 'func2_module'
ptr @func1
; ModuleID = 'func1_module'
Also note that if I run an optimizer on @func2, the call to @func1 will be deleted, probably because it is invalid IR, so I can't just ignore the verifier.
I've pasted a C++ file at the end of this comment that demonstrates the problem. Uncomment functions at the end of main to see the different results.
I tested on the following:
- Windows, with msys2's
mingw-w64-x86_64-llvmpackage (version 15.0.0) - Ubuntu 20.04 using WSL2, with LLVM-15 installed from the official apt repository
In summary:
- Using a JIT engine and a function declaration (
use_decl_with_jitfunction in the attached file): Everything works as expected - Using an interpreter engine and a function declaration (
use_decl_with_interpreter): Execution fails - Using a JIT engine and a function from another module (
no_decl_with_jit): Verification fails, execution succeeds - Using an interpreter engine and a function from another module (
no_decl_with_interpreter): Verification fails, execution succeeds
Code to reproduce the problem:
#include <memory>
#include <utility>
#include <string>
#include <llvm/ADT/Triple.h>
#include <llvm/Support/Host.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/MC/TargetRegistry.h>
#include <llvm/Target/TargetOptions.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Type.h>
#include <llvm/IR/Verifier.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/GenericValue.h>
using ModulePtr = std::unique_ptr<llvm::Module>;
static ModulePtr create_module(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple,
std::string const &name
)
{
auto result = std::make_unique<llvm::Module>(name, context);
result->setDataLayout(data_layout);
result->setTargetTriple(target_triple);
return result;
}
static std::pair<ModulePtr, llvm::Function *> make_func1(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple
)
{
auto module = create_module(context, data_layout, target_triple, "func1_module");
auto const i32_type = llvm::Type::getInt32Ty(context);
auto const fn_type = llvm::FunctionType::get(i32_type, false);
auto const func1 = llvm::Function::Create(
fn_type,
llvm::Function::ExternalLinkage,
"func1",
*module
);
auto const entry_block = llvm::BasicBlock::Create(context, "entry", func1);
llvm::IRBuilder<> builder(context);
builder.SetInsertPoint(entry_block);
builder.CreateRet(llvm::ConstantInt::get(i32_type, 42));
llvm::dbgs() << "==== Verifying " << module->getName() << " ====\n";
llvm::verifyModule(*module, &llvm::dbgs());
return { std::move(module), func1 };
}
static std::pair<ModulePtr, llvm::Function *> make_func2(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple,
llvm::Function *func1,
bool use_decl
)
{
auto module = create_module(context, data_layout, target_triple, "func2_module");
auto const i32_type = llvm::Type::getInt32Ty(context);
auto const fn_type = llvm::FunctionType::get(i32_type, false);
auto const func2 = llvm::Function::Create(
fn_type,
llvm::Function::ExternalLinkage,
"func2",
*module
);
auto const entry_block = llvm::BasicBlock::Create(context, "entry", func2);
llvm::IRBuilder<> builder(context);
builder.SetInsertPoint(entry_block);
if (use_decl)
{
auto const func1_decl = llvm::Function::Create(
func1->getFunctionType(),
llvm::Function::ExternalLinkage,
func1->getName(),
*module
);
auto const result = builder.CreateCall(func1_decl);
builder.CreateRet(result);
}
else
{
auto const result = builder.CreateCall(func1);
builder.CreateRet(result);
}
llvm::dbgs() << "==== Verifying " << module->getName() << " ====\n";
llvm::verifyModule(*module, &llvm::dbgs());
return { std::move(module), func2 };
}
static void use_decl_with_jit(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple
)
{
auto func1_pair = make_func1(context, data_layout, target_triple);
auto &&func1_module = func1_pair.first;
auto &&func1 = func1_pair.second;
auto func2_pair = make_func2(context, data_layout, target_triple, func1, true);
auto &&func2_module = func2_pair.first;
auto &&func2 = func2_pair.second;
llvm::dbgs() << "========\n";
func1_module->print(llvm::dbgs(), nullptr);
llvm::dbgs() << "========\n";
func2_module->print(llvm::dbgs(), nullptr);
llvm::EngineBuilder builder(std::move(func1_module));
builder
.setEngineKind(llvm::EngineKind::JIT)
.setOptLevel(llvm::CodeGenOpt::None);
auto engine = std::unique_ptr<llvm::ExecutionEngine>(builder.create());
engine->addModule(std::move(func2_module));
auto func2_result = engine->runFunction(func2, {});
llvm::dbgs() << "========\n";
llvm::dbgs() << "Result: " << func2_result.IntVal << '\n';
}
static void use_decl_with_interpreter(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple
)
{
auto func1_pair = make_func1(context, data_layout, target_triple);
auto &&func1_module = func1_pair.first;
auto &&func1 = func1_pair.second;
auto func2_pair = make_func2(context, data_layout, target_triple, func1, true);
auto &&func2_module = func2_pair.first;
auto &&func2 = func2_pair.second;
llvm::dbgs() << "========\n";
func1_module->print(llvm::dbgs(), nullptr);
llvm::dbgs() << "========\n";
func2_module->print(llvm::dbgs(), nullptr);
llvm::EngineBuilder builder(std::move(func1_module));
builder
.setEngineKind(llvm::EngineKind::Interpreter)
.setOptLevel(llvm::CodeGenOpt::None);
auto engine = std::unique_ptr<llvm::ExecutionEngine>(builder.create());
engine->addModule(std::move(func2_module));
auto func2_result = engine->runFunction(func2, {});
llvm::dbgs() << "========\n";
llvm::dbgs() << "Result: " << func2_result.IntVal << '\n';
}
static void no_decl_with_jit(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple
)
{
auto func1_pair = make_func1(context, data_layout, target_triple);
auto &&func1_module = func1_pair.first;
auto &&func1 = func1_pair.second;
auto func2_pair = make_func2(context, data_layout, target_triple, func1, true);
auto &&func2_module = func2_pair.first;
auto &&func2 = func2_pair.second;
llvm::dbgs() << "========\n";
func1_module->print(llvm::dbgs(), nullptr);
llvm::dbgs() << "========\n";
func2_module->print(llvm::dbgs(), nullptr);
llvm::EngineBuilder builder(std::move(func1_module));
builder
.setEngineKind(llvm::EngineKind::JIT)
.setOptLevel(llvm::CodeGenOpt::None);
auto engine = std::unique_ptr<llvm::ExecutionEngine>(builder.create());
engine->addModule(std::move(func2_module));
auto func2_result = engine->runFunction(func2, {});
llvm::dbgs() << "========\n";
llvm::dbgs() << "Result: " << func2_result.IntVal << '\n';
}
static void no_decl_with_interpreter(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple
)
{
auto func1_pair = make_func1(context, data_layout, target_triple);
auto &&func1_module = func1_pair.first;
auto &&func1 = func1_pair.second;
auto func2_pair = make_func2(context, data_layout, target_triple, func1, true);
auto &&func2_module = func2_pair.first;
auto &&func2 = func2_pair.second;
llvm::dbgs() << "========\n";
func1_module->print(llvm::dbgs(), nullptr);
llvm::dbgs() << "========\n";
func2_module->print(llvm::dbgs(), nullptr);
llvm::EngineBuilder builder(std::move(func1_module));
builder
.setEngineKind(llvm::EngineKind::Interpreter)
.setOptLevel(llvm::CodeGenOpt::None);
auto engine = std::unique_ptr<llvm::ExecutionEngine>(builder.create());
engine->addModule(std::move(func2_module));
auto func2_result = engine->runFunction(func2, {});
llvm::dbgs() << "========\n";
llvm::dbgs() << "Result: " << func2_result.IntVal << '\n';
}
int main(void)
{
llvm::LLVMContext context;
llvm::InitializeAllDisassemblers();
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmParsers();
llvm::InitializeAllAsmPrinters();
auto const target_triple = llvm::sys::getDefaultTargetTriple();
std::string target_error = "";
auto const target = llvm::TargetRegistry::lookupTarget(target_triple, target_error);
assert(target);
auto const target_machine = std::unique_ptr<llvm::TargetMachine>(
target->createTargetMachine(
target_triple,
"generic",
"",
llvm::TargetOptions(),
llvm::Reloc::Model::PIC_
)
);
assert(target_machine);
auto const data_layout = target_machine->createDataLayout();
// works as expected
// use_decl_with_jit(context, data_layout, target_triple);
// error: "LLVM ERROR: Tried to execute an unknown external function: func1"
use_decl_with_interpreter(context, data_layout, target_triple);
// works, but verfiyFunction fails for func2_module
// no_decl_with_jit(context, data_layout, target_triple);
// works, but verfiyFunction fails for func2_module
// no_decl_with_interpreter(context, data_layout, target_triple);
}
@llvm/issue-subscribers-orcjit
define i32 @func1() { entry: ret i32 42 }
and
```llvm
; ModuleID = 'func2_module'
source_filename = "func2_module"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"
define i32 @<!-- -->func2() {
entry:
%0 = call i32 @<!-- -->func1()
ret i32 %0
}
declare i32 @<!-- -->func1()
When I try to execute @<!-- -->func1 with an interpreter engine, the following error happens:
LLVM ERROR: Tried to execute an unknown external function: func1.
When using a JIT engine, everything works as expected, and the execution returns 42.
A workaround I found is to use the Function object from the module func1_module when creating the call %0 = call i32 @<!-- -->func1(), but with this method, if I run verifyModule on func2_module, it fails with the following message:
Referencing function in another module!
%0 = call i32 @<!-- -->func1()
; ModuleID = 'func2_module'
ptr @<!-- -->func1
; ModuleID = 'func1_module'
Also note that if I run an optimizer on @<!-- -->func2, the call to @<!-- -->func1 will be deleted, probably because it is invalid IR, so I can't just ignore the verifier.
I've pasted a C++ file at the end of this comment that demonstrates the problem. Uncomment functions at the end of main to see the different results.
I tested on the following:
- Windows, with msys2's
mingw-w64-x86_64-llvmpackage (version 15.0.0) - Ubuntu 20.04 using WSL2, with LLVM-15 installed from the official apt repository
In summary:
- Using a JIT engine and a function declaration (
use_decl_with_jitfunction in the attached file): Everything works as expected - Using an interpreter engine and a function declaration (
use_decl_with_interpreter): Execution fails - Using a JIT engine and a function from another module (
no_decl_with_jit): Verification fails, execution succeeds - Using an interpreter engine and a function from another module (
no_decl_with_interpreter): Verification fails, execution succeeds
<details> <summary>Code to reproduce the problem:</summary>
#include <memory>
#include <utility>
#include <string>
#include <llvm/ADT/Triple.h>
#include <llvm/Support/Host.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/MC/TargetRegistry.h>
#include <llvm/Target/TargetOptions.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Type.h>
#include <llvm/IR/Verifier.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/GenericValue.h>
using ModulePtr = std::unique_ptr<llvm::Module>;
static ModulePtr create_module(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple,
std::string const &name
)
{
auto result = std::make_unique<llvm::Module>(name, context);
result->setDataLayout(data_layout);
result->setTargetTriple(target_triple);
return result;
}
static std::pair<ModulePtr, llvm::Function *> make_func1(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple
)
{
auto module = create_module(context, data_layout, target_triple, "func1_module");
auto const i32_type = llvm::Type::getInt32Ty(context);
auto const fn_type = llvm::FunctionType::get(i32_type, false);
auto const func1 = llvm::Function::Create(
fn_type,
llvm::Function::ExternalLinkage,
"func1",
*module
);
auto const entry_block = llvm::BasicBlock::Create(context, "entry", func1);
llvm::IRBuilder<> builder(context);
builder.SetInsertPoint(entry_block);
builder.CreateRet(llvm::ConstantInt::get(i32_type, 42));
llvm::dbgs() << "==== Verifying " << module->getName() << " ====\n";
llvm::verifyModule(*module, &llvm::dbgs());
return { std::move(module), func1 };
}
static std::pair<ModulePtr, llvm::Function *> make_func2(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple,
llvm::Function *func1,
bool use_decl
)
{
auto module = create_module(context, data_layout, target_triple, "func2_module");
auto const i32_type = llvm::Type::getInt32Ty(context);
auto const fn_type = llvm::FunctionType::get(i32_type, false);
auto const func2 = llvm::Function::Create(
fn_type,
llvm::Function::ExternalLinkage,
"func2",
*module
);
auto const entry_block = llvm::BasicBlock::Create(context, "entry", func2);
llvm::IRBuilder<> builder(context);
builder.SetInsertPoint(entry_block);
if (use_decl)
{
auto const func1_decl = llvm::Function::Create(
func1->getFunctionType(),
llvm::Function::ExternalLinkage,
func1->getName(),
*module
);
auto const result = builder.CreateCall(func1_decl);
builder.CreateRet(result);
}
else
{
auto const result = builder.CreateCall(func1);
builder.CreateRet(result);
}
llvm::dbgs() << "==== Verifying " << module->getName() << " ====\n";
llvm::verifyModule(*module, &llvm::dbgs());
return { std::move(module), func2 };
}
static void use_decl_with_jit(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple
)
{
auto func1_pair = make_func1(context, data_layout, target_triple);
auto &&func1_module = func1_pair.first;
auto &&func1 = func1_pair.second;
auto func2_pair = make_func2(context, data_layout, target_triple, func1, true);
auto &&func2_module = func2_pair.first;
auto &&func2 = func2_pair.second;
llvm::dbgs() << "========\n";
func1_module->print(llvm::dbgs(), nullptr);
llvm::dbgs() << "========\n";
func2_module->print(llvm::dbgs(), nullptr);
llvm::EngineBuilder builder(std::move(func1_module));
builder
.setEngineKind(llvm::EngineKind::JIT)
.setOptLevel(llvm::CodeGenOpt::None);
auto engine = std::unique_ptr<llvm::ExecutionEngine>(builder.create());
engine->addModule(std::move(func2_module));
auto func2_result = engine->runFunction(func2, {});
llvm::dbgs() << "========\n";
llvm::dbgs() << "Result: " << func2_result.IntVal << '\n';
}
static void use_decl_with_interpreter(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple
)
{
auto func1_pair = make_func1(context, data_layout, target_triple);
auto &&func1_module = func1_pair.first;
auto &&func1 = func1_pair.second;
auto func2_pair = make_func2(context, data_layout, target_triple, func1, true);
auto &&func2_module = func2_pair.first;
auto &&func2 = func2_pair.second;
llvm::dbgs() << "========\n";
func1_module->print(llvm::dbgs(), nullptr);
llvm::dbgs() << "========\n";
func2_module->print(llvm::dbgs(), nullptr);
llvm::EngineBuilder builder(std::move(func1_module));
builder
.setEngineKind(llvm::EngineKind::Interpreter)
.setOptLevel(llvm::CodeGenOpt::None);
auto engine = std::unique_ptr<llvm::ExecutionEngine>(builder.create());
engine->addModule(std::move(func2_module));
auto func2_result = engine->runFunction(func2, {});
llvm::dbgs() << "========\n";
llvm::dbgs() << "Result: " << func2_result.IntVal << '\n';
}
static void no_decl_with_jit(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple
)
{
auto func1_pair = make_func1(context, data_layout, target_triple);
auto &&func1_module = func1_pair.first;
auto &&func1 = func1_pair.second;
auto func2_pair = make_func2(context, data_layout, target_triple, func1, true);
auto &&func2_module = func2_pair.first;
auto &&func2 = func2_pair.second;
llvm::dbgs() << "========\n";
func1_module->print(llvm::dbgs(), nullptr);
llvm::dbgs() << "========\n";
func2_module->print(llvm::dbgs(), nullptr);
llvm::EngineBuilder builder(std::move(func1_module));
builder
.setEngineKind(llvm::EngineKind::JIT)
.setOptLevel(llvm::CodeGenOpt::None);
auto engine = std::unique_ptr<llvm::ExecutionEngine>(builder.create());
engine->addModule(std::move(func2_module));
auto func2_result = engine->runFunction(func2, {});
llvm::dbgs() << "========\n";
llvm::dbgs() << "Result: " << func2_result.IntVal << '\n';
}
static void no_decl_with_interpreter(
llvm::LLVMContext &context,
llvm::DataLayout const &data_layout,
std::string const &target_triple
)
{
auto func1_pair = make_func1(context, data_layout, target_triple);
auto &&func1_module = func1_pair.first;
auto &&func1 = func1_pair.second;
auto func2_pair = make_func2(context, data_layout, target_triple, func1, true);
auto &&func2_module = func2_pair.first;
auto &&func2 = func2_pair.second;
llvm::dbgs() << "========\n";
func1_module->print(llvm::dbgs(), nullptr);
llvm::dbgs() << "========\n";
func2_module->print(llvm::dbgs(), nullptr);
llvm::EngineBuilder builder(std::move(func1_module));
builder
.setEngineKind(llvm::EngineKind::Interpreter)
.setOptLevel(llvm::CodeGenOpt::None);
auto engine = std::unique_ptr<llvm::ExecutionEngine>(builder.create());
engine->addModule(std::move(func2_module));
auto func2_result = engine->runFunction(func2, {});
llvm::dbgs() << "========\n";
llvm::dbgs() << "Result: " << func2_result.IntVal << '\n';
}
int main(void)
{
llvm::LLVMContext context;
llvm::InitializeAllDisassemblers();
llvm::InitializeAllTargetInfos();
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmParsers();
llvm::InitializeAllAsmPrinters();
auto const target_triple = llvm::sys::getDefaultTargetTriple();
std::string target_error = "";
auto const target = llvm::TargetRegistry::lookupTarget(target_triple, target_error);
assert(target);
auto const target_machine = std::unique_ptr<llvm::TargetMachine>(
target->createTargetMachine(
target_triple,
"generic",
"",
llvm::TargetOptions(),
llvm::Reloc::Model::PIC_
)
);
assert(target_machine);
auto const data_layout = target_machine->createDataLayout();
// works as expected
// use_decl_with_jit(context, data_layout, target_triple);
// error: "LLVM ERROR: Tried to execute an unknown external function: func1"
use_decl_with_interpreter(context, data_layout, target_triple);
// works, but verfiyFunction fails for func2_module
// no_decl_with_jit(context, data_layout, target_triple);
// works, but verfiyFunction fails for func2_module
// no_decl_with_interpreter(context, data_layout, target_triple);
}
</details>
@Il-Capitano The short version is that the interpreter is not well supported. It doesn't handle general cross-module calls because it doesn't know how to handle ABI in general.
Our usual advice is to stick to using the JIT APIs rather than trying to use the interpreter.