Skip to content

[embedded] Add executable tests for array builtins #68738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions test/embedded/Inputs/tiny-runtime-dummy-refcounting.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ typedef struct HeapObject {
size_t refcount;
} HeapObject;

void *swift_allocObject(ClassMetadata *metadata, size_t requiredSize, size_t requiredAlignmentMask) {
void *swift_slowAlloc(size_t bytes, size_t alignMask) {
void *r = NULL;
posix_memalign(&r, requiredAlignmentMask + 1, requiredSize);
bzero(r, requiredSize);
HeapObject *object = r;
posix_memalign(&r, (alignMask == (size_t)-1) ? 16 : (alignMask + 1), bytes);
bzero(r, bytes);
return r;
}

void swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {
free(ptr);
}

void *swift_allocObject(ClassMetadata *metadata, size_t requiredSize, size_t requiredAlignmentMask) {
HeapObject *object = swift_slowAlloc(requiredSize, requiredAlignmentMask);
object->metadata = metadata;
object->refcount = 1;
return object;
Expand Down
62 changes: 62 additions & 0 deletions test/embedded/array-builtins-exec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s %S/Inputs/print.swift -enable-experimental-feature Embedded -enable-builtin-module -c -o %t/main.o
// RUN: %target-clang -x c -c %S/Inputs/tiny-runtime-dummy-refcounting.c -o %t/runtime.o
// RUN: %target-clang %t/main.o %t/runtime.o -o %t/a.out -dead_strip
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: optimized_stdlib
// REQUIRES: VENDOR=apple
// REQUIRES: OS=macosx

import Builtin

var NoisyLifeCount = 0
var NoisyDeathCount = 0

protocol P {}

class Noisy : P {
init() { NoisyLifeCount += 1 }
deinit { NoisyDeathCount += 1 }
}

struct Large : P {
var a, b, c, d: Noisy

init() {
self.a = Noisy()
self.b = Noisy()
self.c = Noisy()
self.d = Noisy()
}
}

func exerciseArrayValueWitnesses<T>(_ value: T) {
let buf = UnsafeMutablePointer<T>.allocate(capacity: 5)

(buf + 0).initialize(to: value)
(buf + 1).initialize(to: value)

Builtin.copyArray(T.self, (buf + 2)._rawValue, buf._rawValue, 2._builtinWordValue)
Builtin.takeArrayBackToFront(T.self, (buf + 1)._rawValue, buf._rawValue, 4._builtinWordValue)
Builtin.takeArrayFrontToBack(T.self, buf._rawValue, (buf + 1)._rawValue, 4._builtinWordValue)
Builtin.destroyArray(T.self, buf._rawValue, 4._builtinWordValue)

buf.deallocate()
}

func test() {
NoisyLifeCount = 0
NoisyDeathCount = 0
do {
exerciseArrayValueWitnesses(44)
exerciseArrayValueWitnesses(Noisy())
exerciseArrayValueWitnesses(Large())
}
precondition(NoisyLifeCount == NoisyDeathCount)
print("Checks out")
// CHECK: Checks out
}

@main struct Main { static func main() { test() } }