This is an archive of the discontinued LLVM Phabricator instance.

CodeGen: Cast temporary variable to proper address space
ClosedPublic

Authored by yaxunl on May 30 2017, 6:41 PM.

Details

Summary

In C++ all variables are in default address space. Previously change has been made to cast automatic variables to default address space. However that is not sufficient since all temporary variables need to be casted to default address space.

This patch casts all temporary variables to default address space except those for passing indirect arguments since they are only used for load/store.

This patch only affects target having non-zero alloca address space.

Diff Detail

Event Timeline

yaxunl created this revision.May 30 2017, 6:41 PM
yaxunl updated this revision to Diff 100819.May 30 2017, 8:19 PM

Fix comments.

Anastasia added inline comments.May 31 2017, 11:06 AM
lib/CodeGen/CGDecl.cpp
1107

Is this code still needed here, considering that CreateTempAlloca is doing this already?

1109

Why removing the comment?

lib/CodeGen/CodeGenFunction.h
1930

I am still confused, what is the case the alloca AS is different from the language AS?

yaxunl marked an inline comment as done.May 31 2017, 11:25 AM
yaxunl added inline comments.
lib/CodeGen/CGDecl.cpp
1107

I have planed but forgot to remove this. This is also the reason I removed the comments. I will remove it in the update.

lib/CodeGen/CodeGenFunction.h
1930

In C or C++ language where all variables without explicit address space attribute are assumed to be in the default address space.

yaxunl updated this revision to Diff 100899.May 31 2017, 11:44 AM
yaxunl marked an inline comment as done.

Remove redundant code.

rjmccall requested changes to this revision.May 31 2017, 5:48 PM

Please split this up into two patches:

  • one for your target-specific changes to the global address space and so on
  • one for the generic fix to CreateTempAlloca etc.
lib/CodeGen/CGExpr.cpp
77

These arguments should both be AST address spaces, not lowered address spaces. That is, this second argument should be LangAS::Default, not DestAddrSpace.

...we should really make proper types for these instead of always passing them around as unsigneds.

lib/CodeGen/CodeGenFunction.h
1931

"On certain targets, the alloca address space is not the same as the generic address space.
Since the address of a temporary is often exposed to the program as a pointer or reference
in the generic address space, this function will convert the returned address into the generic
address space by default. That conversion may be suppressed by passing false as
\p CastToGenericAddrSpace."

And please rename DoCast to CastToGenericAddrSpace.

1979

"generic" instead of "expected", please, and rename DoCast to CastToGenericAddrSpace.

This revision now requires changes to proceed.May 31 2017, 5:48 PM
yaxunl updated this revision to Diff 101070.Jun 1 2017, 12:06 PM
yaxunl edited edge metadata.
yaxunl marked 2 inline comments as done.
yaxunl retitled this revision from [AMDGPU] Fix address space for global and temporary variables in C++ to CodeGen: Cast temporary variable to proper address space.
yaxunl edited the summary of this revision. (Show Details)

Keep changes for temporary variables only.

rjmccall added inline comments.Jun 1 2017, 5:16 PM
lib/CodeGen/CGCall.cpp
3805

How about "indirect-arg-temp" as the name here?

3835

"byval-temp"

lib/CodeGen/CodeGenFunction.h
1937

You have an extra newline here.

Hmm. I see what you're getting at with your addition to the comment, but I think the
real problem is that my original recommendation is just wrong. Let's fix that.

"LangAS::Default is the address space of pointers to local variables and temporaries,
as exposed in the source language. In certain configurations, this is not the same as
the alloca address space, and a cast is needed to lift the pointer from the alloca AS
into LangAS::Default. This can happen when the target uses a restricted address space
for the stack but the source language requires LangAS::Default to be a generic address
space. The latter condition is common for most programming languages; OpenCL is an
exception in that LangAS::Default is the private address space, which naturally maps
to the stack.

Because the address of a temporary is often exposed to the program in various ways,
this function will perform the cast by default. The cast may be avoided by passing false
as \p CastToDefaultAddrSpace; this is more efficient if the caller knows that the address
will not be exposed."

And you should rename the parameter to CastToDefaultAddrSpace here and the other
places.

yaxunl updated this revision to Diff 101221.Jun 2 2017, 8:40 AM
yaxunl marked 7 inline comments as done.

Revised by John's comments.

Thanks. One tweak and then LGTM.

lib/CodeGen/CodeGenFunction.h
1937

This line should have ///.

yaxunl updated this revision to Diff 101227.Jun 2 2017, 9:25 AM

Fix a comment.

rjmccall accepted this revision.Jun 2 2017, 9:28 AM

Thanks!

This revision is now accepted and ready to land.Jun 2 2017, 9:28 AM
This revision was automatically updated to reflect the committed changes.