vgvassilev / clad

clad -- automatic differentiation for C/C++
GNU Lesser General Public License v3.0
277 stars 118 forks source link

crash on jacobian with array inputs #472

Open FROL256 opened 2 years ago

FROL256 commented 2 years ago

Hello! I took an example of jacobian from main page (h). If i make the first parameter array, clad crashes :(

Input:


//void h(float a, float b, float output[]) {
//    output[0] = a * a * a;
//    output[1] = a * a * a + b * b * b;
//    output[2] = 2 * (a + b);
//}

void h(float a[3], float b, float output[]) {
    output[0] = a[0] * a[0] * a[0];
    output[1] = a[0] * a[0] * a[0] + b * b * b;
    output[2] = 2 * (a[0] + b);
}

Error:

Executing task: clang-12 -I /home/frol/PROG/clad_autodiff_examples/inst/include -x c++ -std=c++11 -g -fplugin=/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so SourceFile.cpp -o z_out -lstdc++ -lm -Xclang -plugin-arg-clad -Xclang -fgenerate-source-file -DCLAD_NO_NUM_DIFF 

clang: /usr/lib/llvm-12/include/llvm/ADT/SmallVector.h:277: T& llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::operator[](llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::size_type) [with T = const clang::ValueDecl*; <template-parameter-1-2> = void; llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::reference = const clang::ValueDecl*&; llvm::SmallVectorTemplateCommon<T, <template-parameter-1-2> >::size_type = long unsigned int]: Assertion `idx < size()' failed.
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /usr/lib/llvm-12/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SourceFile.cpp -mrelocation-model static -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -resource-dir /usr/lib/llvm-12/lib/clang/12.0.0 -I /home/frol/PROG/clad_autodiff_examples/inst/include -D CLAD_NO_NUM_DIFF -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/x86_64-linux-gnu/c++/9 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-12/lib/clang/12.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/frol/PROG/clad_autodiff_examples -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -load /home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so -plugin-arg-clad -fgenerate-source-file -faddrsig -o /tmp/SourceFile-dc6b59.o -x c++ SourceFile.cpp
1.      <eof> parser at end of file
 #0 0x00007ffa76823ef3 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/lib/x86_64-linux-gnu/libLLVM-12.so.1+0xbd8ef3)
 #1 0x00007ffa76822210 llvm::sys::RunSignalHandlers() (/lib/x86_64-linux-gnu/libLLVM-12.so.1+0xbd7210)
 #2 0x00007ffa7682455f (/lib/x86_64-linux-gnu/libLLVM-12.so.1+0xbd955f)
 #3 0x00007ffa7e059420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #4 0x00007ffa7574e00b raise /build/glibc-SzIz7B/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #5 0x00007ffa7572d859 abort /build/glibc-SzIz7B/glibc-2.31/stdlib/abort.c:81:7
 #6 0x00007ffa7572d729 get_sysdep_segment_value /build/glibc-SzIz7B/glibc-2.31/intl/loadmsgcat.c:509:8
 #7 0x00007ffa7572d729 _nl_load_domain /build/glibc-SzIz7B/glibc-2.31/intl/loadmsgcat.c:970:34
 #8 0x00007ffa7573efd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #9 0x00007ffa752b036f llvm::SmallVectorTemplateCommon<clang::ValueDecl const*, void>::operator[](unsigned long) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x14936f)
#10 0x00007ffa75475141 clad::ReverseModeVisitor::VisitBinaryOperator(clang::BinaryOperator const*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x30e141)
#11 0x00007ffa754804e8 clang::StmtVisitorBase<llvm::make_const_ptr, clad::ReverseModeVisitor, clad::StmtDiff>::VisitBinAssign(clang::BinaryOperator const*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x3194e8)
#12 0x00007ffa7547bdf1 clang::StmtVisitorBase<llvm::make_const_ptr, clad::ReverseModeVisitor, clad::StmtDiff>::Visit(clang::Stmt const*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x314df1)
#13 0x00007ffa75469efa clad::ReverseModeVisitor::Visit(clang::Stmt const*, clang::Expr*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x302efa)
#14 0x00007ffa754773a7 clad::ReverseModeVisitor::DifferentiateSingleStmt(clang::Stmt const*, clang::Expr*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x3103a7)
#15 0x00007ffa7546de9f clad::ReverseModeVisitor::VisitCompoundStmt(clang::CompoundStmt const*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x306e9f)
#16 0x00007ffa7547c22e clang::StmtVisitorBase<llvm::make_const_ptr, clad::ReverseModeVisitor, clad::StmtDiff>::Visit(clang::Stmt const*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x31522e)
#17 0x00007ffa75469efa clad::ReverseModeVisitor::Visit(clang::Stmt const*, clang::Expr*) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x302efa)
#18 0x00007ffa7546cd14 clad::ReverseModeVisitor::Derive(clang::FunctionDecl const*, clad::DiffRequest const&) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x305d14)
#19 0x00007ffa75468ea4 clad::JacobianModeVisitor::Derive(clang::FunctionDecl const*, clad::DiffRequest const&) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x301ea4)
#20 0x00007ffa7528cb2c clad::DerivativeBuilder::Derive(clad::DiffRequest const&) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x125b2c)
#21 0x00007ffa7527da12 clad::plugin::CladPlugin::ProcessDiffRequest(clad::DiffRequest&) (.localalias) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x116a12)
#22 0x00007ffa7527d225 clad::plugin::CladPlugin::HandleTopLevelDecl(clang::DeclGroupRef) (/home/frol/PROG/clad_autodiff_examples/inst/lib/clad.so+0x116225)
#23 0x00007ffa7d300b2c clang::MultiplexConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/lib/x86_64-linux-gnu/libclang-cpp.so.12+0x1effb2c)
#24 0x00007ffa7bd39e58 clang::ParseAST(clang::Sema&, bool, bool) (/lib/x86_64-linux-gnu/libclang-cpp.so.12+0x938e58)
#25 0x00007ffa7d2cf118 clang::FrontendAction::Execute() (/lib/x86_64-linux-gnu/libclang-cpp.so.12+0x1ece118)
#26 0x00007ffa7d25cdd1 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/lib/x86_64-linux-gnu/libclang-cpp.so.12+0x1e5bdd1)
#27 0x00007ffa7d331502 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/lib/x86_64-linux-gnu/libclang-cpp.so.12+0x1f30502)
#28 0x0000000000412782 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm-12/bin/clang+0x412782)
#29 0x0000000000410afe (/usr/lib/llvm-12/bin/clang+0x410afe)
#30 0x000000000041090e main (/usr/lib/llvm-12/bin/clang+0x41090e)
#31 0x00007ffa7572f083 __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:342:3
#32 0x000000000040dcbe _start (/usr/lib/llvm-12/bin/clang+0x40dcbe)
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 12.0.0-3ubuntu1~20.04.5
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
clang: note: diagnostic msg: 

PS: reproduces both with current version in master and release v0.9. Thank you very much! :)

vgvassilev commented 2 years ago

Hi @FROL256,

This works for me. Can you give a standalone example which crashes? I use Ubuntu clang version 12.0.1-++20210630032957+fed41342a82f-1~exp1~20210630133716.134

cat array.cpp 
#include "clad/Differentiator/Differentiator.h"

void h(float a[3], float b, float output[]) {
    output[0] = a[0] * a[0] * a[0];
    output[1] = a[0] * a[0] * a[0] + b * b * b;
    output[2] = 2 * (a[0] + b);
}

int main() {
  auto dh = clad::gradient(h);
  dh.dump();
  return 0;
}
```bash vvassilev@vv-nuc ~/workspace/builds/clad $ /usr/lib/llvm-12/bin/clang -x c++ -std=c++11 -Xclang -add-plugin -Xclang clad -Xclang -plugin-arg-clad -Xclang -fdump-derived-fn -Xclang -load -Xclang /home/vvassilev/workspace/builds/clad/lib/clad.so array.cpp -I/home/vvassilev/workspace/sources/clad/test/Hessian/../../include -lstdc++ -lm void h_grad(float a[3], float b, float output[], clad::array_ref _d_a, clad::array_ref _d_b, clad::array_ref _d_output) { float _t0; float _t1; float _t2; float _t3; float _t4; float _t5; float _t6; float _t7; float _t8; float _t9; float _t10; float _t11; float _t12; _t2 = a[0]; _t1 = a[0]; _t3 = _t2 * _t1; _t0 = a[0]; output[0] = _t3 * _t0; _t6 = a[0]; _t5 = a[0]; _t7 = _t6 * _t5; _t4 = a[0]; _t10 = b; _t9 = b; _t11 = _t10 * _t9; _t8 = b; output[1] = _t7 * _t4 + _t11 * _t8; _t12 = (a[0] + b); output[2] = 2 * _t12; { float _r_d2 = _d_output[2]; float _r12 = _r_d2 * _t12; float _r13 = 2 * _r_d2; _d_a[0] += _r13; * _d_b += _r13; _d_output[2] -= _r_d2; _d_output[2]; } { float _r_d1 = _d_output[1]; float _r4 = _r_d1 * _t4; float _r5 = _r4 * _t5; _d_a[0] += _r5; float _r6 = _t6 * _r4; _d_a[0] += _r6; float _r7 = _t7 * _r_d1; _d_a[0] += _r7; float _r8 = _r_d1 * _t8; float _r9 = _r8 * _t9; * _d_b += _r9; float _r10 = _t10 * _r8; * _d_b += _r10; float _r11 = _t11 * _r_d1; * _d_b += _r11; _d_output[1] -= _r_d1; _d_output[1]; } { float _r_d0 = _d_output[0]; float _r0 = _r_d0 * _t0; float _r1 = _r0 * _t1; _d_a[0] += _r1; float _r2 = _t2 * _r0; _d_a[0] += _r2; float _r3 = _t3 * _r_d0; _d_a[0] += _r3; _d_output[0] -= _r_d0; _d_output[0]; } } vvassilev@vv-nuc ~/workspace/builds/clad $ ./a.out The code is: void h_grad(float a[3], float b, float output[], clad::array_ref _d_a, clad::array_ref _d_b, clad::array_ref _d_output) { float _t0; float _t1; float _t2; float _t3; float _t4; float _t5; float _t6; float _t7; float _t8; float _t9; float _t10; float _t11; float _t12; _t2 = a[0]; _t1 = a[0]; _t3 = _t2 * _t1; _t0 = a[0]; output[0] = _t3 * _t0; _t6 = a[0]; _t5 = a[0]; _t7 = _t6 * _t5; _t4 = a[0]; _t10 = b; _t9 = b; _t11 = _t10 * _t9; _t8 = b; output[1] = _t7 * _t4 + _t11 * _t8; _t12 = (a[0] + b); output[2] = 2 * _t12; { float _r_d2 = _d_output[2]; float _r12 = _r_d2 * _t12; float _r13 = 2 * _r_d2; _d_a[0] += _r13; * _d_b += _r13; _d_output[2] -= _r_d2; _d_output[2]; } { float _r_d1 = _d_output[1]; float _r4 = _r_d1 * _t4; float _r5 = _r4 * _t5; _d_a[0] += _r5; float _r6 = _t6 * _r4; _d_a[0] += _r6; float _r7 = _t7 * _r_d1; _d_a[0] += _r7; float _r8 = _r_d1 * _t8; float _r9 = _r8 * _t9; * _d_b += _r9; float _r10 = _t10 * _r8; * _d_b += _r10; float _r11 = _t11 * _r_d1; * _d_b += _r11; _d_output[1] -= _r_d1; _d_output[1]; } { float _r_d0 = _d_output[0]; float _r0 = _r_d0 * _t0; float _r1 = _r0 * _t1; _d_a[0] += _r1; float _r2 = _t2 * _r0; _d_a[0] += _r2; float _r3 = _t3 * _r_d0; _d_a[0] += _r3; _d_output[0] -= _r_d0; _d_output[0]; } } ```
vgvassilev commented 2 years ago

I can reproduce the crash if I change clad::gradient to clad::jacobian.

@Nirhar, can you take a look what is wrong here?