Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

invalid bitcast->gep inbounds #42846

Closed
nunoplopes opened this issue Sep 29, 2019 · 8 comments
Closed

invalid bitcast->gep inbounds #42846

nunoplopes opened this issue Sep 29, 2019 · 8 comments
Labels
bugzilla Issues migrated from bugzilla

Comments

@nunoplopes
Copy link
Member

Bugzilla Link 43501
Resolution FIXED
Resolved on Oct 13, 2019 10:26
Version trunk
OS All
CC @jdoerfert,@aqjune,@LebedevRI,@regehr,@rotateright
Fixed by commit(s) r373847, r374728

Extended Description

See below the invalid transformation of a bitcast to a gep inbounds. The input to the bitcast is a function argument, which could be out-of-bounds.

llvm/test/Transforms/InstCombine/cast.ll

define [4 x float]* @​test27([9 x [4 x float]]* %A) {
; CHECK-NEXT: [[C:%.]] = getelementptr inbounds [9 x [4 x float]], [9 x [4 x float]] [[A:%.*]], i64 0, i64 0

%c = bitcast [9 x [4 x float]]* %A to [4 x float]*
ret [4 x float]* %c
}

@rotateright
Copy link
Contributor

No idea if this is the best fix, but it's the simplest:
https://reviews.llvm.org/D68244

@nunoplopes
Copy link
Member Author

Looking at the changed tests, I think it looks ok.
There's a little improvement that can be done in e.g. InstCombine/load-bitcast-vec.ll, which is to use the "dereferenceable(16)" tag on the argument to keep inbounds.

Another easy-ish optimization is to keep inbounds if gep has only one use and the user is a memory access that is guaranteed to dereference the memory. Dereferencing a poison value or a OOB pointer doesn't really matter.
I guess this could be a separate rewrite or something.
We could think of other ways to recover inbounds tags if needed.

@LebedevRI
Copy link
Member

Looking at the changed tests, I think it looks ok.
There's a little improvement that can be done in e.g.
InstCombine/load-bitcast-vec.ll, which is to use the "dereferenceable(16)"
tag on the argument to keep inbounds.
+1

Another easy-ish optimization is to keep inbounds if gep has only one use
and the user is a memory access that is guaranteed to dereference the
memory.
Attributor should deal with that.

@jdoerfert
Copy link
Member

Looking at the changed tests, I think it looks ok.
There's a little improvement that can be done in e.g.
InstCombine/load-bitcast-vec.ll, which is to use the "dereferenceable(16)"
tag on the argument to keep inbounds.
+1

Agreed.

Another easy-ish optimization is to keep inbounds if gep has only one use
and the user is a memory access that is guaranteed to dereference the
memory.
Attributor should deal with that.

Agreed, especially since you have to propagate dereferenceability backwards from a potential access. See https://reviews.llvm.org/D65402 :)

@rotateright
Copy link
Contributor

https://reviews.llvm.org/rL373847

There's an open question (for me at least) about the semantics of a null pointer in a non-default address space, so we probably need to answer that before closing this bug report.

@nunoplopes
Copy link
Member Author

https://reviews.llvm.org/rL373847

There's an open question (for me at least) about the semantics of a null
pointer in a non-default address space, so we probably need to answer that
before closing this bug report.

That's a good point. There's a bit of confusion around that in multiple places in LLVM.
My take on that is that we can't assume anything about null for AS != 0. That means that we can have a block allocated there or not. It's an address like any other.

So while null in AS=0 is in bounds, null in AS != 0 isn't necessarily. Actually I don't even think we should have a null constant in AS != 0 because it just creates confusion; it's not a special value.

@LebedevRI
Copy link
Member

https://reviews.llvm.org/rL373847

There's an open question (for me at least) about the semantics of a null
pointer in a non-default address space, so we probably need to answer that
before closing this bug report.

That's a good point. There's a bit of confusion around that in multiple
places in LLVM.
My take on that is that we can't assume anything about null for AS != 0.
That means that we can have a block allocated there or not. It's an address
like any other.

So while null in AS=0 is in bounds, null in AS != 0 isn't necessarily.
Actually I don't even think we should have a null constant in AS != 0
because it just creates confusion; it's not a special value.

I agree with that assessment, it matches my understanding.

@rotateright
Copy link
Contributor

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla
Projects
None yet
Development

No branches or pull requests

4 participants