如何将生成的llvm :: Module的LLVM-IR代码存储到字符串中?

Rah*_*ahn 4 llvm

FibonacciLLVM 的示例使用打印出LLVM IR errs() << *theModule.

是否有任何函数能够将生成的LLVM IR存储到(矢量)字符串或任何其他变量而不是仅将其打印出来?(例如std::string llvm_IR = theModule->getIR())

我一直在搜索llvm :: Module Class Reference并且没有得到任何帮助.

部分Fibonacci.cpp:

// CreateFibFunction之前定义为生成fibonacci函数.

LLVMContext Context;

// Create some module to put our function into it.
std::unique_ptr<Module> Owner(new Module("test", Context));
Module *theModule = Owner.get();

// We are about to create the "fib" function:
Function *FibF = CreateFibFunction(M, Context);
errs() << "OK\n";
errs() << "We just constructed this LLVM module:\n\n---------\n";
errs() << *theModule;
errs() << "---------\nstarting fibonacci(" << n << ") with JIT...\n";
Run Code Online (Sandbox Code Playgroud)

Ism*_*awi 7

你可以用同样的方式做到 - 而不是使用errs(),这是一个raw_ostream,你可以使用a raw_string_ostream,像这样:

std::string Str;
raw_string_ostream OS(Str);
OS << *theModule;
OS.flush()
// Str now contains the module text
Run Code Online (Sandbox Code Playgroud)