Skip to content

Commit 804eac6

Browse files
authored
Merge pull request #68191 from rjmccall/wip2-variadic-arg-translation
More progress towards variadic-tuple argument reabstraction
2 parents 972229d + acece4c commit 804eac6

File tree

8 files changed

+672
-327
lines changed

8 files changed

+672
-327
lines changed

lib/SILGen/ManagedValue.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class ManagedValue {
9090
}
9191

9292
public:
93+
/// Constructs an invalid ManagedValue.
9394
ManagedValue() = default;
9495

9596
/// Sometimes SILGen wants to represent an owned value or owned address
@@ -251,6 +252,10 @@ class ManagedValue {
251252
return ManagedValue(SILValue(), true, CleanupHandle::invalid());
252253
}
253254

255+
bool isValid() const {
256+
return valueAndFlag.getInt() || valueAndFlag.getPointer();
257+
}
258+
254259
bool isLValue() const {
255260
return valueAndFlag.getInt() && valueAndFlag.getPointer();
256261
}

lib/SILGen/SILGenBuilder.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,20 @@ ManagedValue SILGenBuilder::createLoadTake(SILLocation loc, ManagedValue v,
486486
return SGF.emitManagedRValueWithCleanup(result, lowering);
487487
}
488488

489+
ManagedValue SILGenBuilder::createLoadTrivial(SILLocation loc,
490+
ManagedValue addr) {
491+
#ifndef NDEBUG
492+
auto &lowering = SGF.getTypeLowering(addr.getType());
493+
assert(lowering.isTrivial());
494+
assert((!lowering.isAddressOnly() || !SGF.silConv.useLoweredAddresses()) &&
495+
"cannot load an unloadable type");
496+
assert(!addr.hasCleanup());
497+
#endif
498+
auto value = createLoad(loc, addr.getValue(),
499+
LoadOwnershipQualifier::Trivial);
500+
return ManagedValue::forObjectRValueWithoutOwnership(value);
501+
}
502+
489503
ManagedValue SILGenBuilder::createLoadCopy(SILLocation loc, ManagedValue v) {
490504
auto &lowering = SGF.getTypeLowering(v.getType());
491505
return createLoadCopy(loc, v, lowering);

lib/SILGen/SILGenBuilder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ class SILGenBuilder : public SILBuilder {
267267
ManagedValue createLoadCopy(SILLocation loc, ManagedValue addr,
268268
const TypeLowering &lowering);
269269

270+
ManagedValue createLoadTrivial(SILLocation loc, ManagedValue addr);
271+
270272
/// Create a SILArgument for an input parameter. Asserts if used to create a
271273
/// function argument for an out parameter.
272274
ManagedValue createInputFunctionArgument(

lib/SILGen/SILGenFunction.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2515,6 +2515,13 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
25152515
CanPackType formalPackType,
25162516
unsigned componentIndex);
25172517

2518+
/// Enter a cleanup to destroy the preceding components of a pack,
2519+
/// leading up to (but not including) a particular component index.
2520+
CleanupHandle
2521+
enterDestroyPrecedingPackComponentsCleanup(SILValue addr,
2522+
CanPackType formalPackType,
2523+
unsigned componentIndex);
2524+
25182525
/// Enter a cleanup to destroy the preceding values in a pack-expansion
25192526
/// component of a tuple.
25202527
///
@@ -2766,7 +2773,8 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
27662773
void emitDestroyPack(SILLocation loc,
27672774
SILValue packAddr,
27682775
CanPackType formalPackType,
2769-
unsigned firstComponentIndex = 0);
2776+
unsigned beginIndex,
2777+
unsigned endIndex);
27702778

27712779
/// Emit instructions to destroy a suffix of a tuple value.
27722780
///

lib/SILGen/SILGenPack.cpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,26 @@ class DeallocPackCleanup : public Cleanup {
4444
class DestroyPackCleanup : public Cleanup {
4545
SILValue Addr;
4646
CanPackType FormalPackType;
47-
unsigned FirstComponentIndex;
47+
unsigned BeginIndex, EndIndex;
4848
public:
4949
DestroyPackCleanup(SILValue addr, CanPackType formalPackType,
50-
unsigned firstComponentIndex = 0)
50+
unsigned beginIndex, unsigned endIndex)
5151
: Addr(addr), FormalPackType(formalPackType),
52-
FirstComponentIndex(firstComponentIndex) {}
52+
BeginIndex(beginIndex), EndIndex(endIndex) {}
5353

5454
void emit(SILGenFunction &SGF, CleanupLocation l,
5555
ForUnwind_t forUnwind) override {
56-
SGF.emitDestroyPack(l, Addr, FormalPackType, FirstComponentIndex);
56+
SGF.emitDestroyPack(l, Addr, FormalPackType, BeginIndex, EndIndex);
5757
}
5858

5959
void dump(SILGenFunction &) const override {
6060
#ifndef NDEBUG
6161
llvm::errs() << "DestroyPackCleanup\n"
62-
<< "State: " << getState() << "\n"
63-
<< "Addr: " << Addr << "FormalPackType: " << FormalPackType
64-
<< "FirstComponentIndex:" << FirstComponentIndex << "\n";
62+
<< "State:" << getState() << "\n"
63+
<< "Addr:" << Addr << "\n"
64+
<< "FormalPackType:" << FormalPackType << "\n"
65+
<< "BeginIndex:" << BeginIndex << "\n"
66+
<< "EndIndex:" << EndIndex << "\n";
6567
#endif
6668
}
6769
};
@@ -295,19 +297,29 @@ CleanupHandle SILGenFunction::enterDeallocPackCleanup(SILValue temp) {
295297

296298
CleanupHandle SILGenFunction::enterDestroyPackCleanup(SILValue addr,
297299
CanPackType formalPackType) {
298-
Cleanups.pushCleanup<DestroyPackCleanup>(addr, formalPackType);
300+
Cleanups.pushCleanup<DestroyPackCleanup>(addr, formalPackType,
301+
0, formalPackType->getNumElements());
299302
return Cleanups.getTopCleanup();
300303
}
301304

302305
CleanupHandle
303-
SILGenFunction::enterDestroyRemainingPackComponentsCleanup(SILValue addr,
306+
SILGenFunction::enterDestroyPrecedingPackComponentsCleanup(SILValue addr,
304307
CanPackType formalPackType,
305308
unsigned componentIndex) {
306309
Cleanups.pushCleanup<DestroyPackCleanup>(addr, formalPackType,
307-
componentIndex);
310+
0, componentIndex);
308311
return Cleanups.getTopCleanup();
309312
}
310313

314+
CleanupHandle
315+
SILGenFunction::enterDestroyRemainingPackComponentsCleanup(SILValue addr,
316+
CanPackType formalPackType,
317+
unsigned componentIndex) {
318+
Cleanups.pushCleanup<DestroyPackCleanup>(addr, formalPackType,
319+
componentIndex,
320+
formalPackType->getNumElements());
321+
return Cleanups.getTopCleanup();
322+
}
311323

312324
CleanupHandle
313325
SILGenFunction::enterPartialDestroyPackCleanup(SILValue addr,
@@ -369,12 +381,15 @@ SILGenFunction::enterDestroyRemainingTupleElementsCleanup(SILValue addr,
369381

370382
void SILGenFunction::emitDestroyPack(SILLocation loc, SILValue packAddr,
371383
CanPackType formalPackType,
372-
unsigned firstComponentIndex) {
384+
unsigned beginIndex,
385+
unsigned endIndex) {
373386
auto packTy = packAddr->getType().castTo<SILPackType>();
374387

388+
assert(beginIndex <= endIndex);
389+
assert(endIndex <= packTy->getNumElements());
390+
375391
// Destroy each of the elements of the pack.
376-
for (auto componentIndex :
377-
range(firstComponentIndex, packTy->getNumElements())) {
392+
for (auto componentIndex : range(beginIndex, endIndex)) {
378393
auto eltTy = packTy->getSILElementType(componentIndex);
379394

380395
// We can skip this if the whole thing is trivial.

0 commit comments

Comments
 (0)