Skip to content

Commit 3b42ed4

Browse files
committed
Use order of computation, instead of exprLocs for comparing attach-ptr-exprs.
1 parent 90e1f32 commit 3b42ed4

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6765,35 +6765,16 @@ llvm::Value *CGOpenMPRuntime::emitNumThreadsForTargetDirective(
67656765
namespace {
67666766
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
67676767

6768-
/// Utility to compare expression locations.
6769-
/// Returns true if expr-loc of LHS is less-than that of RHS.
6770-
/// This function asserts that both expressions have valid expr-locations.
6771-
static bool compareExprLocs(const Expr *LHS, const Expr *RHS) {
6772-
// Assert that neither LHS nor RHS can be null
6773-
assert(LHS && "LHS expression cannot be null");
6774-
assert(RHS && "RHS expression cannot be null");
6775-
6776-
// Get source locations
6777-
SourceLocation LocLHS = LHS->getExprLoc();
6778-
SourceLocation LocRHS = RHS->getExprLoc();
6779-
6780-
// Assert that we have valid source locations
6781-
assert(LocLHS.isValid() && "LHS expression must have valid source location");
6782-
assert(LocRHS.isValid() && "RHS expression must have valid source location");
6783-
6784-
// Compare source locations for deterministic ordering
6785-
return LocLHS < LocRHS;
6786-
}
6787-
67886768
// Utility to handle information from clauses associated with a given
67896769
// construct that use mappable expressions (e.g. 'map' clause, 'to' clause).
67906770
// It provides a convenient interface to obtain the information and generate
67916771
// code for that information.
67926772
class MappableExprsHandler {
67936773
public:
67946774
/// Custom comparator for attach-pointer expressions that compares them by
6795-
/// complexity (i.e. their component-depth) first, then by their expr-locs if
6796-
/// they are semantically different.
6775+
/// complexity (i.e. their component-depth) first, then by the order in which
6776+
/// they were computed by findAttachPtrExpr(), if they are semantically
6777+
/// different.
67976778
struct AttachPtrExprComparator {
67986779
const MappableExprsHandler *Handler;
67996780
// Cache of previous equality comparison results.
@@ -6823,8 +6804,8 @@ class MappableExprsHandler {
68236804
// Both have same complexity, now check semantic equality
68246805
if (areEqual(LHS, RHS))
68256806
return false;
6826-
// Different semantically, compare by location
6827-
return compareExprLocs(LHS, RHS);
6807+
// Different semantically, compare by computation order
6808+
return wasComputedBefore(LHS, RHS);
68286809
}
68296810
if (!DepthLHS.has_value())
68306811
return true; // LHS has lower complexity
@@ -6838,8 +6819,8 @@ class MappableExprsHandler {
68386819
// Same complexity, now check semantic equality
68396820
if (areEqual(LHS, RHS))
68406821
return false;
6841-
// Different semantically, compare by location
6842-
return compareExprLocs(LHS, RHS);
6822+
// Different semantically, compare by computation order
6823+
return wasComputedBefore(LHS, RHS);
68436824
}
68446825

68456826
public:
@@ -6860,6 +6841,15 @@ class MappableExprsHandler {
68606841
return ComparisonResult;
68616842
}
68626843

6844+
/// Compare the two attach-ptr expressions by their computation order.
6845+
/// Returns true iff LHS was computed before RHS by findAttachPtrExpr.
6846+
bool wasComputedBefore(const Expr *LHS, const Expr *RHS) const {
6847+
const size_t &OrderLHS = Handler->AttachPtrComputationOrderMap.at(LHS);
6848+
const size_t &OrderRHS = Handler->AttachPtrComputationOrderMap.at(RHS);
6849+
6850+
return OrderLHS < OrderRHS;
6851+
}
6852+
68636853
private:
68646854
/// Helper function to compare attach-pointer expressions semantically.
68656855
/// This function handles various expression types that can be part of an
@@ -7218,6 +7208,11 @@ class MappableExprsHandler {
72187208
llvm::DenseMap<const Expr *, std::optional<size_t>>
72197209
AttachPtrComponentDepthMap = {{nullptr, std::nullopt}};
72207210

7211+
/// Map from attach pointer expressions to the order they were computed in, in
7212+
/// findAttachPtrExpr().
7213+
llvm::DenseMap<const Expr *, size_t> AttachPtrComputationOrderMap = {
7214+
{nullptr, 0}};
7215+
72217216
llvm::Value *getExprTypeSize(const Expr *E) const {
72227217
QualType ExprTy = E->getType().getCanonicalType();
72237218

@@ -8508,9 +8503,13 @@ class MappableExprsHandler {
85088503
return true;
85098504
}
85108505

8511-
// A wrapper around OMPClauseMappableExprCommon::findAttachPtrExpr. that
8512-
// accepts \p CurDir instead of an OpenMPDirectiveKind.
8513-
static std::pair<const Expr *, std::optional<size_t>> findAttachPtrExpr(
8506+
/// Computes the attach-ptr expr for \p Components, and updates various maps
8507+
/// with the information.
8508+
/// It internally calls OMPClauseMappableExprCommon::findAttachPtrExpr()
8509+
/// with the OpenMPDirectiveKind extracted from \p CurDir.
8510+
/// It updates AttachPtrComputationOrderMap, AttachPtrComponentDepthMap, and
8511+
/// AttachPtrExprMap.
8512+
void collectAttachPtrExprInfo(
85148513
OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
85158514
llvm::PointerUnion<const OMPExecutableDirective *,
85168515
const OMPDeclareMapperDecl *>
@@ -8521,8 +8520,14 @@ class MappableExprsHandler {
85218520
? OMPD_declare_mapper
85228521
: cast<const OMPExecutableDirective *>(CurDir)->getDirectiveKind();
85238522

8524-
return OMPClauseMappableExprCommon::findAttachPtrExpr(Components,
8525-
CurDirectiveID);
8523+
const auto &[AttachPtrExpr, Depth] =
8524+
OMPClauseMappableExprCommon::findAttachPtrExpr(Components,
8525+
CurDirectiveID);
8526+
8527+
AttachPtrComputationOrderMap.try_emplace(
8528+
AttachPtrExpr, AttachPtrComputationOrderMap.size());
8529+
AttachPtrComponentDepthMap.try_emplace(AttachPtrExpr, Depth);
8530+
AttachPtrExprMap.try_emplace(Components, AttachPtrExpr);
85268531
}
85278532

85288533
/// Generate all the base pointers, section pointers, sizes, map types, and

0 commit comments

Comments
 (0)