Skip to content

Commit

Permalink
Take __real/__imag operators into account in TBR analysis and Get…
Browse files Browse the repository at this point in the history
…InnermostReturnExpr.
  • Loading branch information
PetroZarytskyi committed Nov 22, 2023
1 parent f11bf1e commit 058b279
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/Differentiator/CladUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,14 @@ namespace clad {
auto opCode = UnOp->getOpcode();
if (opCode == clang::UO_PreInc || opCode == clang::UO_PreDec)
Visit(UnOp->getSubExpr());
else if (opCode == UnaryOperatorKind::UO_Real ||
opCode == UnaryOperatorKind::UO_Imag) {
/// FIXME: Considering real/imaginary part atomic is
/// not always correct since the subexpression can
/// be more complex than just a DeclRefExpr.
/// (e.g. `__real (n++ ? z1 : z2)`)
m_Exprs.push_back(UnOp);
}
}

void VisitDeclRefExpr(clang::DeclRefExpr* DRE) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Differentiator/TBRAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,11 @@ bool TBRAnalyzer::VisitUnaryOperator(clang::UnaryOperator* UnOp) {
markLocation(innerExpr);
}
}
/// FIXME: Ideally, `__real` and `__imag` operators should be
/// treated as member expressions. However, it is not clear
/// where the FieldDecls of real and imaginary parts should be
/// deduced from (their names might be compiler-specific).
/// So for now we visit the whole subexpression.
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion test/Gradient/UserDefinedTypes.C
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ double fn6(dcomplex c, double i) {
}
// CHECK: void real_pullback({{.*}} [[__val:.*]], clad::array_ref<complex<double> > _d_this, clad::array_ref<{{.*}}> [[_d___val:[a-zA-Z_]*]]){{.*}} {
// CHECK-NEXT: double _t0;
// CHECK-NEXT: _t0 ={{(__real)?}} this->[[_M_value:.*]];
// CHECK-NEXT: _t0 ={{( __real)?}} this->[[_M_value:.*]];
// CHECK-NEXT: {{(__real)?}} this->[[_M_value:.*]] = [[__val]];
// CHECK-NEXT: {
// CHECK-NEXT: {{(__real)?}} this->[[_M_value:.*]] = _t0;
Expand Down

0 comments on commit 058b279

Please sign in to comment.