Skip to content

Commit d680db3

Browse files
schweitzpgijeanPerier
authored andcommitted
Convert DenseMap to use value-based hashing and comparison.
Add some more implementations. Second attempt to get rid of warning.
1 parent f30a573 commit d680db3

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

flang/lib/Lower/IterationSpace.cpp

+26-13
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class HashEvaluateExpr {
208208
unsigned args = 13u;
209209
for (const auto &v : x.arguments())
210210
args -= getHashValue(v);
211-
return getHashValue(x.proc()) * 101u;
211+
return getHashValue(x.proc()) * 101u - args;
212212
}
213213
template <typename A>
214214
static unsigned
@@ -217,16 +217,15 @@ class HashEvaluateExpr {
217217
return 127u;
218218
}
219219
static unsigned getHashValue(const Fortran::evaluate::ImpliedDoIndex &x) {
220-
// FIXME: hash the contents.
221-
return 131u;
220+
return llvm::hash_value(toStringRef(x.name).str()) * 131u;
222221
}
223222
static unsigned getHashValue(const Fortran::evaluate::TypeParamInquiry &x) {
224-
// FIXME: hash the contents.
225-
return 137u;
223+
return getHashValue(x.base()) * 137u - getHashValue(x.parameter()) * 3u;
226224
}
227225
static unsigned getHashValue(const Fortran::evaluate::DescriptorInquiry &x) {
228-
// FIXME: hash the contents.
229-
return 139u;
226+
return getHashValue(x.base()) * 139u -
227+
static_cast<unsigned>(x.field()) * 13u +
228+
static_cast<unsigned>(x.dimension());
230229
}
231230
static unsigned
232231
getHashValue(const Fortran::evaluate::StructureConstructor &x) {
@@ -280,6 +279,10 @@ unsigned Fortran::lower::getHashValue(
280279
[&](const auto *p) { return HashEvaluateExpr::getHashValue(*p); }, x);
281280
}
282281

282+
unsigned Fortran::lower::getHashValue(Fortran::lower::FrontEndExpr x) {
283+
return HashEvaluateExpr::getHashValue(*x);
284+
}
285+
283286
namespace {
284287
// Define the is equals test for using Fortran::evaluate::Expr values with
285288
// llvm::DenseMap.
@@ -464,9 +467,8 @@ class IsEqualEvaluateExpr {
464467
return isEqual(*xs, *ys);
465468
return false;
466469
}
467-
if ([[maybe_unused]] const auto *ys = y.GetAssumedTypeDummy())
468-
return false;
469-
return isEqual(*x.UnwrapExpr(), *y.UnwrapExpr());
470+
return !y.GetAssumedTypeDummy() &&
471+
isEqual(*x.UnwrapExpr(), *y.UnwrapExpr());
470472
}
471473
static bool isEqual(const Fortran::evaluate::ProcedureDesignator &x,
472474
const Fortran::evaluate::ProcedureDesignator &y) {
@@ -484,15 +486,16 @@ class IsEqualEvaluateExpr {
484486
}
485487
static bool isEqual(const Fortran::evaluate::ImpliedDoIndex &x,
486488
const Fortran::evaluate::ImpliedDoIndex &y) {
487-
llvm::report_fatal_error("not implemented");
489+
return toStringRef(x.name) == toStringRef(y.name);
488490
}
489491
static bool isEqual(const Fortran::evaluate::TypeParamInquiry &x,
490492
const Fortran::evaluate::TypeParamInquiry &y) {
491-
llvm::report_fatal_error("not implemented");
493+
return isEqual(x.base(), y.base()) && isEqual(x.parameter(), y.parameter());
492494
}
493495
static bool isEqual(const Fortran::evaluate::DescriptorInquiry &x,
494496
const Fortran::evaluate::DescriptorInquiry &y) {
495-
llvm::report_fatal_error("not implemented");
497+
return isEqual(x.base(), y.base()) && x.field() == y.field() &&
498+
x.dimension() == y.dimension();
496499
}
497500
static bool isEqual(const Fortran::evaluate::StructureConstructor &x,
498501
const Fortran::evaluate::StructureConstructor &y) {
@@ -572,6 +575,16 @@ bool Fortran::lower::isEqual(
572575
x, y);
573576
}
574577

578+
bool Fortran::lower::isEqual(Fortran::lower::FrontEndExpr x,
579+
Fortran::lower::FrontEndExpr y) {
580+
auto empty = llvm::DenseMapInfo<Fortran::lower::FrontEndExpr>::getEmptyKey();
581+
auto tombstone =
582+
llvm::DenseMapInfo<Fortran::lower::FrontEndExpr>::getTombstoneKey();
583+
if (x == empty || y == empty || x == tombstone || y == tombstone)
584+
return x == y;
585+
return x == y || IsEqualEvaluateExpr::isEqual(*x, *y);
586+
}
587+
575588
namespace {
576589

577590
/// This class can recover the base array in an expression that contains

flang/lib/Lower/IterationSpace.h

+26-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ using FrontEndSymbol = const semantics::Symbol *;
3636

3737
class AbstractConverter;
3838

39+
unsigned getHashValue(FrontEndExpr x);
40+
bool isEqual(FrontEndExpr x, FrontEndExpr y);
41+
} // namespace lower
42+
} // namespace Fortran
43+
44+
namespace llvm {
45+
template <>
46+
struct DenseMapInfo<Fortran::lower::FrontEndExpr> {
47+
static inline Fortran::lower::FrontEndExpr getEmptyKey() {
48+
return reinterpret_cast<Fortran::lower::FrontEndExpr>(~0);
49+
}
50+
static inline Fortran::lower::FrontEndExpr getTombstoneKey() {
51+
return reinterpret_cast<Fortran::lower::FrontEndExpr>(~0 - 1);
52+
}
53+
static unsigned getHashValue(Fortran::lower::FrontEndExpr v) {
54+
return Fortran::lower::getHashValue(v);
55+
}
56+
static bool isEqual(Fortran::lower::FrontEndExpr lhs,
57+
Fortran::lower::FrontEndExpr rhs) {
58+
return Fortran::lower::isEqual(lhs, rhs);
59+
}
60+
};
61+
} // namespace llvm
62+
63+
namespace Fortran::lower {
3964
template <typename A>
4065
class StackableConstructExpr {
4166
public:
@@ -190,8 +215,7 @@ unsigned getHashValue(const ExplicitSpaceArrayBases &x);
190215
bool isEqual(const ExplicitSpaceArrayBases &x,
191216
const ExplicitSpaceArrayBases &y);
192217

193-
} // namespace lower
194-
} // namespace Fortran
218+
} // namespace Fortran::lower
195219

196220
namespace llvm {
197221
template <>

0 commit comments

Comments
 (0)