Skip to content

Commit ff219ea

Browse files
authored
[OpenACC] Initial commits to support OpenACC (#70234)
Initial commits to support OpenACC. This patchset: adds a clang-command line argument '-fopenacc', and starts to define _OPENACC, albeit to '1' instead of the standardized value (since we don't properly implement OpenACC yet). The OpenACC spec defines `_OPENACC` to be equal to the latest standard implemented. However, since we're not done implementing any standard, we've defined this by default to be `1`. As it is useful to run our compiler against existing OpenACC workloads, we're providing a temporary override flag to change the `_OPENACC` value to be any entirely digit value, permitting testing against any existing OpenACC project. Exactly like the OpenMP parser, the OpenACC pragma parser needs to consume and reprocess the tokens. This patch sets up the infrastructure to do so by refactoring the OpenMP version of this into a more general version that works for OpenACC as well. Additionally, this adds a few diagnostics and token kinds to get us started.
1 parent 9c0e649 commit ff219ea

25 files changed

+304
-27
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ New Compiler Flags
241241
handlers will be smaller. A throw expression of a type with a
242242
potentially-throwing destructor will lead to an error.
243243

244+
* ``-fopenacc`` was added as a part of the effort to support OpenACC in clang.
245+
244246
Deprecated Compiler Flags
245247
-------------------------
246248

@@ -731,6 +733,17 @@ Miscellaneous Clang Crashes Fixed
731733
- Fixed a crash when ``-ast-dump=json`` was used for code using class
732734
template deduction guides.
733735

736+
OpenACC Specific Changes
737+
------------------------
738+
- OpenACC Implementation effort is beginning with semantic analysis and parsing
739+
of OpenACC pragmas. The ``-fopenacc`` flag was added to enable these new,
740+
albeit incomplete changes. The ``_OPENACC`` macro is currently defined to
741+
``1``, as support is too incomplete to update to a standards-required value.
742+
- Added ``-fexperimental-openacc-macro-override``, a command line option to
743+
permit overriding the ``_OPENACC`` macro to be any digit-only value specified
744+
by the user, which permits testing the compiler against existing OpenACC
745+
workloads in order to evaluate implementation progress.
746+
734747
Target Specific Changes
735748
-----------------------
736749

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,6 +1315,10 @@ def OpenMP : DiagGroup<"openmp", [
13151315
OpenMPMapping, OpenMP51Ext, OpenMPExtensions, OpenMPTargetException
13161316
]>;
13171317

1318+
// OpenACC warnings.
1319+
def SourceUsesOpenACC : DiagGroup<"source-uses-openacc">;
1320+
def OpenACC : DiagGroup<"openacc", [SourceUsesOpenACC]>;
1321+
13181322
// Backend warnings.
13191323
def BackendInlineAsm : DiagGroup<"inline-asm">;
13201324
def BackendSourceMgr : DiagGroup<"source-mgr">;

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,15 @@ def err_opencl_logical_exclusive_or : Error<
13421342
def err_openclcxx_virtual_function : Error<
13431343
"virtual functions are not supported in C++ for OpenCL">;
13441344

1345+
// OpenACC Support.
1346+
def warn_pragma_acc_ignored : Warning<
1347+
"unexpected '#pragma acc ...' in program">, InGroup<SourceUsesOpenACC>, DefaultIgnore;
1348+
def err_acc_unexpected_directive : Error<
1349+
"unexpected OpenACC directive %select{|'#pragma acc %1'}0">;
1350+
def warn_pragma_acc_unimplemented
1351+
: Warning<"OpenACC directives not yet implemented, pragma ignored">,
1352+
InGroup<SourceUsesOpenACC>;
1353+
13451354
// OpenMP support.
13461355
def warn_pragma_omp_ignored : Warning<
13471356
"unexpected '#pragma omp ...' in program">, InGroup<SourceUsesOpenMP>, DefaultIgnore;

clang/include/clang/Basic/LangOptions.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ LANGOPT(OffloadUniformBlock, 1, 0, "Assume that kernels are launched with unifor
285285
LANGOPT(HIPStdPar, 1, 0, "Enable Standard Parallel Algorithm Acceleration for HIP (experimental)")
286286
LANGOPT(HIPStdParInterposeAlloc, 1, 0, "Replace allocations / deallocations with HIP RT calls when Standard Parallel Algorithm Acceleration for HIP is enabled (Experimental)")
287287

288+
LANGOPT(OpenACC , 1, 0, "OpenACC Enabled")
289+
288290
LANGOPT(SizedDeallocation , 1, 0, "sized deallocation")
289291
LANGOPT(AlignedAllocation , 1, 0, "aligned allocation")
290292
LANGOPT(AlignedAllocationUnavailable, 1, 0, "aligned allocation functions are unavailable")

clang/include/clang/Basic/LangOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,11 @@ class LangOptions : public LangOptionsBase {
502502
// received as a result of a standard operator new (-fcheck-new)
503503
bool CheckNew = false;
504504

505+
// In OpenACC mode, contains a user provided override for the _OPENACC macro.
506+
// This exists so that we can override the macro value and test our incomplete
507+
// implementation on real-world examples.
508+
std::string OpenACCMacroOverride;
509+
505510
LangOptions();
506511

507512
/// Set language defaults for the given input language and

clang/include/clang/Basic/TokenKinds.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,12 @@ ANNOTATION(attr_openmp)
946946
PRAGMA_ANNOTATION(pragma_openmp)
947947
PRAGMA_ANNOTATION(pragma_openmp_end)
948948

949+
// Annotations for OpenACC pragma directives - #pragma acc.
950+
// Like with OpenMP, these are produced by the lexer when it parses a
951+
// #pragma acc directive so it can be handled during parsing of the directives.
952+
PRAGMA_ANNOTATION(pragma_openacc)
953+
PRAGMA_ANNOTATION(pragma_openacc_end)
954+
949955
// Annotations for loop pragma directives #pragma clang loop ...
950956
// The lexer produces these so that they only take effect when the parser
951957
// handles #pragma loop ... directives.

clang/include/clang/Driver/Options.td

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,19 @@ def fno_hip_emit_relocatable : Flag<["-"], "fno-hip-emit-relocatable">,
13591359
HelpText<"Do not override toolchain to compile HIP source to relocatable">;
13601360
}
13611361

1362+
// Clang specific/exclusive options for OpenACC.
1363+
def openacc_macro_override
1364+
: Separate<["-"], "fexperimental-openacc-macro-override">,
1365+
Visibility<[ClangOption, CC1Option]>,
1366+
Group<f_Group>,
1367+
HelpText<"Overrides the _OPENACC macro value for experimental testing "
1368+
"during OpenACC support development">;
1369+
def openacc_macro_override_EQ
1370+
: Joined<["-"], "fexperimental-openacc-macro-override=">,
1371+
Alias<openacc_macro_override>;
1372+
1373+
// End Clang specific/exclusive options for OpenACC.
1374+
13621375
def libomptarget_amdgpu_bc_path_EQ : Joined<["--"], "libomptarget-amdgpu-bc-path=">, Group<i_Group>,
13631376
HelpText<"Path to libomptarget-amdgcn bitcode library">;
13641377
def libomptarget_amdgcn_bc_path_EQ : Joined<["--"], "libomptarget-amdgcn-bc-path=">, Group<i_Group>,
@@ -3342,6 +3355,14 @@ def fno_openmp_target_debug : Flag<["-"], "fno-openmp-target-debug">;
33423355
} // let Visibility = [ClangOption, CC1Option, FC1Option]
33433356
} // let Flags = [NoArgumentUnused]
33443357

3358+
//===----------------------------------------------------------------------===//
3359+
// FlangOption + FC1 + ClangOption + CC1Option
3360+
//===----------------------------------------------------------------------===//
3361+
let Visibility = [FC1Option, FlangOption, CC1Option, ClangOption] in {
3362+
def fopenacc : Flag<["-"], "fopenacc">, Group<f_Group>,
3363+
HelpText<"Enable OpenACC">;
3364+
} // let Visibility = [FC1Option, FlangOption, CC1Option, ClangOption]
3365+
33453366
//===----------------------------------------------------------------------===//
33463367
// Optimisation remark options
33473368
//===----------------------------------------------------------------------===//
@@ -6266,8 +6287,6 @@ file}]>;
62666287
def ffixed_line_length_VALUE : Joined<["-"], "ffixed-line-length-">, Group<f_Group>, Alias<ffixed_line_length_EQ>;
62676288
def fconvert_EQ : Joined<["-"], "fconvert=">, Group<f_Group>,
62686289
HelpText<"Set endian conversion of data for unformatted files">;
6269-
def fopenacc : Flag<["-"], "fopenacc">, Group<f_Group>,
6270-
HelpText<"Enable OpenACC">;
62716290
def fdefault_double_8 : Flag<["-"],"fdefault-double-8">, Group<f_Group>,
62726291
HelpText<"Set the default double precision kind to an 8 byte wide type">;
62736292
def fdefault_integer_8 : Flag<["-"],"fdefault-integer-8">, Group<f_Group>,

clang/include/clang/Parse/Parser.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class Parser : public CodeCompletionHandler {
175175
std::unique_ptr<PragmaHandler> FPContractHandler;
176176
std::unique_ptr<PragmaHandler> OpenCLExtensionHandler;
177177
std::unique_ptr<PragmaHandler> OpenMPHandler;
178+
std::unique_ptr<PragmaHandler> OpenACCHandler;
178179
std::unique_ptr<PragmaHandler> PCSectionHandler;
179180
std::unique_ptr<PragmaHandler> MSCommentHandler;
180181
std::unique_ptr<PragmaHandler> MSDetectMismatchHandler;
@@ -3524,6 +3525,15 @@ class Parser : public CodeCompletionHandler {
35243525
/// where, map-type-modifier ::= always | close | mapper(mapper-identifier)
35253526
bool parseMapTypeModifiers(Sema::OpenMPVarListDataTy &Data);
35263527

3528+
//===--------------------------------------------------------------------===//
3529+
// OpenACC Parsing.
3530+
3531+
/// Placeholder for now, should just ignore the directives after emitting a
3532+
/// diagnostic. Eventually will be split into a few functions to parse
3533+
/// different situations.
3534+
DeclGroupPtrTy ParseOpenACCDirective();
3535+
StmtResult ParseOpenACCDirectiveStmt();
3536+
35273537
private:
35283538
//===--------------------------------------------------------------------===//
35293539
// C++ 14: Templates [temp]

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3633,6 +3633,23 @@ static void RenderHLSLOptions(const ArgList &Args, ArgStringList &CmdArgs,
36333633
CmdArgs.push_back("-finclude-default-header");
36343634
}
36353635

3636+
static void RenderOpenACCOptions(const Driver &D, const ArgList &Args,
3637+
ArgStringList &CmdArgs, types::ID InputType) {
3638+
if (!Args.hasArg(options::OPT_fopenacc))
3639+
return;
3640+
3641+
CmdArgs.push_back("-fopenacc");
3642+
3643+
if (Arg *A = Args.getLastArg(options::OPT_openacc_macro_override)) {
3644+
StringRef Value = A->getValue();
3645+
int Version;
3646+
if (!Value.getAsInteger(10, Version))
3647+
A->renderAsInput(Args, CmdArgs);
3648+
else
3649+
D.Diag(diag::err_drv_clang_unsupported) << Value;
3650+
}
3651+
}
3652+
36363653
static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args,
36373654
ArgStringList &CmdArgs) {
36383655
bool ARCMTEnabled = false;
@@ -6623,6 +6640,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
66236640
// Forward hlsl options to -cc1
66246641
RenderHLSLOptions(Args, CmdArgs, InputType);
66256642

6643+
// Forward OpenACC options to -cc1
6644+
RenderOpenACCOptions(D, Args, CmdArgs, InputType);
6645+
66266646
if (IsHIP) {
66276647
if (Args.hasFlag(options::OPT_fhip_new_launch_api,
66286648
options::OPT_fno_hip_new_launch_api, true))

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,6 +3549,13 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
35493549
if (Opts.OpenMPCUDAMode)
35503550
GenerateArg(Consumer, OPT_fopenmp_cuda_mode);
35513551

3552+
if (Opts.OpenACC) {
3553+
GenerateArg(Consumer, OPT_fopenacc);
3554+
if (!Opts.OpenACCMacroOverride.empty())
3555+
GenerateArg(Consumer, OPT_openacc_macro_override,
3556+
Opts.OpenACCMacroOverride);
3557+
}
3558+
35523559
// The arguments used to set Optimize, OptimizeSize and NoInlineDefine are
35533560
// generated from CodeGenOptions.
35543561

@@ -4018,6 +4025,14 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
40184025
(T.isNVPTX() || T.isAMDGCN()) &&
40194026
Args.hasArg(options::OPT_fopenmp_cuda_mode);
40204027

4028+
// OpenACC Configuration.
4029+
if (Args.hasArg(options::OPT_fopenacc)) {
4030+
Opts.OpenACC = true;
4031+
4032+
if (Arg *A = Args.getLastArg(options::OPT_openacc_macro_override))
4033+
Opts.OpenACCMacroOverride = A->getValue();
4034+
}
4035+
40214036
// FIXME: Eliminate this dependency.
40224037
unsigned Opt = getOptimizationLevel(Args, IK, Diags),
40234038
OptSize = getOptimizationLevelSize(Args);

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,17 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
605605
Builder.defineMacro("HIP_API_PER_THREAD_DEFAULT_STREAM");
606606
}
607607
}
608+
609+
if (LangOpts.OpenACC) {
610+
// FIXME: When we have full support for OpenACC, we should set this to the
611+
// version we support. Until then, set as '1' by default, but provide a
612+
// temporary mechanism for users to override this so real-world examples can
613+
// be tested against.
614+
if (!LangOpts.OpenACCMacroOverride.empty())
615+
Builder.defineMacro("_OPENACC", LangOpts.OpenACCMacroOverride);
616+
else
617+
Builder.defineMacro("_OPENACC", "1");
618+
}
608619
}
609620

610621
/// Initialize the predefined C++ language feature test macros defined in

clang/lib/Parse/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_clang_library(clangParse
2323
ParseTemplate.cpp
2424
ParseTentative.cpp
2525
Parser.cpp
26+
ParseOpenACC.cpp
2627

2728
LINK_LIBS
2829
clangAST

clang/lib/Parse/ParseDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4734,6 +4734,11 @@ void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
47344734
continue;
47354735
}
47364736

4737+
if (Tok.is(tok::annot_pragma_openacc)) {
4738+
ParseOpenACCDirective();
4739+
continue;
4740+
}
4741+
47374742
if (tok::isPragmaAnnotation(Tok.getKind())) {
47384743
Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl)
47394744
<< DeclSpec::getSpecifierName(

clang/lib/Parse/ParseDeclCXX.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3429,6 +3429,8 @@ Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclarationWithPragmas(
34293429
case tok::annot_pragma_openmp:
34303430
return ParseOpenMPDeclarativeDirectiveWithExtDecl(
34313431
AS, AccessAttrs, /*Delayed=*/true, TagType, TagDecl);
3432+
case tok::annot_pragma_openacc:
3433+
return ParseOpenACCDirective();
34323434

34333435
default:
34343436
if (tok::isPragmaAnnotation(Tok.getKind())) {

clang/lib/Parse/ParseOpenACC.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===--- ParseOpenACC.cpp - OpenACC-specific parsing support --------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements the parsing logic for OpenACC language features.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "clang/Parse/ParseDiagnostic.h"
14+
#include "clang/Parse/Parser.h"
15+
16+
using namespace clang;
17+
18+
Parser::DeclGroupPtrTy Parser::ParseOpenACCDirective() {
19+
Diag(Tok, diag::warn_pragma_acc_unimplemented);
20+
SkipUntil(tok::annot_pragma_openacc_end);
21+
return nullptr;
22+
}
23+
StmtResult Parser::ParseOpenACCDirectiveStmt() {
24+
Diag(Tok, diag::warn_pragma_acc_unimplemented);
25+
SkipUntil(tok::annot_pragma_openacc_end);
26+
return StmtEmpty();
27+
}

0 commit comments

Comments
 (0)