-
Notifications
You must be signed in to change notification settings - Fork 125
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
Fix the generation of invalid code in some common cases #1088
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,76 @@ | ||
// 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 { | ||
int coefficient = 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::coefficient; // 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; // this type needs to be copied into the derived code properly | ||
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::coefficient + x * 0; | ||
//CHECK-NEXT: } | ||
|
||
//CHECK: void fn_grad(double x, double *_d_x) { | ||
//CHECK-NEXT: *_d_x += 1 * TN::coefficient; | ||
//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: } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: do not use const_cast [cppcoreguidelines-pro-type-const-cast]