lf-lang / lingua-franca

Intuitive concurrent programming in any language
https://www.lf-lang.org
Other
238 stars 63 forks source link

`-O2` is no longer applied to C programs by default #636

Closed Soroosh129 closed 3 years ago

Soroosh129 commented 3 years ago

A -O2 flag was passed to gcc during the compilation of C target programs prior to the switch to the CMake build system. That functionality is still there by using the cmake: false flag in the target property.

However, -O2 is specific to gcc and clang, and is not very portable. Hence, it is not part of the CMake build system. Nevertheless, this has a noticeable impact on the performance of C target programs.

I suggest we avoid adding the -O2 flag automatically in the CMake build system because it is not portable. Instead, I think a -O2 flag should be added to the target specification of C target programs where performance is important (e.g., in benchmarks). This should be accompanied by a compiler: gcc to avoid any warnings:

 target C {
    compiler: gcc,
    flags: ["-O2"]
}

Note for anyone who takes up on adding back this functionality

This flag can be added back to the CCmakeGenerator by adding the following lines unconditionally:

cMakeCode.append("add_compile_options( -O2 )\n");
cMakeCode.append("add_link_options( -O2 )\n");
edwardalee commented 3 years ago

Isn't performance important in all programs? Can you clarify exactly what I should add to the target properties of all my programs? Do I need this:

   target C {
      compiler: gcc,
  }

and an additional CMake .txt file (called what? specified where?) with this:

add_compile_options( -O2 )
add_link_options( -O2 )
cmnrd commented 3 years ago

Cmake abstracts build flags with its concept of build types. So cmake already has portable support for optimizing code. There is also already a build-type target property used by the C++ target as explained here. This defaults to Release which will add -O3 if the selected compiler is gcc or clang.

I added this issue to the benchmark project, because obviously having no optimization is a major performance problem.

Soroosh129 commented 3 years ago

@edwardalee You would have to specify

 target C {
    compiler: gcc,
    flags: ["-O2"]
}

Yes, I agree that performance is important but I haven't found a portable design to add this type of flag yet.

Cmake abstracts build flags with its concept of build types. So cmake already has portable support for optimizing code. There is also already a build-type target property used by the C++ target as explained here. This defaults to Release which will add -O3 if the selected compiler is gcc or clang.

The C target does support this build type, and is set to Release by default as well. However, I cannot find a documentation for the -O3 flag being applied. Is there a way to verify this?

Soroosh129 commented 3 years ago

an additional CMake .txt file (called what? specified where?) with this:

add_compile_options( -O2 )
add_link_options( -O2 )

This would have to be added in CCmakeGenerator. This was a hint to whoever would like to fix this in the future.

cmnrd commented 3 years ago

The C target does support this build type, and is set to Release by default as well. However, I cannot find a documentation for the -O3 flag being applied. Is there a way to verify this?

Yes, by setting the environment variable VERBOSE. Note the -O3 below :)

VERBOSE=1 ./bin/lfc test/C/src/Minimal.lf 
Generating code for: file:/home/cmenard/projects/lingua-franca/test/C/src/Minimal.lf
******** mode: STANDALONE
******** source file: /home/cmenard/projects/lingua-franca/test/C/src/Minimal.lf
******** generated sources: /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal
******** generated binaries: /home/cmenard/projects/lingua-franca/test/C/bin
******** Using 1 threads.
Cleaning /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build
--- Current working directory: /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build
--- Executing command: cmake -DCMAKE_INSTALL_PREFIX=/home/cmenard/projects/lingua-franca/test/C -DCMAKE_INSTALL_BINDIR=bin /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal
Re-run cmake no build system arguments
-- The C compiler identification is GNU 11.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build
--- Current working directory: /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build
--- Executing command: cmake --build . --target install --parallel 4 --config Release
/usr/bin/cmake -S/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal -B/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build/CMakeFiles /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build//CMakeFiles/progress.marks
/usr/bin/make  -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build'
/usr/bin/make  -f CMakeFiles/Minimal.dir/build.make CMakeFiles/Minimal.dir/depend
make[2]: Entering directory '/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build'
cd /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build/CMakeFiles/Minimal.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build'
/usr/bin/make  -f CMakeFiles/Minimal.dir/build.make CMakeFiles/Minimal.dir/build
make[2]: Entering directory '/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build'
[ 25%] Building C object CMakeFiles/Minimal.dir/core/platform/lf_linux_support.c.o
/usr/bin/cc  -I/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/core -I/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/core/platform -I/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/core/federated -O3 -DNDEBUG -std=gnu11 -MD -MT CMakeFiles/Minimal.dir/core/platform/lf_linux_support.c.o -MF CMakeFiles/Minimal.dir/core/platform/lf_linux_support.c.o.d -o CMakeFiles/Minimal.dir/core/platform/lf_linux_support.c.o -c /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/core/platform/lf_linux_support.c
[ 50%] Building C object CMakeFiles/Minimal.dir/Minimal.c.o
[ 75%] Building C object CMakeFiles/Minimal.dir/ctarget.c.o
/usr/bin/cc  -I/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/core -I/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/core/platform -I/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/core/federated -O3 -DNDEBUG -std=gnu11 -MD -MT CMakeFiles/Minimal.dir/Minimal.c.o -MF CMakeFiles/Minimal.dir/Minimal.c.o.d -o CMakeFiles/Minimal.dir/Minimal.c.o -c /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/Minimal.c
/usr/bin/cc  -I/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/core -I/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/core/platform -I/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/core/federated -O3 -DNDEBUG -std=gnu11 -MD -MT CMakeFiles/Minimal.dir/ctarget.c.o -MF CMakeFiles/Minimal.dir/ctarget.c.o.d -o CMakeFiles/Minimal.dir/ctarget.c.o -c /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/ctarget.c
[100%] Linking C executable Minimal
/usr/bin/cmake -E cmake_link_script CMakeFiles/Minimal.dir/link.txt --verbose=1
/usr/bin/cc -O3 -DNDEBUG CMakeFiles/Minimal.dir/Minimal.c.o CMakeFiles/Minimal.dir/core/platform/lf_linux_support.c.o CMakeFiles/Minimal.dir/ctarget.c.o -o Minimal 
make[2]: Leaving directory '/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build'
[100%] Built target Minimal
make[1]: Leaving directory '/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build'
/usr/bin/cmake -E cmake_progress_start /home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build/CMakeFiles 0
/usr/bin/make  -f CMakeFiles/Makefile2 preinstall
make[1]: Entering directory '/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build'
make[1]: Nothing to be done for 'preinstall'.
make[1]: Leaving directory '/home/cmenard/projects/lingua-franca/test/C/src-gen/Minimal/build'
Install the project...
/usr/bin/cmake -P cmake_install.cmake
-- Install configuration: "Release"
-- Installing: /home/cmenard/projects/lingua-franca/test/C/bin/Minimal
SUCCESS: Compiling generated code for Minimal finished with no errors.
Code generation finished.
Soroosh129 commented 3 years ago

Thanks @cmnrd. I can indeed see the -O3 on my PC. I wonder if I am seeing a similar issue as this where O2 is faster than O3 on my machine for even the TimeLimit benchmark (by about 10 msec or about 2%).

Soroosh129 commented 3 years ago

@edwardalee I updated the description of the issue to make it a bit clearer, but as mentioned by @cmnrd, it seems like an -O3 flag is applied by default anyway. The hit in performance thus is a lot more nuanced than I thought. I will close this issue.