[llvm-dev] Understanding Chains

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Thu Jun 29 10:26:55 PDT 2017


Hi Dilan,

On 29 June 2017 at 10:16, Dilan Manatunga via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
> In the Code Generator documentation, they describe that chains should be
> used with instructions that have side effects loads, stores, calls, etc. I
> guess I am confused to as why those instructions need chains. For example,
> won't loads have a dependency on address to ensure they are only scheduled
> after address generation.

The address calculation will be ordered, but the memory being accessed
isn't modelled in the DAG. So without chains LLVM won't know which of
a load and a store to the same address needs to happen first.

> Is it
> because of memory disambiguation causing us to be conservative and needing
> to ensure that certain store/loads aren't re-ordered respect to each other?

Sort of, the problem exists even if we know for sure that two
operations are to the same address though, so I wouldn't call it
"conservative" really, we simply have no way to decide without Chains.

> I guess the real question is when I include chains for operations and when I
> want to return chains from operations (especially intrinsics). For example,
> with the backend, when I am inserting Loads from the stacks,  I don't want
> to chain them all together for performance, right, since it doesn't matter
> when the loads are scheduled (as long as it is before the use). But I should
> chain them to the incoming chain from LowerFormalArguments parameter.

For FormalArguments that should be OK because you generally know for a
fact that they're to different addresses so reordering them is
harmless.

> On somewhat related note, take this example DAG
> https://i.stack.imgur.com/RQRpq.gif, why does copyfromreg need a chain
> input?

It's a very similar issue. The physical registers aren't values in the
DAG so if your DAG contained a CopyFromReg and a CopyToReg mentioning
the same register LLVM has no way of knowing which it should do first,
but that has important effects on the result.

As with loads, LowerFormalArguments should be able to use the entry
node for the chain.

You can sort of think of physical registers as just another kind of
memory to unify the model.

Cheers.

Tim.


More information about the llvm-dev mailing list