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 instructions should invalidate their string cache when new incomings are added #661

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

trbabb
Copy link
Contributor

@trbabb trbabb commented Dec 1, 2020

Stringifying a phi (including for debugging purposes) can cause them to freeze themselves in a stale state, and subsequently added incoming instructions will be incorrectly omitted from the instruction.

Any time a phi is mutated by adding an incoming expression, its string cache should be immediately invalidated.

@jceipek
Copy link

jceipek commented Dec 1, 2020

Here's a concrete example of where this could cause a problem:

print("before:", entry_phi) # before: %".7" = phi i64 [5, %"entry"]
entry_phi.add_incoming(v, body_blk) # This isn't represented in the final output because the line above populates the cache
print("after:", entry_phi) # after: %".7" = phi i64 [5, %"entry"]

Leads to this compile error:

compile error
Traceback (most recent call last):
  File "./codexec", line 169, in handle_object_code
    self.module.verify()
  File "/opt/venv/lib/python3.8/site-packages/llvmlite/binding/module.py", line 115, in verify
    raise RuntimeError(str(outmsg))
RuntimeError: PHINode should have one entry for each predecessor of its parent basic block!
  %.7 = phi i64 [ 5, %entry ]

Workaround:

print("before:", entry_phi) # before: %".7" = phi i64 [5, %"entry"]
entry_phi.add_incoming(v, body_blk)
entry_phi._clear_string_cache() # Clearing the cache manually ensures the above line is represented in the output
print("after:", entry_phi) # after: %".7" = phi i64 [5, %"entry"], [%".11", %"loop_body_b85hSr3tU4j6E/Vr"]

@esc
Copy link
Member

esc commented Jan 7, 2021

@trbabb thank you for submitting this. I have added it to the queue for review.

@esc esc added this to the Version 0.37.0 RC milestone Mar 22, 2021
@trbabb
Copy link
Contributor Author

trbabb commented Aug 21, 2021

Any chance this and #710 could make it into the next release? It'd really help clean up some workarounds! Thanks!

@esc esc removed this from the Version 0.38.0 RC milestone Nov 22, 2021
@gmarkall gmarkall added this to the 0.39.0 RC milestone Nov 24, 2021
Copy link
Member

@gmarkall gmarkall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@trbabb Many thanks for the PR, and apologies for the delay in its review. I'm spending some time going through the llvmlite PR backlog so I'm hoping to help move this forward now.

The changes seem like a good idea in general - as a general question (not necessarily needing to be addressed in this PR) are there other instructions that should invalidate their string cache when modified?

Could you please add a test of the modified functionality? (Perhaps based on @jceipek's example above).

@sklam sklam modified the milestones: 0.39.0 RC, PR Backlog Jun 1, 2022
@esc esc removed this from the PR Backlog milestone Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants