Open msoeken opened 3 years ago
@msoeken Do you have an example for what unreachable code it was happy with (since the numbering seems valid to me)? Though the cleanest fix is probably to just trim the unreachable code before generating QIR.
@bettinaheim When I tried to create an example, the following worked well:
@EntryPoint()
operation RunProgram() : Unit {
Message("Hello");
return ();
Message("World");
}
Ok, This one wouldn't have the numbering then. I am not sure why clang is unhappy with it, but I'll put it on the backlog that we trim unused code (I believe it is currently a warning, but no trimming is done).
I think that's a good solution. I was just pointing out that the problem is not due to unreachable code in LLVM, but due to the numbering. First I thought that a ret
block might be followed by a label, but that's not the case.
I am not sure why clang is unhappy with it.
I believe this is because the ret
immediately before is terminal, and so there's a new basic block starting after, which is implicitly assigned temporary %1
. So the next assignment needs to use temporary %2
since this numbering is shared.
Reference: https://llvm.org/docs/LangRef.html#identifiers
Unnamed temporaries are numbered sequentially (using a per-function incrementing counter, starting with 0). Note that basic blocks and unnamed function parameters are included in this numbering.
@cesarzc FYI, I confirmed that this problem does still repro on the latest QDK, and I believe the changes in https://github.com/microsoft/qsharp-compiler/pull/941 would be correct for addressing it. However, that PR was rather out of date and I closed it so a fresh one on latest main could be built up. If you are planning on addressing this still, please coordinate with @kevinhartman to ensure you don't end up duplicating any efforts. Thanks!
Sounds good, @swernli! I'll leave that branch around in case it's helpful. These days I work on Qiskit Terra full-time so I'm less inclined to contribute to quantum projects in my leisure. Though please do feel free to tag me if any questions arise!
Describe the bug
Invalid LLVM code is generated in a Q# example with unreachable code.
To Reproduce
Run the following project:
Then run
clang++ -c qir/project.ll
, which yields:Generated LLVM
System information
Additional context
This is not an issue because of unreachable Q# code, but due to the instruction
%1
. Other examples with unreachable code worked well.