Skip to content

Commit a4e6631

Browse files
author
Harlan Haskins
authored
Merge pull request #22394 from harlanhaskins/target-locked
[ParseableInterface] Use arch for cache key instead of full triple
2 parents 2e252e1 + 4eafcc9 commit a4e6631

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

include/swift/Frontend/ParseableInterfaceSupport.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include "swift/Basic/LLVM.h"
1717
#include "swift/Serialization/SerializedModuleLoader.h"
18+
#include "llvm/ADT/DenseMap.h"
19+
#include "llvm/Support/Errc.h"
1820
#include "llvm/Support/Regex.h"
1921

2022
namespace swift {
@@ -29,6 +31,25 @@ struct ParseableInterfaceOptions {
2931
std::string ParseableInterfaceFlags;
3032
};
3133

34+
class PrebuiltModuleCache {
35+
StringRef cacheDir;
36+
llvm::DenseMap<StringRef, uint64_t> hashedContents;
37+
PrebuiltModuleCache(StringRef cacheDir): cacheDir(cacheDir) {}
38+
39+
std::error_code loadHashes();
40+
public:
41+
static llvm::ErrorOr<PrebuiltModuleCache>
42+
loadFromDirectory(StringRef directory);
43+
44+
StringRef getDirectory() { return cacheDir; }
45+
46+
Optional<uint64_t> getInterfaceHash(StringRef moduleName) {
47+
auto it = hashedContents.find(moduleName);
48+
if (it == hashedContents.end()) return None;
49+
return it->second;
50+
}
51+
};
52+
3253
llvm::Regex getSwiftInterfaceFormatVersionRegex();
3354
llvm::Regex getSwiftInterfaceModuleFlagsRegex();
3455

lib/Frontend/ParseableInterfaceSupport.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,12 @@ static std::string getCacheHash(ASTContext &Ctx,
134134
// pathname, and probably all we can get from the VFS in this regard anyways.
135135
H = hash_combine(H, InPath);
136136

137-
// Include the target CPU. In practice, .swiftinterface files will be in
138-
// architecture-specific subdirectories and would have target-specific pieces
139-
// #if'd out. However, it doesn't hurt to include it, and it guards against
140-
// mistakenly reusing cached modules across targets.
141-
H = hash_combine(H, SubInvocation.getTargetTriple());
137+
// Include the target CPU architecture. In practice, .swiftinterface files
138+
// will be in architecture-specific subdirectories and would have
139+
// architecture-specific pieces #if'd out. However, it doesn't hurt to
140+
// include it, and it guards against mistakenly reusing cached modules across
141+
// architectures.
142+
H = hash_combine(H, SubInvocation.getLangOptions().Target.getArchName());
142143

143144
// The SDK path is going to affect how this module is imported, so include it.
144145
H = hash_combine(H, SubInvocation.getSDKPath());
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This test specifically uses macOS deployment targets
2+
// REQUIRES: OS=macosx
3+
//
4+
// RUN: %empty-directory(%t)
5+
//
6+
// Test will build a module TestModule that depends on OtherModule and LeafModule (built from other.swift and leaf.swift).
7+
//
8+
// RUN: echo 'public func LeafFunc() -> Int { return 10; }' >%t/leaf.swift
9+
//
10+
// RUN: echo 'import LeafModule' >%t/other.swift
11+
// RUN: echo 'public func OtherFunc() -> Int { return LeafFunc(); }' >>%t/other.swift
12+
//
13+
// Phase 1: build LeafModule into a .swiftinterface file with -target x86_64-macosx-10.9:
14+
//
15+
// RUN: %swift -target x86_64-apple-macosx10.9 -I %t -module-cache-path %t/modulecache -emit-parseable-module-interface-path %t/LeafModule.swiftinterface -module-name LeafModule %t/leaf.swift -typecheck
16+
//
17+
// Phase 2: build OtherModule into a .swiftinterface file with -target x86_64-macosx-10.10:
18+
//
19+
// RUN: %swift -target x86_64-apple-macosx10.10 -I %t -module-cache-path %t/modulecache -emit-parseable-module-interface-path %t/OtherModule.swiftinterface -module-name OtherModule %t/other.swift -enable-parseable-module-interface -typecheck
20+
//
21+
// Phase 3: build TestModule in -target x86_64-apple-macosx10.11 and import both of these:
22+
//
23+
// RUN: %swift -target x86_64-apple-macosx10.11 -I %t -module-cache-path %t/modulecache -module-name TestModule %s -enable-parseable-module-interface -typecheck
24+
//
25+
// Phase 4: make sure we only compiled LeafModule and OtherModule one time:
26+
//
27+
// RUN: NUM_LEAF_MODULES=$(find %t/modulecache -type f -name 'LeafModule-*.swiftmodule' | wc -l)
28+
// RUN: NUM_OTHER_MODULES=$(find %t/modulecache -type f -name 'OtherModule-*.swiftmodule' | wc -l)
29+
// RUN: if [ ! $NUM_LEAF_MODULES -eq 1 ]; then echo "Should only be 1 LeafModule, found $NUM_LEAF_MODULES"; exit 1; fi
30+
// RUN: if [ ! $NUM_OTHER_MODULES -eq 1 ]; then echo "Should only be 1 OtherModule, found $NUM_OTHER_MODULES"; exit 1; fi
31+
import LeafModule
32+
import OtherModule

0 commit comments

Comments
 (0)