-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][OpenMP] Rewrite getOpenMPCaptureRegions
in terms of leafs
#97110
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
Conversation
What is considered "executable" in clang differs slightly from the OpenMP's "executable" category. In addition to the executable category, subsidiary directives, and OMPD_error are considered executable. Implement a function that performs that check.
Check if the given directive can capture variables, and thus needs a captured statement. Simplify some code using this function.
…/c02-is-capturing
Replace the switch in `getOpenMPCaptureRegions` with a loop collecting capture regions based on the constituent directives.
getOpenMPCaptureRegions
in term of leafsgetOpenMPCaptureRegions
in terms of leafs
@llvm/pr-subscribers-clang Author: Krzysztof Parzyszek (kparzysz) ChangesReplace the switch in Full diff: https://github.com/llvm/llvm-project/pull/97110.diff 1 Files Affected:
diff --git a/clang/lib/Basic/OpenMPKinds.cpp b/clang/lib/Basic/OpenMPKinds.cpp
index 30c34c207ae23..152891dfa27dc 100644
--- a/clang/lib/Basic/OpenMPKinds.cpp
+++ b/clang/lib/Basic/OpenMPKinds.cpp
@@ -747,105 +747,79 @@ void clang::getOpenMPCaptureRegions(
assert(unsigned(DKind) < llvm::omp::Directive_enumSize);
assert(isOpenMPCapturingDirective(DKind));
- switch (DKind) {
- case OMPD_metadirective:
- CaptureRegions.push_back(OMPD_metadirective);
- break;
- case OMPD_parallel:
- case OMPD_parallel_for:
- case OMPD_parallel_for_simd:
- case OMPD_parallel_master:
- case OMPD_parallel_masked:
- case OMPD_parallel_sections:
- case OMPD_distribute_parallel_for:
- case OMPD_distribute_parallel_for_simd:
- case OMPD_parallel_loop:
- CaptureRegions.push_back(OMPD_parallel);
- break;
- case OMPD_target_teams:
- case OMPD_target_teams_distribute:
- case OMPD_target_teams_distribute_simd:
- CaptureRegions.push_back(OMPD_task);
- CaptureRegions.push_back(OMPD_target);
- CaptureRegions.push_back(OMPD_teams);
- break;
- case OMPD_teams:
- case OMPD_teams_distribute:
- case OMPD_teams_distribute_simd:
- CaptureRegions.push_back(OMPD_teams);
- break;
- case OMPD_target:
- case OMPD_target_simd:
- CaptureRegions.push_back(OMPD_task);
- CaptureRegions.push_back(OMPD_target);
- break;
- case OMPD_teams_loop:
- case OMPD_teams_distribute_parallel_for:
- case OMPD_teams_distribute_parallel_for_simd:
- CaptureRegions.push_back(OMPD_teams);
- CaptureRegions.push_back(OMPD_parallel);
- break;
- case OMPD_target_parallel:
- case OMPD_target_parallel_for:
- case OMPD_target_parallel_for_simd:
- case OMPD_target_parallel_loop:
- CaptureRegions.push_back(OMPD_task);
- CaptureRegions.push_back(OMPD_target);
- CaptureRegions.push_back(OMPD_parallel);
- break;
- case OMPD_task:
- case OMPD_target_enter_data:
- case OMPD_target_exit_data:
- case OMPD_target_update:
- CaptureRegions.push_back(OMPD_task);
- break;
- case OMPD_taskloop:
- case OMPD_taskloop_simd:
- case OMPD_master_taskloop:
- case OMPD_master_taskloop_simd:
- case OMPD_masked_taskloop:
- case OMPD_masked_taskloop_simd:
- CaptureRegions.push_back(OMPD_taskloop);
- break;
- case OMPD_parallel_masked_taskloop:
- case OMPD_parallel_masked_taskloop_simd:
- case OMPD_parallel_master_taskloop:
- case OMPD_parallel_master_taskloop_simd:
- CaptureRegions.push_back(OMPD_parallel);
- CaptureRegions.push_back(OMPD_taskloop);
- break;
- case OMPD_target_teams_loop:
- case OMPD_target_teams_distribute_parallel_for:
- case OMPD_target_teams_distribute_parallel_for_simd:
- CaptureRegions.push_back(OMPD_task);
- CaptureRegions.push_back(OMPD_target);
- CaptureRegions.push_back(OMPD_teams);
- CaptureRegions.push_back(OMPD_parallel);
- break;
- case OMPD_nothing:
- CaptureRegions.push_back(OMPD_nothing);
- break;
- case OMPD_loop:
- // TODO: 'loop' may require different capture regions depending on the bind
- // clause or the parent directive when there is no bind clause. Use
- // OMPD_unknown for now.
- case OMPD_simd:
- case OMPD_for:
- case OMPD_for_simd:
- case OMPD_sections:
- case OMPD_single:
- case OMPD_taskgroup:
- case OMPD_distribute:
- case OMPD_ordered:
- case OMPD_target_data:
- case OMPD_distribute_simd:
- case OMPD_scope:
- case OMPD_dispatch:
+ auto getRegionsForLeaf = [&](OpenMPDirectiveKind LKind) {
+ assert(isLeafConstruct(LKind) && "Epecting leaf directive");
+ switch (LKind) {
+ case OMPD_metadirective:
+ CaptureRegions.push_back(OMPD_metadirective);
+ break;
+ case OMPD_nothing:
+ CaptureRegions.push_back(OMPD_nothing);
+ break;
+ case OMPD_parallel:
+ CaptureRegions.push_back(OMPD_parallel);
+ break;
+ case OMPD_target:
+ CaptureRegions.push_back(OMPD_task);
+ CaptureRegions.push_back(OMPD_target);
+ break;
+ case OMPD_task:
+ case OMPD_target_enter_data:
+ case OMPD_target_exit_data:
+ case OMPD_target_update:
+ CaptureRegions.push_back(OMPD_task);
+ break;
+ case OMPD_teams:
+ CaptureRegions.push_back(OMPD_teams);
+ break;
+ case OMPD_taskloop:
+ CaptureRegions.push_back(OMPD_taskloop);
+ break;
+ case OMPD_loop:
+ // TODO: 'loop' may require different capture regions depending on the
+ // bind clause or the parent directive when there is no bind clause.
+ // If any of the directives that push regions here are parents of 'loop',
+ // assume 'parallel'. Otherwise do nothing.
+ if (!CaptureRegions.empty() &&
+ !llvm::is_contained(CaptureRegions, OMPD_parallel))
+ CaptureRegions.push_back(OMPD_parallel);
+ break;
+ case OMPD_dispatch:
+ case OMPD_distribute:
+ case OMPD_for:
+ case OMPD_masked:
+ case OMPD_master:
+ case OMPD_ordered:
+ case OMPD_scope:
+ case OMPD_sections:
+ case OMPD_simd:
+ case OMPD_single:
+ case OMPD_target_data:
+ case OMPD_taskgroup:
+ // These directives (when standalone) use OMPD_unknown as the region,
+ // but when they're constituents of a compound directive, and other
+ // leafs from that directive have specific regions, then these directives
+ // add no additional regions.
+ break;
+ default:
+ llvm::errs() << getOpenMPDirectiveName(LKind) << '\n';
+ llvm_unreachable("Unexpected directive");
+ }
+ };
+
+ for (OpenMPDirectiveKind L : getLeafConstructsOrSelf(DKind))
+ getRegionsForLeaf(L);
+
+ // If no regions were added, then only the leafs using "unknown" were
+ // present. Push a single OMPD_unknown as the capture region.
+ if (CaptureRegions.empty())
CaptureRegions.push_back(OMPD_unknown);
- break;
- default:
- llvm_unreachable("Unhandled OpenMP directive");
- }
+
+ // OMPD_unknown is only expected as the only region. If other regions
+ // are present OMPD_unknown should not be present.
+ assert((CaptureRegions[0] == OMPD_unknown ||
+ !llvm::is_contained(CaptureRegions, OMPD_unknown)) &&
+ "Misplaced OMPD_unknown");
}
bool clang::checkFailClauseParameter(OpenMPClauseKind FailClauseParameter) {
|
clang/lib/Basic/OpenMPKinds.cpp
Outdated
case OMPD_distribute_simd: | ||
case OMPD_scope: | ||
case OMPD_dispatch: | ||
auto getRegionsForLeaf = [&](OpenMPDirectiveKind LKind) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto getRegionsForLeaf = [&](OpenMPDirectiveKind LKind) { | |
auto GetRegionsForLeaf = [&](OpenMPDirectiveKind LKind) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
clang/lib/Basic/OpenMPKinds.cpp
Outdated
// If no regions were added, then only the leafs using "unknown" were | ||
// present. Push a single OMPD_unknown as the capture region. | ||
if (CaptureRegions.empty()) | ||
CaptureRegions.push_back(OMPD_unknown); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not like this code, this mayt hide bugs, better insert OPD_unknown explicitly where expected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a flag that is set when OMPD_unknown may be required. It's set only for the specific directives.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why just don't add OMPD_unknown in lambda? This extra flag makes the code more complex and less readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because these directives may be encountered as parts of a combined directive. If they push OMPD_unknown, we may end up with it together with other directives. We can do that, but then we'd have to strip the OMPD_unknown if there are other regions present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, for OMPD_parallel_sections, we'd end up with [OMPD_parallel, OMPD_unknown].
Co-authored-by: Alexey Bataev <[email protected]>
Co-authored-by: Alexey Bataev <[email protected]>
Co-authored-by: Alexey Bataev <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LG
Lands and disables: 9a7248a [clang][OpenMP] Rewrite `getOpenMPCaptureRegions` in terms of leafs (llvm#97110) │ needs more debugging. Change-Id: I10f94b3d96c3045eb7dfe4a76bcc76d776dc5c33
…lvm#97110) Replace the switch in `getOpenMPCaptureRegions` with a loop collecting capture regions based on the constituent directives. --------- Co-authored-by: Alexey Bataev <[email protected]>
…lvm#97110) Replace the switch in `getOpenMPCaptureRegions` with a loop collecting capture regions based on the constituent directives. --------- Co-authored-by: Alexey Bataev <[email protected]>
Replace the switch in
getOpenMPCaptureRegions
with a loop collecting capture regions based on the constituent directives.