Skip to content

More idiomatic use of llvm::hash_combine in many places #27497

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
Oct 4, 2019
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
2 changes: 1 addition & 1 deletion include/swift/AST/AnyRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class AnyRequest {
friend llvm::DenseMapInfo<swift::AnyRequest>;

static hash_code hashForHolder(uint64_t typeID, hash_code requestHash) {
return hash_combine(hash_value(typeID), requestHash);
return hash_combine(typeID, requestHash);
}

/// Abstract base class used to hold the specific request kind.
Expand Down
49 changes: 29 additions & 20 deletions include/swift/AST/SearchPathOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef SWIFT_AST_SEARCHPATHOPTIONS_H
#define SWIFT_AST_SEARCHPATHOPTIONS_H

#include "swift/Basic/ArrayRefView.h"
#include "llvm/ADT/Hashing.h"

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

private:
static StringRef
pathStringFromFrameworkSearchPath(const FrameworkSearchPath &next) {
return next.Path;
};

public:
/// Return a hash code of any components from these options that should
/// contribute to a Swift Bridging PCH hash.
llvm::hash_code getPCHHashComponents() const {
using llvm::hash_value;
using llvm::hash_combine;
auto Code = hash_value(SDKPath);
for (auto Import : ImportSearchPaths) {
Code = hash_combine(Code, Import);
}
for (auto VFSFile : VFSOverlayFiles) {
Code = hash_combine(Code, VFSFile);
}
for (const auto &FrameworkPath : FrameworkSearchPaths) {
Code = hash_combine(Code, FrameworkPath.Path);
}
for (auto LibraryPath : LibrarySearchPaths) {
Code = hash_combine(Code, LibraryPath);
}
Code = hash_combine(Code, RuntimeResourcePath);
for (auto RuntimeLibraryImportPath : RuntimeLibraryImportPaths) {
Code = hash_combine(Code, RuntimeLibraryImportPath);
}
Code = hash_combine(Code, DisableModulesValidateSystemDependencies);
return Code;
using llvm::hash_combine_range;

using FrameworkPathView = ArrayRefView<FrameworkSearchPath, StringRef,
pathStringFromFrameworkSearchPath>;
FrameworkPathView frameworkPathsOnly{FrameworkSearchPaths};

return hash_combine(SDKPath,
hash_combine_range(ImportSearchPaths.begin(),
ImportSearchPaths.end()),
hash_combine_range(VFSOverlayFiles.begin(),
VFSOverlayFiles.end()),
// FIXME: Should we include the system-ness of framework
// search paths too?
hash_combine_range(frameworkPathsOnly.begin(),
frameworkPathsOnly.end()),
hash_combine_range(LibrarySearchPaths.begin(),
LibrarySearchPaths.end()),
RuntimeResourcePath,
hash_combine_range(RuntimeLibraryImportPaths.begin(),
RuntimeLibraryImportPaths.end()),
DisableModulesValidateSystemDependencies);
}
};

Expand Down
3 changes: 1 addition & 2 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,7 @@ struct WhereClauseOwner {
SourceLoc getLoc() const;

friend hash_code hash_value(const WhereClauseOwner &owner) {
return hash_combine(hash_value(owner.dc),
hash_value(owner.source.getOpaqueValue()));
return llvm::hash_combine(owner.dc, owner.source.getOpaqueValue());
}

friend bool operator==(const WhereClauseOwner &lhs,
Expand Down
3 changes: 1 addition & 2 deletions include/swift/AST/TypeLoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ struct TypeLoc {
TypeLoc clone(ASTContext &ctx) const;

friend llvm::hash_code hash_value(const TypeLoc &owner) {
return hash_combine(llvm::hash_value(owner.Ty.getPointer()),
llvm::hash_value(owner.TyR));
return llvm::hash_combine(owner.Ty.getPointer(), owner.TyR);
}

friend bool operator==(const TypeLoc &lhs,
Expand Down
4 changes: 1 addition & 3 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,10 @@ namespace swift {
/// Return a hash code of any components from these options that should
/// contribute to a Swift Bridging PCH hash.
llvm::hash_code getPCHHashComponents() const {
auto code = llvm::hash_value(Target.str());
SmallString<16> Scratch;
llvm::raw_svector_ostream OS(Scratch);
OS << EffectiveLanguageVersion;
code = llvm::hash_combine(code, OS.str());
return code;
return llvm::hash_combine(Target.str(), OS.str());
}

private:
Expand Down
6 changes: 2 additions & 4 deletions include/swift/Basic/SourceLoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,8 @@ template <> struct DenseMapInfo<swift::SourceRange> {
}

static unsigned getHashValue(const swift::SourceRange &Val) {
return hash_combine(DenseMapInfo<const void *>::getHashValue(
Val.Start.getOpaquePointerValue()),
DenseMapInfo<const void *>::getHashValue(
Val.End.getOpaquePointerValue()));
return hash_combine(Val.Start.getOpaquePointerValue(),
Val.End.getOpaquePointerValue());
}

static bool isEqual(const swift::SourceRange &LHS,
Expand Down
30 changes: 14 additions & 16 deletions include/swift/ClangImporter/ClangImporterOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,21 @@ class ClangImporterOptions {
/// Return a hash code of any components from these options that should
/// contribute to a Swift Bridging PCH hash.
llvm::hash_code getPCHHashComponents() const {
using llvm::hash_value;
using llvm::hash_combine;

auto Code = hash_value(ModuleCachePath);
Code = hash_combine(Code, llvm::hash_combine_range(ExtraArgs.begin(),
ExtraArgs.end()));
Code = hash_combine(Code, OverrideResourceDir);
Code = hash_combine(Code, TargetCPU);
Code = hash_combine(Code, BridgingHeader);
Code = hash_combine(Code, PrecompiledHeaderOutputDir);
Code = hash_combine(Code, static_cast<uint8_t>(Mode));
Code = hash_combine(Code, DetailedPreprocessingRecord);
Code = hash_combine(Code, ImportForwardDeclarations);
Code = hash_combine(Code, InferImportAsMember);
Code = hash_combine(Code, DisableSwiftBridgeAttr);
Code = hash_combine(Code, DisableOverlayModules);
return Code;
using llvm::hash_combine_range;

return hash_combine(ModuleCachePath,
hash_combine_range(ExtraArgs.begin(), ExtraArgs.end()),
OverrideResourceDir,
TargetCPU,
BridgingHeader,
PrecompiledHeaderOutputDir,
static_cast<uint8_t>(Mode),
DetailedPreprocessingRecord,
ImportForwardDeclarations,
InferImportAsMember,
DisableSwiftBridgeAttr,
DisableOverlayModules);
}
};

Expand Down
15 changes: 7 additions & 8 deletions include/swift/IDE/IDERequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ struct CursorInfoOwner {
CursorInfoOwner(SourceFile *File, SourceLoc Loc): File(File), Loc(Loc) { }

friend llvm::hash_code hash_value(const CursorInfoOwner &CI) {
return hash_combine(hash_value(CI.File),
hash_value(CI.Loc.getOpaquePointerValue()));
return llvm::hash_combine(CI.File, CI.Loc.getOpaquePointerValue());
}

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

friend llvm::hash_code hash_value(const RangeInfoOwner &CI) {
return hash_combine(hash_value(CI.File),
hash_value(CI.StartLoc.getOpaquePointerValue()),
hash_value(CI.EndLoc.getOpaquePointerValue()));
return llvm::hash_combine(CI.File,
CI.StartLoc.getOpaquePointerValue(),
CI.EndLoc.getOpaquePointerValue());
}

friend bool operator==(const RangeInfoOwner &lhs, const RangeInfoOwner &rhs) {
Expand Down Expand Up @@ -183,9 +182,9 @@ struct OverridenDeclsOwner {
Transitive(Transitive) {}

friend llvm::hash_code hash_value(const OverridenDeclsOwner &CI) {
return hash_combine(hash_value(CI.VD),
hash_value(CI.IncludeProtocolRequirements),
hash_value(CI.Transitive));
return llvm::hash_combine(CI.VD,
CI.IncludeProtocolRequirements,
CI.Transitive);
}

friend bool operator==(const OverridenDeclsOwner &lhs,
Expand Down
11 changes: 4 additions & 7 deletions include/swift/Sema/IDETypeCheckingRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ struct DeclApplicabilityOwner {
DC(DC), Ty(Ty), ExtensionOrMember(VD) {}

friend llvm::hash_code hash_value(const DeclApplicabilityOwner &CI) {
return hash_combine(hash_value(CI.Ty.getPointer()),
hash_value(CI.ExtensionOrMember));
return llvm::hash_combine(CI.Ty.getPointer(), CI.ExtensionOrMember);
}

friend bool operator==(const DeclApplicabilityOwner &lhs,
Expand Down Expand Up @@ -96,8 +95,8 @@ struct TypePair {
TypePair(Type FirstTy, Type SecondTy): FirstTy(FirstTy), SecondTy(SecondTy) {}
TypePair(): TypePair(Type(), Type()) {}
friend llvm::hash_code hash_value(const TypePair &TI) {
return hash_combine(hash_value(TI.FirstTy.getPointer()),
hash_value(TI.SecondTy.getPointer()));
return llvm::hash_combine(TI.FirstTy.getPointer(),
TI.SecondTy.getPointer());
}

friend bool operator==(const TypePair &lhs,
Expand Down Expand Up @@ -133,9 +132,7 @@ struct TypeRelationCheckInput {
OpenArchetypes(OpenArchetypes) {}

friend llvm::hash_code hash_value(const TypeRelationCheckInput &TI) {
return hash_combine(hash_value(TI.Pair),
hash_value(TI.Relation),
hash_value(TI.OpenArchetypes));
return llvm::hash_combine(TI.Pair, TI.Relation, TI.OpenArchetypes);
}

friend bool operator==(const TypeRelationCheckInput &lhs,
Expand Down
16 changes: 7 additions & 9 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,15 @@ CompilerInstance::CompilerInstance() = default;
CompilerInstance::~CompilerInstance() = default;

std::string CompilerInvocation::getPCHHash() const {
using llvm::hash_code;
using llvm::hash_value;
using llvm::hash_combine;

auto Code = hash_value(LangOpts.getPCHHashComponents());
Code = hash_combine(Code, FrontendOpts.getPCHHashComponents());
Code = hash_combine(Code, ClangImporterOpts.getPCHHashComponents());
Code = hash_combine(Code, SearchPathOpts.getPCHHashComponents());
Code = hash_combine(Code, DiagnosticOpts.getPCHHashComponents());
Code = hash_combine(Code, SILOpts.getPCHHashComponents());
Code = hash_combine(Code, IRGenOpts.getPCHHashComponents());
auto Code = hash_combine(LangOpts.getPCHHashComponents(),
FrontendOpts.getPCHHashComponents(),
ClangImporterOpts.getPCHHashComponents(),
SearchPathOpts.getPCHHashComponents(),
DiagnosticOpts.getPCHHashComponents(),
SILOpts.getPCHHashComponents(),
IRGenOpts.getPCHHashComponents());

return llvm::APInt(64, Code).toString(36, /*Signed=*/false);
}
Expand Down
52 changes: 27 additions & 25 deletions lib/Frontend/ModuleInterfaceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,34 +380,36 @@ class ModuleInterfaceLoaderImpl {
/// with dead entries -- when other factors change, such as the contents of
/// the .swiftinterface input or its dependencies.
std::string getCacheHash(const CompilerInvocation &SubInvocation) {
// Start with the compiler version (which will be either tag names or revs).
// Explicitly don't pass in the "effective" language version -- this would
// mean modules built in different -swift-version modes would rebuild their
// dependencies.
llvm::hash_code H = hash_value(swift::version::getSwiftFullVersion());

// Simplest representation of input "identity" (not content) is just a
// pathname, and probably all we can get from the VFS in this regard
// anyways.
H = hash_combine(H, interfacePath);

// Include the normalized target triple. In practice, .swiftinterface files
// will be in target-specific subdirectories and would have
// target-specific pieces #if'd out. However, it doesn't hurt to
// include it, and it guards against mistakenly reusing cached modules
// across targets. Note that this normalization explicitly doesn't
// include the minimum deployment target (e.g. the '12.0' in 'ios12.0').
auto normalizedTargetTriple =
getTargetSpecificModuleTriple(SubInvocation.getLangOptions().Target);
H = hash_combine(H, normalizedTargetTriple.str());

// The SDK path is going to affect how this module is imported, so include
// it.
H = hash_combine(H, SubInvocation.getSDKPath());

// Whether or not we're tracking system dependencies affects the
// invalidation behavior of this cache item.
H = hash_combine(H, SubInvocation.getFrontendOptions().TrackSystemDeps);
llvm::hash_code H = hash_combine(
// Start with the compiler version (which will be either tag names or
// revs). Explicitly don't pass in the "effective" language version --
// this would mean modules built in different -swift-version modes would
// rebuild their dependencies.
swift::version::getSwiftFullVersion(),

// Simplest representation of input "identity" (not content) is just a
// pathname, and probably all we can get from the VFS in this regard
// anyways.
interfacePath,

// Include the normalized target triple. In practice, .swiftinterface
// files will be in target-specific subdirectories and would have
// target-specific pieces #if'd out. However, it doesn't hurt to include
// it, and it guards against mistakenly reusing cached modules across
// targets. Note that this normalization explicitly doesn't include the
// minimum deployment target (e.g. the '12.0' in 'ios12.0').
normalizedTargetTriple.str(),

// The SDK path is going to affect how this module is imported, so
// include it.
SubInvocation.getSDKPath(),

// Whether or not we're tracking system dependencies affects the
// invalidation behavior of this cache item.
SubInvocation.getFrontendOptions().TrackSystemDeps);

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