Skip to content

Commit 2d00c73

Browse files
authored
[Clang][CodeGen] Emit fake uses before musttail calls (#136867)
Fixes the issue reported following the merge of #118026. When a valid `musttail` call is made, the function it is made from must return immediately after the call; if there are any cleanups left in the function, then an error is triggered. This is not necessary for fake uses however - it is perfectly valid to simply emit the fake use "cleanup" code before the tail call, and indeed LLVM will automatically move any fake uses following a tail call to come before the tail call. Therefore, this patch specifically choose to handle fake use cleanups when a musttail call is present by simply emitting them immediately before the call.
1 parent b4e2592 commit 2d00c73

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6001,8 +6001,16 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
60016001
for (auto it = EHStack.find(CurrentCleanupScopeDepth); it != EHStack.end();
60026002
++it) {
60036003
EHCleanupScope *Cleanup = dyn_cast<EHCleanupScope>(&*it);
6004-
if (!(Cleanup && Cleanup->getCleanup()->isRedundantBeforeReturn()))
6004+
// Fake uses can be safely emitted immediately prior to the tail call, so
6005+
// we choose to emit them just before the call here.
6006+
if (Cleanup && Cleanup->isFakeUse()) {
6007+
CGBuilderTy::InsertPointGuard IPG(Builder);
6008+
Builder.SetInsertPoint(CI);
6009+
Cleanup->getCleanup()->Emit(*this, EHScopeStack::Cleanup::Flags());
6010+
} else if (!(Cleanup &&
6011+
Cleanup->getCleanup()->isRedundantBeforeReturn())) {
60056012
CGM.ErrorUnsupported(MustTailCall, "tail call skipping over cleanups");
6013+
}
60066014
}
60076015
if (CI->getType()->isVoidTy())
60086016
Builder.CreateRetVoid();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -fextend-variable-liveness -o - %s | FileCheck %s
2+
3+
/// Tests that when we have fake uses in a function ending in a musttail call,
4+
/// we emit the fake uses and their corresponding loads immediately prior to the
5+
/// tail call.
6+
7+
extern "C" char *bar(int *);
8+
9+
// CHECK-LABEL: define dso_local ptr @foo(
10+
// CHECK-SAME: ptr noundef [[E:%.*]])
11+
// CHECK-NEXT: [[ENTRY:.*:]]
12+
// CHECK-NEXT: [[E_ADDR:%.*]] = alloca ptr, align 8
13+
// CHECK-NEXT: store ptr [[E]], ptr [[E_ADDR]], align 8
14+
// CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr [[E_ADDR]], align 8
15+
// CHECK-NEXT: [[FAKE_USE:%.*]] = load ptr, ptr [[E_ADDR]]
16+
// CHECK-NEXT: notail call void (...) @llvm.fake.use(ptr [[FAKE_USE]])
17+
// CHECK-NEXT: [[CALL:%.*]] = musttail call ptr @bar(ptr noundef [[TMP0]])
18+
// CHECK-NEXT: ret ptr [[CALL]]
19+
20+
// CHECK: [[BB1:.*:]]
21+
// CHECK-NEXT: [[FAKE_USE1:%.*]] = load ptr, ptr [[E_ADDR]]
22+
// CHECK-NEXT: notail call void (...) @llvm.fake.use(ptr [[FAKE_USE1]])
23+
// CHECK-NEXT: ret ptr undef
24+
//
25+
extern "C" const char *foo(int *e) {
26+
[[clang::musttail]] return bar(e);
27+
}

0 commit comments

Comments
 (0)