From 0a3f5ede95ff68aaa73437425d9787cbca253d58 Mon Sep 17 00:00:00 2001 From: Atell Krasnopolski Date: Thu, 18 Jul 2024 13:29:45 +0200 Subject: [PATCH] Prevent Clad from trying to create a void zero literal Previously, clad used to try to synthesise a void zero literal when differentiating a call to a void function with literal arguments in the forward mode. This caused it to crash. Fixes: #988 --- lib/Differentiator/BaseForwardModeVisitor.cpp | 13 ++++++++++--- test/FirstDerivative/FunctionCalls.C | 16 ++++++++++++++++ unittests/Kokkos/CMakeLists.txt | 8 +++++++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/Differentiator/BaseForwardModeVisitor.cpp b/lib/Differentiator/BaseForwardModeVisitor.cpp index 7853205a7..a551d1249 100644 --- a/lib/Differentiator/BaseForwardModeVisitor.cpp +++ b/lib/Differentiator/BaseForwardModeVisitor.cpp @@ -1219,9 +1219,16 @@ StmtDiff BaseForwardModeVisitor::VisitCallExpr(const CallExpr* CE) { validLoc, llvm::MutableArrayRef(CallArgs), validLoc) .get(); - auto* zero = ConstantFolder::synthesizeLiteral(CE->getType(), m_Context, - /*val=*/0); - return StmtDiff(call, zero); + if (!CE->getType().getTypePtr()->isVoidType()) { + auto* zero = + ConstantFolder::synthesizeLiteral(CE->getType(), m_Context, + /*val=*/0); + return StmtDiff(call, zero); + } else { + // No differentiation or zero generation for void functions with + // literal arguments. + return StmtDiff(call, nullptr); + } } } } diff --git a/test/FirstDerivative/FunctionCalls.C b/test/FirstDerivative/FunctionCalls.C index 33e15c2c6..2d2f0f651 100644 --- a/test/FirstDerivative/FunctionCalls.C +++ b/test/FirstDerivative/FunctionCalls.C @@ -183,6 +183,21 @@ double test_9(double x) { // CHECK-NEXT: return _t0.pushforward; // CHECK-NEXT: } +void some_important_void_func(double y) { + assert(y < 1); +} + +double test_10(double x) { + some_important_void_func(1); + return x; +} + +// CHECK: double test_10_darg0(double x) { +// CHECK-NEXT: double _d_x = 1; +// CHECK-NEXT: some_important_void_func(1); +// CHECK-NEXT: return _d_x; +// CHECK-NEXT: } + int main () { clad::differentiate(test_1, 0); clad::differentiate(test_2, 0); @@ -196,6 +211,7 @@ int main () { clad::differentiate(test_8); // expected-error {{Both enable and disable TBR options are specified.}} clad::differentiate(test_8); // expected-error {{Diagonal only option is only valid for Hessian mode.}} clad::differentiate(test_9); + clad::differentiate(test_10); return 0; // CHECK: void increment_pushforward(int &i, int &_d_i) { diff --git a/unittests/Kokkos/CMakeLists.txt b/unittests/Kokkos/CMakeLists.txt index bd89f86f5..d9a29ac73 100644 --- a/unittests/Kokkos/CMakeLists.txt +++ b/unittests/Kokkos/CMakeLists.txt @@ -14,5 +14,11 @@ if (NOT (LLVM_REQUIRES_RTTI OR LLVM_ENABLE_RTTI)) endif() endif() -target_link_libraries(KokkosTests PUBLIC ${Kokkos_LIBRARIES}) +# linking kokkos properly +if (TARGET Kokkos::kokkos) +target_link_libraries(KokkosTests PRIVATE Kokkos::kokkos) +else() target_include_directories(KokkosTests SYSTEM PRIVATE ${Kokkos_INCLUDE_DIRS}) +endif() +target_link_libraries(KokkosTests PUBLIC ${Kokkos_LIBRARIES}) +