@@ -1908,8 +1908,8 @@ LogicalResult TargetOp::verifyRegions() {
1908
1908
return emitError (" target containing multiple 'omp.teams' nested ops" );
1909
1909
1910
1910
// Check that host_eval values are only used in legal ways.
1911
- llvm::omp::OMPTgtExecModeFlags execFlags =
1912
- getKernelExecFlags (getInnermostCapturedOmpOp () );
1911
+ Operation *capturedOp = getInnermostCapturedOmpOp ();
1912
+ TargetRegionFlags execFlags = getKernelExecFlags (capturedOp );
1913
1913
for (Value hostEvalArg :
1914
1914
cast<BlockArgOpenMPOpInterface>(getOperation ()).getHostEvalBlockArgs ()) {
1915
1915
for (Operation *user : hostEvalArg.getUsers ()) {
@@ -1924,7 +1924,8 @@ LogicalResult TargetOp::verifyRegions() {
1924
1924
" and 'thread_limit' in 'omp.teams'" ;
1925
1925
}
1926
1926
if (auto parallelOp = dyn_cast<ParallelOp>(user)) {
1927
- if (execFlags == llvm::omp::OMP_TGT_EXEC_MODE_SPMD &&
1927
+ if (bitEnumContainsAny (execFlags, TargetRegionFlags::spmd) &&
1928
+ parallelOp->isAncestor (capturedOp) &&
1928
1929
hostEvalArg == parallelOp.getNumThreads ())
1929
1930
continue ;
1930
1931
@@ -1933,15 +1934,16 @@ LogicalResult TargetOp::verifyRegions() {
1933
1934
" 'omp.parallel' when representing target SPMD" ;
1934
1935
}
1935
1936
if (auto loopNestOp = dyn_cast<LoopNestOp>(user)) {
1936
- if (execFlags != llvm::omp::OMP_TGT_EXEC_MODE_GENERIC &&
1937
+ if (bitEnumContainsAny (execFlags, TargetRegionFlags::trip_count) &&
1938
+ loopNestOp.getOperation () == capturedOp &&
1937
1939
(llvm::is_contained (loopNestOp.getLoopLowerBounds (), hostEvalArg) ||
1938
1940
llvm::is_contained (loopNestOp.getLoopUpperBounds (), hostEvalArg) ||
1939
1941
llvm::is_contained (loopNestOp.getLoopSteps (), hostEvalArg)))
1940
1942
continue ;
1941
1943
1942
1944
return emitOpError () << " host_eval argument only legal as loop bounds "
1943
- " and steps in 'omp.loop_nest' when "
1944
- " representing target SPMD or Generic-SPMD " ;
1945
+ " and steps in 'omp.loop_nest' when trip count "
1946
+ " must be evaluated in the host " ;
1945
1947
}
1946
1948
1947
1949
return emitOpError () << " host_eval argument illegal use in '"
@@ -1951,42 +1953,21 @@ LogicalResult TargetOp::verifyRegions() {
1951
1953
return success ();
1952
1954
}
1953
1955
1954
- // / Only allow OpenMP terminators and non-OpenMP ops that have known memory
1955
- // / effects, but don't include a memory write effect.
1956
- static bool siblingAllowedInCapture (Operation *op) {
1957
- if (!op)
1958
- return false ;
1956
+ static Operation *
1957
+ findCapturedOmpOp (Operation *rootOp,
1958
+ llvm::function_ref<bool (Operation *)> siblingAllowedFn) {
1959
+ assert (rootOp && " expected valid operation" );
1959
1960
1960
- bool isOmpDialect =
1961
- op->getContext ()->getLoadedDialect <omp::OpenMPDialect>() ==
1962
- op->getDialect ();
1963
-
1964
- if (isOmpDialect)
1965
- return op->hasTrait <OpTrait::IsTerminator>();
1966
-
1967
- if (auto memOp = dyn_cast<MemoryEffectOpInterface>(op)) {
1968
- SmallVector<SideEffects::EffectInstance<MemoryEffects::Effect>, 4 > effects;
1969
- memOp.getEffects (effects);
1970
- return !llvm::any_of (effects, [&](MemoryEffects::EffectInstance &effect) {
1971
- return isa<MemoryEffects::Write>(effect.getEffect ()) &&
1972
- isa<SideEffects::AutomaticAllocationScopeResource>(
1973
- effect.getResource ());
1974
- });
1975
- }
1976
- return true ;
1977
- }
1978
-
1979
- Operation *TargetOp::getInnermostCapturedOmpOp () {
1980
- Dialect *ompDialect = (*this )->getDialect ();
1961
+ Dialect *ompDialect = rootOp->getDialect ();
1981
1962
Operation *capturedOp = nullptr ;
1982
1963
DominanceInfo domInfo;
1983
1964
1984
1965
// Process in pre-order to check operations from outermost to innermost,
1985
1966
// ensuring we only enter the region of an operation if it meets the criteria
1986
1967
// for being captured. We stop the exploration of nested operations as soon as
1987
1968
// we process a region holding no operations to be captured.
1988
- walk<WalkOrder::PreOrder>([&](Operation *op) {
1989
- if (op == * this )
1969
+ rootOp-> walk <WalkOrder::PreOrder>([&](Operation *op) {
1970
+ if (op == rootOp )
1990
1971
return WalkResult::advance ();
1991
1972
1992
1973
// Ignore operations of other dialects or omp operations with no regions,
@@ -2016,7 +1997,7 @@ Operation *TargetOp::getInnermostCapturedOmpOp() {
2016
1997
// Don't capture this op if it has a not-allowed sibling, and stop recursing
2017
1998
// into nested operations.
2018
1999
for (Operation &sibling : op->getParentRegion ()->getOps ())
2019
- if (&sibling != op && !siblingAllowedInCapture (&sibling))
2000
+ if (&sibling != op && !siblingAllowedFn (&sibling))
2020
2001
return WalkResult::interrupt ();
2021
2002
2022
2003
// Don't continue capturing nested operations if we reach an omp.loop_nest.
@@ -2029,10 +2010,33 @@ Operation *TargetOp::getInnermostCapturedOmpOp() {
2029
2010
return capturedOp;
2030
2011
}
2031
2012
2032
- llvm::omp::OMPTgtExecModeFlags
2033
- TargetOp::getKernelExecFlags (Operation *capturedOp) {
2034
- using namespace llvm ::omp;
2013
+ Operation *TargetOp::getInnermostCapturedOmpOp () {
2014
+ auto *ompDialect = getContext ()->getLoadedDialect <omp::OpenMPDialect>();
2035
2015
2016
+ // Only allow OpenMP terminators and non-OpenMP ops that have known memory
2017
+ // effects, but don't include a memory write effect.
2018
+ return findCapturedOmpOp (*this , [&](Operation *sibling) {
2019
+ if (!sibling)
2020
+ return false ;
2021
+
2022
+ if (ompDialect == sibling->getDialect ())
2023
+ return sibling->hasTrait <OpTrait::IsTerminator>();
2024
+
2025
+ if (auto memOp = dyn_cast<MemoryEffectOpInterface>(sibling)) {
2026
+ SmallVector<SideEffects::EffectInstance<MemoryEffects::Effect>, 4 >
2027
+ effects;
2028
+ memOp.getEffects (effects);
2029
+ return !llvm::any_of (effects, [&](MemoryEffects::EffectInstance &effect) {
2030
+ return isa<MemoryEffects::Write>(effect.getEffect ()) &&
2031
+ isa<SideEffects::AutomaticAllocationScopeResource>(
2032
+ effect.getResource ());
2033
+ });
2034
+ }
2035
+ return true ;
2036
+ });
2037
+ }
2038
+
2039
+ TargetRegionFlags TargetOp::getKernelExecFlags (Operation *capturedOp) {
2036
2040
// A non-null captured op is only valid if it resides inside of a TargetOp
2037
2041
// and is the result of calling getInnermostCapturedOmpOp() on it.
2038
2042
TargetOp targetOp =
@@ -2041,57 +2045,106 @@ TargetOp::getKernelExecFlags(Operation *capturedOp) {
2041
2045
(targetOp && targetOp.getInnermostCapturedOmpOp () == capturedOp)) &&
2042
2046
" unexpected captured op" );
2043
2047
2044
- // Make sure this region is capturing a loop. Otherwise, it's a generic
2045
- // kernel.
2048
+ // If it's not capturing a loop, it's a default target region.
2046
2049
if (!isa_and_present<LoopNestOp>(capturedOp))
2047
- return OMP_TGT_EXEC_MODE_GENERIC;
2048
-
2049
- SmallVector<LoopWrapperInterface> wrappers;
2050
- cast<LoopNestOp>(capturedOp).gatherWrappers (wrappers);
2051
- assert (!wrappers.empty ());
2050
+ return TargetRegionFlags::generic;
2052
2051
2053
- // Ignore optional SIMD leaf construct.
2054
- auto *innermostWrapper = wrappers. begin () ;
2055
- if (isa<SimdOp>(innermostWrapper))
2056
- innermostWrapper = std::next (innermostWrapper );
2052
+ auto getInnermostWrapper = [](LoopNestOp loopOp, int &numWrappers) {
2053
+ SmallVector<LoopWrapperInterface> wrappers;
2054
+ loopOp. gatherWrappers (wrappers);
2055
+ assert (!wrappers. empty () );
2057
2056
2058
- long numWrappers = std::distance (innermostWrapper, wrappers.end ());
2057
+ // Ignore optional SIMD leaf construct.
2058
+ auto *wrapper = wrappers.begin ();
2059
+ if (isa<SimdOp>(wrapper))
2060
+ wrapper = std::next (wrapper);
2059
2061
2060
- // Detect Generic-SPMD: target-teams-distribute[-simd].
2061
- if (numWrappers == 1 ) {
2062
- if (!isa<DistributeOp>(innermostWrapper))
2063
- return OMP_TGT_EXEC_MODE_GENERIC;
2062
+ numWrappers = static_cast <int >(std::distance (wrapper, wrappers.end ()));
2063
+ return wrapper;
2064
+ };
2064
2065
2065
- Operation *teamsOp = (*innermostWrapper)-> getParentOp () ;
2066
- if (!isa_and_present<TeamsOp>(teamsOp))
2067
- return OMP_TGT_EXEC_MODE_GENERIC ;
2066
+ int numWrappers ;
2067
+ LoopWrapperInterface *innermostWrapper =
2068
+ getInnermostWrapper (cast<LoopNestOp>(capturedOp), numWrappers) ;
2068
2069
2069
- if (teamsOp->getParentOp () == targetOp.getOperation ())
2070
- return OMP_TGT_EXEC_MODE_GENERIC_SPMD;
2071
- }
2070
+ if (numWrappers != 1 && numWrappers != 2 )
2071
+ return TargetRegionFlags::generic;
2072
2072
2073
- // Detect SPMD: target-teams-distribute-parallel-wsloop[-simd].
2073
+ // Detect target-teams-distribute-parallel-wsloop[-simd].
2074
2074
if (numWrappers == 2 ) {
2075
2075
if (!isa<WsloopOp>(innermostWrapper))
2076
- return OMP_TGT_EXEC_MODE_GENERIC ;
2076
+ return TargetRegionFlags::generic ;
2077
2077
2078
2078
innermostWrapper = std::next (innermostWrapper);
2079
2079
if (!isa<DistributeOp>(innermostWrapper))
2080
- return OMP_TGT_EXEC_MODE_GENERIC ;
2080
+ return TargetRegionFlags::generic ;
2081
2081
2082
2082
Operation *parallelOp = (*innermostWrapper)->getParentOp ();
2083
2083
if (!isa_and_present<ParallelOp>(parallelOp))
2084
- return OMP_TGT_EXEC_MODE_GENERIC ;
2084
+ return TargetRegionFlags::generic ;
2085
2085
2086
2086
Operation *teamsOp = parallelOp->getParentOp ();
2087
2087
if (!isa_and_present<TeamsOp>(teamsOp))
2088
- return OMP_TGT_EXEC_MODE_GENERIC ;
2088
+ return TargetRegionFlags::generic ;
2089
2089
2090
2090
if (teamsOp->getParentOp () == targetOp.getOperation ())
2091
- return OMP_TGT_EXEC_MODE_SPMD;
2091
+ return TargetRegionFlags::spmd | TargetRegionFlags::trip_count;
2092
+ }
2093
+ // Detect target-teams-distribute[-simd].
2094
+ else if (isa<DistributeOp>(innermostWrapper)) {
2095
+ Operation *teamsOp = (*innermostWrapper)->getParentOp ();
2096
+ if (!isa_and_present<TeamsOp>(teamsOp))
2097
+ return TargetRegionFlags::generic;
2098
+
2099
+ if (teamsOp->getParentOp () != targetOp.getOperation ())
2100
+ return TargetRegionFlags::generic;
2101
+
2102
+ TargetRegionFlags result =
2103
+ TargetRegionFlags::generic | TargetRegionFlags::trip_count;
2104
+
2105
+ // Find single nested parallel-do and add spmd flag (generic-spmd case).
2106
+ // TODO: This shouldn't have to be done here, as it is too easy to break.
2107
+ // The openmp-opt pass should be updated to be able to promote kernels like
2108
+ // this from "Generic" to "Generic-SPMD". However, the use of the
2109
+ // `kmpc_distribute_static_loop` family of functions produced by the
2110
+ // OMPIRBuilder for these kernels prevents that from working.
2111
+ Dialect *ompDialect = targetOp->getDialect ();
2112
+ Operation *nestedCapture =
2113
+ findCapturedOmpOp (capturedOp, [&](Operation *sibling) {
2114
+ return sibling && (ompDialect != sibling->getDialect () ||
2115
+ sibling->hasTrait <OpTrait::IsTerminator>());
2116
+ });
2117
+
2118
+ if (!isa_and_present<LoopNestOp>(nestedCapture))
2119
+ return result;
2120
+
2121
+ int numNestedWrappers;
2122
+ LoopWrapperInterface *nestedWrapper =
2123
+ getInnermostWrapper (cast<LoopNestOp>(nestedCapture), numNestedWrappers);
2124
+
2125
+ if (numNestedWrappers != 1 || !isa<WsloopOp>(nestedWrapper))
2126
+ return result;
2127
+
2128
+ Operation *parallelOp = (*nestedWrapper)->getParentOp ();
2129
+ if (!isa_and_present<ParallelOp>(parallelOp))
2130
+ return result;
2131
+
2132
+ if (parallelOp->getParentOp () != capturedOp)
2133
+ return result;
2134
+
2135
+ return result | TargetRegionFlags::spmd;
2136
+ }
2137
+ // Detect target-parallel-wsloop[-simd].
2138
+ else if (isa<WsloopOp>(innermostWrapper)) {
2139
+ Operation *parallelOp = (*innermostWrapper)->getParentOp ();
2140
+ if (!isa_and_present<ParallelOp>(parallelOp))
2141
+ return TargetRegionFlags::generic;
2142
+
2143
+ if (parallelOp->getParentOp () == targetOp.getOperation ())
2144
+ return TargetRegionFlags::spmd;
2092
2145
}
2093
2146
2094
- return OMP_TGT_EXEC_MODE_GENERIC ;
2147
+ return TargetRegionFlags::generic ;
2095
2148
}
2096
2149
2097
2150
// ===----------------------------------------------------------------------===//
0 commit comments