Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phi Nodes and Register Allocation #12

Open
Lol3rrr opened this issue Apr 5, 2022 · 2 comments
Open

Phi Nodes and Register Allocation #12

Lol3rrr opened this issue Apr 5, 2022 · 2 comments
Labels
Backend Everything related to one or more Backends bug Something isn't working

Comments

@Lol3rrr
Copy link
Owner

Lol3rrr commented Apr 5, 2022

Problem

Currently Phi Nodes dont really work well with Register Allocation, because it gets "invalided" when destructuring the SSA Form of the Phi Nodes

Cause

This is caused by an oversight of the interference graph/register allocation.
Lets say we define x1 in b1 and x2 in b2 and now define x3 as Phi(x1, x2). With the current System this means that we assign Registers to x1, x2 and x3 independently of each other and the Register assigned to x3 might be used before x3 was assigned with the Phi Node. When we then destructure our SSA we essentially move the assignment of x3 up into b1 and b2, but because the Register for x3 was also used before we originally assigned x3 using the Phi we now overwrite its register in between.

Example

Original SSA:

b1:
  x1 = 13
  jump b3

b2:
  x2 = 23
  jump b3

b3:
  test1 = 123
  print(test1)
  x3 = Phi(x1, x2)
  print (x3)

Destructured

b1:
  x1 = 13
  x3 = x1
  jump b3

b2:
  x2 = 23
  x3 = x2
  jump b3

b3:
  test1 = 123 // Could be storing into the same Register that x3 was previously stored in and therefore corrupting the Data
  print (test1)
  print (x3)

Because test1 and x3 dont interfere in the original SSA they could very well be assigned to the same Register meaning that we then generate an invalid SSA after destructuring because the Register for x3 is overwritten, but we just use it again afterwards as if nothing happened

@Lol3rrr Lol3rrr added bug Something isn't working Backend Everything related to one or more Backends labels Apr 5, 2022
@Lol3rrr
Copy link
Owner Author

Lol3rrr commented Apr 5, 2022

Proposal

We should change the ways that we generate the interference Graph to correctly represent the destructured case. For this we should check if a variable is no longer used in a Block and only gets used once in a following Block and if that use case is as an Entry in a Phi-Node otherwise continue as normal. However if that is the Case, the current Variables liveness ends at the end of the current Block and we "raise" the start of the Variable defined by the Phi-Node to the end of the Block as well. This would basically mimic the way that the destructured SSA would look without actually changing the IR we are working with.

Problems

This would not deal with multiple Phi Nodes correctly, because we dont garantuee anything about the Order and therefore could still mess things up in that sense.
Its also unclear on how this could be implemented efficiently, but this would take a backseat as correctness is the Priority right now

@Lol3rrr
Copy link
Owner Author

Lol3rrr commented Apr 5, 2022

Temporary Solution

For a temporary Fix we could use the fact that variables of the same group are assigned to the same underlying Memory location and we could therefore store the Phi-Entries to the shared memory location at the end of their blocks and replace the Phi-Node with a simple read from the shared memory location. This should work correctly and generate the correct result.

Problem

This obviously comes with a performance penalty because we need to perform at least two more extra memory writes and one more memory read. Moreover this also isnt really a fix because we are now just working around this issue and avoiding it instead of properly fixing its root cause

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backend Everything related to one or more Backends bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant