Skip to content

Commit

Permalink
Patch for the failure of functions with void return type and with ret…
Browse files Browse the repository at this point in the history
…urn statement

Prior to this patch, clad would throw an assertion error when attempting to differentiate void functions that contain a return statement.
The assertion error would indicate that there was an attempt to differentiate a nullptr.
This Patch checks if the there is an expression associsted with the null pointer and only if there is one, we differentiate it.

Added tests for the same

This patch fixes #432
  • Loading branch information
Nirhar authored and vgvassilev committed Jul 15, 2022
1 parent 85de6e4 commit 5505764
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/Differentiator/ForwardModeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,10 @@ namespace clad {
}

StmtDiff ForwardModeVisitor::VisitReturnStmt(const ReturnStmt* RS) {
//If there is no return value, we must not attempt to differentiate
if (!RS->getRetValue())
return nullptr;

StmtDiff retValDiff = Visit(RS->getRetValue());
Stmt* returnStmt = nullptr;
if (m_Mode == DiffMode::forward) {
Expand Down
23 changes: 23 additions & 0 deletions test/ForwardMode/MemberFunctions.C
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ public:
// CHECK-NEXT: return (_d_this->x + _d_this->y) * i + _t0 * _d_i + (_d_i * j + i * _d_j) * j + _t1 * _d_j;
// CHECK-NEXT: }

void mem_fn_with_void_return() {
return;
}

// CHECK: void mem_fn_with_void_return_pushforward(SimpleFunctions *_d_this) {
// CHECK-NEXT:}

double mem_fn_with_void_function_call(double i, double j) {
mem_fn_with_void_return();
return i*j;
}

// CHECK: double mem_fn_with_void_function_call_darg0(double i, double j) {
// CHECK-NEXT: double _d_i = 1;
// CHECK-NEXT: double _d_j = 0;
// CHECK-NEXT: SimpleFunctions _d_this_obj;
// CHECK-NEXT: SimpleFunctions *_d_this = &_d_this_obj;
// CHECK-NEXT: this->mem_fn_with_void_return_pushforward(_d_this);
// CHECK-NEXT: return _d_i * j + i * _d_j;
// CHECK-NEXT:}

double mem_fn_with_var_arg_list(double i, double j, ...) {
return (x+y)*i + i*j*j;
}
Expand Down Expand Up @@ -727,6 +748,8 @@ int main() {
TEST(mem_fn, 3, 5) // CHECK-EXEC: 30.00
// CHECK-EXEC: 33.00

TEST(mem_fn_with_void_function_call, 3, 5) //CHECK-EXEC: 5.00

TEST(mem_fn_with_var_arg_list, 3, 5) // CHECK-EXEC: 30.00
// CHECK-EXEC: 33.00

Expand Down

0 comments on commit 5505764

Please sign in to comment.