chipsalliance / VeeR-ISS

Apache License 2.0
114 stars 33 forks source link

C++ Program either gets caught in infinite loop or get aborted! #3

Closed anubane closed 3 years ago

anubane commented 3 years ago

I have a multi-file (3 .cpp files and 2 .h files) C++ code that uses new, new[], delete, and delete[] operators to allocate large chunks of memory and performs some floating point operations (requires division by sqrt()). The program compiles and runs well with regular g++ and produces the required output. I now want to convert it to RISC-V instructions.

  1. I first used the GNU RISC-V compiler toolchain to compile the program with following command: riscv32-unknown-elf-g++ -w -march=rv32imafc -mabi=ilp32f -DPREALLOCATE=1 -mcmodel=medany -fno-common -static -Iinclude/ f1.cpp f2.cpp f3.cpp -o executable here executable is the compiled binary file.

  2. Since no errors are thrown at compile time, I proceeded to run the whisper simulator as follows: whisper --isa imafc --target executable --logfile mylog --profileinst prfl where mylog and prfl are output file paths.

  3. The log file generated exceeds 6 GB in size and it doesn't terminate, so I assume that it is stuck in some infinite loop.

  4. I tried adding the _start() function and tohost variable which didn't work either (same problem as 3 above).

  5. I generated an object dump for executable and captured the (hex) addresses of starting address of main(), its ending address and address of tohost and passed them as parameters to --startpc, --endpc and --tohost respectively, in which case, the program was aborted after 64 illegal operations and ~300 MB of log file.

I think I am making some mistake. Kindly help me fix this issue or share the correct steps to generate the RISC-V assembly instructions for my C++ program.

jrahmeh commented 3 years ago

Hi Anurag,

The commands you used look good. Maybe we have a bug in whisper. Would it be possible to share the source files so that we can try to reproduce the problem and fix it.

Thanks,

Joe

anubane commented 3 years ago

cppcode.zip

Please find the code attached in the .zip file. (Please modify the makefile in case you have to use it.)

The code, when compiled and run with regular g++, will currently not produce any output as I have commented out all the print statements. However it runs fine.

One thing to note is that I have used inline functions - could that have caused the issue?

jrahmeh commented 3 years ago

Hi Anurag,

The program has a bug in the normalize function: float* hs = new float[rows]; for (int i=0; i<cols; ++i) { hs[i] = 0; //++memwrite; //++l_memwrite; } The cols in the loop should be replaced by rows. The bug causes memory corruption. Once bug is fixed, code runs fine in whisper.

Joe

jrahmeh commented 3 years ago

Hi Anurag,

The program has a bug that corrupts memory. The bug is in the normalize function at line 161 of symm_normalize.h. The code should be: for (int i=0; i<rows; ++i) {

After fixing the bug, the code ran fine on whisper.

I found the bug by compiling the code for x86 and running the valgrind tool on the executable: valgrind bin/executable

Joe

On Tue, Dec 1, 2020 at 12:24 AM Anurag Banerjee notifications@github.com wrote:

cppcode.zip https://github.com/chipsalliance/SweRV-ISS/files/5620613/cppcode.zip

Please find the code attached in the .zip file. (Please modify the makefil in case you have to use it.)

The code, when compiled and run with regular g++, will currently not produce any output as I have commented out all the print statements.

One thing to note is that I have used inline functions - could that have caused the issue?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/chipsalliance/SweRV-ISS/issues/3#issuecomment-736251729, or unsubscribe https://github.com/notifications/unsubscribe-auth/AASHQX2PZT27SHS5IWZ2PQDSSSD23ANCNFSM4UHRKGFA .

anubane commented 3 years ago

Hello Dr. Rahmeh,

Thank you for helping me out with this - I feel silly for having made such an obvious out-of-bound-access error. Further, I also made an error at symm_normalize.h:112 - it should have been D[i] instead of D[j] - but that is more of a logical error.

However, for anyone reading this in future - please also make sure that you have the latest whisper simulator (this repo) and not the older one (westerndigital). In case you are using -march=rv32imafc during compile time and --isa imafc during running whisper, make sure your data is in float datatype - I used sqrt() (returns double) which caused some error for me, but, when I used sqrtf() all was fine. My final log file was about 725 MB in size.

Also, for me, using --newlib was essential while running whisper.

jrahmeh commented 3 years ago

HI Anurag,

No worries. Thanks for using whisper. I will remove the old repository. I will also add notes about double/float issues.

Thanks,

Joe

On Wed, Dec 2, 2020 at 1:41 PM Anurag Banerjee notifications@github.com wrote:

Hello Dr. Rahmeh,

Thank you for helping me out with this - I feel silly for having made such an obvious out-of-bound-access error. Further, I also made an error at symm_normalize.h:112 - it should have been D[i] instead of D[j] - but that is more of a logical error.

However, for anyone reading this in future - please also make sure that you have the latest whisper simulator (this repo) and not the older one (westerndigital). In case you are using -march=rv32imafc during compile time and --isa imafc during running whisper, make sure your data is in float datatype - I used sqrt() (returns double) which caused some error for me, but, when I used sqrtf() all was fine. My final log file was about 725 MB in size.

Also, for me, using --newlib was essential while running whisper.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/chipsalliance/SweRV-ISS/issues/3#issuecomment-737452564, or unsubscribe https://github.com/notifications/unsubscribe-auth/AASHQXZIWYL2K6V3LK25IH3SS2J7XANCNFSM4UHRKGFA .