Skip to content

Commit

Permalink
Fix #1383: bridge not lowering to correct IR (#1402)
Browse files Browse the repository at this point in the history
The operator[] returns a reference, but the AST doesn't have a cast
node. So the value appearing in the conditional is not loaded.
This adds an explicit check and an error message if the load is
returning an unexpected value.

Fix #1383.
  • Loading branch information
schweitzpgi committed Mar 14, 2024
1 parent afe840b commit bb711f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/Frontend/nvqpp/ConvertExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,8 @@ bool QuakeBridgeVisitor::WalkUpFromCXXOperatorCallExpr(
bool QuakeBridgeVisitor::VisitCXXOperatorCallExpr(
clang::CXXOperatorCallExpr *x) {
auto loc = toLocation(x->getSourceRange());

// Helper to replace the operator[] function name with the value, v.
auto replaceTOSValue = [&](Value v) {
[[maybe_unused]] auto funcVal = popValue();
assert(funcVal.getDefiningOp<func::ConstantOp>());
Expand Down
13 changes: 13 additions & 0 deletions lib/Frontend/nvqpp/ConvertStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,19 @@ bool QuakeBridgeVisitor::TraverseIfStmt(clang::IfStmt *x,
// If there is no initialization expression, skip creating an `if` scope.
if (!TraverseStmt(cond))
return false;

// For something like an `operator[]` the TOS value (likely) is a
// pointer to the indexed element in a vector. Since there may not be a cast
// node in the AST to make that a RHS value, we must explicitly check here
// and add the required a load and cast.
if (auto ptrTy = dyn_cast<cc::PointerType>(peekValue().getType())) {
Value v = popValue();
pushValue(builder.create<cc::LoadOp>(loc, v));
if (ptrTy != builder.getI1Type()) {
reportClangError(x, mangler,
"expression in condition not yet supported");
}
}
if (x->getElse())
builder.create<cc::IfOp>(loc, TypeRange{}, popValue(),
stmtBuilder(x->getThen()),
Expand Down

0 comments on commit bb711f7

Please sign in to comment.