Skip to content

Commit d1be8b0

Browse files
authored
Merge pull request swiftlang#8824 from gottesmm/mark_uninitialized_fixup
2 parents 34a5e75 + aa76c2a commit d1be8b0

33 files changed

+539
-266
lines changed

include/swift/Basic/STLExtras.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,17 @@ inline void copy(const Container &C, OutputIterator iter) {
711711
std::copy(C.begin(), C.end(), iter);
712712
}
713713

714+
template <typename Container, typename OutputIterator, typename Predicate>
715+
inline void copy_if(const Container &C, OutputIterator result, Predicate pred) {
716+
std::copy_if(C.begin(), C.end(), result, pred);
717+
}
718+
719+
template <typename Container, typename OutputIterator, typename UnaryOperation>
720+
inline OutputIterator transform(const Container &C, OutputIterator result,
721+
UnaryOperation op) {
722+
return std::transform(C.begin(), C.end(), result, op);
723+
}
724+
714725
//===----------------------------------------------------------------------===//
715726
// Function Traits
716727
//===----------------------------------------------------------------------===//

include/swift/SIL/SILInstruction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,11 @@ class AllocBoxInst final
895895
return getType().castTo<SILBoxType>();
896896
}
897897

898+
// Return the type of the memory stored in the alloc_box.
899+
SILType getAddressType() const {
900+
return getBoxType()->getFieldType(getModule(), 0).getAddressType();
901+
}
902+
898903
/// Return the underlying variable declaration associated with this
899904
/// allocation, or null if this is a temporary allocation.
900905
VarDecl *getDecl() const;

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ PASS(ValueOwnershipKindDumper, "value-ownership-kind-dumper",
261261
"Print Value Ownership Kind for Testing")
262262
PASS(SemanticARCOpts, "semantic-arc-opts",
263263
"Semantic ARC Optimization")
264+
PASS(MarkUninitializedFixup, "mark-uninitialized-fixup",
265+
"Temporary pass for staging in mark_uninitialized changes.")
264266
PASS(BugReducerTester, "bug-reducer-tester",
265267
"sil-bug-reducer Tool Testing by Asserting on a Sentinel Function")
266268
PASS_RANGE(AllPasses, AADumper, BugReducerTester)

lib/SIL/SILOwnershipVerifier.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ class OwnershipCompatibilityUseChecker
303303
bool isAddressOrTrivialType() const {
304304
if (getType().isAddress())
305305
return true;
306-
return getOwnershipKind() == ValueOwnershipKind::Trivial;
306+
return getOwnershipKind() == ValueOwnershipKind::Trivial ||
307+
getOwnershipKind() == ValueOwnershipKind::Any;
307308
}
308309

309310
/// Depending on our initialization, either return false or call Func and
@@ -1928,6 +1929,9 @@ void SILInstruction::verifyOperandOwnership() const {
19281929
for (const Operand &Op : getAllOperands()) {
19291930
if (isTypeDependentOperand(Op))
19301931
continue;
1932+
// Skip any SILUndef that we see.
1933+
if (isa<SILUndef>(Op.get()))
1934+
continue;
19311935
OwnershipCompatibilityUseChecker(getModule(), Op, Op.get(), ErrorBehavior)
19321936
.check(Self);
19331937
}

lib/SIL/SILVerifier.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,9 +1415,17 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
14151415
require(MU->getModule().getStage() == SILStage::Raw,
14161416
"mark_uninitialized instruction can only exist in raw SIL");
14171417
require(Src->getType().isAddress() ||
1418-
Src->getType().getSwiftRValueType()->getClassOrBoundGenericClass(),
1419-
"mark_uninitialized must be an address or class");
1418+
Src->getType()
1419+
.getSwiftRValueType()
1420+
->getClassOrBoundGenericClass() ||
1421+
Src->getType().getAs<SILBoxType>(),
1422+
"mark_uninitialized must be an address, class, or box type");
14201423
require(Src->getType() == MU->getType(),"operand and result type mismatch");
1424+
#if 0
1425+
// This will be turned back on in a couple of commits.
1426+
require(isa<AllocationInst>(Src) || isa<SILArgument>(Src),
1427+
"Mark Uninitialized should always be on the storage location");
1428+
#endif
14211429
}
14221430

14231431
void checkMarkUninitializedBehaviorInst(MarkUninitializedBehaviorInst *MU) {
@@ -1888,9 +1896,19 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
18881896

18891897
require(AI->getType().isObject(),
18901898
"result of alloc_box must be an object");
1891-
for (unsigned field : indices(AI->getBoxType()->getLayout()->getFields()))
1899+
for (unsigned field : indices(AI->getBoxType()->getLayout()->getFields())) {
18921900
verifyOpenedArchetype(AI,
18931901
AI->getBoxType()->getFieldLoweredType(F.getModule(), field));
1902+
}
1903+
1904+
// An alloc_box with a mark_uninitialized user can not have any other users.
1905+
require(none_of(AI->getUses(),
1906+
[](Operand *Op) -> bool {
1907+
return isa<MarkUninitializedInst>(Op->getUser());
1908+
}) ||
1909+
AI->hasOneUse(),
1910+
"An alloc_box with a mark_uninitialized user can not have any "
1911+
"other users.");
18941912
}
18951913

18961914
void checkDeallocBoxInst(DeallocBoxInst *DI) {

lib/SILGen/SILGenConstructor.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,7 @@ void SILGenFunction::emitClassConstructorInitializer(ConstructorDecl *ctor) {
567567

568568
if (NeedsBoxForSelf) {
569569
// Allocate the local variable for 'self'.
570-
emitLocalVariableWithCleanup(selfDecl, None)->finishInitialization(*this);
571-
572-
auto &SelfVarLoc = VarLocs[selfDecl];
573-
SelfVarLoc.value = B.createMarkUninitialized(selfDecl,
574-
SelfVarLoc.value, MUKind);
570+
emitLocalVariableWithCleanup(selfDecl, MUKind)->finishInitialization(*this);
575571
}
576572

577573
// Emit the prolog for the non-self arguments.

lib/SILGen/SILGenDecl.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,14 @@ class LocalVariableInitialization : public SingleBufferInitialization {
386386

387387
// The variable may have its lifetime extended by a closure, heap-allocate
388388
// it using a box.
389-
AllocBoxInst *allocBox =
389+
SILInstruction *allocBox =
390390
SGF.B.createAllocBox(decl, boxType, {decl->isLet(), ArgNo});
391-
SILValue addr = SGF.B.createProjectBox(decl, allocBox, 0);
392391

393392
// Mark the memory as uninitialized, so DI will track it for us.
394393
if (kind)
395-
addr = SGF.B.createMarkUninitialized(decl, addr, kind.getValue());
394+
allocBox = SGF.B.createMarkUninitialized(decl, allocBox, kind.getValue());
395+
396+
SILValue addr = SGF.B.createProjectBox(decl, allocBox, 0);
396397

397398
/// Remember that this is the memory location that we're emitting the
398399
/// decl to.

lib/SILGen/SILGenFunction.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ void SILGenFunction::emitCaptures(SILLocation loc,
276276

277277
AllocBoxInst *allocBox = B.createAllocBox(loc, boxTy);
278278
ProjectBoxInst *boxAddress = B.createProjectBox(loc, allocBox, 0);
279-
B.createCopyAddr(loc, vl.value, boxAddress, IsNotTake,IsInitialization);
279+
B.createCopyAddr(loc, vl.value, boxAddress, IsNotTake,
280+
IsInitialization);
280281
capturedArgs.push_back(emitManagedRValueWithCleanup(allocBox));
281282
}
282283

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ static void addOwnershipModelEliminatorPipeline(SILPassPipelinePlan &P) {
7474
static void addMandatoryOptPipeline(SILPassPipelinePlan &P,
7575
const SILOptions &Options) {
7676
P.startPipeline("Guaranteed Passes");
77+
P.addMarkUninitializedFixup();
7778
if (Options.EnableMandatorySemanticARCOpts) {
7879
P.addSemanticARCOpts();
7980
}

lib/SILOptimizer/Transforms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(TRANSFORMS_SOURCES
1414
Transforms/FunctionSignatureOpts.cpp
1515
Transforms/GenericSpecializer.cpp
1616
Transforms/MergeCondFail.cpp
17+
Transforms/MarkUninitializedFixup.cpp
1718
Transforms/OwnershipModelEliminator.cpp
1819
Transforms/PerformanceInliner.cpp
1920
Transforms/RedundantLoadElimination.cpp

0 commit comments

Comments
 (0)