Skip to content

Commit be99c61

Browse files
committed
[OPENMP50]Codegen for iterator construct.
Implemented codegen for the iterator expression in the depend clauses. Iterator construct is emitted the following way: iterator(cnt1, cnt2, ...), in : <dep> <TotalNumDeps> = <cnt1_size> * <cnt2_size> * ...; kmp_depend_t deps[<TotalNumDeps>]; deps_counter = 0; for (cnt1) { for (cnt2) { ... deps[deps_counter].base_addr = &<dep>; deps[deps_counter].size = sizeof(<dep>); deps[deps_counter].flags = in; deps_counter += 1; ... } } For depobj construct the codegen is very similar, but the memory is allocated dynamically and added extra first item reserved for internal use.
1 parent e3ba652 commit be99c61

25 files changed

+1204
-504
lines changed

clang/include/clang/AST/ExprOpenMP.h

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,21 @@ class OMPArrayShapingExpr final
206206
}
207207
};
208208

209+
/// Helper expressions and declaration for OMPIteratorExpr class for each
210+
/// iteration space.
211+
struct OMPIteratorHelperData {
212+
/// Internal normalized counter.
213+
VarDecl *CounterVD = nullptr;
214+
/// Normalized upper bound. Normalized loop iterates from 0 to Upper with
215+
/// step 1.
216+
Expr *Upper = nullptr;
217+
/// Update expression for the originally specified iteration variable,
218+
/// calculated as VD = Begin + CounterVD * Step;
219+
Expr *Update = nullptr;
220+
/// Updater for the internal counter: ++CounterVD;
221+
Expr *CounterUpdate = nullptr;
222+
};
223+
209224
/// OpenMP 5.0 [2.1.6 Iterators]
210225
/// Iterators are identifiers that expand to multiple values in the clause on
211226
/// which they appear.
@@ -233,7 +248,7 @@ class OMPArrayShapingExpr final
233248
class OMPIteratorExpr final
234249
: public Expr,
235250
private llvm::TrailingObjects<OMPIteratorExpr, Decl *, Expr *,
236-
SourceLocation> {
251+
SourceLocation, OMPIteratorHelperData> {
237252
public:
238253
/// Iterator range representation begin:end[:step].
239254
struct IteratorRange {
@@ -280,7 +295,8 @@ class OMPIteratorExpr final
280295

281296
OMPIteratorExpr(QualType ExprTy, SourceLocation IteratorKwLoc,
282297
SourceLocation L, SourceLocation R,
283-
ArrayRef<OMPIteratorExpr::IteratorDefinition> Data);
298+
ArrayRef<IteratorDefinition> Data,
299+
ArrayRef<OMPIteratorHelperData> Helpers);
284300

285301
/// Construct an empty expression.
286302
explicit OMPIteratorExpr(EmptyShell Shell, unsigned NumIterators)
@@ -298,6 +314,9 @@ class OMPIteratorExpr final
298314
void setIteratorRange(unsigned I, Expr *Begin, SourceLocation ColonLoc,
299315
Expr *End, SourceLocation SecondColonLoc, Expr *Step);
300316

317+
/// Sets helpers for the specified iteration space.
318+
void setHelper(unsigned I, const OMPIteratorHelperData &D);
319+
301320
unsigned numTrailingObjects(OverloadToken<Decl *>) const {
302321
return NumIterators;
303322
}
@@ -306,11 +325,16 @@ class OMPIteratorExpr final
306325
return NumIterators * static_cast<int>(RangeExprOffset::Total);
307326
}
308327

328+
unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
329+
return NumIterators * static_cast<int>(RangeLocOffset::Total);
330+
}
331+
309332
public:
310333
static OMPIteratorExpr *Create(const ASTContext &Context, QualType T,
311334
SourceLocation IteratorKwLoc, SourceLocation L,
312335
SourceLocation R,
313-
ArrayRef<IteratorDefinition> Data);
336+
ArrayRef<IteratorDefinition> Data,
337+
ArrayRef<OMPIteratorHelperData> Helpers);
314338

315339
static OMPIteratorExpr *CreateEmpty(const ASTContext &Context,
316340
unsigned NumIterators);
@@ -350,6 +374,10 @@ class OMPIteratorExpr final
350374
/// Returns number of iterator definitions.
351375
unsigned numOfIterators() const { return NumIterators; }
352376

377+
/// Fetches helper data for the specified iteration space.
378+
OMPIteratorHelperData &getHelper(unsigned I);
379+
const OMPIteratorHelperData &getHelper(unsigned I) const;
380+
353381
static bool classof(const Stmt *T) {
354382
return T->getStmtClass() == OMPIteratorExprClass;
355383
}

clang/lib/AST/Expr.cpp

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4695,9 +4695,22 @@ SourceLocation OMPIteratorExpr::getSecondColonLoc(unsigned I) const {
46954695
static_cast<int>(RangeLocOffset::SecondColonLoc)];
46964696
}
46974697

4698+
void OMPIteratorExpr::setHelper(unsigned I, const OMPIteratorHelperData &D) {
4699+
getTrailingObjects<OMPIteratorHelperData>()[I] = D;
4700+
}
4701+
4702+
OMPIteratorHelperData &OMPIteratorExpr::getHelper(unsigned I) {
4703+
return getTrailingObjects<OMPIteratorHelperData>()[I];
4704+
}
4705+
4706+
const OMPIteratorHelperData &OMPIteratorExpr::getHelper(unsigned I) const {
4707+
return getTrailingObjects<OMPIteratorHelperData>()[I];
4708+
}
4709+
46984710
OMPIteratorExpr::OMPIteratorExpr(
46994711
QualType ExprTy, SourceLocation IteratorKwLoc, SourceLocation L,
4700-
SourceLocation R, ArrayRef<OMPIteratorExpr::IteratorDefinition> Data)
4712+
SourceLocation R, ArrayRef<OMPIteratorExpr::IteratorDefinition> Data,
4713+
ArrayRef<OMPIteratorHelperData> Helpers)
47014714
: Expr(OMPIteratorExprClass, ExprTy, VK_LValue, OK_Ordinary),
47024715
IteratorKwLoc(IteratorKwLoc), LPLoc(L), RPLoc(R),
47034716
NumIterators(Data.size()) {
@@ -4707,6 +4720,7 @@ OMPIteratorExpr::OMPIteratorExpr(
47074720
setAssignmentLoc(I, D.AssignmentLoc);
47084721
setIteratorRange(I, D.Range.Begin, D.ColonLoc, D.Range.End,
47094722
D.SecondColonLoc, D.Range.Step);
4723+
setHelper(I, Helpers[I]);
47104724
}
47114725
setDependence(computeDependence(this));
47124726
}
@@ -4715,21 +4729,25 @@ OMPIteratorExpr *
47154729
OMPIteratorExpr::Create(const ASTContext &Context, QualType T,
47164730
SourceLocation IteratorKwLoc, SourceLocation L,
47174731
SourceLocation R,
4718-
ArrayRef<OMPIteratorExpr::IteratorDefinition> Data) {
4732+
ArrayRef<OMPIteratorExpr::IteratorDefinition> Data,
4733+
ArrayRef<OMPIteratorHelperData> Helpers) {
4734+
assert(Data.size() == Helpers.size() &&
4735+
"Data and helpers must have the same size.");
47194736
void *Mem = Context.Allocate(
4720-
totalSizeToAlloc<Decl *, Expr *, SourceLocation>(
4737+
totalSizeToAlloc<Decl *, Expr *, SourceLocation, OMPIteratorHelperData>(
47214738
Data.size(), Data.size() * static_cast<int>(RangeExprOffset::Total),
4722-
Data.size() * static_cast<int>(RangeLocOffset::Total)),
4739+
Data.size() * static_cast<int>(RangeLocOffset::Total),
4740+
Helpers.size()),
47234741
alignof(OMPIteratorExpr));
4724-
return new (Mem) OMPIteratorExpr(T, IteratorKwLoc, L, R, Data);
4742+
return new (Mem) OMPIteratorExpr(T, IteratorKwLoc, L, R, Data, Helpers);
47254743
}
47264744

47274745
OMPIteratorExpr *OMPIteratorExpr::CreateEmpty(const ASTContext &Context,
47284746
unsigned NumIterators) {
47294747
void *Mem = Context.Allocate(
4730-
totalSizeToAlloc<Decl *, Expr *, SourceLocation>(
4748+
totalSizeToAlloc<Decl *, Expr *, SourceLocation, OMPIteratorHelperData>(
47314749
NumIterators, NumIterators * static_cast<int>(RangeExprOffset::Total),
4732-
NumIterators * static_cast<int>(RangeLocOffset::Total)),
4750+
NumIterators * static_cast<int>(RangeLocOffset::Total), NumIterators),
47334751
alignof(OMPIteratorExpr));
47344752
return new (Mem) OMPIteratorExpr(EmptyShell(), NumIterators);
47354753
}

0 commit comments

Comments
 (0)