[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
This commit is contained in:
Krzysztof Parzyszek
2025-05-09 07:42:15 -05:00
committed by GitHub
parent 6094080d27
commit 41aa67488c
15 changed files with 96 additions and 56 deletions

View File

@@ -267,8 +267,9 @@ void OpenMPCounterVisitor::Post(const OmpScheduleClause::Kind &c) {
"type=" + std::string{OmpScheduleClause::EnumToString(c)} + ";";
}
void OpenMPCounterVisitor::Post(const OmpDirectiveNameModifier &c) {
clauseDetails +=
"name_modifier=" + llvm::omp::getOpenMPDirectiveName(c.v).str() + ";";
clauseDetails += "name_modifier=" +
llvm::omp::getOpenMPDirectiveName(c.v, llvm::omp::FallbackVersion).str() +
";";
}
void OpenMPCounterVisitor::Post(const OmpClause &c) {
PostClauseCommon(normalize_clause_name(c.source.ToString()));

View File

@@ -17,6 +17,7 @@
#include "flang/Common/idioms.h"
#include "flang/Common/indirection.h"
#include "flang/Support/Fortran.h"
#include "llvm/Frontend/OpenMP/OMP.h"
#include "llvm/Support/raw_ostream.h"
#include <string>
#include <type_traits>
@@ -545,8 +546,8 @@ public:
NODE(parser, OmpBeginSectionsDirective)
NODE(parser, OmpBlockDirective)
static std::string GetNodeName(const llvm::omp::Directive &x) {
return llvm::Twine(
"llvm::omp::Directive = ", llvm::omp::getOpenMPDirectiveName(x))
return llvm::Twine("llvm::omp::Directive = ",
llvm::omp::getOpenMPDirectiveName(x, llvm::omp::FallbackVersion))
.str();
}
NODE(parser, OmpClause)

View File

@@ -18,6 +18,10 @@ namespace llvm {
class raw_ostream;
}
namespace Fortran::common {
class LangOptions;
}
namespace Fortran::evaluate {
struct GenericExprWrapper;
struct GenericAssignmentWrapper;
@@ -47,15 +51,18 @@ struct AnalyzedObjectsAsFortran {
// Converts parsed program (or fragment) to out as Fortran.
template <typename A>
void Unparse(llvm::raw_ostream &out, const A &root,
Encoding encoding = Encoding::UTF_8, bool capitalizeKeywords = true,
bool backslashEscapes = true, preStatementType *preStatement = nullptr,
const common::LangOptions &langOpts, Encoding encoding = Encoding::UTF_8,
bool capitalizeKeywords = true, bool backslashEscapes = true,
preStatementType *preStatement = nullptr,
AnalyzedObjectsAsFortran * = nullptr);
extern template void Unparse(llvm::raw_ostream &out, const Program &program,
Encoding encoding, bool capitalizeKeywords, bool backslashEscapes,
const common::LangOptions &langOpts, Encoding encoding,
bool capitalizeKeywords, bool backslashEscapes,
preStatementType *preStatement, AnalyzedObjectsAsFortran *);
extern template void Unparse(llvm::raw_ostream &out, const Expr &expr,
Encoding encoding, bool capitalizeKeywords, bool backslashEscapes,
const common::LangOptions &langOpts, Encoding encoding,
bool capitalizeKeywords, bool backslashEscapes,
preStatementType *preStatement, AnalyzedObjectsAsFortran *);
} // namespace Fortran::parser

View File

@@ -16,6 +16,10 @@ namespace llvm {
class raw_ostream;
}
namespace Fortran::common {
class LangOptions;
}
namespace Fortran::parser {
struct Program;
}
@@ -23,6 +27,7 @@ struct Program;
namespace Fortran::semantics {
class SemanticsContext;
void UnparseWithSymbols(llvm::raw_ostream &, const parser::Program &,
const common::LangOptions &,
parser::Encoding encoding = parser::Encoding::UTF_8);
void UnparseWithModules(llvm::raw_ostream &, SemanticsContext &,
const parser::Program &,

View File

@@ -119,7 +119,7 @@ void debugUnparseNoSema(CompilerInstance &ci, llvm::raw_ostream &out) {
auto &parseTree{ci.getParsing().parseTree()};
// TODO: Options should come from CompilerInvocation
Unparse(out, *parseTree,
Unparse(out, *parseTree, ci.getInvocation().getLangOpts(),
/*encoding=*/parser::Encoding::UTF_8,
/*capitalizeKeywords=*/true, /*backslashEscapes=*/false,
/*preStatement=*/nullptr,
@@ -131,6 +131,7 @@ void debugUnparseWithSymbols(CompilerInstance &ci) {
auto &parseTree{*ci.getParsing().parseTree()};
semantics::UnparseWithSymbols(llvm::outs(), parseTree,
ci.getInvocation().getLangOpts(),
/*encoding=*/parser::Encoding::UTF_8);
}

View File

@@ -200,9 +200,11 @@ void ClauseProcessor::processTODO(mlir::Location currentLocation,
auto checkUnhandledClause = [&](llvm::omp::Clause id, const auto *x) {
if (!x)
return;
unsigned version = semaCtx.langOptions().OpenMPVersion;
TODO(currentLocation,
"Unhandled clause " + llvm::omp::getOpenMPClauseName(id).upper() +
" in " + llvm::omp::getOpenMPDirectiveName(directive).upper() +
" in " +
llvm::omp::getOpenMPDirectiveName(directive, version).upper() +
" construct");
};

View File

@@ -70,7 +70,7 @@ struct ConstructDecomposition {
namespace Fortran::lower::omp {
LLVM_DUMP_METHOD llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
const UnitConstruct &uc) {
os << llvm::omp::getOpenMPDirectiveName(uc.id);
os << llvm::omp::getOpenMPDirectiveName(uc.id, llvm::omp::FallbackVersion);
for (auto [index, clause] : llvm::enumerate(uc.clauses)) {
os << (index == 0 ? '\t' : ' ');
os << llvm::omp::getOpenMPClauseName(clause.id);

View File

@@ -3754,9 +3754,11 @@ static void genOMPDispatch(lower::AbstractConverter &converter,
item);
break;
case llvm::omp::Directive::OMPD_tile:
case llvm::omp::Directive::OMPD_unroll:
case llvm::omp::Directive::OMPD_unroll: {
unsigned version = semaCtx.langOptions().OpenMPVersion;
TODO(loc, "Unhandled loop directive (" +
llvm::omp::getOpenMPDirectiveName(dir) + ")");
llvm::omp::getOpenMPDirectiveName(dir, version) + ")");
}
// case llvm::omp::Directive::OMPD_workdistribute:
case llvm::omp::Directive::OMPD_workshare:
newOp = genWorkshareOp(converter, symTable, stmtCtx, semaCtx, eval, loc,

View File

@@ -125,7 +125,8 @@ OmpDirectiveNameParser::directives() const {
void OmpDirectiveNameParser::initTokens(NameWithId *table) const {
for (size_t i{0}, e{llvm::omp::Directive_enumSize}; i != e; ++i) {
auto id{static_cast<llvm::omp::Directive>(i)};
llvm::StringRef name{llvm::omp::getOpenMPDirectiveName(id)};
llvm::StringRef name{
llvm::omp::getOpenMPDirectiveName(id, llvm::omp::FallbackVersion)};
table[i] = std::make_pair(name.str(), id);
}
// Sort the table with respect to the directive name length in a descending

View File

@@ -11,6 +11,7 @@
#include "flang/Common/indirection.h"
#include "flang/Parser/tools.h"
#include "flang/Parser/user-state.h"
#include "llvm/Frontend/OpenMP/OMP.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
@@ -305,7 +306,9 @@ std::string OmpTraitSelectorName::ToString() const {
return std::string(EnumToString(v));
},
[&](llvm::omp::Directive d) {
return llvm::omp::getOpenMPDirectiveName(d).str();
return llvm::omp::getOpenMPDirectiveName(
d, llvm::omp::FallbackVersion)
.str();
},
[&](const std::string &s) { //
return s;

View File

@@ -17,6 +17,7 @@
#include "flang/Parser/parse-tree.h"
#include "flang/Parser/tools.h"
#include "flang/Support/Fortran.h"
#include "flang/Support/LangOptions.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cinttypes>
@@ -27,12 +28,14 @@ namespace Fortran::parser {
class UnparseVisitor {
public:
UnparseVisitor(llvm::raw_ostream &out, int indentationAmount,
Encoding encoding, bool capitalize, bool backslashEscapes,
preStatementType *preStatement, AnalyzedObjectsAsFortran *asFortran)
: out_{out}, indentationAmount_{indentationAmount}, encoding_{encoding},
capitalizeKeywords_{capitalize}, backslashEscapes_{backslashEscapes},
preStatement_{preStatement}, asFortran_{asFortran} {}
UnparseVisitor(llvm::raw_ostream &out, const common::LangOptions &langOpts,
int indentationAmount, Encoding encoding, bool capitalize,
bool backslashEscapes, preStatementType *preStatement,
AnalyzedObjectsAsFortran *asFortran)
: out_{out}, langOpts_{langOpts}, indentationAmount_{indentationAmount},
encoding_{encoding}, capitalizeKeywords_{capitalize},
backslashEscapes_{backslashEscapes}, preStatement_{preStatement},
asFortran_{asFortran} {}
// In nearly all cases, this code avoids defining Boolean-valued Pre()
// callbacks for the parse tree walking framework in favor of two void
@@ -2102,7 +2105,8 @@ public:
Walk(":", std::get<std::optional<OmpReductionCombiner>>(x.t));
}
void Unparse(const llvm::omp::Directive &x) {
Word(llvm::omp::getOpenMPDirectiveName(x).str());
unsigned ompVersion{langOpts_.OpenMPVersion};
Word(llvm::omp::getOpenMPDirectiveName(x, ompVersion).str());
}
void Unparse(const OmpDirectiveSpecification &x) {
auto unparseArgs{[&]() {
@@ -2167,7 +2171,8 @@ public:
x.u);
}
void Unparse(const OmpDirectiveNameModifier &x) {
Word(llvm::omp::getOpenMPDirectiveName(x.v));
unsigned ompVersion{langOpts_.OpenMPVersion};
Word(llvm::omp::getOpenMPDirectiveName(x.v, ompVersion));
}
void Unparse(const OmpIteratorSpecifier &x) {
Walk(std::get<TypeDeclarationStmt>(x.t));
@@ -3249,6 +3254,7 @@ private:
}
llvm::raw_ostream &out_;
const common::LangOptions &langOpts_;
int indent_{0};
const int indentationAmount_{1};
int column_{1};
@@ -3341,17 +3347,20 @@ void UnparseVisitor::Word(const std::string_view &str) {
}
template <typename A>
void Unparse(llvm::raw_ostream &out, const A &root, Encoding encoding,
void Unparse(llvm::raw_ostream &out, const A &root,
const common::LangOptions &langOpts, Encoding encoding,
bool capitalizeKeywords, bool backslashEscapes,
preStatementType *preStatement, AnalyzedObjectsAsFortran *asFortran) {
UnparseVisitor visitor{out, 1, encoding, capitalizeKeywords, backslashEscapes,
preStatement, asFortran};
UnparseVisitor visitor{out, langOpts, 1, encoding, capitalizeKeywords,
backslashEscapes, preStatement, asFortran};
Walk(root, visitor);
visitor.Done();
}
template void Unparse<Program>(llvm::raw_ostream &, const Program &, Encoding,
bool, bool, preStatementType *, AnalyzedObjectsAsFortran *);
template void Unparse<Expr>(llvm::raw_ostream &, const Expr &, Encoding, bool,
bool, preStatementType *, AnalyzedObjectsAsFortran *);
template void Unparse<Program>(llvm::raw_ostream &, const Program &,
const common::LangOptions &, Encoding, bool, bool, preStatementType *,
AnalyzedObjectsAsFortran *);
template void Unparse<Expr>(llvm::raw_ostream &, const Expr &,
const common::LangOptions &, Encoding, bool, bool, preStatementType *,
AnalyzedObjectsAsFortran *);
} // namespace Fortran::parser

View File

@@ -2434,8 +2434,7 @@ void OmpStructureChecker::Enter(
break;
default:
context_.Say(dirName.source, "%s is not a cancellable construct"_err_en_US,
parser::ToUpperCaseLetters(
llvm::omp::getOpenMPDirectiveName(dirName.v).str()));
parser::ToUpperCaseLetters(getDirectiveName(dirName.v).str()));
break;
}
}
@@ -2468,7 +2467,7 @@ std::optional<llvm::omp::Directive> OmpStructureChecker::GetCancelType(
// Given clauses from CANCEL or CANCELLATION_POINT, identify the construct
// to which the cancellation applies.
std::optional<llvm::omp::Directive> cancelee;
llvm::StringRef cancelName{llvm::omp::getOpenMPDirectiveName(cancelDir)};
llvm::StringRef cancelName{getDirectiveName(cancelDir)};
for (const parser::OmpClause &clause : maybeClauses->v) {
using CancellationConstructType =
@@ -2496,7 +2495,7 @@ std::optional<llvm::omp::Directive> OmpStructureChecker::GetCancelType(
void OmpStructureChecker::CheckCancellationNest(
const parser::CharBlock &source, llvm::omp::Directive type) {
llvm::StringRef typeName{llvm::omp::getOpenMPDirectiveName(type)};
llvm::StringRef typeName{getDirectiveName(type)};
if (CurrentDirectiveIsNested()) {
// If construct-type-clause is taskgroup, the cancellation construct must be
@@ -4060,10 +4059,10 @@ void OmpStructureChecker::Enter(const parser::OmpClause::If &x) {
if (auto *dnm{OmpGetUniqueModifier<parser::OmpDirectiveNameModifier>(
modifiers)}) {
llvm::omp::Directive sub{dnm->v};
std::string subName{parser::ToUpperCaseLetters(
llvm::omp::getOpenMPDirectiveName(sub).str())};
std::string dirName{parser::ToUpperCaseLetters(
llvm::omp::getOpenMPDirectiveName(dir).str())};
std::string subName{
parser::ToUpperCaseLetters(getDirectiveName(sub).str())};
std::string dirName{
parser::ToUpperCaseLetters(getDirectiveName(dir).str())};
parser::CharBlock modifierSource{OmpGetModifierSource(modifiers, dnm)};
auto desc{OmpGetDescriptor<parser::OmpDirectiveNameModifier>()};
@@ -5433,7 +5432,8 @@ llvm::StringRef OmpStructureChecker::getClauseName(llvm::omp::Clause clause) {
llvm::StringRef OmpStructureChecker::getDirectiveName(
llvm::omp::Directive directive) {
return llvm::omp::getOpenMPDirectiveName(directive);
unsigned version{context_.langOptions().OpenMPVersion};
return llvm::omp::getOpenMPDirectiveName(directive, version);
}
const Symbol *OmpStructureChecker::GetObjectSymbol(

View File

@@ -50,7 +50,7 @@ static void CollectSymbols(
const Scope &, SymbolVector &, SymbolVector &, SourceOrderedSymbolSet &);
static void PutPassName(llvm::raw_ostream &, const std::optional<SourceName> &);
static void PutInit(llvm::raw_ostream &, const Symbol &, const MaybeExpr &,
const parser::Expr *);
const parser::Expr *, SemanticsContext &);
static void PutInit(llvm::raw_ostream &, const MaybeIntExpr &);
static void PutBound(llvm::raw_ostream &, const Bound &);
static void PutShapeSpec(llvm::raw_ostream &, const ShapeSpec &);
@@ -605,7 +605,7 @@ void ModFileWriter::PutDECStructure(
}
decls_ << ref->name();
PutShape(decls_, object->shape(), '(', ')');
PutInit(decls_, *ref, object->init(), nullptr);
PutInit(decls_, *ref, object->init(), nullptr, context_);
emittedDECFields_.insert(*ref);
} else if (any) {
break; // any later use of this structure will use RECORD/str/
@@ -944,7 +944,8 @@ void ModFileWriter::PutObjectEntity(
getSymbolAttrsToWrite(symbol));
PutShape(os, details.shape(), '(', ')');
PutShape(os, details.coshape(), '[', ']');
PutInit(os, symbol, details.init(), details.unanalyzedPDTComponentInit());
PutInit(os, symbol, details.init(), details.unanalyzedPDTComponentInit(),
context_);
os << '\n';
if (auto tkr{GetIgnoreTKR(symbol)}; !tkr.empty()) {
os << "!dir$ ignore_tkr(";
@@ -1036,11 +1037,11 @@ void ModFileWriter::PutTypeParam(llvm::raw_ostream &os, const Symbol &symbol) {
}
void PutInit(llvm::raw_ostream &os, const Symbol &symbol, const MaybeExpr &init,
const parser::Expr *unanalyzed) {
const parser::Expr *unanalyzed, SemanticsContext &context) {
if (IsNamedConstant(symbol) || symbol.owner().IsDerivedType()) {
const char *assign{symbol.attrs().test(Attr::POINTER) ? "=>" : "="};
if (unanalyzed) {
parser::Unparse(os << assign, *unanalyzed);
parser::Unparse(os << assign, *unanalyzed, context.langOptions());
} else if (init) {
init->AsFortran(os << assign);
}

View File

@@ -1887,6 +1887,7 @@ std::int64_t OmpAttributeVisitor::GetAssociatedLoopLevelFromClauses(
// construct with multiple associated do-loops are lastprivate.
void OmpAttributeVisitor::PrivatizeAssociatedLoopIndexAndCheckLoopLevel(
const parser::OpenMPLoopConstruct &x) {
unsigned version{context_.langOptions().OpenMPVersion};
std::int64_t level{GetContext().associatedLoopLevel};
if (level <= 0) {
return;
@@ -1922,7 +1923,8 @@ void OmpAttributeVisitor::PrivatizeAssociatedLoopIndexAndCheckLoopLevel(
context_.Say(GetContext().directiveSource,
"A DO loop must follow the %s directive"_err_en_US,
parser::ToUpperCaseLetters(
llvm::omp::getOpenMPDirectiveName(GetContext().directive).str()));
llvm::omp::getOpenMPDirectiveName(GetContext().directive, version)
.str()));
}
}
void OmpAttributeVisitor::CheckAssocLoopLevel(
@@ -2442,6 +2444,7 @@ static bool SymbolOrEquivalentIsInNamelist(const Symbol &symbol) {
void OmpAttributeVisitor::ResolveOmpObject(
const parser::OmpObject &ompObject, Symbol::Flag ompFlag) {
unsigned version{context_.langOptions().OpenMPVersion};
common::visit(
common::visitors{
[&](const parser::Designator &designator) {
@@ -2464,7 +2467,7 @@ void OmpAttributeVisitor::ResolveOmpObject(
Symbol::OmpFlagToClauseName(secondOmpFlag),
parser::ToUpperCaseLetters(
llvm::omp::getOpenMPDirectiveName(
GetContext().directive)
GetContext().directive, version)
.str()));
}
};
@@ -2500,7 +2503,7 @@ void OmpAttributeVisitor::ResolveOmpObject(
"in which the %s directive appears"_err_en_US,
parser::ToUpperCaseLetters(
llvm::omp::getOpenMPDirectiveName(
GetContext().directive)
GetContext().directive, version)
.str()));
}
if (ompFlag == Symbol::Flag::OmpReduction) {
@@ -2924,6 +2927,7 @@ void OmpAttributeVisitor::CheckSourceLabel(const parser::Label &label) {
void OmpAttributeVisitor::CheckLabelContext(const parser::CharBlock source,
const parser::CharBlock target, std::optional<DirContext> sourceContext,
std::optional<DirContext> targetContext) {
unsigned version{context_.langOptions().OpenMPVersion};
if (targetContext &&
(!sourceContext ||
(sourceContext->scope != targetContext->scope &&
@@ -2932,8 +2936,8 @@ void OmpAttributeVisitor::CheckLabelContext(const parser::CharBlock source,
context_
.Say(source, "invalid branch into an OpenMP structured block"_err_en_US)
.Attach(target, "In the enclosing %s directive branched into"_en_US,
parser::ToUpperCaseLetters(
llvm::omp::getOpenMPDirectiveName(targetContext->directive)
parser::ToUpperCaseLetters(llvm::omp::getOpenMPDirectiveName(
targetContext->directive, version)
.str()));
}
if (sourceContext &&
@@ -2945,8 +2949,8 @@ void OmpAttributeVisitor::CheckLabelContext(const parser::CharBlock source,
.Say(source,
"invalid branch leaving an OpenMP structured block"_err_en_US)
.Attach(target, "Outside the enclosing %s directive"_en_US,
parser::ToUpperCaseLetters(
llvm::omp::getOpenMPDirectiveName(sourceContext->directive)
parser::ToUpperCaseLetters(llvm::omp::getOpenMPDirectiveName(
sourceContext->directive, version)
.str()));
}
}
@@ -2979,12 +2983,14 @@ void OmpAttributeVisitor::CheckNameInAllocateStmt(
}
}
}
unsigned version{context_.langOptions().OpenMPVersion};
context_.Say(source,
"Object '%s' in %s directive not "
"found in corresponding ALLOCATE statement"_err_en_US,
name.ToString(),
parser::ToUpperCaseLetters(
llvm::omp::getOpenMPDirectiveName(GetContext().directive).str()));
llvm::omp::getOpenMPDirectiveName(GetContext().directive, version)
.str()));
}
void OmpAttributeVisitor::AddOmpRequiresToScope(Scope &scope,
@@ -3030,9 +3036,10 @@ void OmpAttributeVisitor::IssueNonConformanceWarning(
llvm::omp::Directive D, parser::CharBlock source) {
std::string warnStr;
llvm::raw_string_ostream warnStrOS(warnStr);
unsigned version{context_.langOptions().OpenMPVersion};
warnStrOS << "OpenMP directive "
<< parser::ToUpperCaseLetters(
llvm::omp::getOpenMPDirectiveName(D).str())
llvm::omp::getOpenMPDirectiveName(D, version).str())
<< " has been deprecated";
auto setAlternativeStr = [&warnStrOS](llvm::StringRef alt) {

View File

@@ -107,13 +107,13 @@ void SymbolDumpVisitor::Post(const parser::Name &name) {
}
void UnparseWithSymbols(llvm::raw_ostream &out, const parser::Program &program,
parser::Encoding encoding) {
const common::LangOptions &langOpts, parser::Encoding encoding) {
SymbolDumpVisitor visitor;
parser::Walk(program, visitor);
parser::preStatementType preStatement{
[&](const parser::CharBlock &location, llvm::raw_ostream &out,
int indent) { visitor.PrintSymbols(location, out, indent); }};
parser::Unparse(out, program, encoding, false, true, &preStatement);
parser::Unparse(out, program, langOpts, encoding, false, true, &preStatement);
}
// UnparseWithModules()
@@ -150,6 +150,6 @@ void UnparseWithModules(llvm::raw_ostream &out, SemanticsContext &context,
for (SymbolRef moduleRef : visitor.modulesUsed()) {
writer.WriteClosure(out, *moduleRef, nonIntrinsicModulesWritten);
}
parser::Unparse(out, program, encoding, false, true);
parser::Unparse(out, program, context.langOptions(), encoding, false, true);
}
} // namespace Fortran::semantics