-
Notifications
You must be signed in to change notification settings - Fork 0
/
clpkm.sh
executable file
·195 lines (162 loc) · 4.15 KB
/
clpkm.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env bash
: << COMMENT
CLPKM compiler driver
====================
Introduction
--------------------
- The source is read from stdin
- Instrumented code will be emitted to stdout
- Kernel profile in YAML will be emitted to stderr
- On failure, nothing will be emitted to stdout, and log is emitted to stderr
COMMENT
function print_banner() {
echo "- $1:"
}
function dump_diag() {
while read LINE; do
# Skip empty line
if [[ -z "$LINE" ]]; then
echo ''
continue
fi
# Don't pad banner
if [[ -n "$(echo "$LINE" | grep '^- .*:$')" ]]; then
echo "$LINE"
continue
fi
# Pad log
while [[ -n "$LINE" ]]; do
echo " ${LINE:0:76}"
LINE="${LINE:76}"
done
done < "$1" 1>&2
}
function prettify() {
"$CLANG_FORMAT" -style=llvm "$1" > "$1"_ && \
mv "$1"_ "$1"
}
function prettify_to {
"$CLANG_FORMAT" -style=llvm "$1" > "$2" && \
rm "$1"
}
# Clang tools
CLANG_ROOT="$HOME"/llvm/5.0.1/clang-rel
CLANG="$CLANG_ROOT"/bin/clang
CLANG_FORMAT="$CLANG_ROOT"/bin/clang-format
CLANG_RENAME="$CLANG_ROOT"/bin/clang-rename
CLANG_TIDY="$CLANG_ROOT"/bin/clang-tidy
export LD_LIBRARY_PATH="$CLANG_ROOT"/lib:"$LD_LIBRARY_PATH"
# Tool path configuration
CLINLINER="$HOME"/CLPKM/inliner/clinliner
RENAME_LST_GEN="$HOME"/CLPKM/rename-lst-gen/rename-lst-gen
CLPKMCC="$HOME"/CLPKM/cc/clpkmcc
TOOLKIT="$HOME"/CLPKM/toolkit.cl
# Code cache
CACHE_DIR=/tmp/clpkm-code-cache
# Temp files
TMPBASE=/tmp/clpkm_"$BASHPID"_"$RANDOM"
ORIGINAL="$TMPBASE"/original.cl
PREPROCED="$TMPBASE"/preproced.cl
INLINED="$TMPBASE"/inlined.cl
RENAME_LST="$TMPBASE"/rename.yaml
RENAMED="$TMPBASE"/renamed.cl
INSTRED="$TMPBASE"/instr.cl
PROFLIST="$TMPBASE"/profile.yaml
CCLOG="$TMPBASE"/log.txt
# Step 0
# Read source code from stdin
mkdir -p "$TMPBASE" "$CACHE_DIR"
cat > "$ORIGINAL"
print_banner 'Options' > "$CCLOG"
echo "'$@'" >> "$CCLOG"
# Step 1
# Macro expansion
print_banner 'Preprocess stage' >> "$CCLOG"
# NOTE: Preprocess in advance here may break things!
# Please have a look at OpenCL 1.2 §6.10
"$CLANG" "$ORIGINAL" -E -P -std=cl1.2 $@ 1> "$PREPROCED" 2>> "$CCLOG"
# Failed
if [ ! "$?" -eq 0 ]; then
dump_diag "$CCLOG"
exit 1
fi
# Lookup code cache
# Don't use two SHA-512 checksum because most filesystems have a 255-byte limit
# on filenames
# Note: chance of collision! (very rare tho)
SRC_HASH=$(sha512sum "$PREPROCED" | cut -d " " -f 1)
OPT_HASH=$(echo "$@" | sha384sum | cut -d " " -f 1)
CACHE_BASE="$CACHE_DIR"/"$SRC_HASH"-"$OPT_HASH"
if [ -f "$CACHE_BASE".cl ] && [ -f "$CACHE_BASE".yaml ]; then
cat "$TOOLKIT" "$CACHE_BASE".cl
cat "$CACHE_BASE".yaml 1>&2
rm -rf "$TMPBASE"
exit
fi
prettify "$PREPROCED" && \
"$CLANG_TIDY" "$PREPROCED" -format-style=llvm -fix \
-checks="readability-braces-around-statements" -- -include clc/clc.h \
-std=cl1.2 $@ 1>> "$CCLOG" 2>&1
# Failed
if [ ! "$?" -eq 0 ]; then
dump_diag "$CCLOG"
exit 1
fi
# Step 2
# Renaming
print_banner 'Rename stage' >> "$CCLOG"
"$RENAME_LST_GEN" "$PREPROCED" \
-- -include clc/clc.h -std=cl1.2 $@ \
1> "$RENAME_LST" 2>> "$CCLOG"
# Failed
if [ ! "$?" -eq 0 ]; then
dump_diag "$CCLOG"
exit 1
fi
# Don't do shit if it gens nothin'
if [ "$(stat -c '%s' "$RENAME_LST")" -gt 0 ]; then
"$CLANG_RENAME" -input "$RENAME_LST" "$PREPROCED" \
-- -include clc/clc.h -std=cl1.2 $@ \
1> "$RENAMED" 2>> "$CCLOG"
# Failed
if [ ! "$?" -eq 0 ]; then
dump_diag "$CCLOG"
exit 1
fi
else
cp "$PREPROCED" "$RENAMED"
fi
# Step 3
# Inlining
print_banner 'Inline stage' >> "$CCLOG"
"$CLINLINER" "$RENAMED" \
-- -include clc/clc.h -std=cl1.2 $@ \
1> "$INLINED" 2>> "$CCLOG"
# Failed
if [ ! "$?" -eq 0 ]; then
dump_diag "$CCLOG"
exit 1
fi
# Step 4
# Invoke CLPKMCC
print_banner 'Instrument stage' >> "$CCLOG"
"$CLPKMCC" "$INLINED" \
--source-output="$INSTRED" --profile-output="$PROFLIST" \
-- -include clc/clc.h -std=cl1.2 $@ \
1> /dev/null 2>> "$CCLOG"
# Failed
if [ ! "$?" -eq 0 ]; then
dump_diag "$CCLOG"
exit 1
# Succeed
else
prettify "$INSTRED"
# Cache the result
cp "$INSTRED" "$CACHE_BASE".cl
cp "$PROFLIST" "$CACHE_BASE".yaml
cat "$TOOLKIT" "$INSTRED"
cat "$PROFLIST" 1>&2
fi
# Stage 4
# Clean up
rm -rf "$TMPBASE"