Skip to content

Commit 4b99272

Browse files
author
Nathan Hawes
authored
Merge pull request #23844 from nathawes/parseable-interface-cherry-picks
[5.1][03-18-2019] Parseable interface cherrypicks from 5.1
2 parents b87ba4a + 69c68f9 commit 4b99272

25 files changed

+887
-177
lines changed

include/swift/Frontend/ParseableInterfaceModuleLoader.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,18 @@ namespace clang {
110110
class CompilerInstance;
111111
}
112112

113+
namespace unittest {
114+
class ParseableInterfaceModuleLoaderTest;
115+
}
116+
113117
namespace swift {
114118

115119
/// A ModuleLoader that runs a subordinate \c CompilerInvocation and
116120
/// \c CompilerInstance to convert .swiftinterface files to .swiftmodule
117121
/// files on the fly, caching the resulting .swiftmodules in the module cache
118122
/// directory, and loading the serialized .swiftmodules from there.
119123
class ParseableInterfaceModuleLoader : public SerializedModuleLoaderBase {
124+
friend class unittest::ParseableInterfaceModuleLoaderTest;
120125
explicit ParseableInterfaceModuleLoader(ASTContext &ctx, StringRef cacheDir,
121126
StringRef prebuiltCacheDir,
122127
DependencyTracker *tracker,
@@ -134,6 +139,7 @@ class ParseableInterfaceModuleLoader : public SerializedModuleLoaderBase {
134139
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
135140
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer) override;
136141

142+
bool isCached(StringRef DepPath) override;
137143

138144
public:
139145
static std::unique_ptr<ParseableInterfaceModuleLoader>
@@ -152,7 +158,7 @@ class ParseableInterfaceModuleLoader : public SerializedModuleLoaderBase {
152158
static bool buildSwiftModuleFromSwiftInterface(
153159
ASTContext &Ctx, StringRef CacheDir, StringRef PrebuiltCacheDir,
154160
StringRef ModuleName, StringRef InPath, StringRef OutPath,
155-
bool SerializeDependencyHashes);
161+
bool SerializeDependencyHashes, bool TrackSystemDependencies);
156162
};
157163

158164
/// Extract the specified-or-defaulted -module-cache-path that winds up in

include/swift/Serialization/ModuleFormat.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5252
/// describe what change you made. The content of this comment isn't important;
5353
/// it just ensures a conflict if two people change the module format.
5454
/// Don't worry about adhering to the 80-column limit for this line.
55-
const uint16_t SWIFTMODULE_VERSION_MINOR = 477; // Last change: prebuilt module cache
55+
const uint16_t SWIFTMODULE_VERSION_MINOR = 478; // SDK-relative dependencies flag
5656

5757
using DeclIDField = BCFixed<31>;
5858

@@ -674,6 +674,7 @@ namespace input_block {
674674
FileSizeField, // file size (for validation)
675675
FileModTimeOrContentHashField, // mtime or content hash (for validation)
676676
BCFixed<1>, // are we reading mtime (0) or hash (1)?
677+
BCFixed<1>, // SDK-relative?
677678
BCBlob // path
678679
>;
679680
}

include/swift/Serialization/SerializationOptions.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ namespace swift {
3939
/// appropriate strategy for how to verify if it's up-to-date.
4040
class FileDependency {
4141
/// The size of the file on disk, in bytes.
42-
uint64_t Size : 63;
42+
uint64_t Size : 62;
4343

4444
/// A dependency can be either hash-based or modification-time-based.
4545
bool IsHashBased : 1;
4646

47+
/// The dependency path can be absolute or relative to the SDK
48+
bool IsSDKRelative : 1;
49+
4750
union {
4851
/// The last modification time of the file.
4952
uint64_t ModificationTime;
@@ -56,22 +59,22 @@ namespace swift {
5659
std::string Path;
5760

5861
FileDependency(uint64_t size, bool isHash, uint64_t hashOrModTime,
59-
StringRef path):
60-
Size(size), IsHashBased(isHash), ModificationTime(hashOrModTime),
61-
Path(path) {}
62+
StringRef path, bool isSDKRelative):
63+
Size(size), IsHashBased(isHash), IsSDKRelative(isSDKRelative),
64+
ModificationTime(hashOrModTime), Path(path) {}
6265
public:
6366
FileDependency() = delete;
6467

6568
/// Creates a new hash-based file dependency.
6669
static FileDependency
67-
hashBased(StringRef path, uint64_t size, uint64_t hash) {
68-
return FileDependency(size, /*isHash*/true, hash, path);
70+
hashBased(StringRef path, bool isSDKRelative, uint64_t size, uint64_t hash) {
71+
return FileDependency(size, /*isHash*/true, hash, path, isSDKRelative);
6972
}
7073

7174
/// Creates a new modification time-based file dependency.
7275
static FileDependency
73-
modTimeBased(StringRef path, uint64_t size, uint64_t mtime) {
74-
return FileDependency(size, /*isHash*/false, mtime, path);
76+
modTimeBased(StringRef path, bool isSDKRelative, uint64_t size, uint64_t mtime) {
77+
return FileDependency(size, /*isHash*/false, mtime, path, isSDKRelative);
7578
}
7679

7780
/// Updates the last-modified time of this dependency.
@@ -94,6 +97,9 @@ namespace swift {
9497
/// based on content hash.
9598
bool isHashBased() const { return IsHashBased; }
9699

100+
/// Determines if this dependency is absolute or relative to the SDK.
101+
bool isSDKRelative() const { return IsSDKRelative; }
102+
97103
/// Determines if this dependency is hash-based and should be validated
98104
/// based on modification time.
99105
bool isModificationTimeBased() const { return !IsHashBased; }

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class SerializedModuleLoaderBase : public ModuleLoader {
6363
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
6464
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer);
6565

66+
std::error_code
67+
openModuleDocFile(AccessPathElem ModuleID,
68+
StringRef ModuleDocPath,
69+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer);
70+
6671
/// If the module loader subclass knows that all options have been tried for
6772
/// loading an architecture-specific file out of a swiftmodule bundle, try
6873
/// to list the architectures that \e are present.
@@ -75,6 +80,12 @@ class SerializedModuleLoaderBase : public ModuleLoader {
7580
return false;
7681
}
7782

83+
/// Determines if the provided path is a cached artifact for dependency
84+
/// tracking purposes.
85+
virtual bool isCached(StringRef DepPath) {
86+
return false;
87+
}
88+
7889
public:
7990
virtual ~SerializedModuleLoaderBase();
8091
SerializedModuleLoaderBase(const SerializedModuleLoaderBase &) = delete;

include/swift/Subsystems.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,18 @@ namespace swift {
264264
void serialize(ModuleOrSourceFile DC, const SerializationOptions &options,
265265
const SILModule *M = nullptr);
266266

267+
/// Serializes a module or single source file to the given output file and
268+
/// returns back the file's contents as a memory buffer.
269+
///
270+
/// Use this if you intend to immediately load the serialized module, as that
271+
/// will both avoid extra filesystem traffic and will ensure you read back
272+
/// exactly what was written.
273+
void serializeToBuffers(ModuleOrSourceFile DC,
274+
const SerializationOptions &opts,
275+
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
276+
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
277+
const SILModule *M = nullptr);
278+
267279
/// Get the CPU, subtarget feature options, and triple to use when emitting code.
268280
std::tuple<llvm::TargetOptions, std::string, std::vector<std::string>,
269281
std::string>

lib/AST/ModuleLoader.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ void
3434
DependencyTracker::addDependency(StringRef File, bool IsSystem) {
3535
// DependencyTracker exposes an interface that (intentionally) does not talk
3636
// about clang at all, nor about missing deps. It does expose an IsSystem
37-
// dimension, though it is presently always false, we accept it and pass it
38-
// along to the clang DependencyCollector in case Swift callers start setting
39-
// it to true someday.
37+
// dimension, which we accept and pass along to the clang DependencyCollector.
38+
// along to the clang DependencyCollector.
4039
clangCollector->maybeAddDependency(File, /*FromModule=*/false,
4140
IsSystem, /*IsModuleFile=*/false,
4241
/*IsMissing=*/false);

lib/ClangImporter/ClangImporter.cpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,35 @@ void ClangImporter::clearTypeResolver() {
407407

408408
#pragma mark Module loading
409409

410+
/// Finds the glibc.modulemap file relative to the provided resource dir.
411+
///
412+
/// Note that the module map used for Glibc depends on the target we're
413+
/// compiling for, and is not included in the resource directory with the other
414+
/// implicit module maps. It's at {freebsd|linux}/{arch}/glibc.modulemap.
415+
static Optional<StringRef>
416+
getGlibcModuleMapPath(StringRef resourceDir, llvm::Triple triple,
417+
SmallVectorImpl<char> &scratch) {
418+
if (resourceDir.empty())
419+
return None;
420+
421+
scratch.append(resourceDir.begin(), resourceDir.end());
422+
llvm::sys::path::append(
423+
scratch,
424+
swift::getPlatformNameForTriple(triple),
425+
swift::getMajorArchitectureName(triple),
426+
"glibc.modulemap");
427+
428+
// Only specify the module map if that file actually exists.
429+
// It may not--for example in the case that
430+
// `swiftc -target x86_64-unknown-linux-gnu -emit-ir` is invoked using
431+
// a Swift compiler not built for Linux targets.
432+
if (llvm::sys::fs::exists(scratch)) {
433+
return StringRef(scratch.data(), scratch.size());
434+
} else {
435+
return None;
436+
}
437+
}
438+
410439
static void
411440
getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
412441
ASTContext &ctx,
@@ -571,28 +600,10 @@ getNormalInvocationArguments(std::vector<std::string> &invocationArgStrs,
571600
}
572601
}
573602

574-
// The module map used for Glibc depends on the target we're compiling for,
575-
// and is not included in the resource directory with the other implicit
576-
// module maps. It's at {freebsd|linux}/{arch}/glibc.modulemap.
577603
SmallString<128> GlibcModuleMapPath;
578-
GlibcModuleMapPath = searchPathOpts.RuntimeResourcePath;
579-
580-
// Running without a resource directory is not a supported configuration.
581-
assert(!GlibcModuleMapPath.empty());
582-
583-
llvm::sys::path::append(
584-
GlibcModuleMapPath,
585-
swift::getPlatformNameForTriple(triple),
586-
swift::getMajorArchitectureName(triple),
587-
"glibc.modulemap");
588-
589-
// Only specify the module map if that file actually exists.
590-
// It may not--for example in the case that
591-
// `swiftc -target x86_64-unknown-linux-gnu -emit-ir` is invoked using
592-
// a Swift compiler not built for Linux targets.
593-
if (llvm::sys::fs::exists(GlibcModuleMapPath)) {
594-
invocationArgStrs.push_back(
595-
(Twine("-fmodule-map-file=") + GlibcModuleMapPath).str());
604+
if (auto path = getGlibcModuleMapPath(searchPathOpts.RuntimeResourcePath,
605+
triple, GlibcModuleMapPath)) {
606+
invocationArgStrs.push_back((Twine("-fmodule-map-file=") + *path).str());
596607
} else {
597608
// FIXME: Emit a warning of some kind.
598609
}

0 commit comments

Comments
 (0)