-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the generation of invalid code in some common cases
This commit fixes the way Clad generates code. Specifically, it addresses the way operators appear in the generated code in the reverse mode and the way nested name qualifiers are built in both modes. Fixes: #1050 Fixes: #1087
- Loading branch information
Showing
6 changed files
with
171 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// XFAIL: asserts | ||
// RUN: %cladclang -std=c++14 %s -I%S/../../include -oValidCodeGen.out 2>&1 | %filecheck %s | ||
// RUN: ./ValidCodeGen.out | %filecheck_exec %s | ||
// RUN: %cladclang -std=c++14 -Xclang -plugin-arg-clad -Xclang -enable-tbr %s -I%S/../../include -oValidCodeGenWithTBR.out | ||
// RUN: ./ValidCodeGenWithTBR.out | %filecheck_exec %s | ||
// CHECK-NOT: {{.*error|warning|note:.*}} | ||
|
||
#include "clad/Differentiator/Differentiator.h" | ||
#include "clad/Differentiator/STLBuiltins.h" | ||
#include "../TestUtils.h" | ||
#include "../PrintOverloads.h" | ||
|
||
namespace TN { | ||
struct Test { | ||
static int multiplier; | ||
}; | ||
int Test::multiplier = 3; | ||
|
||
template <typename T> | ||
struct Test2 { | ||
T operator[](T x) { | ||
return 4*x; | ||
} | ||
}; | ||
} | ||
|
||
namespace clad { | ||
namespace custom_derivatives { | ||
namespace class_functions { | ||
template <typename T> | ||
void operator_subscript_pullback(::TN::Test2<T>* obj, T x, T d_u, ::TN::Test2<T>* d_obj, T* d_x) { | ||
(*d_x) += 4*d_u; | ||
} | ||
}}} | ||
|
||
double fn(double x) { | ||
// fwd and rvs mode test | ||
return x*TN::Test::multiplier; // in this test, it's important that this nested name is copied into the generated code properly in both modes | ||
} | ||
|
||
double fn2(double x, double y) { | ||
// rvs mode test | ||
TN::Test2<double> t; | ||
auto q = t[x]; // in this test, it's important that this operator call is copied into the generated code properly and that the pullback function is called with all the needed namespace prefixes | ||
return q; | ||
} | ||
|
||
int main() { | ||
double dx, dy; | ||
INIT_DIFFERENTIATE(fn, "x"); | ||
INIT_GRADIENT(fn); | ||
INIT_GRADIENT(fn2); | ||
|
||
TEST_GRADIENT(fn, /*numOfDerivativeArgs=*/1, 3, &dx); // CHECK-EXEC: {3.00} | ||
TEST_GRADIENT(fn2, /*numOfDerivativeArgs=*/2, 3, 4, &dx, &dy); // CHECK-EXEC: {4.00, 0.00} | ||
TEST_DIFFERENTIATE(fn, 3) // CHECK-EXEC: {3.00} | ||
} | ||
|
||
//CHECK: double fn_darg0(double x) { | ||
//CHECK-NEXT: double _d_x = 1; | ||
//CHECK-NEXT: return _d_x * TN::Test::multiplier + x * 0; | ||
//CHECK-NEXT: } | ||
|
||
//CHECK: void fn_grad(double x, double *_d_x) { | ||
//CHECK-NEXT: *_d_x += 1 * TN::Test::multiplier; | ||
//CHECK-NEXT: } | ||
|
||
//CHECK: void fn2_grad(double x, double y, double *_d_x, double *_d_y) { | ||
//CHECK-NEXT: TN::Test2<double> _d_t({}); | ||
//CHECK-NEXT: TN::Test2<double> t; | ||
//CHECK-NEXT: TN::Test2<double> _t0 = t; | ||
//CHECK-NEXT: double _d_q = 0.; | ||
//CHECK-NEXT: double q = t.operator[](x); | ||
//CHECK-NEXT: _d_q += 1; | ||
//CHECK-NEXT: { | ||
//CHECK-NEXT: double _r0 = 0.; | ||
//CHECK-NEXT: clad::custom_derivatives::class_functions::operator_subscript_pullback(&_t0, x, _d_q, &_d_t, &_r0); | ||
//CHECK-NEXT: *_d_x += _r0; | ||
//CHECK-NEXT: } | ||
//CHECK-NEXT: } |