Skip to content

Commit f2f4193

Browse files
authored
[OpenMP][MLIR] Set omp.composite attr for composite loop wrappers and add verifier checks (#102341)
This patch sets the omp.composite unit attr for composite wrapper ops and also add appropriate checks to the verifiers of supported ops for the presence/absence of the attribute. This is patch 2/2 in a series of patches. Patch 1 - #102340.
1 parent d372361 commit f2f4193

File tree

4 files changed

+253
-35
lines changed

4 files changed

+253
-35
lines changed

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,10 +2069,12 @@ static void genCompositeDistributeSimd(
20692069
// TODO: Populate entry block arguments with private variables.
20702070
auto distributeOp = genWrapperOp<mlir::omp::DistributeOp>(
20712071
converter, loc, distributeClauseOps, /*blockArgTypes=*/{});
2072+
distributeOp.setComposite(/*val=*/true);
20722073

20732074
// TODO: Populate entry block arguments with reduction and private variables.
20742075
auto simdOp = genWrapperOp<mlir::omp::SimdOp>(converter, loc, simdClauseOps,
20752076
/*blockArgTypes=*/{});
2077+
simdOp.setComposite(/*val=*/true);
20762078

20772079
// Construct wrapper entry block list and associated symbols. It is important
20782080
// that the symbol order and the block argument order match, so that the
@@ -2117,10 +2119,12 @@ static void genCompositeDoSimd(lower::AbstractConverter &converter,
21172119
// TODO: Add private variables to entry block arguments.
21182120
auto wsloopOp = genWrapperOp<mlir::omp::WsloopOp>(
21192121
converter, loc, wsloopClauseOps, wsloopReductionTypes);
2122+
wsloopOp.setComposite(/*val=*/true);
21202123

21212124
// TODO: Populate entry block arguments with reduction and private variables.
21222125
auto simdOp = genWrapperOp<mlir::omp::SimdOp>(converter, loc, simdClauseOps,
21232126
/*blockArgTypes=*/{});
2127+
simdOp.setComposite(/*val=*/true);
21242128

21252129
// Construct wrapper entry block list and associated symbols. It is important
21262130
// that the symbol and block argument order match, so that the symbol-value

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,9 @@ LogicalResult ParallelOp::verify() {
15461546
if (!isWrapper())
15471547
return emitOpError() << "must take a loop wrapper role if nested inside "
15481548
"of 'omp.distribute'";
1549+
if (!isComposite())
1550+
return emitError()
1551+
<< "'omp.composite' attribute missing from composite wrapper";
15491552

15501553
if (LoopWrapperInterface nested = getNestedWrapper()) {
15511554
// Check for the allowed leaf constructs that may appear in a composite
@@ -1555,6 +1558,9 @@ LogicalResult ParallelOp::verify() {
15551558
} else {
15561559
return emitOpError() << "must not wrap an 'omp.loop_nest' directly";
15571560
}
1561+
} else if (isComposite()) {
1562+
return emitError()
1563+
<< "'omp.composite' attribute present in non-composite wrapper";
15581564
}
15591565

15601566
if (getAllocateVars().size() != getAllocatorVars().size())
@@ -1748,11 +1754,28 @@ LogicalResult WsloopOp::verify() {
17481754
if (!isWrapper())
17491755
return emitOpError() << "must be a loop wrapper";
17501756

1757+
auto wrapper =
1758+
llvm::dyn_cast_if_present<LoopWrapperInterface>((*this)->getParentOp());
1759+
bool isCompositeChildLeaf =
1760+
wrapper && wrapper.isWrapper() &&
1761+
(!llvm::isa<ParallelOp>(wrapper) ||
1762+
llvm::isa_and_present<DistributeOp>(wrapper->getParentOp()));
17511763
if (LoopWrapperInterface nested = getNestedWrapper()) {
1764+
if (!isComposite())
1765+
return emitError()
1766+
<< "'omp.composite' attribute missing from composite wrapper";
1767+
17521768
// Check for the allowed leaf constructs that may appear in a composite
17531769
// construct directly after DO/FOR.
17541770
if (!isa<SimdOp>(nested))
17551771
return emitError() << "only supported nested wrapper is 'omp.simd'";
1772+
1773+
} else if (isComposite() && !isCompositeChildLeaf) {
1774+
return emitError()
1775+
<< "'omp.composite' attribute present in non-composite wrapper";
1776+
} else if (!isComposite() && isCompositeChildLeaf) {
1777+
return emitError()
1778+
<< "'omp.composite' attribute missing from composite wrapper";
17561779
}
17571780

17581781
return verifyReductionVarList(*this, getReductionSyms(), getReductionVars(),
@@ -1796,6 +1819,21 @@ LogicalResult SimdOp::verify() {
17961819
if (getNestedWrapper())
17971820
return emitOpError() << "must wrap an 'omp.loop_nest' directly";
17981821

1822+
auto wrapper =
1823+
llvm::dyn_cast_if_present<LoopWrapperInterface>((*this)->getParentOp());
1824+
bool isCompositeChildLeaf =
1825+
wrapper && wrapper.isWrapper() &&
1826+
(!llvm::isa<ParallelOp>(wrapper) ||
1827+
llvm::isa_and_present<DistributeOp>(wrapper->getParentOp()));
1828+
1829+
if (!isComposite() && isCompositeChildLeaf)
1830+
return emitError()
1831+
<< "'omp.composite' attribute missing from composite wrapper";
1832+
1833+
if (isComposite() && !isCompositeChildLeaf)
1834+
return emitError()
1835+
<< "'omp.composite' attribute present in non-composite wrapper";
1836+
17991837
return success();
18001838
}
18011839

@@ -1825,11 +1863,17 @@ LogicalResult DistributeOp::verify() {
18251863
return emitOpError() << "must be a loop wrapper";
18261864

18271865
if (LoopWrapperInterface nested = getNestedWrapper()) {
1866+
if (!isComposite())
1867+
return emitError()
1868+
<< "'omp.composite' attribute missing from composite wrapper";
18281869
// Check for the allowed leaf constructs that may appear in a composite
18291870
// construct directly after DISTRIBUTE.
18301871
if (!isa<ParallelOp, SimdOp>(nested))
18311872
return emitError() << "only supported nested wrappers are 'omp.parallel' "
18321873
"and 'omp.simd'";
1874+
} else if (isComposite()) {
1875+
return emitError()
1876+
<< "'omp.composite' attribute present in non-composite wrapper";
18331877
}
18341878

18351879
return success();
@@ -2031,11 +2075,19 @@ LogicalResult TaskloopOp::verify() {
20312075
return emitOpError() << "must be a loop wrapper";
20322076

20332077
if (LoopWrapperInterface nested = getNestedWrapper()) {
2078+
if (!isComposite())
2079+
return emitError()
2080+
<< "'omp.composite' attribute missing from composite wrapper";
2081+
20342082
// Check for the allowed leaf constructs that may appear in a composite
20352083
// construct directly after TASKLOOP.
20362084
if (!isa<SimdOp>(nested))
20372085
return emitError() << "only supported nested wrapper is 'omp.simd'";
2086+
} else if (isComposite()) {
2087+
return emitError()
2088+
<< "'omp.composite' attribute present in non-composite wrapper";
20382089
}
2090+
20392091
return success();
20402092
}
20412093

mlir/test/Dialect/OpenMP/invalid.mlir

Lines changed: 173 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ func.func @invalid_nested_wrapper(%lb : index, %ub : index, %step : index) {
3434
omp.yield
3535
}
3636
omp.terminator
37-
}
37+
} {omp.composite}
3838
omp.terminator
39-
}
39+
} {omp.composite}
4040
omp.terminator
41-
}
41+
} {omp.composite}
4242

4343
return
4444
}
@@ -53,9 +53,9 @@ func.func @no_nested_wrapper(%lb : index, %ub : index, %step : index) {
5353
omp.yield
5454
}
5555
omp.terminator
56-
}
56+
} {omp.composite}
5757
omp.terminator
58-
}
58+
} {omp.composite}
5959

6060
return
6161
}
@@ -208,9 +208,9 @@ func.func @invalid_nested_wrapper(%lb : index, %ub : index, %step : index) {
208208
omp.yield
209209
}
210210
omp.terminator
211-
}
211+
} {omp.composite}
212212
omp.terminator
213-
}
213+
} {omp.composite}
214214
}
215215

216216
// -----
@@ -1963,9 +1963,9 @@ func.func @taskloop(%lb: i32, %ub: i32, %step: i32) {
19631963
omp.yield
19641964
}
19651965
omp.terminator
1966-
}
1966+
} {omp.composite}
19671967
omp.terminator
1968-
}
1968+
} {omp.composite}
19691969
return
19701970
}
19711971

@@ -2171,9 +2171,9 @@ func.func @omp_distribute_nested_wrapper(%lb: index, %ub: index, %step: index) -
21712171
"omp.yield"() : () -> ()
21722172
}
21732173
"omp.terminator"() : () -> ()
2174-
}) : () -> ()
2174+
}) {omp.composite} : () -> ()
21752175
"omp.terminator"() : () -> ()
2176-
}
2176+
} {omp.composite}
21772177
}
21782178

21792179
// -----
@@ -2383,3 +2383,165 @@ func.func @masked_arg_count_mismatch(%arg0: i32, %arg1: i32) {
23832383
}) : (i32, i32) -> ()
23842384
return
23852385
}
2386+
2387+
// -----
2388+
func.func @omp_parallel_missing_composite(%lb: index, %ub: index, %step: index) -> () {
2389+
omp.distribute {
2390+
// expected-error@+1 {{'omp.composite' attribute missing from composite wrapper}}
2391+
omp.parallel {
2392+
omp.wsloop {
2393+
omp.loop_nest (%iv) : index = (%lb) to (%ub) step (%step) {
2394+
omp.yield
2395+
}
2396+
omp.terminator
2397+
} {omp.composite}
2398+
omp.terminator
2399+
}
2400+
omp.terminator
2401+
} {omp.composite}
2402+
return
2403+
}
2404+
2405+
// -----
2406+
func.func @omp_parallel_invalid_composite(%lb: index, %ub: index, %step: index) -> () {
2407+
// expected-error @below {{'omp.composite' attribute present in non-composite wrapper}}
2408+
omp.parallel {
2409+
omp.wsloop {
2410+
omp.loop_nest (%iv) : index = (%lb) to (%ub) step (%step) {
2411+
omp.yield
2412+
}
2413+
omp.terminator
2414+
}
2415+
omp.terminator
2416+
} {omp.composite}
2417+
return
2418+
}
2419+
2420+
// -----
2421+
func.func @omp_wsloop_missing_composite(%lb: index, %ub: index, %step: index) -> () {
2422+
// expected-error @below {{'omp.composite' attribute missing from composite wrapper}}
2423+
omp.wsloop {
2424+
omp.simd {
2425+
omp.loop_nest (%iv) : index = (%lb) to (%ub) step (%step) {
2426+
omp.yield
2427+
}
2428+
omp.terminator
2429+
} {omp.composite}
2430+
omp.terminator
2431+
}
2432+
return
2433+
}
2434+
2435+
// -----
2436+
func.func @omp_wsloop_invalid_composite(%lb: index, %ub: index, %step: index) -> () {
2437+
// expected-error @below {{'omp.composite' attribute present in non-composite wrapper}}
2438+
omp.wsloop {
2439+
omp.loop_nest (%iv) : index = (%lb) to (%ub) step (%step) {
2440+
omp.yield
2441+
}
2442+
omp.terminator
2443+
} {omp.composite}
2444+
return
2445+
}
2446+
2447+
// -----
2448+
func.func @omp_wsloop_missing_composite_2(%lb: index, %ub: index, %step: index) -> () {
2449+
omp.distribute {
2450+
omp.parallel {
2451+
// expected-error @below {{'omp.composite' attribute missing from composite wrapper}}
2452+
omp.wsloop {
2453+
omp.loop_nest (%iv) : index = (%lb) to (%ub) step (%step) {
2454+
omp.yield
2455+
}
2456+
omp.terminator
2457+
}
2458+
omp.terminator
2459+
} {omp.composite}
2460+
omp.terminator
2461+
} {omp.composite}
2462+
return
2463+
}
2464+
2465+
// -----
2466+
func.func @omp_simd_missing_composite(%lb: index, %ub: index, %step: index) -> () {
2467+
omp.wsloop {
2468+
// expected-error @below {{'omp.composite' attribute missing from composite wrapper}}
2469+
omp.simd {
2470+
omp.loop_nest (%iv) : index = (%lb) to (%ub) step (%step) {
2471+
omp.yield
2472+
}
2473+
omp.terminator
2474+
}
2475+
omp.terminator
2476+
} {omp.composite}
2477+
return
2478+
}
2479+
2480+
// -----
2481+
func.func @omp_simd_invalid_composite(%lb: index, %ub: index, %step: index) -> () {
2482+
// expected-error @below {{'omp.composite' attribute present in non-composite wrapper}}
2483+
omp.simd {
2484+
omp.loop_nest (%iv) : index = (%lb) to (%ub) step (%step) {
2485+
omp.yield
2486+
}
2487+
omp.terminator
2488+
} {omp.composite}
2489+
return
2490+
}
2491+
2492+
// -----
2493+
func.func @omp_distribute_missing_composite(%lb: index, %ub: index, %step: index) -> () {
2494+
// expected-error @below {{'omp.composite' attribute missing from composite wrapper}}
2495+
omp.distribute {
2496+
omp.parallel {
2497+
omp.wsloop {
2498+
omp.loop_nest (%iv) : index = (%lb) to (%ub) step (%step) {
2499+
omp.yield
2500+
}
2501+
omp.terminator
2502+
} {omp.composite}
2503+
omp.terminator
2504+
} {omp.composite}
2505+
omp.terminator
2506+
}
2507+
return
2508+
}
2509+
2510+
// -----
2511+
func.func @omp_distribute_invalid_composite(%lb: index, %ub: index, %step: index) -> () {
2512+
// expected-error @below {{'omp.composite' attribute present in non-composite wrapper}}
2513+
omp.distribute {
2514+
omp.loop_nest (%0) : index = (%lb) to (%ub) step (%step) {
2515+
omp.yield
2516+
}
2517+
omp.terminator
2518+
} {omp.composite}
2519+
return
2520+
}
2521+
2522+
// -----
2523+
func.func @omp_taskloop_missing_composite(%lb: index, %ub: index, %step: index) -> () {
2524+
// expected-error @below {{'omp.composite' attribute missing from composite wrapper}}
2525+
omp.taskloop {
2526+
omp.simd {
2527+
omp.loop_nest (%i) : index = (%lb) to (%ub) step (%step) {
2528+
omp.yield
2529+
}
2530+
omp.terminator
2531+
} {omp.composite}
2532+
omp.terminator
2533+
}
2534+
return
2535+
}
2536+
2537+
// -----
2538+
func.func @omp_taskloop_invalid_composite(%lb: index, %ub: index, %step: index) -> () {
2539+
// expected-error @below {{'omp.composite' attribute present in non-composite wrapper}}
2540+
omp.taskloop {
2541+
omp.loop_nest (%i) : index = (%lb) to (%ub) step (%step) {
2542+
omp.yield
2543+
}
2544+
omp.terminator
2545+
} {omp.composite}
2546+
return
2547+
}

0 commit comments

Comments
 (0)