Skip to content
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

Program crash when calling a function with single array argument #386

Closed
parth-07 opened this issue Feb 23, 2022 · 0 comments · Fixed by #383
Closed

Program crash when calling a function with single array argument #386

parth-07 opened this issue Feb 23, 2022 · 0 comments · Fixed by #383

Comments

@parth-07
Copy link
Collaborator

parth-07 commented Feb 23, 2022

Reproducible example:

double modify(double* arr) {
    arr[0] = 5*arr[0] + arr[1];
    return 1;
}

double fn(double* arr) {
    double temp = modify(arr);
    return arr[0];
}

auto fn_grad = clad::gradient(fn);

Running this code with Clad results in a compile-time crash.

Stack Trace
Stack dump:
0.	Program arguments: /usr/lib/llvm-10/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name bug-rev-fn-call-array.cpp -mrelocation-model static -mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -I /home/parth/clubs/open-source/clad/pristine/builds/build-10/inst/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/x86_64-linux-gnu/c++/9 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/x86_64-linux-gnu/c++/9 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/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/parth/clubs/open-source/clad/tutorial -ferror-limit 19 -fmessage-length 0 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -add-plugin clad -load /home/parth/clubs/open-source/clad/pristine/builds/build-10/inst/lib/clad.so -plugin-arg-clad -fdump-derived-fn -faddrsig -o /tmp/bug-rev-fn-call-array-e55a8c.o -x c++ bug-rev-fn-call-array.cpp 
1.	<eof> parser at end of file
#0 0x00007f06970be4ff llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/lib/x86_64-linux-gnu/libLLVM-10.so.1+0x9814ff)
#1 0x00007f06970bc7b0 llvm::sys::RunSignalHandlers() (/lib/x86_64-linux-gnu/libLLVM-10.so.1+0x97f7b0)
#2 0x00007f06970beac5 (/lib/x86_64-linux-gnu/libLLVM-10.so.1+0x981ac5)
#3 0x00007f069d88e3c0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x153c0)
#4 0x00007f069b6e94f1 clang::ASTContext::getTypeInfoImpl(clang::Type const*) const (/lib/x86_64-linux-gnu/libclang-cpp.so.10+0x9184f1)
#5 0x00007ffcf00c8960 
clang: error: unable to execute command: Segmentation fault (core dumped)
clang: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 10.0.0-4ubuntu1 
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-10/bin
clang: note: diagnostic msg: PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
clang: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/bug-rev-fn-call-array-847715.cpp
clang: note: diagnostic msg: /tmp/bug-rev-fn-call-array-847715.sh
clang: note: diagnostic msg: 

********************
/usr/lib/llvm-10/bin/clang++ -g -Xclang -add-plugin -Xclang clad -Xclang -load -Xclang /home/parth/clubs/open-source/clad/pristine/builds/build-10/inst/lib/clad.so -Xclang -plugin-arg-clad -Xclang -fdump-derived-fn -I/home/parth/clubs/open-source/clad/pristine/builds/build-10/inst/include -x c++ -std=c++11 bug-rev-fn-call-array.cpp -o build/bug-rev-fn-call-array && build/bug-rev-fn-call-array

The root cause of the error is that we are trying to differentiate the function call using the forward mode AD since there is only one argument. We cannot use the forward mode AD on array arguments because array has multiple elements, thus we should not try to use forward mode AD for single array argument functions.

parth-07 added a commit to parth-07/clad that referenced this issue Mar 1, 2022
The pullback function approach allows to "continue" the reverse mode automatic derivation when required.
This allows correctly computing derivatives when arguments are passed by reference or pointers.

This commit also modifies custom gradient functions to custom pullback functions.

Closes vgvassilev#281, Closes vgvassilev#386, Closes vgvassilev#387
vgvassilev pushed a commit that referenced this issue Mar 1, 2022
The pullback function approach allows to "continue" the reverse mode automatic derivation when required.
This allows correctly computing derivatives when arguments are passed by reference or pointers.

This commit also modifies custom gradient functions to custom pullback functions.

Closes #281, Closes #386, Closes #387
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant