Skip to content

[OpenACC] Switch Clang to use the Flang 'appertainment' rules for cla… #135372

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 24 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2e5a137
[OpenACC] Switch Clang to use the Flang 'appertainment' rules for cla…
erichkeane Apr 10, 2025
2b05c62
Fix bug with 'exclusive clauses'
erichkeane Apr 11, 2025
82cb486
Starting effort to make tablegen specifically for clang.x
erichkeane Apr 11, 2025
ff1a7bd
Modify tablegen to emit something clang can just consume.
erichkeane Apr 11, 2025
b596122
Clang-format for tablegen changes
erichkeane Apr 11, 2025
e4eddde
Merge branch 'main' into ACCtdAppertainment
erichkeane Apr 11, 2025
73763be
Dealias clauses correctly, which should eliminate failures after tabl…
erichkeane Apr 11, 2025
4a6228b
Merge branch 'main' into ACCtdAppertainment
erichkeane Apr 11, 2025
8493154
Clang-format
erichkeane Apr 11, 2025
3a8a5bb
Merge remote-tracking branch 'origin/main' into ACCtdAppertainment
erichkeane Apr 11, 2025
5b50f08
Correct implementation of 'device_type' on routine
erichkeane Apr 11, 2025
8fb440a
Update 'init' and 'shutdown' tests to accept that multiples of 'if' a…
erichkeane Apr 12, 2025
dcec016
Allow multiple tile clauses, fix multiple 'collapse' checking
erichkeane Apr 14, 2025
3d27eba
correctly diagnose duplicate 'tile' and 'collapse' clauses
erichkeane Apr 14, 2025
ff18a19
Merge branch 'main' into ACCtdAppertainment
erichkeane Apr 15, 2025
71afd23
Restore the seq/gang/worker/vector combination rules on loops
erichkeane Apr 16, 2025
504b2a8
Implement 'bind' restrictions on routine like we previously did for
erichkeane Apr 16, 2025
5c42a53
Merge branch 'main' into ACCtdAppertainment
erichkeane Apr 18, 2025
21a295a
Merge branch 'main' into ACCtdAppertainment
erichkeane Apr 18, 2025
58aea76
Update init/shutdown tests now that we agree it should only have devi…
erichkeane Apr 18, 2025
24dc800
Implement 'new' num_gangs/worker/vector_length rules around device_ty…
erichkeane Apr 18, 2025
9f4a0d7
Remove unneeded forward decl
erichkeane Apr 18, 2025
8dc4d59
Fix space removed during directive-tablegen
erichkeane Apr 18, 2025
d43a702
Merge branch 'main' into ACCtdAppertainment
erichkeane Apr 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -12961,6 +12961,19 @@ def err_acc_clause_after_device_type
def err_acc_clause_cannot_combine
: Error<"OpenACC clause '%0' may not appear on the same construct as a "
"'%1' clause on a '%2' construct">;
def err_acc_clause_routine_cannot_combine_before_device_type
: Error<"OpenACC clause '%0' after 'device_type' clause on a 'routine' "
"conflicts with the '%1' clause before the first 'device_type'">;
def err_acc_clause_routine_cannot_combine_same_device_type
: Error<"OpenACC clause '%0' on a 'routine' directive conflicts with the "
"'%1' clause applying to the same 'device_type'">;
def err_acc_clause_routine_one_of_in_region
: Error<"OpenACC 'routine' construct must have at least one 'gang', 'seq', "
"'vector', or 'worker' clause that applies to each 'device_type'">;
def err_acc_clause_since_last_device_type
: Error<"OpenACC '%0' clause cannot appear more than once%select{| in a "
"'device_type' region}2 on a '%1' directive">;

def err_acc_reduction_num_gangs_conflict
: Error<"OpenACC '%1' clause %select{|with more than 1 argument }0may not "
"appear on a '%2' construct "
Expand All @@ -12978,9 +12991,6 @@ def note_acc_reduction_composite_member_loc : Note<"invalid field is here">;
def err_acc_loop_not_for_loop
: Error<"OpenACC '%0' construct can only be applied to a 'for' loop">;
def note_acc_construct_here : Note<"'%0' construct is here">;
def err_acc_loop_spec_conflict
: Error<"OpenACC clause '%0' on '%1' construct conflicts with previous "
"data dependence clause">;
def err_acc_collapse_loop_count
: Error<"OpenACC 'collapse' clause loop count must be a %select{constant "
"expression|positive integer value, evaluated to %1}0">;
Expand Down Expand Up @@ -13032,9 +13042,10 @@ def err_acc_gang_reduction_numgangs_conflict
"'%1' clause %select{inside a compute construct with a|and a}3 "
"'num_gangs' clause with more than one argument">;

def err_reduction_op_mismatch
def err_reduction_op_mismatch
: Error<"OpenACC 'reduction' variable must have the same operator in all "
"nested constructs (%0 vs %1)">;

def err_acc_loop_variable_type
: Error<"loop variable of loop associated with an OpenACC '%0' construct "
"must be of integer, pointer, or random-access-iterator type (is "
Expand Down
170 changes: 90 additions & 80 deletions clang/include/clang/Basic/OpenACCKinds.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,133 +202,140 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &Out,
return printOpenACCAtomicKind(Out, AK);
}

/// Represents the kind of an OpenACC clause.
/// Represents the kind of an OpenACC clause. Sorted alphabetically, since this
/// order ends up influencing the sorting of the list diagnostic.
enum class OpenACCClauseKind : uint8_t {
/// 'finalize' clause, allowed on 'exit data' directive.
Finalize,
/// 'if_present' clause, allowed on 'host_data' and 'update' directives.
IfPresent,
/// 'seq' clause, allowed on 'loop' and 'routine' directives.
Seq,
/// 'independent' clause, allowed on 'loop' directives.
Independent,
/// 'async' clause, allowed on Compute, Data, 'update', 'wait', and Combined
/// constructs.
Async,
/// 'attach' clause, allowed on Compute and Combined constructs, plus 'data'
/// and 'enter data'.
Attach,
/// 'auto' clause, allowed on 'loop' directives.
Auto,
/// 'worker' clause, allowed on 'loop', Combined, and 'routine' directives.
Worker,
/// 'vector' clause, allowed on 'loop', Combined, and 'routine' directives.
Vector,
/// 'nohost' clause, allowed on 'routine' directives.
NoHost,
/// 'default' clause, allowed on parallel, serial, kernel (and compound)
/// constructs.
Default,
/// 'if' clause, allowed on all the Compute Constructs, Data Constructs,
/// Executable Constructs, and Combined Constructs.
If,
/// 'self' clause, allowed on Compute and Combined Constructs, plus 'update'.
Self,
/// 'bind' clause, allowed on routine constructs.
Bind,
/// 'collapse' clause, allowed on 'loop' and Combined constructs.
Collapse,
/// 'copy' clause, allowed on Compute and Combined Constructs, plus 'data' and
/// 'declare'.
Copy,
/// 'copy' clause alias 'pcopy'. Preserved for diagnostic purposes.
PCopy,
/// 'copy' clause alias 'present_or_copy'. Preserved for diagnostic purposes.
PresentOrCopy,
/// 'use_device' clause, allowed on 'host_data' construct.
UseDevice,
/// 'attach' clause, allowed on Compute and Combined constructs, plus 'data'
/// and 'enter data'.
Attach,
/// 'copyin' clause, allowed on Compute and Combined constructs, plus 'data',
/// 'enter data', and 'declare'.
CopyIn,
/// 'copyin' clause alias 'pcopyin'. Preserved for diagnostic purposes.
PCopyIn,
/// 'copyin' clause alias 'present_or_copyin'. Preserved for diagnostic
/// purposes.
PresentOrCopyIn,
/// 'copyout' clause, allowed on Compute and Combined constructs, plus 'data',
/// 'exit data', and 'declare'.
CopyOut,
/// 'copyout' clause alias 'pcopyout'. Preserved for diagnostic purposes.
PCopyOut,
/// 'copyout' clause alias 'present_or_copyout'. Preserved for diagnostic
/// purposes.
PresentOrCopyOut,
/// 'create' clause, allowed on Compute and Combined constructs, plus 'data',
/// 'enter data', and 'declare'.
Create,
/// 'create' clause alias 'pcreate'. Preserved for diagnostic purposes.
PCreate,
/// 'create' clause alias 'present_or_create'. Preserved for diagnostic
/// purposes.
PresentOrCreate,
/// 'default' clause, allowed on parallel, serial, kernel (and compound)
/// constructs.
Default,
/// 'default_async' clause, allowed on 'set' construct.
DefaultAsync,
/// 'delete' clause, allowed on the 'exit data' construct.
Delete,
/// 'detach' clause, allowed on the 'exit data' construct.
Detach,
/// 'device' clause, allowed on the 'update' construct.
Device,
/// 'device_num' clause, allowed on 'init', 'shutdown', and 'set' constructs.
DeviceNum,
/// 'deviceptr' clause, allowed on Compute and Combined Constructs, plus
/// 'data' and 'declare'.
DevicePtr,
/// 'device_resident' clause, allowed on the 'declare' construct.
DeviceResident,
/// 'device_type' clause, allowed on Compute, 'data', 'init', 'shutdown',
/// 'set', update', 'loop', 'routine', and Combined constructs.
DeviceType,
/// 'dtype' clause, an alias for 'device_type', stored separately for
/// diagnostic purposes.
DType,
/// 'finalize' clause, allowed on 'exit data' directive.
Finalize,
/// 'firstprivate' clause, allowed on 'parallel', 'serial', 'parallel loop',
/// and 'serial loop' constructs.
FirstPrivate,
/// 'gang' clause, allowed on 'loop' and Combined constructs.
Gang,
/// 'host' clause, allowed on 'update' construct.
Host,
/// 'if' clause, allowed on all the Compute Constructs, Data Constructs,
/// Executable Constructs, and Combined Constructs.
If,
/// 'if_present' clause, allowed on 'host_data' and 'update' directives.
IfPresent,
/// 'independent' clause, allowed on 'loop' directives.
Independent,
/// 'link' clause, allowed on 'declare' construct.
Link,
/// 'no_create' clause, allowed on allowed on Compute and Combined constructs,
/// plus 'data'.
NoCreate,
/// 'nohost' clause, allowed on 'routine' directives.
NoHost,
/// 'num_gangs' clause, allowed on 'parallel', 'kernels', parallel loop', and
/// 'kernels loop' constructs.
NumGangs,
/// 'num_workers' clause, allowed on 'parallel', 'kernels', parallel loop',
/// and 'kernels loop' constructs.
NumWorkers,
/// 'present' clause, allowed on Compute and Combined constructs, plus 'data'
/// and 'declare'.
Present,
/// 'private' clause, allowed on 'parallel', 'serial', 'loop', 'parallel
/// loop', and 'serial loop' constructs.
Private,
/// 'copyout' clause, allowed on Compute and Combined constructs, plus 'data',
/// 'exit data', and 'declare'.
CopyOut,
/// 'copyout' clause alias 'pcopyout'. Preserved for diagnostic purposes.
PCopyOut,
/// 'copyout' clause alias 'present_or_copyout'. Preserved for diagnostic
/// purposes.
PresentOrCopyOut,
/// 'copyin' clause, allowed on Compute and Combined constructs, plus 'data',
/// 'enter data', and 'declare'.
CopyIn,
/// 'copyin' clause alias 'pcopyin'. Preserved for diagnostic purposes.
PCopyIn,
/// 'copyin' clause alias 'present_or_copyin'. Preserved for diagnostic
/// purposes.
PresentOrCopyIn,
/// 'create' clause, allowed on Compute and Combined constructs, plus 'data',
/// 'enter data', and 'declare'.
Create,
/// 'create' clause alias 'pcreate'. Preserved for diagnostic purposes.
PCreate,
/// 'create' clause alias 'present_or_create'. Preserved for diagnostic
/// purposes.
PresentOrCreate,
/// 'reduction' clause, allowed on Parallel, Serial, Loop, and the combined
/// constructs.
Reduction,
/// 'collapse' clause, allowed on 'loop' and Combined constructs.
Collapse,
/// 'bind' clause, allowed on routine constructs.
Bind,
/// 'self' clause, allowed on Compute and Combined Constructs, plus 'update'.
Self,
/// 'seq' clause, allowed on 'loop' and 'routine' directives.
Seq,
/// 'tile' clause, allowed on 'loop' and Combined constructs.
Tile,
/// 'use_device' clause, allowed on 'host_data' construct.
UseDevice,
/// 'vector' clause, allowed on 'loop', Combined, and 'routine' directives.
Vector,
/// 'vector_length' clause, allowed on 'parallel', 'kernels', 'parallel loop',
/// and 'kernels loop' constructs.
VectorLength,
/// 'num_gangs' clause, allowed on 'parallel', 'kernels', parallel loop', and
/// 'kernels loop' constructs.
NumGangs,
/// 'num_workers' clause, allowed on 'parallel', 'kernels', parallel loop',
/// and 'kernels loop' constructs.
NumWorkers,
/// 'device_num' clause, allowed on 'init', 'shutdown', and 'set' constructs.
DeviceNum,
/// 'default_async' clause, allowed on 'set' construct.
DefaultAsync,
/// 'device_type' clause, allowed on Compute, 'data', 'init', 'shutdown',
/// 'set', update', 'loop', 'routine', and Combined constructs.
DeviceType,
/// 'dtype' clause, an alias for 'device_type', stored separately for
/// diagnostic purposes.
DType,
/// 'async' clause, allowed on Compute, Data, 'update', 'wait', and Combined
/// constructs.
Async,
/// 'tile' clause, allowed on 'loop' and Combined constructs.
Tile,
/// 'gang' clause, allowed on 'loop' and Combined constructs.
Gang,
/// 'wait' clause, allowed on Compute, Data, 'update', and Combined
/// constructs.
Wait,
/// 'worker' clause, allowed on 'loop', Combined, and 'routine' directives.
Worker,

/// Represents an invalid clause, for the purposes of parsing.
/// 'shortloop' is represented in the ACC.td file, but isn't present in the
/// standard. This appears to be an old extension for the nvidia fortran
// compiler, but seemingly not elsewhere. Put it here as a placeholder, but it
// is never expected to be generated.
Shortloop,
/// Represents an invalid clause, for the purposes of parsing. Should be
/// 'last'.
Invalid,
};

Expand Down Expand Up @@ -485,6 +492,9 @@ inline StreamTy &printOpenACCClauseKind(StreamTy &Out, OpenACCClauseKind K) {
case OpenACCClauseKind::Wait:
return Out << "wait";

case OpenACCClauseKind::Shortloop:
llvm_unreachable("Shortloop shouldn't be generated in clang");
LLVM_FALLTHROUGH;
case OpenACCClauseKind::Invalid:
return Out << "<invalid>";
}
Expand Down
19 changes: 17 additions & 2 deletions clang/include/clang/Sema/SemaOpenACC.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class SemaOpenACC : public SemaBase {
/// Collapse has an 'N' count that makes it apply to a number of loops 'below'
/// it.
struct CollapseCheckingInfo {
OpenACCCollapseClause *ActiveCollapse = nullptr;
const OpenACCCollapseClause *ActiveCollapse = nullptr;

/// This is a value that maintains the current value of the 'N' on the
/// current collapse, minus the depth that has already been traversed. When
Expand Down Expand Up @@ -107,7 +107,7 @@ class SemaOpenACC : public SemaBase {
/// own counting of elements.
UnsignedOrNone CurTileCount = std::nullopt;

/// Records whether we've hit a 'CurTileCount' of '0' on the wya down,
/// Records whether we've hit a 'CurTileCount' of '0' on the way down,
/// which allows us to diagnose if the number of arguments is too large for
/// the current number of 'for' loops.
bool TileDepthSatisfied = true;
Expand Down Expand Up @@ -177,6 +177,21 @@ class SemaOpenACC : public SemaBase {

void CheckLastRoutineDeclNameConflict(const NamedDecl *ND);

bool DiagnoseRequiredClauses(OpenACCDirectiveKind DK, SourceLocation DirLoc,
ArrayRef<const OpenACCClause *> Clauses);

bool DiagnoseAllowedClauses(OpenACCDirectiveKind DK, OpenACCClauseKind CK,
SourceLocation ClauseLoc);

public:
// Needed from the visitor, so should be public.
bool DiagnoseAllowedOnceClauses(OpenACCDirectiveKind DK, OpenACCClauseKind CK,
SourceLocation ClauseLoc,
ArrayRef<const OpenACCClause *> Clauses);
bool DiagnoseExclusiveClauses(OpenACCDirectiveKind DK, OpenACCClauseKind CK,
SourceLocation ClauseLoc,
ArrayRef<const OpenACCClause *> Clauses);

public:
ComputeConstructInfo &getActiveComputeConstructInfo() {
return ActiveComputeConstructInfo;
Expand Down
11 changes: 4 additions & 7 deletions clang/lib/AST/OpenACCClause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,15 @@ OpenACCCollapseClause::OpenACCCollapseClause(SourceLocation BeginLoc,
SourceLocation EndLoc)
: OpenACCClauseWithSingleIntExpr(OpenACCClauseKind::Collapse, BeginLoc,
LParenLoc, LoopCount, EndLoc),
HasForce(HasForce) {
assert(LoopCount && "LoopCount required");
}
HasForce(HasForce) {}

OpenACCCollapseClause *
OpenACCCollapseClause::Create(const ASTContext &C, SourceLocation BeginLoc,
SourceLocation LParenLoc, bool HasForce,
Expr *LoopCount, SourceLocation EndLoc) {
assert(
LoopCount &&
(LoopCount->isInstantiationDependent() || isa<ConstantExpr>(LoopCount)) &&
"Loop count not constant expression");
assert((!LoopCount || (LoopCount->isInstantiationDependent() ||
isa<ConstantExpr>(LoopCount))) &&
"Loop count not constant expression");
void *Mem =
C.Allocate(sizeof(OpenACCCollapseClause), alignof(OpenACCCollapseClause));
return new (Mem)
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/TextNodeDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ void TextNodeDumper::Visit(const OpenACCClause *C) {
case OpenACCClauseKind::Vector:
case OpenACCClauseKind::VectorLength:
case OpenACCClauseKind::Invalid:
case OpenACCClauseKind::Shortloop:
// The condition expression will be printed as a part of the 'children',
// but print 'clause' here so it is clear what is happening from the dump.
OS << " clause";
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Parse/ParseOpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ ClauseParensKind getClauseParensKind(OpenACCDirectiveKind DirKind,
case OpenACCClauseKind::Tile:
return ClauseParensKind::Required;

case OpenACCClauseKind::Shortloop:
llvm_unreachable("Shortloop shouldn't be generated in clang");
case OpenACCClauseKind::Auto:
case OpenACCClauseKind::Finalize:
case OpenACCClauseKind::IfPresent:
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Sema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ add_clang_library(clangSema
SemaOpenACC.cpp
SemaOpenACCAtomic.cpp
SemaOpenACCClause.cpp
SemaOpenACCClauseAppertainment.cpp
SemaOpenCL.cpp
SemaOpenMP.cpp
SemaOverload.cpp
Expand Down
Loading
Loading