-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[OpenACC] Implement AST for OpenACC Compute Constructs #81188
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
//===- StmtOpenACC.h - Classes for OpenACC directives ----------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// \file | ||
/// This file defines OpenACC AST classes for statement-level contructs. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_AST_STMTOPENACC_H | ||
#define LLVM_CLANG_AST_STMTOPENACC_H | ||
|
||
#include "clang/AST/Stmt.h" | ||
#include "clang/Basic/OpenACCKinds.h" | ||
#include "clang/Basic/SourceLocation.h" | ||
|
||
namespace clang { | ||
/// This is the base class for an OpenACC statement-level construct, other | ||
/// construct types are expected to inherit from this. | ||
class OpenACCConstructStmt : public Stmt { | ||
friend class ASTStmtWriter; | ||
friend class ASTStmtReader; | ||
/// The directive kind. Each implementation of this interface should handle | ||
/// specific kinds. | ||
OpenACCDirectiveKind Kind = OpenACCDirectiveKind::Invalid; | ||
/// The location of the directive statement, from the '#' to the last token of | ||
/// the directive. | ||
SourceRange Range; | ||
|
||
// TODO OPENACC: Clauses should probably be collected in this class. | ||
|
||
protected: | ||
OpenACCConstructStmt(StmtClass SC, OpenACCDirectiveKind K, | ||
SourceLocation Start, SourceLocation End) | ||
: Stmt(SC), Kind(K), Range(Start, End) {} | ||
|
||
public: | ||
OpenACCDirectiveKind getDirectiveKind() const { return Kind; } | ||
|
||
static bool classof(const Stmt *S) { | ||
return S->getStmtClass() >= firstOpenACCConstructStmtConstant && | ||
S->getStmtClass() <= lastOpenACCConstructStmtConstant; | ||
} | ||
|
||
SourceLocation getBeginLoc() const { return Range.getBegin(); } | ||
SourceLocation getEndLoc() const { return Range.getEnd(); } | ||
|
||
child_range children() { | ||
return child_range(child_iterator(), child_iterator()); | ||
} | ||
|
||
const_child_range children() const { | ||
return const_cast<OpenACCConstructStmt *>(this)->children(); | ||
} | ||
}; | ||
|
||
/// This is a base class for any OpenACC statement-level constructs that have an | ||
/// associated statement. This class is not intended to be instantiated, but is | ||
/// a convenient place to hold the associated statement. | ||
class OpenACCAssociatedStmtConstruct : public OpenACCConstructStmt { | ||
friend class ASTStmtWriter; | ||
friend class ASTStmtReader; | ||
template <typename Derived> friend class RecursiveASTVisitor; | ||
Stmt *AssociatedStmt = nullptr; | ||
|
||
protected: | ||
OpenACCAssociatedStmtConstruct(StmtClass SC, OpenACCDirectiveKind K, | ||
SourceLocation Start, SourceLocation End) | ||
: OpenACCConstructStmt(SC, K, Start, End) {} | ||
|
||
void setAssociatedStmt(Stmt *S) { AssociatedStmt = S; } | ||
Stmt *getAssociatedStmt() { return AssociatedStmt; } | ||
const Stmt *getAssociatedStmt() const { | ||
return const_cast<OpenACCAssociatedStmtConstruct *>(this) | ||
->getAssociatedStmt(); | ||
} | ||
|
||
public: | ||
child_range children() { | ||
if (getAssociatedStmt()) | ||
return child_range(&AssociatedStmt, &AssociatedStmt + 1); | ||
return child_range(child_iterator(), child_iterator()); | ||
} | ||
|
||
const_child_range children() const { | ||
return const_cast<OpenACCAssociatedStmtConstruct *>(this)->children(); | ||
} | ||
}; | ||
/// This class represents a compute construct, representing a 'Kind' of | ||
/// `parallel', 'serial', or 'kernel'. These constructs are associated with a | ||
/// 'structured block', defined as: | ||
/// | ||
/// in C or C++, an executable statement, possibly compound, with a single | ||
/// entry at the top and a single exit at the bottom | ||
/// | ||
/// At the moment there is no real motivation to have a different AST node for | ||
/// those three, as they are semantically identical, and have only minor | ||
/// differences in the permitted list of clauses, which can be differentiated by | ||
/// the 'Kind'. | ||
class OpenACCComputeConstruct : public OpenACCAssociatedStmtConstruct { | ||
friend class ASTStmtWriter; | ||
friend class ASTStmtReader; | ||
friend class ASTContext; | ||
OpenACCComputeConstruct() | ||
: OpenACCAssociatedStmtConstruct(OpenACCComputeConstructClass, | ||
OpenACCDirectiveKind::Invalid, | ||
SourceLocation{}, SourceLocation{}) {} | ||
|
||
OpenACCComputeConstruct(OpenACCDirectiveKind K, SourceLocation Start, | ||
SourceLocation End) | ||
: OpenACCAssociatedStmtConstruct(OpenACCComputeConstructClass, K, Start, | ||
End) { | ||
assert((K == OpenACCDirectiveKind::Parallel || | ||
K == OpenACCDirectiveKind::Serial || | ||
K == OpenACCDirectiveKind::Kernels) && | ||
"Only parallel, serial, and kernels constructs should be " | ||
"represented by this type"); | ||
} | ||
|
||
void setStructuredBlock(Stmt *S) { setAssociatedStmt(S); } | ||
|
||
public: | ||
static bool classof(const Stmt *T) { | ||
return T->getStmtClass() == OpenACCComputeConstructClass; | ||
} | ||
|
||
static OpenACCComputeConstruct *CreateEmpty(const ASTContext &C, EmptyShell); | ||
static OpenACCComputeConstruct *Create(const ASTContext &C, | ||
OpenACCDirectiveKind K, | ||
SourceLocation BeginLoc, | ||
SourceLocation EndLoc); | ||
|
||
Stmt *getStructuredBlock() { return getAssociatedStmt(); } | ||
const Stmt *getStructuredBlock() const { | ||
return const_cast<OpenACCComputeConstruct *>(this)->getStructuredBlock(); | ||
} | ||
}; | ||
} // namespace clang | ||
#endif // LLVM_CLANG_AST_STMTOPENACC_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//===--- StmtOpenACC.cpp - Classes for OpenACC Constructs -----------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements the subclesses of Stmt class declared in StmtOpenACC.h | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "clang/AST/StmtOpenACC.h" | ||
#include "clang/AST/ASTContext.h" | ||
using namespace clang; | ||
|
||
OpenACCComputeConstruct * | ||
OpenACCComputeConstruct::CreateEmpty(const ASTContext &C, EmptyShell) { | ||
void *Mem = C.Allocate(sizeof(OpenACCComputeConstruct), | ||
alignof(OpenACCComputeConstruct)); | ||
auto *Inst = new (Mem) OpenACCComputeConstruct; | ||
return Inst; | ||
} | ||
|
||
OpenACCComputeConstruct * | ||
OpenACCComputeConstruct::Create(const ASTContext &C, OpenACCDirectiveKind K, | ||
SourceLocation BeginLoc, | ||
SourceLocation EndLoc) { | ||
void *Mem = C.Allocate(sizeof(OpenACCComputeConstruct), | ||
alignof(OpenACCComputeConstruct)); | ||
auto *Inst = new (Mem) OpenACCComputeConstruct(K, BeginLoc, EndLoc); | ||
return Inst; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I hhave a question about children stmts/exprs here. Does OpenACC nodes may have other children rather than AssociatedStmt? If it is possible, better to use tail allocation and store AssociatedStmt as part of the tail-allocated memory space
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.
As far as I can tell, they do NOT have any. Individual clauses have expressions (and some of the other nodes have expressions), but there is never more than 1 statement associated with it.
I considered the trailing storage, but since it is always 'required' and only on certain constructs (which I'll select via giving them a different AST node), I think a member for the
ComputeConstruct
, and a few others makes the most sense? But if you see a good reason to switch to trailing storage during future review, please let me know, and I'll switch it over!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.
The problem here, that if you're going to implement it in some other level of abstraction, rather than here, you will need to migrate AssociatedStmt to the same level to make children() correctly return list of child stmt/exprs. I don't know what's your plan/design approach, so it is absolutely up to you for now. I'm just bringing this to make you aware of possible issues here.
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.
Thanks! I'll keep it in mind... I presume there will be quite a few things that end up getting 'moved around' a bit as I get deeper into implementation, so this is good to know!
There are only two constructs with a directly associated expression (
cache
andwait
) so those might need trailing storage (and there isroutine
, but that is an associated name which will turn into a decl), but neither of those have an associated stmt.