Skip to content

Commit 1b5a94e

Browse files
[ClangImporter] Let clang pick default target CPU. (#80451)
Currently, ClangImporter has some logic to pick the default arm64 target CPU (when -target-cpu isn't passed) based on the target OS and arch variant. This was originally done long ago in 3cf3f42, and later maintained through 49a6c8e, because it was necessary when clang (targeting darwin) relied on -arch to set this sort of default. Clang has migrated to full -target triples for a while now, and has sophisticated logic for picking default target CPUs and features based on the target triple. Currently, we override that by passing our -mcpu explicitly. Instead, allow clang to pick its defaults, and only pass -mcpu when asked via -target-cpu. This is visible in the test, with arm64 macOS now defaulting to M1. The same applies to simulators, per Triple::isTargetMachineMac. rdar://148377686
1 parent 8909190 commit 1b5a94e

File tree

2 files changed

+11
-30
lines changed

2 files changed

+11
-30
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -821,39 +821,15 @@ importer::addCommonInvocationArguments(
821821
switch (triple.getArch()) {
822822
case llvm::Triple::x86:
823823
case llvm::Triple::x86_64:
824-
// `-mcpu` is deprecated and an alias for `-mtune`. We need to pass
825-
// `-march` and `-mtune` to behave identically to the `apple-a\d+` cases
826-
// below.
824+
// For x86, `-mcpu` is deprecated and an alias of `-mtune`. We need to
825+
// pass `-march` and `-mtune` to behave like `-mcpu` on other targets.
827826
invocationArgStrs.push_back("-march=" + importerOpts.TargetCPU);
828827
invocationArgStrs.push_back("-mtune=" + importerOpts.TargetCPU);
829828
break;
830829
default:
831830
invocationArgStrs.push_back("-mcpu=" + importerOpts.TargetCPU);
832831
break;
833832
}
834-
} else if (triple.isOSDarwin()) {
835-
// Special case CPU based on known deployments:
836-
// - arm64 deploys to apple-a7
837-
// - arm64 on macOS
838-
// - arm64 for iOS/tvOS/watchOS simulators
839-
// - arm64e deploys to apple-a12
840-
// and arm64e (everywhere) and arm64e macOS defaults to the "apple-a12" CPU
841-
// for Darwin, but Clang only detects this if we use -arch.
842-
if (triple.getArchName() == "arm64e")
843-
invocationArgStrs.push_back("-mcpu=apple-a12");
844-
else if (triple.isAArch64() && triple.isMacOSX())
845-
invocationArgStrs.push_back("-mcpu=apple-a12");
846-
else if (triple.isAArch64() && triple.isSimulatorEnvironment() &&
847-
(triple.isiOS() || triple.isWatchOS()))
848-
invocationArgStrs.push_back("-mcpu=apple-a12");
849-
else if (triple.isAArch64() && triple.isSimulatorEnvironment() &&
850-
triple.isXROS())
851-
invocationArgStrs.push_back("-mcpu=apple-a12");
852-
else if (triple.getArch() == llvm::Triple::aarch64 ||
853-
triple.getArch() == llvm::Triple::aarch64_32 ||
854-
triple.getArch() == llvm::Triple::aarch64_be) {
855-
invocationArgStrs.push_back("-mcpu=apple-a7");
856-
}
857833
} else if (triple.getArch() == llvm::Triple::systemz) {
858834
invocationArgStrs.push_back("-march=z13");
859835
}
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
// RUN: %swift %s -typecheck -parse-stdlib -dump-clang-diagnostics -target arm64e-apple-macos11.0 2>&1 | %FileCheck -check-prefix=CHECK-APPLE-M1 %s
2+
// RUN: %swift %s -typecheck -parse-stdlib -dump-clang-diagnostics -target arm64-apple-ios13.0-simulator 2>&1 | %FileCheck -check-prefix=CHECK-APPLE-M1 %s
3+
// CHECK-APPLE-M1: '-target-cpu' 'apple-m1' '-target-feature'
4+
15
// RUN: %swift %s -typecheck -parse-stdlib -dump-clang-diagnostics -target arm64e-apple-ios13.0 2>&1 | %FileCheck -check-prefix=CHECK-APPLE-A12 %s
2-
// RUN: %swift %s -typecheck -parse-stdlib -dump-clang-diagnostics -target arm64e-apple-macos11.0 2>&1 | %FileCheck -check-prefix=CHECK-APPLE-A12 %s
3-
// RUN: %swift %s -typecheck -parse-stdlib -dump-clang-diagnostics -target arm64e-apple-ios13.0-simulator 2>&1 | %FileCheck -check-prefix=CHECK-APPLE-A12 %s
4-
// CHECK-APPLE-A12: '-mcpu=apple-a12'
6+
// CHECK-APPLE-A12: '-target-cpu' 'apple-a12' '-target-feature'
57

68
// RUN: %swift %s -typecheck -parse-stdlib -dump-clang-diagnostics -target arm64-apple-ios13.0 2>&1 | %FileCheck -check-prefix=CHECK-APPLE-A7 %s
7-
// CHECK-APPLE-A7: '-mcpu=apple-a7'
9+
// CHECK-APPLE-A7: '-target-cpu' 'apple-a7' '-target-feature'
10+
11+
// RUN: %swift %s -typecheck -parse-stdlib -dump-clang-diagnostics -target arm64-apple-ios13.0 -target-cpu apple-a16 2>&1 | %FileCheck -check-prefix=CHECK-APPLE-A16 %s
12+
// CHECK-APPLE-A16: '-target-cpu' 'apple-a16' '-target-feature'

0 commit comments

Comments
 (0)