Skip to content

[Coroutines] Respect noinline attributes when eliding heap allocation #115384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/Coroutines/CoroSplit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2088,9 +2088,9 @@ static void doSplitCoroutine(Function &F, SmallVectorImpl<Function *> &Clones,

bool isNoSuspendCoroutine = Shape.CoroSuspends.empty();

bool shouldCreateNoAllocVariant = !isNoSuspendCoroutine &&
Shape.ABI == coro::ABI::Switch &&
hasSafeElideCaller(F);
bool shouldCreateNoAllocVariant =
!isNoSuspendCoroutine && Shape.ABI == coro::ABI::Switch &&
hasSafeElideCaller(F) && !F.hasFnAttribute(llvm::Attribute::NoInline);

// If there are no suspend points, no split required, just remove
// the allocation and deallocation blocks, they are not needed.
Expand Down
62 changes: 62 additions & 0 deletions llvm/test/Transforms/Coroutines/coro-split-noinline.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
; Tests that coro-split does not generate noinline variant for noinline functions
; RUN: opt < %s -passes='cgscc(coro-split),simplifycfg,early-cse' -S | FileCheck %s

; CHECK-LABEL: @cannotinline()
define ptr @cannotinline() presplitcoroutine noinline {
entry:
%id = call token @llvm.coro.id(i32 0, ptr null, ptr null, ptr null)
%need.alloc = call i1 @llvm.coro.alloc(token %id)
br i1 %need.alloc, label %dyn.alloc, label %begin

dyn.alloc:
%size = call i32 @llvm.coro.size.i32()
%alloc = call ptr @malloc(i32 %size)
br label %begin

begin:
%phi = phi ptr [ null, %entry ], [ %alloc, %dyn.alloc ]
%hdl = call ptr @llvm.coro.begin(token %id, ptr %phi)
call void @print(i32 0)
%0 = call i8 @llvm.coro.suspend(token none, i1 false)
switch i8 %0, label %suspend [i8 0, label %resume
i8 1, label %cleanup]
resume:
call void @print(i32 1)
br label %cleanup

cleanup:
%mem = call ptr @llvm.coro.free(token %id, ptr %hdl)
call void @free(ptr %mem)
br label %suspend
suspend:
call i1 @llvm.coro.end(ptr %hdl, i1 0, token none)
ret ptr %hdl
}

; CHECK-NOT-LABEL: @cannotinline.noalloc()


; Make a safe_elide call to cannotinline
define void @caller() presplitcoroutine {
entry:
%ptr = call ptr @cannotinline() #1
ret void
}


declare ptr @llvm.coro.free(token, ptr)
declare i32 @llvm.coro.size.i32()
declare i8 @llvm.coro.suspend(token, i1)
declare void @llvm.coro.resume(ptr)
declare void @llvm.coro.destroy(ptr)

declare token @llvm.coro.id(i32, ptr, ptr, ptr)
declare i1 @llvm.coro.alloc(token)
declare ptr @llvm.coro.begin(token, ptr)
declare i1 @llvm.coro.end(ptr, i1, token)

declare noalias ptr @malloc(i32) allockind("alloc,uninitialized") "alloc-family"="malloc"
declare void @print(i32)
declare void @free(ptr) willreturn allockind("free") "alloc-family"="malloc"

attributes #1 = { coro_elide_safe }
Loading