Skip to content

Commit 2630dba

Browse files
committed
Runtime: Rename Box2 functions to Box.
Swift SVN r29763
1 parent 9bdac15 commit 2630dba

File tree

9 files changed

+43
-180
lines changed

9 files changed

+43
-180
lines changed

include/swift/Runtime/HeapObject.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ extern "C" void swift_deallocPOD(HeapObject *obj);
123123
/// The heap object has an initial retain count of 1, and its metadata is set
124124
/// such that destroying the heap object destroys the contained value.
125125
extern "C" BoxPair::Return swift_allocBox(Metadata const *type);
126-
extern "C" BoxPair::Return swift_allocBox2(Metadata const *type);
127126

128127
// Allocate plain old memory. This is the generalized entry point
129128
// Never returns nil. The returned memory is uninitialized.
@@ -277,13 +276,12 @@ extern "C" void swift_deallocClassInstance(HeapObject *object,
277276
/// by swift_allocBox but is otherwise in an unknown state. The given Metadata
278277
/// pointer must be the same metadata pointer that was passed to swift_allocBox
279278
/// when the memory was allocated.
280-
extern "C" void swift_deallocBox(HeapObject *object, Metadata const *type);
281-
extern "C" void swift_deallocBox2(HeapObject *object);
279+
extern "C" void swift_deallocBox(HeapObject *object);
282280

283281
/// Project the value out of a box. `object` must have been allocated
284-
/// using `swift_allocBox2`, or by the compiler using a statically-emitted
282+
/// using `swift_allocBox`, or by the compiler using a statically-emitted
285283
/// box metadata object.
286-
extern "C" OpaqueValue *swift_projectBox2(HeapObject *object);
284+
extern "C" OpaqueValue *swift_projectBox(HeapObject *object);
287285

288286
/// RAII object that wraps a Swift heap object and releases it upon
289287
/// destruction.

include/swift/Runtime/InstrumentsSupport.h

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ extern "C" HeapObject *(*_swift_allocObject)(HeapMetadata const *metadata,
2525
size_t requiredAlignmentMask);
2626

2727
extern "C" BoxPair::Return (*_swift_allocBox)(Metadata const *type);
28-
extern "C" BoxPair::Return (*_swift_allocBox2)(Metadata const *type);
2928

3029
extern "C" HeapObject *(*_swift_retain)(HeapObject *object);
3130
extern "C" HeapObject *(*_swift_tryRetain)(HeapObject *object);

lib/IRGen/GenHeap.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ class NonFixedBoxTypeInfo final : public BoxTypeInfo {
11081108
// Use the runtime to allocate a box of the appropriate size.
11091109
auto metadata = IGF.emitTypeMetadataRefForLayout(boxedType);
11101110
llvm::Value *box, *address;
1111-
IGF.emitAllocBox2Call(metadata, box, address);
1111+
IGF.emitAllocBoxCall(metadata, box, address);
11121112
address = IGF.Builder.CreateBitCast(address,
11131113
ti.getStorageType()->getPointerTo());
11141114
return {ti.getAddressForPointer(address), box};
@@ -1118,15 +1118,15 @@ class NonFixedBoxTypeInfo final : public BoxTypeInfo {
11181118
deallocate(IRGenFunction &IGF, llvm::Value *box, SILType boxedType)
11191119
const override {
11201120
auto metadata = IGF.emitTypeMetadataRefForLayout(boxedType);
1121-
IGF.emitDeallocBox2Call(box, metadata);
1121+
IGF.emitDeallocBoxCall(box, metadata);
11221122
}
11231123

11241124
Address
11251125
project(IRGenFunction &IGF, llvm::Value *box, SILType boxedType)
11261126
const override {
11271127
auto &ti = IGF.getTypeInfo(boxedType);
11281128
auto metadata = IGF.emitTypeMetadataRefForLayout(boxedType);
1129-
llvm::Value *address = IGF.emitProjectBox2Call(box, metadata);
1129+
llvm::Value *address = IGF.emitProjectBoxCall(box, metadata);
11301130
address = IGF.Builder.CreateBitCast(address,
11311131
ti.getStorageType()->getPointerTo());
11321132
return ti.getAddressForPointer(address);

lib/IRGen/IRGenFunction.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -111,35 +111,35 @@ llvm::Value *IRGenFunction::emitAllocObjectCall(llvm::Value *metadata,
111111
{ metadata, size, alignMask }, name);
112112
}
113113

114-
void IRGenFunction::emitAllocBox2Call(llvm::Value *typeMetadata,
114+
void IRGenFunction::emitAllocBoxCall(llvm::Value *typeMetadata,
115115
llvm::Value *&box,
116116
llvm::Value *&valueAddress) {
117117
auto attrs = llvm::AttributeSet::get(IGM.LLVMContext,
118118
llvm::AttributeSet::FunctionIndex,
119119
llvm::Attribute::NoUnwind);
120120

121121
llvm::CallInst *call =
122-
Builder.CreateCall(IGM.getAllocBox2Fn(), typeMetadata);
122+
Builder.CreateCall(IGM.getAllocBoxFn(), typeMetadata);
123123
call->setCallingConv(IGM.RuntimeCC);
124124
call->setAttributes(attrs);
125125

126126
box = Builder.CreateExtractValue(call, 0);
127127
valueAddress = Builder.CreateExtractValue(call, 1);
128128
}
129129

130-
void IRGenFunction::emitDeallocBox2Call(llvm::Value *box,
130+
void IRGenFunction::emitDeallocBoxCall(llvm::Value *box,
131131
llvm::Value *typeMetadata) {
132132
auto attrs = llvm::AttributeSet::get(IGM.LLVMContext,
133133
llvm::AttributeSet::FunctionIndex,
134134
llvm::Attribute::NoUnwind);
135135

136136
llvm::CallInst *call =
137-
Builder.CreateCall(IGM.getDeallocBox2Fn(), box);
137+
Builder.CreateCall(IGM.getDeallocBoxFn(), box);
138138
call->setCallingConv(IGM.RuntimeCC);
139139
call->setAttributes(attrs);
140140
}
141141

142-
llvm::Value *IRGenFunction::emitProjectBox2Call(llvm::Value *box,
142+
llvm::Value *IRGenFunction::emitProjectBoxCall(llvm::Value *box,
143143
llvm::Value *typeMetadata) {
144144
llvm::Attribute::AttrKind attrKinds[] = {
145145
llvm::Attribute::NoUnwind,
@@ -149,7 +149,7 @@ llvm::Value *IRGenFunction::emitProjectBox2Call(llvm::Value *box,
149149
llvm::AttributeSet::FunctionIndex,
150150
attrKinds);
151151
llvm::CallInst *call =
152-
Builder.CreateCall(IGM.getProjectBox2Fn(), box);
152+
Builder.CreateCall(IGM.getProjectBoxFn(), box);
153153
call->setCallingConv(IGM.RuntimeCC);
154154
call->setAttributes(attrs);
155155
return call;

lib/IRGen/IRGenFunction.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,13 @@ class IRGenFunction {
188188
void emitDeallocRawCall(llvm::Value *pointer, llvm::Value *size,
189189
llvm::Value *alignMask);
190190

191-
void emitAllocBox2Call(llvm::Value *typeMetadata,
191+
void emitAllocBoxCall(llvm::Value *typeMetadata,
192192
llvm::Value *&box,
193193
llvm::Value *&valueAddress);
194194

195-
void emitDeallocBox2Call(llvm::Value *box, llvm::Value *typeMetadata);
195+
void emitDeallocBoxCall(llvm::Value *box, llvm::Value *typeMetadata);
196196

197-
llvm::Value *emitProjectBox2Call(llvm::Value *box, llvm::Value *typeMetadata);
197+
llvm::Value *emitProjectBoxCall(llvm::Value *box, llvm::Value *typeMetadata);
198198

199199
// Emit a reference to the canonical type metadata record for the given AST
200200
// type. This can be used to identify the type at runtime. For types with

lib/IRGen/RuntimeFunctions.def

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@
3838
#define FUNCTION(Id, Name, CC, ReturnTys, ArgTys, Attrs) FUNCTION_ID(Id)
3939
#endif
4040

41-
FUNCTION(AllocBox2, swift_allocBox2, RuntimeCC,
41+
FUNCTION(AllocBox, swift_allocBox, RuntimeCC,
4242
RETURNS(RefCountedPtrTy, OpaquePtrTy),
4343
ARGS(TypeMetadataPtrTy),
4444
ATTRS(NoUnwind))
4545

46-
FUNCTION(DeallocBox2, swift_deallocBox2, RuntimeCC,
46+
FUNCTION(DeallocBox, swift_deallocBox, RuntimeCC,
4747
RETURNS(VoidTy),
4848
ARGS(RefCountedPtrTy),
4949
ATTRS(NoUnwind))
5050

51-
FUNCTION(ProjectBox2, swift_projectBox2, RuntimeCC,
51+
FUNCTION(ProjectBox, swift_projectBox, RuntimeCC,
5252
RETURNS(OpaquePtrTy),
5353
ARGS(RefCountedPtrTy),
5454
ATTRS(NoUnwind, ReadNone))

stdlib/public/runtime/HeapObject.cpp

+18-152
Original file line numberDiff line numberDiff line change
@@ -131,147 +131,13 @@ swift::swift_allocPOD(size_t dataSize, size_t dataAlignmentMask) {
131131
return BoxPair{obj, reinterpret_cast<OpaqueValue*>(data)};
132132
}
133133

134-
namespace {
135-
/// Header for a generic box created by swift_allocBox in the worst case.
136-
struct GenericBox : HeapObject {
137-
/// The type of the value inside the box.
138-
Metadata const *type;
139-
140-
/// Returns the offset in bytes from the address of the box header to the
141-
/// address of the value inside the box.
142-
size_t getValueOffset() const {
143-
return getValueOffset(type);
144-
}
145-
146-
/// Returns the offset in bytes from the address of the box header for
147-
/// a box containing a value of the given type to the address of the value
148-
/// inside the box.
149-
static size_t getValueOffset(Metadata const *type) {
150-
return llvm::RoundUpToAlignment(sizeof(GenericBox),
151-
type->getValueWitnesses()->getAlignment());
152-
}
153-
154-
/// Returns the size of the allocation for the box, including the header
155-
/// and the value.
156-
size_t getAllocatedSize() const {
157-
return getAllocatedSize(type);
158-
}
159-
160-
/// Returns the size of the allocation that would be made for a box
161-
/// containing a value of the given type, including the header and the value.
162-
static size_t getAllocatedSize(Metadata const *type) {
163-
return getValueOffset(type) + type->getValueWitnesses()->stride;
164-
}
165-
166-
size_t getAllocatedAlignMask() const {
167-
return getAllocatedAlignMask(type);
168-
}
169-
170-
/// Returns the alignment mask of the allocation that would be
171-
/// made for a box containing a value of the given type, including
172-
/// the header and the value.
173-
static size_t getAllocatedAlignMask(Metadata const *type) {
174-
return std::max(type->getValueWitnesses()->getAlignmentMask(),
175-
alignof(GenericBox) - 1);
176-
}
177-
178-
/// Returns an opaque pointer to the value inside the box.
179-
OpaqueValue *getValuePointer() {
180-
char *p = reinterpret_cast<char*>(this) + getValueOffset();
181-
return reinterpret_cast<OpaqueValue*>(p);
182-
}
183-
184-
/// Returns an opaque pointer to the value inside the box.
185-
OpaqueValue const *getValuePointer() const {
186-
auto *p = reinterpret_cast<char const *>(this) + getValueOffset();
187-
return reinterpret_cast<OpaqueValue const *>(p);
188-
}
189-
};
190-
}
191-
192-
/// Heap object destructor for a generic box allocated with swift_allocBox.
193-
static void destroyGenericBox(HeapObject *o) {
194-
auto *box = static_cast<GenericBox*>(o);
195-
196-
// Destroy the value inside the box.
197-
OpaqueValue *value = box->getValuePointer();
198-
box->type->getValueWitnesses()->destroy(value, box->type);
199-
200-
// Deallocate the buffer.
201-
return swift_deallocObject(o, box->getAllocatedSize(),
202-
box->getAllocatedAlignMask());
203-
}
204-
205-
/// Generic heap metadata for generic allocBox allocations.
206-
/// FIXME: It may be worth the tradeoff to instantiate type-specific
207-
/// heap metadata at runtime.
208-
static const FullMetadata<HeapMetadata> GenericBoxHeapMetadata{
209-
HeapMetadataHeader{{destroyGenericBox}, {nullptr}},
210-
HeapMetadata{Metadata{MetadataKind::HeapLocalVariable}}
211-
};
212-
213-
BoxPair::Return
214-
swift::swift_allocBox(Metadata const *type) {
215-
return _swift_allocBox(type);
216-
}
217-
static BoxPair::Return _swift_allocBox_(Metadata const *type) {
218-
// NB: Special cases here need to also be checked for and handled in
219-
// swift_deallocBox.
220-
221-
// If the contained type is POD, perform a POD allocation.
222-
auto *vw = type->getValueWitnesses();
223-
if (vw->isPOD()) {
224-
return swift_allocPOD(vw->getSize(), vw->getAlignmentMask());
225-
}
226-
227-
// Allocate the box.
228-
HeapObject *obj = swift_allocObject(&GenericBoxHeapMetadata,
229-
GenericBox::getAllocatedSize(type),
230-
GenericBox::getAllocatedAlignMask(type));
231-
// allocObject will initialize the heap metadata pointer and refcount for us.
232-
// We also need to store the type metadata between the header and the
233-
// value.
234-
auto *box = static_cast<GenericBox *>(obj);
235-
box->type = type;
236-
237-
// Return the box and the value pointer.
238-
return BoxPair{box, box->getValuePointer()};
239-
}
240-
auto swift::_swift_allocBox = _swift_allocBox_;
241-
242-
void swift::swift_deallocBox(HeapObject *box, Metadata const *type) {
243-
// NB: Special cases here need to also be checked for and handled in
244-
// swift_allocBox.
245-
246-
// First, we need to recover what the allocation size was.
247-
size_t allocatedSize, allocatedAlignMask;
248-
auto *vw = type->getValueWitnesses();
249-
if (vw->isPOD()) {
250-
// If the contained type is POD, use the POD allocation size.
251-
allocatedSize = static_cast<PODBox*>(box)->allocatedSize;
252-
allocatedAlignMask = static_cast<PODBox*>(box)->allocatedAlignMask;
253-
} else {
254-
// Use the generic box size to deallocate the object.
255-
allocatedSize = GenericBox::getAllocatedSize(type);
256-
allocatedAlignMask = GenericBox::getAllocatedAlignMask(type);
257-
}
258-
259-
// Deallocate the box.
260-
swift_deallocObject(box, allocatedSize, allocatedAlignMask);
261-
}
262-
263-
void swift::swift_deallocPOD(HeapObject *obj) {
264-
swift_deallocObject(obj, static_cast<PODBox*>(obj)->allocatedSize,
265-
static_cast<PODBox*>(obj)->allocatedAlignMask);
266-
}
267-
268134
namespace {
269135
/// Heap metadata for runtime-instantiated generic boxes.
270-
struct GenericBox2HeapMetadata : public HeapMetadata {
136+
struct GenericBoxHeapMetadata : public HeapMetadata {
271137
/// The type inside the box.
272138
const Metadata *BoxedType;
273139

274-
constexpr GenericBox2HeapMetadata(MetadataKind kind,
140+
constexpr GenericBoxHeapMetadata(MetadataKind kind,
275141
const Metadata *boxedType)
276142
: HeapMetadata{kind}, BoxedType(boxedType)
277143
{}
@@ -301,9 +167,9 @@ struct GenericBox2HeapMetadata : public HeapMetadata {
301167
}
302168
};
303169

304-
/// Heap object destructor for a generic box allocated with swift_allocBox2.
305-
static void destroyGenericBox2(HeapObject *o) {
306-
auto metadata = static_cast<const GenericBox2HeapMetadata *>(o->metadata);
170+
/// Heap object destructor for a generic box allocated with swift_allocBox.
171+
static void destroyGenericBox(HeapObject *o) {
172+
auto metadata = static_cast<const GenericBoxHeapMetadata *>(o->metadata);
307173
// Destroy the object inside.
308174
auto *value = metadata->project(o);
309175
metadata->BoxedType->vw_destroy(value);
@@ -315,11 +181,11 @@ static void destroyGenericBox2(HeapObject *o) {
315181

316182
class BoxCacheEntry : public CacheEntry<BoxCacheEntry> {
317183
public:
318-
FullMetadata<GenericBox2HeapMetadata> Metadata;
184+
FullMetadata<GenericBoxHeapMetadata> Metadata;
319185

320186
BoxCacheEntry(size_t numArguments)
321-
: Metadata{HeapMetadataHeader{{destroyGenericBox2}, {nullptr}},
322-
GenericBox2HeapMetadata{MetadataKind::HeapGenericLocalVariable,
187+
: Metadata{HeapMetadataHeader{{destroyGenericBox}, {nullptr}},
188+
GenericBoxHeapMetadata{MetadataKind::HeapGenericLocalVariable,
323189
nullptr}} {
324190
assert(numArguments == 1);
325191
}
@@ -328,10 +194,10 @@ class BoxCacheEntry : public CacheEntry<BoxCacheEntry> {
328194
return 1;
329195
}
330196

331-
FullMetadata<GenericBox2HeapMetadata> *getData() {
197+
FullMetadata<GenericBoxHeapMetadata> *getData() {
332198
return &Metadata;
333199
}
334-
const FullMetadata<GenericBox2HeapMetadata> *getData() const {
200+
const FullMetadata<GenericBoxHeapMetadata> *getData() const {
335201
return &Metadata;
336202
}
337203
};
@@ -341,10 +207,10 @@ class BoxCacheEntry : public CacheEntry<BoxCacheEntry> {
341207
static Lazy<MetadataCache<BoxCacheEntry>> Boxes;
342208

343209
BoxPair::Return
344-
swift::swift_allocBox2(const Metadata *type) {
345-
return _swift_allocBox2(type);
210+
swift::swift_allocBox(const Metadata *type) {
211+
return _swift_allocBox(type);
346212
}
347-
static BoxPair::Return _swift_allocBox2_(const Metadata *type) {
213+
static BoxPair::Return _swift_allocBox_(const Metadata *type) {
348214
// Get the heap metadata for the box.
349215
auto &B = Boxes.get();
350216
const void *typeArg = type;
@@ -367,16 +233,16 @@ static BoxPair::Return _swift_allocBox2_(const Metadata *type) {
367233

368234
return BoxPair{allocation, projection};
369235
}
370-
auto swift::_swift_allocBox2 = _swift_allocBox2_;
236+
auto swift::_swift_allocBox = _swift_allocBox_;
371237

372-
void swift::swift_deallocBox2(HeapObject *o) {
373-
auto metadata = static_cast<const GenericBox2HeapMetadata *>(o->metadata);
238+
void swift::swift_deallocBox(HeapObject *o) {
239+
auto metadata = static_cast<const GenericBoxHeapMetadata *>(o->metadata);
374240
swift_deallocObject(o, metadata->getAllocSize(),
375241
metadata->getAllocAlignMask());
376242
}
377243

378-
OpaqueValue *swift::swift_projectBox2(HeapObject *o) {
379-
auto metadata = static_cast<const GenericBox2HeapMetadata *>(o->metadata);
244+
OpaqueValue *swift::swift_projectBox(HeapObject *o) {
245+
auto metadata = static_cast<const GenericBoxHeapMetadata *>(o->metadata);
380246
return metadata->project(o);
381247
}
382248

test/IRGen/closure.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func b<T : Ordinable>(var seq seq: T) -> (Int) -> Int {
5353
func captures_tuple<T, U>(var x x: (T, U)) -> () -> (T, U) {
5454
// CHECK: [[METADATA:%.*]] = call %swift.type* @swift_getTupleTypeMetadata2(%swift.type* %T, %swift.type* %U, i8* null, i8** null)
5555
// CHECK-NOT: @swift_getTupleTypeMetadata2
56-
// CHECK: [[BOX:%.*]] = call { %swift.refcounted*, %swift.opaque* } @swift_allocBox2(%swift.type* [[METADATA]])
56+
// CHECK: [[BOX:%.*]] = call { %swift.refcounted*, %swift.opaque* } @swift_allocBox(%swift.type* [[METADATA]])
5757
// CHECK: [[ADDR:%.*]] = extractvalue { %swift.refcounted*, %swift.opaque* } [[BOX]], 1
5858
// CHECK: bitcast %swift.opaque* [[ADDR]] to <{}>*
5959
return {x}

0 commit comments

Comments
 (0)