Skip to content

Commit b41f6e9

Browse files
committed
Several algebraic simplification rewrites are added. All rewrites can be found in the file Canonicalize.cpp. All tests regarding the simplification rewrites can be found under the directory test/util.
1 parent c6f7563 commit b41f6e9

18 files changed

+826
-7
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ cmake -S "$projectRoot" -B "$daphneBuildDir" -G Ninja -DANTLR_VERSION="$antlrVer
11111111
-DCMAKE_PREFIX_PATH="$installPrefix" \
11121112
$BUILD_CUDA $BUILD_FPGAOPENCL $BUILD_DEBUG $BUILD_MPI $BUILD_HDFS $BUILD_PAPI
11131113

1114-
cmake --build "$daphneBuildDir" --target "$target"
1114+
cmake --build "$daphneBuildDir" --target "$target" -j1
11151115

11161116
build_ts_end=$(date +%s%N)
11171117
daphne_msg "Successfully built Daphne://${target} (took $(printableTimestamp $((build_ts_end - build_ts_begin))))"

llvm.sh

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
#!/bin/bash
2+
################################################################################
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
################################################################################
7+
#
8+
# This script will install the llvm toolchain on the different
9+
# Debian and Ubuntu versions
10+
11+
set -eux
12+
13+
usage() {
14+
set +x
15+
echo "Usage: $0 [llvm_major_version] [all] [OPTIONS]" 1>&2
16+
echo -e "all\t\t\tInstall all packages." 1>&2
17+
echo -e "-n=code_name\t\tSpecifies the distro codename, for example bionic" 1>&2
18+
echo -e "-h\t\t\tPrints this help." 1>&2
19+
echo -e "-m=repo_base_url\tSpecifies the base URL from which to download." 1>&2
20+
exit 1;
21+
}
22+
23+
CURRENT_LLVM_STABLE=19
24+
BASE_URL="http://apt.llvm.org"
25+
26+
NEW_DEBIAN_DISTROS=("trixie" "unstable")
27+
# Set default values for commandline arguments
28+
# We default to the current stable branch of LLVM
29+
LLVM_VERSION=$CURRENT_LLVM_STABLE
30+
ALL=0
31+
DISTRO=$(lsb_release -is)
32+
VERSION_CODENAME=$(lsb_release -cs)
33+
VERSION=$(lsb_release -sr)
34+
UBUNTU_CODENAME=""
35+
CODENAME_FROM_ARGUMENTS=""
36+
# Obtain VERSION_CODENAME and UBUNTU_CODENAME (for Ubuntu and its derivatives)
37+
source /etc/os-release
38+
DISTRO=${DISTRO,,}
39+
40+
# Check for required tools
41+
42+
# Check if this is a new Debian distro
43+
is_new_debian=0
44+
if [[ "${DISTRO}" == "debian" ]]; then
45+
for new_distro in "${NEW_DEBIAN_DISTROS[@]}"; do
46+
if [[ "${VERSION_CODENAME}" == "${new_distro}" ]]; then
47+
is_new_debian=1
48+
break
49+
fi
50+
done
51+
fi
52+
53+
# Check for required tools
54+
needed_binaries=(lsb_release wget gpg)
55+
# add-apt-repository is not needed for newer Debian distros
56+
if [[ $is_new_debian -eq 0 ]]; then
57+
needed_binaries+=(add-apt-repository)
58+
fi
59+
60+
missing_binaries=()
61+
using_curl=
62+
for binary in "${needed_binaries[@]}"; do
63+
if ! command -v $binary &>/dev/null ; then
64+
if [[ "$binary" == "wget" ]] && command -v curl &>/dev/null; then
65+
using_curl=1
66+
continue
67+
fi
68+
missing_binaries+=($binary)
69+
fi
70+
done
71+
72+
if [[ ${#missing_binaries[@]} -gt 0 ]] ; then
73+
echo "You are missing some tools this script requires: ${missing_binaries[@]}"
74+
echo "(hint: apt install lsb-release wget software-properties-common gnupg)"
75+
echo "curl is also supported"
76+
exit 4
77+
fi
78+
79+
case ${DISTRO} in
80+
debian)
81+
# Debian Trixie has a workaround because of
82+
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1038383
83+
if [[ "${VERSION}" == "unstable" ]] || [[ "${VERSION}" == "testing" ]] || [[ "${VERSION_CODENAME}" == "trixie" ]]; then
84+
CODENAME=unstable
85+
LINKNAME=
86+
else
87+
# "stable" Debian release
88+
CODENAME=${VERSION_CODENAME}
89+
LINKNAME=-${CODENAME}
90+
fi
91+
;;
92+
*)
93+
# ubuntu and its derivatives
94+
if [[ -n "${UBUNTU_CODENAME}" ]]; then
95+
CODENAME=${UBUNTU_CODENAME}
96+
if [[ -n "${CODENAME}" ]]; then
97+
LINKNAME=-${CODENAME}
98+
fi
99+
fi
100+
;;
101+
esac
102+
103+
# read optional command line arguments
104+
if [ "$#" -ge 1 ] && [ "${1::1}" != "-" ]; then
105+
if [ "$1" != "all" ]; then
106+
LLVM_VERSION=$1
107+
else
108+
# special case for ./llvm.sh all
109+
ALL=1
110+
fi
111+
OPTIND=2
112+
if [ "$#" -ge 2 ]; then
113+
if [ "$2" == "all" ]; then
114+
# Install all packages
115+
ALL=1
116+
OPTIND=3
117+
fi
118+
fi
119+
fi
120+
121+
while getopts ":hm:n:" arg; do
122+
case $arg in
123+
h)
124+
usage
125+
;;
126+
m)
127+
BASE_URL=${OPTARG}
128+
;;
129+
n)
130+
CODENAME=${OPTARG}
131+
if [[ "${CODENAME}" == "unstable" ]]; then
132+
# link name does not apply to unstable repository
133+
LINKNAME=
134+
else
135+
LINKNAME=-${CODENAME}
136+
fi
137+
CODENAME_FROM_ARGUMENTS="true"
138+
;;
139+
esac
140+
done
141+
142+
if [[ $EUID -ne 0 ]]; then
143+
echo "This script must be run as root!"
144+
exit 1
145+
fi
146+
147+
declare -A LLVM_VERSION_PATTERNS
148+
LLVM_VERSION_PATTERNS[9]="-9"
149+
LLVM_VERSION_PATTERNS[10]="-10"
150+
LLVM_VERSION_PATTERNS[11]="-11"
151+
LLVM_VERSION_PATTERNS[12]="-12"
152+
LLVM_VERSION_PATTERNS[13]="-13"
153+
LLVM_VERSION_PATTERNS[14]="-14"
154+
LLVM_VERSION_PATTERNS[15]="-15"
155+
LLVM_VERSION_PATTERNS[16]="-16"
156+
LLVM_VERSION_PATTERNS[17]="-17"
157+
LLVM_VERSION_PATTERNS[18]="-18"
158+
LLVM_VERSION_PATTERNS[19]="-19"
159+
LLVM_VERSION_PATTERNS[20]="-20"
160+
LLVM_VERSION_PATTERNS[21]=""
161+
162+
if [ ! ${LLVM_VERSION_PATTERNS[$LLVM_VERSION]+_} ]; then
163+
echo "This script does not support LLVM version $LLVM_VERSION"
164+
exit 3
165+
fi
166+
167+
LLVM_VERSION_STRING=${LLVM_VERSION_PATTERNS[$LLVM_VERSION]}
168+
169+
# join the repository name
170+
if [[ -n "${CODENAME}" ]]; then
171+
REPO_NAME="deb ${BASE_URL}/${CODENAME}/ llvm-toolchain${LINKNAME}${LLVM_VERSION_STRING} main"
172+
# check if the repository exists for the distro and version
173+
if ! wget -q --method=HEAD ${BASE_URL}/${CODENAME} &> /dev/null && \
174+
! curl -sSLI -XHEAD ${BASE_URL}/${CODENAME} &> /dev/null; then
175+
if [[ -n "${CODENAME_FROM_ARGUMENTS}" ]]; then
176+
echo "Specified codename '${CODENAME}' is not supported by this script."
177+
else
178+
echo "Distribution '${DISTRO}' in version '${VERSION}' is not supported by this script."
179+
fi
180+
exit 2
181+
fi
182+
fi
183+
184+
185+
# install everything
186+
187+
if [[ ! -f /etc/apt/trusted.gpg.d/apt.llvm.org.asc ]]; then
188+
# download GPG key once
189+
if [[ -z "$using_curl" ]]; then
190+
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
191+
else
192+
curl -sSL https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
193+
fi
194+
fi
195+
196+
if [[ -z "`apt-key list 2> /dev/null | grep -i llvm`" ]]; then
197+
# Delete the key in the old format
198+
apt-key del AF4F7421 || true
199+
fi
200+
201+
202+
# Add repository based on distribution
203+
if [[ "${VERSION_CODENAME}" == "bookworm" ]]; then
204+
# add it twice to workaround:
205+
# https://github.com/llvm/llvm-project/issues/62475
206+
add-apt-repository -y "${REPO_NAME}"
207+
add-apt-repository -y "${REPO_NAME}"
208+
elif [[ $is_new_debian -eq 1 ]]; then
209+
# workaround missing add-apt-repository in newer Debian and use new source.list format
210+
SOURCES_FILE="/etc/apt/sources.list.d/http_apt_llvm_org_${CODENAME}_-${VERSION_CODENAME}.sources"
211+
TEXT_TO_ADD="Types: deb
212+
Architectures: amd64 arm64
213+
Signed-By: /etc/apt/trusted.gpg.d/apt.llvm.org.asc
214+
URIs: ${BASE_URL}/${CODENAME}/
215+
Suites: llvm-toolchain${LINKNAME}${LLVM_VERSION_STRING}
216+
Components: main"
217+
echo "$TEXT_TO_ADD" | tee -a "$SOURCES_FILE" > /dev/null
218+
else
219+
add-apt-repository -y "${REPO_NAME}"
220+
fi
221+
222+
apt-get update
223+
PKG="clang-$LLVM_VERSION lldb-$LLVM_VERSION lld-$LLVM_VERSION clangd-$LLVM_VERSION"
224+
if [[ $ALL -eq 1 ]]; then
225+
# same as in test-install.sh
226+
# No worries if we have dups
227+
PKG="$PKG clang-tidy-$LLVM_VERSION clang-format-$LLVM_VERSION clang-tools-$LLVM_VERSION llvm-$LLVM_VERSION-dev lld-$LLVM_VERSION lldb-$LLVM_VERSION llvm-$LLVM_VERSION-tools libomp-$LLVM_VERSION-dev libc++-$LLVM_VERSION-dev libc++abi-$LLVM_VERSION-dev libclang-common-$LLVM_VERSION-dev libclang-$LLVM_VERSION-dev libclang-cpp$LLVM_VERSION-dev liblldb-$LLVM_VERSION-dev libunwind-$LLVM_VERSION-dev"
228+
if test $LLVM_VERSION -gt 14; then
229+
PKG="$PKG libclang-rt-$LLVM_VERSION-dev libpolly-$LLVM_VERSION-dev"
230+
fi
231+
fi
232+
apt-get install -y $PKG

scripts/examples/dyn_insert.daph

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
//Test for the simplification rewrite X[a:b, c:d] = Y -> X=Y if dims(X) = dims(Y)
3+
4+
//Simplification cannot be applied
5+
X = fill(0.0, 7, 8);
6+
Y = rand(3, 4, 10.0, 100.0, 1.0, 2);
7+
X[0:3, 2:6] = Y;
8+
print(Y);
9+
print(X);
10+
11+
//with simplification
12+
Q = fill(0.0, 7, 8);
13+
Z = rand(7, 8, 10.0, 100.0, 1.0, 3);
14+
Q[0:7, 0:8] = Z;
15+
print(Z);
16+
print(Q);

scripts/examples/mm_RowCol.daph

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
//Test for the simplification rewrite : (X%*%Y)[7,3] → X[7,]%*%Y[,3]
3+
m1 = rand(10, 8, 10.0, 20.0, 1.0, 1);
4+
m2 = rand(8, 5, 10.0, 20.0, 1.0, 1);
5+
m3 = (m1 @ m2)[7, 3];
6+
print(m3);
7+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Test for the simplification rewrite "(X%*%Y)[7,3] → X[7,]%*%Y[,3]" with transposed inputs:
2+
m1 = rand(8, 8, 10.0, 20.0, 1.0, 1);
3+
m2 = rand(8, 8, 10.0, 20.0, 1.0, 1);
4+
5+
mm1 = (m1 @ m2)[7,3];
6+
mm2 = (transpose(m1) @ transpose(m2))[7,3];
7+
mm3 = (m1 @ transpose(m2))[7,3];
8+
mm4 = (transpose(m1) @ m2)[7,3];
9+
10+
11+
print(mm1);
12+
print(mm2);
13+
print(mm3);
14+
print(mm4);
15+
16+
17+
18+

scripts/examples/sum_ewAdd.daph

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
// Test for the simplification rewrtie sum(X+Y) -> sum(X) + sum(Y)
3+
4+
m1 = rand(4, 1, 100.0, 200.0, 1.0, 4);
5+
m2 = rand(4, 1, 0, 100, 1.0, 4);
6+
sum_m1_plus_m2 = sum(m1 + m2);
7+
print(sum_m1_plus_m2);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
m1 = rand(4, 3, 100.0, 300.0, 1.0, 4);
3+
lamda = as.scalar(rand(1, 1, 1.0, 100.0, 1.0, 42));
4+
sum_lam = sum(lamda * m1);
5+
print(sum_lam);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
// Test for simplification rewrite sum(t(x)) -> sum(X)
3+
4+
// (row ,col ,min ,max ,sparsity (%), seed)
5+
m1 = rand(4, 3, 100.0, 300.0, 1.0, 4);
6+
sum_m1 = sum(transpose(m1));
7+
print(sum_m1);

scripts/examples/trace.daph

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//Test for the simplificiation rewrite: trace(X @ Y) = sum(diagVector(X @ Y)) → sum(X @ transpose(Y))
2+
3+
X = rand(7, 7, 1.0, 10.0, 1.0, 42);
4+
Y = rand(7, 7, 1.0, 10.0, 1.0, 42);
5+
tr = sum(diagVector(X @ Y));
6+
print(tr);

0 commit comments

Comments
 (0)