-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[Clang][OpenMP][LoopTransformations] Add support for "#pragma omp fuse" loop transformation directive and "looprange" clause #139293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fb91129
34ac92a
c76888b
1c05467
860fcd9
65cbfeb
b0fb1b3
b252aa9
e294777
1c8f0fe
009d863
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1143,6 +1143,101 @@ class OMPFullClause final : public OMPNoChildClause<llvm::omp::OMPC_full> { | |
static OMPFullClause *CreateEmpty(const ASTContext &C); | ||
}; | ||
|
||
/// This class represents the 'looprange' clause in the | ||
/// '#pragma omp fuse' directive | ||
/// | ||
/// \code {c} | ||
/// #pragma omp fuse looprange(1,2) | ||
/// { | ||
/// for(int i = 0; i < 64; ++i) | ||
/// for(int j = 0; j < 256; j+=2) | ||
/// for(int k = 127; k >= 0; --k) | ||
/// \endcode | ||
class OMPLoopRangeClause final | ||
: public OMPClause, | ||
private llvm::TrailingObjects<OMPLoopRangeClause, Expr *> { | ||
friend class OMPClauseReader; | ||
friend class llvm::TrailingObjects<OMPLoopRangeClause, Expr *>; | ||
|
||
/// Location of '(' | ||
SourceLocation LParenLoc; | ||
|
||
/// Location of first and count expressions | ||
SourceLocation FirstLoc, CountLoc; | ||
|
||
/// Number of looprange arguments (always 2: first, count) | ||
unsigned NumArgs = 2; | ||
|
||
/// Set the argument expressions. | ||
void setArgs(ArrayRef<Expr *> Args) { | ||
assert(Args.size() == NumArgs && "Expected exactly 2 looprange arguments"); | ||
std::copy(Args.begin(), Args.end(), getTrailingObjects<Expr *>()); | ||
} | ||
|
||
/// Build an empty clause for deserialization. | ||
explicit OMPLoopRangeClause() | ||
: OMPClause(llvm::omp::OMPC_looprange, {}, {}), NumArgs(2) {} | ||
|
||
public: | ||
/// Build a 'looprange' clause AST node. | ||
static OMPLoopRangeClause * | ||
Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, | ||
SourceLocation FirstLoc, SourceLocation CountLoc, | ||
SourceLocation EndLoc, ArrayRef<Expr *> Args); | ||
|
||
/// Build an empty 'looprange' clause node. | ||
static OMPLoopRangeClause *CreateEmpty(const ASTContext &C); | ||
|
||
// Location getters/setters | ||
SourceLocation getLParenLoc() const { return LParenLoc; } | ||
SourceLocation getFirstLoc() const { return FirstLoc; } | ||
SourceLocation getCountLoc() const { return CountLoc; } | ||
|
||
void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; } | ||
void setFirstLoc(SourceLocation Loc) { FirstLoc = Loc; } | ||
void setCountLoc(SourceLocation Loc) { CountLoc = Loc; } | ||
|
||
/// Get looprange 'first' expression | ||
Expr *getFirst() const { return getArgs()[0]; } | ||
|
||
/// Get looprange 'count' expression | ||
Expr *getCount() const { return getArgs()[1]; } | ||
|
||
/// Set looprange 'first' expression | ||
void setFirst(Expr *E) { getArgs()[0] = E; } | ||
|
||
/// Set looprange 'count' expression | ||
void setCount(Expr *E) { getArgs()[1] = E; } | ||
|
||
Comment on lines
+1206
to
+1211
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These functions should be private |
||
MutableArrayRef<Expr *> getArgs() { | ||
return MutableArrayRef<Expr *>(getTrailingObjects<Expr *>(), NumArgs); | ||
} | ||
ArrayRef<Expr *> getArgs() const { | ||
return ArrayRef<Expr *>(getTrailingObjects<Expr *>(), NumArgs); | ||
} | ||
Comment on lines
+1212
to
+1217
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doubt you need these functions |
||
|
||
child_range children() { | ||
return child_range(reinterpret_cast<Stmt **>(getArgs().begin()), | ||
reinterpret_cast<Stmt **>(getArgs().end())); | ||
} | ||
const_child_range children() const { | ||
auto AR = getArgs(); | ||
return const_child_range(reinterpret_cast<Stmt *const *>(AR.begin()), | ||
reinterpret_cast<Stmt *const *>(AR.end())); | ||
} | ||
|
||
child_range used_children() { | ||
return child_range(child_iterator(), child_iterator()); | ||
} | ||
const_child_range used_children() const { | ||
return const_child_range(const_child_iterator(), const_child_iterator()); | ||
} | ||
|
||
static bool classof(const OMPClause *T) { | ||
return T->getClauseKind() == llvm::omp::OMPC_looprange; | ||
} | ||
}; | ||
|
||
/// Representation of the 'partial' clause of the '#pragma omp unroll' | ||
/// directive. | ||
/// | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -962,6 +962,9 @@ class OMPLoopTransformationDirective : public OMPLoopBasedDirective { | |
|
||
/// Number of loops generated by this loop transformation. | ||
unsigned NumGeneratedLoops = 0; | ||
/// Number of top level canonical loop nests generated by this loop | ||
/// transformation | ||
unsigned NumGeneratedLoopNests = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need this new field? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe the name is a bit unfortunate and could be improved, but they are 2 completely different fields conceptually. This top level loops are the ones actually managed by loop Sequence constructs like fuse and the upcoming split. A loop sequence contains loops which may contain several inner nestes loops, but these should not be taken into account for performing fusion or splitting. This was not taken into account originally due to all transformations having a fixed number of generated top level nests (1). However fuse or split may generate several loop nests with inner nested loops. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that unroll is an exception, it could have 0 or 1 but it coincides perfectly with the original number of loops . There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The question is how it is used. I did not see it is being read anywhere There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This distinction is indeed important and actively used in |
||
|
||
protected: | ||
explicit OMPLoopTransformationDirective(StmtClass SC, | ||
|
@@ -974,13 +977,21 @@ class OMPLoopTransformationDirective : public OMPLoopBasedDirective { | |
/// Set the number of loops generated by this loop transformation. | ||
void setNumGeneratedLoops(unsigned Num) { NumGeneratedLoops = Num; } | ||
|
||
/// Set the number of top level canonical loop nests generated by this loop | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] empty line for separating functions |
||
/// transformation | ||
void setNumGeneratedLoopNests(unsigned Num) { NumGeneratedLoopNests = Num; } | ||
|
||
public: | ||
/// Return the number of associated (consumed) loops. | ||
unsigned getNumAssociatedLoops() const { return getLoopsNumber(); } | ||
|
||
/// Return the number of loops generated by this loop transformation. | ||
unsigned getNumGeneratedLoops() const { return NumGeneratedLoops; } | ||
|
||
/// Return the number of top level canonical loop nests generated by this loop | ||
/// transformation | ||
unsigned getNumGeneratedLoopNests() const { return NumGeneratedLoopNests; } | ||
|
||
/// Get the de-sugared statements after the loop transformation. | ||
/// | ||
/// Might be nullptr if either the directive generates no loops and is handled | ||
|
@@ -995,7 +1006,7 @@ class OMPLoopTransformationDirective : public OMPLoopBasedDirective { | |
Stmt::StmtClass C = T->getStmtClass(); | ||
return C == OMPTileDirectiveClass || C == OMPUnrollDirectiveClass || | ||
C == OMPReverseDirectiveClass || C == OMPInterchangeDirectiveClass || | ||
C == OMPStripeDirectiveClass; | ||
C == OMPStripeDirectiveClass || C == OMPFuseDirectiveClass; | ||
} | ||
}; | ||
|
||
|
@@ -5561,7 +5572,10 @@ class OMPTileDirective final : public OMPLoopTransformationDirective { | |
: OMPLoopTransformationDirective(OMPTileDirectiveClass, | ||
llvm::omp::OMPD_tile, StartLoc, EndLoc, | ||
NumLoops) { | ||
// Tiling doubles the original number of loops | ||
setNumGeneratedLoops(2 * NumLoops); | ||
// Produces a single top-level canonical loop nest | ||
setNumGeneratedLoopNests(1); | ||
} | ||
|
||
void setPreInits(Stmt *PreInits) { | ||
|
@@ -5639,6 +5653,8 @@ class OMPStripeDirective final : public OMPLoopTransformationDirective { | |
llvm::omp::OMPD_stripe, StartLoc, EndLoc, | ||
NumLoops) { | ||
setNumGeneratedLoops(2 * NumLoops); | ||
// Similar to Tile, it only generates a single top level loop nest | ||
setNumGeneratedLoopNests(1); | ||
} | ||
|
||
void setPreInits(Stmt *PreInits) { | ||
|
@@ -5792,7 +5808,9 @@ class OMPReverseDirective final : public OMPLoopTransformationDirective { | |
: OMPLoopTransformationDirective(OMPReverseDirectiveClass, | ||
llvm::omp::OMPD_reverse, StartLoc, | ||
EndLoc, NumLoops) { | ||
// Reverse produces a single top-level canonical loop nest | ||
setNumGeneratedLoops(NumLoops); | ||
setNumGeneratedLoopNests(1); | ||
} | ||
|
||
void setPreInits(Stmt *PreInits) { | ||
|
@@ -5864,7 +5882,10 @@ class OMPInterchangeDirective final : public OMPLoopTransformationDirective { | |
: OMPLoopTransformationDirective(OMPInterchangeDirectiveClass, | ||
llvm::omp::OMPD_interchange, StartLoc, | ||
EndLoc, NumLoops) { | ||
setNumGeneratedLoops(NumLoops); | ||
// Interchange produces a single top-level canonical loop | ||
// nest, with the exact same amount of total loops | ||
setNumGeneratedLoops(3 * NumLoops); | ||
setNumGeneratedLoopNests(1); | ||
} | ||
|
||
void setPreInits(Stmt *PreInits) { | ||
|
@@ -5915,6 +5936,91 @@ class OMPInterchangeDirective final : public OMPLoopTransformationDirective { | |
} | ||
}; | ||
|
||
/// Represents the '#pragma omp fuse' loop transformation directive | ||
/// | ||
/// \code{c} | ||
/// #pragma omp fuse | ||
/// { | ||
/// for(int i = 0; i < m1; ++i) {...} | ||
/// for(int j = 0; j < m2; ++j) {...} | ||
/// ... | ||
/// } | ||
/// \endcode | ||
|
||
class OMPFuseDirective final : public OMPLoopTransformationDirective { | ||
friend class ASTStmtReader; | ||
friend class OMPExecutableDirective; | ||
|
||
// Offsets of child members. | ||
enum { | ||
PreInitsOffset = 0, | ||
TransformedStmtOffset, | ||
}; | ||
|
||
explicit OMPFuseDirective(SourceLocation StartLoc, SourceLocation EndLoc, | ||
unsigned NumLoops) | ||
: OMPLoopTransformationDirective(OMPFuseDirectiveClass, | ||
llvm::omp::OMPD_fuse, StartLoc, EndLoc, | ||
NumLoops) { | ||
// This default initialization assumes simple loop fusion. | ||
// If a 'looprange' clause is specified, these values must be explicitly set | ||
setNumGeneratedLoopNests(1); | ||
setNumGeneratedLoops(NumLoops); | ||
} | ||
|
||
void setPreInits(Stmt *PreInits) { | ||
Data->getChildren()[PreInitsOffset] = PreInits; | ||
} | ||
|
||
void setTransformedStmt(Stmt *S) { | ||
Data->getChildren()[TransformedStmtOffset] = S; | ||
} | ||
|
||
public: | ||
/// Create a new AST node representation for #pragma omp fuse' | ||
/// | ||
/// \param C Context of the AST | ||
/// \param StartLoc Location of the introducer (e.g the 'omp' token) | ||
/// \param EndLoc Location of the directive's end (e.g the tok::eod) | ||
/// \param Clauses The directive's clauses | ||
/// \param NumLoops Number of total affected loops | ||
/// \param NumLoopNests Number of affected top level canonical loops | ||
/// (number of items in the 'looprange' clause if present) | ||
/// \param AssociatedStmt The outermost associated loop | ||
/// \param TransformedStmt The loop nest after fusion, or nullptr in | ||
/// dependent | ||
/// \param PreInits Helper preinits statements for the loop nest | ||
static OMPFuseDirective *Create(const ASTContext &C, SourceLocation StartLoc, | ||
SourceLocation EndLoc, | ||
ArrayRef<OMPClause *> Clauses, | ||
unsigned NumLoops, unsigned NumLoopNests, | ||
Stmt *AssociatedStmt, Stmt *TransformedStmt, | ||
Stmt *PreInits); | ||
|
||
/// Build an empty '#pragma omp fuse' AST node for deserialization | ||
/// | ||
/// \param C Context of the AST | ||
/// \param NumClauses Number of clauses to allocate | ||
/// \param NumLoops Number of associated loops to allocate | ||
/// \param NumLoopNests Number of top level loops to allocate | ||
static OMPFuseDirective *CreateEmpty(const ASTContext &C, unsigned NumClauses, | ||
unsigned NumLoops, | ||
unsigned NumLoopNests); | ||
|
||
/// Gets the associated loops after the transformation. This is the de-sugared | ||
/// replacement or nulltpr in dependent contexts. | ||
Stmt *getTransformedStmt() const { | ||
return Data->getChildren()[TransformedStmtOffset]; | ||
} | ||
|
||
/// Return preinits statement. | ||
Stmt *getPreInits() const { return Data->getChildren()[PreInitsOffset]; } | ||
|
||
static bool classof(const Stmt *T) { | ||
return T->getStmtClass() == OMPFuseDirectiveClass; | ||
} | ||
}; | ||
|
||
/// This represents '#pragma omp scan' directive. | ||
/// | ||
/// \code | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it always constant? In this case you don't need to use tail allocation, use fixed array