Skip to content

Commit 437a4b7

Browse files
author
git apple-llvm automerger
committed
Merge commit 'ef3526facfc0' from apple/stable/20200714 into swift/main
2 parents 9ea5f19 + ef3526f commit 437a4b7

File tree

11 files changed

+215
-50
lines changed

11 files changed

+215
-50
lines changed

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,21 +2098,42 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
20982098

20992099
switch (GetCXXStdlibType(DriverArgs)) {
21002100
case ToolChain::CST_Libcxx: {
2101-
// On Darwin, libc++ is installed alongside the compiler in
2102-
// include/c++/v1, so get from '<install>/bin' to '<install>/include/c++/v1'.
2103-
{
2104-
llvm::SmallString<128> P = llvm::StringRef(getDriver().getInstalledDir());
2105-
// Note that P can be relative, so we have to '..' and not parent_path.
2106-
llvm::sys::path::append(P, "..", "include", "c++", "v1");
2107-
addSystemInclude(DriverArgs, CC1Args, P);
2101+
// On Darwin, libc++ can be installed in one of the following two places:
2102+
// 1. Alongside the compiler in <install>/include/c++/v1
2103+
// 2. In a SDK (or a custom sysroot) in <sysroot>/usr/include/c++/v1
2104+
//
2105+
// The precendence of paths is as listed above, i.e. we take the first path
2106+
// that exists. Also note that we never include libc++ twice -- we take the
2107+
// first path that exists and don't send the other paths to CC1 (otherwise
2108+
// include_next could break).
2109+
2110+
// Check for (1)
2111+
// Get from '<install>/bin' to '<install>/include/c++/v1'.
2112+
// Note that InstallBin can be relative, so we use '..' instead of
2113+
// parent_path.
2114+
llvm::SmallString<128> InstallBin =
2115+
llvm::StringRef(getDriver().getInstalledDir()); // <install>/bin
2116+
llvm::sys::path::append(InstallBin, "..", "include", "c++", "v1");
2117+
if (getVFS().exists(InstallBin)) {
2118+
addSystemInclude(DriverArgs, CC1Args, InstallBin);
2119+
return;
2120+
} else if (DriverArgs.hasArg(options::OPT_v)) {
2121+
llvm::errs() << "ignoring nonexistent directory \"" << InstallBin
2122+
<< "\"\n";
21082123
}
2109-
// Also add <sysroot>/usr/include/c++/v1 unless -nostdinc is used,
2110-
// to match the legacy behavior in CC1.
2111-
if (!DriverArgs.hasArg(options::OPT_nostdinc)) {
2112-
llvm::SmallString<128> P = Sysroot;
2113-
llvm::sys::path::append(P, "usr", "include", "c++", "v1");
2114-
addSystemInclude(DriverArgs, CC1Args, P);
2124+
2125+
// Otherwise, check for (2)
2126+
llvm::SmallString<128> SysrootUsr = Sysroot;
2127+
llvm::sys::path::append(SysrootUsr, "usr", "include", "c++", "v1");
2128+
if (getVFS().exists(SysrootUsr)) {
2129+
addSystemInclude(DriverArgs, CC1Args, SysrootUsr);
2130+
return;
2131+
} else if (DriverArgs.hasArg(options::OPT_v)) {
2132+
llvm::errs() << "ignoring nonexistent directory \"" << SysrootUsr
2133+
<< "\"\n";
21152134
}
2135+
2136+
// Otherwise, don't add any path.
21162137
break;
21172138
}
21182139

clang/test/Driver/Inputs/basic_darwin_sdk_usr_cxx_v1/usr/include/c++/v1/.keep

Whitespace-only changes.

clang/test/Driver/Inputs/basic_darwin_sdk_usr_cxx_v1/usr/lib/.keep

Whitespace-only changes.

clang/test/Driver/darwin-header-search-libcxx.cpp

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,57 @@
1313
// RUN: | FileCheck --check-prefix=CHECK-LIBCXX-NONE %s
1414
// CHECK-LIBCXX-NONE: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
1515

16-
// Check with only headers alongside the installation (those should be used,
17-
// but we should still add /usr/include/c++/v1 after to preserve legacy).
16+
// Check with only headers alongside the installation (those should be used).
1817
//
1918
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
2019
// RUN: -target x86_64-apple-darwin \
2120
// RUN: -stdlib=libc++ \
2221
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
2322
// RUN: --sysroot="" \
24-
// RUN: | FileCheck -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain --check-prefix=CHECK-LIBCXX-TOOLCHAIN-1 %s
23+
// RUN: | FileCheck -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
24+
// RUN: --check-prefix=CHECK-LIBCXX-TOOLCHAIN-1 %s
2525
// CHECK-LIBCXX-TOOLCHAIN-1: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
2626
// CHECK-LIBCXX-TOOLCHAIN-1: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
27-
// CHECK-LIBCXX-TOOLCHAIN-1: "-internal-isystem" "/usr/include/c++/v1"
27+
// CHECK-LIBCXX-TOOLCHAIN-1-NOT: "-internal-isystem" "/usr/include/c++/v1"
2828
//
2929
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
3030
// RUN: -target x86_64-apple-darwin \
3131
// RUN: -stdlib=libc++ \
3232
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
3333
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_no_libcxx \
34-
// RUN: | FileCheck -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain --check-prefix=CHECK-LIBCXX-TOOLCHAIN-2 %s
34+
// RUN: | FileCheck -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
35+
// RUN: -DSYSROOT=%S/Inputs/basic_darwin_sdk_no_libcxx \
36+
// RUN: --check-prefix=CHECK-LIBCXX-TOOLCHAIN-2 %s
3537
// CHECK-LIBCXX-TOOLCHAIN-2: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
3638
// CHECK-LIBCXX-TOOLCHAIN-2: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
39+
// CHECK-LIBCXX-TOOLCHAIN-2-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
40+
41+
// Check with only headers in the sysroot (those should be used).
42+
//
43+
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
44+
// RUN: -target x86_64-apple-darwin \
45+
// RUN: -stdlib=libc++ \
46+
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain_no_libcxx/usr/bin \
47+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
48+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
49+
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain_no_libcxx \
50+
// RUN: --check-prefix=CHECK-LIBCXX-SYSROOT-1 %s
51+
// CHECK-LIBCXX-SYSROOT-1: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
52+
// CHECK-LIBCXX-SYSROOT-1: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
53+
// CHECK-LIBCXX-SYSROOT-1-NOT: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
3754

3855
// Check with both headers in the sysroot and headers alongside the installation
39-
// (the headers in <sysroot> should be added after the toolchain headers).
40-
// Ensure that both -isysroot and --sysroot work, and that isysroot has precedence.
56+
// (the headers in the toolchain should be preferred over the <sysroot> headers).
57+
// Ensure that both -isysroot and --sysroot work, and that isysroot has precedence
58+
// over --sysroot.
4159
//
4260
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
4361
// RUN: -target x86_64-apple-darwin \
4462
// RUN: -stdlib=libc++ \
4563
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
4664
// RUN: -resource-dir=%S/Inputs/resource_dir \
47-
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr \
48-
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
65+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
66+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
4967
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
5068
// RUN: --check-prefix=CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1 %s
5169
//
@@ -54,8 +72,8 @@
5472
// RUN: -stdlib=libc++ \
5573
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
5674
// RUN: -resource-dir=%S/Inputs/resource_dir \
57-
// RUN: --sysroot %S/Inputs/basic_darwin_sdk_usr \
58-
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
75+
// RUN: --sysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
76+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
5977
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
6078
// RUN: --check-prefix=CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1 %s
6179
//
@@ -64,32 +82,46 @@
6482
// RUN: -stdlib=libc++ \
6583
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
6684
// RUN: -resource-dir=%S/Inputs/resource_dir \
67-
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr \
85+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
6886
// RUN: --sysroot %S/Inputs/basic_darwin_sdk_no_libcxx \
69-
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
87+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
7088
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
7189
// RUN: --check-prefix=CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1 %s
7290
//
7391
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
7492
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
75-
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
93+
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
7694

77-
// Make sure that using -nostdinc will drop the sysroot C++ library include
78-
// path, but not the toolchain one.
95+
// Make sure that using -nostdinc does not drop any C++ library include path.
96+
// This behavior is strange, but it is compatible with the legacy CC1 behavior.
7997
//
8098
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
8199
// RUN: -target x86_64-apple-darwin16 \
82100
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
83101
// RUN: -resource-dir=%S/Inputs/resource_dir \
84-
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr \
102+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
85103
// RUN: -stdlib=platform \
86104
// RUN: -nostdinc \
87-
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
105+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
88106
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
89-
// RUN: --check-prefix=CHECK-LIBCXX-NOSTDINC %s
90-
// CHECK-LIBCXX-NOSTDINC: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
91-
// CHECK-LIBCXX-NOSTDINC: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
92-
// CHECK-LIBCXX-NOSTDINC-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
107+
// RUN: --check-prefix=CHECK-LIBCXX-NOSTDINC-1 %s
108+
// CHECK-LIBCXX-NOSTDINC-1: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
109+
// CHECK-LIBCXX-NOSTDINC-1-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
110+
// CHECK-LIBCXX-NOSTDINC-1: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
111+
//
112+
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
113+
// RUN: -target x86_64-apple-darwin16 \
114+
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
115+
// RUN: -resource-dir=%S/Inputs/resource_dir \
116+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_no_libcxx \
117+
// RUN: -stdlib=platform \
118+
// RUN: -nostdinc \
119+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_no_libcxx \
120+
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
121+
// RUN: --check-prefix=CHECK-LIBCXX-NOSTDINC-2 %s
122+
// CHECK-LIBCXX-NOSTDINC-2: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
123+
// CHECK-LIBCXX-NOSTDINC-2: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
124+
// CHECK-LIBCXX-NOSTDINC-2-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
93125

94126
// Make sure that using -nostdinc++ or -nostdlib will drop both the toolchain
95127
// C++ include path and the sysroot one.
@@ -98,7 +130,7 @@
98130
// RUN: -target x86_64-apple-darwin16 \
99131
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
100132
// RUN: -resource-dir=%S/Inputs/resource_dir \
101-
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr \
133+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_cxx_v1 \
102134
// RUN: -stdlib=platform \
103135
// RUN: -nostdinc++ \
104136
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
@@ -121,3 +153,26 @@
121153
// CHECK-LIBCXX-NOSTDLIBINC: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
122154
// CHECK-LIBCXX-NOSTDLIBINC-NOT: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
123155
// CHECK-LIBCXX-NOSTDLIBINC-NOT: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"
156+
157+
// Make sure we explain that we considered a path but didn't add it when it
158+
// doesn't exist.
159+
//
160+
// RUN: %clang -no-canonical-prefixes %s -fsyntax-only -v 2>&1 \
161+
// RUN: -target x86_64-apple-darwin \
162+
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain_no_libcxx/usr/bin \
163+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk \
164+
// RUN: -stdlib=libc++ \
165+
// RUN: | FileCheck -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain_no_libcxx \
166+
// RUN: --check-prefix=CHECK-LIBCXX-MISSING-TOOLCHAIN %s
167+
// CHECK-LIBCXX-MISSING-TOOLCHAIN: ignoring nonexistent directory "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
168+
//
169+
// RUN: %clang -no-canonical-prefixes %s -fsyntax-only -v 2>&1 \
170+
// RUN: -target x86_64-apple-darwin \
171+
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain_no_libcxx/usr/bin \
172+
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_no_libcxx \
173+
// RUN: -stdlib=libc++ \
174+
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_no_libcxx \
175+
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain_no_libcxx \
176+
// RUN: --check-prefix=CHECK-LIBCXX-MISSING-BOTH %s
177+
// CHECK-LIBCXX-MISSING-BOTH: ignoring nonexistent directory "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
178+
// CHECK-LIBCXX-MISSING-BOTH: ignoring nonexistent directory "[[SYSROOT]]/usr/include/c++/v1"

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9700,14 +9700,15 @@ GetSpecializedASTName(ScratchTypeSystemClang::IsolatedASTKind feature) {
97009700
}
97019701

97029702
TypeSystemClang &ScratchTypeSystemClang::GetIsolatedAST(
9703-
ScratchTypeSystemClang::IsolatedASTKind feature) {
9703+
ScratchTypeSystemClang::IsolatedASTKind feature_enum) {
9704+
int feature = static_cast<int>(feature_enum);
97049705
auto found_ast = m_isolated_asts.find(feature);
97059706
if (found_ast != m_isolated_asts.end())
97069707
return *found_ast->second;
97079708

97089709
// Couldn't find the requested sub-AST, so create it now.
97099710
std::unique_ptr<TypeSystemClang> new_ast;
9710-
new_ast.reset(new SpecializedScratchAST(GetSpecializedASTName(feature),
9711+
new_ast.reset(new SpecializedScratchAST(GetSpecializedASTName(feature_enum),
97119712
m_triple, CreateASTSource()));
97129713
m_isolated_asts[feature] = std::move(new_ast);
97139714
return *m_isolated_asts[feature];

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ class ScratchTypeSystemClang : public TypeSystemClang {
12371237
/// Map from IsolatedASTKind to their actual TypeSystemClang instance.
12381238
/// This map is lazily filled with sub-ASTs and should be accessed via
12391239
/// `GetSubAST` (which lazily fills this map).
1240-
std::unordered_map<IsolatedASTKind, std::unique_ptr<TypeSystemClang>>
1240+
std::unordered_map<int, std::unique_ptr<TypeSystemClang>>
12411241
m_isolated_asts;
12421242
};
12431243

llvm/lib/Transforms/Coroutines/CoroFrame.cpp

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ static Instruction *insertSpills(const SpillInfo &Spills, coro::Shape &Shape) {
804804
CurrentValue->getName() + Twine(".reload"));
805805
};
806806

807+
SmallDenseMap<llvm::Value *, llvm::AllocaInst *, 4> DbgPtrAllocaCache;
807808
Value *GEP = nullptr, *CurrentGEP = nullptr;
808809
for (auto const &E : Spills) {
809810
// If we have not seen the value, generate a spill.
@@ -896,12 +897,21 @@ static Instruction *insertSpills(const SpillInfo &Spills, coro::Shape &Shape) {
896897
if (CurrentGEP != GEP) {
897898
CurrentGEP = GEP;
898899
TinyPtrVector<DbgDeclareInst *> DIs = FindDbgDeclareUses(CurrentValue);
899-
if (!DIs.empty())
900-
DIBuilder(*CurrentBlock->getParent()->getParent(),
901-
/*AllowUnresolved*/ false)
902-
.insertDeclare(CurrentGEP, DIs.front()->getVariable(),
903-
DIs.front()->getExpression(),
904-
DIs.front()->getDebugLoc(), DIs.front());
900+
if (!DIs.empty()) {
901+
auto *DDI = DIs.front();
902+
bool AllowUnresolved = false;
903+
// This dbg.declare is preserved for all coro-split function
904+
// fragments. It will be unreachable in the main function, and
905+
// processed by coro::salvageDebugInfo() by CoroCloner.
906+
DIBuilder(*CurrentBlock->getParent()->getParent(), AllowUnresolved)
907+
.insertDeclare(CurrentGEP, DDI->getVariable(),
908+
DDI->getExpression(),
909+
DDI->getDebugLoc(),
910+
&*Builder.GetInsertPoint());
911+
// This dbg.declare is for the main function entry point. It
912+
// will be deleted in all coro-split functions.
913+
coro::salvageDebugInfo(DbgPtrAllocaCache, DDI);
914+
}
905915
}
906916

907917
// Replace all uses of CurrentValue in the current instruction with reload.
@@ -1633,6 +1643,55 @@ static void sinkLifetimeStartMarkers(Function &F, coro::Shape &Shape,
16331643
}
16341644
}
16351645

1646+
void coro::salvageDebugInfo(
1647+
SmallDenseMap<llvm::Value *, llvm::AllocaInst *, 4> &DbgPtrAllocaCache,
1648+
DbgDeclareInst *DDI, bool LoadFromFramePtr) {
1649+
Function *F = DDI->getFunction();
1650+
IRBuilder<> Builder(F->getContext());
1651+
auto InsertPt = F->getEntryBlock().getFirstInsertionPt();
1652+
while (isa<IntrinsicInst>(InsertPt))
1653+
++InsertPt;
1654+
Builder.SetInsertPoint(&F->getEntryBlock(), InsertPt);
1655+
DIExpression *Expr = DDI->getExpression();
1656+
// Follow the pointer arithmetic all the way to the incoming
1657+
// function argument and convert into a DIExpression.
1658+
Value *Storage = DDI->getAddress();
1659+
while (Storage) {
1660+
if (auto *LdInst = dyn_cast<LoadInst>(Storage)) {
1661+
Storage = LdInst->getOperand(0);
1662+
} else if (auto *GEPInst = dyn_cast<GetElementPtrInst>(Storage)) {
1663+
Expr = llvm::salvageDebugInfoImpl(*GEPInst, Expr,
1664+
/*WithStackValue=*/false);
1665+
Storage = GEPInst->getOperand(0);
1666+
} else if (auto *BCInst = dyn_cast<llvm::BitCastInst>(Storage))
1667+
Storage = BCInst->getOperand(0);
1668+
else
1669+
break;
1670+
}
1671+
// Store a pointer to the coroutine frame object in an alloca so it
1672+
// is available throughout the function when producing unoptimized
1673+
// code. Extending the lifetime this way is correct because the
1674+
// variable has been declared by a dbg.declare intrinsic.
1675+
if (auto Arg = dyn_cast_or_null<llvm::Argument>(Storage)) {
1676+
auto &Cached = DbgPtrAllocaCache[Storage];
1677+
if (!Cached) {
1678+
Cached = Builder.CreateAlloca(Storage->getType(), 0, nullptr,
1679+
Arg->getName() + ".debug");
1680+
Builder.CreateStore(Storage, Cached);
1681+
}
1682+
Storage = Cached;
1683+
Expr = DIExpression::prepend(Expr, DIExpression::DerefBefore);
1684+
}
1685+
// The FramePtr object adds one extra layer of indirection that
1686+
// needs to be unwrapped.
1687+
if (LoadFromFramePtr)
1688+
Expr = DIExpression::prepend(Expr, DIExpression::DerefBefore);
1689+
auto &VMContext = DDI->getFunction()->getContext();
1690+
DDI->setOperand(
1691+
0, MetadataAsValue::get(VMContext, ValueAsMetadata::get(Storage)));
1692+
DDI->setOperand(2, MetadataAsValue::get(VMContext, Expr));
1693+
}
1694+
16361695
void coro::buildCoroutineFrame(Function &F, Shape &Shape) {
16371696
eliminateSwiftError(F, Shape);
16381697

llvm/lib/Transforms/Coroutines/CoroInternal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ void replaceAllCoroFrees(CoroBeginInst *CB, Value *Replacement);
5252
void replaceCoroFree(CoroIdInst *CoroId, bool Elide);
5353
void updateCallGraph(Function &Caller, ArrayRef<Function *> Funcs,
5454
CallGraph &CG, CallGraphSCC &SCC);
55+
/// Recover a dbg.declare prepared by the frontend and emit an alloca
56+
/// holding a pointer to the coroutine frame.
57+
void salvageDebugInfo(
58+
SmallDenseMap<llvm::Value *, llvm::AllocaInst *, 4> &DbgPtrAllocaCache,
59+
DbgDeclareInst *DDI, bool LoadFromCoroFrame = false);
5560

5661
// Keeps data and helper functions for lowering coroutine intrinsics.
5762
struct LowererBase {

0 commit comments

Comments
 (0)