Skip to content

Commit b3d3f67

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 8b76522 commit b3d3f67

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;
@@ -268,6 +272,11 @@ void diagnoseSerializedASTLoadFailureTransitive(
268272
ASTContext &Ctx, SourceLoc diagLoc, const serialization::Status status,
269273
ModuleFile *loadedModuleFile, Identifier ModuleName, bool forTestable);
270274

275+
/// Determine whether two triples are considered to be compatible for module
276+
/// serialization purposes.
277+
bool areCompatible(const llvm::Triple &moduleTarget,
278+
const llvm::Triple &ctxTarget);
279+
271280
} // end namespace serialization
272281
} // end namespace swift
273282

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());
@@ -248,8 +258,7 @@ Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
248258
ASTContext &ctx = getContext();
249259

250260
llvm::Triple moduleTarget(llvm::Triple::normalize(Core->TargetTriple));
251-
if (!areCompatibleArchitectures(moduleTarget, ctx.LangOpts.Target) ||
252-
!areCompatibleOSs(moduleTarget, ctx.LangOpts.Target)) {
261+
if (!areCompatible(moduleTarget, ctx.LangOpts.Target)) {
253262
status = Status::TargetIncompatible;
254263
if (!recoverFromIncompatibility)
255264
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
@@ -230,6 +230,9 @@ int main(int argc, char **argv) {
230230
opt<bool> Verbose("verbose", desc("Dump informations on the loaded module"),
231231
cat(Visible));
232232

233+
opt<std::string> Filter("filter", desc("triple for filtering modules"),
234+
cat(Visible));
235+
233236
opt<std::string> ModuleCachePath(
234237
"module-cache-path", desc("Clang module cache path"), cat(Visible));
235238

@@ -342,9 +345,11 @@ int main(int argc, char **argv) {
342345
ClangImporter->setDWARFImporterDelegate(dummyDWARFImporter);
343346
}
344347

348+
llvm::Triple filter(Filter);
345349
for (auto &Module : Modules)
346350
if (!parseASTSection(*CI.getMemoryBufferSerializedModuleLoader(),
347-
StringRef(Module.first, Module.second), modules)) {
351+
StringRef(Module.first, Module.second), filter,
352+
modules)) {
348353
llvm::errs() << "error: Failed to parse AST section!\n";
349354
return 1;
350355
}

0 commit comments

Comments
 (0)