-
Notifications
You must be signed in to change notification settings - Fork 200
Add support for SourceLocExpr #1929
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,6 +79,7 @@ namespace utils { | |
| DECLARE_CLONE_FN(CXXCatchStmt) | ||
| DECLARE_CLONE_FN(CXXTryStmt) | ||
| DECLARE_CLONE_FN(PredefinedExpr) | ||
| DECLARE_CLONE_FN(SourceLocExpr) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "clang::SourceLocExpr" is directly included [misc-include-cleaner] DECLARE_CLONE_FN(SourceLocExpr)
^ |
||
| DECLARE_CLONE_FN(CharacterLiteral) | ||
| DECLARE_CLONE_FN(FloatingLiteral) | ||
| DECLARE_CLONE_FN(ImaginaryLiteral) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -581,7 +581,8 @@ StmtDiff BaseForwardModeVisitor::VisitConditionalOperator( | |||||||
| .ActOnConditionalOp(noLoc, noLoc, cond, ifTrueDiff.getExpr(), | ||||||||
| ifFalseDiff.getExpr()) | ||||||||
| .get(); | ||||||||
|
|
||||||||
| if (condExpr->getType()->isVoidType()) | ||||||||
| return StmtDiff(condExpr, nullptr); | ||||||||
| // cond is already used by the value conditional above; clone it for the | ||||||||
| // derivative conditional so the two do not share the stored condition. | ||||||||
| Expr* condExprDiff = | ||||||||
|
|
@@ -1262,7 +1263,7 @@ StmtDiff BaseForwardModeVisitor::VisitCallExpr(const CallExpr* CE) { | |||||||
| if (!isa<CXXOperatorCallExpr>(CE) && !isa<CXXMemberCallExpr>(CE) && | ||||||||
| !needsForwPass) { | ||||||||
| bool allArgsHaveZeroDerivatives = true; | ||||||||
| for (unsigned i = 0, e = CE->getNumArgs(); i < e; ++i) { | ||||||||
| for (unsigned i = 0, e = diffArgs.size(); i < e; ++i) { | ||||||||
| Expr* dArg = diffArgs[i]; | ||||||||
| // If argDiff.expr_dx is nullptr or is a constant 0, then the derivative | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: use range-based for loop instead [modernize-loop-convert]
Suggested change
|
||||||||
| // of the function call is 0. | ||||||||
|
|
@@ -1321,7 +1322,7 @@ StmtDiff BaseForwardModeVisitor::VisitCallExpr(const CallExpr* CE) { | |||||||
| // If clad failed to derive it, try finding its derivative using | ||||||||
| // numerical diff. | ||||||||
| if (!callDiff) { | ||||||||
| Multiplier = diffArgs[0]; | ||||||||
| Multiplier = diffArgs.empty() ? nullptr : diffArgs[0]; | ||||||||
| Expr* call = | ||||||||
| m_Sema | ||||||||
| .ActOnCallExpr(getCurrentScope(), Clone(CE->getCallee()), validLoc, | ||||||||
|
|
@@ -1533,7 +1534,7 @@ BaseForwardModeVisitor::VisitBinaryOperator(const BinaryOperator* BinOp) { | |||||||
| } else if (opCode == BO_Comma) { | ||||||||
| // if expression is (E1, E2) then derivative is (E1', E1, E2') | ||||||||
| // because E1 may change some variables that E2 depends on. | ||||||||
| if (!isUnusedResult(Ldiff.getExpr_dx())) { | ||||||||
| if (Ldiff.getExpr_dx() && !isUnusedResult(Ldiff.getExpr_dx())) { | ||||||||
| opDiff = BuildOp(BO_Comma, BuildParens(Ldiff.getExpr_dx()), | ||||||||
| BuildParens(Ldiff.getExpr())); | ||||||||
| opDiff = BuildOp(BO_Comma, BuildParens(opDiff), | ||||||||
|
|
@@ -1874,6 +1875,13 @@ StmtDiff BaseForwardModeVisitor::VisitStringLiteral(const StringLiteral* SL) { | |||||||
| SL->getType(), utils::GetValidSLoc(m_Sema))); | ||||||||
| } | ||||||||
|
|
||||||||
| StmtDiff | ||||||||
| BaseForwardModeVisitor::VisitSourceLocExpr(const clang::SourceLocExpr* E) { | ||||||||
| auto* Constant0 = | ||||||||
| ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, /*val=*/0); | ||||||||
| return StmtDiff(CloneNode(E), Constant0); | ||||||||
| } | ||||||||
|
|
||||||||
| StmtDiff BaseForwardModeVisitor::VisitWhileStmt(const WhileStmt* WS) { | ||||||||
| // Scope for the whole while loop. | ||||||||
| ScopeRAII whileScope(*this, Scope::ContinueScope | Scope::BreakScope | | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,16 @@ Stmt* StmtClone::VisitPredefinedExpr(PredefinedExpr* Node) { | |
| clad_compat::ExprSetDeps(result, Node); | ||
| return result; | ||
| } | ||
| #if CLANG_VERSION_MAJOR >= 15 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: no header providing "CLANG_VERSION_MAJOR" is directly included [misc-include-cleaner] lib/Differentiator/StmtClone.cpp:17: + #include <clang/Basic/Version.h> |
||
| DEFINE_CLONE_EXPR(SourceLocExpr, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: initializing non-owner 'SourceLocExpr *' with a newly created 'gsl::owner<>' [cppcoreguidelines-owning-memory] DEFINE_CLONE_EXPR(SourceLocExpr,
^Additional contextlib/Differentiator/StmtClone.cpp:38: expanded from macro 'DEFINE_CLONE_EXPR' CLASS* result = new (Ctx) CLASS CTORARGS; \
^ |
||
| (Ctx, Node->getIdentKind(), CloneType(Node->getType()), | ||
| Node->getBeginLoc(), Node->getEndLoc(), | ||
| Node->getParentContext())) | ||
| #else | ||
| DEFINE_CLONE_EXPR(SourceLocExpr, | ||
| (Ctx, Node->getIdentKind(), Node->getBeginLoc(), | ||
| Node->getEndLoc(), Node->getParentContext())) | ||
| #endif | ||
| DEFINE_CLONE_EXPR(CharacterLiteral, | ||
| (Node->getValue(), Node->getKind(), | ||
| CloneType(Node->getType()), Node->getLocation())) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // RUN: %cladclang -std=c++17 -I%S/../../include %s -o %t | ||
| // RUN: %t | %filecheck_exec %s | ||
|
|
||
| #include <cassert> | ||
| #include "clad/Differentiator/Differentiator.h" | ||
|
|
||
| double func(double x){ | ||
| assert(x>0.0); | ||
| return x*x*x; | ||
| } | ||
| double func1(double x) { | ||
| const char* file = __builtin_FILE(); | ||
| int line = __builtin_LINE(); | ||
| return x*x*x; | ||
| } | ||
|
|
||
| void temp() {} | ||
| double temp_func(double x) { | ||
| x > 0.0 ? temp() : temp(); | ||
| return x * x * x; | ||
| } | ||
|
|
||
| int main(){ | ||
| auto d_func=clad::gradient(func); | ||
| double dx=0; | ||
| d_func.execute(3.0,&dx); | ||
| printf("Diff result: %.2f\n", dx); | ||
| //CHECK-EXEC: Diff result: 27.00 | ||
|
|
||
| auto d_func_forw=clad::differentiate(func); | ||
| auto res_forw=d_func_forw.execute(3.0); | ||
| printf("Diff result: %.2f\n", res_forw); | ||
| // CHECK-EXEC: Diff result: 27.00 | ||
|
|
||
| auto d_func1=clad::gradient(func1); | ||
| double dx1=0; | ||
| d_func1.execute(3.0,&dx1); | ||
| printf("Diff result: %.2f\n", dx1); | ||
| //CHECK-EXEC: Diff result: 27.00 | ||
|
|
||
| auto d_func1_forw = clad::differentiate(func1); | ||
| auto res1_forw = d_func1_forw.execute(3.0); | ||
| printf("Diff result: %.2f\n", res1_forw); | ||
| //CHECK-EXEC: Diff result: 27.00 | ||
|
|
||
| auto d_temp_func = clad::differentiate(temp_func); | ||
| auto res_void = d_temp_func.execute(3.0); | ||
| printf("Diff result: %.2f\n", res_void); | ||
| //CHECK-EXEC: Diff result: 27.00 | ||
|
|
||
| return 0; | ||
| } |
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: no header providing "clang::SourceLocExpr" is directly included [misc-include-cleaner]