Skip to content

Commit 188c14c

Browse files
committed
More idiomatic use of llvm::hash_combine in many places
- No need to hash input values first - Pass many values to a single hash_combine to save on intermediates - Use hash_combine_range instead of a loop of hash_combines No functionality change.
1 parent bb4f46f commit 188c14c

File tree

11 files changed

+94
-97
lines changed

11 files changed

+94
-97
lines changed

include/swift/AST/AnyRequest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class AnyRequest {
5454
friend llvm::DenseMapInfo<swift::AnyRequest>;
5555

5656
static hash_code hashForHolder(uint64_t typeID, hash_code requestHash) {
57-
return hash_combine(hash_value(typeID), requestHash);
57+
return hash_combine(typeID, requestHash);
5858
}
5959

6060
/// Abstract base class used to hold the specific request kind.

include/swift/AST/SearchPathOptions.h

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_AST_SEARCHPATHOPTIONS_H
1414
#define SWIFT_AST_SEARCHPATHOPTIONS_H
1515

16+
#include "swift/Basic/ArrayRefView.h"
1617
#include "llvm/ADT/Hashing.h"
1718

1819
#include <string>
@@ -81,30 +82,38 @@ class SearchPathOptions {
8182
/// would for a non-system header.
8283
bool DisableModulesValidateSystemDependencies = false;
8384

85+
private:
86+
static StringRef
87+
pathStringFromFrameworkSearchPath(const FrameworkSearchPath &next) {
88+
return next.Path;
89+
};
90+
91+
public:
8492
/// Return a hash code of any components from these options that should
8593
/// contribute to a Swift Bridging PCH hash.
8694
llvm::hash_code getPCHHashComponents() const {
87-
using llvm::hash_value;
8895
using llvm::hash_combine;
89-
auto Code = hash_value(SDKPath);
90-
for (auto Import : ImportSearchPaths) {
91-
Code = hash_combine(Code, Import);
92-
}
93-
for (auto VFSFile : VFSOverlayFiles) {
94-
Code = hash_combine(Code, VFSFile);
95-
}
96-
for (const auto &FrameworkPath : FrameworkSearchPaths) {
97-
Code = hash_combine(Code, FrameworkPath.Path);
98-
}
99-
for (auto LibraryPath : LibrarySearchPaths) {
100-
Code = hash_combine(Code, LibraryPath);
101-
}
102-
Code = hash_combine(Code, RuntimeResourcePath);
103-
for (auto RuntimeLibraryImportPath : RuntimeLibraryImportPaths) {
104-
Code = hash_combine(Code, RuntimeLibraryImportPath);
105-
}
106-
Code = hash_combine(Code, DisableModulesValidateSystemDependencies);
107-
return Code;
96+
using llvm::hash_combine_range;
97+
98+
using FrameworkPathView = ArrayRefView<FrameworkSearchPath, StringRef,
99+
pathStringFromFrameworkSearchPath>;
100+
FrameworkPathView frameworkPathsOnly{FrameworkSearchPaths};
101+
102+
return hash_combine(SDKPath,
103+
hash_combine_range(ImportSearchPaths.begin(),
104+
ImportSearchPaths.end()),
105+
hash_combine_range(VFSOverlayFiles.begin(),
106+
VFSOverlayFiles.end()),
107+
// FIXME: Should we include the system-ness of framework
108+
// search paths too?
109+
hash_combine_range(frameworkPathsOnly.begin(),
110+
frameworkPathsOnly.end()),
111+
hash_combine_range(LibrarySearchPaths.begin(),
112+
LibrarySearchPaths.end()),
113+
RuntimeResourcePath,
114+
hash_combine_range(RuntimeLibraryImportPaths.begin(),
115+
RuntimeLibraryImportPaths.end()),
116+
DisableModulesValidateSystemDependencies);
108117
}
109118
};
110119

include/swift/AST/TypeCheckRequests.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,7 @@ struct WhereClauseOwner {
380380
SourceLoc getLoc() const;
381381

382382
friend hash_code hash_value(const WhereClauseOwner &owner) {
383-
return hash_combine(hash_value(owner.dc),
384-
hash_value(owner.source.getOpaqueValue()));
383+
return llvm::hash_combine(owner.dc, owner.source.getOpaqueValue());
385384
}
386385

387386
friend bool operator==(const WhereClauseOwner &lhs,

include/swift/AST/TypeLoc.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ struct TypeLoc {
6868
TypeLoc clone(ASTContext &ctx) const;
6969

7070
friend llvm::hash_code hash_value(const TypeLoc &owner) {
71-
return hash_combine(llvm::hash_value(owner.Ty.getPointer()),
72-
llvm::hash_value(owner.TyR));
71+
return llvm::hash_combine(owner.Ty.getPointer(), owner.TyR);
7372
}
7473

7574
friend bool operator==(const TypeLoc &lhs,

include/swift/Basic/LangOptions.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,12 +441,10 @@ namespace swift {
441441
/// Return a hash code of any components from these options that should
442442
/// contribute to a Swift Bridging PCH hash.
443443
llvm::hash_code getPCHHashComponents() const {
444-
auto code = llvm::hash_value(Target.str());
445444
SmallString<16> Scratch;
446445
llvm::raw_svector_ostream OS(Scratch);
447446
OS << EffectiveLanguageVersion;
448-
code = llvm::hash_combine(code, OS.str());
449-
return code;
447+
return llvm::hash_combine(Target.str(), OS.str());
450448
}
451449

452450
private:

include/swift/Basic/SourceLoc.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,8 @@ template <> struct DenseMapInfo<swift::SourceRange> {
265265
}
266266

267267
static unsigned getHashValue(const swift::SourceRange &Val) {
268-
return hash_combine(DenseMapInfo<const void *>::getHashValue(
269-
Val.Start.getOpaquePointerValue()),
270-
DenseMapInfo<const void *>::getHashValue(
271-
Val.End.getOpaquePointerValue()));
268+
return hash_combine(Val.Start.getOpaquePointerValue(),
269+
Val.End.getOpaquePointerValue());
272270
}
273271

274272
static bool isEqual(const swift::SourceRange &LHS,

include/swift/ClangImporter/ClangImporterOptions.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,21 @@ class ClangImporterOptions {
9999
/// Return a hash code of any components from these options that should
100100
/// contribute to a Swift Bridging PCH hash.
101101
llvm::hash_code getPCHHashComponents() const {
102-
using llvm::hash_value;
103102
using llvm::hash_combine;
104-
105-
auto Code = hash_value(ModuleCachePath);
106-
Code = hash_combine(Code, llvm::hash_combine_range(ExtraArgs.begin(),
107-
ExtraArgs.end()));
108-
Code = hash_combine(Code, OverrideResourceDir);
109-
Code = hash_combine(Code, TargetCPU);
110-
Code = hash_combine(Code, BridgingHeader);
111-
Code = hash_combine(Code, PrecompiledHeaderOutputDir);
112-
Code = hash_combine(Code, static_cast<uint8_t>(Mode));
113-
Code = hash_combine(Code, DetailedPreprocessingRecord);
114-
Code = hash_combine(Code, ImportForwardDeclarations);
115-
Code = hash_combine(Code, InferImportAsMember);
116-
Code = hash_combine(Code, DisableSwiftBridgeAttr);
117-
Code = hash_combine(Code, DisableOverlayModules);
118-
return Code;
103+
using llvm::hash_combine_range;
104+
105+
return hash_combine(ModuleCachePath,
106+
hash_combine_range(ExtraArgs.begin(), ExtraArgs.end()),
107+
OverrideResourceDir,
108+
TargetCPU,
109+
BridgingHeader,
110+
PrecompiledHeaderOutputDir,
111+
static_cast<uint8_t>(Mode),
112+
DetailedPreprocessingRecord,
113+
ImportForwardDeclarations,
114+
InferImportAsMember,
115+
DisableSwiftBridgeAttr,
116+
DisableOverlayModules);
119117
}
120118
};
121119

include/swift/IDE/IDERequests.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ struct CursorInfoOwner {
3838
CursorInfoOwner(SourceFile *File, SourceLoc Loc): File(File), Loc(Loc) { }
3939

4040
friend llvm::hash_code hash_value(const CursorInfoOwner &CI) {
41-
return hash_combine(hash_value(CI.File),
42-
hash_value(CI.Loc.getOpaquePointerValue()));
41+
return llvm::hash_combine(CI.File, CI.Loc.getOpaquePointerValue());
4342
}
4443

4544
friend bool operator==(const CursorInfoOwner &lhs, const CursorInfoOwner &rhs) {
@@ -97,9 +96,9 @@ struct RangeInfoOwner {
9796
RangeInfoOwner(SourceFile *File, unsigned Offset, unsigned Length);
9897

9998
friend llvm::hash_code hash_value(const RangeInfoOwner &CI) {
100-
return hash_combine(hash_value(CI.File),
101-
hash_value(CI.StartLoc.getOpaquePointerValue()),
102-
hash_value(CI.EndLoc.getOpaquePointerValue()));
99+
return llvm::hash_combine(CI.File,
100+
CI.StartLoc.getOpaquePointerValue(),
101+
CI.EndLoc.getOpaquePointerValue());
103102
}
104103

105104
friend bool operator==(const RangeInfoOwner &lhs, const RangeInfoOwner &rhs) {
@@ -183,9 +182,9 @@ struct OverridenDeclsOwner {
183182
Transitive(Transitive) {}
184183

185184
friend llvm::hash_code hash_value(const OverridenDeclsOwner &CI) {
186-
return hash_combine(hash_value(CI.VD),
187-
hash_value(CI.IncludeProtocolRequirements),
188-
hash_value(CI.Transitive));
185+
return llvm::hash_combine(CI.VD,
186+
CI.IncludeProtocolRequirements,
187+
CI.Transitive);
189188
}
190189

191190
friend bool operator==(const OverridenDeclsOwner &lhs,

include/swift/Sema/IDETypeCheckingRequests.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ struct DeclApplicabilityOwner {
3838
DC(DC), Ty(Ty), ExtensionOrMember(VD) {}
3939

4040
friend llvm::hash_code hash_value(const DeclApplicabilityOwner &CI) {
41-
return hash_combine(hash_value(CI.Ty.getPointer()),
42-
hash_value(CI.ExtensionOrMember));
41+
return llvm::hash_combine(CI.Ty.getPointer(), CI.ExtensionOrMember);
4342
}
4443

4544
friend bool operator==(const DeclApplicabilityOwner &lhs,
@@ -96,8 +95,8 @@ struct TypePair {
9695
TypePair(Type FirstTy, Type SecondTy): FirstTy(FirstTy), SecondTy(SecondTy) {}
9796
TypePair(): TypePair(Type(), Type()) {}
9897
friend llvm::hash_code hash_value(const TypePair &TI) {
99-
return hash_combine(hash_value(TI.FirstTy.getPointer()),
100-
hash_value(TI.SecondTy.getPointer()));
98+
return llvm::hash_combine(TI.FirstTy.getPointer(),
99+
TI.SecondTy.getPointer());
101100
}
102101

103102
friend bool operator==(const TypePair &lhs,
@@ -133,9 +132,7 @@ struct TypeRelationCheckInput {
133132
OpenArchetypes(OpenArchetypes) {}
134133

135134
friend llvm::hash_code hash_value(const TypeRelationCheckInput &TI) {
136-
return hash_combine(hash_value(TI.Pair),
137-
hash_value(TI.Relation),
138-
hash_value(TI.OpenArchetypes));
135+
return llvm::hash_combine(TI.Pair, TI.Relation, TI.OpenArchetypes);
139136
}
140137

141138
friend bool operator==(const TypeRelationCheckInput &lhs,

lib/Frontend/Frontend.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,15 @@ CompilerInstance::CompilerInstance() = default;
4646
CompilerInstance::~CompilerInstance() = default;
4747

4848
std::string CompilerInvocation::getPCHHash() const {
49-
using llvm::hash_code;
50-
using llvm::hash_value;
5149
using llvm::hash_combine;
5250

53-
auto Code = hash_value(LangOpts.getPCHHashComponents());
54-
Code = hash_combine(Code, FrontendOpts.getPCHHashComponents());
55-
Code = hash_combine(Code, ClangImporterOpts.getPCHHashComponents());
56-
Code = hash_combine(Code, SearchPathOpts.getPCHHashComponents());
57-
Code = hash_combine(Code, DiagnosticOpts.getPCHHashComponents());
58-
Code = hash_combine(Code, SILOpts.getPCHHashComponents());
59-
Code = hash_combine(Code, IRGenOpts.getPCHHashComponents());
51+
auto Code = hash_combine(LangOpts.getPCHHashComponents(),
52+
FrontendOpts.getPCHHashComponents(),
53+
ClangImporterOpts.getPCHHashComponents(),
54+
SearchPathOpts.getPCHHashComponents(),
55+
DiagnosticOpts.getPCHHashComponents(),
56+
SILOpts.getPCHHashComponents(),
57+
IRGenOpts.getPCHHashComponents());
6058

6159
return llvm::APInt(64, Code).toString(36, /*Signed=*/false);
6260
}

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -380,34 +380,36 @@ class ModuleInterfaceLoaderImpl {
380380
/// with dead entries -- when other factors change, such as the contents of
381381
/// the .swiftinterface input or its dependencies.
382382
std::string getCacheHash(const CompilerInvocation &SubInvocation) {
383-
// Start with the compiler version (which will be either tag names or revs).
384-
// Explicitly don't pass in the "effective" language version -- this would
385-
// mean modules built in different -swift-version modes would rebuild their
386-
// dependencies.
387-
llvm::hash_code H = hash_value(swift::version::getSwiftFullVersion());
388-
389-
// Simplest representation of input "identity" (not content) is just a
390-
// pathname, and probably all we can get from the VFS in this regard
391-
// anyways.
392-
H = hash_combine(H, interfacePath);
393-
394-
// Include the normalized target triple. In practice, .swiftinterface files
395-
// will be in target-specific subdirectories and would have
396-
// target-specific pieces #if'd out. However, it doesn't hurt to
397-
// include it, and it guards against mistakenly reusing cached modules
398-
// across targets. Note that this normalization explicitly doesn't
399-
// include the minimum deployment target (e.g. the '12.0' in 'ios12.0').
400383
auto normalizedTargetTriple =
401384
getTargetSpecificModuleTriple(SubInvocation.getLangOptions().Target);
402-
H = hash_combine(H, normalizedTargetTriple.str());
403385

404-
// The SDK path is going to affect how this module is imported, so include
405-
// it.
406-
H = hash_combine(H, SubInvocation.getSDKPath());
407-
408-
// Whether or not we're tracking system dependencies affects the
409-
// invalidation behavior of this cache item.
410-
H = hash_combine(H, SubInvocation.getFrontendOptions().TrackSystemDeps);
386+
llvm::hash_code H = hash_combine(
387+
// Start with the compiler version (which will be either tag names or
388+
// revs). Explicitly don't pass in the "effective" language version --
389+
// this would mean modules built in different -swift-version modes would
390+
// rebuild their dependencies.
391+
swift::version::getSwiftFullVersion(),
392+
393+
// Simplest representation of input "identity" (not content) is just a
394+
// pathname, and probably all we can get from the VFS in this regard
395+
// anyways.
396+
interfacePath,
397+
398+
// Include the normalized target triple. In practice, .swiftinterface
399+
// files will be in target-specific subdirectories and would have
400+
// target-specific pieces #if'd out. However, it doesn't hurt to include
401+
// it, and it guards against mistakenly reusing cached modules across
402+
// targets. Note that this normalization explicitly doesn't include the
403+
// minimum deployment target (e.g. the '12.0' in 'ios12.0').
404+
normalizedTargetTriple.str(),
405+
406+
// The SDK path is going to affect how this module is imported, so
407+
// include it.
408+
SubInvocation.getSDKPath(),
409+
410+
// Whether or not we're tracking system dependencies affects the
411+
// invalidation behavior of this cache item.
412+
SubInvocation.getFrontendOptions().TrackSystemDeps);
411413

412414
return llvm::APInt(64, H).toString(36, /*Signed=*/false);
413415
}

0 commit comments

Comments
 (0)