Skip to content

Commit 1f7f5bb

Browse files
committed
[embedded] Add executable tests for array builtins
1 parent f88c919 commit 1f7f5bb

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

test/embedded/Inputs/tiny-runtime-dummy-refcounting.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,19 @@ typedef struct HeapObject {
2222
size_t refcount;
2323
} HeapObject;
2424

25-
void *swift_allocObject(ClassMetadata *metadata, size_t requiredSize, size_t requiredAlignmentMask) {
25+
void *swift_slowAlloc(size_t bytes, size_t alignMask) {
2626
void *r = NULL;
27-
posix_memalign(&r, requiredAlignmentMask + 1, requiredSize);
28-
bzero(r, requiredSize);
29-
HeapObject *object = r;
27+
posix_memalign(&r, (alignMask == (size_t)-1) ? 16 : (alignMask + 1), bytes);
28+
bzero(r, bytes);
29+
return r;
30+
}
31+
32+
void swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {
33+
free(ptr);
34+
}
35+
36+
void *swift_allocObject(ClassMetadata *metadata, size_t requiredSize, size_t requiredAlignmentMask) {
37+
HeapObject *object = swift_slowAlloc(requiredSize, requiredAlignmentMask);
3038
object->metadata = metadata;
3139
object->refcount = 1;
3240
return object;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s %S/Inputs/print.swift -enable-experimental-feature Embedded -enable-builtin-module -c -o %t/main.o
3+
// RUN: %target-clang -x c -c %S/Inputs/tiny-runtime-dummy-refcounting.c -o %t/runtime.o
4+
// RUN: %target-clang %t/main.o %t/runtime.o -o %t/a.out -dead_strip
5+
// RUN: %target-run %t/a.out | %FileCheck %s
6+
7+
// REQUIRES: executable_test
8+
// REQUIRES: optimized_stdlib
9+
// REQUIRES: VENDOR=apple
10+
// REQUIRES: OS=macosx
11+
12+
import Builtin
13+
14+
var NoisyLifeCount = 0
15+
var NoisyDeathCount = 0
16+
17+
protocol P {}
18+
19+
class Noisy : P {
20+
init() { NoisyLifeCount += 1 }
21+
deinit { NoisyDeathCount += 1 }
22+
}
23+
24+
struct Large : P {
25+
var a, b, c, d: Noisy
26+
27+
init() {
28+
self.a = Noisy()
29+
self.b = Noisy()
30+
self.c = Noisy()
31+
self.d = Noisy()
32+
}
33+
}
34+
35+
func exerciseArrayValueWitnesses<T>(_ value: T) {
36+
let buf = UnsafeMutablePointer<T>.allocate(capacity: 5)
37+
38+
(buf + 0).initialize(to: value)
39+
(buf + 1).initialize(to: value)
40+
41+
Builtin.copyArray(T.self, (buf + 2)._rawValue, buf._rawValue, 2._builtinWordValue)
42+
Builtin.takeArrayBackToFront(T.self, (buf + 1)._rawValue, buf._rawValue, 4._builtinWordValue)
43+
Builtin.takeArrayFrontToBack(T.self, buf._rawValue, (buf + 1)._rawValue, 4._builtinWordValue)
44+
Builtin.destroyArray(T.self, buf._rawValue, 4._builtinWordValue)
45+
46+
buf.deallocate()
47+
}
48+
49+
func test() {
50+
NoisyLifeCount = 0
51+
NoisyDeathCount = 0
52+
do {
53+
exerciseArrayValueWitnesses(44)
54+
exerciseArrayValueWitnesses(Noisy())
55+
exerciseArrayValueWitnesses(Large())
56+
}
57+
precondition(NoisyLifeCount == NoisyDeathCount)
58+
print("Checks out")
59+
// CHECK: Checks out
60+
}
61+
62+
@main struct Main { static func main() { test() } }

0 commit comments

Comments
 (0)