[clang] [NFC][Clang][OpenMP] Add helper functions/utils for finding/comparing attach base-ptrs. (PR #155625)
Abhinav Gaba via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 11 16:53:42 PDT 2025
================
@@ -6765,12 +6765,255 @@ llvm::Value *CGOpenMPRuntime::emitNumThreadsForTargetDirective(
namespace {
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
+/// Utility to compare expression locations.
+/// Returns true if expr-loc of LHS is less-than that of RHS.
+/// This function asserts that both expressions have valid expr-locations.
+static bool compareExprLocs(const Expr *LHS, const Expr *RHS) {
+ // Assert that neither LHS nor RHS can be null
+ assert(LHS && "LHS expression cannot be null");
+ assert(RHS && "RHS expression cannot be null");
+
+ // Get source locations
+ SourceLocation LocLHS = LHS->getExprLoc();
+ SourceLocation LocRHS = RHS->getExprLoc();
+
+ // Assert that we have valid source locations
+ assert(LocLHS.isValid() && "LHS expression must have valid source location");
+ assert(LocRHS.isValid() && "RHS expression must have valid source location");
+
+ // Compare source locations for deterministic ordering
+ return LocLHS < LocRHS;
----------------
abhinavgaba wrote:
I was trying to think of a case where the exprs we are comparing are from different files, but I couldn't think of any for attach-ptr exprs. Do you have a case in mind?
For the purpose for attach-ptr expr comparisons, we do the comparison on a per directive basis, as part of MappableExprHandler, and the ExprLocs are those for the expressions within the clause list of that directive. For example:
```c
#pragma omp target data map(s1.p1[0], s1.p2[2])
```
Here s1.p1 and s1.p2's ExprLocs should point to the occurrence of these expressions within this directive.
I know that we insert inlined maps for outer structs with inner structs that have declare-mappers, like:
```c
struct S {
int *p;
};
#pragma omp declare mapper(default: s1) map(s1.p[0:1])
struct T {
S s1;
int *q;
};
T *tp;
#pragma omp target data map(tp[0]), map(from: tp[0].q) // implicit: map(tp[0].s1)
```
However, in this case, the attach-pointer-expr for the implicit map for `tp[0].s1` will still be `tp`, whose expr location should come from `map(tp[0])` on the line with `pragma omp target data`
https://github.com/llvm/llvm-project/pull/155625
More information about the cfe-commits
mailing list