Skip to content

Commit 00ab7bb

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents 7ad5d2c + 965655e commit 00ab7bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+213
-16
lines changed

include/kllvm/binary/serializer.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define AST_SERIALIZER_H
33

44
#include <kllvm/ast/AST.h>
5+
#include <kllvm/binary/ringbuffer.h>
56
#include <kllvm/binary/version.h>
67

78
#include <array>
@@ -160,6 +161,8 @@ class proof_trace_writer {
160161
write(&n, 1);
161162
}
162163

164+
virtual void write_eof() = 0;
165+
163166
void write_bool(bool b) { write(&b, sizeof(bool)); }
164167
void write_uint32(uint32_t i) { write(&i, sizeof(uint32_t)); }
165168
void write_uint64(uint64_t i) { write(&i, sizeof(uint64_t)); }
@@ -172,9 +175,42 @@ class proof_trace_file_writer : public proof_trace_writer {
172175
public:
173176
proof_trace_file_writer(FILE *file)
174177
: file_(file) { }
178+
179+
void write(void const *ptr, size_t len) override;
180+
void write_string(char const *str, size_t len) override;
181+
void write_string(char const *str) override;
182+
void write_eof() override { }
183+
};
184+
185+
class proof_trace_ringbuffer_writer : public proof_trace_writer {
186+
private:
187+
shm_ringbuffer *shm_buffer_;
188+
sem_t *data_avail_;
189+
sem_t *space_avail_;
190+
191+
void write(uint8_t const *ptr, size_t len = 1);
192+
193+
public:
194+
proof_trace_ringbuffer_writer(
195+
void *shm_object, sem_t *data_avail, sem_t *space_avail)
196+
: shm_buffer_(reinterpret_cast<shm_ringbuffer *>(shm_object))
197+
, data_avail_(data_avail)
198+
, space_avail_(space_avail) { }
199+
200+
~proof_trace_ringbuffer_writer() override { shm_buffer_->~shm_ringbuffer(); }
201+
202+
proof_trace_ringbuffer_writer(proof_trace_ringbuffer_writer const &) = delete;
203+
proof_trace_ringbuffer_writer(proof_trace_ringbuffer_writer &&) = delete;
204+
proof_trace_ringbuffer_writer &
205+
operator=(proof_trace_ringbuffer_writer const &)
206+
= delete;
207+
proof_trace_ringbuffer_writer &operator=(proof_trace_ringbuffer_writer &&)
208+
= delete;
209+
175210
void write(void const *ptr, size_t len) override;
176211
void write_string(char const *str, size_t len) override;
177212
void write_string(char const *str) override;
213+
void write_eof() override;
178214
};
179215

180216
} // namespace kllvm

lib/binary/serializer.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,31 @@ void proof_trace_file_writer::write_string(char const *str) {
222222
fputs(str, file_);
223223
}
224224

225+
void proof_trace_ringbuffer_writer::write(uint8_t const *ptr, size_t len) {
226+
for (size_t i = 0; i < len; i++) {
227+
sem_wait(space_avail_);
228+
shm_buffer_->put(&ptr[i]);
229+
sem_post(data_avail_);
230+
}
231+
}
232+
233+
void proof_trace_ringbuffer_writer::write(void const *ptr, size_t len) {
234+
auto const *data = reinterpret_cast<uint8_t const *>(ptr);
235+
write(data, len);
236+
}
237+
238+
void proof_trace_ringbuffer_writer::write_string(char const *str, size_t len) {
239+
auto const *data = reinterpret_cast<uint8_t const *>(str);
240+
write(data, len);
241+
}
242+
243+
void proof_trace_ringbuffer_writer::write_string(char const *str) {
244+
auto const *data = reinterpret_cast<uint8_t const *>(str);
245+
write(data, strlen(str));
246+
}
247+
248+
void proof_trace_ringbuffer_writer::write_eof() {
249+
shm_buffer_->put_eof();
250+
}
251+
225252
} // namespace kllvm

runtime/main/main.ll

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ declare void @print_proof_hint_header(ptr)
1919
@statistics.flag = private constant [13 x i8] c"--statistics\00"
2020
@binary_out.flag = private constant [16 x i8] c"--binary-output\00"
2121
@proof_out.flag = private constant [15 x i8] c"--proof-output\00"
22+
@use_shm.flag = private constant [20 x i8] c"--use-shared-memory\00"
2223

2324
@proof_writer = external global ptr
2425
@statistics = external global i1
2526
@binary_output = external global i1
2627
@proof_output = external global i1
28+
@use_shm = external global i1
2729

2830
declare i32 @strcmp(ptr %a, ptr %b)
2931

@@ -63,10 +65,19 @@ binary.set:
6365
proof.body:
6466
%proof.cmp = call i32 @strcmp(ptr %arg, ptr getelementptr inbounds ([15 x i8], ptr @proof_out.flag, i64 0, i64 0))
6567
%proof.eq = icmp eq i32 %proof.cmp, 0
66-
br i1 %proof.eq, label %proof.set, label %body.tail
68+
br i1 %proof.eq, label %proof.set, label %shm.body
6769

6870
proof.set:
6971
store i1 1, ptr @proof_output
72+
br label %shm.body
73+
74+
shm.body:
75+
%shm.cmp = call i32 @strcmp(ptr %arg, ptr getelementptr inbounds ([20 x i8], ptr @use_shm.flag, i64 0, i64 0))
76+
%shm.eq = icmp eq i32 %shm.cmp, 0
77+
br i1 %shm.eq, label %shm.set, label %body.tail
78+
79+
shm.set:
80+
store i1 1, ptr @use_shm
7081
br label %body.tail
7182

7283
body.tail:

runtime/util/finish_rewriting.cpp

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,77 @@
1+
#include <kllvm/binary/serializer.h>
2+
13
#include <runtime/header.h>
24

35
#include <cstdint>
6+
#include <fcntl.h>
47
#include <iostream>
58
#include <memory>
9+
#include <sys/mman.h>
610

711
extern "C" {
812

913
FILE *output_file = nullptr;
10-
kllvm::proof_trace_writer *proof_writer = nullptr;
14+
void *proof_writer = nullptr;
1115
bool statistics = false;
1216
bool binary_output = false;
1317
bool proof_output = false;
18+
bool use_shm = false;
1419

1520
extern int64_t steps;
1621
extern bool safe_partial;
1722
extern bool proof_hint_instrumentation_slow;
1823

1924
int32_t get_exit_code(block *);
2025

26+
#define ERR_EXIT(msg) \
27+
do { \
28+
perror(msg); \
29+
exit(1); \
30+
} while (0)
31+
32+
static void set_up_shm_writer(char const *output_filename) {
33+
// Open existing shared memory object
34+
int fd = shm_open(output_filename, O_RDWR, 0);
35+
if (fd == -1) {
36+
ERR_EXIT("shm_open writer");
37+
}
38+
39+
// Map the object into the caller's address space
40+
size_t shm_size = sizeof(kllvm::shm_ringbuffer);
41+
void *shm_object
42+
= mmap(nullptr, shm_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
43+
if (shm_object == MAP_FAILED) {
44+
ERR_EXIT("mmap writer");
45+
}
46+
47+
// MacOS has deprecated unnamed semaphores, so we need to use named ones
48+
std::string base_name(output_filename);
49+
std::string data_avail_sem_name = base_name + ".d";
50+
std::string space_avail_sem_name = base_name + ".s";
51+
52+
// Open existing semaphores
53+
// NOLINTNEXTLINE(*-pro-type-vararg)
54+
sem_t *data_avail = sem_open(data_avail_sem_name.c_str(), 0);
55+
if (data_avail == SEM_FAILED) {
56+
ERR_EXIT("sem_init data_avail writer");
57+
}
58+
// NOLINTNEXTLINE(*-pro-type-vararg)
59+
sem_t *space_avail = sem_open(space_avail_sem_name.c_str(), 0);
60+
if (space_avail == SEM_FAILED) {
61+
ERR_EXIT("sem_init space_avail writer");
62+
}
63+
64+
// Create the proof_trace_ringbuffer_writer object
65+
proof_writer = new kllvm::proof_trace_ringbuffer_writer(
66+
shm_object, data_avail, space_avail);
67+
}
68+
2169
void init_outputs(char const *output_filename) {
70+
if (proof_output && use_shm) {
71+
set_up_shm_writer(output_filename);
72+
return;
73+
}
74+
2275
output_file = fopen(output_filename, "a");
2376
if (proof_output) {
2477
proof_writer = new kllvm::proof_trace_file_writer(output_file);
@@ -34,17 +87,19 @@ void init_outputs(char const *output_filename) {
3487
= std::unique_ptr<FILE, decltype(&fclose)>(output_file, fclose);
3588

3689
// Similar for deletinging the proof_output_buffer data structure
37-
[[maybe_unused]] auto deleter
38-
= std::unique_ptr<kllvm::proof_trace_writer>(proof_writer);
90+
auto *w = static_cast<kllvm::proof_trace_writer *>(proof_writer);
91+
[[maybe_unused]] auto deleter = std::unique_ptr<kllvm::proof_trace_writer>(w);
3992

4093
if (error && safe_partial) {
4194
throw std::runtime_error(
4295
"Attempted to evaluate partial function at an undefined input");
4396
}
4497

4598
if (!output_file) {
46-
throw std::runtime_error(
47-
"Called finish_rewriting with no output file specified");
99+
if (!proof_output || !use_shm) {
100+
throw std::runtime_error(
101+
"Called finish_rewriting with no output file specified");
102+
}
48103
}
49104

50105
if (statistics) {
@@ -58,11 +113,16 @@ void init_outputs(char const *output_filename) {
58113
print_configuration(output_file, subject);
59114
}
60115
} else if (!error && !proof_hint_instrumentation_slow) {
61-
write_uint64_to_proof_trace(proof_writer, 0xFFFFFFFFFFFFFFFF);
116+
w->write_uint64(0xFFFFFFFFFFFFFFFF);
62117
serialize_configuration_to_proof_writer(proof_writer, subject);
63118
}
64119

65120
auto exit_code = error ? 113 : get_exit_code(subject);
121+
122+
if (proof_output) {
123+
w->write_eof();
124+
}
125+
66126
std::exit(exit_code);
67127
}
68128
}

test/defn/imp-sum-slow.kore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %proof-slow-interpreter
22
// RUN: %check-proof-out
3+
// RUN: %check-proof-shm-out
34
[topCellInitializer{}(LblinitGeneratedTopCell{}()), org'Stop'kframework'Stop'attributes'Stop'Source{}("Source(/home/robertorosmaninho/rv/k/llvm-backend/src/main/native/llvm-backend/test/defn/k-files/imp.md)")]
45

56
module BASIC-K

test/defn/imp-sum.kore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %proof-interpreter
22
// RUN: %check-proof-out
3+
// RUN: %check-proof-shm-out
34
[topCellInitializer{}(LblinitGeneratedTopCell{}()), org'Stop'kframework'Stop'attributes'Stop'Source{}("Source(/home/robertorosmaninho/rv/k/llvm-backend/src/main/native/llvm-backend/test/defn/k-files/imp.md)")]
45

56
module BASIC-K

test/defn/imp.kore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// RUN: %check-grep
33
// RUN: %proof-interpreter
44
// RUN: %check-proof-out
5+
// RUN: %check-proof-shm-out
56
[topCellInitializer{}(LblinitGeneratedTopCell{}()), org'Stop'kframework'Stop'attributes'Stop'Source{}("Source(/home/robertorosmaninho/rv/k/llvm-backend/src/main/native/llvm-backend/test/defn/k-files/imp.md)")]
67

78
module BASIC-K

test/defn/kool-static.kore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// RUN: %check-diff
33
// RUN: %proof-interpreter
44
// RUN: %check-proof-out
5+
// RUN: %check-proof-shm-out
56
[topCellInitializer{}(LblinitGeneratedTopCell{}()), org'Stop'kframework'Stop'attributes'Stop'Source{}("Source(/home/dwightguth/kframework-5.0.0/k-distribution/pl-tutorial/2_languages/2_kool/2_typed/2_static/kool-typed-static.md)")]
67

78
module BASIC-K

test/lit.cfg.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,14 @@ def one_line(s):
167167
echo "kore-proof-trace error while parsing proof hint trace with expanded kore terms and streaming parser"
168168
exit 1
169169
fi
170+
''')),
171+
172+
('%check-proof-shm-out', one_line('''
173+
%kore-rich-header %s > %t.header.bin
170174
%kore-proof-trace --shared-memory --verbose --expand-terms %t.header.bin %test-shm-buffer | diff - %test-proof-diff-out &
171175
reader_pid="$!"
172176
sleep 1
173-
%kore-proof-trace-shm-writer %t.out.bin %test-shm-buffer
177+
%run-proof-shm-out
174178
wait $reader_pid
175179
result="$?"
176180
if [ "$result" -ne 0 ]; then
@@ -196,11 +200,9 @@ def one_line(s):
196200

197201
('%check-dir-proof-out', one_line('''
198202
%kore-rich-header %s > %t.header.bin
199-
count=0
200203
for out in %test-dir-out/*.proof.out.diff; do
201204
in=%test-dir-in/`basename $out .proof.out.diff`.in
202205
hint=%t.`basename $out .proof.out.diff`.hint
203-
shmbuf=%test-shm-buffer.$count
204206
rm -f $hint
205207
%t.interpreter $in -1 $hint --proof-output
206208
%kore-proof-trace --verbose --expand-terms %t.header.bin $hint | diff - $out
@@ -215,10 +217,19 @@ def one_line(s):
215217
echo "kore-proof-trace error while parsing proof hint trace with expanded kore terms and streaming parser"
216218
exit 1
217219
fi
220+
done
221+
''')),
222+
223+
('%check-dir-proof-shm-out', one_line('''
224+
%kore-rich-header %s > %t.header.bin
225+
count=0
226+
for out in %test-dir-out/*.proof.out.diff; do
227+
in=%test-dir-in/`basename $out .proof.out.diff`.in
228+
shmbuf=%test-shm-buffer.$count
218229
%kore-proof-trace --shared-memory --verbose --expand-terms %t.header.bin $shmbuf | diff - $out &
219230
reader_pid="$!"
220231
sleep 1
221-
%kore-proof-trace-shm-writer $hint $shmbuf
232+
%t.interpreter $in -1 $shmbuf --proof-output --use-shared-memory
222233
wait $reader_pid
223234
result="$?"
224235
if [ "$result" -ne 0 ]; then
@@ -232,6 +243,7 @@ def one_line(s):
232243
('%run-binary-out', 'rm -f %t.out.bin && %t.interpreter %test-input -1 %t.out.bin --binary-output'),
233244
('%run-binary', 'rm -f %t.bin && %convert-input && %t.interpreter %t.bin -1 /dev/stdout'),
234245
('%run-proof-out', 'rm -f %t.out.bin && %t.interpreter %test-input -1 %t.out.bin --proof-output'),
246+
('%run-proof-shm-out', '%t.interpreter %test-input -1 %test-shm-buffer --proof-output --use-shared-memory'),
235247
('%run', '%t.interpreter %test-input -1 /dev/stdout'),
236248

237249
('%kprint-check', 'kprint %S %s true | diff - %s.out'),

test/proof/add-rewrite.kore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %proof-interpreter
22
// RUN: %check-dir-proof-out
3+
// RUN: %check-dir-proof-shm-out
34
// RUN: %check-proof-debug-out
45
[topCellInitializer{}(LblinitGeneratedTopCell{}()), org'Stop'kframework'Stop'attributes'Stop'Source{}("Source(/home/dwightguth/proof-checker/generation/k-benchmarks/add-rewrite/add-rewrite.k)")]
56

0 commit comments

Comments
 (0)