-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtypeart-tmpl.sh.in
More file actions
297 lines (266 loc) · 7.45 KB
/
typeart-tmpl.sh.in
File metadata and controls
297 lines (266 loc) · 7.45 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/bin/bash
#
# TypeART library
#
# Copyright (c) 2017-2025 TypeART Authors
# Distributed under the BSD 3-Clause license.
# (See accompanying file LICENSE.txt or copy at
# https://opensource.org/licenses/BSD-3-Clause)
#
# Project home: https://github.com/tudasc/TypeART
#
# SPDX-License-Identifier: BSD-3-Clause
#
function parse_cmd_line() {
readonly source_file=$1
optimize=""
ta_more_args=""
with_omp=0
with_intercept=0
with_thread=0
skip_typeart=0
compile_flags=""
show_ir=0
clean_types_file=0
shift # skip over $1
while (("$#")); do
case "$1" in
-o | --optimization)
if [ -n "$2" ] && [ "${2:0:2}" == "-O" ]; then
optimize=$2
shift 2
else
echo "Error: Optimization argument for $1 is erroneous: $2" >&2
exit 1
fi
;;
--omp)
with_omp=1
shift
;;
--thread)
with_thread=1
shift
;;
--mpi_intercept)
with_intercept=1
shift
;;
--manual)
skip_typeart=1
shift
;;
--object)
if [ -n "$2" ]; then
object_file="$2"
shift 2
else
echo "Error: Object file argument for $1 is missing." >&2
shift
fi
;;
--executable)
if [ -n "$2" ]; then
exe_file="$2"
shift 2
else
echo "Error: Executable file argument for $1 is missing." >&2
shift
fi
;;
--compile_flags)
if [ -n "$2" ]; then
compile_flags="$2"
shift 2
else
echo "Error: argument for $1 is missing." >&2
shift
fi
;;
--show_ir)
show_ir=1
shift
;;
--clean_types)
clean_types_file=1
shift
;;
*) # preserve other arguments
ta_more_args="$ta_more_args $1"
shift
;;
esac
done
if [ -z "${object_file}" ]; then
# use filename(source_file).o
object_file="${source_file%.*}".o
fi
if [ -z "${exe_file}" ]; then
# use filename(source_file).o
exe_file="${source_file%.*}".exe
fi
if [ -z "${compile_flags}" ]; then
compile_flags=""
fi
if [ $show_ir == 1 ]; then
ta_more_args+=" -S"
fi
}
function global_init() {
local -r typeart_use_rel_path=@TYPEART_RELOCATABLE@
if [ "$typeart_use_rel_path" == 0 ]; then
local -r typeart_bin_dir="@TYPEART_BINARY_DIR@"
local -r typeart_lib_dir="@TYPEART_RT_DIR@"
local -r typeart_include_dir="@TYPEART_INCLUDE_DIRS@"
local -r typeart_pass="@TYPEART_PASS_DIR@/$<TARGET_FILE_NAME:typeart::TransformPass>"
readonly typeart_interceptor="@TYPEART_MPI_INTERCEPT_DIR@/@TYPEART_MPI_TOOL@"
else
# shellcheck disable=SC2155
local -r typeart_bin_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC2155
local -r typeart_install_dir="$(dirname "${typeart_bin_dir}")"
local -r typeart_lib_dir="${typeart_install_dir}/@CMAKE_INSTALL_LIBDIR@"
local -r typeart_include_dir="-I${typeart_install_dir}/@CMAKE_INSTALL_INCLUDEDIR@/@PROJECT_NAME@"
local -r typeart_pass="${typeart_lib_dir}/$<TARGET_FILE_NAME:typeart::TransformPass>"
readonly typeart_interceptor="@TYPEART_MPI_INTERCEPT_DIR@/@TYPEART_MPI_TOOL@"
fi
readonly typeart_has_interceptor="@TYPEART_HAS_MPI_TOOL@"
readonly opt_tool="@TYPEART_OPT@"
readonly llc_tool="@TYPEART_LLC@"
readonly typeart_includes="${typeart_include_dir}"
readonly typeart_ldflags="-L${typeart_lib_dir}/ \
-Wl,-rpath,${typeart_lib_dir}/ \
-l$<TARGET_FILE_BASE_NAME:typeart::Runtime>"
# shellcheck disable=SC2027
typeart_plugin="-load-pass-plugin "${typeart_pass}" -passes=typeart<"
case "${TYPEART_TYPEGEN_IR}" in
on | ON | 1 | true | TRUE)
typeart_plugin+="typegen=ir;"
;;
esac
readonly typeart_stack_mode_args="no-heap;stack;stats>"
readonly typeart_heap_mode_args="heap;stats>"
readonly typeart_combined_mode_args="heap;stats;stack>"
}
function toolchain_init() {
readonly extension="${source_file##*.}"
case "$extension" in
c)
compiler="@TYPEART_CLANG_EXEC@"
if [ $with_omp == 1 ]; then
readonly omp_flags="@OpenMP_C_FLAGS@" # -I@OpenMP_C_INCLUDE_DIRS@
fi
;;
cc | cxx | cpp)
compiler="@TYPEART_CLANGCXX_EXEC@"
if [ $with_omp == 1 ]; then
readonly omp_flags="@OpenMP_CXX_FLAGS@" # -I@OpenMP_CXX_INCLUDE_DIRS@
fi
;;
esac
if [ $with_thread == 1 ]; then
readonly threads_flags="@CMAKE_THREAD_LIBS_INIT@"
fi
readonly typeart_san_flags="@TYPEART_SAN_FLAGS@"
if [ -e "typeart-types.yaml" ]; then
rm "typeart-types.yaml"
fi
}
function source_to_llvm() {
# flag errors with implicit conversions when changing API
local error_flags="-Werror=incompatible-pointer-types"
$compiler ${error_flags} ${omp_flags} ${typeart_includes} ${typeart_san_flags} \
-O1 -g ${compile_flags} -Xclang -disable-llvm-passes -S -emit-llvm "${source_file}" -o -
}
function apply_typeart() {
ta_more_args+=" -fPIC"
source_to_llvm |
$opt_tool ${typeart_plugin} ${typeart_combined_mode_args} ${ta_more_args}
}
function apply_typeart_optim() {
source_to_llvm |
$opt_tool ${typeart_plugin}${typeart_heap_mode_args} ${ta_more_args} |
$opt_tool ${optimize} -S |
$opt_tool ${typeart_plugin}${typeart_stack_mode_args} ${ta_more_args}
}
function make_no_optim() {
# Order: heap and stack/global together, no optimization pass
if [ $skip_typeart == 0 ]; then
if [ $show_ir == 0 ]; then
apply_typeart | $llc_tool -x=ir ${llc_flags} -o "${object_file}"
else
apply_typeart
fi
else
# First create typeart-types.yaml:
apply_typeart >/dev/null
if [ $show_ir == 0 ]; then
source_to_llvm | $llc_tool -x=ir ${llc_flags} -o "${object_file}"
else
source_to_llvm
fi
fi
}
function make_with_optim() {
# Order: heap, optimize, alloca with additional args.
if [ $skip_typeart == 0 ]; then
if [ $show_ir == 0 ]; then
apply_typeart_optim | $llc_tool -x=ir ${llc_flags} -o "${object_file}"
else
apply_typeart_optim
fi
else
# First create typeart-types.yaml:
apply_typeart_optim >/dev/null
if [ $show_ir == 0 ]; then
source_to_llvm | $opt_tool ${optimize} | $llc_tool -x=ir ${llc_flags} -o "${object_file}"
else
source_to_llvm | $opt_tool ${optimize}
fi
fi
}
function compile() {
local llc_flags="--filetype=obj --relocation-model=pic"
if [ "$optimize" == " " ]; then
make_no_optim
else
make_with_optim
fi
}
function main_link() {
more_link_flags="-fPIC"
$compiler ${more_link_flags} ${omp_flags} ${threads_flags} ${typeart_san_flags} ${typeart_ldflags} "${object_file}" -o "${exe_file}"
}
function execute() {
if [ $clean_types_file == 1 ]; then
if [ -e "typeart-types.yaml" ]; then
rm "typeart-types.yaml"
fi
fi
if [ $with_omp == 1 ]; then
export TSAN_OPTIONS='ignore_noninstrumented_modules=1'
fi
if [ $with_intercept == 1 ]; then
if [ $typeart_has_interceptor == 1 ]; then
echo -e Executing with runtime and interceptor lib
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${typeart_lib_dir}" LD_PRELOAD="${typeart_interceptor}" "${exe_file}"
else
echo -e Interceptor lib not compiled with TypeART, exiting!
exit 1
fi
else
echo -e Executing with runtime lib
LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${typeart_lib_dir}" "${exe_file}"
fi
}
function main_in() {
parse_cmd_line "$@"
global_init
toolchain_init
compile
if [ @TYPEART_RUN_SCRIPT@ == 1 -a $show_ir == 0 ]; then
main_link
execute
fi
}
main_in "$@"