【发布时间】:2021-04-15 04:29:21
【问题描述】:
情况:我目前正在解析一种前端语言并在 LLVM IR 中生成函数定义。
我可以使用 LLVM12 C++ API 将函数定义编译为 WebAssembly 文件。
但是,生成的 wasm 代码不会“导出”任何已编译的函数,因此无法从加载 wasm 文件的 javascript 访问。
问题:有人可以告诉我我可能遗漏了什么吗?如何告诉 llvm 编译器为定义的函数创建导出。我尝试将函数可见性设置为 llvm::GlobalValue::DefaultVisibility。但这似乎没有帮助。
为函数生成的 IR(具有默认可见性)看起来像
define double @f(double %x) #0 {
entry:
%multmp = fmul double %x, 2.000000e+00
ret double %multmp
}
attributes #0 = { "target-features" }
将包含函数定义的模块编译到 Wasm 目标的函数如下所示:
llvm::Module *TheModule; // module containing the function definition
// function to compile to Wasm target
bool compile_file(){
const char *TargetTriple = "wasm-wasi";
// create a llvm::Target for the specified triple
std::string Error;
const llvm::Target *Target = llvm::TargetRegistry::lookupTarget(TargetTriple, Error);
if(!Target) {
llvm::errs() << Error;
return false;
}
// set the options and features for the target and create a TargetMachine instance
auto CPU = "generic";
auto Features = "";
llvm::TargetOptions opt;
auto RM = llvm::Optional<llvm::Reloc::Model>();
auto TheTargetMachine = Target->createTargetMachine(TargetTriple, CPU, Features, opt, RM);
TheModule->setDataLayout(TheTargetMachine->createDataLayout());
// create a output stream to write the compiled code to a .wasm file in the current directory
std::error_code EC;
llvm::raw_fd_ostream dest("output.wasm", EC, llvm::sys::fs::OF_None);
if(EC) {
llvm::errs() << "Could not open file: " << EC.message();
return false;
}
// set the visibility of all functions in the module to DefaultVisibility
auto &functionList = TheModule->getFunctionList();
for (auto &function : functionList) {
function.setVisibility(llvm::GlobalValue::DefaultVisibility);
}
// add a emit pass to write the generated code to the wasm file
llvm::legacy::PassManager pass;
if(TheTargetMachine->addPassesToEmitFile(pass,dest,nullptr,llvm::CGFT_ObjectFile)){
llvm::errs() << "TheTargetMachine can't emit a file of this type";
return false;
}
// run the pass on the module and flush the output stream to the file
pass.run(*(TheModule));
dest.flush();
// return true on success
return true;
这会输出一个类似的 wasm 文件
(module
(type $t0 (func (param f64) (result f64)))
(import "env" "__linear_memory" (memory $env.__linear_memory 0))
(import "env" "__indirect_function_table" (table $env.__indirect_function_table 0 funcref))
(func $f0 (type $t0) (param $p0 f64) (result f64)
local.get $p0
local.get $p0
f64.add))
但是,这个生成的文件有问题。 它没有添加“导出”语句以使函数 f0 对外界可见,这将允许加载 wasm 模块的 javascript 调用函数 f0。 理想情况下,生成的文件应该有类似的函数定义行
func $f0 (export "f") (type $t0) (param $p0 f64) (result f64)
local.get $p0
local.get $p0
f64.add))
这样,加载的 javascript 将可以访问名为“f”的函数,它可以从 wasm 调用。
有没有办法向 LLVM C++ API 指定函数应该被导出?
【问题讨论】:
-
要按照以下答案中的建议设置函数属性,可以添加如下属性: function.addFnAttr( llvm::Attribute::get(TheContext,"wasm-export-name", function.getName()) ) // TheContext 是定义模块的 llvm::Context
标签: c++ clang llvm webassembly llvm-c++-api