New temporary constant operator may be created in getPointerAlignment() function.
This instance is not used by any instruction. However later it may lead to errors during
code processing as not expected.
Remove this constant after alignment calculation as no more required.
Details
- Reviewers
lebedev.ri jdoerfert nikic
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
I don't believe the question
was answered. Do we have this guarantee in the first place?
Maybe, we don't have it. But I thought it wasn't a good practice to change something in get-methods (except caches, maybe). If we can cleanup instances after some unexpected changes why not to do so?
If we can cleanup instances after some unexpected changes why not to do so?
The big problem here is that getPointerAlignment could be used in places where not all uses of a constant are actual "Use" instances. Then you have a really subtle use-after-free. (This could happen in places with maps on the side, like frontends or complex transforms).
If you really want to avoid creating extra constants, probably the right strategy is to avoid calling getPtrToInt in the first place. There aren't that many ways to construct a pointer that getPtrToInt can fold to a ConstantInt, anyway. (Off the top of my head, it would have to be null, an inttoptr, or a gep of one of those.)
I was hoping that would be a viable solution. Maybe matching null explicitly and otherwise just dealing with the constant that we have. IIRC, @lebedev.ri mentioned that would not work well but I let him explain.
I mean, sure, we can avoid using getPtrToInt and hardcode what it does here, but still, why?
Either the current state breaks guarantees and we should fix it,
or it does not and this roughly falls under the "c++ api is unstable" and whatever code
was relying on the unwritten guarantee should be relaxed not to rely on it.
When reading the following definition of method 'MaybeAlign Value::getPointerAlignment(const DataLayout &DL) const' most users assume that Value won't be modified by this method. If we don't guarantee it then it would be nice to remove const-qualifier and change name on something like 'accountPointerAlignment'. As renaming is less possible then a comment might be present that this method may change an IR.
The general rule for IR passes is that dangling constants may exist, and transformations are expected to correctly handle them (ignore or erase them, as appropriate). So creating them should be harmless across IR passes.
That's not really an excuse for IR queries that don't obviously involve creating constants to do so, though: it could be confusing within a pass. For example, if a pass is trying to iterate over a use list, and asking for the alignment modifies that use list.