Null pointers with a non-0 representation

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Null pointers with a non-0 representation

Kuperstein, Michael M

I feel a bit silly asking this, but here goes.

 

The C spec  does not require the null pointer to be implemented as an integer with the 0 bit-pattern. The implementation may arbitrarily choose any other (integer, but not necessarily) representation, as long as it’s distinct from any legal pointer. The only requirement is that casting an integer 0 to a pointer type results in a null pointer.  

LLVM IR appears to have similar the casting behavior.  That is, if I try to create a pointer with integer value 0 (e.g. by constructing an inttoptr constant expression with a i32 0 argument), it gets immediately folded into a null pointer constant. Furthermore, a PtrToInt from a null pointer results in 0 (which is even “stricter” than the C spec.)

 

So, that raises a few question:

a)      Are those the desired semantics? I guess if clang generates this kind of IntToPtr instructions (as opposed to directly resolving the cast to a null pointer) this is unavoidable. But otherwise, why does LLVM treat null pointers and pointers with the integer value 0 as equivalent?
It’s completely natural for most platforms, but isn’t necessarily the right thing to do.

b)      Assuming this is really desirable, should the fact that casting an integer 0 to a pointer type results in a null pointer be documented in the langref (as part of the inttoptr/ptrtoint documentation)? Or is it too “low-level”?

c)       Let’s say I want to create a pointer with the numeric value 0, distinct from the null pointer, because 0 really does represent a valid pointer on my platform. Should this possible at the IR level? If it should be, then how?

 

Anyone has any inputs, except laughing me out of the (virtual) room? :-)

 

Thanks,

   Michael

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Null pointers with a non-0 representation

Micah Villmow

I ran into the same problem in OpenCL with Local/Private address spaces and I believe it was discussed here in the past. So I would suggest looking through the archives as I believe some of your questions are answered there.

This post might be a good starting point:

http://lists.cs.uiuc.edu/pipermail/llvmdev/2011-October/044101.html

 

Also the SPIR provisional spec as of last November deals with this issue if I remember correctly, so maybe you can discuss with Boaz Ouriel if he is still at Intel Israel about how they went about handling this problem, or look at the SPIR related posts from last year.

 

Hope this helps,

Micah

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Kuperstein, Michael M
Sent: Sunday, November 17, 2013 1:15 AM
To: [hidden email]
Subject: [LLVMdev] Null pointers with a non-0 representation

 

I feel a bit silly asking this, but here goes.

 

The C spec  does not require the null pointer to be implemented as an integer with the 0 bit-pattern. The implementation may arbitrarily choose any other (integer, but not necessarily) representation, as long as it’s distinct from any legal pointer. The only requirement is that casting an integer 0 to a pointer type results in a null pointer.  

LLVM IR appears to have similar the casting behavior.  That is, if I try to create a pointer with integer value 0 (e.g. by constructing an inttoptr constant expression with a i32 0 argument), it gets immediately folded into a null pointer constant. Furthermore, a PtrToInt from a null pointer results in 0 (which is even “stricter” than the C spec.)

 

So, that raises a few question:

a)      Are those the desired semantics? I guess if clang generates this kind of IntToPtr instructions (as opposed to directly resolving the cast to a null pointer) this is unavoidable. But otherwise, why does LLVM treat null pointers and pointers with the integer value 0 as equivalent?
It’s completely natural for most platforms, but isn’t necessarily the right thing to do.

b)      Assuming this is really desirable, should the fact that casting an integer 0 to a pointer type results in a null pointer be documented in the langref (as part of the inttoptr/ptrtoint documentation)? Or is it too “low-level”?

c)       Let’s say I want to create a pointer with the numeric value 0, distinct from the null pointer, because 0 really does represent a valid pointer on my platform. Should this possible at the IR level? If it should be, then how?

 

Anyone has any inputs, except laughing me out of the (virtual) room? :-)

 

Thanks,

   Michael

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Null pointers with a non-0 representation

Kuperstein, Michael M

Hi Micah,

 

Thanks a lot for the reference.

Unfortunately, it looks like the discussion there is dealing with a somewhat different issue – modeling OpenCL address spaces. It’s true that the null pointer issue may arise in a similar context (e.g. null pointers in different address spaces may have different internal representations), but it’s not entirely related. Or have I missed something in the thread?

 

Thanks,

  Michael

 

From: Micah Villmow [mailto:[hidden email]]
Sent: Sunday, November 17, 2013 11:42
To: Kuperstein, Michael M; [hidden email]
Subject: RE: Null pointers with a non-0 representation

 

I ran into the same problem in OpenCL with Local/Private address spaces and I believe it was discussed here in the past. So I would suggest looking through the archives as I believe some of your questions are answered there.

This post might be a good starting point:

http://lists.cs.uiuc.edu/pipermail/llvmdev/2011-October/044101.html

 

Also the SPIR provisional spec as of last November deals with this issue if I remember correctly, so maybe you can discuss with Boaz Ouriel if he is still at Intel Israel about how they went about handling this problem, or look at the SPIR related posts from last year.

 

Hope this helps,

Micah

 

From: [hidden email] [[hidden email]] On Behalf Of Kuperstein, Michael M
Sent: Sunday, November 17, 2013 1:15 AM
To: [hidden email]
Subject: [LLVMdev] Null pointers with a non-0 representation

 

I feel a bit silly asking this, but here goes.

 

The C spec  does not require the null pointer to be implemented as an integer with the 0 bit-pattern. The implementation may arbitrarily choose any other (integer, but not necessarily) representation, as long as it’s distinct from any legal pointer. The only requirement is that casting an integer 0 to a pointer type results in a null pointer.  

LLVM IR appears to have similar the casting behavior.  That is, if I try to create a pointer with integer value 0 (e.g. by constructing an inttoptr constant expression with a i32 0 argument), it gets immediately folded into a null pointer constant. Furthermore, a PtrToInt from a null pointer results in 0 (which is even “stricter” than the C spec.)

 

So, that raises a few question:

a)      Are those the desired semantics? I guess if clang generates this kind of IntToPtr instructions (as opposed to directly resolving the cast to a null pointer) this is unavoidable. But otherwise, why does LLVM treat null pointers and pointers with the integer value 0 as equivalent?
It’s completely natural for most platforms, but isn’t necessarily the right thing to do.

b)      Assuming this is really desirable, should the fact that casting an integer 0 to a pointer type results in a null pointer be documented in the langref (as part of the inttoptr/ptrtoint documentation)? Or is it too “low-level”?

c)       Let’s say I want to create a pointer with the numeric value 0, distinct from the null pointer, because 0 really does represent a valid pointer on my platform. Should this possible at the IR level? If it should be, then how?

 

Anyone has any inputs, except laughing me out of the (virtual) room? :-)

 

Thanks,

   Michael

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Null pointers with a non-0 representation

Micah Villmow

The reason why I suggested looking there is because the same questions came up as address 0 in some of the OpenCL address spaces are legal addresses. So in order to solve this problem, they would have to find answers to your questions, mainly your ‘c)’ question.  

I took a look at LangRef and it does look like there is a ‘null’ representation of the null pointer constant, which wasn’t part of LLVM back when I worked on this problem.  So maybe you are just running into an issue where the code hasn’t been updated yet because it works on everyones platform but yours.


Micah

 

From: Kuperstein, Michael M [mailto:[hidden email]]
Sent: Sunday, November 17, 2013 3:28 AM
To: Micah Villmow; [hidden email]
Subject: RE: Null pointers with a non-0 representation

 

Hi Micah,

 

Thanks a lot for the reference.

Unfortunately, it looks like the discussion there is dealing with a somewhat different issue – modeling OpenCL address spaces. It’s true that the null pointer issue may arise in a similar context (e.g. null pointers in different address spaces may have different internal representations), but it’s not entirely related. Or have I missed something in the thread?

 

Thanks,

  Michael

 

From: Micah Villmow [[hidden email]]
Sent: Sunday, November 17, 2013 11:42
To: Kuperstein, Michael M; [hidden email]
Subject: RE: Null pointers with a non-0 representation

 

I ran into the same problem in OpenCL with Local/Private address spaces and I believe it was discussed here in the past. So I would suggest looking through the archives as I believe some of your questions are answered there.

This post might be a good starting point:

http://lists.cs.uiuc.edu/pipermail/llvmdev/2011-October/044101.html

 

Also the SPIR provisional spec as of last November deals with this issue if I remember correctly, so maybe you can discuss with Boaz Ouriel if he is still at Intel Israel about how they went about handling this problem, or look at the SPIR related posts from last year.

 

Hope this helps,

Micah

 

From: [hidden email] [[hidden email]] On Behalf Of Kuperstein, Michael M
Sent: Sunday, November 17, 2013 1:15 AM
To: [hidden email]
Subject: [LLVMdev] Null pointers with a non-0 representation

 

I feel a bit silly asking this, but here goes.

 

The C spec  does not require the null pointer to be implemented as an integer with the 0 bit-pattern. The implementation may arbitrarily choose any other (integer, but not necessarily) representation, as long as it’s distinct from any legal pointer. The only requirement is that casting an integer 0 to a pointer type results in a null pointer.  

LLVM IR appears to have similar the casting behavior.  That is, if I try to create a pointer with integer value 0 (e.g. by constructing an inttoptr constant expression with a i32 0 argument), it gets immediately folded into a null pointer constant. Furthermore, a PtrToInt from a null pointer results in 0 (which is even “stricter” than the C spec.)

 

So, that raises a few question:

a)      Are those the desired semantics? I guess if clang generates this kind of IntToPtr instructions (as opposed to directly resolving the cast to a null pointer) this is unavoidable. But otherwise, why does LLVM treat null pointers and pointers with the integer value 0 as equivalent?
It’s completely natural for most platforms, but isn’t necessarily the right thing to do.

b)      Assuming this is really desirable, should the fact that casting an integer 0 to a pointer type results in a null pointer be documented in the langref (as part of the inttoptr/ptrtoint documentation)? Or is it too “low-level”?

c)       Let’s say I want to create a pointer with the numeric value 0, distinct from the null pointer, because 0 really does represent a valid pointer on my platform. Should this possible at the IR level? If it should be, then how?

 

Anyone has any inputs, except laughing me out of the (virtual) room? :-)

 

Thanks,

   Michael

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Null pointers with a non-0 representation

Micah Villmow

+llvm-dev

Yes. The question then becomes, does LLVM handle the case where the cast of the value 0 to a non-void* pointer?

 

Think of this case:

int *ptr = (int*)0;

 

based on a strict reading of the spec, ptr itself technically is not the null pointer constant. If ptr points to an object in the local address space in OpenCL, or any address space where 0 is a valid address and memory exists, does LLVM handle this correctly?

 

I don’t believe that it does, but I can’t say for certain.

 

Micah

 

From: Michael LIAO [mailto:[hidden email]]
Sent: Sunday, November 17, 2013 10:46 PM
To: Micah Villmow
Subject: Re: [LLVMdev] Null pointers with a non-0 representation

 

Hi Micah & Michael

 

By C standard, section 6.3.2.2, "An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function." As LLVM IR is modelled after C, I assume null pointer in LLVM IR is equivalent to 0 as well. The folding from 0 to null pointer in LLVM follows that behaviour very similar.

 

- michael

 

 

On Sun, Nov 17, 2013 at 1:00 PM, Micah Villmow <[hidden email]> wrote:

The reason why I suggested looking there is because the same questions came up as address 0 in some of the OpenCL address spaces are legal addresses. So in order to solve this problem, they would have to find answers to your questions, mainly your ‘c)’ question.  

I took a look at LangRef and it does look like there is a ‘null’ representation of the null pointer constant, which wasn’t part of LLVM back when I worked on this problem.  So maybe you are just running into an issue where the code hasn’t been updated yet because it works on everyones platform but yours.


Micah

 

From: Kuperstein, Michael M [mailto:[hidden email]]
Sent: Sunday, November 17, 2013 3:28 AM
To: Micah Villmow; [hidden email]


Subject: RE: Null pointers with a non-0 representation

 

Hi Micah,

 

Thanks a lot for the reference.

Unfortunately, it looks like the discussion there is dealing with a somewhat different issue – modeling OpenCL address spaces. It’s true that the null pointer issue may arise in a similar context (e.g. null pointers in different address spaces may have different internal representations), but it’s not entirely related. Or have I missed something in the thread?

 

Thanks,

  Michael

 

From: Micah Villmow [[hidden email]]
Sent: Sunday, November 17, 2013 11:42
To: Kuperstein, Michael M; [hidden email]
Subject: RE: Null pointers with a non-0 representation

 

I ran into the same problem in OpenCL with Local/Private address spaces and I believe it was discussed here in the past. So I would suggest looking through the archives as I believe some of your questions are answered there.

This post might be a good starting point:

http://lists.cs.uiuc.edu/pipermail/llvmdev/2011-October/044101.html

 

Also the SPIR provisional spec as of last November deals with this issue if I remember correctly, so maybe you can discuss with Boaz Ouriel if he is still at Intel Israel about how they went about handling this problem, or look at the SPIR related posts from last year.

 

Hope this helps,

Micah

 

From: [hidden email] [[hidden email]] On Behalf Of Kuperstein, Michael M
Sent: Sunday, November 17, 2013 1:15 AM
To: [hidden email]
Subject: [LLVMdev] Null pointers with a non-0 representation

 

I feel a bit silly asking this, but here goes.

 

The C spec  does not require the null pointer to be implemented as an integer with the 0 bit-pattern. The implementation may arbitrarily choose any other (integer, but not necessarily) representation, as long as it’s distinct from any legal pointer. The only requirement is that casting an integer 0 to a pointer type results in a null pointer.  

LLVM IR appears to have similar the casting behavior.  That is, if I try to create a pointer with integer value 0 (e.g. by constructing an inttoptr constant expression with a i32 0 argument), it gets immediately folded into a null pointer constant. Furthermore, a PtrToInt from a null pointer results in 0 (which is even “stricter” than the C spec.)

 

So, that raises a few question:

a)      Are those the desired semantics? I guess if clang generates this kind of IntToPtr instructions (as opposed to directly resolving the cast to a null pointer) this is unavoidable. But otherwise, why does LLVM treat null pointers and pointers with the integer value 0 as equivalent?
It’s completely natural for most platforms, but isn’t necessarily the right thing to do.

b)      Assuming this is really desirable, should the fact that casting an integer 0 to a pointer type results in a null pointer be documented in the langref (as part of the inttoptr/ptrtoint documentation)? Or is it too “low-level”?

c)       Let’s say I want to create a pointer with the numeric value 0, distinct from the null pointer, because 0 really does represent a valid pointer on my platform. Should this possible at the IR level? If it should be, then how?

 

Anyone has any inputs, except laughing me out of the (virtual) room? :-)

 

Thanks,

   Michael

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

 


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Null pointers with a non-0 representation

Clark Smith Cox III

On Nov 19, 2013, at 10:52, Micah Villmow <[hidden email]> wrote:

+llvm-dev
Yes. The question then becomes, does LLVM handle the case where the cast of the value 0 to a non-void* pointer?
 
Think of this case:
int *ptr = (int*)0;
 
based on a strict reading of the spec, ptr itself technically is not the null pointer constant.

Not true, the literal zero *is* a null pointer constant, which is then converted to a null pointer.


If ptr points to an object in the local address space in OpenCL, or any address space where 0 is a valid address and memory exists, does LLVM handle this correctly?
 
I don’t believe that it does, but I can’t say for certain.
 
Micah
 
From: Michael LIAO [[hidden email]] 
Sent: Sunday, November 17, 2013 10:46 PM
To: Micah Villmow
Subject: Re: [LLVMdev] Null pointers with a non-0 representation
 
Hi Micah & Michael
 
By C standard, section 6.3.2.2, "An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function." As LLVM IR is modelled after C, I assume null pointer in LLVM IR is equivalent to 0 as well. The folding from 0 to null pointer in LLVM follows that behaviour very similar.
 
- michael
 

 

On Sun, Nov 17, 2013 at 1:00 PM, Micah Villmow <[hidden email]> wrote:
The reason why I suggested looking there is because the same questions came up as address 0 in some of the OpenCL address spaces are legal addresses. So in order to solve this problem, they would have to find answers to your questions, mainly your ‘c)’ question.  
I took a look at LangRef and it does look like there is a ‘null’ representation of the null pointer constant, which wasn’t part of LLVM back when I worked on this problem.  So maybe you are just running into an issue where the code hasn’t been updated yet because it works on everyones platform but yours.

Micah
 
From: Kuperstein, Michael M [mailto:[hidden email]] 
Sent: Sunday, November 17, 2013 3:28 AM
To: Micah Villmow; [hidden email]

Subject: RE: Null pointers with a non-0 representation
 
Hi Micah,
 
Thanks a lot for the reference.
Unfortunately, it looks like the discussion there is dealing with a somewhat different issue – modeling OpenCL address spaces. It’s true that the null pointer issue may arise in a similar context (e.g. null pointers in different address spaces may have different internal representations), but it’s not entirely related. Or have I missed something in the thread?
 
Thanks,
  Michael
 
From: Micah Villmow [[hidden email]] 
Sent: Sunday, November 17, 2013 11:42
To: Kuperstein, Michael M; [hidden email]
Subject: RE: Null pointers with a non-0 representation
 
I ran into the same problem in OpenCL with Local/Private address spaces and I believe it was discussed here in the past. So I would suggest looking through the archives as I believe some of your questions are answered there.
This post might be a good starting point:
 
Also the SPIR provisional spec as of last November deals with this issue if I remember correctly, so maybe you can discuss with Boaz Ouriel if he is still at Intel Israel about how they went about handling this problem, or look at the SPIR related posts from last year.
 
Hope this helps,
Micah
 
From: [hidden email] [[hidden email]] On Behalf Of Kuperstein, Michael M
Sent: Sunday, November 17, 2013 1:15 AM
To: [hidden email]
Subject: [LLVMdev] Null pointers with a non-0 representation
 
I feel a bit silly asking this, but here goes.
 
The C spec  does not require the null pointer to be implemented as an integer with the 0 bit-pattern. The implementation may arbitrarily choose any other (integer, but not necessarily) representation, as long as it’s distinct from any legal pointer. The only requirement is that casting an integer 0 to a pointer type results in a null pointer.  
LLVM IR appears to have similar the casting behavior.  That is, if I try to create a pointer with integer value 0 (e.g. by constructing an inttoptr constant expression with a i32 0 argument), it gets immediately folded into a null pointer constant. Furthermore, a PtrToInt from a null pointer results in 0 (which is even “stricter” than the C spec.)
 
So, that raises a few question:

a)      Are those the desired semantics? I guess if clang generates this kind of IntToPtr instructions (as opposed to directly resolving the cast to a null pointer) this is unavoidable. But otherwise, why does LLVM treat null pointers and pointers with the integer value 0 as equivalent? 
It’s completely natural for most platforms, but isn’t necessarily the right thing to do.

b)      Assuming this is really desirable, should the fact that casting an integer 0 to a pointer type results in a null pointer be documented in the langref (as part of the inttoptr/ptrtoint documentation)? Or is it too “low-level”?

c)       Let’s say I want to create a pointer with the numeric value 0, distinct from the null pointer, because 0 really does represent a valid pointer on my platform. Should this possible at the IR level? If it should be, then how?

 
Anyone has any inputs, except laughing me out of the (virtual) room? :-)
 
Thanks,
   Michael

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

 
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Null pointers with a non-0 representation

Micah Villmow

Ahh yeah duh, your right. I misread that.

 

That doesn’t solve this problem however, which is a different example of what I was trying to show but failed.

kernel void test(local int* a) {

local int*ptr = NULL;

if (a != ptr) *a = 0;

}

 

from host code:

test(0);

 

Micah

From: Clark Smith Cox III [mailto:[hidden email]]
Sent: Tuesday, November 19, 2013 12:40 PM
To: Micah Villmow
Cc: Michael LIAO; [hidden email]
Subject: Re: [LLVMdev] Null pointers with a non-0 representation

 

 

On Nov 19, 2013, at 10:52, Micah Villmow <[hidden email]> wrote:



+llvm-dev

Yes. The question then becomes, does LLVM handle the case where the cast of the value 0 to a non-void* pointer?

 

Think of this case:

int *ptr = (int*)0;

 

based on a strict reading of the spec, ptr itself technically is not the null pointer constant.

 

Not true, the literal zero *is* a null pointer constant, which is then converted to a null pointer.

 



If ptr points to an object in the local address space in OpenCL, or any address space where 0 is a valid address and memory exists, does LLVM handle this correctly?

 

I don’t believe that it does, but I can’t say for certain.

 

Micah

 

From: Michael LIAO [[hidden email]] 
Sent: Sunday, November 17, 2013 10:46 PM
To: Micah Villmow
Subject: Re: [LLVMdev] Null pointers with a non-0 representation

 

Hi Micah & Michael

 

By C standard, section 6.3.2.2, "An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function." As LLVM IR is modelled after C, I assume null pointer in LLVM IR is equivalent to 0 as well. The folding from 0 to null pointer in LLVM follows that behaviour very similar.

 

- michael

 

 

On Sun, Nov 17, 2013 at 1:00 PM, Micah Villmow <[hidden email]> wrote:

The reason why I suggested looking there is because the same questions came up as address 0 in some of the OpenCL address spaces are legal addresses. So in order to solve this problem, they would have to find answers to your questions, mainly your ‘c)’ question.  

I took a look at LangRef and it does look like there is a ‘null’ representation of the null pointer constant, which wasn’t part of LLVM back when I worked on this problem.  So maybe you are just running into an issue where the code hasn’t been updated yet because it works on everyones platform but yours.


Micah

 

From: Kuperstein, Michael M [mailto:[hidden email]] 
Sent: Sunday, November 17, 2013 3:28 AM
To: Micah Villmow; [hidden email]


Subject: RE: Null pointers with a non-0 representation

 

Hi Micah,

 

Thanks a lot for the reference.

Unfortunately, it looks like the discussion there is dealing with a somewhat different issue – modeling OpenCL address spaces. It’s true that the null pointer issue may arise in a similar context (e.g. null pointers in different address spaces may have different internal representations), but it’s not entirely related. Or have I missed something in the thread?

 

Thanks,

  Michael

 

From: Micah Villmow [[hidden email]] 
Sent: Sunday, November 17, 2013 11:42
To: Kuperstein, Michael M; [hidden email]
Subject: RE: Null pointers with a non-0 representation

 

I ran into the same problem in OpenCL with Local/Private address spaces and I believe it was discussed here in the past. So I would suggest looking through the archives as I believe some of your questions are answered there.

This post might be a good starting point:

 

Also the SPIR provisional spec as of last November deals with this issue if I remember correctly, so maybe you can discuss with Boaz Ouriel if he is still at Intel Israel about how they went about handling this problem, or look at the SPIR related posts from last year.

 

Hope this helps,

Micah

 

From: [hidden email] [[hidden email]] On Behalf Of Kuperstein, Michael M
Sent: Sunday, November 17, 2013 1:15 AM
To: [hidden email]
Subject: [LLVMdev] Null pointers with a non-0 representation

 

I feel a bit silly asking this, but here goes.

 

The C spec  does not require the null pointer to be implemented as an integer with the 0 bit-pattern. The implementation may arbitrarily choose any other (integer, but not necessarily) representation, as long as it’s distinct from any legal pointer. The only requirement is that casting an integer 0 to a pointer type results in a null pointer.  

LLVM IR appears to have similar the casting behavior.  That is, if I try to create a pointer with integer value 0 (e.g. by constructing an inttoptr constant expression with a i32 0 argument), it gets immediately folded into a null pointer constant. Furthermore, a PtrToInt from a null pointer results in 0 (which is even “stricter” than the C spec.)

 

So, that raises a few question:

a)      Are those the desired semantics? I guess if clang generates this kind of IntToPtr instructions (as opposed to directly resolving the cast to a null pointer) this is unavoidable. But otherwise, why does LLVM treat null pointers and pointers with the integer value 0 as equivalent? 
It’s completely natural for most platforms, but isn’t necessarily the right thing to do.

b)      Assuming this is really desirable, should the fact that casting an integer 0 to a pointer type results in a null pointer be documented in the langref (as part of the inttoptr/ptrtoint documentation)? Or is it too “low-level”?

c)       Let’s say I want to create a pointer with the numeric value 0, distinct from the null pointer, because 0 really does represent a valid pointer on my platform. Should this possible at the IR level? If it should be, then how?

 

Anyone has any inputs, except laughing me out of the (virtual) room? :-)

 

Thanks,

   Michael

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

 

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

 


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev