Skip to content

Commit

Permalink
Atomic, section of Accelerate ML/DL on OpenCL/SYC and refine Makefile…
Browse files Browse the repository at this point in the history
… and install.rst
  • Loading branch information
Jonathan2251 committed Sep 23, 2022
1 parent 10f3cf6 commit 804338d
Show file tree
Hide file tree
Showing 27 changed files with 368 additions and 122 deletions.
Binary file added Fig/gpu/opencl_ml_graph.key
Binary file not shown.
Binary file added Fig/gpu/opencl_ml_graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 8 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,29 @@ If you would like to view an up to date version of this book in your
browser without checking out and building the book, please visit:
http://jonathan2251.github.io/lbd/index.html

Linux prerequisite:
Add swapfile for Linux: http://jonathan2251.github.io/lbd/install.html#build-steps

BUILD steps:

$ pwd

$ $HOME // HOME directory

$ mkdir llvm

$ mkdir git
$ cd git

$ pwd

$ $HOME/git

$ git clone https://github.com/Jonathan2251/lbd

$ cd lbd/lbdex/install_llvm

$ bash build-llvm.sh

...
# Please remember to add ${LLVM_RELEASE_DIR}/bin to variable ${PATH} to your
# environment for clang++, clang. Reference last line of
# lbd/lbdex/install_llvm/build-llvm.sh

$ cd ..

$ pwd

$ $HOME/git/lbd/lbdex

$ bash build-cpu0.sh

CHECK step:
$ bash check.sh
46 changes: 46 additions & 0 deletions check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

pushd lbdex/input
echo "bash build-run_backend.sh cpu032I eb"
bash build-run_backend.sh cpu032I eb
if [ "$?" != "0" ]; then
echo "FAIL"
exit 1;
fi
pushd ../verilog
make
./cpu0Is
popd

echo "bash build-run_backend.sh cpu032I el"
bash build-run_backend.sh cpu032I el
if [ "$?" != "0" ]; then
echo "FAIL"
exit 1;
fi
pushd ../verilog
./cpu0Is
popd

echo "bash build-run_backend.sh cpu032II eb"
bash build-run_backend.sh cpu032II eb
if [ "$?" != "0" ]; then
echo "FAIL"
exit 1;
fi
pushd ../verilog
./cpu0IIs
popd

echo "bash build-run_backend.sh cpu032II el"
bash build-run_backend.sh cpu032II el
if [ "$?" != "0" ]; then
echo "FAIL"
exit 1;
fi
pushd ../verilog
./cpu0IIs
popd

echo "PASS"
popd
2 changes: 1 addition & 1 deletion lbdex/build-cpu0.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

LLVM_DIR=~/llvm
LLVM_RELEASE_DIR=${LLVM_DIR}/release
LLVM_DEBUG_DIR=${LLVM_DIR}/debug
LLVM_TEST_DIR=${LLVM_DIR}/test

if ! test -d ${LLVM_TEST_DIR}; then
Expand Down
10 changes: 5 additions & 5 deletions lbdex/build-llvm-test-suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LLVM_DIR=~/llvm
# Cpu0 set the other installed DIRs here
LLVM_SRC_DIR=${LLVM_DIR}/llvm-project
LLVM_TEST_SUITE_DIR=${LLVM_SRC_DIR}/test-suite
LLVM_RELEASE_DIR=${LLVM_DIR}/release
LLVM_DEBUG_DIR=${LLVM_DIR}/debug

# ref.
# https://llvm.org/docs/TestSuiteGuide.html
Expand All @@ -17,11 +17,11 @@ LLVM_RELEASE_DIR=${LLVM_DIR}/release
# sudo apt-get install tcl tk tcl-dev tk-dev
# On macos,
# brew install tcl-tk
# ${LLVM_RELEASE_DIR}/build: build with clang and compiler-rt, -DLLVM_ENABLE_PROJECTS="clang;compiler-rt" --> ref. https://github.com/Jonathan2251/lbd/blob/master/lbdex/install_llvm/build-llvm.sh
# ${LLVM_DEBUG_DIR}/build: build with clang and compiler-rt, -DLLVM_ENABLE_PROJECTS="clang;compiler-rt" --> ref. https://github.com/Jonathan2251/lbd/blob/master/lbdex/install_llvm/build-llvm.sh

build() {
cd test-suite-build
cmake -DCMAKE_C_COMPILER=${LLVM_RELEASE_DIR}/build/bin/clang -C../test-suite/cmake/caches/O3.cmake -DCMAKE_C_FLAGS=-fPIE -DCMAKE_CXX_FLAGS=-fPIE ../test-suite
cmake -DCMAKE_C_COMPILER=${LLVM_DEBUG_DIR}/build/bin/clang -C../test-suite/cmake/caches/O3.cmake -DCMAKE_C_FLAGS=-fPIE -DCMAKE_CXX_FLAGS=-fPIE ../test-suite
make
}

Expand All @@ -31,14 +31,14 @@ if ! test -d ${LLVM_TEST_SUITE_DIR}; then
cd test-suite
git checkout -b 12.x origin/release/12.x
popd
pushd ${LLVM_RELEASE_DIR}
pushd ${LLVM_DEBUG_DIR}
ln -s ../llvm-project/test-suite test-suite
# sed 's/^add_subdirectory(XRay)/#add_subdirectory(XRay)/g' test-suite/MicroBenchmarks/CMakeLists.txt
rm -rf test-suite-build
mkdir test-suite-build
build;
else
pushd ${LLVM_RELEASE_DIR}
pushd ${LLVM_DEBUG_DIR}
build;
echo "${LLVM_TEST_SUITE_DIR} has existed already"
exit 1
Expand Down
2 changes: 1 addition & 1 deletion lbdex/gen-ref-output.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ OS=`uname -s`
echo "OS =" ${OS}

TOOLDIR=~/llvm/test/build/bin
CLANG=~/llvm/release/build/bin/clang
CLANG=~/llvm/debug/build/bin/clang

rm -rf output
mkdir output
Expand Down
27 changes: 17 additions & 10 deletions lbdex/input/build-run_backend.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#!/usr/bin/env bash

# for example:
# bash build-run_backend.sh cpu032I el
# bash build-run_backend.sh cpu032II eb

source functions.sh

sh_name=build-run_backend.sh
argNum=$#
arg1=$1
arg2=$2
ARG_NUM=$#
CPU=$1
ENDIAN=$2

DEFFLAGS=""
if [ "$arg1" == cpu032II ] ; then
if [ "$CPU" == cpu032II ] ; then
DEFFLAGS=${DEFFLAGS}" -DCPU032II"
fi
echo ${DEFFLAGS}
Expand All @@ -19,23 +23,26 @@ prologue;
# on verilog.
$CLANG ${DEFFLAGS} -target mips-unknown-linux-gnu -c ch8_2_select_global_pic.cpp \
-emit-llvm -o ch8_2_select_global_pic.bc
${TOOLDIR}/llc -march=cpu0${endian} -mcpu=${CPU} -relocation-model=pic \
${TOOLDIR}/llc -march=cpu0${ENDIAN} -mcpu=${CPU} -relocation-model=pic \
-filetype=obj ch8_2_select_global_pic.bc -o ch8_2_select_global_pic.cpu0.o

$CLANG ${DEFFLAGS} -target mips-unknown-linux-gnu -c ch_run_backend.cpp \
-emit-llvm -o ch_run_backend.bc
echo "${TOOLDIR}/llc -march=cpu0${endian} -mcpu=${CPU} -relocation-model=static \
echo "${TOOLDIR}/llc -march=cpu0${ENDIAN} -mcpu=${CPU} -relocation-model=static \
-filetype=obj -enable-cpu0-tail-calls ch_run_backend.bc -o ch_run_backend.cpu0.o"
${TOOLDIR}/llc -march=cpu0${endian} -mcpu=${CPU} -relocation-model=static \
${TOOLDIR}/llc -march=cpu0${ENDIAN} -mcpu=${CPU} -relocation-model=static \
-filetype=obj -enable-cpu0-tail-calls ch_run_backend.bc -o ch_run_backend.cpu0.o
# print must at the same line, otherwise it will spilt into 2 lines
${TOOLDIR}/llvm-objdump --section=.text -d ch_run_backend.cpu0.o | tail -n +8| awk \
'{print "/* " $1 " */\t" $2 " " $3 " " $4 " " $5 "\t/* " $6"\t" $7" " $8" " $9" " $10 "\t*/"}' \
> ../verilog/cpu0.hex

if [ "$arg2" == le ] ; then
echo "1 /* 0: big endian, 1: little endian */" > ../verilog/cpu0.config
ENDIAN=`${TOOLDIR}/llvm-readobj -h ch_run_backend.cpu0.o|grep "DataEncoding"|awk '{print $2}'`
isLittleEndian;

if [ $LE == "true" ] ; then
echo "1 /* 0: big ENDIAN, 1: little ENDIAN */" > ../verilog/cpu0.config
else
echo "0 /* 0: big endian, 1: little endian */" > ../verilog/cpu0.config
echo "0 /* 0: big ENDIAN, 1: little ENDIAN */" > ../verilog/cpu0.config
fi
cat ../verilog/cpu0.config
13 changes: 8 additions & 5 deletions lbdex/input/build-run_backend2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
source functions.sh

sh_name=build-run_backend.sh
argNum=$#
arg1=$1
arg2=$2
ARG_NUM=$#
CPU=$1
ENDIAN=$2

DEFFLAGS=""
if [ "$arg1" == cpu032II ] ; then
Expand All @@ -17,13 +17,16 @@ prologue;

${CLANG} ${DEFFLAGS} -c ch_run_backend2.cpp \
-emit-llvm -o ch_run_backend2.bc
${TOOLDIR}/llc -march=cpu0${endian} -mcpu=${CPU} -relocation-model=static \
${TOOLDIR}/llc -march=cpu0${ENDIAN} -mcpu=${CPU} -relocation-model=static \
-filetype=obj ch_run_backend2.bc -o ch_run_backend2.cpu0.o
${TOOLDIR}/llvm-objdump -d ch_run_backend2.cpu0.o | tail -n +8| awk \
'{print "/* " $1 " */\t" $2 " " $3 " " $4 " " $5 "\t/* " $6"\t" $7" " $8" \
" $9" " $10 "\t*/"}' > ../verilog/cpu0.hex

if [ "$arg2" == le ] ; then
ENDIAN=`${TOOLDIR}/llvm-readobj -h ch_run_backend2.cpu0.o|grep "DataEncoding"|awk '{print $2}'`
isLittleEndian;

if [ $LE == "true" ] ; then
echo "1 /* 0: big endian, 1: little endian */" > ../verilog/cpu0.config
else
echo "0 /* 0: big endian, 1: little endian */" > ../verilog/cpu0.config
Expand Down
4 changes: 2 additions & 2 deletions lbdex/input/ch8_2_phinode.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ~/llvm/release/build/bin/clang -O3 -target mips-unknown-linux-gnu -c ch8_2_phinode.cpp -emit-llvm -o ch8_2_phinode.bc
// ~/llvm/release/build/bin/clang -O0 -target mips-unknown-linux-gnu -c ch8_2_phinode.cpp -emit-llvm -o ch8_2_phinode.bc
// ~/llvm/debug/build/bin/clang -O3 -target mips-unknown-linux-gnu -c ch8_2_phinode.cpp -emit-llvm -o ch8_2_phinode.bc
// ~/llvm/debug/build/bin/clang -O0 -target mips-unknown-linux-gnu -c ch8_2_phinode.cpp -emit-llvm -o ch8_2_phinode.bc
// ~/llvm/test/build/bin/llvm-dis ch8_2_phinode.bc -o -
// ~/llvm/test/build/bin/llc -march=cpu0 -mcpu=cpu032I -relocation-model=pic -filetype=asm ch8_2_phinode.bc -o -

Expand Down
4 changes: 2 additions & 2 deletions lbdex/input/ch9_caller_callee_save_registers.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// clang -O0 -target mips-unknown-linux-gnu -c ch9_caller_callee_save_registers.cpp -emit-llvm -o ch9_caller_callee_save_registers.bc
// ~/llvm/test/build/bin/llc -O0 -march=cpu0 -relocation-model=static -filetype=asm ch9_caller_callee_save_registers.bc -o -
// ~/llvm/release/build/bin/llc -O0 -march=mips -relocation-model=static -filetype=asm ch9_caller_callee_save_registers.bc -o -
// ~/llvm/debug/build/bin/llc -O0 -march=mips -relocation-model=static -filetype=asm ch9_caller_callee_save_registers.bc -o -

/// start
extern int add1(int x);
Expand All @@ -12,4 +12,4 @@ int callee()
result = result - t1;

return result;
}
}
36 changes: 17 additions & 19 deletions lbdex/input/functions.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
prologue() {
if [ $argNum == 0 ]; then
if [ $ARG_NUM == 0 ]; then
echo "useage: bash $sh_name cpu_type endian"
echo " cpu_type: cpu032I or cpu032II"
echo " endian: be (big endian, default) or le (little endian)"
echo " endian: eb (big endian, default) or el (little endian)"
echo "for example:"
echo " bash build-slinker.sh cpu032I be"
exit 1;
fi
if [ $arg1 != cpu032I ] && [ $arg1 != cpu032II ]; then
if [ $CPU != cpu032I ] && [ $CPU != cpu032II ]; then
echo "1st argument is cpu032I or cpu032II"
exit 1
fi
Expand All @@ -18,38 +18,36 @@ prologue() {
TOOLDIR=~/llvm/test/build/bin
CLANG=~/llvm/test/build/bin/clang

CPU=$arg1
CPU=$CPU
echo "CPU =" "${CPU}"

if [ "$arg2" != "" ] && [ $arg2 != le ] && [ $arg2 != be ]; then
echo "2nd argument is be (big endian, default) or le (little endian)"
if [ "$ENDIAN" != "" ] && [ $ENDIAN != el ] && [ $ENDIAN != eb ]; then
echo "2nd argument is eb (big endian, default) or el (little endian)"
exit 1
fi
if [ "$arg2" == "" ] || [ $arg2 == be ]; then
endian=
else
endian=el
if [ $ENDIAN == eb ]; then
ENDIAN=
fi
echo "endian =" "${endian}"
echo "ENDIAN =" "${ENDIAN}"

bash clean.sh
}

isLittleEndian() {
echo "endian = " "$endian"
if [ "$endian" == "LittleEndian" ] ; then
le="true"
elif [ "$endian" == "BigEndian" ] ; then
le="false"
echo "ENDIAN = " "$ENDIAN"
if [ "$ENDIAN" == "LittleEndian" ] ; then
LE="true"
elif [ "$ENDIAN" == "BigEndian" ] ; then
LE="false"
else
echo "!endian unknown"
echo "!ENDIAN unknown"
exit 1
fi
}

elf2hex() {
${TOOLDIR}/llvm-objdump -elf2hex -le=${le} a.out > ../verilog/cpu0.hex
if [ ${le} == "true" ] ; then
${TOOLDIR}/llvm-objdump -elf2hex -le=$LE a.out > ../verilog/cpu0.hex
if [ $LE == "true" ] ; then
echo "1 /* 0: big endian, 1: little endian */" > ../verilog/cpu0.config
else
echo "0 /* 0: big endian, 1: little endian */" > ../verilog/cpu0.config
Expand Down
29 changes: 18 additions & 11 deletions lbdex/install_llvm/build-llvm.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#!/usr/bin/env bash

# If your memory is large enough, add swap size ubuntu as follows,
# 1. search "add swap size ubuntu"
# 2. https://bogdancornianu.com/change-swap-size-in-ubuntu/
# 3. add to boot by change to /swapfile swap swap default 0 0
# https://linuxize.com/post/how-to-add-swap-space-on-ubuntu-18-04/
# 4. reboot

LLVM_DIR=~/llvm
LLVM_RELEASE_DIR=${LLVM_DIR}/release
LLVM_DEBUG_DIR=${LLVM_DIR}/debug
LLVM_TEST_DIR=${LLVM_DIR}/test

pushd ${LLVM_DIR}

if ! test -d ${LLVM_DIR}/llvm-project; then
git clone git://github.com/llvm/llvm-project.git
git clone https://github.com/llvm/llvm-project.git
cd llvm-project
git checkout -b 12.x origin/release/12.x
git checkout e8a397203c67adbeae04763ce25c6a5ae76af52c
Expand All @@ -17,22 +24,22 @@ else
# exit 1
fi

if ! test -d ${LLVM_RELEASE_DIR}; then
mkdir ${LLVM_RELEASE_DIR}
cp -rf llvm-project/clang ${LLVM_RELEASE_DIR}
cp -rf llvm-project/llvm ${LLVM_RELEASE_DIR}
if ! test -d ${LLVM_DEBUG_DIR}; then
mkdir ${LLVM_DEBUG_DIR}
cp -rf llvm-project/clang ${LLVM_DEBUG_DIR}
cp -rf llvm-project/llvm ${LLVM_DEBUG_DIR}
# build compiler-rt for llvm-test-suite
cp -rf llvm-project/compiler-rt ${LLVM_RELEASE_DIR}
mkdir ${LLVM_RELEASE_DIR}/build
pushd ${LLVM_RELEASE_DIR}/build
cp -rf llvm-project/compiler-rt ${LLVM_DEBUG_DIR}
mkdir ${LLVM_DEBUG_DIR}/build
pushd ${LLVM_DEBUG_DIR}/build
OS=`uname -s`
echo "OS =" ${OS}
cmake -DCMAKE_BUILD_TYPE=Debug -DLLVM_ENABLE_PROJECTS="clang;compiler-rt" \
cmake -DCMAKE_BUILD_TYPE=Debug -DLLVM_ENABLE_PROJECTS="clang" \
-DLLVM_PARALLEL_COMPILE_JOBS=4 -DLLVM_PARALLEL_LINK_JOBS=1 -G "Ninja" ../llvm
ninja
popd
fi

popd
echo "Please remember to add ${LLVM_RELEASE_DIR}/bin to variable \${PATH} to your \
echo "Please remember to add ${LLVM_DEBUG_DIR}/build/bin to variable \${PATH} to your \
environment for clang++, clang."
2 changes: 1 addition & 1 deletion lbdex/opt/add.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; ~/llvm/3.9.0/release/cmake_debug_build/bin/opt -O3 -S add.ll -o -
; ~/llvm/debug/build/bin/opt -O3 -S add.ll -o -

define i32 @add(i32 %a, i32 %b) nounwind {
entry:
Expand Down
3 changes: 2 additions & 1 deletion lbdex/verilog/cpu0.v
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ module cpu0(input clock, reset, input [2:0] itype, output reg [2:0] tick,
// Mathematic
ADDiu: regSet(a, Rb+c16); // ADDiu Ra, Rb+Cx; Ra<=Rb+Cx
CMP: begin
if (Rb < Rc) `N=1; else `N=0;
if (Rb < Rc) `N=1; else `N=0;
// `N=(Rb-Rc<0); // why not work for bash make.sh cpu032I el Makefile.builtins?
`Z=(Rb-Rc==0);
end // CMP Rb, Rc; SW=(Rb >=< Rc)
CMPu: begin
Expand Down
Loading

0 comments on commit 804338d

Please sign in to comment.