This is an archive of the discontinued LLVM Phabricator instance.

[Flang][openmp] Add semantic checks for OpenMP critical construct name resolution
ClosedPublic

Authored by NimishMishra on Sep 26 2021, 8:12 AM.

Details

Summary

Taken forward from https://reviews.llvm.org/D93051 (authored by @sameeranjoshi )

As reported in https://bugs.llvm.org/show_bug.cgi?id=48145, name resolution for omp critical construct was failing. This patch adds functionality to help that name resolution as well as implementation to catch name mismatches.

  • Changes to check-omp-structure.cpp

In llvm-project/flang/lib/Semantics/check-omp-structure.cpp, under void OmpStructureChecker::Enter(const parser::OpenMPCriticalConstruct &x), logic is added to handle the different forms of name mismatches and report appropriate error. The following semantic restrictions are therefore handled here:

  1. If a name is specified on a critical directive, the same name must also be specified on the end critical directive
  2. If no name appears on the critical directive, no name can appear on the end critical directive
  3. If a name appears on either the start critical directive or the end critical directive

The only allowed combinations are: (1) both start and end directives have same name, (2) both start and end directives have NO name altogether

  1. A hint clause may not exist on a critical directive that is unnamed
  • Changes to resolve-directives.cpp

In llvm-project/flang/lib/Semantics/resolve-directives.cpp, two Pre functions are added for OmpCriticalDirective and OmpEndCriticalDirective that invoke the services of ResolveOmpName to give new symbols to the names. This aids in removing the undesirable behavior noted in https://bugs.llvm.org/show_bug.cgi?id=48145

Diff Detail

Event Timeline

NimishMishra created this revision.Sep 26 2021, 8:12 AM
NimishMishra requested review of this revision.Sep 26 2021, 8:12 AM
clementval added inline comments.Sep 29 2021, 6:28 AM
flang/lib/Semantics/check-omp-structure.cpp
13

unrelated change?

flang/test/Semantics/omp-sync-critical02.f90
18

I'm not sure I follow what this test adds to the same code in omp-sync-critical01.f90...

NimishMishra added inline comments.Sep 29 2021, 7:18 AM
flang/lib/Semantics/check-omp-structure.cpp
13

Not sure what's caused this. Could it be possible that git-clang-format introduced these?

flang/test/Semantics/omp-sync-critical02.f90
18

I added this verbatim from the artifact at https://bugs.llvm.org/show_bug.cgi?id=48145. Should I remove it?

clementval added inline comments.Sep 29 2021, 8:45 AM
flang/lib/Semantics/check-omp-structure.cpp
13

Quite unlikely but who knows :-) If you can just undo the change it would be great.

flang/test/Semantics/omp-sync-critical02.f90
18

Looks like it is tested in your omp-sync-critical01.f90 so if it's a duplicate just remove it.

NimishMishra marked 2 inline comments as done.Sep 29 2021, 11:16 PM
NimishMishra added inline comments.
flang/lib/Semantics/check-omp-structure.cpp
13

Removed this

Thanks @NimishMishra for taking this forward. Can you include the parser changes also from https://reviews.llvm.org/D93051?

NimishMishra marked an inline comment as done.EditedOct 7 2021, 7:48 AM

Thanks @NimishMishra for taking this forward. Can you include the parser changes also from https://reviews.llvm.org/D93051?

Thanks for the comment. I analyzed this as well as worked on a few semantic checks related to hint clause. Are we sure that we require this as a clause list? Because everything is working smoothly anyway. In D93051, parser changes involves changing the optional clause to a list, of which I do not presently understand the requirement.

Also the clause list is no longer a std::optional. However, clauses are actually optional. But there exist examples in https://www.openmp.org/wp-content/uploads/openmp-examples-5.0.0.pdf on page 221 which indicates that it is indeed optional.

Thanks @NimishMishra for taking this forward. Can you include the parser changes also from https://reviews.llvm.org/D93051?

Thanks for the comment. I analyzed this as well as worked on a few semantic checks related to hint clause. Are we sure that we require this as a clause list? Because everything is working smoothly anyway. In D93051, parser changes involves changing the optional clause to a list, of which I do not presently understand the requirement.

Also the clause list is no longer a std::optional. However, clauses are actually optional. But there exist examples in https://www.openmp.org/wp-content/uploads/openmp-examples-5.0.0.pdf on page 221 which indicates that it is indeed optional.

The clause list should never be optional. Only the clauses inside of the list should be optional or not. D93051 is important to make sure we have a homogenous way of dealing with clauses and the OMP.td is correctly defined. Anyway D93051 should probably be updated and landed separately if you don't bring these changes in.

Thanks @NimishMishra for taking this forward. Can you include the parser changes also from https://reviews.llvm.org/D93051?

Thanks for the comment. I analyzed this as well as worked on a few semantic checks related to hint clause. Are we sure that we require this as a clause list? Because everything is working smoothly anyway. In D93051, parser changes involves changing the optional clause to a list, of which I do not presently understand the requirement.

Also the clause list is no longer a std::optional. However, clauses are actually optional. But there exist examples in https://www.openmp.org/wp-content/uploads/openmp-examples-5.0.0.pdf on page 221 which indicates that it is indeed optional.

The clause list should never be optional. Only the clauses inside of the list should be optional or not. D93051 is important to make sure we have a homogenous way of dealing with clauses and the OMP.td is correctly defined. Anyway D93051 should probably be updated and landed separately if you don't bring these changes in.

+1.

Also, this will reduce the diff between fir-dev and llvm-project/flang since the parser changes are already in fir-dev.

Thanks @NimishMishra for taking this forward. Can you include the parser changes also from https://reviews.llvm.org/D93051?

Thanks for the comment. I analyzed this as well as worked on a few semantic checks related to hint clause. Are we sure that we require this as a clause list? Because everything is working smoothly anyway. In D93051, parser changes involves changing the optional clause to a list, of which I do not presently understand the requirement.

Also the clause list is no longer a std::optional. However, clauses are actually optional. But there exist examples in https://www.openmp.org/wp-content/uploads/openmp-examples-5.0.0.pdf on page 221 which indicates that it is indeed optional.

The clause list should never be optional. Only the clauses inside of the list should be optional or not. D93051 is important to make sure we have a homogenous way of dealing with clauses and the OMP.td is correctly defined. Anyway D93051 should probably be updated and landed separately if you don't bring these changes in.

Thanks. I understand the necessity of those changes better now. I will bring forward those parser changes in this patch only.

@NimishMishra Can you prioritise this patch for completion today?

@NimishMishra Can you prioritise this patch for completion today?

Okay. I have added one hint clause semantic check as well. Hope that is fine too. It also has to done with name resolution problem only: namely, a hint clause can't exist on a critical directive that is unnamed.

Everything is ready. As soon as advise on this, I push the revised diff here.

@NimishMishra Can you prioritise this patch for completion today?

Okay. I have added one hint clause semantic check as well. Hope that is fine too. It also has to done with name resolution problem only: namely, a hint clause can't exist on a critical directive that is unnamed.

Everything is ready. As soon as advise on this, I push the revised diff here.

Yes, please add that check also to this patch.

NimishMishra edited the summary of this revision. (Show Details)
NimishMishra marked 2 inline comments as done.
kiranchandramohan requested changes to this revision.Oct 12 2021, 5:14 AM

I have one request for change.

I think the other hint clause specific checks can come in a separate patch.

flang/test/Semantics/omp-sync-critical02.f90
47

The standard says the following and I think this is not an error.

"Unless the effect is as if hint(omp_sync_hint_none) was specified, the critical construct must specify a name."

This revision now requires changes to proceed.Oct 12 2021, 5:14 AM

I have one request for change.

I think the other hint clause specific checks can come in a separate patch.

Should I revert the hint clause change from this patch?

Yes I am working on a separate patch for hint clauses. Should I move this patch's hint clause test to that one?

flang/test/Semantics/omp-sync-critical02.f90
47

You are right. I missed it

I have one request for change.

I think the other hint clause specific checks can come in a separate patch.

Should I revert the hint clause change from this patch?

Yes I am working on a separate patch for hint clauses. Should I move this patch's hint clause test to that one?

You can keep this hint check in this patch.

LGTM. I have two NIT comments. Please apply these if possible.

Once you submit, can you cherry-pick the changes to the fir-dev branch?

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

Nit: It would be better if you can get the value of the expression and check for it to be ZERO. You can also add a comment above indication what this check is.

| | | | OmpClauseList -> OmpClause -> Hint -> Constant -> Expr = '0_4'
| | | | | Designator -> DataRef -> Name = 'omp_sync_hint_none'
1058

Nit: Consider changing to something like the following.

Hint clause with a value other than omp_sync_hint_none cannot be specified for an unnamed CRITICAL directive.

This revision is now accepted and ready to land.Oct 12 2021, 8:37 AM
NimishMishra marked 2 inline comments as done.Oct 12 2021, 9:43 AM

Thanks @kiranchandramohan and @clementval for comments on the patch. I am working on another hint clause related patch and am taking forward a little TODO to it: improving hint clause comparison.

@kiranchandramohan You mentioned cherry picking this to fir-dev. Do you want just this patch or sections patch as well in the cherry picking? Also whenever your bandwidth allows, please have a quick look at https://reviews.llvm.org/D110714. There's a little question I have posted regarded design choice of a function. Once resolved, I can spend time on that revision too and take it to closure.

Many thanks.

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

Yes this is better.

I gave time to this, but as of now, can't figure our a clean solution to this. The problem is OmpClause defines a variant and that warrants its own std::visit or a HasMember thing.

This hint clause thing is still in my radar though, because a few checks still need to be done for these. So I plan to figure out a clean way (or if I can't, have another function with std::visit to do the comparison) and
come back to change this.

1058

Done

NimishMishra marked 3 inline comments as done.Oct 12 2021, 9:43 AM

Thanks @kiranchandramohan and @clementval for comments on the patch. I am working on another hint clause related patch and am taking forward a little TODO to it: improving hint clause comparison.

OK

@kiranchandramohan You mentioned cherry picking this to fir-dev. Do you want just this patch or sections patch as well in the cherry picking?

Just this patch.

Also whenever your bandwidth allows, please have a quick look at https://reviews.llvm.org/D110714. There's a little question I have posted regarded design choice of a function. Once resolved, I can spend time on that revision too and take it to closure.

I will have a look tomorrow.