[PATCH] D87596: [Coroutines] Reuse storage for local variables with non-overlapping lifetimes

Chuanqi Xu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 13 23:09:49 PDT 2020


ChuanqiXu created this revision.
ChuanqiXu added reviewers: junparser, modocache, lxfind.
Herald added subscribers: llvm-commits, hiraditya, mgorny.
Herald added a project: LLVM.
ChuanqiXu requested review of this revision.

bug 45566 <https://bugs.llvm.org/show_bug.cgi?id=45566> shows the process of building coroutine frame won't consider that the lifetimes of different local variables are not overlapped, which means the compiler could generates smaller frame.

  struct big_structure {
      big_structure();
      char dummy[500];
  };
  cppcoro::task<void> alternative_paths(bool cond) {
     // 1072 byte coroutine frame
      if (cond) {
          big_structure a;
          process(a);
          co_await something();
      } else {
          big_structure b;
          process2(b);
          co_await something();
      }
  }
  void normal_function(bool cond) {
      // 512 byte stack frame
      if (cond) {
          big_structure a;
          process(a);
      } else {
          big_structure b;
          process2(b);
      }
  }

This patch calculate the lifetime range of each `alloca` by `StackLifetime` class. Then the patch build non-overlapped sets for `allocas` whose lifetime ranges are not overlapped.  We use the largest type in a non-overlapped set as  the field type in the frame. In `insertSpills` process, if we find the type of field is not the same with the alloca, we cast the pointer to the field type to the pointer to the alloca type. Since the lifetime range of alloca in one non-overlapped set is not overlapped with each other, it should be ok to reuse the storage space in the frame.

Test plan: check-llvm, check-clang, cppcoro, folly


https://reviews.llvm.org/D87596

Files:
  llvm/lib/Transforms/Coroutines/CMakeLists.txt
  llvm/lib/Transforms/Coroutines/CoroFrame.cpp
  llvm/test/Transforms/Coroutines/coro-frame-arrayalloca.ll
  llvm/test/Transforms/Coroutines/coro-frame-reuse-alloca-00.ll
  llvm/test/Transforms/Coroutines/coro-frame-reuse-alloca-01.ll
  llvm/test/Transforms/Coroutines/coro-frame-reuse-alloca-02.ll
  llvm/test/Transforms/Coroutines/coro-param-copy.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87596.291493.patch
Type: text/x-patch
Size: 23816 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200914/9b7f9922/attachment.bin>


More information about the llvm-commits mailing list