Skip to content

[flang][OpenMP] Sema checks, lowering with new format of MAP modifiers #149137

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

Open
wants to merge 3 commits into
base: users/kparzysz/m01-map60-parser
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 8 additions & 8 deletions flang/lib/Lower/OpenMP/ClauseProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,8 @@ bool ClauseProcessor::processMap(
const parser::CharBlock &source) {
using Map = omp::clause::Map;
mlir::Location clauseLocation = converter.genLocation(source);
const auto &[mapType, typeMods, mappers, iterator, objects] = clause.t;
const auto &[mapType, typeMods, refMod, mappers, iterator, objects] =
clause.t;
llvm::omp::OpenMPOffloadMappingFlags mapTypeBits =
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_NONE;
std::string mapperIdName = "__implicit_mapper";
Expand All @@ -1342,16 +1343,13 @@ bool ClauseProcessor::processMap(
mapTypeBits |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_TO |
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_FROM;
break;
case Map::MapType::Alloc:
case Map::MapType::Release:
case Map::MapType::Storage:
// alloc and release is the default map_type for the Target Data
// Ops, i.e. if no bits for map_type is supplied then alloc/release
// is implicitly assumed based on the target directive. Default
// value for Target Data and Enter Data is alloc and for Exit Data
// it is release.
// (aka storage in 6.0+) is implicitly assumed based on the target
// directive. Default value for Target Data and Enter Data is alloc
// and for Exit Data it is release.
break;
case Map::MapType::Delete:
mapTypeBits |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_DELETE;
}

if (typeMods) {
Expand All @@ -1362,6 +1360,8 @@ bool ClauseProcessor::processMap(
mapTypeBits |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_PRESENT;
if (llvm::is_contained(*typeMods, Map::MapTypeModifier::Close))
mapTypeBits |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_CLOSE;
if (llvm::is_contained(*typeMods, Map::MapTypeModifier::Delete))
mapTypeBits |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_DELETE;
if (llvm::is_contained(*typeMods, Map::MapTypeModifier::OmpxHold))
mapTypeBits |= llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_OMPX_HOLD;
}
Expand Down
90 changes: 62 additions & 28 deletions flang/lib/Lower/OpenMP/Clauses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1003,12 +1003,13 @@ Map make(const parser::OmpClause::Map &inp,
CLAUSET_ENUM_CONVERT( //
convert1, parser::OmpMapType::Value, Map::MapType,
// clang-format off
MS(Alloc, Alloc)
MS(Delete, Delete)
MS(From, From)
MS(Release, Release)
MS(To, To)
MS(Tofrom, Tofrom)
MS(Alloc, Storage)
MS(Delete, Storage)
MS(Release, Storage)
MS(Storage, Storage)
MS(From, From)
MS(To, To)
MS(Tofrom, Tofrom)
// clang-format on
);

Expand All @@ -1022,43 +1023,76 @@ Map make(const parser::OmpClause::Map &inp,
// clang-format on
);

CLAUSET_ENUM_CONVERT( //
convert3, parser::OmpRefModifier::Value, Map::RefModifier,
// clang-format off
MS(Ref_Ptee, RefPtee)
MS(Ref_Ptr, RefPtr)
MS(Ref_Ptr_Ptee, RefPtrPtee)
// clang-format on
);

// Treat always, close, present, self, delete modifiers as map-type-
// modifiers.
auto &mods = semantics::OmpGetModifiers(inp.v);
auto *t1 = semantics::OmpGetUniqueModifier<parser::OmpMapper>(mods);
auto *t2 = semantics::OmpGetUniqueModifier<parser::OmpIterator>(mods);
auto *t3 = semantics::OmpGetUniqueModifier<parser::OmpMapType>(mods);
auto &t4 = std::get<parser::OmpObjectList>(inp.v.t);

auto mappers = [&]() -> std::optional<List<Mapper>> {
auto *t1 = semantics::OmpGetUniqueModifier<parser::OmpMapType>(mods);
auto &t2 = std::get<parser::OmpObjectList>(inp.v.t);

auto type = [&]() -> std::optional<Map::MapType> {
if (t1)
return List<Mapper>{Mapper{makeObject(t1->v, semaCtx)}};
return convert1(t1->v);
return std::nullopt;
}();

auto iterator = [&]() -> std::optional<Iterator> {
if (t2)
return makeIterator(*t2, semaCtx);
llvm::DenseSet<Map::MapTypeModifier> modSet;
if (t1 && t1->v == parser::OmpMapType::Value::Delete)
modSet.insert(Map::MapTypeModifier::Delete);

for (auto *typeMod :
semantics::OmpGetRepeatableModifier<parser::OmpMapTypeModifier>(mods)) {
modSet.insert(convert2(typeMod->v));
}
if (semantics::OmpGetUniqueModifier<parser::OmpAlwaysModifier>(mods))
modSet.insert(Map::MapTypeModifier::Always);
if (semantics::OmpGetUniqueModifier<parser::OmpCloseModifier>(mods))
modSet.insert(Map::MapTypeModifier::Close);
if (semantics::OmpGetUniqueModifier<parser::OmpDeleteModifier>(mods))
modSet.insert(Map::MapTypeModifier::Delete);
if (semantics::OmpGetUniqueModifier<parser::OmpPresentModifier>(mods))
modSet.insert(Map::MapTypeModifier::Present);
if (semantics::OmpGetUniqueModifier<parser::OmpSelfModifier>(mods))
modSet.insert(Map::MapTypeModifier::Self);
if (semantics::OmpGetUniqueModifier<parser::OmpxHoldModifier>(mods))
modSet.insert(Map::MapTypeModifier::OmpxHold);

std::optional<Map::MapTypeModifiers> maybeTypeMods{};
if (!modSet.empty())
maybeTypeMods = Map::MapTypeModifiers(modSet.begin(), modSet.end());

auto refMod = [&]() -> std::optional<Map::RefModifier> {
if (auto *t = semantics::OmpGetUniqueModifier<parser::OmpRefModifier>(mods))
return convert3(t->v);
return std::nullopt;
}();

auto type = [&]() -> std::optional<Map::MapType> {
if (t3)
return convert1(t3->v);
auto mappers = [&]() -> std::optional<List<Mapper>> {
if (auto *t = semantics::OmpGetUniqueModifier<parser::OmpMapper>(mods))
return List<Mapper>{Mapper{makeObject(t->v, semaCtx)}};
return std::nullopt;
}();

Map::MapTypeModifiers typeMods;
for (auto *typeMod :
semantics::OmpGetRepeatableModifier<parser::OmpMapTypeModifier>(mods)) {
typeMods.push_back(convert2(typeMod->v));
}
std::optional<Map::MapTypeModifiers> maybeTypeMods{};
if (!typeMods.empty())
maybeTypeMods = std::move(typeMods);
auto iterator = [&]() -> std::optional<Iterator> {
if (auto *t = semantics::OmpGetUniqueModifier<parser::OmpIterator>(mods))
return makeIterator(*t, semaCtx);
return std::nullopt;
}();

return Map{{/*MapType=*/std::move(type),
/*MapTypeModifiers=*/std::move(maybeTypeMods),
/*Mapper=*/std::move(mappers), /*Iterator=*/std::move(iterator),
/*LocatorList=*/makeObjects(t4, semaCtx)}};
/*RefModifier=*/std::move(refMod), /*Mapper=*/std::move(mappers),
/*Iterator=*/std::move(iterator),
/*LocatorList=*/makeObjects(t2, semaCtx)}};
}

Match make(const parser::OmpClause::Match &inp,
Expand Down
5 changes: 5 additions & 0 deletions flang/lib/Semantics/canonicalize-omp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ class CanonicalizationOfOmp {
// if the specified OpenMP version is less than 6.0, rewrite the affected
// modifiers back into the pre-6.0 forms.
void CanonicalizeMapModifiers(parser::OmpMapClause &map) {
unsigned version{context_.langOptions().OpenMPVersion};
if (version >= 60) {
return;
}

// Omp{Always, Close, Present, xHold}Modifier -> OmpMapTypeModifier
// OmpDeleteModifier -> OmpMapType
using Modifier = parser::OmpMapClause::Modifier;
Expand Down
107 changes: 71 additions & 36 deletions flang/lib/Semantics/check-omp-structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Frontend/OpenMP/OMP.h"

Expand Down Expand Up @@ -3399,23 +3400,22 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Detach &x) {
}
}

void OmpStructureChecker::CheckAllowedMapTypes(
const parser::OmpMapType::Value &type,
const std::list<parser::OmpMapType::Value> &allowedMapTypeList) {
if (!llvm::is_contained(allowedMapTypeList, type)) {
std::string commaSeparatedMapTypes;
llvm::interleave(
allowedMapTypeList.begin(), allowedMapTypeList.end(),
[&](const parser::OmpMapType::Value &mapType) {
commaSeparatedMapTypes.append(parser::ToUpperCaseLetters(
parser::OmpMapType::EnumToString(mapType)));
},
[&] { commaSeparatedMapTypes.append(", "); });
context_.Say(GetContext().clauseSource,
"Only the %s map types are permitted "
"for MAP clauses on the %s directive"_err_en_US,
commaSeparatedMapTypes, ContextDirectiveAsFortran());
void OmpStructureChecker::CheckAllowedMapTypes(parser::OmpMapType::Value type,
llvm::ArrayRef<parser::OmpMapType::Value> allowed) {
if (llvm::is_contained(allowed, type)) {
return;
}

llvm::SmallVector<std::string> names;
llvm::transform(
allowed, std::back_inserter(names), [](parser::OmpMapType::Value val) {
return parser::ToUpperCaseLetters(
parser::OmpMapType::EnumToString(val));
});
llvm::sort(names);
context_.Say(GetContext().clauseSource,
"Only the %s map types are permitted for MAP clauses on the %s directive"_err_en_US,
llvm::join(names, ", "), ContextDirectiveAsFortran());
}

void OmpStructureChecker::Enter(const parser::OmpClause::Map &x) {
Expand All @@ -3436,27 +3436,62 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Map &x) {
CheckIteratorModifier(*iter);
}
if (auto *type{OmpGetUniqueModifier<parser::OmpMapType>(modifiers)}) {
using Directive = llvm::omp::Directive;
using Value = parser::OmpMapType::Value;
switch (GetContext().directive) {
case llvm::omp::Directive::OMPD_target:
case llvm::omp::Directive::OMPD_target_teams:
case llvm::omp::Directive::OMPD_target_teams_distribute:
case llvm::omp::Directive::OMPD_target_teams_distribute_simd:
case llvm::omp::Directive::OMPD_target_teams_distribute_parallel_do:
case llvm::omp::Directive::OMPD_target_teams_distribute_parallel_do_simd:
case llvm::omp::Directive::OMPD_target_data:
CheckAllowedMapTypes(
type->v, {Value::To, Value::From, Value::Tofrom, Value::Alloc});
break;
case llvm::omp::Directive::OMPD_target_enter_data:
CheckAllowedMapTypes(type->v, {Value::To, Value::Alloc});
break;
case llvm::omp::Directive::OMPD_target_exit_data:
CheckAllowedMapTypes(
type->v, {Value::From, Value::Release, Value::Delete});
break;
default:
break;

static auto isValidForVersion{
[](parser::OmpMapType::Value t, unsigned version) {
switch (t) {
case parser::OmpMapType::Value::Alloc:
case parser::OmpMapType::Value::Delete:
case parser::OmpMapType::Value::Release:
return version < 60;
case parser::OmpMapType::Value::Storage:
return version >= 60;
default:
return true;
}
}};

llvm::SmallVector<parser::OmpMapType::Value> mapEnteringTypes{[&]() {
llvm::SmallVector<parser::OmpMapType::Value> result;
for (size_t i{0}; i != parser::OmpMapType::Value_enumSize; ++i) {
auto t{static_cast<parser::OmpMapType::Value>(i)};
if (isValidForVersion(t, version) && IsMapEnteringType(t)) {
result.push_back(t);
}
}
return result;
}()};
llvm::SmallVector<parser::OmpMapType::Value> mapExitingTypes{[&]() {
llvm::SmallVector<parser::OmpMapType::Value> result;
for (size_t i{0}; i != parser::OmpMapType::Value_enumSize; ++i) {
auto t{static_cast<parser::OmpMapType::Value>(i)};
if (isValidForVersion(t, version) && IsMapExitingType(t)) {
result.push_back(t);
}
}
return result;
}()};

llvm::omp::Directive dir{GetContext().directive};
llvm::ArrayRef<llvm::omp::Directive> leafs{
llvm::omp::getLeafConstructsOrSelf(dir)};

if (llvm::is_contained(leafs, Directive::OMPD_target) ||
llvm::is_contained(leafs, Directive::OMPD_target_data)) {
if (version >= 60) {
// Map types listed in the decay table. [6.0:276]
CheckAllowedMapTypes(
type->v, {Value::Storage, Value::From, Value::To, Value::Tofrom});
} else {
CheckAllowedMapTypes(
type->v, {Value::Alloc, Value::From, Value::To, Value::Tofrom});
}
} else if (llvm::is_contained(leafs, Directive::OMPD_target_enter_data)) {
CheckAllowedMapTypes(type->v, mapEnteringTypes);
} else if (llvm::is_contained(leafs, Directive::OMPD_target_exit_data)) {
CheckAllowedMapTypes(type->v, mapExitingTypes);
}
}

Expand Down
4 changes: 2 additions & 2 deletions flang/lib/Semantics/check-omp-structure.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ class OmpStructureChecker
void HasInvalidDistributeNesting(const parser::OpenMPLoopConstruct &x);
void HasInvalidLoopBinding(const parser::OpenMPLoopConstruct &x);
// specific clause related
void CheckAllowedMapTypes(const parser::OmpMapType::Value &,
const std::list<parser::OmpMapType::Value> &);
void CheckAllowedMapTypes(
parser::OmpMapType::Value, llvm::ArrayRef<parser::OmpMapType::Value>);

const std::list<parser::OmpTraitProperty> &GetTraitPropertyList(
const parser::OmpTraitSelector &);
Expand Down
25 changes: 25 additions & 0 deletions flang/lib/Semantics/openmp-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,31 @@ bool IsVarOrFunctionRef(const MaybeExpr &expr) {
}
}

bool IsMapEnteringType(parser::OmpMapType::Value type) {
switch (type) {
case parser::OmpMapType::Value::Alloc:
case parser::OmpMapType::Value::Storage:
case parser::OmpMapType::Value::To:
case parser::OmpMapType::Value::Tofrom:
return true;
default:
return false;
}
}

bool IsMapExitingType(parser::OmpMapType::Value type) {
switch (type) {
case parser::OmpMapType::Value::Delete:
case parser::OmpMapType::Value::From:
case parser::OmpMapType::Value::Release:
case parser::OmpMapType::Value::Storage:
case parser::OmpMapType::Value::Tofrom:
return true;
default:
return false;
}
}

std::optional<SomeExpr> GetEvaluateExpr(const parser::Expr &parserExpr) {
const parser::TypedExpr &typedExpr{parserExpr.typedExpr};
// ForwardOwningPointer typedExpr
Expand Down
3 changes: 3 additions & 0 deletions flang/lib/Semantics/openmp-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ bool IsExtendedListItem(const Symbol &sym);
bool IsVariableListItem(const Symbol &sym);
bool IsVarOrFunctionRef(const MaybeExpr &expr);

bool IsMapEnteringType(parser::OmpMapType::Value type);
bool IsMapExitingType(parser::OmpMapType::Value type);

std::optional<SomeExpr> GetEvaluateExpr(const parser::Expr &parserExpr);
std::optional<evaluate::DynamicType> GetDynamicType(
const parser::Expr &parserExpr);
Expand Down
Loading
Loading