[llvm-dev] Does llvm has IR level implementation of vector and map?

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Wed Oct 16 10:12:39 PDT 2019


Hi,

On Wed, 16 Oct 2019 at 01:38, Yafei Liu via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> My question is I don't want to implement things like vector or map in my compiler code, so this any implemented classes in llvm API which can help me to do this?

LLVM doesn't have those kinds of types natively. The easiest way to
get them is to write C wrapper functions that do the operations you
want and insert calls to those functions with your compiler. The C
wrapper functions could even be C++ declared with 'extern "C"' and
directly use std::vector as the underlying implementation.

For example one function might be:

extern "C" vectorPushBack(void *Vec, void *Elt) {
  std::vector<void *> *RealVec = static_cast<std::vector<void *>>(Vec);
  RealVec.push_back(Elt);
}

Then you'd insert a call to @vectorPushBack in the IR you generate and
link against this library of helper functions.

> Or, I know how to change the code above to C++, is there any way I can turn the paring into calling C++ libraries?

Calling C++ functions directly is quite hard because there are so many
different ABIs out in the world. But in principle it can be done. I
believe Swift does it, and uses Clang itself to get the correct ABI.
Using 'extern "C"' above sidesteps quite a few of those issues.

Cheers.

Tim.


More information about the llvm-dev mailing list