[cfe-dev] Can indirect class parameters be noalias?

Hal Finkel via cfe-dev cfe-dev at lists.llvm.org
Fri Jul 31 04:35:15 PDT 2020


On 7/29/20 9:00 PM, John McCall via cfe-dev wrote:
>
> On 29 Jul 2020, at 17:42, Richard Smith wrote:
>
>     On Wed, 29 Jul 2020 at 12:52, John McCall <rjmccall at apple.com> wrote:
>
>     ...
>
>     I think concretely, the escape hatch doesn't stop things from
>     going wrong,
>     because -- as you note -- even though we *could* have made a copy,
>     it's
>     observable whether or not we *did* make a copy. For example:
>
> I would say that it’s observable whether the parameter variable has
> the same address as the argument. That doesn’t /have/ to be the same
> question as whether a copy was performed: we could consider there to be
> a formal copy (or series of copies) that ultimately creates /an/ object
> at the same address, but it’s not the /same/ object and so pointers
> to the old object no longer validly pointer to it. But I guess that
> would probably violate the lifetime rules, because it would make accesses
> through old pointers UB when in fact they should at worst access a valid
> object that’s just unrelated to the parameter object.
>

I think that it would be great to be able to do this, but unfortunately, 
I think that the point that you raise here is a key issue. Whether or 
not the copy is performed is visible in the model, and so we can't 
simply act as though there was a copy when optimizing. Someone could 
easily have code that looks like:

Foo DefaultX;

...

void something(Foo &A, Foo &B) {

   if (&A == &B) { ... }

}

void bar(Foo X) { something(X, DefaultX); }

As Richard's example shows, the code doesn't need to explicitly compare 
the addresses to detect the copy either. Any code that reads/writes to 
the objects can do it. A perhaps-more-realistic example might be:

   int Cnt = A.RefCnt; ++A.RefCnt; ++B.RefCnt; if (Cnt + 1 != A.RefCnt) 
{ /* same object case */ }

The best suggestion that I have so far is that we could add an attribute 
like 'can_copy' indicating that the optimizer can make a formal copy of 
the argument in the callee and use that instead of the original pointer 
if that seems useful. I can certainly imagine a transformation such as 
LICM making use of such a thing (although the cost modeling would 
probably need to be fairly conservative).

  -Hal


> ...
>
> John.
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-- 
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200731/18a913fb/attachment-0001.html>


More information about the cfe-dev mailing list