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

Better error messages for unsupported statements in kernels #808

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Frontend/nvqpp/ConvertExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,12 @@ bool QuakeBridgeVisitor::VisitCallExpr(clang::CallExpr *x) {
TODO_loc(loc, "unknown function, " + funcName + ", in cudaq namespace");
}

if (func->isVariadic()) {
reportClangError(x, mangler,
"cannot call variadic function from quantum kernel");
return false;
}

// If we get here, and the CallExpr takes qubits or qreg and it must be
// another kernel call.
auto mlirFuncTy = cast<FunctionType>(calleeOp.getType());
Expand Down
2 changes: 2 additions & 0 deletions lib/Frontend/nvqpp/ConvertStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ bool QuakeBridgeVisitor::TraverseCompoundStmt(clang::CompoundStmt *stmt,
}
llvm::dbgs() << "]]]\n";
});
} else {
reportClangError(cs, mangler, "statement not supported in qpu kernel");
}
}
return true;
Expand Down
6 changes: 5 additions & 1 deletion lib/Frontend/nvqpp/ConvertType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ bool QuakeBridgeVisitor::TraverseRecordType(clang::RecordType *t) {
if (!result)
return false;
if (typeStack.size() != typeStackDepth + 1) {
assert(typeStack.size() == typeStackDepth);
if (typeStack.size() != typeStackDepth) {
emitWarning(toLocation(recDecl),
"compiler encountered type traversal issue");
return false;
}
if (allowUnknownRecordType)
pushType(noneTy);
else {
Expand Down
8 changes: 6 additions & 2 deletions test/AST-error/inaccessible_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ struct Kernel {
int main() {
int i;
auto f = [&]() __qpu__ { // expected-remark{{An inaccessible symbol}}
cudaq::qreg q(i); // expected-error{{symbol is not accessible in this kernel}}
mz(q); // expected-error{{symbol is not accessible in this kernel}}
cudaq::qreg q(i);
// expected-error@-1 {{symbol is not accessible in this kernel}}
// expected-error@-2 {{statement not supported in qpu kernel}}
mz(q);
// expected-error@-1 {{symbol is not accessible in this kernel}}
// expected-error@-2 {{statement not supported in qpu kernel}}
};
Kernel{}(f);
return 0;
Expand Down
21 changes: 18 additions & 3 deletions test/AST-error/statements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
// RUN: cudaq-quake -verify %s

#include <cudaq.h>
#include <iostream>

void foo();
void bar();

struct S2 {
auto operator()() __qpu__ {
try { // expected-error{{try statement is not yet supported}}
try { // expected-error {{try statement is not yet supported}}
// expected-error@-1{{statement not supported in qpu kernel}}
foo();
} catch (int) {
bar();
Expand All @@ -25,15 +27,17 @@ struct S2 {

struct S4 {
auto operator()() __qpu__ {
goto label; // expected-error{{goto statement}}
goto label; // expected-error {{goto statement}}
// expected-error@-1{{statement not supported in qpu kernel}}
label:
foo();
}
};

struct S5 {
auto operator()(int sp) __qpu__ {
switch (sp) { // expected-error{{switch statement}}
switch (sp) { // expected-error {{switch statement}}
// expected-error@-1{{statement not supported in qpu kernel}}
case 1:
foo();
default:
Expand All @@ -42,3 +46,14 @@ struct S5 {
}
}
};

struct S6 {
auto operator()() __qpu__ {
// expected-error@+1{{statement not supported in qpu kernel}}
std::cout << "Hello\n";

// expected-error@+2{{cannot call variadic function from quantum kernel}}
// expected-error@+1{{statement not supported in qpu kernel}}
printf("Hello\n");
}
};
Loading