[LLVMdev] possible addrspacecast problem

Sanjoy Das sanjoy at playingwithpointers.com
Sun Mar 15 23:25:09 PDT 2015


The LangRef says this about addrspacecast: "Note that if the address
space conversion is legal then both result and operand refer to the
same memory location.".  This brings forth two related questions:

 1. what happens if you execute an "invalid" addrspacecast?  Does this
    axiom make addrspacecast non-hoistable?  From a quick glance at
    the code, ValueTracking seems to assume that addrspacecasts can be
    speculatively executed.

 2. even if two pointers from two different address spaces point to
    the same "location", can one of them not be dereferenceable while
    the other is?  In other words, does dereferenceability depend on
    the addrspace of the pointer given that the "location" is
    dereferenceable?

On Sun, Mar 15, 2015 at 10:43 PM, Junjie Gu <jgu222 at gmail.com> wrote:
> Given a pointer,  does any addrspacecast affect the pointer's
> dereferenceablity ?  For example,
>     %pm = addrspaacecast float addrspacecast(n)* %pn to float
> addrspacecast(m)*
>     %r  = load float addrspace(m)* %pm
> In another word. the question is whether the following is true ?
>     isDereferenceablePointer(pn) == isDereferenceablePointer(pm)
>
>   [Note that the function is defined as
>    Value.cpp:isDereferenceaablePointer(const Value*, ...) ]
>
>
> The reason I am asking this is that the current LLVM thinks the answer is
> yes
> (they are the same).  But for the following case, it will make
> non-speculative
> load to be moved out of its guard.  For example (assume that
> isDereferenceablePointer(p)
> is true),
>
> The following code:
>
>      %pm = addrspaacecast float* %p to float addrspacecast(m)*
>      if (p is in addrspace(m))
>         // BB_then
>         %r0 = load float,  float addrspace(m)* %pm;
>      else  // p is in default addrspace
>         // BB_else
>         %r1 = load float,  float* p;
>      %r = phi float [r0, BB_then], [r1, BB_else]
>
> will be transformed to
>
>      %pm = addrspaacecast float* %p to float addrspacecast(m)*
>      %r0 = load float addrspace(m)* %pm;
>      %r1 = load float* p;
>      %r = select i1 (p is in adrspace(m)) float %r0, float %r1
>
> which is wrong as "%r0 = load float addrspace(m)* %pm" would be illegal
> if (p is in addrspace(m)) is false.
>
> If we agree that the answer to the above question is no, then fix
> is simple (fix diff is attached). I also have a small test to show
> the problem,  simplifyCFG actually does the above transformation
> (to reproduce, use "opt -simplifycfg -S test3.ll -o tt.ll).
>
> Any suggestions/comments ?
>
>
> thanks
> Junjie
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>



More information about the llvm-dev mailing list