[llvm-dev] [frontend-dev][beginner] Allocation of structures

Dimitri Racordon via llvm-dev llvm-dev at lists.llvm.org
Mon May 29 18:14:58 PDT 2017


Hi all,

I’m pretty new to the list, and to LLVM in general, so please excuse my extreme newbiesness.

I’m trying to figure out what would be the appropriate way to implement move semantics.
I’ve been trying to dump the IR produced by clang with some basic C++ snippet, but I’m afraid it didn’t help me much.

Here’s the example I’ve been playing with (in C++):

struct S {
  S() noexcept: x(new int) {}
  S(S&& other) {
    x = other.x
    other.x = nullptr;
  }
  ~S() {
    delete x;
  }
};

S f1() {
  auto s = S();
  return s;
}

S f2() {
  auto s = S();
  return std::move(s);
}

This of course produces a lot of LLVM code (with -O0), but I think I may have figured out most of what’s what. In particular, I’ve been able to identify the IR code for `f1` and `f2`, but to my surprise, neither of those return a value. Both take a pointer to `S` as parameter, which in turn gets passed to the constructor of `S`, and return void.

This leaves me with two main questions:

  *   First, is the use of a pointer to S as parameter a specificity of clang, or generally the way to go? I’ve seen in the language reference that one could return a struct with a simple ret instruction, so I’m surprised not to see it for the version that doesn’t use move semantics.
  *   Second, would I use a non-void ret instruction to return the result of an alloca, when would the latter be destroyed? Would that involve a copy from the runtime stack of the callee to that of the caller?

Thank you very much for your time and your answer,

Best,


Dimitri Racordon
CUI, Université de Genève
7, route de Drize, CH-1227 Carouge - Switzerland
Phone: +41 22 379 01 24




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170530/22b4026c/attachment.html>


More information about the llvm-dev mailing list