[all-commits] [llvm/llvm-project] c8755b: [Coroutines] Optimize the lifespan of temporary co...

Xun Li via All-commits all-commits at lists.llvm.org
Sun Jun 28 10:18:34 PDT 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: c8755b6378c2a1f32d9a90bad6c56a1cc5a830c3
      https://github.com/llvm/llvm-project/commit/c8755b6378c2a1f32d9a90bad6c56a1cc5a830c3
  Author: Xun Li <xun at fb.com>
  Date:   2020-06-28 (Sun, 28 Jun 2020)

  Changed paths:
    M llvm/lib/Transforms/Coroutines/CoroSplit.cpp
    M llvm/test/Transforms/Coroutines/coro-split-02.ll
    A llvm/test/Transforms/Coroutines/coro-split-sink-lifetime.ll

  Log Message:
  -----------
  [Coroutines] Optimize the lifespan of temporary co_await object

Summary:
If we ever assign co_await to a temporary variable, such as foo(co_await expr),
we generate AST that looks like this: MaterializedTemporaryExpr(CoawaitExpr(...)).
MaterializedTemporaryExpr would emit an intrinsics that marks the lifetime start of the
temporary storage. However such temporary storage will not be used until co_await is ready
to write the result. Marking the lifetime start way too early causes extra storage to be
put in the coroutine frame instead of the stack.
As you can see from https://godbolt.org/z/zVx_eB, the frame generated for get_big_object2 is 12K, which contains a big_object object unnecessarily.
After this patch, the frame size for get_big_object2 is now only 8K. There are still room for improvements, in particular, GCC has a 4K frame for this function. But that's a separate problem and not addressed in this patch.

The basic idea of this patch is during CoroSplit, look for every local variable in the coroutine created through AllocaInst, identify all the lifetime start/end markers and the use of the variables, and sink the lifetime.start maker to the places as close to the first-ever use as possible.

Reviewers: lewissbaker, modocache, junparser

Reviewed By: junparser

Subscribers: hiraditya, llvm-commits, rsmith, ChuanqiXu, cfe-commits

Tags: #clang, #llvm

Differential Revision: https://reviews.llvm.org/D82314




More information about the All-commits mailing list