BitNet icon indicating copy to clipboard operation
BitNet copied to clipboard

LLVM bug triggered by BitNet's code patterns

Open zymurgy opened this issue 6 months ago • 2 comments

On Apple M2 silicon it seems that the BitNet code is triggering a LLVM optimization bug. Others have reported this as the build running for hours: issue #251, #260, etc. I have observed this with Apple clang version 17.0.0 and Homebrew clang version 20.1.6. The issue is with InterleavedLoadCombine optimization pass and the specific vector shuffle patterns in the BitNet code. The same infinite recursion is happening with VectorInfo::computeFromSVI calling itself recursively. The fix for me was to disable the problematic optimization. With optimization disabled I was able to compile llama-cli and run inference.

diff setup_env.py setup_env.py.orig 
214,227c214
<     
<     # Add LLVM optimization flags to work around the interleaved load combine bug
<     llvm_fix_flags = "-O2 -mllvm -disable-interleaved-load-combine"
<     
<     run_command([
<         "cmake", "-B", "build", 
<         *COMPILER_EXTRA_ARGS[arch], 
<         *OS_EXTRA_ARGS.get(platform.system(), []), 
<         "-DCMAKE_C_COMPILER=clang", 
<         "-DCMAKE_CXX_COMPILER=clang++",
<         f"-DCMAKE_CXX_FLAGS={llvm_fix_flags}",
<         f"-DCMAKE_C_FLAGS={llvm_fix_flags}"
<     ], log_step="generate_build_files")
<     
---
>     run_command(["cmake", "-B", "build", *COMPILER_EXTRA_ARGS[arch], *OS_EXTRA_ARGS.get(platform.system(), []), "-DCMAKE_C_COMPILER=clang", "-DCMAKE_CXX_COMPILER=clang++"], log_step="generate_build_files")

Sample of clang.txt

zymurgy avatar Jun 05 '25 23:06 zymurgy

This does fix it!

Thanks @zymurgy

CtrlAltGiri avatar Jun 12 '25 11:06 CtrlAltGiri

I'm getting an error:

"ERROR:root:Error occurred while running command: Command '['cmake', '--build', 'build', '--config', 'Release']' returned non-zero exit status 2., check details in logs/compile.log"

and in the compile.log last three lines are:

"make[2]: *** [3rdparty/llama.cpp/ggml/src/CMakeFiles/ggml.dir/ggml-blas.cpp.o] Error 1 make[1]: *** [3rdparty/llama.cpp/ggml/src/CMakeFiles/ggml.dir/all] Error 2 make: *** [all] Error 2"

t-anton avatar Aug 29 '25 00:08 t-anton