Skip to content

Commit 1d0356a

Browse files
committed
refactor new event as template
1 parent 4431b20 commit 1d0356a

File tree

3 files changed

+133
-93
lines changed

3 files changed

+133
-93
lines changed

include/kllvm/codegen/ProofEvent.h

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
#include "kllvm/ast/AST.h"
55
#include "kllvm/codegen/Decision.h"
66
#include "kllvm/codegen/DecisionParser.h"
7+
#include "kllvm/codegen/Options.h"
78
#include "kllvm/codegen/Util.h"
89

910
#include "llvm/IR/Instructions.h"
1011

12+
#include <fmt/format.h>
13+
1114
#include <map>
1215
#include <tuple>
1316

@@ -21,31 +24,58 @@ class proof_event {
2124

2225
/*
2326
* Load the boolean flag that controls whether proof hint output is enabled or
24-
* not, then create a branch at the end of this basic block depending on the
25-
* result.
27+
* not, then create a branch at the specified location depending on the
28+
* result. The location can be before a given instruction or at the end of a
29+
* given basic block.
2630
*
2731
* Returns a pair of blocks [proof enabled, merge]; the first of these is
2832
* intended for self-contained behaviour only relevant in proof output mode,
2933
* while the second is for the continuation of the interpreter's previous
3034
* behaviour.
3135
*/
36+
template <typename Location>
3237
std::pair<llvm::BasicBlock *, llvm::BasicBlock *>
33-
proof_branch(std::string const &label, llvm::BasicBlock *insert_at_end);
34-
std::pair<llvm::BasicBlock *, llvm::BasicBlock *>
35-
proof_branch(std::string const &label, llvm::Instruction *insert_before);
38+
proof_branch(std::string const &label, Location *insert_loc);
39+
40+
/*
41+
* Return the parent function of the given location.
42+
43+
* Template specializations for llvm::Instruction and llvm::BasicBlock.
44+
*/
45+
template <typename Location>
46+
llvm::Function *get_parent_function(Location *loc);
47+
48+
/*
49+
* Return the parent basic block of the given location.
50+
51+
* Template specializations for llvm::Instruction and llvm::BasicBlock.
52+
*/
53+
template <typename Location>
54+
llvm::BasicBlock *get_parent_block(Location *loc);
55+
56+
/*
57+
* If the given location is an Instruction, this method moves the instruction
58+
* to the merge block.
59+
* If the given location is a BasicBlock, this method simply emits a no-op
60+
* instruction to the merge block.
61+
62+
* Template specializations for llvm::Instruction and llvm::BasicBlock.
63+
*/
64+
template <typename Location>
65+
void fix_insert_loc(Location *loc, llvm::BasicBlock *merge_block);
3666

3767
/*
3868
* Set up a standard event prelude by creating a pair of basic blocks for the
3969
* proof output and continuation, then loading the output filename from its
40-
* global.
70+
* global. The location for the prelude can be before a given instruction or
71+
* at the end of a given basic block.
4172
*
4273
* Returns a triple [proof enabled, merge, proof_writer]; see `proofBranch`
4374
* and `emitGetOutputFileName`.
4475
*/
76+
template <typename Location>
4577
std::tuple<llvm::BasicBlock *, llvm::BasicBlock *, llvm::Value *>
46-
event_prelude(std::string const &label, llvm::BasicBlock *insert_at_end);
47-
std::tuple<llvm::BasicBlock *, llvm::BasicBlock *, llvm::Value *>
48-
event_prelude(std::string const &label, llvm::Instruction *insert_before);
78+
event_prelude(std::string const &label, Location *insert_loc);
4979

5080
/*
5181
* Set up a check of whether a new proof hint chunk should be started. The
@@ -239,9 +269,9 @@ class proof_event {
239269
[[nodiscard]] llvm::BasicBlock *pattern_matching_failure(
240270
kore_composite_pattern const &pattern, llvm::BasicBlock *current_block);
241271

242-
[[nodiscard]] llvm::BasicBlock *function_exit(
243-
uint64_t ordinal, bool is_tail, llvm::Instruction *insert_before,
244-
llvm::BasicBlock *current_block);
272+
template <typename Location>
273+
[[nodiscard]] llvm::BasicBlock *
274+
function_exit(uint64_t ordinal, bool is_tail, Location *insert_loc);
245275

246276
proof_event(kore_definition *definition, llvm::Module *module)
247277
: definition_(definition)
@@ -251,4 +281,57 @@ class proof_event {
251281

252282
} // namespace kllvm
253283

284+
//===----------------------------------------------------------------------===//
285+
// Implementation for method templates
286+
//===----------------------------------------------------------------------===//
287+
288+
template <typename Location>
289+
std::pair<llvm::BasicBlock *, llvm::BasicBlock *>
290+
kllvm::proof_event::proof_branch(
291+
std::string const &label, Location *insert_loc) {
292+
auto *i1_ty = llvm::Type::getInt1Ty(ctx_);
293+
294+
auto *proof_output_flag = module_->getOrInsertGlobal("proof_output", i1_ty);
295+
auto *proof_output = new llvm::LoadInst(
296+
i1_ty, proof_output_flag, "proof_output", insert_loc);
297+
298+
auto *f = get_parent_function(insert_loc);
299+
auto *true_block
300+
= llvm::BasicBlock::Create(ctx_, fmt::format("if_{}", label), f);
301+
auto *merge_block
302+
= llvm::BasicBlock::Create(ctx_, fmt::format("tail_{}", label), f);
303+
304+
llvm::BranchInst::Create(true_block, merge_block, proof_output, insert_loc);
305+
306+
fix_insert_loc(insert_loc, merge_block);
307+
308+
return {true_block, merge_block};
309+
}
310+
311+
template <typename Location>
312+
std::tuple<llvm::BasicBlock *, llvm::BasicBlock *, llvm::Value *>
313+
kllvm::proof_event::event_prelude(
314+
std::string const &label, Location *insert_loc) {
315+
auto [true_block, merge_block] = proof_branch(label, insert_loc);
316+
return {true_block, merge_block, emit_get_proof_trace_writer(true_block)};
317+
}
318+
319+
template <typename Location>
320+
llvm::BasicBlock *kllvm::proof_event::function_exit(
321+
uint64_t ordinal, bool is_tail, Location *insert_loc) {
322+
323+
if (!proof_hint_instrumentation) {
324+
return get_parent_block(insert_loc);
325+
}
326+
327+
auto [true_block, merge_block, proof_writer]
328+
= event_prelude("function_exit", insert_loc);
329+
330+
emit_write_function_exit(proof_writer, ordinal, is_tail, true_block);
331+
332+
llvm::BranchInst::Create(merge_block, true_block);
333+
334+
return merge_block;
335+
}
336+
254337
#endif // PROOF_EVENT_H

lib/codegen/CreateTerm.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,20 +1295,19 @@ bool make_function(
12951295
if (is_apply_rule) {
12961296
current_block
12971297
= proof_event(definition, module)
1298-
.function_exit(ordinal, true, call, current_block);
1298+
.function_exit(
1299+
ordinal, true, llvm::dyn_cast<llvm::Instruction>(call));
12991300
}
13001301
} else {
13011302
if (is_apply_rule) {
1302-
current_block
1303-
= proof_event(definition, module)
1304-
.function_exit(ordinal, false, nullptr, current_block);
1303+
current_block = proof_event(definition, module)
1304+
.function_exit(ordinal, false, current_block);
13051305
}
13061306
}
13071307
} else {
13081308
if (is_apply_rule) {
1309-
current_block
1310-
= proof_event(definition, module)
1311-
.function_exit(ordinal, false, nullptr, current_block);
1309+
current_block = proof_event(definition, module)
1310+
.function_exit(ordinal, false, current_block);
13121311
}
13131312
}
13141313
}

lib/codegen/ProofEvent.cpp

Lines changed: 32 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -372,63 +372,6 @@ proof_event::emit_get_proof_chunk_size(llvm::BasicBlock *insert_at_end) {
372372
i64_ty, proof_chunk_size_pointer, "proof_chunk_size", insert_at_end);
373373
}
374374

375-
std::pair<llvm::BasicBlock *, llvm::BasicBlock *> proof_event::proof_branch(
376-
std::string const &label, llvm::BasicBlock *insert_at_end) {
377-
auto *i1_ty = llvm::Type::getInt1Ty(ctx_);
378-
379-
auto *proof_output_flag = module_->getOrInsertGlobal("proof_output", i1_ty);
380-
auto *proof_output = new llvm::LoadInst(
381-
i1_ty, proof_output_flag, "proof_output", insert_at_end);
382-
383-
auto *f = insert_at_end->getParent();
384-
auto *true_block
385-
= llvm::BasicBlock::Create(ctx_, fmt::format("if_{}", label), f);
386-
auto *merge_block
387-
= llvm::BasicBlock::Create(ctx_, fmt::format("tail_{}", label), f);
388-
389-
emit_no_op(merge_block);
390-
391-
llvm::BranchInst::Create(
392-
true_block, merge_block, proof_output, insert_at_end);
393-
return {true_block, merge_block};
394-
}
395-
396-
std::pair<llvm::BasicBlock *, llvm::BasicBlock *> proof_event::proof_branch(
397-
std::string const &label, llvm::Instruction *insert_before) {
398-
auto *i1_ty = llvm::Type::getInt1Ty(ctx_);
399-
400-
auto *proof_output_flag = module_->getOrInsertGlobal("proof_output", i1_ty);
401-
auto *proof_output = new llvm::LoadInst(
402-
i1_ty, proof_output_flag, "proof_output", insert_before);
403-
404-
auto *f = insert_before->getParent()->getParent();
405-
auto *true_block
406-
= llvm::BasicBlock::Create(ctx_, fmt::format("if_{}", label), f);
407-
auto *merge_block
408-
= llvm::BasicBlock::Create(ctx_, fmt::format("tail_{}", label), f);
409-
410-
llvm::BranchInst::Create(
411-
true_block, merge_block, proof_output, insert_before);
412-
413-
insert_before->moveBefore(*merge_block, merge_block->begin());
414-
415-
return {true_block, merge_block};
416-
}
417-
418-
std::tuple<llvm::BasicBlock *, llvm::BasicBlock *, llvm::Value *>
419-
proof_event::event_prelude(
420-
std::string const &label, llvm::BasicBlock *insert_at_end) {
421-
auto [true_block, merge_block] = proof_branch(label, insert_at_end);
422-
return {true_block, merge_block, emit_get_proof_trace_writer(true_block)};
423-
}
424-
425-
std::tuple<llvm::BasicBlock *, llvm::BasicBlock *, llvm::Value *>
426-
proof_event::event_prelude(
427-
std::string const &label, llvm::Instruction *insert_before) {
428-
auto [true_block, merge_block] = proof_branch(label, insert_before);
429-
return {true_block, merge_block, emit_get_proof_trace_writer(true_block)};
430-
}
431-
432375
llvm::BasicBlock *proof_event::check_for_emit_new_chunk(
433376
llvm::BasicBlock *insert_at_end, llvm::BasicBlock *merge_block) {
434377
auto *f = insert_at_end->getParent();
@@ -745,29 +688,44 @@ llvm::BasicBlock *proof_event::pattern_matching_failure(
745688
return merge_block;
746689
}
747690

748-
llvm::BasicBlock *proof_event::function_exit(
749-
uint64_t ordinal, bool is_tail, llvm::Instruction *insert_before,
750-
llvm::BasicBlock *current_block) {
691+
//===----------------------------------------------------------------------===//
692+
// Method template specializations
693+
//===----------------------------------------------------------------------===//
751694

752-
if (!proof_hint_instrumentation) {
753-
return current_block;
754-
}
695+
template <>
696+
llvm::Function *kllvm::proof_event::get_parent_function<llvm::Instruction>(
697+
llvm::Instruction *loc) {
698+
return loc->getParent()->getParent();
699+
}
755700

756-
std::tuple<llvm::BasicBlock *, llvm::BasicBlock *, llvm::Value *> prelude;
757-
if (is_tail) {
758-
assert(insert_before);
759-
prelude = event_prelude("function_exit", insert_before);
760-
} else {
761-
prelude = event_prelude("function_exit", current_block);
762-
}
701+
template <>
702+
llvm::Function *kllvm::proof_event::get_parent_function<llvm::BasicBlock>(
703+
llvm::BasicBlock *loc) {
704+
return loc->getParent();
705+
}
763706

764-
auto [true_block, merge_block, proof_writer] = prelude;
707+
template <>
708+
llvm::BasicBlock *kllvm::proof_event::get_parent_block<llvm::Instruction>(
709+
llvm::Instruction *loc) {
710+
return loc->getParent();
711+
}
765712

766-
emit_write_function_exit(proof_writer, ordinal, is_tail, true_block);
713+
template <>
714+
llvm::BasicBlock *
715+
kllvm::proof_event::get_parent_block<llvm::BasicBlock>(llvm::BasicBlock *loc) {
716+
return loc;
717+
}
767718

768-
llvm::BranchInst::Create(merge_block, true_block);
719+
template <>
720+
void kllvm::proof_event::fix_insert_loc<llvm::Instruction>(
721+
llvm::Instruction *loc, llvm::BasicBlock *merge_block) {
722+
loc->moveBefore(*merge_block, merge_block->begin());
723+
}
769724

770-
return merge_block;
725+
template <>
726+
void kllvm::proof_event::fix_insert_loc<llvm::BasicBlock>(
727+
llvm::BasicBlock *loc, llvm::BasicBlock *merge_block) {
728+
emit_no_op(merge_block);
771729
}
772730

773731
} // namespace kllvm

0 commit comments

Comments
 (0)