[LLVMdev] Proposal: New IR instruction for casting between address spaces

Dan Gohman gohman at apple.com
Tue Sep 11 13:28:13 PDT 2012


On Sep 11, 2012, at 1:03 PM, "Villmow, Micah" <Micah.Villmow at amd.com> wrote:

>  
> From: Villmow, Micah 
> Sent: Tuesday, September 11, 2012 12:51 PM
> To: llvm-commits at cs.uiuc.edu
> Subject: Proposal: New IR instruction for casting between address spaces
>  
> Problem:
> Bit casting between pointers of different address spaces only works if all address space pointers are the same size. With changes from email chain [1][2], support for different pointer sizes breaks the bitcast instruction since there is no guarantee that the pointer size for the address space is on the source and destination arguments are of the same size.

Can you comment on whether the need for this seems like a fundamental
need, in your field, or more of a limitation of the current generation of
architectures?

> Solution:
> Remove the ability of bitcast to cast between pointers of different address spaces and replace with an instruction that handles this case explicitely.
>  
> Proposed changes:
> ·         Add restriction to the verifier on the bitcast instruction making bitcasting between address spaces illegal.
> ·         Change documentation[3] to state the bitcast to pointers of different address spaces is illegal.
> ·         Add in a new IR node, addrspacecast, that allows conversions between address spaces
> ·         Updated the reader/writer to handle these cases
> ·         Update the documentation to insert the new IR node.
> ·         Add the following documentation:
> 'addrspacecast .. to' Instruction
> 
> Syntax:
> 
>   <result> = addrspacecast <ty> <value> to <ty2>             ; yields ty2
> Overview:
> 
> The ' addrspacecast ' instruction converts value to type ty2 without changing any bits.

This is mildly imprecise, because the whole point of this instruction is that
it can change the bit width.

> 
> Arguments:
> 
> The ' addrspacecast ' instruction takes a value to cast, which must be a non-aggregate first class value with a pointer type, and a type to cast it to, which must also be a pointer type. The pointer types of value and the destination type, ty2, must be identical, except for the address space.

Having a "pointer type" is sufficient to imply that it is a
"non-aggregate first class value".

> 
> Semantics:
> 
> The ' addrspacecast ' instruction converts value to type ty2. It converts the type to the type that is implied by the address space of the destination pointer. If the destination pointer is smaller than the source pointer, the upper bits are truncated. If the inverse is true, the upper bits are sign extended, otherwise the operation is a no-op.

Why sign-extended? Ptrtoint/inttoptr are zero-extended, and it's surprising
that addrspacecast would be different from them.

Dan





More information about the llvm-dev mailing list