qir-alliance / pyqir

PyQIR is a set of APIs for generating, parsing, and evaluating Quantum Intermediate Representation (QIR).
https://qir-alliance.github.io/pyqir
MIT License
57 stars 24 forks source link

Support for `void` with `QirRetTerminator` #112

Closed qci-amos closed 2 years ago

qci-amos commented 2 years ago

I've got a QIR instruction:

  ret void

But when I try to parse that I'm getting an exception

~/miniconda3/envs/aq/lib/python3.8/site-packages/pyqir/parser/_parser.py in operand(self)
    416         """
    417         if not hasattr(self, "_operand"):
--> 418             self._operand = QirOperand(self.term.ret_operand)
    419         return self._operand
    420 

~/miniconda3/envs/aq/lib/python3.8/site-packages/pyqir/parser/_parser.py in __new__(cls, op)
    233     """
    234     def __new__(cls, op: PyQirOperand):
--> 235         if op.is_local:
    236             return super().__new__(QirLocalOperand)
    237         elif op.is_constant:

AttributeError: 'NoneType' object has no attribute 'is_local'

I can work around this with a try/except:

            try:
                operand = ter.operand
            except AttributeError:
                operand = None
LaurentAjdnik commented 2 years ago

Hi @qci-amos and thanks for your feedback!

For further analysis that could help for a PR:

ret void is indeed a legit LLVM syntax: https://llvm.org/docs/LangRef.html#ret-instruction.

We should take into account that return_operand in llvm-ir::terminator::Ret can be "None if returning void": https://docs.rs/llvm-ir/0.8.0/llvm_ir/terminator/struct.Ret.html#structfield.return_operand.

This should be updated accordingly: