This is an archive of the discontinued LLVM Phabricator instance.

[Clang][OpenMP] Fix the issue that globalization doesn't work with byval struct function argument
Changes PlannedPublic

Authored by tianshilei1992 on Jul 1 2022, 10:39 AM.

Details

Reviewers
jdoerfert
ABataev
Summary

This patch fixes the issue that the globalized variable is not properly
initialized when it is a byval struct function argument.

Fixes #56218.

Diff Detail

Event Timeline

tianshilei1992 created this revision.Jul 1 2022, 10:39 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 1 2022, 10:39 AM
tianshilei1992 requested review of this revision.Jul 1 2022, 10:39 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 1 2022, 10:39 AM

fix unused variable

rebase and fix test error

tianshilei1992 edited the summary of this revision. (Show Details)Jul 6 2022, 8:51 AM

cleanup/destructor test missing.

@ABataev WDYT?

cleanup/destructor test missing.

@ABataev WDYT?

Looks good in general, extra tests would be good

tianshilei1992 planned changes to this revision.Jul 8 2022, 5:01 PM

callCStructCopyConstructor is actually for Objective-C…Cannot use it here.

callCStructCopyConstructor is actually for Objective-C…Cannot use it here.

Don't we generate copies of things elsewhere already?

callCStructCopyConstructor is actually for Objective-C…Cannot use it here.

Don't we generate copies of things elsewhere already?

No we don't. I think the best place to emit copy is where the globalized variable is generated.

tianshilei1992 added inline comments.
clang/lib/CodeGen/CGDecl.cpp
2501

Hi @aaron.ballman, is it possible to invoke the copy constructor of a struct/class here, either it is user defined or default one?

callCStructCopyConstructor is actually for Objective-C…Cannot use it here.

Is it for ObjC? Looking at the comments, it looks like it's for C:

// These functions emit calls to the special functions of non-trivial C
// structs.
clang/lib/CodeGen/CGDecl.cpp
2501

CC @rjmccall and @efriedma for codegen owner opinions.

I'm honestly not certain; I would imagine that OpenMP defines what should happen, but this is outside of my area of expertise.

callCStructCopyConstructor is actually for Objective-C…Cannot use it here.

Is it for ObjC? Looking at the comments, it looks like it's for C:

// These functions emit calls to the special functions of non-trivial C
// structs.

That's not ObjC. It can be C or C++.

My apology I should have added more context here. In OpenMP, we need to "globalize" certain captured local variables by allocating another buffers and then copy the memory. If the local variables are structs or even C++ classes, the copy has to be done by invoking the corresponding copy constructors. However, I don't know how to invoke the copy constructor here. callCStructCopyConstructor only does plain copy. It's not gonna work for C++ classes.

callCStructCopyConstructor is actually for Objective-C…Cannot use it here.

Is it for ObjC? Looking at the comments, it looks like it's for C:

// These functions emit calls to the special functions of non-trivial C
// structs.

That's not ObjC. It can be C or C++.

My apology I should have added more context here. In OpenMP, we need to "globalize" certain captured local variables by allocating another buffers and then copy the memory. If the local variables are structs or even C++ classes, the copy has to be done by invoking the corresponding copy constructors. However, I don't know how to invoke the copy constructor here. callCStructCopyConstructor only does plain copy. It's not gonna work for C++ classes.

You need to do it manually. Usually we built the required copy constructor call in Sema (it requires correct lookup) and then emitted it in codegen.

callCStructCopyConstructor is actually for Objective-C…Cannot use it here.

Is it for ObjC? Looking at the comments, it looks like it's for C:

// These functions emit calls to the special functions of non-trivial C
// structs.

That's not ObjC. It can be C or C++.

My apology I should have added more context here. In OpenMP, we need to "globalize" certain captured local variables by allocating another buffers and then copy the memory. If the local variables are structs or even C++ classes, the copy has to be done by invoking the corresponding copy constructors. However, I don't know how to invoke the copy constructor here. callCStructCopyConstructor only does plain copy. It's not gonna work for C++ classes.

You need to do it manually. Usually we built the required copy constructor call in Sema (it requires correct lookup) and then emitted it in codegen.

But we don't do globalization in Sema right?

callCStructCopyConstructor is actually for Objective-C…Cannot use it here.

Is it for ObjC? Looking at the comments, it looks like it's for C:

// These functions emit calls to the special functions of non-trivial C
// structs.

That's not ObjC. It can be C or C++.

My apology I should have added more context here. In OpenMP, we need to "globalize" certain captured local variables by allocating another buffers and then copy the memory. If the local variables are structs or even C++ classes, the copy has to be done by invoking the corresponding copy constructors. However, I don't know how to invoke the copy constructor here. callCStructCopyConstructor only does plain copy. It's not gonna work for C++ classes.

You need to do it manually. Usually we built the required copy constructor call in Sema (it requires correct lookup) and then emitted it in codegen.

But we don't do globalization in Sema right?

Right. If we really needed the constructor call, we emitted special helper expressions/statements and stored them in the associated construct/clause, and then emitted it, if required.

Right. C structs can require more than just memcpy to copy in several different situations (all involving language extensions), but it doesn't require Sema to be involved: it doesn't introduce uses of user declarations, and the compiler can figure out the work it needs to do with a straightforward recursive inspection of the field types, so IRGen just synthesizes those operations automatically when requested. Neither condition holds in C++, so Sema has to synthesize a copy expression which resolves the correct copy constructor and sets up any extra arguments it might require, triggering appropriate diagnostics and/or tracking of used decls. There are a bunch of places we do that for various language extensions already, including a bunch in the OpenMP code, and we usually just store the expression in the associated AST node.

@rjmccall @ABataev Thanks for the help. So the basic idea is to build the expressions in Sema for those captured decls and then emit them accordingly in code gen, right?

There are a bunch of places we do that for various language extensions already, including a bunch in the OpenMP code, and we usually just store the expression in the associated AST node.

Could you please point me a rough location where we already have similar things such that I can learn and do the same thing for globalization? Thanks.

@rjmccall @ABataev Thanks for the help. So the basic idea is to build the expressions in Sema for those captured decls and then emit them accordingly in code gen, right?

Yes.

There are a bunch of places we do that for various language extensions already, including a bunch in the OpenMP code, and we usually just store the expression in the associated AST node.

Could you please point me a rough location where we already have similar things such that I can learn and do the same thing for globalization? Thanks.

You can check ActOnOpenMPFirstprivateClause, lines 18322-18353 (building of VDInitRefExpr)

There are a bunch of places we do that for various language extensions already, including a bunch in the OpenMP code, and we usually just store the expression in the associated AST node.

Could you please point me a rough location where we already have similar things such that I can learn and do the same thing for globalization? Thanks.

You can check ActOnOpenMPFirstprivateClause, lines 18322-18353 (building of VDInitRefExpr)

Really appreciate that.