Kaleidoscope on M1 Mac Help

I’m reading through the Kaleidoscope tutorial and attempting to run it on my M1 macbook running Ventura 13.2.1.

The first difficulty I had was in chapter 3. When I ran the compiler command it said:
ld: library not found for -lzstd

I fixed this using brew install zstd and then adding -L/opt/homebrew/lib to my compile commands.

It emitted a lot of warnings about mac os 13.0 vs 13.2, but those went away when I added -mmacosx-version-min=13.2 to the command as well.

Currently I’m stuck on chapter 9. I would like to be able to build a stand-alone executable, so this chapter is important.

The compile command there is: Kaleidoscope-Ch9 < fib.ks | & clang -x ir -
I’m not sure what the & is for but this command doesn’t parse in zsh or bash. I’m assuming it’s intending to direct stderr into clang, so I did:

./toy < fib.ks 2> fib.out
clang -x ir fib.out

The result was:

fib.out:43:77: error: unterminated attribute group
attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
                                                                            ^
1 error generated.

This is a issue of system clang version mismatch with the LLVM version you are using in kaleidoscope

You usually get this error when you feed “new” IR into an old clang/opt that doesn’t have the memory attribute.
Use an older Kaleidoscope or change the IR, s/memory(none)/readnone might do it already.

The textual IR format isn’t stable; make sure you use clang from the version of LLVM you’re linking against.

In theory you just install zstd and pkg-config should pick that up,

Ok, I tried building clang from the llvm repo version I was using, but this happened:

% clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core orcjit native` -o toy
In file included from toy.cpp:1:
In file included from ./../include/KaleidoscopeJIT.h:16:
In file included from /Users/nicholasretallack/Documents/llvm-project/llvm/include/llvm/ADT/StringRef.h:12:
/Users/nicholasretallack/Documents/llvm-project/llvm/include/llvm/ADT/DenseMapInfo.h:17:10: fatal error: 'cassert' file not found
#include <cassert>
         ^~~~~~~~~
1 error generated.

% clang++ -v
clang version 17.0.0 (https://github.com/llvm/llvm-project.git 4dd4eb939caef1138c655e22bb4adc8978f16427)
Target: arm64-apple-darwin22.3.0
Thread model: posix
InstalledDir: /Users/nicholasretallack/Documents/llvm-project/build/bin

It’s not seeing basic includes that my system version of clang can see.

This is how I built it:

cmake -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_BUILD_TYPE=Release ../llvm
make

This a well known feature of OSX. You have to tell clang where to find the SDK.

> xcrun --show-sdk-path

and then

> clang -sysroot <path-to-sdk> ....

Seems it’s -isysroot. Thanks. I had to add this to both invocations of clang but it’s outputting a binary now!

I’m curious about this warning though:

warning: overriding the module target triple with arm64-apple-macosx13.0.0 [-Woverride-module]

In chapter 8 I got this target:

% clang --version | grep Target
Target: arm64-apple-darwin22.3.0

-sysroot was too fast. You can ask clang for the triple:
cc --print-target-triple .