From efb09381f8dc3f8f62e8197e23cb5a0095589e08 Mon Sep 17 00:00:00 2001 From: Maxxxx Date: Thu, 23 May 2024 21:06:37 +0200 Subject: [PATCH] Return type adjusted to depend on called function's return type This commit changes the return type of 0 that is returned in case all arguments are constant literals. In particular it used to give warnings when differentiating NaN(or __builtin_nanf (const char * str )) which is crucial for the future fmax custom derivative implementation. Fixes: #908. --- lib/Differentiator/BaseForwardModeVisitor.cpp | 4 ++-- test/FirstDerivative/FunctionCallsWithResults.C | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Differentiator/BaseForwardModeVisitor.cpp b/lib/Differentiator/BaseForwardModeVisitor.cpp index 491a36683..5a3ed4394 100644 --- a/lib/Differentiator/BaseForwardModeVisitor.cpp +++ b/lib/Differentiator/BaseForwardModeVisitor.cpp @@ -1175,8 +1175,8 @@ StmtDiff BaseForwardModeVisitor::VisitCallExpr(const CallExpr* CE) { validLoc, llvm::MutableArrayRef(CallArgs), validLoc) .get(); - auto* zero = - ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, 0); + auto* zero = ConstantFolder::synthesizeLiteral(CE->getType(), m_Context, + /*val=*/0); return StmtDiff(call, zero); } } diff --git a/test/FirstDerivative/FunctionCallsWithResults.C b/test/FirstDerivative/FunctionCallsWithResults.C index 3ee69445b..54a3f521c 100644 --- a/test/FirstDerivative/FunctionCallsWithResults.C +++ b/test/FirstDerivative/FunctionCallsWithResults.C @@ -165,7 +165,7 @@ double fn4(double i, double j) { // CHECK: double fn4_darg0(double i, double j) { // CHECK-NEXT: double _d_i = 1; // CHECK-NEXT: double _d_j = 0; -// CHECK-NEXT: double _d_res = 0; +// CHECK-NEXT: double _d_res = 0.; // CHECK-NEXT: double res = nonRealParamFn(0, 0); // CHECK-NEXT: _d_res += _d_i; // CHECK-NEXT: res += i; @@ -266,7 +266,7 @@ double fn8(double i, double j) { // CHECK-NEXT: clad::ValueAndPushforward _t1 = check_and_return_pushforward(_t0.value, 'a', _t0.pushforward, 0); // CHECK-NEXT: double &_t2 = _t1.value; // CHECK-NEXT: double _t3 = std::tanh(1.); -// CHECK-NEXT: return _t1.pushforward * _t3 + _t2 * 0; +// CHECK-NEXT: return _t1.pushforward * _t3 + _t2 * 0.; // CHECK-NEXT: } double g (double x) { return x; }