This is an archive of the discontinued LLVM Phabricator instance.

Fix assert when inlining a constantexpr addrspacecast
ClosedPublic

Authored by arsenm on Jul 15 2015, 2:37 PM.

Details

Reviewers
hfinkel
Summary

The pointer size of the addrspacecasted pointer might not have matched,
so this would have hit an assert in accumulateConstantOffset.

I think this was here to allow constant folding of a load of an
addrspacecasted constant. Accumulating the offset through the
addrspacecast doesn't make much sense, so something else is necessary
to allow folding the load through this cast.

Diff Detail

Event Timeline

arsenm updated this revision to Diff 29835.Jul 15 2015, 2:37 PM
arsenm retitled this revision from to Fix assert when inlining a constantexpr addrspacecast.
arsenm updated this object.
arsenm added a subscriber: llvm-commits.
majnemer added inline comments.
lib/Analysis/ConstantFolding.cpp
250

Isn't there still a bug here? The size of the result type of ptrtoint can disagree with its operand's type.

Sounds like the correct thing to do would be to call the static member function CastInst::isNoopCast with appropriate operands.

arsenm added inline comments.Jul 15 2015, 3:06 PM
lib/Analysis/ConstantFolding.cpp
250

This is what I thought at first to, but ptrtoints with noncanonical integer sizes seem to work. I'm not exactly sure why

arsenm added inline comments.Jul 22 2015, 11:54 AM
lib/Analysis/ConstantFolding.cpp
250

I think I understand why it isn't necessary to worry about that case. Because this doesn't look through inttoptr, we don't see pointer size changes through the ptrtoint/inttoptr path.

Accumulating the offset through the addrspacecast doesn't make much sense

Why does it not make sense? It just means that the base object is in a different address space from the original derived pointer. The LangRef says:

Note that if the address space conversion is legal then both result and operand refer to the same memory location.

and so, unless the addresscast were somehow illegal (is that possible for a constant expression addresscast?) then it should have a well-defined meaning. Am I missing something?

Accumulating the offset through the addrspacecast doesn't make much sense

Why does it not make sense? It just means that the base object is in a different address space from the original derived pointer. The LangRef says:

Note that if the address space conversion is legal then both result and operand refer to the same memory location.

and so, unless the addresscast were somehow illegal (is that possible for a constant expression addresscast?) then it should have a well-defined meaning. Am I missing something?

The addrspacecast can totally change the representation of the pointer. In the case I ran into, the original pointer is a 32-bit offset into special on chip memory being casted to a 64-bit address space. The pointer is converted to a full virtual 64-bit address which will resolve to the same location, but doesn't have a common base with the original pointer, so there isn't a simple add relationship between the two.

hfinkel accepted this revision.Jul 26 2015, 9:50 AM
hfinkel added a reviewer: hfinkel.

Accumulating the offset through the addrspacecast doesn't make much sense

Why does it not make sense? It just means that the base object is in a different address space from the original derived pointer. The LangRef says:

Note that if the address space conversion is legal then both result and operand refer to the same memory location.

and so, unless the addresscast were somehow illegal (is that possible for a constant expression addresscast?) then it should have a well-defined meaning. Am I missing something?

The addrspacecast can totally change the representation of the pointer. In the case I ran into, the original pointer is a 32-bit offset into special on chip memory being casted to a 64-bit address space. The pointer is converted to a full virtual 64-bit address which will resolve to the same location, but doesn't have a common base with the original pointer, so there isn't a simple add relationship between the two.

Ah, okay. So the problem is then that you can't subtract them (or something like that). Or more specifically, the address space is part of the type, and constant folding can't change the type.

LGTM.

This revision is now accepted and ready to land.Jul 26 2015, 9:50 AM
arsenm closed this revision.Jul 27 2015, 11:31 AM

r243300