Skip to content

Commit 22c9792

Browse files
author
Dwight Guth
authored
Verify ir during CI (#1145)
I noticed to my dismay that a recent PR had introduced code that passed tests, but was generating LLVM IR that did not pass the verification process. This PR does two things: 1. Fixes the bug in the code generator that was leading to code that did not verify. 2. Introduces code to the regression test suite to test that every definition we compile generates IR that is valid.
1 parent dafe4ec commit 22c9792

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

bin/llvm-kompile

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Options:
4444
"hidden"
4545
--profile-matching Instrument interpeter to emit a profile of time spent in
4646
top-level rule matching on stderr.
47+
--verify-ir Verify result of IR generation.
4748
-O[0123] Set the optimization level for code generation.
4849
4950
Any option not listed above will be passed through to clang; use '--' to
@@ -89,6 +90,7 @@ llvm_major_version () {
8990
positional_args=()
9091
reading_clang_args=false
9192
codegen_flags=()
93+
codegen_verify_flags=()
9294
if [[ -z "${NIX_LLVM_KOMPILE_LIBS}" ]]; then
9395
kompile_clang_flags=()
9496
else
@@ -105,6 +107,7 @@ embedded_files=()
105107
embedded_names=()
106108
use_opt=false
107109
emit_ir=false
110+
verify_ir=false
108111
frame_pointer=false
109112

110113
export verbose=false
@@ -175,23 +178,32 @@ while [[ $# -gt 0 ]]; do
175178
;;
176179
--proof-hint-instrumentation)
177180
codegen_flags+=("--proof-hint-instrumentation")
181+
codegen_verify_flags+=("--proof-hint-instrumentation")
178182
shift
179183
;;
180184
--proof-hint-instrumentation-slow)
181185
codegen_flags+=("--proof-hint-instrumentation-slow")
186+
codegen_verify_flags+=("--proof-hint-instrumentation-slow")
182187
shift
183188
;;
184189
--mutable-bytes)
185190
codegen_flags+=("--mutable-bytes")
191+
codegen_verify_flags+=("--mutable-bytes")
186192
shift
187193
;;
188194
--hidden-visibility)
189195
codegen_flags+=("--hidden-visibility")
196+
codegen_verify_flags+=("--hidden-visibility")
190197
kompile_clang_flags+=("--hidden-visibility")
191198
shift
192199
;;
193200
--profile-matching)
194201
codegen_flags+=("--profile-matching")
202+
codegen_verify_flags+=("--profile-matching")
203+
shift
204+
;;
205+
--verify-ir)
206+
verify_ir=true
195207
shift
196208
;;
197209
-O*)
@@ -236,17 +248,19 @@ if [[ "$emit_ir" == "false" ]]; then
236248
codegen_flags+=("--emit-object")
237249
if [[ "$frame_pointer" == "true" ]]; then
238250
codegen_flags+=("-fno-omit-frame-pointer")
251+
codegen_verify_flags+=("-fno-omit-frame-pointer")
239252
fi
240253
else
241254
codegen_flags+=("--binary-ir")
242255
fi
243256

244257
mod="$(mktemp tmp.XXXXXXXXXX)"
258+
modtmp="$(mktemp tmp.XXXXXXXXXX)"
245259
modopt_tmp="$(mktemp tmp.XXXXXXXXXX)"
246260
tmpdir="$(mktemp -d tmp.XXXXXXXXXX)"
247261
modopt="$modopt_tmp"
248262
if [ "$save_temps" = false ]; then
249-
trap 'rm -rf "$mod" "$modopt_tmp" "$tmpdir"' INT TERM EXIT
263+
trap 'rm -rf "$mod" "$modtmp" "$modopt_tmp" "$tmpdir"' INT TERM EXIT
250264
fi
251265

252266
definition="${positional_args[0]}"
@@ -271,19 +285,27 @@ if [[ "$compile" = "default" ]]; then
271285
# flag.
272286
if [[ "$main" = "c" ]] || [[ "$main" = "python" ]]; then
273287
codegen_flags+=("--safe-partial")
288+
codegen_verify_flags+=("--safe-partial")
274289
fi
275290

276291
for arg in "${clang_args[@]}"; do
277292
case "$arg" in
278293
-g)
279294
codegen_flags+=("--debug")
295+
codegen_verify_flags+=("--debug")
280296
;;
281297
esac
282298
done
283299

284300
run "$(dirname "$0")"/llvm-kompile-codegen "${codegen_flags[@]}" \
285301
"$definition" "$dt_dir"/dt.yaml "$dt_dir" -o "$mod"
286302

303+
if [[ "$verify_ir" == "true" ]]; then
304+
run "$(dirname "$0")"/llvm-kompile-codegen "${codegen_verify_flags[@]}" \
305+
"$definition" "$dt_dir"/dt.yaml "$dt_dir" -o "$modtmp" --no-optimize
306+
run @OPT@ -passes=verify "$modtmp" -o /dev/null
307+
fi
308+
287309
if [[ "$use_opt" = "true" ]]; then
288310
if [ "$(llvm_major_version)" -ge "16" ]; then
289311
run @OPT@ -passes='mem2reg,tailcallelim' "$mod" -o "$modopt"

bin/llvm-kompile-testing

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ while [ $# -gt 0 ]; do
4141
shift
4242
done
4343

44-
llvm-kompile "$definition" "$dt_dir" "$mode" "${llvm_kompile_flags[@]}" "${clang_flags[@]}" -g
44+
llvm-kompile "$definition" "$dt_dir" "$mode" "${llvm_kompile_flags[@]}" "${clang_flags[@]}" -g --verify-ir

lib/codegen/CreateTerm.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ llvm::Value *create_term::create_hook(
730730

731731
// These do not short circuit when the first argument is true.
732732
current_block_ = e.short_circuit_hook_argument(
733-
args[1], args[0], false, sort_1, current_block_);
733+
result, args[0], false, sort_1, current_block_);
734734
} else if (name == "BOOL.or" || name == "BOOL.orElse") {
735735
auto const &p = pattern->get_arguments();
736736
auto *sort_0 = dynamic_cast<kore_composite_sort *>(p[0]->get_sort().get());
@@ -740,7 +740,7 @@ llvm::Value *create_term::create_hook(
740740

741741
// These do not short circuit when the first argument is false.
742742
current_block_ = e.short_circuit_hook_argument(
743-
args[1], args[0], true, sort_1, current_block_);
743+
result, args[0], true, sort_1, current_block_);
744744
} else if (name == "KEQUAL.ite") {
745745
auto const &p = pattern->get_arguments();
746746
auto *sort_0 = dynamic_cast<kore_composite_sort *>(p[0]->get_sort().get());
@@ -752,7 +752,7 @@ llvm::Value *create_term::create_hook(
752752
// The second argument does not short circuit when the first argument is true, while
753753
// the third argument does not short circuit when the first argument is false.
754754
current_block_ = e.short_circuit_hook_argument(
755-
args[1], args[2], args[0], sort_1, sort_2, current_block_);
755+
result, result, args[0], sort_1, sort_2, current_block_);
756756
} else {
757757
size_t i = 0;
758758
for (auto const &p : pattern->get_arguments()) {

0 commit comments

Comments
 (0)