Skip to content

Commit cbb9426

Browse files
committed
Canonicalize aarch64 to arm64 before passing it to the Swift compiler.
Unfortunately Swift is really peculiar about the CPU type when looking for the standard library. This fixes the Swift REPL on Apple Silicon. <rdar://problem/66802226>
1 parent 8cbcb68 commit cbb9426

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

lldb/include/lldb/Symbol/SwiftASTContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,11 @@ class SwiftASTContext : public TypeSystemSwift {
749749
bool SetTriple(const llvm::Triple triple,
750750
lldb_private::Module *module = nullptr);
751751

752+
/// Condition a triple to be safe for use with Swift. Swift is
753+
/// really peculiar about what CPU types it thinks it has standard
754+
/// libraries for.
755+
static llvm::Triple GetSwiftFriendlyTriple(llvm::Triple triple);
756+
752757
CompilerType GetCompilerType(swift::TypeBase *swift_type);
753758
CompilerType GetCompilerType(ConstString mangled_name);
754759
swift::Type GetSwiftType(CompilerType compiler_type);

lldb/source/Symbol/SwiftASTContext.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,14 +2319,20 @@ llvm::Triple SwiftASTContext::GetTriple() const {
23192319
return llvm::Triple(m_compiler_invocation_ap->getTargetTriple());
23202320
}
23212321

2322-
/// Conditions a triple string to be safe for use with Swift. Right
2323-
/// now this just strips the Haswell marker off the CPU name.
2322+
/// Conditions a triple string to be safe for use with Swift. Swift
2323+
/// is really peculiar about what CPU types it thinks it has standard
2324+
/// libraries for.
23242325
///
23252326
/// TODO: Make Swift more robust.
2326-
static std::string GetSwiftFriendlyTriple(StringRef triple) {
2327-
if (triple.consume_front("x86_64h"))
2328-
return std::string("x86_64") + triple.str();
2329-
return triple.str();
2327+
llvm::Triple SwiftASTContext::GetSwiftFriendlyTriple(llvm::Triple triple) {
2328+
StringRef arch_name = triple.getArchName();
2329+
if (arch_name == "x86_64h")
2330+
triple.setArchName("x86_64");
2331+
else if (arch_name == "aarch64")
2332+
triple.setArchName("arm64");
2333+
else if (arch_name == "aarch64_32")
2334+
triple.setArchName("arm64_32");
2335+
return triple;
23302336
}
23312337

23322338
bool SwiftASTContext::SetTriple(const llvm::Triple triple, Module *module) {
@@ -2344,18 +2350,15 @@ bool SwiftASTContext::SetTriple(const llvm::Triple triple, Module *module) {
23442350
}
23452351

23462352
const unsigned unspecified = 0;
2347-
std::string adjusted_triple = GetSwiftFriendlyTriple(triple.str());
2353+
llvm::Triple adjusted_triple = GetSwiftFriendlyTriple(triple);
23482354
// If the OS version is unspecified, do fancy things.
23492355
if (triple.getOSMajorVersion() == unspecified) {
23502356
// If a triple is "<arch>-apple-darwin" change it to be
23512357
// "<arch>-apple-macosx" otherwise the major and minor OS
23522358
// version we append below would be wrong.
23532359
if (triple.getVendor() == llvm::Triple::VendorType::Apple &&
2354-
triple.getOS() == llvm::Triple::OSType::Darwin) {
2355-
llvm::Triple mac_triple(adjusted_triple);
2356-
mac_triple.setOS(llvm::Triple::OSType::MacOSX);
2357-
adjusted_triple = mac_triple.str();
2358-
}
2360+
triple.getOS() == llvm::Triple::OSType::Darwin)
2361+
adjusted_triple.setOS(llvm::Triple::OSType::MacOSX);
23592362

23602363
// Append the min OS to the triple if we have a target
23612364
ModuleSP module_sp;
@@ -2370,12 +2373,9 @@ bool SwiftASTContext::SetTriple(const llvm::Triple triple, Module *module) {
23702373

23712374
if (module) {
23722375
if (ObjectFile *objfile = module->GetObjectFile())
2373-
if (llvm::VersionTuple version = objfile->GetMinimumOSVersion()) {
2374-
llvm::Triple vers_triple(adjusted_triple);
2375-
vers_triple.setOSName(vers_triple.getOSName().str() +
2376-
version.getAsString());
2377-
adjusted_triple = vers_triple.str();
2378-
}
2376+
if (llvm::VersionTuple version = objfile->GetMinimumOSVersion())
2377+
adjusted_triple.setOSName(adjusted_triple.getOSName().str() +
2378+
version.getAsString());
23792379
}
23802380
}
23812381
if (llvm::Triple(triple).getOS() == llvm::Triple::UnknownOS) {
@@ -2384,15 +2384,14 @@ bool SwiftASTContext::SetTriple(const llvm::Triple triple, Module *module) {
23842384
return false;
23852385
}
23862386
LOG_PRINTF(LIBLLDB_LOG_TYPES, "(\"%s\") setting to \"%s\"",
2387-
triple.str().c_str(), adjusted_triple.c_str());
2387+
triple.str().c_str(), adjusted_triple.str().c_str());
23882388

2389-
llvm::Triple adjusted_llvm_triple(adjusted_triple);
2390-
m_compiler_invocation_ap->setTargetTriple(adjusted_llvm_triple);
2389+
m_compiler_invocation_ap->setTargetTriple(adjusted_triple);
23912390

2392-
assert(GetTriple() == adjusted_llvm_triple);
2391+
assert(GetTriple() == adjusted_triple);
23932392
assert(!m_ast_context_ap ||
23942393
(llvm::Triple(m_ast_context_ap->LangOpts.Target.getTriple()) ==
2395-
adjusted_llvm_triple));
2394+
adjusted_triple));
23962395

23972396
// Every time the triple is changed the LangOpts must be updated
23982397
// too, because Swift default-initializes the EnableObjCInterop

lldb/unittests/Symbol/TestSwiftASTContext.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,18 @@ TEST_F(TestSwiftASTContext, IsNonTriviallyManagedReferenceType) {
197197
nullptr));
198198
#endif
199199
}
200+
201+
TEST_F(TestSwiftASTContext, SwiftFriendlyTriple) {
202+
EXPECT_EQ(SwiftASTContext::GetSwiftFriendlyTriple(
203+
llvm::Triple("x86_64-apple-macosx")),
204+
llvm::Triple("x86_64-apple-macosx"));
205+
EXPECT_EQ(SwiftASTContext::GetSwiftFriendlyTriple(
206+
llvm::Triple("x86_64h-apple-macosx")),
207+
llvm::Triple("x86_64-apple-macosx"));
208+
EXPECT_EQ(SwiftASTContext::GetSwiftFriendlyTriple(
209+
llvm::Triple("aarch64-apple-macosx")),
210+
llvm::Triple("arm64-apple-macosx"));
211+
EXPECT_EQ(SwiftASTContext::GetSwiftFriendlyTriple(
212+
llvm::Triple("aarch64_32-apple-watchos")),
213+
llvm::Triple("arm64_32-apple-watchos"));
214+
}

0 commit comments

Comments
 (0)