@@ -964,7 +964,8 @@ void swift::serialization::diagnoseSerializedASTLoadFailure(
964964 }
965965}
966966
967- bool swift::extractCompilerFlagsFromInterface (StringRef buffer,
967+ bool swift::extractCompilerFlagsFromInterface (StringRef interfacePath,
968+ StringRef buffer,
968969 llvm::StringSaver &ArgSaver,
969970 SmallVectorImpl<const char *> &SubArgs) {
970971 SmallVector<StringRef, 1 > FlagMatches;
@@ -973,6 +974,29 @@ bool swift::extractCompilerFlagsFromInterface(StringRef buffer,
973974 return true ;
974975 assert (FlagMatches.size () == 2 );
975976 llvm::cl::TokenizeGNUCommandLine (FlagMatches[1 ], ArgSaver, SubArgs);
977+
978+ auto intFileName = llvm::sys::path::filename (interfacePath);
979+
980+ // Sanitize arch if the file name and the encoded flags disagree.
981+ // It's a known issue that we are using arm64e interfaces contents for the arm64 target,
982+ // meaning the encoded module flags are using -target arm64e-x-x. Fortunately,
983+ // we can tell the target arch from the interface file name, so we could sanitize
984+ // the target to use by inferring target from the file name.
985+ StringRef arm64 = " arm64" ;
986+ StringRef arm64e = " arm64e" ;
987+ if (intFileName.contains (arm64) && !intFileName.contains (arm64e)) {
988+ for (unsigned I = 1 ; I < SubArgs.size (); ++I) {
989+ if (strcmp (SubArgs[I - 1 ], " -target" ) != 0 ) {
990+ continue ;
991+ }
992+ StringRef triple (SubArgs[I]);
993+ if (triple.startswith (arm64e)) {
994+ SubArgs[I] = ArgSaver.save ((llvm::Twine (arm64) +
995+ triple.substr (arm64e.size ())).str ()).data ();
996+ }
997+ }
998+ }
999+
9761000 SmallVector<StringRef, 1 > IgnFlagMatches;
9771001 // Cherry-pick supported options from the ignorable list.
9781002 auto IgnFlagRe = llvm::Regex (" ^// swift-module-flags-ignorable:(.*)$" ,
@@ -1011,7 +1035,8 @@ swift::extractUserModuleVersionFromInterface(StringRef moduleInterfacePath) {
10111035 llvm::BumpPtrAllocator alloc;
10121036 llvm::StringSaver argSaver (alloc);
10131037 SmallVector<const char *, 8 > args;
1014- (void )extractCompilerFlagsFromInterface ((*file)->getBuffer (), argSaver, args);
1038+ (void )extractCompilerFlagsFromInterface (moduleInterfacePath,
1039+ (*file)->getBuffer (), argSaver, args);
10151040 for (unsigned I = 0 , N = args.size (); I + 1 < N; I++) {
10161041 // Check the version number specified via -user-module-version.
10171042 StringRef current (args[I]), next (args[I + 1 ]);
0 commit comments