Skip to content

Add new ideas for IR#62

Draft
Doomsk wants to merge 2 commits intomainfrom
dev/python_impl/min_lang
Draft

Add new ideas for IR#62
Doomsk wants to merge 2 commits intomainfrom
dev/python_impl/min_lang

Conversation

@Doomsk
Copy link
Copy Markdown
Member

@Doomsk Doomsk commented Jun 22, 2025

Previous IR structure and logic were lacking essential working pieces for most of the non-trivial simple instructions. Now it should work to represent all the possible expressions, such as declare, assign, call, call with body, any combination of the previous ones and more.

work in progress.

@q-inho we discuss about this PR later. You can check the code at the new IR file.

I also placed a test so we can check how it looks like and have proper ideas, comments, suggestions, critiques, etc. The test is basically as follows:

def test_dac1() -> None:
qq = Symbol("@q")
q1 = CoreLiteral("@1", lit_type="@int")
qu3 = Symbol("@u3")
qredim = Symbol("@redim")
i1 = IRDeclare(var=qq, var_type=qu3)
assert i1.instr == IRFlag.DECLARE
assert i1.args == (qq, qu3)
i2 = IRAssign(var=qq, value=q1)
assert i2.instr == IRFlag.ASSIGN
assert i2.args == (qq, q1)
i3 = IRCall(caller=qredim, args=IRArgs(qq))
i3_args_block_name = IRBlock(IRArgs(qq)).name
assert i3.instr == IRFlag.CALL
assert i3.args == (qredim, i3_args_block_name)
block1 = IRBlock(i1, i2, i3)
assert block1.instrs == (i1, i2, i3)
ir1 = IR()
ir1.add_new_block(block1)
assert block1.name in ir1.table
assert ir1.table[block1.name] == block1
print()
print(ir1)

Doomsk added 2 commits June 13, 2025 15:33
Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
Signed-off-by: Doomsk <eduardo.maschio@theqube.io>
@Doomsk Doomsk self-assigned this Jun 22, 2025
@q-inho
Copy link
Copy Markdown
Member

q-inho commented Jul 8, 2025

@Doomsk Thanks for the new IR design you mentioned. It clarifies how to implement the upcoming functions for my side as well. Maybe we could also add test covering CALL_WITH_BODY and CALL_WITH_OPTION to ensure those features work as expected?
Additionally, do you have a further roadmap or todo lists for this new IR?

Copy link
Copy Markdown
Member

@q-inho q-inho left a comment

Choose a reason for hiding this comment

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

I noticed a few spots that appeared in mind while reading the file, so I've left some comment and question.

args: IRArgs,
*options: tuple[IROption, ...]
):
super().__init__(caller, args, *options, name=IRFlag.CALL_WITH_BODY)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe name=IRFlag.CALL_WITH_BODY should be name=IRFlag.CALL_WITH_OPTION?

name: str

def __init__(self, *instrs: Any):
self.name = str(hash(instrs))
Copy link
Copy Markdown
Member

@q-inho q-inho Jul 8, 2025

Choose a reason for hiding this comment

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

Just question. I think Python hash output varies between interpreter sessions, so the same code may produce different block names on each run. Could a stable naming scheme make comparing IR instances and serialization the IR easier?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I thought of using uuid.uuid5 at first, but seemed too much for the job. Now that you mentioned it, makes sense to replace to uuid5 indeed

@Doomsk
Copy link
Copy Markdown
Member Author

Doomsk commented Jul 8, 2025

Thanks @q-inho ! Indeed, there are things to improve. While I was playing with it, I realized "why not trying to write from source code to IR directly?"
If you are available, we can discuss it further, both the IR roadmap and ways to get it clearer and leaner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants