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

[c++17] changes to cudaq headers for pure c++17 environment #1468

Merged
merged 2 commits into from
Apr 11, 2024

Conversation

schweitzpgi
Copy link
Collaborator

@schweitzpgi schweitzpgi commented Apr 2, 2024

The driver and headers were allowing all C++20 syntax to pass, even in C++17 mode. This was happening by leaning into the idea that a C++20 compiler was available, it was just being told to compile as C++17. This meant that in a pure C++17 environment, which didn't know anything about C++20 whatsoever, would choke on the CUDA-Q syntax.

This PR disables the use of the offending syntax in the headers unless a C++20 (or better) compiler is truly in use. This distinction means that CUDA-Q programs written for C++17 may be required to use a different syntax in order to compile.

For example, the following C++20 syntax

  // C++20 only!
  void qernel(cudaq::qubit &c, cudaq::qubit &q) __qpu__ {
    x<cudaq::ctrl>(c, q);
  }

must be written as

  // C++17 or C++20
  void qernel(cudaq::qubit &c, cudaq::qubit &q) __qpu__ {
    cx(c, q);
  }

github-actions bot pushed a commit that referenced this pull request Apr 2, 2024
github-actions bot pushed a commit that referenced this pull request Apr 3, 2024
github-actions bot pushed a commit that referenced this pull request Apr 3, 2024
@1tnguyen
Copy link
Collaborator

1tnguyen commented Apr 4, 2024

LGTM.

I also want to ask @bmhowe23's opinion regarding breaking/non-breaking classification of the syntax change from x<ctrl>(a,b,c,d) to cx(a,b,c,d), etc. when compiling with -std=c++17.

@bettinaheim bettinaheim added the breaking change Change breaks backwards compatibility label Apr 4, 2024
@bettinaheim
Copy link
Collaborator

@schweitzpgi What's the issue with the reasoning that the nvq++ compiler is necessarily used to compile the code and nvq++ contains C++20 support? Is there a scenario where the code with nvq++ --std=c++17 would not compile or run when only a C++17 runtime is available?

If we pull in this change, we should remove the -Wno-c++20-extensions flag here and here.

@amccaskey
Copy link
Collaborator

LGTM.

I also want to ask @bmhowe23's opinion regarding breaking/non-breaking classification of the syntax change from x<ctrl>(a,b,c,d) to cx(a,b,c,d), etc. when compiling with -std=c++17.

traditionally, cOP has meant the singly-controlled version of OP. Then we'd see Toffoli gates (doubly-controlled) expressed as ccx. This change departs from this and allows the full OP<ctrl>(...) functionality for multi-controlled operations. I do not think we should do this since it departs from a norm. If a C++17 user wants a general multi-control operations on more than 1 control, they should use cudaq::control(...) on a kernel calling the OP.

@bmhowe23
Copy link
Collaborator

bmhowe23 commented Apr 4, 2024

@schweitzpgi What's the issue with the reasoning that the nvq++ compiler is necessarily used to compile the code and nvq++ contains C++20 support? Is there a scenario where the code with nvq++ --std=c++17 would not compile or run when only a C++17 runtime is available?

I assume the requirement that Eric is trying to satisfy is that cudaq.h, which includes runtime/cudaq/qis/qubit_qis.h, should be includable by any C++17 compiler (e.g. nvcc). However, I think this is already achievable.

For example, this file:

#include "cudaq.h"
int test() {
    return 0;
}

is compilable with all 3 of these commands.

$ nvcc -c -I/usr/local/cudaq/include -std=c++17 bentest.cpp -o bentest.o
$ g++ -c -I/usr/local/cudaq/include -std=c++17 bentest.cpp -o bentest.o
$ /opt/llvm/bin/clang++ -c -I/usr/local/cudaq/include -std=c++17 bentest.cpp -o bentest.o

Is the requirement that our full language is callable from C++17 code, or just that the standard cudaq.h header files cannot interfere with a C++17-only compiler?

@schweitzpgi
Copy link
Collaborator Author

@schweitzpgi What's the issue with the reasoning that the nvq++ compiler is necessarily used to compile the code and nvq++ contains C++20 support?

The bug in the current setup is that we simply allow code that is not C++17 to compile without so much as a warning as C++17. That can only happen if the compiler already knows about C++20. This is clearly problematic. For one thing, it lets us assume we can write tests and other code which is incorrect and no amount of testing will catch the problems.

In library mode, it should not the case that we force users down specific usage paths because we can't be consistent and do the right thing.

@schweitzpgi
Copy link
Collaborator Author

LGTM.
I also want to ask @bmhowe23's opinion regarding breaking/non-breaking classification of the syntax change from x<ctrl>(a,b,c,d) to cx(a,b,c,d), etc. when compiling with -std=c++17.

traditionally, cOP has meant the singly-controlled version of OP. Then we'd see Toffoli gates (doubly-controlled) expressed as ccx. This change departs from this and allows the full OP<ctrl>(...) functionality for multi-controlled operations. I do not think we should do this since it departs from a norm. If a C++17 user wants a general multi-control operations on more than 1 control, they should use cudaq::control(...) on a kernel calling the OP.

Yes, this was a bit of a backdoor extension. I can remove it. Thanks for the feedback.

@schweitzpgi
Copy link
Collaborator Author

If we pull in this change, we should remove the -Wno-c++20-extensions flag here and here.

Taken care of in c16e5ff

github-actions bot pushed a commit that referenced this pull request Apr 4, 2024
github-actions bot pushed a commit that referenced this pull request Apr 4, 2024
@schweitzpgi schweitzpgi enabled auto-merge (squash) April 4, 2024 22:55
github-actions bot pushed a commit that referenced this pull request Apr 4, 2024
@schweitzpgi
Copy link
Collaborator Author

@schweitzpgi What's the issue with the reasoning that the nvq++ compiler is necessarily used to compile the code and nvq++ contains C++20 support? Is there a scenario where the code with nvq++ --std=c++17 would not compile or run when only a C++17 runtime is available?

I assume the requirement that Eric is trying to satisfy is that cudaq.h, which includes runtime/cudaq/qis/qubit_qis.h, should be includable by any C++17 compiler (e.g. nvcc). However, I think this is already achievable.

For example, this file:

#include "cudaq.h"
int test() {
    return 0;
}

is compilable with all 3 of these commands.

$ nvcc -c -I/usr/local/cudaq/include -std=c++17 bentest.cpp -o bentest.o
$ g++ -c -I/usr/local/cudaq/include -std=c++17 bentest.cpp -o bentest.o
$ /opt/llvm/bin/clang++ -c -I/usr/local/cudaq/include -std=c++17 bentest.cpp -o bentest.o

Is the requirement that our full language is callable from C++17 code, or just that the standard cudaq.h header files cannot interfere with a C++17-only compiler?

Yes, it should be true that the cuda.h file is (and should remain) legal C++17 and should compile with any C++17 compiler. That was the requirement: supporting C++17 only systems. We don't want to silently regress from this point, which is possible because we are disabling our compiler checks currently as a workaround for an omission.

Thanks for verifying.

@bmhowe23
Copy link
Collaborator

bmhowe23 commented Apr 5, 2024

@schweitzpgi What's the issue with the reasoning that the nvq++ compiler is necessarily used to compile the code and nvq++ contains C++20 support? Is there a scenario where the code with nvq++ --std=c++17 would not compile or run when only a C++17 runtime is available?

I assume the requirement that Eric is trying to satisfy is that cudaq.h, which includes runtime/cudaq/qis/qubit_qis.h, should be includable by any C++17 compiler (e.g. nvcc). However, I think this is already achievable.
For example, this file:

#include "cudaq.h"
int test() {
    return 0;
}

is compilable with all 3 of these commands.

$ nvcc -c -I/usr/local/cudaq/include -std=c++17 bentest.cpp -o bentest.o
$ g++ -c -I/usr/local/cudaq/include -std=c++17 bentest.cpp -o bentest.o
$ /opt/llvm/bin/clang++ -c -I/usr/local/cudaq/include -std=c++17 bentest.cpp -o bentest.o

Is the requirement that our full language is callable from C++17 code, or just that the standard cudaq.h header files cannot interfere with a C++17-only compiler?

Yes, it should be true that the cuda.h file is (and should remain) legal C++17 and should compile with any C++17 compiler. That was the requirement: supporting C++17 only systems. We don't want to silently regress from this point, which is possible because we are disabling our compiler checks currently as a workaround for an omission.

Thanks for verifying.

To be clear, the above test file and test compile commands worked for me without any of the changes in this PR. Were you seeing failures before this PR with a specific compiler, or was this issue found by inspection only?

@schweitzpgi
Copy link
Collaborator Author

To be clear, the above test file and test compile commands worked for me without any of the changes in this PR. Were you seeing failures before this PR with a specific compiler, or was this issue found by inspection only?

This was found by inspection. The current setup accepts C++20 programs even when adding the C++17 flag, which is a problem.

github-actions bot pushed a commit that referenced this pull request Apr 8, 2024
github-actions bot pushed a commit that referenced this pull request Apr 9, 2024
github-actions bot pushed a commit that referenced this pull request Apr 11, 2024
@schweitzpgi schweitzpgi force-pushed the ch-disable.template.form branch 3 times, most recently from c2a64bd to ac01a72 Compare April 11, 2024 17:31
github-actions bot pushed a commit that referenced this pull request Apr 11, 2024
We were allowing calls such as
  x<cudaq::ctrl>(q);
in C++17 compilation mode even though that syntax requires C++20. This
PR prevents using the C++20 syntax when compiling as C++17. C++17 code
will have to rewrite the above as
  cx(q);
More generally, the cudaq::control and cudaq::adjoint functions can be
used.

Change the definition of my to be C++17.

Refactor the definition of swap to be C++17. Adds a "cswap" for the
control variant.

Add test.

Add handling of cr[xyz1] to the bridge.

Port the conditional_sample.cpp test to C++17.

Mark tests that use C++20 syntax as REQUIRES c++20.

Port negation.cpp test to have a c++17 variant.

Review comments: add back deleted RUN lines.

Cleanup the cr? functions. Remove unneeded templates.
github-actions bot pushed a commit that referenced this pull request Apr 11, 2024
Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

@NVIDIA NVIDIA deleted a comment from github-actions bot Apr 11, 2024
@NVIDIA NVIDIA deleted a comment from github-actions bot Apr 11, 2024
@NVIDIA NVIDIA deleted a comment from github-actions bot Apr 11, 2024
@NVIDIA NVIDIA deleted a comment from github-actions bot Apr 11, 2024
@NVIDIA NVIDIA deleted a comment from github-actions bot Apr 11, 2024
@NVIDIA NVIDIA deleted a comment from github-actions bot Apr 11, 2024
@NVIDIA NVIDIA deleted a comment from github-actions bot Apr 11, 2024
@NVIDIA NVIDIA deleted a comment from github-actions bot Apr 11, 2024
@NVIDIA NVIDIA deleted a comment from github-actions bot Apr 11, 2024
@NVIDIA NVIDIA deleted a comment from github-actions bot Apr 11, 2024
@NVIDIA NVIDIA deleted a comment from github-actions bot Apr 11, 2024
@schweitzpgi schweitzpgi merged commit bfb7cb5 into NVIDIA:main Apr 11, 2024
125 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2024
@schweitzpgi schweitzpgi deleted the ch-disable.template.form branch April 12, 2024 14:57
@bettinaheim bettinaheim added this to the release 0.8.0 milestone Apr 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking change Change breaks backwards compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants