Skip to content

Commit 175464a

Browse files
authored
Merge pull request swiftlang#26605 from gottesmm/pr-f6473de264baf653d815996dc951b702c9ead2fe
[ownership] When deserializing a SILFunction, match the ownership of …
2 parents 50d21fd + 93ed958 commit 175464a

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

include/swift/SIL/SILFunction.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ class SILFunction
318318
SILFunction *InsertBefore = nullptr,
319319
const SILDebugScope *DebugScope = nullptr);
320320

321+
/// Set has ownership to the given value. True means that the function has
322+
/// ownership, false means it does not.
323+
///
324+
/// Only for use by FunctionBuilders!
325+
void setHasOwnership(bool newValue) { HasOwnership = newValue; }
326+
321327
public:
322328
~SILFunction();
323329

@@ -444,9 +450,7 @@ class SILFunction
444450

445451
/// Sets the HasOwnership flag to false. This signals to SIL that no
446452
/// ownership instructions should be in this function any more.
447-
void setOwnershipEliminated() {
448-
HasOwnership = false;
449-
}
453+
void setOwnershipEliminated() { setHasOwnership(false); }
450454

451455
/// Returns true if this function was deserialized from canonical
452456
/// SIL. (.swiftmodule files contain canonical SIL; .sib files may be 'raw'

include/swift/SIL/SILFunctionBuilder.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,15 @@ class SILFunctionBuilder {
106106

107107
void addFunctionAttributes(SILFunction *F, DeclAttributes &Attrs,
108108
SILModule &M, SILDeclRef constant = SILDeclRef());
109+
110+
/// We do not expose this to everyone, instead we allow for our users to opt
111+
/// into this if they need to. Please do not do this in general! We only want
112+
/// to use this when deserializing a function body.
113+
static void setHasOwnership(SILFunction *F, bool newValue) {
114+
F->setHasOwnership(newValue);
115+
}
109116
};
117+
110118
} // namespace swift
111119

112120
#endif

lib/Serialization/DeserializeSIL.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ SILDeserializer::readSILFunctionChecked(DeclID FID, SILFunction *existingFn,
545545
if (SILMod.isSerialized())
546546
isSerialized = IsNotSerialized;
547547

548+
SILSerializationFunctionBuilder builder(SILMod);
549+
548550
// If we have an existing function, verify that the types match up.
549551
if (fn) {
550552
if (fn->getLoweredType() != ty) {
@@ -576,7 +578,6 @@ SILDeserializer::readSILFunctionChecked(DeclID FID, SILFunction *existingFn,
576578

577579
} else {
578580
// Otherwise, create a new function.
579-
SILSerializationFunctionBuilder builder(SILMod);
580581
fn = builder.createDeclaration(name, ty, loc);
581582
fn->setLinkage(linkage.getValue());
582583
fn->setTransparent(IsTransparent_t(isTransparent == 1));
@@ -599,19 +600,22 @@ SILDeserializer::readSILFunctionChecked(DeclID FID, SILFunction *existingFn,
599600
for (auto ID : SemanticsIDs) {
600601
fn->addSemanticsAttr(MF->getIdentifierText(ID));
601602
}
602-
if (!hasQualifiedOwnership)
603-
fn->setOwnershipEliminated();
604603
if (Callback) Callback->didDeserialize(MF->getAssociatedModule(), fn);
605604
}
605+
606+
// First before we do /anything/ validate that our function is truly empty.
607+
assert(fn->empty() && "SILFunction to be deserialized starts being empty.");
608+
609+
// Given that our original function was empty, just match the deserialized
610+
// function. Ownership doesn't really have a meaning without a body.
611+
builder.setHasOwnership(fn, hasQualifiedOwnership);
612+
606613
// Mark this function as deserialized. This avoids rerunning diagnostic
607614
// passes. Certain passes in the madatory pipeline may not work as expected
608615
// after arbitrary optimization and lowering.
609616
if (!MF->IsSIB)
610617
fn->setWasDeserializedCanonical();
611618

612-
assert(fn->empty() &&
613-
"SILFunction to be deserialized starts being empty.");
614-
615619
fn->setBare(IsBare);
616620
const SILDebugScope *DS = fn->getDebugScope();
617621
if (!DS) {
@@ -675,9 +679,6 @@ SILDeserializer::readSILFunctionChecked(DeclID FID, SILFunction *existingFn,
675679
&& "function already has context generic params?!");
676680
if (genericEnv)
677681
fn->setGenericEnvironment(genericEnv);
678-
if (!hasQualifiedOwnership) {
679-
fn->setOwnershipEliminated();
680-
}
681682

682683
scratch.clear();
683684
kind = SILCursor.readRecord(entry.ID, scratch);

lib/Serialization/SILSerializationFunctionBuilder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class LLVM_LIBRARY_VISIBILITY SILSerializationFunctionBuilder {
3333
IsNotSerialized, IsNotDynamic, ProfileCounter(), IsNotThunk,
3434
SubclassScope::NotApplicable);
3535
}
36+
37+
void setHasOwnership(SILFunction *f, bool newValue) {
38+
builder.setHasOwnership(f, newValue);
39+
}
3640
};
3741

3842
} // namespace swift

0 commit comments

Comments
 (0)