Skip to content

Commit c093bc2

Browse files
authored
Merge pull request #71879 from gottesmm/pr-2299957baf71ed51bddde5296979d58d7f97d91c
[sil] Ensure that all SILValues have a parent function by making it so that SILUndef is uniqued at the function instead of module level.
2 parents f5a7ef2 + e4f3655 commit c093bc2

Some content is hidden

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

62 files changed

+537
-388
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LetPropertyLowering.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ private func insertEndInitInstructions(
9999
atEndOf initRegion: InstructionRange,
100100
_ context: FunctionPassContext
101101
) {
102-
var ssaUpdater = SSAUpdater(type: markUninitialized.type, ownership: .owned, context)
102+
var ssaUpdater = SSAUpdater(function: markUninitialized.parentFunction,
103+
type: markUninitialized.type, ownership: .owned, context)
103104
ssaUpdater.addAvailableValue(markUninitialized, in: markUninitialized.parentBlock)
104105

105106
for endInst in initRegion.ends {

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/RedundantLoadElimination.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ private extension LoadInst {
236236
}
237237

238238
private func replace(load: LoadInst, with availableValues: [AvailableValue], _ context: FunctionPassContext) {
239-
var ssaUpdater = SSAUpdater(type: load.type, ownership: load.ownership, context)
239+
var ssaUpdater = SSAUpdater(function: load.parentFunction,
240+
type: load.type, ownership: load.ownership, context)
240241

241242
for availableValue in availableValues {
242243
let block = availableValue.instruction.parentBlock

SwiftCompilerSources/Sources/Optimizer/Utilities/SSAUpdater.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import OptimizerBridging
1717
struct SSAUpdater<Context: MutatingContext> {
1818
let context: Context
1919

20-
init(type: Type, ownership: Ownership, _ context: Context) {
20+
init(function: Function, type: Type, ownership: Ownership,
21+
_ context: Context) {
2122
self.context = context
22-
context._bridged.SSAUpdater_initialize(type.bridged, ownership._bridged)
23+
context._bridged.SSAUpdater_initialize(function.bridged, type.bridged,
24+
ownership._bridged)
2325
}
2426

2527
mutating func addAvailableValue(_ value: Value, in block: BasicBlock) {

include/swift/SIL/SILBuilder.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ class SILBuilder {
200200
assert(F && "cannot create this instruction without a function context");
201201
return *F;
202202
}
203-
203+
204+
/// If this SILBuilder is inserting into a function, return that function. If
205+
/// we are inserting into a global, this returns nullptr.
206+
SILFunction *maybeGetFunction() const { return F; }
207+
204208
bool isInsertingIntoGlobal() const { return F == nullptr; }
205209

206210
TypeExpansionContext getTypeExpansionContext() const {

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ SILCloner<ImplClass>::getMappedValue(SILValue Value) {
593593
if (auto *U = dyn_cast<SILUndef>(Value)) {
594594
auto type = getOpType(U->getType());
595595
ValueBase *undef =
596-
(type == U->getType() ? U : SILUndef::get(type, Builder.getFunction()));
596+
(type == U->getType() ? U : SILUndef::get(Builder.getFunction(), type));
597597
return SILValue(undef);
598598
}
599599

include/swift/SIL/SILFunction.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class BasicBlockBitfield;
4040
class NodeBitfield;
4141
class OperandBitfield;
4242
class CalleeCache;
43+
class SILUndef;
4344

4445
namespace Lowering {
4546
class TypeLowering;
@@ -211,6 +212,7 @@ class SILFunction
211212
friend class BasicBlockBitfield;
212213
friend class NodeBitfield;
213214
friend class OperandBitfield;
215+
friend SILUndef;
214216

215217
/// Module - The SIL module that the function belongs to.
216218
SILModule &Module;
@@ -329,6 +331,13 @@ class SILFunction
329331

330332
PerformanceConstraints perfConstraints = PerformanceConstraints::None;
331333

334+
/// This is the set of undef values we've created, for uniquing purposes.
335+
///
336+
/// We use a SmallDenseMap since in most functions, we will have only one type
337+
/// of undef if we have any at all. In that case, by staying small we avoid
338+
/// needing a heap allocation.
339+
llvm::SmallDenseMap<SILType, SILUndef *, 1> undefValues;
340+
332341
/// This is the number of uses of this SILFunction inside the SIL.
333342
/// It does not include references from debug scopes.
334343
unsigned RefCount = 0;
@@ -1558,6 +1567,12 @@ class SILFunction
15581567
/// This is a fast subset of the checks performed in the SILVerifier.
15591568
void verifyCriticalEdges() const;
15601569

1570+
/// Validate that all SILUndefs stored in the function's type -> SILUndef map
1571+
/// have this function as their parent function.
1572+
///
1573+
/// Please only call this from the SILVerifier.
1574+
void verifySILUndefMap() const;
1575+
15611576
/// Pretty-print the SILFunction.
15621577
void dump(bool Verbose) const;
15631578
void dump() const;

include/swift/SIL/SILModule.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ class FuncDecl;
108108
class IRGenOptions;
109109
class KeyPathPattern;
110110
class ModuleDecl;
111-
class SILUndef;
112111
class SourceFile;
113112
class SerializedSILLoader;
114113
class SILFunctionBuilder;
@@ -192,7 +191,6 @@ class SILModule {
192191
friend SILType;
193192
friend SILVTable;
194193
friend SILProperty;
195-
friend SILUndef;
196194
friend SILWitnessTable;
197195
friend SILMoveOnlyDeinit;
198196
friend Lowering::SILGenModule;
@@ -315,9 +313,6 @@ class SILModule {
315313
/// This is a cache of builtin Function declarations to numeric ID mappings.
316314
llvm::DenseMap<Identifier, BuiltinInfo> BuiltinIDCache;
317315

318-
/// This is the set of undef values we've created, for uniquing purposes.
319-
llvm::DenseMap<SILType, SILUndef *> UndefValues;
320-
321316
llvm::DenseMap<std::pair<Decl *, VarDecl *>, unsigned> fieldIndices;
322317
llvm::DenseMap<EnumElementDecl *, unsigned> enumCaseIndices;
323318

include/swift/SIL/SILNode.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,13 @@ class alignas(8) SILNode :
409409
/// otherwise return null.
410410
SILBasicBlock *getParentBlock() const;
411411

412-
/// If this is a SILArgument or a SILInstruction get its parent function,
413-
/// otherwise return null.
412+
/// Returns the parent function of this value.
413+
///
414+
/// Only returns nullptr if the given value's parent is a sil global variable
415+
/// initializer.
414416
SILFunction *getFunction() const;
415417

416-
/// If this is a SILArgument or a SILInstruction get its parent module,
417-
/// otherwise return null.
418+
/// Return the parent module of this value.
418419
SILModule *getModule() const;
419420

420421
/// Pretty-print the node. If the node is an instruction, the output

include/swift/SIL/SILUndef.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,36 @@ class SILInstruction;
2323
class SILModule;
2424

2525
class SILUndef : public ValueBase {
26-
SILUndef(SILType type);
26+
/// A back pointer to the function that this SILUndef is uniqued by.
27+
SILFunction *parent;
28+
29+
SILUndef(SILFunction *parent, SILType type);
2730

2831
public:
2932
void operator=(const SILArgument &) = delete;
3033
void operator delete(void *, size_t) = delete;
3134

32-
static SILUndef *get(SILType ty, SILModule &m);
33-
3435
/// Return a SILUndef with the same type as the passed in value.
3536
static SILUndef *get(SILValue value) {
36-
return SILUndef::get(value->getType(), *value->getModule());
37+
return SILUndef::get(value->getFunction(), value->getType());
3738
}
3839

39-
static SILUndef *get(SILType ty, const SILFunction &f);
40+
static SILUndef *get(SILFunction *f, SILType ty);
41+
static SILUndef *get(SILFunction &f, SILType ty) {
42+
return SILUndef::get(&f, ty);
43+
}
4044

45+
/// This is an API only used by SILSSAUpdater... please do not use it anywhere
46+
/// else.
4147
template <class OwnerTy>
42-
static SILUndef *getSentinelValue(SILType type, OwnerTy owner) {
48+
static SILUndef *getSentinelValue(SILFunction *fn, OwnerTy owner,
49+
SILType type) {
4350
// Ownership kind isn't used here, the value just needs to have a unique
4451
// address.
45-
return new (*owner) SILUndef(type);
52+
return new (*owner) SILUndef(fn, type);
4653
}
4754

55+
SILFunction *getParent() const { return parent; }
4856
ValueOwnershipKind getOwnershipKind() const { return OwnershipKind::None; }
4957

5058
static bool classof(const SILArgument *) = delete;

include/swift/SIL/SILValue.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,16 +1592,20 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, SILValue V) {
15921592

15931593
/// Used internally in e.g. the SIL parser and deserializer to handle forward-
15941594
/// referenced values.
1595+
///
15951596
/// A PlaceholderValue must not appear in valid SIL.
15961597
class PlaceholderValue : public ValueBase {
1598+
SILFunction *parentFunction;
15971599
static int numPlaceholderValuesAlive;
15981600

15991601
public:
1600-
PlaceholderValue(SILType type);
1602+
PlaceholderValue(SILFunction *parentFunction, SILType type);
16011603
~PlaceholderValue();
16021604

16031605
static int getNumPlaceholderValuesAlive() { return numPlaceholderValuesAlive; }
16041606

1607+
SILFunction *getParent() const { return parentFunction; }
1608+
16051609
static bool classof(const SILArgument *) = delete;
16061610
static bool classof(const SILInstruction *) = delete;
16071611
static bool classof(SILNodePointer node) {

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,9 @@ struct BridgedPassContext {
307307

308308
// SSAUpdater
309309

310-
BRIDGED_INLINE void SSAUpdater_initialize(BridgedType type, BridgedValue::Ownership ownership) const;
310+
BRIDGED_INLINE void
311+
SSAUpdater_initialize(BridgedFunction function, BridgedType type,
312+
BridgedValue::Ownership ownership) const;
311313
BRIDGED_INLINE void SSAUpdater_addAvailableValue(BridgedBasicBlock block, BridgedValue value) const;
312314
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue SSAUpdater_getValueAtEndOfBlock(BridgedBasicBlock block) const;
313315
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue SSAUpdater_getValueInMiddleOfBlock(BridgedBasicBlock block) const;

include/swift/SILOptimizer/OptimizerBridgingImpl.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void BridgedPassContext::moveInstructionBefore(BridgedInstruction inst, BridgedI
229229
}
230230

231231
BridgedValue BridgedPassContext::getSILUndef(BridgedType type) const {
232-
return {swift::SILUndef::get(type.unbridged(), *invocation->getFunction())};
232+
return {swift::SILUndef::get(invocation->getFunction(), type.unbridged())};
233233
}
234234

235235
bool BridgedPassContext::optimizeMemoryAccesses(BridgedFunction f) const {
@@ -410,8 +410,10 @@ bool BridgedPassContext::continueWithNextSubpassRun(OptionalBridgedInstruction i
410410
inst.unbridged(), invocation->getFunction(), invocation->getTransform());
411411
}
412412

413-
void BridgedPassContext::SSAUpdater_initialize(BridgedType type, BridgedValue::Ownership ownership) const {
414-
invocation->initializeSSAUpdater(type.unbridged(),
413+
void BridgedPassContext::SSAUpdater_initialize(
414+
BridgedFunction function, BridgedType type,
415+
BridgedValue::Ownership ownership) const {
416+
invocation->initializeSSAUpdater(function.getFunction(), type.unbridged(),
415417
BridgedValue::castToOwnership(ownership));
416418
}
417419

include/swift/SILOptimizer/PassManager/PassManager.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,11 @@ class SwiftPassInvocation {
160160
void setNeedFixStackNesting(bool newValue) { needFixStackNesting = newValue; }
161161
bool getNeedFixStackNesting() const { return needFixStackNesting; }
162162

163-
void initializeSSAUpdater(SILType type, ValueOwnershipKind ownership) {
163+
void initializeSSAUpdater(SILFunction *fn, SILType type,
164+
ValueOwnershipKind ownership) {
164165
if (!ssaUpdater)
165166
ssaUpdater = new SILSSAUpdater;
166-
ssaUpdater->initialize(type, ownership);
167+
ssaUpdater->initialize(fn, type, ownership);
167168
}
168169

169170
SILSSAUpdater *getSSAUpdater() const {

include/swift/SILOptimizer/Utils/SILSSAUpdater.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ class SILSSAUpdater {
7979
}
8080

8181
/// Initialize for a use of a value of type and ownershipKind
82-
void initialize(SILType type, ValueOwnershipKind ownershipKind);
82+
void initialize(SILFunction *fn, SILType type,
83+
ValueOwnershipKind ownershipKind);
8384

8485
bool hasValueForBlock(SILBasicBlock *block) const;
8586
void addAvailableValue(SILBasicBlock *block, SILValue value);

lib/IRGen/IRGenSIL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2809,7 +2809,7 @@ void IRGenSILFunction::visitDifferentiableFunctionInst(
28092809
i->getModule().Types,
28102810
LookUpConformanceInModule(i->getModule().getSwiftModule()));
28112811
auto *undef = SILUndef::get(
2812-
SILType::getPrimitiveObjectType(derivativeFnType), *i->getFunction());
2812+
i->getFunction(), SILType::getPrimitiveObjectType(derivativeFnType));
28132813
return getLoweredExplosion(undef);
28142814
};
28152815
auto jvpExp = getDerivativeExplosion(AutoDiffDerivativeFunctionKind::JVP);

lib/IRGen/LoadableByAddress.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ void LoadableStorageAllocation::allocateLoadableStorage() {
13831383
SILArgument *LoadableStorageAllocation::replaceArgType(SILBuilder &argBuilder,
13841384
SILArgument *arg,
13851385
SILType newSILType) {
1386-
SILValue undef = SILUndef::get(newSILType, *pass.F);
1386+
SILValue undef = SILUndef::get(pass.F, newSILType);
13871387
SmallVector<Operand *, 8> useList(arg->use_begin(), arg->use_end());
13881388
for (auto *use : useList) {
13891389
use->set(undef);
@@ -1459,7 +1459,7 @@ void LoadableStorageAllocation::convertIndirectFunctionArgs() {
14591459

14601460
static void convertBBArgType(SILBuilder &argBuilder, SILType newSILType,
14611461
SILArgument *arg) {
1462-
SILValue undef = SILUndef::get(newSILType, argBuilder.getFunction());
1462+
SILValue undef = SILUndef::get(argBuilder.getFunction(), newSILType);
14631463
SmallVector<Operand *, 8> useList(arg->use_begin(), arg->use_end());
14641464
for (auto *use : useList) {
14651465
use->set(undef);

lib/SIL/IR/SILBasicBlock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ SILPhiArgument *SILBasicBlock::replacePhiArgumentAndReplaceAllUses(
232232
// replacePhiArgument() expects the replaced argument to not have
233233
// any uses.
234234
SmallVector<Operand *, 16> operands;
235-
SILValue undef = SILUndef::get(ty, *getParent());
235+
SILValue undef = SILUndef::get(getParent(), ty);
236236
SILArgument *arg = getArgument(i);
237237
while (!arg->use_empty()) {
238238
Operand *use = *arg->use_begin();

lib/SIL/IR/SILModule.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,8 @@ SILValue SILModule::getRootLocalArchetypeDef(CanLocalArchetypeType archetype,
680680
SILValue &def = RootLocalArchetypeDefs[{archetype, inFunction}];
681681
if (!def) {
682682
numUnresolvedLocalArchetypes++;
683-
def = ::new PlaceholderValue(SILType::getPrimitiveAddressType(archetype));
683+
def = ::new PlaceholderValue(inFunction,
684+
SILType::getPrimitiveAddressType(archetype));
684685
}
685686

686687
return def;
@@ -760,6 +761,12 @@ void SILModule::notifyAddedInstruction(SILInstruction *inst) {
760761

761762
void SILModule::notifyMovedInstruction(SILInstruction *inst,
762763
SILFunction *fromFunction) {
764+
for (auto &op : inst->getAllOperands()) {
765+
if (auto *undef = dyn_cast<SILUndef>(op.get())) {
766+
op.set(SILUndef::get(inst->getFunction(), undef->getType()));
767+
}
768+
}
769+
763770
inst->forEachDefinedLocalArchetype([&](CanLocalArchetypeType archeTy,
764771
SILValue dependency) {
765772
LocalArchetypeKey key = {archeTy, fromFunction};

lib/SIL/IR/SILUndef.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@
1515

1616
using namespace swift;
1717

18-
SILUndef::SILUndef(SILType type)
19-
: ValueBase(ValueKind::SILUndef, type) {}
18+
SILUndef::SILUndef(SILFunction *parent, SILType type)
19+
: ValueBase(ValueKind::SILUndef, type), parent(parent) {}
2020

21-
SILUndef *SILUndef::get(SILType ty, SILModule &m) {
22-
SILUndef *&entry = m.UndefValues[ty];
21+
SILUndef *SILUndef::get(SILFunction *fn, SILType ty) {
22+
SILUndef *&entry = fn->undefValues[ty];
2323
if (entry == nullptr)
24-
entry = new (m) SILUndef(ty);
24+
entry = new (fn->getModule()) SILUndef(fn, ty);
2525
return entry;
2626
}
27-
28-
SILUndef *SILUndef::get(SILType ty, const SILFunction &f) {
29-
return SILUndef::get(ty, f.getModule());
30-
}

lib/SIL/IR/SILValue.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void ValueBase::replaceAllUsesWithUndef() {
6262
}
6363
while (!use_empty()) {
6464
Operand *Op = *use_begin();
65-
Op->set(SILUndef::get(Op->get()->getType(), *F));
65+
Op->set(SILUndef::get(F, Op->get()->getType()));
6666
}
6767
}
6868

@@ -213,17 +213,25 @@ SILBasicBlock *SILNode::getParentBlock() const {
213213
}
214214

215215
SILFunction *SILNode::getFunction() const {
216-
if (auto *parentBlock = getParentBlock())
217-
return parentBlock->getParent();
218-
return nullptr;
219-
}
216+
if (auto *parentBlock = getParentBlock()) {
217+
// This can return nullptr if the block's parent is a global variable
218+
// initializer.
219+
if (auto *parentFunction = parentBlock->getParent()) {
220+
return parentFunction;
221+
}
222+
}
223+
224+
if (auto *undef = dyn_cast<SILUndef>(this))
225+
return undef->getParent();
226+
227+
if (auto *placeHolder = dyn_cast<PlaceholderValue>(this))
228+
return placeHolder->getParent();
220229

221-
SILModule *SILNode::getModule() const {
222-
if (SILFunction *func = getFunction())
223-
return &func->getModule();
224230
return nullptr;
225231
}
226232

233+
SILModule *SILNode::getModule() const { return &getFunction()->getModule(); }
234+
227235
/// Get a location for this value.
228236
SILLocation SILValue::getLoc() const {
229237
if (auto *instr = Value->getDefiningInstruction())
@@ -535,8 +543,8 @@ llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
535543

536544
int PlaceholderValue::numPlaceholderValuesAlive = 0;
537545

538-
PlaceholderValue::PlaceholderValue(SILType type)
539-
: ValueBase(ValueKind::PlaceholderValue, type) {
546+
PlaceholderValue::PlaceholderValue(SILFunction *fn, SILType type)
547+
: ValueBase(ValueKind::PlaceholderValue, type), parentFunction(fn) {
540548
numPlaceholderValuesAlive++;
541549
}
542550

0 commit comments

Comments
 (0)