Skip to content

Commit 7c9404c

Browse files
authored
[flang][OpenMP] Add frontend support for ompx_bare clause (#111106)
1 parent 5fd385b commit 7c9404c

File tree

14 files changed

+122
-12
lines changed

14 files changed

+122
-12
lines changed

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3474,6 +3474,16 @@ OMPClause *Parser::ParseOpenMPClause(OpenMPDirectiveKind DKind,
34743474
Clause = ParseOpenMPOMPXAttributesClause(WrongDirective);
34753475
break;
34763476
case OMPC_ompx_bare:
3477+
if (DKind == llvm::omp::Directive::OMPD_target) {
3478+
// Flang splits the combined directives which requires OMPD_target to be
3479+
// marked as accepting the `ompx_bare` clause in `OMP.td`. Thus, we need
3480+
// to explicitly check whether this clause is applied to an `omp target`
3481+
// without `teams` and emit an error.
3482+
Diag(Tok, diag::err_omp_unexpected_clause)
3483+
<< getOpenMPClauseName(CKind) << getOpenMPDirectiveName(DKind);
3484+
ErrorFound = true;
3485+
WrongDirective = true;
3486+
}
34773487
if (WrongDirective)
34783488
Diag(Tok, diag::note_ompx_bare_clause)
34793489
<< getOpenMPClauseName(CKind) << "target teams";

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ static void convertLoopBounds(lower::AbstractConverter &converter,
220220
// ClauseProcessor unique clauses
221221
//===----------------------------------------------------------------------===//
222222

223+
bool ClauseProcessor::processBare(mlir::omp::BareClauseOps &result) const {
224+
return markClauseOccurrence<omp::clause::OmpxBare>(result.bare);
225+
}
226+
223227
bool ClauseProcessor::processBind(mlir::omp::BindClauseOps &result) const {
224228
if (auto *clause = findUniqueClause<omp::clause::Bind>()) {
225229
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();

flang/lib/Lower/OpenMP/ClauseProcessor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class ClauseProcessor {
5353
: converter(converter), semaCtx(semaCtx), clauses(clauses) {}
5454

5555
// 'Unique' clauses: They can appear at most once in the clause list.
56+
bool processBare(mlir::omp::BareClauseOps &result) const;
5657
bool processBind(mlir::omp::BindClauseOps &result) const;
5758
bool
5859
processCollapse(mlir::Location currentLocation, lower::pft::Evaluation &eval,

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,7 @@ static void genTargetClauses(
11841184
llvm::SmallVectorImpl<const semantics::Symbol *> &isDevicePtrSyms,
11851185
llvm::SmallVectorImpl<const semantics::Symbol *> &mapSyms) {
11861186
ClauseProcessor cp(converter, semaCtx, clauses);
1187+
cp.processBare(clauseOps);
11871188
cp.processDepend(clauseOps);
11881189
cp.processDevice(stmtCtx, clauseOps);
11891190
cp.processHasDeviceAddr(clauseOps, hasDeviceAddrSyms);
@@ -2860,6 +2861,7 @@ static void genOMP(lower::AbstractConverter &converter, lower::SymMap &symTable,
28602861
!std::holds_alternative<clause::Nowait>(clause.u) &&
28612862
!std::holds_alternative<clause::NumTeams>(clause.u) &&
28622863
!std::holds_alternative<clause::NumThreads>(clause.u) &&
2864+
!std::holds_alternative<clause::OmpxBare>(clause.u) &&
28632865
!std::holds_alternative<clause::Priority>(clause.u) &&
28642866
!std::holds_alternative<clause::Private>(clause.u) &&
28652867
!std::holds_alternative<clause::ProcBind>(clause.u) &&

flang/lib/Parser/openmp-parsers.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ TYPE_PARSER(
657657
parenthesized(scalarIntExpr))) ||
658658
"NUM_THREADS" >> construct<OmpClause>(construct<OmpClause::NumThreads>(
659659
parenthesized(scalarIntExpr))) ||
660+
"OMPX_BARE" >> construct<OmpClause>(construct<OmpClause::OmpxBare>()) ||
660661
"ORDER" >> construct<OmpClause>(construct<OmpClause::Order>(
661662
parenthesized(Parser<OmpOrderClause>{}))) ||
662663
"ORDERED" >> construct<OmpClause>(construct<OmpClause::Ordered>(

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2867,7 +2867,6 @@ CHECK_SIMPLE_CLAUSE(Align, OMPC_align)
28672867
CHECK_SIMPLE_CLAUSE(Compare, OMPC_compare)
28682868
CHECK_SIMPLE_CLAUSE(CancellationConstructType, OMPC_cancellation_construct_type)
28692869
CHECK_SIMPLE_CLAUSE(OmpxAttribute, OMPC_ompx_attribute)
2870-
CHECK_SIMPLE_CLAUSE(OmpxBare, OMPC_ompx_bare)
28712870
CHECK_SIMPLE_CLAUSE(Weak, OMPC_weak)
28722871

28732872
CHECK_REQ_SCALAR_INT_CLAUSE(NumTeams, OMPC_num_teams)
@@ -4395,6 +4394,17 @@ void OmpStructureChecker::Enter(const parser::OmpClause::To &x) {
43954394
}
43964395
}
43974396

4397+
void OmpStructureChecker::Enter(const parser::OmpClause::OmpxBare &x) {
4398+
// Don't call CheckAllowedClause, because it allows "ompx_bare" on
4399+
// a non-combined "target" directive (for reasons of splitting combined
4400+
// directives). In source code it's only allowed on "target teams".
4401+
if (GetContext().directive != llvm::omp::Directive::OMPD_target_teams) {
4402+
context_.Say(GetContext().clauseSource,
4403+
"%s clause is only allowed on combined TARGET TEAMS"_err_en_US,
4404+
parser::ToUpperCaseLetters(getClauseName(llvm::omp::OMPC_ompx_bare)));
4405+
}
4406+
}
4407+
43984408
llvm::StringRef OmpStructureChecker::getClauseName(llvm::omp::Clause clause) {
43994409
return llvm::omp::getOpenMPClauseName(clause);
44004410
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fopenmp-version=51 %s -o - | FileCheck %s
2+
3+
program test
4+
integer :: tmp
5+
!$omp target teams ompx_bare num_teams(42) thread_limit(43)
6+
tmp = 1
7+
!$omp end target teams
8+
end program
9+
10+
! CHECK: omp.target ompx_bare
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
!RUN: %python %S/../test_errors.py %s %flang -fopenmp -fopenmp-version=51
2+
3+
subroutine test1
4+
!ERROR: OMPX_BARE clause is only allowed on combined TARGET TEAMS
5+
!$omp target ompx_bare
6+
!$omp end target
7+
end
8+
9+
subroutine test2
10+
!$omp target
11+
!ERROR: OMPX_BARE clause is only allowed on combined TARGET TEAMS
12+
!$omp teams ompx_bare
13+
!$omp end teams
14+
!$omp end target
15+
end
16+
17+
subroutine test3
18+
integer i
19+
!ERROR: OMPX_BARE clause is only allowed on combined TARGET TEAMS
20+
!$omp target teams distribute ompx_bare
21+
do i = 0, 10
22+
end do
23+
!$omp end target teams distribute
24+
end
25+
26+
subroutine test4
27+
!No errors
28+
!$omp target teams ompx_bare
29+
!$omp end target teams
30+
end

llvm/include/llvm/Frontend/OpenMP/ConstructDecompositionT.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ struct ConstructDecompositionT {
239239
bool
240240
applyClause(const tomp::clause::OmpxAttributeT<TypeTy, IdTy, ExprTy> &clause,
241241
const ClauseTy *);
242+
bool applyClause(const tomp::clause::OmpxBareT<TypeTy, IdTy, ExprTy> &clause,
243+
const ClauseTy *);
242244

243245
uint32_t version;
244246
llvm::omp::Directive construct;
@@ -1103,6 +1105,13 @@ bool ConstructDecompositionT<C, H>::applyClause(
11031105
return applyToOutermost(node);
11041106
}
11051107

1108+
template <typename C, typename H>
1109+
bool ConstructDecompositionT<C, H>::applyClause(
1110+
const tomp::clause::OmpxBareT<TypeTy, IdTy, ExprTy> &clause,
1111+
const ClauseTy *node) {
1112+
return applyToOutermost(node);
1113+
}
1114+
11061115
template <typename C, typename H>
11071116
bool ConstructDecompositionT<C, H>::applyClause(
11081117
const tomp::clause::OmpxAttributeT<TypeTy, IdTy, ExprTy> &clause,

llvm/include/llvm/Frontend/OpenMP/OMP.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,7 @@ def OMP_Target : Directive<"target"> {
10181018
VersionedClause<OMPC_Device>,
10191019
VersionedClause<OMPC_If>,
10201020
VersionedClause<OMPC_NoWait>,
1021+
VersionedClause<OMPC_OMPX_Bare>,
10211022
VersionedClause<OMPC_OMPX_DynCGroupMem>,
10221023
VersionedClause<OMPC_ThreadLimit, 51>,
10231024
];

mlir/include/mlir/Dialect/OpenMP/OpenMPClauses.td

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,32 @@ class OpenMP_AllocateClauseSkip<
8484

8585
def OpenMP_AllocateClause : OpenMP_AllocateClauseSkip<>;
8686

87+
//===----------------------------------------------------------------------===//
88+
// LLVM OpenMP extension `ompx_bare` clause
89+
//===----------------------------------------------------------------------===//
90+
91+
class OpenMP_BareClauseSkip<
92+
bit traits = false, bit arguments = false, bit assemblyFormat = false,
93+
bit description = false, bit extraClassDeclaration = false
94+
> : OpenMP_Clause<traits, arguments, assemblyFormat, description,
95+
extraClassDeclaration> {
96+
let arguments = (ins
97+
UnitAttr:$bare
98+
);
99+
100+
let optAssemblyFormat = [{
101+
`ompx_bare` $bare
102+
}];
103+
104+
let description = [{
105+
`ompx_bare` allows `omp target teams` to be executed on a GPU with an
106+
explicit number of teams and threads. This clause also allows the teams and
107+
threads sizes to have up to 3 dimensions.
108+
}];
109+
}
110+
111+
def OpenMP_BareClause : OpenMP_BareClauseSkip<>;
112+
87113
//===----------------------------------------------------------------------===//
88114
// V5.2: [16.1, 16.2] `cancel-directive-name` clause set
89115
//===----------------------------------------------------------------------===//

mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,10 +1223,11 @@ def TargetOp : OpenMP_Op<"target", traits = [
12231223
OutlineableOpenMPOpInterface
12241224
], clauses = [
12251225
// TODO: Complete clause list (defaultmap, uses_allocators).
1226-
OpenMP_AllocateClause, OpenMP_DependClause, OpenMP_DeviceClause,
1227-
OpenMP_HasDeviceAddrClause, OpenMP_IfClause, OpenMP_InReductionClause,
1228-
OpenMP_IsDevicePtrClause, OpenMP_MapClauseSkip<assemblyFormat = true>,
1229-
OpenMP_NowaitClause, OpenMP_PrivateClause, OpenMP_ThreadLimitClause
1226+
OpenMP_AllocateClause, OpenMP_BareClause, OpenMP_DependClause,
1227+
OpenMP_DeviceClause, OpenMP_HasDeviceAddrClause, OpenMP_IfClause,
1228+
OpenMP_InReductionClause, OpenMP_IsDevicePtrClause,
1229+
OpenMP_MapClauseSkip<assemblyFormat = true>, OpenMP_NowaitClause,
1230+
OpenMP_PrivateClause, OpenMP_ThreadLimitClause,
12301231
], singleRegion = true> {
12311232
let summary = "target construct";
12321233
let description = [{

mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,13 +1709,13 @@ void TargetOp::build(OpBuilder &builder, OperationState &state,
17091709
// TODO Store clauses in op: allocateVars, allocatorVars, inReductionVars,
17101710
// inReductionByref, inReductionSyms.
17111711
TargetOp::build(builder, state, /*allocate_vars=*/{}, /*allocator_vars=*/{},
1712-
makeArrayAttr(ctx, clauses.dependKinds), clauses.dependVars,
1713-
clauses.device, clauses.hasDeviceAddrVars, clauses.ifExpr,
1714-
/*in_reduction_vars=*/{}, /*in_reduction_byref=*/nullptr,
1715-
/*in_reduction_syms=*/nullptr, clauses.isDevicePtrVars,
1716-
clauses.mapVars, clauses.nowait, clauses.privateVars,
1717-
makeArrayAttr(ctx, clauses.privateSyms), clauses.threadLimit,
1718-
/*private_maps=*/nullptr);
1712+
clauses.bare, makeArrayAttr(ctx, clauses.dependKinds),
1713+
clauses.dependVars, clauses.device, clauses.hasDeviceAddrVars,
1714+
clauses.ifExpr, /*in_reduction_vars=*/{},
1715+
/*in_reduction_byref=*/nullptr, /*in_reduction_syms=*/nullptr,
1716+
clauses.isDevicePtrVars, clauses.mapVars, clauses.nowait,
1717+
clauses.privateVars, makeArrayAttr(ctx, clauses.privateSyms),
1718+
clauses.threadLimit, /*private_maps=*/nullptr);
17191719
}
17201720

17211721
LogicalResult TargetOp::verify() {

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ static LogicalResult checkImplementationStatus(Operation &op) {
158158
if (!op.getAllocateVars().empty() || !op.getAllocatorVars().empty())
159159
result = todo("allocate");
160160
};
161+
auto checkBare = [&todo](auto op, LogicalResult &result) {
162+
if (op.getBare())
163+
result = todo("ompx_bare");
164+
};
161165
auto checkDepend = [&todo](auto op, LogicalResult &result) {
162166
if (!op.getDependVars().empty() || op.getDependKinds())
163167
result = todo("depend");
@@ -283,6 +287,7 @@ static LogicalResult checkImplementationStatus(Operation &op) {
283287
[&](auto op) { checkDepend(op, result); })
284288
.Case([&](omp::TargetOp op) {
285289
checkAllocate(op, result);
290+
checkBare(op, result);
286291
checkDevice(op, result);
287292
checkHasDeviceAddr(op, result);
288293
checkIf(op, result);

0 commit comments

Comments
 (0)