Skip to content

Commit

Permalink
[OpenACC] Implement beginning parts of the 'parallel' Sema impl (#81659)
Browse files Browse the repository at this point in the history
This patch Implements AST node creation and appertainment enforcement
for 'parallel', as well as changes the 'not implemented' messages to be
more specific. It does not deal with clauses/clause legality, nor a few
of the other rules from the standard, but this gets us most of the way
for a framework for future construct implementation.
  • Loading branch information
erichkeane committed Feb 16, 2024
1 parent 0da0966 commit cb89112
Show file tree
Hide file tree
Showing 16 changed files with 525 additions and 327 deletions.
22 changes: 11 additions & 11 deletions clang/include/clang/AST/StmtOpenACC.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ class OpenACCAssociatedStmtConstruct : public OpenACCConstructStmt {

protected:
OpenACCAssociatedStmtConstruct(StmtClass SC, OpenACCDirectiveKind K,
SourceLocation Start, SourceLocation End)
: OpenACCConstructStmt(SC, K, Start, End) {}
SourceLocation Start, SourceLocation End,
Stmt *AssocStmt)
: OpenACCConstructStmt(SC, K, Start, End), AssociatedStmt(AssocStmt) {}

void setAssociatedStmt(Stmt *S) { AssociatedStmt = S; }
Stmt *getAssociatedStmt() { return AssociatedStmt; }
Expand Down Expand Up @@ -105,14 +106,14 @@ class OpenACCComputeConstruct : public OpenACCAssociatedStmtConstruct {
friend class ASTStmtReader;
friend class ASTContext;
OpenACCComputeConstruct()
: OpenACCAssociatedStmtConstruct(OpenACCComputeConstructClass,
OpenACCDirectiveKind::Invalid,
SourceLocation{}, SourceLocation{}) {}
: OpenACCAssociatedStmtConstruct(
OpenACCComputeConstructClass, OpenACCDirectiveKind::Invalid,
SourceLocation{}, SourceLocation{}, /*AssociatedStmt=*/nullptr) {}

OpenACCComputeConstruct(OpenACCDirectiveKind K, SourceLocation Start,
SourceLocation End)
SourceLocation End, Stmt *StructuredBlock)
: OpenACCAssociatedStmtConstruct(OpenACCComputeConstructClass, K, Start,
End) {
End, StructuredBlock) {
assert((K == OpenACCDirectiveKind::Parallel ||
K == OpenACCDirectiveKind::Serial ||
K == OpenACCDirectiveKind::Kernels) &&
Expand All @@ -128,10 +129,9 @@ class OpenACCComputeConstruct : public OpenACCAssociatedStmtConstruct {
}

static OpenACCComputeConstruct *CreateEmpty(const ASTContext &C, EmptyShell);
static OpenACCComputeConstruct *Create(const ASTContext &C,
OpenACCDirectiveKind K,
SourceLocation BeginLoc,
SourceLocation EndLoc);
static OpenACCComputeConstruct *
Create(const ASTContext &C, OpenACCDirectiveKind K, SourceLocation BeginLoc,
SourceLocation EndLoc, Stmt *StructuredBlock);

Stmt *getStructuredBlock() { return getAssociatedStmt(); }
const Stmt *getStructuredBlock() const {
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "clang/AST/NSAPI.h"
#include "clang/AST/PrettyPrinter.h"
#include "clang/AST/StmtCXX.h"
#include "clang/AST/StmtOpenACC.h"
#include "clang/AST/StmtOpenMP.h"
#include "clang/AST/TypeLoc.h"
#include "clang/AST/TypeOrdering.h"
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "clang/AST/RawCommentList.h"
#include "clang/AST/RecordLayout.h"
#include "clang/AST/Stmt.h"
#include "clang/AST/StmtOpenACC.h"
#include "clang/AST/TemplateBase.h"
#include "clang/AST/TemplateName.h"
#include "clang/AST/Type.h"
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/AST/StmtOpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ OpenACCComputeConstruct::CreateEmpty(const ASTContext &C, EmptyShell) {

OpenACCComputeConstruct *
OpenACCComputeConstruct::Create(const ASTContext &C, OpenACCDirectiveKind K,
SourceLocation BeginLoc,
SourceLocation EndLoc) {
SourceLocation BeginLoc, SourceLocation EndLoc,
Stmt *StructuredBlock) {
void *Mem = C.Allocate(sizeof(OpenACCComputeConstruct),
alignof(OpenACCComputeConstruct));
auto *Inst = new (Mem) OpenACCComputeConstruct(K, BeginLoc, EndLoc);
auto *Inst =
new (Mem) OpenACCComputeConstruct(K, BeginLoc, EndLoc, StructuredBlock);
return Inst;
}
11 changes: 8 additions & 3 deletions clang/lib/Parse/ParseOpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,13 @@ void SkipUntilEndOfDirective(Parser &P) {
}

bool doesDirectiveHaveAssociatedStmt(OpenACCDirectiveKind DirKind) {
// TODO OpenACC: Implement.
return false;
switch (DirKind) {
default:
return false;
case OpenACCDirectiveKind::Parallel:
return true;
}
llvm_unreachable("Unhandled directive->assoc stmt");
}

} // namespace
Expand Down Expand Up @@ -1215,7 +1220,7 @@ StmtResult Parser::ParseOpenACCDirectiveStmt() {
ConsumeAnnotationToken();

OpenACCDirectiveParseInfo DirInfo = ParseOpenACCDirective();
if (getActions().ActOnStartOpenACCDeclDirective(DirInfo.DirKind,
if (getActions().ActOnStartOpenACCStmtDirective(DirInfo.DirKind,
DirInfo.StartLoc))
return StmtError();

Expand Down
56 changes: 52 additions & 4 deletions clang/lib/Sema/SemaOpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@
#include "clang/Sema/Sema.h"

using namespace clang;

namespace {
bool diagnoseConstructAppertainment(Sema &S, OpenACCDirectiveKind K,
SourceLocation StartLoc, bool IsStmt) {
switch (K) {
default:
case OpenACCDirectiveKind::Invalid:
// Nothing to do here, both invalid and unimplemented don't really need to
// do anything.
break;
case OpenACCDirectiveKind::Parallel:
if (!IsStmt)
return S.Diag(StartLoc, diag::err_acc_construct_appertainment) << K;
break;
}
return false;
}
} // namespace

bool Sema::ActOnOpenACCClause(OpenACCClauseKind ClauseKind,
SourceLocation StartLoc) {
if (ClauseKind == OpenACCClauseKind::Invalid)
Expand All @@ -35,6 +54,10 @@ void Sema::ActOnOpenACCConstruct(OpenACCDirectiveKind K,
// ensure that we can still work and don't check any construct-specific
// rules anywhere.
break;
case OpenACCDirectiveKind::Parallel:
// Nothing to do here, there is no real legalization that needs to happen
// here as these constructs do not take any arguments.
break;
default:
Diag(StartLoc, diag::warn_acc_construct_unimplemented) << K;
break;
Expand All @@ -43,24 +66,49 @@ void Sema::ActOnOpenACCConstruct(OpenACCDirectiveKind K,

bool Sema::ActOnStartOpenACCStmtDirective(OpenACCDirectiveKind K,
SourceLocation StartLoc) {
return true;
return diagnoseConstructAppertainment(*this, K, StartLoc, /*IsStmt=*/true);
}

StmtResult Sema::ActOnEndOpenACCStmtDirective(OpenACCDirectiveKind K,
SourceLocation StartLoc,
SourceLocation EndLoc,
StmtResult AssocStmt) {
return StmtEmpty();
switch (K) {
default:
return StmtEmpty();
case OpenACCDirectiveKind::Invalid:
return StmtError();
case OpenACCDirectiveKind::Parallel:
return OpenACCComputeConstruct::Create(
getASTContext(), K, StartLoc, EndLoc,
AssocStmt.isUsable() ? AssocStmt.get() : nullptr);
}
llvm_unreachable("Unhandled case in directive handling?");
}

StmtResult Sema::ActOnOpenACCAssociatedStmt(OpenACCDirectiveKind K,
StmtResult AssocStmt) {
return AssocStmt;
switch (K) {
default:
llvm_unreachable("Unimplemented associated statement application");
case OpenACCDirectiveKind::Parallel:
// There really isn't any checking here that could happen. As long as we
// have a statement to associate, this should be fine.
// OpenACC 3.3 Section 6:
// Structured Block: in C or C++, an executable statement, possibly
// compound, with a single entry at the top and a single exit at the
// bottom.
// FIXME: Should we reject DeclStmt's here? The standard isn't clear, and
// an interpretation of it is to allow this and treat the initializer as
// the 'structured block'.
return AssocStmt;
}
llvm_unreachable("Invalid associated statement application");
}

bool Sema::ActOnStartOpenACCDeclDirective(OpenACCDirectiveKind K,
SourceLocation StartLoc) {
return true;
return diagnoseConstructAppertainment(*this, K, StartLoc, /*IsStmt=*/false);
}

DeclGroupRef Sema::ActOnEndOpenACCDeclDirective() { return DeclGroupRef{}; }
11 changes: 10 additions & 1 deletion clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -4000,7 +4000,16 @@ class TreeTransform {
SourceLocation BeginLoc,
SourceLocation EndLoc,
StmtResult StrBlock) {
llvm_unreachable("Not yet implemented!");
getSema().ActOnOpenACCConstruct(K, BeginLoc);

// TODO OpenACC: Include clauses.
if (getSema().ActOnStartOpenACCStmtDirective(K, BeginLoc))
return StmtError();

StrBlock = getSema().ActOnOpenACCAssociatedStmt(K, StrBlock);

return getSema().ActOnEndOpenACCStmtDirective(K, BeginLoc, EndLoc,
StrBlock);
}

private:
Expand Down

0 comments on commit cb89112

Please sign in to comment.