Skip to content

[Serialization] Teach ASTSectionImporter to filter by triple. #65725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion include/swift/ASTSectionImporter/ASTSectionImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#include "swift/Basic/LLVM.h"
#include <string>

namespace llvm {
class Triple;
}
namespace swift {
class MemoryBufferSerializedModuleLoader;

Expand All @@ -28,9 +31,10 @@ namespace swift {
/// registers them using registerMemoryBuffer() so they can be found by
/// loadModule(). The access path of all modules found in the section is
/// appended to the vector foundModules.
/// \param filter If fully specified, only matching modules are registered.
/// \return true if successful.
bool parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
StringRef Data,
StringRef Data, const llvm::Triple &filter,
SmallVectorImpl<std::string> &foundModules);
}
#endif
9 changes: 9 additions & 0 deletions include/swift/Serialization/Validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"

namespace llvm {
class Triple;
}

namespace swift {

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

/// Determine whether two triples are considered to be compatible for module
/// serialization purposes.
bool areCompatible(const llvm::Triple &moduleTarget,
const llvm::Triple &ctxTarget);

} // end namespace serialization
} // end namespace swift

Expand Down
12 changes: 10 additions & 2 deletions lib/ASTSectionImporter/ASTSectionImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ using namespace swift;

bool swift::parseASTSection(MemoryBufferSerializedModuleLoader &Loader,
StringRef buf,
const llvm::Triple &filter,
SmallVectorImpl<std::string> &foundModules) {
if (!serialization::isSerializedAST(buf))
return false;

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

if (info.status == serialization::Status::Valid) {
assert(info.bytes != 0);
if (!info.name.empty()) {
bool selected = true;
if (haveFilter) {
llvm::Triple moduleTriple(info.targetTriple);
selected = serialization::areCompatible(moduleTriple, filter);
}
if (!info.name.empty() && selected) {
StringRef moduleData = buf.substr(0, info.bytes);
std::unique_ptr<llvm::MemoryBuffer> bitstream(
llvm::MemoryBuffer::getMemBuffer(moduleData, info.name, false));
llvm::MemoryBuffer::getMemBuffer(moduleData, info.name, false));

// Register the memory buffer.
Loader.registerMemoryBuffer(info.name, std::move(bitstream),
Expand Down
13 changes: 11 additions & 2 deletions lib/Serialization/ModuleFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ static bool isTargetTooNew(const llvm::Triple &moduleTarget,
return ctxTarget.isOSVersionLT(moduleTarget);
}

namespace swift {
namespace serialization {
bool areCompatible(const llvm::Triple &moduleTarget,
const llvm::Triple &ctxTarget) {
return areCompatibleArchitectures(moduleTarget, ctxTarget) &&
areCompatibleOSs(moduleTarget, ctxTarget);
}
} // namespace serialization
} // namespace swift

ModuleFile::ModuleFile(std::shared_ptr<const ModuleFileSharedCore> core)
: Core(core) {
assert(!core->hasError());
Expand Down Expand Up @@ -249,8 +259,7 @@ Status ModuleFile::associateWithFileContext(FileUnit *file, SourceLoc diagLoc,
ASTContext &ctx = getContext();

llvm::Triple moduleTarget(llvm::Triple::normalize(Core->TargetTriple));
if (!areCompatibleArchitectures(moduleTarget, ctx.LangOpts.Target) ||
!areCompatibleOSs(moduleTarget, ctx.LangOpts.Target)) {
if (!areCompatible(moduleTarget, ctx.LangOpts.Target)) {
status = Status::TargetIncompatible;
if (!recoverFromIncompatibility)
return error(status);
Expand Down
1 change: 1 addition & 0 deletions test/DebugInfo/ASTSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

// REQUIRES: executable_test
// REQUIRES: swift_tools_extra
Expand Down
7 changes: 6 additions & 1 deletion tools/lldb-moduleimport-test/lldb-moduleimport-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ int main(int argc, char **argv) {
opt<bool> Verbose("verbose", desc("Dump informations on the loaded module"),
cat(Visible));

opt<std::string> Filter("filter", desc("triple for filtering modules"),
cat(Visible));

opt<std::string> ModuleCachePath(
"module-cache-path", desc("Clang module cache path"), cat(Visible));

Expand Down Expand Up @@ -351,9 +354,11 @@ int main(int argc, char **argv) {
ClangImporter->setDWARFImporterDelegate(dummyDWARFImporter);
}

llvm::Triple filter(Filter);
for (auto &Module : Modules)
if (!parseASTSection(*CI.getMemoryBufferSerializedModuleLoader(),
StringRef(Module.first, Module.second), modules)) {
StringRef(Module.first, Module.second), filter,
modules)) {
llvm::errs() << "error: Failed to parse AST section!\n";
return 1;
}
Expand Down