Skip to content

Commit c00b6a7

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents bf51ee4 + f27fbd5 commit c00b6a7

File tree

14 files changed

+262
-47
lines changed

14 files changed

+262
-47
lines changed

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 --verify-ir
44+
llvm-kompile "$definition" "$dt_dir" "$mode" "${llvm_kompile_flags[@]}" --verify-ir "${clang_flags[@]}" -g

config/llvm_header.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ define i1 @string_equal(ptr %str1, ptr %str2, i64 %len1, i64 %len2) {
193193
194194
declare tailcc ptr @k_step(ptr)
195195
declare tailcc void @step_all(ptr)
196-
declare void @write_configuration_to_proof_trace(ptr, ptr)
196+
declare void @write_configuration_to_proof_trace(ptr, ptr, i1)
197197
198198
@proof_output = external global i1
199199
@proof_writer = external global ptr
@@ -231,7 +231,7 @@ define ptr @take_steps(i64 %depth, ptr %subject) {
231231
br i1 %proof_output, label %if, label %merge
232232
if:
233233
%proof_writer = load ptr, ptr @proof_writer
234-
call void @write_configuration_to_proof_trace(ptr %proof_writer, ptr %subject)
234+
call void @write_configuration_to_proof_trace(ptr %proof_writer, ptr %subject, i1 true)
235235
br label %merge
236236
merge:
237237
store i64 %depth, ptr @depth

include/kllvm/binary/ProofTraceParser.h

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ class proof_trace_parser {
323323
public:
324324
static constexpr uint32_t expected_version = 13U;
325325

326+
enum class trace_kind { Hint, PreTrace, Chunk };
327+
326328
private:
327329
bool verbose_;
328330
bool expand_terms_;
@@ -345,13 +347,24 @@ class proof_trace_parser {
345347
return result;
346348
}
347349

348-
static bool parse_header(proof_trace_buffer &buffer, uint32_t &version) {
350+
static bool parse_header(
351+
proof_trace_buffer &buffer, trace_kind &kind, uint32_t &version) {
349352
std::array<char, 4> magic{};
350353
if (!buffer.read(magic.data(), sizeof(magic))) {
351354
return false;
352355
}
353-
if (magic[0] != 'H' || magic[1] != 'I' || magic[2] != 'N'
354-
|| magic[3] != 'T') {
356+
if (magic[0] == 'H' && magic[1] == 'I' && magic[2] == 'N'
357+
&& magic[3] == 'T') {
358+
kind = trace_kind::Hint;
359+
} else if (
360+
magic[0] == 'P' && magic[1] == 'T' && magic[2] == 'R'
361+
&& magic[3] == 'C') {
362+
kind = trace_kind::PreTrace;
363+
} else if (
364+
magic[0] == 'C' && magic[1] == 'H' && magic[2] == 'N'
365+
&& magic[3] == 'K') {
366+
kind = trace_kind::Chunk;
367+
} else {
355368
return false;
356369
}
357370

@@ -650,27 +663,30 @@ class proof_trace_parser {
650663

651664
bool parse_trace(proof_trace_buffer &buffer, llvm_rewrite_trace &trace) {
652665
uint32_t version = 0;
653-
if (!parse_header(buffer, version)) {
666+
trace_kind kind = trace_kind::Hint;
667+
if (!parse_header(buffer, kind, version)) {
654668
return false;
655669
}
656670
trace.set_version(version);
657671

658-
while (buffer.has_word() && buffer.peek_word() != config_sentinel) {
659-
llvm_event event;
660-
if (!parse_event(buffer, event)) {
661-
return false;
672+
if (kind == trace_kind::Hint || kind == trace_kind::PreTrace) {
673+
while (buffer.has_word() && buffer.peek_word() != config_sentinel) {
674+
llvm_event event;
675+
if (!parse_event(buffer, event)) {
676+
return false;
677+
}
678+
trace.add_pre_trace_event(event);
662679
}
663-
trace.add_pre_trace_event(event);
664-
}
665680

666-
uint64_t pattern_len = 0;
667-
auto config = parse_config(buffer, pattern_len);
668-
if (!config) {
669-
return false;
681+
uint64_t pattern_len = 0;
682+
auto config = parse_config(buffer, pattern_len);
683+
if (!config) {
684+
return false;
685+
}
686+
llvm_event config_event;
687+
config_event.setkore_pattern(config, pattern_len);
688+
trace.set_initial_config(config_event);
670689
}
671-
llvm_event config_event;
672-
config_event.setkore_pattern(config, pattern_len);
673-
trace.set_initial_config(config_event);
674690

675691
while (!buffer.eof()) {
676692
llvm_event event;
@@ -699,6 +715,7 @@ class proof_trace_parser {
699715
class llvm_rewrite_trace_iterator {
700716
private:
701717
uint32_t version_{};
718+
proof_trace_parser::trace_kind kind_{};
702719
std::unique_ptr<proof_trace_buffer> buffer_;
703720
llvm_event_type type_ = llvm_event_type::PreTrace;
704721
proof_trace_parser parser_;

include/kllvm/codegen/ProofEvent.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ class proof_event {
4343
std::tuple<llvm::BasicBlock *, llvm::BasicBlock *, llvm::Value *>
4444
event_prelude(std::string const &label, llvm::BasicBlock *insert_at_end);
4545

46+
/*
47+
* Set up a check of whether a new proof hint chunk should be started. The
48+
* condition for that is
49+
* proof_chunk_size != 0 and steps % proof_chunk_size = 0
50+
* Returns a block intended for adding code that starts a new chunk. If the
51+
* condition is false, we branch to the given merge_block.
52+
*/
53+
llvm::BasicBlock *check_for_emit_new_chunk(
54+
llvm::BasicBlock *insert_at_end, llvm::BasicBlock *merge_block);
55+
4656
/*
4757
* Emit an instruction that has no effect and will be removed by optimization
4858
* passes.
@@ -60,6 +70,19 @@ class proof_event {
6070
*/
6171
llvm::LoadInst *emit_get_proof_trace_writer(llvm::BasicBlock *insert_at_end);
6272

73+
/*
74+
* Emit instructions to get the current value of the steps global variable,
75+
* which counts the number of rewrite steps taken.
76+
*/
77+
llvm::LoadInst *emit_get_steps(llvm::BasicBlock *insert_at_end);
78+
79+
/*
80+
* Emit instructions to get the current value of the proof_chunk_size global
81+
* variable, which dictates how many rewrite steps should be included per
82+
* chunk of the hint trace.
83+
*/
84+
llvm::LoadInst *emit_get_proof_chunk_size(llvm::BasicBlock *insert_at_end);
85+
6386
/*
6487
* Get the block header value for the given `sort_name`.
6588
*/
@@ -136,12 +159,18 @@ class proof_event {
136159
llvm::BasicBlock *insert_at_end);
137160

138161
/*
139-
* Emit a call to the `patteern_matching_failure` API of the specified `proof_writer`.
162+
* Emit a call to the `pattern_matching_failure` API of the specified `proof_writer`.
140163
*/
141164
llvm::CallInst *emit_write_pattern_matching_failure(
142165
llvm::Value *proof_writer, std::string const &function_name,
143166
llvm::BasicBlock *insert_at_end);
144167

168+
/*
169+
* Emit a call to the `start_new_chunk` API of the specified `proof_writer`.
170+
*/
171+
llvm::CallInst *emit_start_new_chunk(
172+
llvm::Value *proof_writer, llvm::BasicBlock *insert_at_end);
173+
145174
public:
146175
[[nodiscard]] llvm::BasicBlock *hook_event_pre(
147176
std::string const &name, kore_composite_pattern *pattern,

include/runtime/header.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,9 @@ void write_side_condition_event_post_to_proof_trace(
374374
void *proof_writer, uint64_t ordinal, bool side_cond_result);
375375
void write_pattern_matching_failure_to_proof_trace(
376376
void *proof_writer, char const *function_name);
377-
void write_configuration_to_proof_trace(void *proof_writer, block *config);
377+
void write_configuration_to_proof_trace(
378+
void *proof_writer, block *config, bool is_initial);
379+
void start_new_chunk_in_proof_trace(void *proof_writer);
378380

379381
// The following functions have to be generated at kompile time
380382
// and linked with the interpreter.

include/runtime/proof_trace_writer.h

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,21 @@ class proof_trace_writer {
3333
side_condition_event_post(uint64_t ordinal, bool side_cond_result)
3434
= 0;
3535
virtual void pattern_matching_failure(char const *function_name) = 0;
36-
virtual void configuration(block *config) = 0;
36+
virtual void configuration(block *config, bool is_initial) = 0;
37+
virtual void start_new_chunk() = 0;
3738
virtual void end_of_trace() = 0;
3839
};
3940

41+
extern "C" {
42+
extern FILE *output_file;
43+
}
44+
4045
class proof_trace_file_writer : public proof_trace_writer {
4146
private:
47+
char const *filename_base_;
48+
size_t chunk_size_;
49+
size_t file_number_;
50+
uint32_t version_;
4251
FILE *file_;
4352

4453
void write_bytes(void const *ptr, size_t len) { fwrite(ptr, len, 1, file_); }
@@ -62,12 +71,29 @@ class proof_trace_file_writer : public proof_trace_writer {
6271
void write_uint64(uint64_t i) { write_bytes(&i, sizeof(uint64_t)); }
6372

6473
public:
65-
proof_trace_file_writer(FILE *file)
66-
: file_(file) { }
74+
proof_trace_file_writer(char const *filename_base, size_t chunk_size)
75+
: filename_base_(filename_base)
76+
, chunk_size_(chunk_size)
77+
, file_number_(0)
78+
, version_(0) {
79+
if (chunk_size_ > 0) {
80+
std::string filename = std::string(filename_base_) + ".pre_trace";
81+
file_ = std::fopen(filename.c_str(), "w");
82+
} else {
83+
file_ = std::fopen(filename_base_, "w");
84+
}
85+
output_file = file_;
86+
}
6787

6888
void proof_trace_header(uint32_t version) override {
69-
write_string("HINT");
70-
write_uint32(version);
89+
if (chunk_size_ == 0) {
90+
write_string("HINT");
91+
write_uint32(version);
92+
} else {
93+
write_string("PTRC");
94+
write_uint32(version);
95+
version_ = version;
96+
}
7197
}
7298

7399
void hook_event_pre(
@@ -137,9 +163,24 @@ class proof_trace_file_writer : public proof_trace_writer {
137163
write_null_terminated_string(function_name);
138164
}
139165

140-
void configuration(block *config) override {
166+
void configuration(block *config, bool is_initial) override {
141167
write_uint64(kllvm::config_sentinel);
142168
serialize_configuration_to_proof_trace(file_, config, 0);
169+
170+
if (chunk_size_ > 0 && is_initial) {
171+
start_new_chunk();
172+
}
173+
}
174+
175+
void start_new_chunk() override {
176+
std::fclose(file_);
177+
std::string filename
178+
= std::string(filename_base_) + "." + std::to_string(file_number_);
179+
file_number_++;
180+
file_ = std::fopen(filename.c_str(), "w");
181+
output_file = file_;
182+
write_string("CHNK");
183+
write_uint32(version_);
143184
}
144185

145186
void end_of_trace() override { }
@@ -242,9 +283,8 @@ class proof_trace_callback_writer : public proof_trace_writer {
242283
side_condition_result_construction const &event) { }
243284
[[clang::optnone]] void pattern_matching_failure_callback(
244285
pattern_matching_failure_construction const &event) { }
245-
[[clang::optnone]] void
246-
configuration_event_callback(kore_configuration_construction const &config) {
247-
}
286+
[[clang::optnone]] void configuration_event_callback(
287+
kore_configuration_construction const &config, bool is_initial) { }
248288

249289
public:
250290
proof_trace_callback_writer() { }
@@ -328,11 +368,13 @@ class proof_trace_callback_writer : public proof_trace_writer {
328368
pattern_matching_failure_callback(pm_failure);
329369
}
330370

331-
void configuration(block *config) override {
371+
void configuration(block *config, bool is_initial) override {
332372
kore_configuration_construction configuration(config);
333-
configuration_event_callback(configuration);
373+
configuration_event_callback(configuration, is_initial);
334374
}
335375

376+
void start_new_chunk() override { }
377+
336378
void end_of_trace() override { }
337379
};
338380

lib/binary/ProofTraceParser.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void llvm_event::print(
115115
std::ostream &out, bool expand_terms, bool is_arg, unsigned ind) const {
116116
if (is_step_event_) {
117117
step_event_->print(out, expand_terms, ind);
118-
} else {
118+
} else if (kore_pattern_) {
119119
std::string indent(ind * indent_size, ' ');
120120
if (expand_terms) {
121121
out << fmt::format("{}{}: kore[", indent, is_arg ? "arg" : "config");
@@ -133,9 +133,13 @@ llvm_rewrite_trace_iterator::llvm_rewrite_trace_iterator(
133133
std::unique_ptr<proof_trace_buffer> buffer, kore_header const &header)
134134
: buffer_(std::move(buffer))
135135
, parser_(false, false, header) {
136-
if (!proof_trace_parser::parse_header(*buffer_, version_)) {
136+
if (!proof_trace_parser::parse_header(*buffer_, kind_, version_)) {
137137
throw std::runtime_error("invalid header");
138138
}
139+
if (kind_ != proof_trace_parser::trace_kind::Hint) {
140+
throw std::runtime_error("invalid hint file: streaming parser does not "
141+
"work with partial traces");
142+
}
139143
}
140144

141145
std::optional<annotated_llvm_event>

0 commit comments

Comments
 (0)