MLIR build with coverage takes up too much memory

When I build MLIR with code instrumentation with the following cmd:

cmake -G Ninja ../llvm \
   -DLLVM_ENABLE_PROJECTS=mlir \
   -DLLVM_BUILD_EXAMPLES=ON \
   -DLLVM_TARGETS_TO_BUILD="X86;NVPTX;AMDGPU" \
   -DCMAKE_BUILD_TYPE=Release \
   -DLLVM_ENABLE_ASSERTIONS=ON \
   -DCMAKE_C_FLAGS="-g -O0 -fprofile-arcs -ftest-coverage" \
   -DCMAKE_CXX_FLAGS="-g -O0 -fprofile-arcs -ftest-coverage" \
   -DCMAKE_EXE_LINKER_FLAGS="-g -fprofile-arcs -ftest-coverage -lgcov"

I found the linking takes up all of my 128G RAM and requests a large amount of swap memory. If I limit the swap space to 1G, it will report

collect2: fatal error: ld terminated with signal 9 [Killed]

Extending the swap will solve this, however, using swap space makes the build process much slower, and I couldn’t do anything else since all the RAM are occupied.

Is it normal that a build with coverage instrumentation needs so much memory? How to reduce/limit the memory used?

Try with -DLLVM_PARALLEL_LINK_JOBS=1. This may be a problem even for the LLVM itself.

The most impactful thing to do is installing LLD and enable it with -DLLVM_ENABLE_LLD=ON.

1 Like