This is an archive of the discontinued LLVM Phabricator instance.

[LVI] Try to document the complexities of context instructions
AbandonedPublic

Authored by reames on Oct 7 2016, 5:46 PM.

Details

Summary

This is an attempt to document and clarify the semantics of the context instruction parameter and how it interacts with LVI's primary analysis algorithm. I've found myself having to explain this a bunch of times over and I'm pretty sure that *I* get it wrong about half the time.

I'm really curious if this explanation makes sense to others and if they think it's actually correct w.r.t. both implementation and intent. :) With that in mind, I've added both LVI old hands and a few folks who've touched it a bit recently. I'd appreciate multiple sets of eyes.

Diff Detail

Event Timeline

reames updated this revision to Diff 74005.Oct 7 2016, 5:46 PM
reames retitled this revision from to [LVI] Try to document the complexities of context instructions.
reames updated this object.
reames added a subscriber: llvm-commits.
sanjoy added inline comments.Oct 9 2016, 12:14 PM
include/llvm/Analysis/LazyValueInfo.h
68

I almost hate nitpicking on this, but you have an extra ~.

69

I'd s/Block, Edge/basic block or basic block edge/ The way you've written it now, it looks like a tuple of a block and an edge.

73

s/a true/are true/

78

This is stylistic and my personal preference (so feel free to ignore), but I'd not put the stuff about IPO / guards / assumes here (maybe as a footnote, but not "inlined" here like this).

Instead, I'd say However, there are control flow facts which can become true part way through a block (for instance, once after a pointer has been dereferenced, we know it is non-null). and continue with your main point of describing CtxI.

84

Have you tried restructuring the description this way:

The LVI query predicates accept a CFG element (Block, Edge) and an optional context instruction.  When a context instruction is present ... (semantics of the context instruction).  If it isn't present, we pretend the context instruction is the beginning of the block / somewhere "inside" the edge.
91

But what if execution does not reach the context instruction?

reames added inline comments.Oct 11 2016, 5:40 PM
include/llvm/Analysis/LazyValueInfo.h
78

I really think the assume/guard bits need to be there, but otherwise someone reading has no way to connect it to the implementation. Adding a footnote with an reference might work. Would you find that cleaner?

84

Hm, you repharsing of the missing context case is interesting. In particular, we might be able to implement the semantics you suggest and get slightly more precise results than we do today. This *isn't* our semantic today, but it would be a completely reasonable one and I don't think it would break any existing users.

We'd have to pick beginning or end consistently. End seems like it's more powerful, what do you think? (i.e. for an invoke's normal edge, we'd want the context to be the first non-phi instruction in the normal block, not the invoke itself.)

91

Today, I believe the answer is "don't do that!" or "UB". This may very well be a latent bug in a couple of transforms. JumpThreading - which is the main transform which exploits this - seems okay on first read.

anna added inline comments.Oct 14 2016, 12:21 PM
include/llvm/Analysis/LazyValueInfo.h
77

s/an context/a context/

84

How is the value chosen currently when the context instruction is not specified? Is this correct:

  1. in the case of single Block -- value of Y depends on all instructions in BB? One of these instructions might as well be the context instruction, but it wasnt specified.
  2. for Edge (start BB, end BB) -- value of Y depends on instructions from first inst in start BB upto last inst in end BB.
lib/Analysis/LazyValueInfo.cpp
965

extra 'is' - 'This result is safe to'

anna added inline comments.Jun 13 2017, 1:38 PM
include/llvm/Analysis/LazyValueInfo.h
84

Coming back to this, since it would be better to document this clearly and clarify our assumptions :)

I think LVI info is for an entire basic block. Once we have guards and instructions that are not `guaranteedToTransferExecutionToSuccessor` in the block, the info at beginning maybe different from the end. However, this is handled correctly depending on the Context Instruction passed in. If there's no CtxtI instruction, we should conservatively chose the info as that in the beginning of the block in such cases.
reames planned changes to this revision.Jan 25 2019, 10:11 AM
reames abandoned this revision.Dec 3 2020, 3:06 PM