-
Notifications
You must be signed in to change notification settings - Fork 0
/
plorc
executable file
·196 lines (156 loc) · 6.04 KB
/
plorc
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
196
#!/bin/bash
#
# Copyright (C) 2008 Uday Bondhugula
#
# Available under GNU GPL v2 or (at your option) any later version
# See file COPYING for details
#
# Automatic annotation generation for unrolling
# Unroll/jammable loops are detected in the transformation
# framework. The purpose of this script is to find the portion
# of the AST to insert annotations for Orio. For perfect nests,
# this is trivial, but for tiled imperfect nests, a perfect nest
# comes out and the annotation needs to be inserted there
#
# Syntax: plorc <file name to insert annotation> <orio dir>
#
# This script is called from polycc
#
match_closing_brace() {
grep -n [{}] $1 > loopopenclose
sed -n -e '/^'$2':/,$p' loopopenclose > cut
open=0
numclose=0
while read -n1 char; do
if [ "$char" == "{" ]; then
open=$(($open+1))
elif [ "$char" == "}" ]; then
numclose=$(($numclose+1))
open=$(($open-1))
if [ $open == 0 ]; then
break
fi
fi
done <cut
cat cut | grep "}" > closecut
closing_brace_linenum=`sed -n -e ''$numclose'p' closecut | awk '{split($1,t,":"); print t[1]}'`
rm -f cut loopopenclose closecut
return $closing_brace_linenum
}
if [ ! -f .unroll ]; then
exit 2
fi
numloops=`wc -l .unroll | awk '{print $1}'`
if [ $numloops == 0 ]; then
exit 3
fi
# Generate the unrolling annotation
# unroll only two
unrolldim1=`sed -n -e '1p' .unroll | awk '{print $1}'`
unrolldim2=`sed -n -e '2p' .unroll | awk '{print $1}'`
unrollcmd1=`sed -n -e '1p' .unroll | awk '{print $2}'`
unrollcmd2=`sed -n -e '2p' .unroll | awk '{print $2}'`
ufactor1=`sed -n -e '1p' .unroll | awk '{print $3}'`
ufactor2=`sed -n -e '2p' .unroll | awk '{print $3}'`
if [ $numloops -ne 1 ]; then
# 2 loops to unroll
dirname=`dirname $1`
# Find the unrollable hotspot with $unrolldim1 and $unrolldim2
params=""
for i in `cat .params`; do params=$i\\\|$params; done
params=$params"phony"
num=""
# Don't even bother to ask what this is - the lowest levels I have stooped to
for i in `grep -n "for ($unrolldim1=" $1 | grep -v $params | awk '{split($1,t,":"); print t[1]}'` ; do grep -n ".*" $1 | sed -n -e \
''$(($i+1))'p' | grep "for ($unrolldim2=" | grep -q -v "$params" ; if [ $? == "0" ]; then num=$i; break; fi
done
# If no loops with bounds free of parameters are found, just go for those
# with params in them (for the first unrolldim and then for the second)
if [ "$num" == "" ]; then
for i in `grep -n "for ($unrolldim1=" $1 | awk '{split($1,t,":"); print t[1]}'` ; do grep -n ".*" $1 | sed -n -e \
''$(($i+1))'p' | grep "for ($unrolldim2=" | grep -q -v "$params" ; if [ $? == "0" ]; then num=$i; break; fi
done
# Now, both with params
if [ "$num" == "" ]; then
for i in `grep -n "for ($unrolldim1=" $1 | awk '{split($1,t,":"); print t[1]}'` ; do grep -n ".*" $1 | sed -n -e \
''$(($i+1))'p' | grep -q "for ($unrolldim2=" ; if [ $? == "0" ]; then num=$i; break; fi
done
fi
fi
if [ "$num" == "" ]; then
exit 4
fi
#echo "[Plorc] Found unrollable nest at $num"
echo "[Plorc] Found unroll-jammable nest"
match_closing_brace $1 $num
annotation_end_line_num=$?
sed -n -e '1,'$(($num-1))'p' $1 > .header
echo "{" > .unrollbody
sed -n -e ''$(($num+2))','$(($annotation_end_line_num-2))'p' $1 >> .unrollbody
echo "}" >> .unrollbody
#cat .unrollbody
sed -n -e ''$num'p' $1 | tr -d "{" > .unroll1
sed -n -e ''$(($num+1))'p' $1 | tr -d "{" > .unroll2
echo "/*@ begin Loop(" > .annotation
echo -e "\ttransform RegTile(loops=['$unrolldim1','$unrolldim2'], ufactors=[$ufactor1,$ufactor2])" >> .annotation
cat .unroll1 >> .annotation
cat .unroll2 >> .annotation
cat .unrollbody >> .annotation
echo ") @*/" >> .annotation
echo "/*@ end @*/" > .annotation_end
sed -n -e ''$(($num+1))'p' $1 | tr -d "{" > .unroll2
sed -n -e ''$num','$annotation_end_line_num'p' $1 > .middle
sed -n -e ''$(($annotation_end_line_num+1))',$p' $1 > .footer
else
# only 1 loops to unroll
dirname=`dirname $1`
# find the unrollable loop(s)
params=""
for i in `sed -n 1~2p .params`; do params=$i\\\|$params; done
params=$params"phony"
num=""
# don't even bother to ask what this is - the lowest levels I have stooped to
num=`grep -n "for ($unrolldim1=" $1 | grep -v $params | awk '{split($1,t,":"); print t[1]}' | head -n 1`
# if no loops with bounds free of parameters are found, just go for those
# with params in them
if [ "$num" == "" ]; then
num=`grep -n "for ($unrolldim1=" $1 | awk '{split($1,t,":"); print t[1]}' | head -n 1`
fi
if [ "$num" == "" ]; then
exit 4
fi
# echo "[Plorc] Found unroll-jammable nest at $num"
echo "[Plorc] Found unroll-jammable nest"
match_closing_brace $1 $num
annotation_end_line_num=$?
sed -n -e '1,'$(($num-1))'p' $1 > .header
echo "{" > .unrollbody
sed -n -e ''$(($num+1))','$(($annotation_end_line_num-1))'p' $1 >> .unrollbody
echo "}" >> .unrollbody
#cat .unrollbody
sed -n -e ''$num'p' $1 | tr -d "{" > .unroll1
echo "/*@ begin Loop(" > .annotation
echo "transform $unrollcmd1(ufactor=$ufactor1)" >> .annotation
cat .unroll1 >> .annotation
cat .unrollbody >> .annotation
echo ") @*/" >> .annotation
echo "/*@ end @*/" > .annotation_end
sed -n -e ''$num','$annotation_end_line_num'p' $1 > .middle
sed -n -e ''$(($annotation_end_line_num+1))',$p' $1 > .footer
fi
cat .header .annotation .middle .annotation_end .footer > $1
# run the annotation generator
export ORCC_DIR=$2
ORCC="$ORCC_DIR/orcc"
# $1 is file.pluto.c
$ORCC -o $1.orcc $1
rc=$?
if [ "$rc" -eq "0" ]; then
cp -f $1 .orcc
mv -f $1.orcc $1
echo "[Plorc] Finished running Orio"
else
echo "[Plorc] Failed to run Orio"
fi
rm -f .header .lb .ub .header .footer .vecloop .middle .footer .annotation .annotation_end .unroll1 .unroll2 .unrollbody
exit 0