clad icon indicating copy to clipboard operation
clad copied to clipboard

Tests for differentiation of constexpr and consteval functions

Open Nirhar opened this issue 2 years ago • 8 comments

I have added tests for constexpr functions for the ForwardMode, Gradient and Hessian Modes. It is not possible for us to have constexpr functions in the Jacobian mode as clad convention dictates that such a function be of void return type and we pass the return parameter as an argument to the function. Constexpr functions must have a literal return type.

Also consteval is a C++20 feature and is implemented in clang-14(according to this link here:Link). According to Clad's readme document, clad is supported only upto clang 12. So should I go ahead and implement tests for consteval? I am currently using clang-9 and I'm getting unknown keyword error with it.

cc @vgvassilev @davidlange6 @parth-07

closes #437

Nirhar avatar May 27 '22 16:05 Nirhar

Codecov Report

Merging #455 (a44984f) into master (6f98627) will not change coverage. The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #455   +/-   ##
=======================================
  Coverage   92.52%   92.52%           
=======================================
  Files          35       35           
  Lines        5363     5363           
=======================================
  Hits         4962     4962           
  Misses        401      401           

codecov[bot] avatar May 27 '22 16:05 codecov[bot]

Also consteval is a C++20 feature and is implemented in clang-14

You can define consteval functions by enabling C++20 features in earlier clang versions (>=9) using -std=c++2a or -std=c++20 flags (depending on the clang version).

parth-07 avatar Jun 02 '22 17:06 parth-07

Also consteval is a C++20 feature and is implemented in clang-14(according to this link here:Link). According to Clad's readme document, clad is supported only upto clang 12. So should I go ahead and implement tests for consteval? I am currently using clang-9 and I'm getting unknown keyword error with it.

We have updated clad and you can use clang-14.

vgvassilev avatar Jun 02 '22 18:06 vgvassilev

Is this updated version of clad in the master branch?

I tried to compile the first program for (forward mode) differentiating a function as mentioned in the Readme.md file for clad, while it compiles for clang-13 it throws the following error for clang-14: bool clad::plugin::CladPlugin::CheckBuiltins(): Assertion `&C.Idents == &m_CI.getPreprocessor().getIdentifierTable() && "Miscompiled?"' failed.

Here is the stack dump;

Stack dump:
0.	Program arguments: /usr/lib/llvm-14/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name trial.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -fcoverage-compilation-dir=/home/nirhar/Documents/Projects/GSoC/Workspace2 -resource-dir /usr/lib/llvm-14/lib/clang/14.0.5 -I ./inst/include/ -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/x86_64-linux-gnu/c++/9 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.5/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c++11 -fdeprecated-macro -fdebug-compilation-dir=/home/nirhar/Documents/Projects/GSoC/Workspace2 -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -load ./inst/lib/clad.so -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/trial-6f9bdf.o -x c++ trial.cpp
1.	/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/x86_64-linux-gnu/c++/9/bits/c++config.h:278:1: current parser token 'namespace'
 #0 0x00007f48f0a02041 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xe4b041)
 #1 0x00007f48f09ffd8e llvm::sys::RunSignalHandlers() (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xe48d8e)
 #2 0x00007f48f0a0257b (/lib/x86_64-linux-gnu/libLLVM-14.so.1+0xe4b57b)
 #3 0x00007f48f9c713c0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x143c0)
 #4 0x00007f48ef6ba03b raise /build/glibc-sMfBJT/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #5 0x00007f48ef699859 abort /build/glibc-sMfBJT/glibc-2.31/stdlib/abort.c:81:7
 #6 0x00007f48ef699729 get_sysdep_segment_value /build/glibc-sMfBJT/glibc-2.31/intl/loadmsgcat.c:509:8
 #7 0x00007f48ef699729 _nl_load_domain /build/glibc-sMfBJT/glibc-2.31/intl/loadmsgcat.c:970:34
 #8 0x00007f48ef6ab006 (/lib/x86_64-linux-gnu/libc.so.6+0x34006)
 #9 0x00007f48ed377030 clad::plugin::CladPlugin::CheckBuiltins() (./inst/lib/clad.so+0x118030)
#10 0x00007f48ed3761d5 clad::plugin::CladPlugin::HandleTopLevelDecl(clang::DeclGroupRef) (./inst/lib/clad.so+0x1171d5)
#11 0x00007f48f89ca84c clang::MultiplexConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x255484c)
#12 0x00007f48f6e7a268 clang::ParseAST(clang::Sema&, bool, bool) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0xa04268)
#13 0x00007f48f898ff57 clang::FrontendAction::Execute() (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x2519f57)
#14 0x00007f48f88e8c26 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x2472c26)
#15 0x00007f48f8a08f7b clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/lib/x86_64-linux-gnu/libclang-cpp.so.14+0x2592f7b)
#16 0x00000000004131b3 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm-14/bin/clang+0x4131b3)
#17 0x000000000041140c (/usr/lib/llvm-14/bin/clang+0x41140c)
#18 0x0000000000411257 main (/usr/lib/llvm-14/bin/clang+0x411257)
#19 0x00007f48ef69b0b3 __libc_start_main /build/glibc-sMfBJT/glibc-2.31/csu/../csu/libc-start.c:342:3
#20 0x000000000040e30e _start (/usr/lib/llvm-14/bin/clang+0x40e30e)
clang: error: unable to execute command: Aborted (core dumped)
clang: error: clang frontend command failed due to signal (use -v to see invocation)
Ubuntu clang version 14.0.5-++20220603124341+2f0a69c32a4c-1~exp1~20220603124352.149
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
clang: error: unable to execute command: Aborted (core dumped)
clang: note: diagnostic msg: Error generating preprocessed source(s).

Nirhar avatar Jun 05 '22 04:06 Nirhar

This message usually tells us that clad is miscompiled. That is, it loads different header files from the ones which were used to originally build the libraries.

vgvassilev avatar Jun 05 '22 14:06 vgvassilev

The following code snippet:

consteval double fn2(double x, double y, double z){
	return x*y*z;
}

int main(){
    auto fdx=clad::differentiate(fn2,"x");
    std::cout<<fdx.execute(3,4,5)<<std::endl;
    fdx.dump();
}

gives the error: error: cannot take address of consteval function 'fn2' outside of an immediate invocation

From my understanding, clad::differentiate must also be a consteval function for this to work. How do I solve this issue?

Nirhar avatar Jun 11 '22 10:06 Nirhar

The following code snippet:

consteval double fn2(double x, double y, double z){
	return x*y*z;
}

int main(){
    auto fdx=clad::differentiate(fn2,"x");
    std::cout<<fdx.execute(3,4,5)<<std::endl;
    fdx.dump();
}

gives the error: error: cannot take address of consteval function 'fn2' outside of an immediate invocation

From my understanding, clad::differentiate must also be a consteval function for this to work. How do I solve this issue?

@vgvassilev Should we leave tests for consteval functions for now?

If we can add support for consteval function calls, then we can add a test for the function fn.

consteval double sum(double a, double b) {
  return a + b;
}

double fn(double i, double j) {
  auto val = sum(7, 9);
  return val * i;
}

parth-07 avatar Jun 13 '22 06:06 parth-07

Yes, we can add tests for constexpr and then in a separate PR for consteval eventually.

vgvassilev avatar Jun 23 '22 18:06 vgvassilev