This is an archive of the discontinued LLVM Phabricator instance.

[OpenMP] Always apply target declarations to canonical definitions
ClosedPublic

Authored by jhuber6 on Jun 20 2023, 10:10 AM.

Details

Summary

This patch changes the handling of OpenMP to add the device attributes
to the canonical definitions when we encounter a non-canonical
definition. Previously, the following code would not work because it
would find the non-canonical definition first which would then not be
used anywhere else.

int x;
extern int x;

This patch now adds the attribute to both of them. This allows us to
perform the following operation if, for example, there were an
implementation of stderr on the device.

#include <stdio.h>

// List of libc symbols supported on the device.
extern FILE *stderr;

Unfortunately I cannot think of an equivalent solution to HIP / CUDA
device declarations as those are done with simple attributes. Attributes
themselves cannot be used to affect a definition once its canonical
definition has already been seen. Some help on that front would be
appreciated.

Fixes https://github.com/llvm/llvm-project/issues/63355

Diff Detail

Event Timeline

jhuber6 created this revision.Jun 20 2023, 10:10 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 20 2023, 10:10 AM
jhuber6 requested review of this revision.Jun 20 2023, 10:10 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 20 2023, 10:10 AM
ABataev added inline comments.Jun 20 2023, 11:20 AM
clang/lib/Sema/SemaOpenMP.cpp
23099 ↗(On Diff #532979)

Need to use ML->DeclarationMarkedOpenMPDeclareTarget for CD too.

jhuber6 updated this revision to Diff 533000.Jun 20 2023, 11:30 AM

Adding AST mutation listener to the other modified declaration to signal that it was changed.

jhuber6 updated this revision to Diff 533002.Jun 20 2023, 11:33 AM

Fix logic

Did you try instead fix the OMPDeclareTargetDeclAttr::getActiveAttr() function to make it look through all the declarations and return the attribute from the first found instead of adding a new attribute?

clang/lib/Sema/SemaOpenMP.cpp
22993–22999 ↗(On Diff #533002)

Here it would be better to reconstruct the attribute and make it implicit attribute.

23105–23112 ↗(On Diff #533002)

Here it would be better to reconstruct the attribute and make it implicit attribute

Did you try instead fix the OMPDeclareTargetDeclAttr::getActiveAttr() function to make it look through all the declarations and return the attribute from the first found instead of adding a new attribute?

I originally tried that but found that once we've found a canonical declaration, nothing will really bind to the new non-canonical definition. So the only way to do it would be to scan the entire file with the source manager as far as I could tell. I could be wrong though, I'm not as familiar with Clang here.

clang/lib/Sema/SemaOpenMP.cpp
23105–23112 ↗(On Diff #533002)

I thought it was already implcit since we use OMPDeclareTargetDeclAttr::CreateImplicit above, what would be different in the new version?

Did you try instead fix the OMPDeclareTargetDeclAttr::getActiveAttr() function to make it look through all the declarations and return the attribute from the first found instead of adding a new attribute?

I originally tried that but found that once we've found a canonical declaration, nothing will really bind to the new non-canonical definition. So the only way to do it would be to scan the entire file with the source manager as far as I could tell. I could be wrong though, I'm not as familiar with Clang here.

Hmm, it should not be so. Did you try to get redecls()?

jhuber6 updated this revision to Diff 534950.Jun 27 2023, 6:08 AM

Updating to use VD->redecls().

Thanks for pointing that out, couldn't find it when I looked initially.

ABataev accepted this revision.Jun 27 2023, 6:22 AM

LG with a nit

clang/lib/Sema/SemaOpenMP.cpp
22991 ↗(On Diff #534950)

Remove new line here

This revision is now accepted and ready to land.Jun 27 2023, 6:22 AM
This revision was landed with ongoing or failed builds.Jun 27 2023, 7:15 AM
This revision was automatically updated to reflect the committed changes.