llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Incorrect liveness information after updating MBB live-ins through RDF

Open last5bits opened this issue 7 years ago • 7 comments

Bugzilla Link 39960
Version trunk
OS All
CC @francisvm,@kparzysz-quic

Extended Description

Consider a case of an imaginary register R1 that is declared live-in in BB#1:

BB#0: %R1 = define ...

BB#1: Live Ins: %R1 … = use %R1

Then, the code went through an RDF-based transformation, which lead to updating live-ins of each basic block:

BB#0: %R1 = define ...

BB#1: Live Ins: %R0 … = use %R1

But why do we have %R0 instead of %R1? One thing I didn't mention about this imaginary processor is that R0 and R1 completely overlap with each other. Nevertheless, the live-in information at this point is incorrect, which is discovered by the machine instruction verifier.

My understanding of the reason behind this is that the data structure used by rdf::Liveness:computeLiveIns() has a shortcoming: RegisterAggr stores register units rather than registers, so loosing information is inevitable. Since R0 and R1 share the same register unit, RDF picks the first register that corresponds to the stored register unit.

Unfortunately, I cannot share the test case since it is written for our out-of-tree backend; however, the bug should be relevant for each backend that makes use of RDF.

last5bits avatar Dec 11 '18 15:12 last5bits

Does it seem like a machine verifier bug to you?

last5bits avatar Dec 14 '18 16:12 last5bits

Good to know that optimizations should be aware of that, thanks.

From what I see in the machine verifier code, there is a piece that checks whether any subregisters of R1 are declared live-in; if they are, then no error is reported. However, in our case, R0 is not a subregister of R1, R0 is R1, i.e., they alias completely.

"Using an undefined physical register" https://github.com/llvm-mirror/llvm/blob/master/lib/CodeGen/MachineVerifier.cpp#L1608

last5bits avatar Dec 14 '18 16:12 last5bits

There have been bugs in LLVM where an optimization did not check for aliased registers, even outside of the RDF context, so that particular issue is really something that all codegen optimizations should be aware of. I agree that it may be confusing to someone reading the IR dumps, ideally we'd just list live register units instead.

What concerns me most, though, is the failure in the machine verifier. I'm not sure why it would consider this a problem, especially knowing that even if a part of a register is live, the whole register is treated as live. I'd like to understand exactly what happens in there that leads to the error.

kparzysz-quic avatar Dec 14 '18 14:12 kparzysz-quic

I see your point. However, the current behaviour of choosing some register for a register unit is clearly disliked by the machine instruction verifier, at the very least.

At its worst, some optimizations might rely on the fact that a given register is not live, while not checking that every live register is not aliased with the given register:

BB#0: %R1 = define ...

BB#1: Live Ins: %R0 // Great, I can use R1 to store some values since it's not live. … = use %R1 // Oh, wait

Although, I'm not sure that it is a fair thing for an optimization to do.

What are your thoughts on this, as well as on a potential solution?

last5bits avatar Dec 12 '18 21:12 last5bits

You are correct---there is no way to go from register units to the "desired" set of registers. The idea here rests on the fact that it shouldn't be necessary: indicating the liveness of register units (via some representative registers) should be enough. It may look strange to the person looking at the dump, but it should be functionally correct. Register aliasing is a source of complications of various sorts, in particular reflecting partial register updates using only defined registers is a problem. This was in part addressed through lane masks, but the most accurate description is really through using register units. The downside is that units don't always map clearly/cleanly to the original registers.

kparzysz-quic avatar Dec 11 '18 22:12 kparzysz-quic

Thank you for your quick response, Krzysztof. Correct me if I'm wrong but I don't see the mechanism by which RDF can currently go from a register unit to the correct physical register, given that two registers alias.

Since R0 and R1 alias, there is just one register unit, that gets marked as live-in for a basic block during rdf::Liveness:computeLiveIns(). However, live-ins of an MBB should contain actual physical registers, not register units, thus RDF converts a unit to the corresponding register (inside PhysicalRegisterInfo::getRefForUnit()). But in this case, there is one unit and two corresponding registers; so which register should it choose?

Here's the code in question (PhysicalRegisterInfo::PhysicalRegisterInfo()): https://github.com/llvm-mirror/llvm/blob/master/lib/Target/Hexagon/RDFRegisters.cpp#L58

The algorithm goes: are there other physical registers associated with this register unit? If so (R.isValid()), then map the register unit to the first physical register that we've found (R0), and ignore the other one (R1).

last5bits avatar Dec 11 '18 21:12 last5bits

Something doesn't sound right. If R0 and R1 overlap, defining R0 should be sufficient to make R1 live. Sharing register units is equivalent to being aliased, and a definition of an aliased register should suffice to make a given register live.

kparzysz-quic avatar Dec 11 '18 16:12 kparzysz-quic