Skip to content

Commit 95616f6

Browse files
authored
Merge pull request #4796 from apple/revert-4722-tail-alloc
Revert "New SIL instructions to support tail-allocated arrays in SIL."
2 parents cd07c2c + fbb3cf3 commit 95616f6

Some content is hidden

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

68 files changed

+1148
-1473
lines changed

docs/Runtime.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ Rename with a non-`stdlib` naming scheme.
7373
000000000001cb30 T _swift_allocBox
7474
000000000001c990 T _swift_allocObject
7575
000000000001ca60 T _swift_bufferAllocate
76+
000000000001ca70 T _swift_bufferAllocateOnStack
77+
000000000001ca80 T _swift_bufferDeallocateFromStack
7678
000000000001ca90 T _swift_bufferHeaderSize
7779
000000000001cd30 T _swift_deallocBox
7880
000000000001d490 T _swift_deallocClassInstance

docs/SIL.rst

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,24 +1828,16 @@ alloc_ref
18281828
`````````
18291829
::
18301830

1831-
sil-instruction ::= 'alloc_ref'
1832-
('[' 'objc' ']')?
1833-
('[' 'stack' ']')?
1834-
('[' 'tail_elems' sil-type '*' sil-operand ']')*
1835-
sil-type
1831+
sil-instruction ::= 'alloc_ref' ('[' 'objc' ']')? ('[' 'stack' ']')? sil-type
18361832

18371833
%1 = alloc_ref [stack] $T
1838-
%1 = alloc_ref [tail_elems $E * %2 : Builtin.Word] $T
18391834
// $T must be a reference type
18401835
// %1 has type $T
1841-
// $E is the type of the tail-allocated elements
1842-
// %2 must be of a builtin integer type
18431836

18441837
Allocates an object of reference type ``T``. The object will be initialized
18451838
with retain count 1; its state will be otherwise uninitialized. The
18461839
optional ``objc`` attribute indicates that the object should be
18471840
allocated using Objective-C's allocation methods (``+allocWithZone:``).
1848-
18491841
The optional ``stack`` attribute indicates that the object can be allocated
18501842
on the stack instead on the heap. In this case the instruction must have
18511843
balanced with a ``dealloc_ref [stack]`` instruction to mark the end of the
@@ -1855,15 +1847,6 @@ possible. The final decision on stack allocation is done during llvm IR
18551847
generation. This is because the decision also depends on the object size,
18561848
which is not necessarily known at SIL level.
18571849

1858-
The optional ``tail_elems`` attributes specifies the amount of space to be
1859-
reserved for tail-allocated arrays of given element types and element counts.
1860-
If there are more than one ``tail_elems`` attributes then the tail arrays are
1861-
allocated in the specified order.
1862-
The count-operand must be of a builtin integer type.
1863-
The instructions ``ref_tail_addr`` and ``tail_addr`` can be used to project
1864-
the tail elements.
1865-
The ``objc`` attribute cannot be used together with ``tail_elems``.
1866-
18671850
alloc_ref_dynamic
18681851
`````````````````
18691852
::
@@ -2346,31 +2329,6 @@ special behavior in this regard, unlike ``char*`` or ``void*`` in C.) It is
23462329
also undefined behavior to index out of bounds of an array, except to index
23472330
the "past-the-end" address of the array.
23482331

2349-
tail_addr
2350-
`````````
2351-
::
2352-
2353-
sil-instruction ::= 'tail_addr' sil-operand ',' sil-operand ',' sil-type
2354-
2355-
%2 = tail_addr %0 : $*T, %1 : $Builtin.Int<n>, $E
2356-
// %0 must be of an address type $*T
2357-
// %1 must be of a builtin integer type
2358-
// %2 will be of type $*E
2359-
2360-
Given an address of an array of ``%1`` values, returns the address of an
2361-
element which is tail-allocated after the array.
2362-
This instruction is equivalent to ``index_addr`` except that the resulting
2363-
address is aligned-up to the tail-element type ``$E``.
2364-
2365-
This instruction is used to project the N-th tail-allocated array from an
2366-
object which is created by an ``alloc_ref`` with multiple ``tail_elems``.
2367-
The first operand is the address of an element of the (N-1)-th array, usually
2368-
the first element. The second operand is the number of elements until the end
2369-
of that array. The result is the address of the first element of the N-th array.
2370-
2371-
It is undefined behavior if the provided address, count and type do not match
2372-
the actual layout of tail-allocated arrays of the underlying object.
2373-
23742332
index_raw_pointer
23752333
`````````````````
23762334
::
@@ -3298,23 +3256,6 @@ Given an instance of a class, derives the address of a physical instance
32983256
variable inside the instance. It is undefined behavior if the class value
32993257
is null.
33003258

3301-
ref_tail_addr
3302-
`````````````
3303-
::
3304-
3305-
sil-instruction ::= 'ref_tail_addr' sil-operand ',' sil-type
3306-
3307-
%1 = ref_tail_addr %0 : $C, $E
3308-
// %0 must be a value of class type $C with tail-allocated elements $E
3309-
// %1 will be of type $*E
3310-
3311-
Given an instance of a class, which is created with tail-allocated array(s),
3312-
derives the address of the first element of the first tail-allocated array.
3313-
This instruction is used to project the first tail-allocated element from an
3314-
object which is created by an ``alloc_ref`` with ``tail_elems``.
3315-
It is undefined behavior if the class instance does not have tail-allocated
3316-
arrays or if the element-types do not match.
3317-
33183259
Enums
33193260
~~~~~
33203261

include/swift/AST/Builtins.def

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -252,23 +252,9 @@ BUILTIN_SIL_OPERATION(ReinterpretCast, "reinterpretCast", Special)
252252
/// valid within the scope of the statement for logical lvalues.
253253
BUILTIN_SIL_OPERATION(AddressOf, "addressof", Special)
254254

255-
/// GepRaw(Builtin.RawPointer, Builtin.Word) -> Builtin.RawPointer
256-
///
257-
/// Adds index bytes to a base pointer.
258-
BUILTIN_SIL_OPERATION(GepRaw, "gepRaw", Integer)
259-
260-
/// Gep (Builtin.RawPointer, Builtin.Word, T.Type) -> Builtin.RawPointer
261-
///
262-
/// Like the GepRaw-bultin, but multiplies the index by stride-of type 'T'.
255+
/// GetElementPtr has type (Builtin.RawPointer, T) -> Builtin.RawPointer
263256
BUILTIN_SIL_OPERATION(Gep, "gep", Integer)
264257

265-
/// getTailAddr(Builtin.RawPointer,
266-
/// Builtin.Word, T.Type, E.Type) -> Builtin.RawPointer
267-
///
268-
/// Like the Gep-builtin, but rounds up the resulting address to a tail-
269-
/// allocated element type 'E'.
270-
BUILTIN_SIL_OPERATION(GetTailAddr, "getTailAddr", Integer)
271-
272258
/// condfail(Int1) -> ()
273259
/// Triggers a runtime failure if the condition is true.
274260
BUILTIN_SIL_OPERATION(CondFail, "condfail", Special)
@@ -328,18 +314,6 @@ BUILTIN_SIL_OPERATION(IsUniqueOrPinned_native, "isUniqueOrPinned_native",
328314
/// bindMemory : <T> (Builtin.RawPointer, Builtin.Word, T.Type) -> ()
329315
BUILTIN_SIL_OPERATION(BindMemory, "bindMemory", Special)
330316

331-
/// allocWithTailElems_<n>(C.Type,
332-
/// Builtin.Word, E1.Type, ... , Builtin.Word, En.Type) -> C\
333-
///
334-
/// The integer suffix <n> specifies the number of tail-allocated arrays.
335-
/// Each tail-allocated array adds a counter and an element meta-type parameter.
336-
BUILTIN_SIL_OPERATION(AllocWithTailElems, "allocWithTailElems", Special)
337-
338-
/// projectTailElems : <C,E> (C) -> Builtin.RawPointer
339-
///
340-
/// Projects the first tail-allocated element of type E from a class C.
341-
BUILTIN_SIL_OPERATION(ProjectTailElems, "projectTailElems", Special)
342-
343317
#undef BUILTIN_SIL_OPERATION
344318

345319
// BUILTIN_RUNTIME_CALL - A call into a runtime function.

include/swift/AST/DiagnosticsParse.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,6 @@ ERROR(sil_too_many_substitutions,none,
511511
"too many substitutions", ())
512512
ERROR(sil_dbg_unknown_key,none,
513513
"unknown key '%0' in debug variable declaration", (StringRef))
514-
ERROR(sil_objc_with_tail_elements,none,
515-
"alloc_ref [objc] cannot have tail allocated elements", ())
516514

517515
// SIL Basic Blocks
518516
ERROR(expected_sil_block_name,none,

include/swift/LLVMPasses/Passes.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ namespace swift {
8787
SwiftARCContract() : llvm::FunctionPass(ID) {}
8888
};
8989

90+
class SwiftStackPromotion : public llvm::FunctionPass {
91+
virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override;
92+
virtual bool runOnFunction(llvm::Function &F) override;
93+
public:
94+
static char ID;
95+
SwiftStackPromotion() : llvm::FunctionPass(ID) {}
96+
};
97+
9098
class InlineTreePrinter : public llvm::ModulePass {
9199
virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override;
92100
virtual bool runOnModule(llvm::Module &M) override;

include/swift/LLVMPasses/PassesFwd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ namespace llvm {
2323
void initializeSwiftRCIdentityPass(PassRegistry &);
2424
void initializeSwiftARCOptPass(PassRegistry &);
2525
void initializeSwiftARCContractPass(PassRegistry &);
26+
void initializeSwiftStackPromotionPass(PassRegistry &);
2627
void initializeInlineTreePrinterPass(PassRegistry &);
2728
void initializeSwiftMergeFunctionsPass(PassRegistry &);
2829
}
2930

3031
namespace swift {
3132
llvm::FunctionPass *createSwiftARCOptPass();
3233
llvm::FunctionPass *createSwiftARCContractPass();
34+
llvm::FunctionPass *createSwiftStackPromotionPass();
3335
llvm::ModulePass *createInlineTreePrinterPass();
3436
llvm::ModulePass *createSwiftMergeFunctionsPass();
3537
llvm::ImmutablePass *createSwiftAAWrapperPass();

include/swift/SIL/InstructionUtils.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ namespace swift {
2020
/// Strip off casts/indexing insts/address projections from V until there is
2121
/// nothing left to strip.
2222
SILValue getUnderlyingObject(SILValue V);
23-
24-
/// Strip off indexing and address projections.
25-
///
26-
/// This is similar to getUnderlyingObject, except that it does not strip any
27-
/// object-to-address projections, like ref_element_addr. In other words, the
28-
/// result is always an address value.
29-
SILValue getUnderlyingAddressRoot(SILValue V);
30-
3123
SILValue getUnderlyingObjectStopAtMarkDependence(SILValue V);
3224

3325
SILValue stripSinglePredecessorArgs(SILValue V);

include/swift/SIL/Projection.h

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ enum class ProjectionKind : unsigned {
9191
Upcast = 0,
9292
RefCast = 1,
9393
BitwiseCast = 2,
94-
TailElems = 3,
9594
FirstPointerKind = Upcast,
96-
LastPointerKind = TailElems,
95+
LastPointerKind = BitwiseCast,
9796

9897
// Index Projection Kinds
9998
FirstIndexKind = 7,
@@ -125,7 +124,6 @@ static inline bool isCastProjectionKind(ProjectionKind Kind) {
125124
case ProjectionKind::Class:
126125
case ProjectionKind::Enum:
127126
case ProjectionKind::Box:
128-
case ProjectionKind::TailElems:
129127
return false;
130128
}
131129
}
@@ -160,12 +158,6 @@ struct ProjectionIndex {
160158
Aggregate = REA->getOperand();
161159
break;
162160
}
163-
case ValueKind::RefTailAddrInst: {
164-
RefTailAddrInst *REA = cast<RefTailAddrInst>(V);
165-
Index = 0;
166-
Aggregate = REA->getOperand();
167-
break;
168-
}
169161
case ValueKind::ProjectBoxInst: {
170162
ProjectBoxInst *PBI = cast<ProjectBoxInst>(V);
171163
// A box has only a single payload.
@@ -301,7 +293,6 @@ class Projection {
301293
case ProjectionKind::Upcast:
302294
case ProjectionKind::RefCast:
303295
case ProjectionKind::BitwiseCast:
304-
case ProjectionKind::TailElems:
305296
return getCastType(BaseType);
306297
case ProjectionKind::Index:
307298
// Index types do not change the underlying type.
@@ -332,19 +323,13 @@ class Projection {
332323

333324
SILType getCastType(SILType BaseType) const {
334325
assert(isValid());
326+
assert(getKind() == ProjectionKind::Upcast ||
327+
getKind() == ProjectionKind::RefCast ||
328+
getKind() == ProjectionKind::BitwiseCast);
335329
auto *Ty = getPointer();
336330
assert(Ty->isCanonical());
337-
switch (getKind()) {
338-
case ProjectionKind::Upcast:
339-
case ProjectionKind::RefCast:
340-
case ProjectionKind::BitwiseCast:
341-
return SILType::getPrimitiveType(Ty->getCanonicalType(),
342-
BaseType.getCategory());
343-
case ProjectionKind::TailElems:
344-
return SILType::getPrimitiveAddressType(Ty->getCanonicalType());
345-
default:
346-
llvm_unreachable("unknown cast projection type");
347-
}
331+
return SILType::getPrimitiveType(Ty->getCanonicalType(),
332+
BaseType.getCategory());
348333
}
349334

350335
bool operator<(const Projection &Other) const;
@@ -377,7 +362,6 @@ class Projection {
377362
}
378363
case ValueKind::StructElementAddrInst:
379364
case ValueKind::RefElementAddrInst:
380-
case ValueKind::RefTailAddrInst:
381365
case ValueKind::ProjectBoxInst:
382366
case ValueKind::TupleElementAddrInst:
383367
case ValueKind::UncheckedTakeEnumDataAddrInst:
@@ -401,8 +385,7 @@ class Projection {
401385
/// Returns true if this instruction projects from an object type into an
402386
/// address subtype.
403387
static bool isObjectToAddressProjection(SILValue V) {
404-
return isa<RefElementAddrInst>(V) || isa<RefTailAddrInst>(V) ||
405-
isa<ProjectBoxInst>(V);
388+
return isa<RefElementAddrInst>(V) || isa<ProjectBoxInst>(V);
406389
}
407390

408391
/// Given a specific SILType, return all first level projections if it is an
@@ -427,7 +410,6 @@ class Projection {
427410
case ProjectionKind::Tuple:
428411
case ProjectionKind::Index:
429412
case ProjectionKind::Class:
430-
case ProjectionKind::TailElems:
431413
case ProjectionKind::Enum:
432414
case ProjectionKind::Box:
433415
return false;
@@ -446,7 +428,6 @@ class Projection {
446428
case ProjectionKind::Tuple:
447429
case ProjectionKind::Upcast:
448430
case ProjectionKind::Box:
449-
case ProjectionKind::TailElems:
450431
return false;
451432
}
452433
}

include/swift/SIL/SILBuilder.h

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -255,26 +255,13 @@ class SILBuilder {
255255
Var));
256256
}
257257

258-
AllocRefInst *createAllocRef(SILLocation Loc, SILType ObjectType,
259-
bool objc, bool canAllocOnStack) {
258+
AllocRefInst *createAllocRef(SILLocation Loc, SILType elementType, bool objc,
259+
bool canAllocOnStack) {
260260
// AllocRefInsts expand to function calls and can therefore not be
261261
// counted towards the function prologue.
262262
assert(!Loc.isInPrologue());
263-
return insert(AllocRefInst::create(getSILDebugLocation(Loc), F, ObjectType,
263+
return insert(AllocRefInst::create(getSILDebugLocation(Loc), elementType, F,
264264
objc, canAllocOnStack,
265-
{}, {}, OpenedArchetypes));
266-
}
267-
268-
AllocRefInst *createAllocRef(SILLocation Loc, SILType ObjectType,
269-
bool canAllocOnStack,
270-
ArrayRef<SILType> ElementTypes,
271-
ArrayRef<SILValue> ElementCountOperands) {
272-
// AllocRefInsts expand to function calls and can therefore not be
273-
// counted towards the function prologue.
274-
assert(!Loc.isInPrologue());
275-
return insert(AllocRefInst::create(getSILDebugLocation(Loc),
276-
F, ObjectType, false, canAllocOnStack,
277-
ElementTypes, ElementCountOperands,
278265
OpenedArchetypes));
279266
}
280267

@@ -911,12 +898,6 @@ class SILBuilder {
911898
return createRefElementAddr(Loc, Operand, Field, ResultTy);
912899
}
913900

914-
RefTailAddrInst *createRefTailAddr(SILLocation Loc, SILValue Ref,
915-
SILType ResultTy) {
916-
return insert(new (F.getModule()) RefTailAddrInst(getSILDebugLocation(Loc),
917-
Ref, ResultTy));
918-
}
919-
920901
ClassMethodInst *createClassMethod(SILLocation Loc, SILValue Operand,
921902
SILDeclRef Member, SILType MethodTy,
922903
bool Volatile = false) {
@@ -1262,12 +1243,6 @@ class SILBuilder {
12621243
Operand, Index));
12631244
}
12641245

1265-
TailAddrInst *createTailAddr(SILLocation Loc, SILValue Operand,
1266-
SILValue Count, SILType ResultTy) {
1267-
return insert(new (F.getModule()) TailAddrInst(getSILDebugLocation(Loc),
1268-
Operand, Count, ResultTy));
1269-
}
1270-
12711246
IndexRawPointerInst *createIndexRawPointer(SILLocation Loc, SILValue Operand,
12721247
SILValue Index) {
12731248
return insert(new (F.getModule()) IndexRawPointerInst(

0 commit comments

Comments
 (0)