Skip to content

Commit

Permalink
Convert DenseMap to use value-based hashing and comparison.
Browse files Browse the repository at this point in the history
Add some more implementations.
Second attempt to get rid of warning.
  • Loading branch information
schweitzpgi committed Oct 27, 2021
1 parent efea0c9 commit f72c975
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
39 changes: 26 additions & 13 deletions flang/lib/Lower/IterationSpace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class HashEvaluateExpr {
unsigned args = 13u;
for (const auto &v : x.arguments())
args -= getHashValue(v);
return getHashValue(x.proc()) * 101u;
return getHashValue(x.proc()) * 101u - args;
}
template <typename A>
static unsigned
Expand All @@ -217,16 +217,15 @@ class HashEvaluateExpr {
return 127u;
}
static unsigned getHashValue(const Fortran::evaluate::ImpliedDoIndex &x) {
// FIXME: hash the contents.
return 131u;
return llvm::hash_value(toStringRef(x.name).str()) * 131u;
}
static unsigned getHashValue(const Fortran::evaluate::TypeParamInquiry &x) {
// FIXME: hash the contents.
return 137u;
return getHashValue(x.base()) * 137u - getHashValue(x.parameter()) * 3u;
}
static unsigned getHashValue(const Fortran::evaluate::DescriptorInquiry &x) {
// FIXME: hash the contents.
return 139u;
return getHashValue(x.base()) * 139u -
static_cast<unsigned>(x.field()) * 13u +
static_cast<unsigned>(x.dimension());
}
static unsigned
getHashValue(const Fortran::evaluate::StructureConstructor &x) {
Expand Down Expand Up @@ -280,6 +279,10 @@ unsigned Fortran::lower::getHashValue(
[&](const auto *p) { return HashEvaluateExpr::getHashValue(*p); }, x);
}

unsigned Fortran::lower::getHashValue(Fortran::lower::FrontEndExpr x) {
return HashEvaluateExpr::getHashValue(*x);
}

namespace {
// Define the is equals test for using Fortran::evaluate::Expr values with
// llvm::DenseMap.
Expand Down Expand Up @@ -464,9 +467,8 @@ class IsEqualEvaluateExpr {
return isEqual(*xs, *ys);
return false;
}
if ([[maybe_unused]] const auto *ys = y.GetAssumedTypeDummy())
return false;
return isEqual(*x.UnwrapExpr(), *y.UnwrapExpr());
return !y.GetAssumedTypeDummy() &&
isEqual(*x.UnwrapExpr(), *y.UnwrapExpr());
}
static bool isEqual(const Fortran::evaluate::ProcedureDesignator &x,
const Fortran::evaluate::ProcedureDesignator &y) {
Expand All @@ -484,15 +486,16 @@ class IsEqualEvaluateExpr {
}
static bool isEqual(const Fortran::evaluate::ImpliedDoIndex &x,
const Fortran::evaluate::ImpliedDoIndex &y) {
llvm::report_fatal_error("not implemented");
return toStringRef(x.name) == toStringRef(y.name);
}
static bool isEqual(const Fortran::evaluate::TypeParamInquiry &x,
const Fortran::evaluate::TypeParamInquiry &y) {
llvm::report_fatal_error("not implemented");
return isEqual(x.base(), y.base()) && isEqual(x.parameter(), y.parameter());
}
static bool isEqual(const Fortran::evaluate::DescriptorInquiry &x,
const Fortran::evaluate::DescriptorInquiry &y) {
llvm::report_fatal_error("not implemented");
return isEqual(x.base(), y.base()) && x.field() == y.field() &&
x.dimension() == y.dimension();
}
static bool isEqual(const Fortran::evaluate::StructureConstructor &x,
const Fortran::evaluate::StructureConstructor &y) {
Expand Down Expand Up @@ -572,6 +575,16 @@ bool Fortran::lower::isEqual(
x, y);
}

bool Fortran::lower::isEqual(Fortran::lower::FrontEndExpr x,
Fortran::lower::FrontEndExpr y) {
auto empty = llvm::DenseMapInfo<Fortran::lower::FrontEndExpr>::getEmptyKey();
auto tombstone =
llvm::DenseMapInfo<Fortran::lower::FrontEndExpr>::getTombstoneKey();
if (x == empty || y == empty || x == tombstone || y == tombstone)
return x == y;
return x == y || IsEqualEvaluateExpr::isEqual(*x, *y);
}

namespace {

/// This class can recover the base array in an expression that contains
Expand Down
28 changes: 26 additions & 2 deletions flang/lib/Lower/IterationSpace.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ using FrontEndSymbol = const semantics::Symbol *;

class AbstractConverter;

unsigned getHashValue(FrontEndExpr x);
bool isEqual(FrontEndExpr x, FrontEndExpr y);
} // namespace lower
} // namespace Fortran

namespace llvm {
template <>
struct DenseMapInfo<Fortran::lower::FrontEndExpr> {
static inline Fortran::lower::FrontEndExpr getEmptyKey() {
return reinterpret_cast<Fortran::lower::FrontEndExpr>(~0);
}
static inline Fortran::lower::FrontEndExpr getTombstoneKey() {
return reinterpret_cast<Fortran::lower::FrontEndExpr>(~0 - 1);
}
static unsigned getHashValue(Fortran::lower::FrontEndExpr v) {
return Fortran::lower::getHashValue(v);
}
static bool isEqual(Fortran::lower::FrontEndExpr lhs,
Fortran::lower::FrontEndExpr rhs) {
return Fortran::lower::isEqual(lhs, rhs);
}
};
} // namespace llvm

namespace Fortran::lower {
template <typename A>
class StackableConstructExpr {
public:
Expand Down Expand Up @@ -190,8 +215,7 @@ unsigned getHashValue(const ExplicitSpaceArrayBases &x);
bool isEqual(const ExplicitSpaceArrayBases &x,
const ExplicitSpaceArrayBases &y);

} // namespace lower
} // namespace Fortran
} // namespace Fortran::lower

namespace llvm {
template <>
Expand Down

0 comments on commit f72c975

Please sign in to comment.