Skip to content

Commit 41aa674

Browse files
authored
[flang][OpenMP] Pass OpenMP version to getOpenMPDirectiveName (#139131)
The OpenMP version is stored in LangOptions in SemanticsContext. Use the fallback version where SemanticsContext is unavailable (mostly in case of debug dumps). RFC: https://discourse.llvm.org/t/rfc-alternative-spellings-of-openmp-directives/85507
1 parent 6094080 commit 41aa674

15 files changed

+96
-56
lines changed

flang/examples/FlangOmpReport/FlangOmpReportVisitor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,9 @@ void OpenMPCounterVisitor::Post(const OmpScheduleClause::Kind &c) {
267267
"type=" + std::string{OmpScheduleClause::EnumToString(c)} + ";";
268268
}
269269
void OpenMPCounterVisitor::Post(const OmpDirectiveNameModifier &c) {
270-
clauseDetails +=
271-
"name_modifier=" + llvm::omp::getOpenMPDirectiveName(c.v).str() + ";";
270+
clauseDetails += "name_modifier=" +
271+
llvm::omp::getOpenMPDirectiveName(c.v, llvm::omp::FallbackVersion).str() +
272+
";";
272273
}
273274
void OpenMPCounterVisitor::Post(const OmpClause &c) {
274275
PostClauseCommon(normalize_clause_name(c.source.ToString()));

flang/include/flang/Parser/dump-parse-tree.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "flang/Common/idioms.h"
1818
#include "flang/Common/indirection.h"
1919
#include "flang/Support/Fortran.h"
20+
#include "llvm/Frontend/OpenMP/OMP.h"
2021
#include "llvm/Support/raw_ostream.h"
2122
#include <string>
2223
#include <type_traits>
@@ -545,8 +546,8 @@ class ParseTreeDumper {
545546
NODE(parser, OmpBeginSectionsDirective)
546547
NODE(parser, OmpBlockDirective)
547548
static std::string GetNodeName(const llvm::omp::Directive &x) {
548-
return llvm::Twine(
549-
"llvm::omp::Directive = ", llvm::omp::getOpenMPDirectiveName(x))
549+
return llvm::Twine("llvm::omp::Directive = ",
550+
llvm::omp::getOpenMPDirectiveName(x, llvm::omp::FallbackVersion))
550551
.str();
551552
}
552553
NODE(parser, OmpClause)

flang/include/flang/Parser/unparse.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ namespace llvm {
1818
class raw_ostream;
1919
}
2020

21+
namespace Fortran::common {
22+
class LangOptions;
23+
}
24+
2125
namespace Fortran::evaluate {
2226
struct GenericExprWrapper;
2327
struct GenericAssignmentWrapper;
@@ -47,15 +51,18 @@ struct AnalyzedObjectsAsFortran {
4751
// Converts parsed program (or fragment) to out as Fortran.
4852
template <typename A>
4953
void Unparse(llvm::raw_ostream &out, const A &root,
50-
Encoding encoding = Encoding::UTF_8, bool capitalizeKeywords = true,
51-
bool backslashEscapes = true, preStatementType *preStatement = nullptr,
54+
const common::LangOptions &langOpts, Encoding encoding = Encoding::UTF_8,
55+
bool capitalizeKeywords = true, bool backslashEscapes = true,
56+
preStatementType *preStatement = nullptr,
5257
AnalyzedObjectsAsFortran * = nullptr);
5358

5459
extern template void Unparse(llvm::raw_ostream &out, const Program &program,
55-
Encoding encoding, bool capitalizeKeywords, bool backslashEscapes,
60+
const common::LangOptions &langOpts, Encoding encoding,
61+
bool capitalizeKeywords, bool backslashEscapes,
5662
preStatementType *preStatement, AnalyzedObjectsAsFortran *);
5763
extern template void Unparse(llvm::raw_ostream &out, const Expr &expr,
58-
Encoding encoding, bool capitalizeKeywords, bool backslashEscapes,
64+
const common::LangOptions &langOpts, Encoding encoding,
65+
bool capitalizeKeywords, bool backslashEscapes,
5966
preStatementType *preStatement, AnalyzedObjectsAsFortran *);
6067
} // namespace Fortran::parser
6168

flang/include/flang/Semantics/unparse-with-symbols.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ namespace llvm {
1616
class raw_ostream;
1717
}
1818

19+
namespace Fortran::common {
20+
class LangOptions;
21+
}
22+
1923
namespace Fortran::parser {
2024
struct Program;
2125
}
2226

2327
namespace Fortran::semantics {
2428
class SemanticsContext;
2529
void UnparseWithSymbols(llvm::raw_ostream &, const parser::Program &,
30+
const common::LangOptions &,
2631
parser::Encoding encoding = parser::Encoding::UTF_8);
2732
void UnparseWithModules(llvm::raw_ostream &, SemanticsContext &,
2833
const parser::Program &,

flang/lib/Frontend/ParserActions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void debugUnparseNoSema(CompilerInstance &ci, llvm::raw_ostream &out) {
119119
auto &parseTree{ci.getParsing().parseTree()};
120120

121121
// TODO: Options should come from CompilerInvocation
122-
Unparse(out, *parseTree,
122+
Unparse(out, *parseTree, ci.getInvocation().getLangOpts(),
123123
/*encoding=*/parser::Encoding::UTF_8,
124124
/*capitalizeKeywords=*/true, /*backslashEscapes=*/false,
125125
/*preStatement=*/nullptr,
@@ -131,6 +131,7 @@ void debugUnparseWithSymbols(CompilerInstance &ci) {
131131
auto &parseTree{*ci.getParsing().parseTree()};
132132

133133
semantics::UnparseWithSymbols(llvm::outs(), parseTree,
134+
ci.getInvocation().getLangOpts(),
134135
/*encoding=*/parser::Encoding::UTF_8);
135136
}
136137

flang/lib/Lower/OpenMP/ClauseProcessor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,11 @@ void ClauseProcessor::processTODO(mlir::Location currentLocation,
200200
auto checkUnhandledClause = [&](llvm::omp::Clause id, const auto *x) {
201201
if (!x)
202202
return;
203+
unsigned version = semaCtx.langOptions().OpenMPVersion;
203204
TODO(currentLocation,
204205
"Unhandled clause " + llvm::omp::getOpenMPClauseName(id).upper() +
205-
" in " + llvm::omp::getOpenMPDirectiveName(directive).upper() +
206+
" in " +
207+
llvm::omp::getOpenMPDirectiveName(directive, version).upper() +
206208
" construct");
207209
};
208210

flang/lib/Lower/OpenMP/Decomposer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct ConstructDecomposition {
7070
namespace Fortran::lower::omp {
7171
LLVM_DUMP_METHOD llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
7272
const UnitConstruct &uc) {
73-
os << llvm::omp::getOpenMPDirectiveName(uc.id);
73+
os << llvm::omp::getOpenMPDirectiveName(uc.id, llvm::omp::FallbackVersion);
7474
for (auto [index, clause] : llvm::enumerate(uc.clauses)) {
7575
os << (index == 0 ? '\t' : ' ');
7676
os << llvm::omp::getOpenMPClauseName(clause.id);

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3754,9 +3754,11 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
37543754
item);
37553755
break;
37563756
case llvm::omp::Directive::OMPD_tile:
3757-
case llvm::omp::Directive::OMPD_unroll:
3757+
case llvm::omp::Directive::OMPD_unroll: {
3758+
unsigned version = semaCtx.langOptions().OpenMPVersion;
37583759
TODO(loc, "Unhandled loop directive (" +
3759-
llvm::omp::getOpenMPDirectiveName(dir) + ")");
3760+
llvm::omp::getOpenMPDirectiveName(dir, version) + ")");
3761+
}
37603762
// case llvm::omp::Directive::OMPD_workdistribute:
37613763
case llvm::omp::Directive::OMPD_workshare:
37623764
newOp = genWorkshareOp(converter, symTable, stmtCtx, semaCtx, eval, loc,

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ OmpDirectiveNameParser::directives() const {
125125
void OmpDirectiveNameParser::initTokens(NameWithId *table) const {
126126
for (size_t i{0}, e{llvm::omp::Directive_enumSize}; i != e; ++i) {
127127
auto id{static_cast<llvm::omp::Directive>(i)};
128-
llvm::StringRef name{llvm::omp::getOpenMPDirectiveName(id)};
128+
llvm::StringRef name{
129+
llvm::omp::getOpenMPDirectiveName(id, llvm::omp::FallbackVersion)};
129130
table[i] = std::make_pair(name.str(), id);
130131
}
131132
// Sort the table with respect to the directive name length in a descending

flang/lib/Parser/parse-tree.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "flang/Common/indirection.h"
1212
#include "flang/Parser/tools.h"
1313
#include "flang/Parser/user-state.h"
14+
#include "llvm/Frontend/OpenMP/OMP.h"
1415
#include "llvm/Support/raw_ostream.h"
1516
#include <algorithm>
1617

@@ -305,7 +306,9 @@ std::string OmpTraitSelectorName::ToString() const {
305306
return std::string(EnumToString(v));
306307
},
307308
[&](llvm::omp::Directive d) {
308-
return llvm::omp::getOpenMPDirectiveName(d).str();
309+
return llvm::omp::getOpenMPDirectiveName(
310+
d, llvm::omp::FallbackVersion)
311+
.str();
309312
},
310313
[&](const std::string &s) { //
311314
return s;

flang/lib/Parser/unparse.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "flang/Parser/parse-tree.h"
1818
#include "flang/Parser/tools.h"
1919
#include "flang/Support/Fortran.h"
20+
#include "flang/Support/LangOptions.h"
2021
#include "llvm/Support/raw_ostream.h"
2122
#include <algorithm>
2223
#include <cinttypes>
@@ -27,12 +28,14 @@ namespace Fortran::parser {
2728

2829
class UnparseVisitor {
2930
public:
30-
UnparseVisitor(llvm::raw_ostream &out, int indentationAmount,
31-
Encoding encoding, bool capitalize, bool backslashEscapes,
32-
preStatementType *preStatement, AnalyzedObjectsAsFortran *asFortran)
33-
: out_{out}, indentationAmount_{indentationAmount}, encoding_{encoding},
34-
capitalizeKeywords_{capitalize}, backslashEscapes_{backslashEscapes},
35-
preStatement_{preStatement}, asFortran_{asFortran} {}
31+
UnparseVisitor(llvm::raw_ostream &out, const common::LangOptions &langOpts,
32+
int indentationAmount, Encoding encoding, bool capitalize,
33+
bool backslashEscapes, preStatementType *preStatement,
34+
AnalyzedObjectsAsFortran *asFortran)
35+
: out_{out}, langOpts_{langOpts}, indentationAmount_{indentationAmount},
36+
encoding_{encoding}, capitalizeKeywords_{capitalize},
37+
backslashEscapes_{backslashEscapes}, preStatement_{preStatement},
38+
asFortran_{asFortran} {}
3639

3740
// In nearly all cases, this code avoids defining Boolean-valued Pre()
3841
// callbacks for the parse tree walking framework in favor of two void
@@ -2102,7 +2105,8 @@ class UnparseVisitor {
21022105
Walk(":", std::get<std::optional<OmpReductionCombiner>>(x.t));
21032106
}
21042107
void Unparse(const llvm::omp::Directive &x) {
2105-
Word(llvm::omp::getOpenMPDirectiveName(x).str());
2108+
unsigned ompVersion{langOpts_.OpenMPVersion};
2109+
Word(llvm::omp::getOpenMPDirectiveName(x, ompVersion).str());
21062110
}
21072111
void Unparse(const OmpDirectiveSpecification &x) {
21082112
auto unparseArgs{[&]() {
@@ -2167,7 +2171,8 @@ class UnparseVisitor {
21672171
x.u);
21682172
}
21692173
void Unparse(const OmpDirectiveNameModifier &x) {
2170-
Word(llvm::omp::getOpenMPDirectiveName(x.v));
2174+
unsigned ompVersion{langOpts_.OpenMPVersion};
2175+
Word(llvm::omp::getOpenMPDirectiveName(x.v, ompVersion));
21712176
}
21722177
void Unparse(const OmpIteratorSpecifier &x) {
21732178
Walk(std::get<TypeDeclarationStmt>(x.t));
@@ -3249,6 +3254,7 @@ class UnparseVisitor {
32493254
}
32503255

32513256
llvm::raw_ostream &out_;
3257+
const common::LangOptions &langOpts_;
32523258
int indent_{0};
32533259
const int indentationAmount_{1};
32543260
int column_{1};
@@ -3341,17 +3347,20 @@ void UnparseVisitor::Word(const std::string_view &str) {
33413347
}
33423348

33433349
template <typename A>
3344-
void Unparse(llvm::raw_ostream &out, const A &root, Encoding encoding,
3350+
void Unparse(llvm::raw_ostream &out, const A &root,
3351+
const common::LangOptions &langOpts, Encoding encoding,
33453352
bool capitalizeKeywords, bool backslashEscapes,
33463353
preStatementType *preStatement, AnalyzedObjectsAsFortran *asFortran) {
3347-
UnparseVisitor visitor{out, 1, encoding, capitalizeKeywords, backslashEscapes,
3348-
preStatement, asFortran};
3354+
UnparseVisitor visitor{out, langOpts, 1, encoding, capitalizeKeywords,
3355+
backslashEscapes, preStatement, asFortran};
33493356
Walk(root, visitor);
33503357
visitor.Done();
33513358
}
33523359

3353-
template void Unparse<Program>(llvm::raw_ostream &, const Program &, Encoding,
3354-
bool, bool, preStatementType *, AnalyzedObjectsAsFortran *);
3355-
template void Unparse<Expr>(llvm::raw_ostream &, const Expr &, Encoding, bool,
3356-
bool, preStatementType *, AnalyzedObjectsAsFortran *);
3360+
template void Unparse<Program>(llvm::raw_ostream &, const Program &,
3361+
const common::LangOptions &, Encoding, bool, bool, preStatementType *,
3362+
AnalyzedObjectsAsFortran *);
3363+
template void Unparse<Expr>(llvm::raw_ostream &, const Expr &,
3364+
const common::LangOptions &, Encoding, bool, bool, preStatementType *,
3365+
AnalyzedObjectsAsFortran *);
33573366
} // namespace Fortran::parser

flang/lib/Semantics/check-omp-structure.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,8 +2434,7 @@ void OmpStructureChecker::Enter(
24342434
break;
24352435
default:
24362436
context_.Say(dirName.source, "%s is not a cancellable construct"_err_en_US,
2437-
parser::ToUpperCaseLetters(
2438-
llvm::omp::getOpenMPDirectiveName(dirName.v).str()));
2437+
parser::ToUpperCaseLetters(getDirectiveName(dirName.v).str()));
24392438
break;
24402439
}
24412440
}
@@ -2468,7 +2467,7 @@ std::optional<llvm::omp::Directive> OmpStructureChecker::GetCancelType(
24682467
// Given clauses from CANCEL or CANCELLATION_POINT, identify the construct
24692468
// to which the cancellation applies.
24702469
std::optional<llvm::omp::Directive> cancelee;
2471-
llvm::StringRef cancelName{llvm::omp::getOpenMPDirectiveName(cancelDir)};
2470+
llvm::StringRef cancelName{getDirectiveName(cancelDir)};
24722471

24732472
for (const parser::OmpClause &clause : maybeClauses->v) {
24742473
using CancellationConstructType =
@@ -2496,7 +2495,7 @@ std::optional<llvm::omp::Directive> OmpStructureChecker::GetCancelType(
24962495

24972496
void OmpStructureChecker::CheckCancellationNest(
24982497
const parser::CharBlock &source, llvm::omp::Directive type) {
2499-
llvm::StringRef typeName{llvm::omp::getOpenMPDirectiveName(type)};
2498+
llvm::StringRef typeName{getDirectiveName(type)};
25002499

25012500
if (CurrentDirectiveIsNested()) {
25022501
// If construct-type-clause is taskgroup, the cancellation construct must be
@@ -4060,10 +4059,10 @@ void OmpStructureChecker::Enter(const parser::OmpClause::If &x) {
40604059
if (auto *dnm{OmpGetUniqueModifier<parser::OmpDirectiveNameModifier>(
40614060
modifiers)}) {
40624061
llvm::omp::Directive sub{dnm->v};
4063-
std::string subName{parser::ToUpperCaseLetters(
4064-
llvm::omp::getOpenMPDirectiveName(sub).str())};
4065-
std::string dirName{parser::ToUpperCaseLetters(
4066-
llvm::omp::getOpenMPDirectiveName(dir).str())};
4062+
std::string subName{
4063+
parser::ToUpperCaseLetters(getDirectiveName(sub).str())};
4064+
std::string dirName{
4065+
parser::ToUpperCaseLetters(getDirectiveName(dir).str())};
40674066

40684067
parser::CharBlock modifierSource{OmpGetModifierSource(modifiers, dnm)};
40694068
auto desc{OmpGetDescriptor<parser::OmpDirectiveNameModifier>()};
@@ -5433,7 +5432,8 @@ llvm::StringRef OmpStructureChecker::getClauseName(llvm::omp::Clause clause) {
54335432

54345433
llvm::StringRef OmpStructureChecker::getDirectiveName(
54355434
llvm::omp::Directive directive) {
5436-
return llvm::omp::getOpenMPDirectiveName(directive);
5435+
unsigned version{context_.langOptions().OpenMPVersion};
5436+
return llvm::omp::getOpenMPDirectiveName(directive, version);
54375437
}
54385438

54395439
const Symbol *OmpStructureChecker::GetObjectSymbol(

flang/lib/Semantics/mod-file.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static void CollectSymbols(
5050
const Scope &, SymbolVector &, SymbolVector &, SourceOrderedSymbolSet &);
5151
static void PutPassName(llvm::raw_ostream &, const std::optional<SourceName> &);
5252
static void PutInit(llvm::raw_ostream &, const Symbol &, const MaybeExpr &,
53-
const parser::Expr *);
53+
const parser::Expr *, SemanticsContext &);
5454
static void PutInit(llvm::raw_ostream &, const MaybeIntExpr &);
5555
static void PutBound(llvm::raw_ostream &, const Bound &);
5656
static void PutShapeSpec(llvm::raw_ostream &, const ShapeSpec &);
@@ -605,7 +605,7 @@ void ModFileWriter::PutDECStructure(
605605
}
606606
decls_ << ref->name();
607607
PutShape(decls_, object->shape(), '(', ')');
608-
PutInit(decls_, *ref, object->init(), nullptr);
608+
PutInit(decls_, *ref, object->init(), nullptr, context_);
609609
emittedDECFields_.insert(*ref);
610610
} else if (any) {
611611
break; // any later use of this structure will use RECORD/str/
@@ -944,7 +944,8 @@ void ModFileWriter::PutObjectEntity(
944944
getSymbolAttrsToWrite(symbol));
945945
PutShape(os, details.shape(), '(', ')');
946946
PutShape(os, details.coshape(), '[', ']');
947-
PutInit(os, symbol, details.init(), details.unanalyzedPDTComponentInit());
947+
PutInit(os, symbol, details.init(), details.unanalyzedPDTComponentInit(),
948+
context_);
948949
os << '\n';
949950
if (auto tkr{GetIgnoreTKR(symbol)}; !tkr.empty()) {
950951
os << "!dir$ ignore_tkr(";
@@ -1036,11 +1037,11 @@ void ModFileWriter::PutTypeParam(llvm::raw_ostream &os, const Symbol &symbol) {
10361037
}
10371038

10381039
void PutInit(llvm::raw_ostream &os, const Symbol &symbol, const MaybeExpr &init,
1039-
const parser::Expr *unanalyzed) {
1040+
const parser::Expr *unanalyzed, SemanticsContext &context) {
10401041
if (IsNamedConstant(symbol) || symbol.owner().IsDerivedType()) {
10411042
const char *assign{symbol.attrs().test(Attr::POINTER) ? "=>" : "="};
10421043
if (unanalyzed) {
1043-
parser::Unparse(os << assign, *unanalyzed);
1044+
parser::Unparse(os << assign, *unanalyzed, context.langOptions());
10441045
} else if (init) {
10451046
init->AsFortran(os << assign);
10461047
}

0 commit comments

Comments
 (0)