Skip to content

Commit f9da8b2

Browse files
committed
rt: Add an ivec length intrinsic and an ivec reserve function, both untested as of yet
1 parent 66c5203 commit f9da8b2

File tree

4 files changed

+106
-24
lines changed

4 files changed

+106
-24
lines changed

src/rt/intrinsics/intrinsics.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Use `clang++ -emit-llvm -S -arch i386 -O3 -I../isaac -I../uthash -o
2-
// intrinsics.ll intrinsics.cpp`
1+
// Use `clang++ -emit-llvm -S -arch i386 -O3 -I../isaac -I../uthash
2+
// -I../arch/i386 -o intrinsics.ll intrinsics.cpp`
33

44
#include "../rust_internal.h"
55

@@ -9,3 +9,16 @@ rust_intrinsic_vec_len(rust_task *task, type_desc *ty, rust_vec *v)
99
return v->fill / ty->size;
1010
}
1111

12+
extern "C" size_t
13+
rust_intrinsic_ivec_len(rust_task *task, type_desc *ty, rust_ivec *v)
14+
{
15+
size_t fill;
16+
if (v->fill)
17+
fill = v->fill;
18+
else if (v->payload.ptr)
19+
fill = v->payload.ptr->fill;
20+
else
21+
fill = 0;
22+
return fill / ty->size;
23+
}
24+

src/rt/intrinsics/intrinsics.ll.in

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,63 @@ target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f3
33
target triple = "@CFG_LLVM_TRIPLE@"
44

55
%0 = type { i32, i8**, i32 }
6-
%1 = type { %"struct.hash_map<rust_task *, rust_handle<rust_task> *>::map_entry"* }
7-
%2 = type { %"struct.hash_map<rust_port *, rust_handle<rust_port> *>::map_entry"* }
8-
%3 = type { %"struct.hash_map<rust_dom *, rust_handle<rust_dom> *>::map_entry"* }
9-
%4 = type { i32, %struct.rust_dom**, i32 }
6+
%1 = type { i32, %struct.rust_task**, i32 }
7+
%2 = type { %"struct.hash_map<rust_task *, rust_handle<rust_task> *>::map_entry"* }
8+
%3 = type { %"struct.hash_map<rust_port *, rust_handle<rust_port> *>::map_entry"* }
9+
%4 = type { %"struct.hash_map<rust_dom *, rust_handle<rust_dom> *>::map_entry"* }
1010
%5 = type { i32 (...)**, %6 }
11-
%6 = type { i32, %class.rust_message_queue**, i32 }
12-
%7 = type opaque
13-
%8 = type { %"struct.hash_map<rust_port *, rust_proxy<rust_port> *>::map_entry"* }
11+
%6 = type { i32, %struct.rust_dom**, i32 }
12+
%7 = type { i32 (...)**, %8 }
13+
%8 = type { i32, %class.rust_message_queue**, i32 }
14+
%9 = type opaque
15+
%10 = type opaque
16+
%11 = type { %"struct.hash_map<rust_port *, rust_proxy<rust_port> *>::map_entry"* }
1417
%class.array_list = type { i32, %struct.maybe_proxy**, i32 }
18+
%class.context = type { %struct.registers_t, %class.context* }
1519
%class.hash_map = type { %"struct.hash_map<rust_task *, rust_proxy<rust_task> *>::map_entry"* }
16-
%class.indexed_list = type { i32 (...)**, %4 }
20+
%class.indexed_list = type { i32 (...)**, %1 }
1721
%class.lock_and_signal = type { i32 (...)**, %struct._opaque_pthread_cond_t, %struct._opaque_pthread_mutex_t }
22+
%class.lock_free_queue = type { i32 (...)**, %"struct.lock_free_queue<rust_message *>::pointer_t", %"struct.lock_free_queue<rust_message *>::pointer_t" }
1823
%class.memory_region = type { i32 (...)**, %class.rust_srv*, %class.memory_region*, i32, %0, i8, i8, %class.lock_and_signal }
19-
%class.ptr_vec = type { %struct.rust_dom*, i32, i32, %class.rust_crate_cache** }
20-
%class.rust_crate = type { i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }
21-
%class.rust_crate_cache = type { [4 x i8], %"class.rust_crate_cache::rust_sym"**, %"class.rust_crate_cache::c_sym"**, %"class.rust_crate_cache::lib"**, %struct.type_desc*, %class.rust_crate*, %struct.rust_dom*, i32 }
22-
%"class.rust_crate_cache::c_sym" = type { [4 x i8], i32, %"class.rust_crate_cache::lib"*, %struct.rust_dom* }
23-
%"class.rust_crate_cache::lib" = type { [4 x i8], i32, %struct.rust_dom* }
24-
%"class.rust_crate_cache::rust_sym" = type { [4 x i8], i32, %"class.rust_crate_cache::c_sym"*, %struct.rust_dom* }
24+
%class.rust_crate_cache = type { %struct.type_desc*, %struct.rust_dom*, i32 }
2525
%class.rust_handle = type opaque
26-
%class.rust_kernel = type { [12 x i8], %class.memory_region*, %class.rust_log, %class.rust_srv*, %1, %2, %3, i8, %class.lock_and_signal, %class.indexed_list, %5 }
26+
%class.rust_kernel = type { %class.rust_thread, %class.memory_region*, %class.rust_log, %class.rust_srv*, %2, %3, %4, i8, %class.lock_and_signal, %5, %7 }
2727
%class.rust_log = type { i32 (...)**, %class.rust_srv*, %struct.rust_dom*, i8, i8 }
28-
%class.rust_message_queue = type { [20 x i8], %class.memory_region, %class.rust_kernel*, %7*, i32 }
28+
%class.rust_message_queue = type { %class.lock_free_queue, %class.memory_region, %class.rust_kernel*, %10*, i32 }
2929
%class.rust_srv = type { i32 (...)**, %class.memory_region, %class.memory_region }
30-
%class.rust_task_list = type { [16 x i8], %struct.rust_dom*, i8* }
30+
%class.rust_task_list = type { %class.indexed_list, %struct.rust_dom*, i8* }
31+
%class.rust_thread = type { i32 (...)**, i8, %struct._opaque_pthread_t* }
3132
%class.timer = type { i32 (...)**, i64, i64 }
3233
%struct.UT_hash_bucket = type { %struct.UT_hash_handle*, i32, i32 }
3334
%struct.UT_hash_handle = type { %struct.UT_hash_table*, i8*, i8*, %struct.UT_hash_handle*, %struct.UT_hash_handle*, i8*, i32, i32 }
3435
%struct.UT_hash_table = type { %struct.UT_hash_bucket*, i32, i32, i32, %struct.UT_hash_handle*, i32, i32, i32, i32, i32 }
36+
%struct.__darwin_pthread_handler_rec = type { void (i8*)*, i8*, %struct.__darwin_pthread_handler_rec* }
3537
%struct._opaque_pthread_attr_t = type { i32, [36 x i8] }
3638
%struct._opaque_pthread_cond_t = type { i32, [24 x i8] }
3739
%struct._opaque_pthread_mutex_t = type { i32, [40 x i8] }
40+
%struct._opaque_pthread_t = type { i32, %struct.__darwin_pthread_handler_rec*, [596 x i8] }
3841
%struct.gc_alloc = type { %struct.gc_alloc*, %struct.gc_alloc*, i32, [0 x i8] }
3942
%"struct.hash_map<rust_dom *, rust_handle<rust_dom> *>::map_entry" = type opaque
4043
%"struct.hash_map<rust_port *, rust_handle<rust_port> *>::map_entry" = type opaque
4144
%"struct.hash_map<rust_port *, rust_proxy<rust_port> *>::map_entry" = type opaque
4245
%"struct.hash_map<rust_task *, rust_handle<rust_task> *>::map_entry" = type opaque
4346
%"struct.hash_map<rust_task *, rust_proxy<rust_task> *>::map_entry" = type opaque
44-
%struct.maybe_proxy = type { [4 x i8], %struct.rust_task* }
47+
%"struct.lock_free_queue<rust_message *>::node_t" = type { %9*, %"struct.lock_free_queue<rust_message *>::pointer_t" }
48+
%"struct.lock_free_queue<rust_message *>::pointer_t" = type { %"struct.lock_free_queue<rust_message *>::node_t"*, i32 }
49+
%struct.maybe_proxy = type { %struct.rc_base, %struct.rust_task* }
4550
%struct.randctx = type { i32, [256 x i32], [256 x i32], i32, i32, i32 }
51+
%struct.rc_base = type { i32 }
52+
%struct.registers_t = type { i32, i32, i32, i32, i32, i32, i32, i32, i16, i16, i16, i16, i16, i16, i32, i32 }
4653
%struct.rust_alarm = type { %struct.rust_task*, i32 }
4754
%struct.rust_cond = type { i8 }
48-
%struct.rust_dom = type { [4 x i8], i32, %class.rust_crate*, %class.rust_log, i32, %class.rust_srv*, %class.memory_region, %class.memory_region, i8*, %class.rust_task_list, %class.rust_task_list, %class.rust_task_list, %class.rust_task_list, %class.ptr_vec, %struct.randctx, %struct.rust_task*, %struct.rust_task*, i32, %class.rust_kernel*, i32, %class.hash_map, %8, %class.rust_message_queue*, %struct._opaque_pthread_attr_t }
49-
%struct.rust_task = type { [8 x i8], %struct.stk_seg*, i32, i32, %struct.gc_alloc*, %struct.rust_dom*, %class.rust_crate_cache*, i8*, %class.rust_task_list*, %struct.rust_cond*, i8*, %struct.rust_task*, i32, i32, i32, %class.timer, i32*, %class.array_list, %struct.rust_alarm, %class.rust_handle* }
50-
%struct.rust_vec = type { [4 x i8], i32, i32, i32, [0 x i8] }
55+
%struct.rust_dom = type { %struct.rc_base, i32, %class.rust_log, i32, %class.rust_srv*, %class.memory_region, %class.memory_region, i8*, %class.rust_task_list, %class.rust_task_list, %class.rust_task_list, %class.rust_task_list, %class.rust_crate_cache, %struct.randctx, %struct.rust_task*, %struct.rust_task*, i32, %class.rust_kernel*, i32, %class.hash_map, %11, %class.rust_message_queue*, %struct._opaque_pthread_attr_t }
56+
%struct.rust_ivec = type { i32, i32, %union.rust_ivec_payload }
57+
%struct.rust_ivec_heap = type { i32, [0 x i8] }
58+
%struct.rust_task = type { %struct.maybe_proxy, %struct.stk_seg*, i32, i32, %struct.gc_alloc*, %struct.rust_dom*, %class.rust_crate_cache*, i8*, %class.rust_task_list*, %struct.rust_cond*, i8*, %struct.rust_task*, i32, i32, i32, %class.timer, i32*, %class.array_list, %struct.rust_alarm, %class.rust_handle*, %class.context }
59+
%struct.rust_vec = type { %struct.rc_base, i32, i32, i32, [0 x i8] }
5160
%struct.stk_seg = type { i32, i32, [0 x i8] }
52-
%struct.type_desc = type { %struct.type_desc**, i32, i32, i32, i32, i32, i32, i32, i32, i32, %struct.UT_hash_handle, i32, [0 x %struct.type_desc*] }
61+
%struct.type_desc = type { %struct.type_desc**, i32, i32, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*)*, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*)*, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*)*, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*)*, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*)*, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*)*, i32, void (i8*, %struct.rust_task*, i8*, %struct.type_desc**, i8*, i8*, i8)*, %struct.UT_hash_handle, i32, [0 x %struct.type_desc*] }
62+
%union.rust_ivec_payload = type { %struct.rust_ivec_heap* }
5363

5464
define linkonce_odr i32 @rust_intrinsic_vec_len(%struct.rust_task* nocapture %task, %struct.type_desc* nocapture %ty, %struct.rust_vec* nocapture %v) nounwind readonly ssp {
5565
entry:
@@ -61,6 +71,33 @@ entry:
6171
ret i32 %div
6272
}
6373

74+
define linkonce_odr i32 @rust_intrinsic_ivec_len(%struct.rust_task* nocapture %task, %struct.type_desc* nocapture %ty, %struct.rust_ivec* nocapture %v) nounwind readonly ssp {
75+
entry:
76+
%fill1 = getelementptr inbounds %struct.rust_ivec* %v, i32 0, i32 0
77+
%tmp2 = load i32* %fill1, align 4, !tbaa !0
78+
%tobool = icmp eq i32 %tmp2, 0
79+
br i1 %tobool, label %if.else, label %if.end17
80+
81+
if.else: ; preds = %entry
82+
%ptr = getelementptr inbounds %struct.rust_ivec* %v, i32 0, i32 2, i32 0
83+
%tmp7 = load %struct.rust_ivec_heap** %ptr, align 4, !tbaa !3
84+
%tobool8 = icmp eq %struct.rust_ivec_heap* %tmp7, null
85+
br i1 %tobool8, label %if.end17, label %if.then9
86+
87+
if.then9: ; preds = %if.else
88+
%fill14 = getelementptr inbounds %struct.rust_ivec_heap* %tmp7, i32 0, i32 0
89+
%tmp15 = load i32* %fill14, align 4, !tbaa !0
90+
br label %if.end17
91+
92+
if.end17: ; preds = %if.else, %entry, %if.then9
93+
%fill.0 = phi i32 [ %tmp15, %if.then9 ], [ %tmp2, %entry ], [ 0, %if.else ]
94+
%size = getelementptr inbounds %struct.type_desc* %ty, i32 0, i32 1
95+
%tmp20 = load i32* %size, align 4, !tbaa !0
96+
%div = udiv i32 %fill.0, %tmp20
97+
ret i32 %div
98+
}
99+
64100
!0 = metadata !{metadata !"long", metadata !1}
65101
!1 = metadata !{metadata !"omnipotent char", metadata !2}
66102
!2 = metadata !{metadata !"Simple C/C++ TBAA", null}
103+
!3 = metadata !{metadata !"any pointer", metadata !1}

src/rt/rust_builtin.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,37 @@ get_time(rust_task *task, uint32_t *sec, uint32_t *usec) {
576576
}
577577
#endif
578578

579+
/**
580+
* Preallocates the exact number of bytes in the given interior vector.
581+
*/
582+
extern "C" void
583+
ivec_reserve(rust_task *task, type_desc *ty, rust_ivec *v, size_t n_elems)
584+
{
585+
size_t new_alloc = n_elems * ty->size;
586+
if (new_alloc <= v->alloc)
587+
return; // Already big enough.
588+
589+
rust_ivec_heap *heap_part;
590+
if (v->fill) {
591+
// On stack; spill to heap.
592+
heap_part = (rust_ivec_heap *)task->malloc(new_alloc +
593+
sizeof(size_t));
594+
heap_part->fill = v->fill;
595+
memcpy(&heap_part->data, v->payload.data, v->fill);
596+
597+
v->fill = 0;
598+
v->payload.ptr = heap_part;
599+
} else {
600+
// On heap; resize.
601+
heap_part = (rust_ivec_heap *)task->realloc(v->payload.ptr,
602+
new_alloc);
603+
v->payload.ptr = heap_part;
604+
}
605+
606+
v->alloc = new_alloc;
607+
}
608+
609+
579610
//
580611
// Local Variables:
581612
// mode: C++

src/rt/rustrt.def.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ debug_trap
99
debug_tydesc
1010
do_gc
1111
get_time
12+
ivec_reserve
1213
last_os_error
1314
rand_free
1415
rand_new

0 commit comments

Comments
 (0)