Skip to content

Commit a3a43d4

Browse files
committed
[Serialization] Teach ASTSectionImporter to filter by triple.
On macOS it is possible for one application to contain Swift modules compiled for different triples that are incompatible as far as the Swift compiler is concerned. Examples include an iOS simulator application hunning on a macOS host, or a macCatalyst application running on macOS. A debugger might see .swift_ast sections for all triples at the same time. This patch adds an interface to let the client provide a triple to filter Swift modules in an ASTSection. rdar://107869141
1 parent 6860238 commit a3a43d4

File tree

6 files changed

+42
-6
lines changed

6 files changed

+42
-6
lines changed

include/swift/ASTSectionImporter/ASTSectionImporter.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "swift/Basic/LLVM.h"
2121
#include <string>
2222

23+
namespace llvm {
24+
class Triple;
25+
}
2326
namespace swift {
2427
class MemoryBufferSerializedModuleLoader;
2528

@@ -28,9 +31,10 @@ namespace swift {
2831
/// registers them using registerMemoryBuffer() so they can be found by
2932
/// loadModule(). The access path of all modules found in the section is
3033
/// appended to the vector foundModules.
34+
/// \param filter If fully specified, only matching modules are registered.
3135
/// \return true if successful.
3236
bool parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
33-
StringRef Data,
37+
StringRef Data, const llvm::Triple &filter,
3438
SmallVectorImpl<std::string> &foundModules);
3539
}
3640
#endif

include/swift/Serialization/Validation.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "llvm/ADT/SmallVector.h"
2020
#include "llvm/ADT/StringRef.h"
2121

22+
namespace llvm {
23+
class Triple;
24+
}
25+
2226
namespace swift {
2327

2428
class ModuleFile;
@@ -302,6 +306,11 @@ void diagnoseSerializedASTLoadFailureTransitive(
302306
ASTContext &Ctx, SourceLoc diagLoc, const serialization::Status status,
303307
ModuleFile *loadedModuleFile, Identifier ModuleName, bool forTestable);
304308

309+
/// Determine whether two triples are considered to be compatible for module
310+
/// serialization purposes.
311+
bool areCompatible(const llvm::Triple &moduleTarget,
312+
const llvm::Triple &ctxTarget);
313+
305314
} // end namespace serialization
306315
} // end namespace swift
307316

lib/ASTSectionImporter/ASTSectionImporter.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,13 @@ using namespace swift;
2828

2929
bool swift::parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
3030
StringRef buf,
31+
const llvm::Triple &filter,
3132
SmallVectorImpl<std::string> &foundModules) {
3233
if (!serialization::isSerializedAST(buf))
3334
return false;
3435

36+
bool haveFilter = filter.getOS() != llvm::Triple::UnknownOS &&
37+
filter.getArch() != llvm::Triple::UnknownArch;
3538
// An AST section consists of one or more AST modules, optionally with
3639
// headers. Iterate over all AST modules.
3740
while (!buf.empty()) {
@@ -43,10 +46,15 @@ bool swift::parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
4346

4447
if (info.status == serialization::Status::Valid) {
4548
assert(info.bytes != 0);
46-
if (!info.name.empty()) {
49+
bool selected = true;
50+
if (haveFilter) {
51+
llvm::Triple moduleTriple(info.targetTriple);
52+
selected = serialization::areCompatible(moduleTriple, filter);
53+
}
54+
if (!info.name.empty() && selected) {
4755
StringRef moduleData = buf.substr(0, info.bytes);
4856
std::unique_ptr<llvm::MemoryBuffer> bitstream(
49-
llvm::MemoryBuffer::getMemBuffer(moduleData, info.name, false));
57+
llvm::MemoryBuffer::getMemBuffer(moduleData, info.name, false));
5058

5159
// Register the memory buffer.
5260
Loader.registerMemoryBuffer(info.name, std::move(bitstream),

lib/Serialization/ModuleFile.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ static bool isTargetTooNew(const llvm::Triple &moduleTarget,
9191
return ctxTarget.isOSVersionLT(moduleTarget);
9292
}
9393

94+
namespace swift {
95+
namespace serialization {
96+
bool areCompatible(const llvm::Triple &moduleTarget,
97+
const llvm::Triple &ctxTarget) {
98+
return areCompatibleArchitectures(moduleTarget, ctxTarget) &&
99+
areCompatibleOSs(moduleTarget, ctxTarget);
100+
}
101+
} // namespace serialization
102+
} // namespace swift
103+
94104
ModuleFile::ModuleFile(std::shared_ptr<const ModuleFileSharedCore> core)
95105
: Core(core) {
96106
assert(!core->hasError());
@@ -249,8 +259,7 @@ Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
249259
ASTContext &ctx = getContext();
250260

251261
llvm::Triple moduleTarget(llvm::Triple::normalize(Core->TargetTriple));
252-
if (!areCompatibleArchitectures(moduleTarget, ctx.LangOpts.Target) ||
253-
!areCompatibleOSs(moduleTarget, ctx.LangOpts.Target)) {
262+
if (!areCompatible(moduleTarget, ctx.LangOpts.Target)) {
254263
status = Status::TargetIncompatible;
255264
if (!recoverFromIncompatibility)
256265
return error(status);

test/DebugInfo/ASTSection.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// RUN: %target-build-swift -emit-executable %s -g -o %t/ASTSection -emit-module
44
// RUN: %lldb-moduleimport-test -verbose %t/ASTSection | %FileCheck %s
5+
// RUN: %lldb-moduleimport-test -filter mips64-unknown-hurd -verbose %t/ASTSection | %FileCheck %s --check-prefix=LINETABLE-CHECK
56

67
// REQUIRES: executable_test
78
// REQUIRES: swift_tools_extra

tools/lldb-moduleimport-test/lldb-moduleimport-test.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ int main(int argc, char **argv) {
239239
opt<bool> Verbose("verbose", desc("Dump informations on the loaded module"),
240240
cat(Visible));
241241

242+
opt<std::string> Filter("filter", desc("triple for filtering modules"),
243+
cat(Visible));
244+
242245
opt<std::string> ModuleCachePath(
243246
"module-cache-path", desc("Clang module cache path"), cat(Visible));
244247

@@ -351,9 +354,11 @@ int main(int argc, char **argv) {
351354
ClangImporter->setDWARFImporterDelegate(dummyDWARFImporter);
352355
}
353356

357+
llvm::Triple filter(Filter);
354358
for (auto &Module : Modules)
355359
if (!parseASTSection(*CI.getMemoryBufferSerializedModuleLoader(),
356-
StringRef(Module.first, Module.second), modules)) {
360+
StringRef(Module.first, Module.second), filter,
361+
modules)) {
357362
llvm::errs() << "error: Failed to parse AST section!\n";
358363
return 1;
359364
}

0 commit comments

Comments
 (0)