Skip to content

Commit 46fd9b3

Browse files
authored
Merge pull request #39574 from atrick/ossa-selectvalue
Fix SelectValue for OSSA
2 parents 5bce60f + 99f49d9 commit 46fd9b3

File tree

7 files changed

+31
-34
lines changed

7 files changed

+31
-34
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,18 +1526,9 @@ class SILBuilder {
15261526
SelectValueInst *createSelectValue(
15271527
SILLocation Loc, SILValue Operand, SILType Ty, SILValue DefaultResult,
15281528
ArrayRef<std::pair<SILValue, SILValue>> CaseValuesAndResult) {
1529-
return createSelectValue(Loc, Operand, Ty, DefaultResult,
1530-
CaseValuesAndResult, Operand.getOwnershipKind());
1531-
}
1532-
1533-
SelectValueInst *
1534-
createSelectValue(SILLocation Loc, SILValue Operand, SILType Ty,
1535-
SILValue DefaultResult,
1536-
ArrayRef<std::pair<SILValue, SILValue>> CaseValuesAndResult,
1537-
ValueOwnershipKind forwardingOwnershipKind) {
1538-
return insert(SelectValueInst::create(
1539-
getSILDebugLocation(Loc), Operand, Ty, DefaultResult,
1540-
CaseValuesAndResult, getModule(), forwardingOwnershipKind));
1529+
return insert(SelectValueInst::create(getSILDebugLocation(Loc), Operand, Ty,
1530+
DefaultResult, CaseValuesAndResult,
1531+
getModule()));
15411532
}
15421533

15431534
TupleExtractInst *createTupleExtract(SILLocation Loc, SILValue Operand,

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2815,10 +2815,7 @@ SILCloner<ImplClass>::visitSelectValueInst(SelectValueInst *Inst) {
28152815
recordClonedInstruction(
28162816
Inst, getBuilder().createSelectValue(
28172817
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
2818-
getOpType(Inst->getType()), DefaultResult, CaseResults,
2819-
getBuilder().hasOwnership()
2820-
? Inst->getForwardingOwnershipKind()
2821-
: ValueOwnershipKind(OwnershipKind::None)));
2818+
getOpType(Inst->getType()), DefaultResult, CaseResults));
28222819
}
28232820

28242821
template <typename ImplClass>

include/swift/SIL/SILInstruction.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,8 +1089,7 @@ class OwnershipForwardingMixin {
10891089
}
10901090
};
10911091

1092-
/// A single value inst that forwards a static ownership from one (or all) of
1093-
/// its operands.
1092+
/// A single value inst that forwards a static ownership from its first operand.
10941093
///
10951094
/// The ownership kind is set on construction and afterwards must be changed
10961095
/// explicitly using setOwnershipKind().
@@ -6263,20 +6262,17 @@ class SelectEnumAddrInst final
62636262
class SelectValueInst final
62646263
: public InstructionBaseWithTrailingOperands<
62656264
SILInstructionKind::SelectValueInst, SelectValueInst,
6266-
SelectInstBase<SelectValueInst, SILValue,
6267-
FirstArgOwnershipForwardingSingleValueInst>> {
6265+
SelectInstBase<SelectValueInst, SILValue, SingleValueInstruction>> {
62686266
friend SILBuilder;
62696267

62706268
SelectValueInst(SILDebugLocation DebugLoc, SILValue Operand, SILType Type,
62716269
SILValue DefaultResult,
6272-
ArrayRef<SILValue> CaseValuesAndResults,
6273-
ValueOwnershipKind forwardingOwnershipKind);
6270+
ArrayRef<SILValue> CaseValuesAndResults);
62746271

62756272
static SelectValueInst *
62766273
create(SILDebugLocation DebugLoc, SILValue Operand, SILType Type,
62776274
SILValue DefaultValue,
6278-
ArrayRef<std::pair<SILValue, SILValue>> CaseValues, SILModule &M,
6279-
ValueOwnershipKind forwardingOwnershipKind);
6275+
ArrayRef<std::pair<SILValue, SILValue>> CaseValues, SILModule &M);
62806276

62816277
public:
62826278
std::pair<SILValue, SILValue>

lib/SIL/IR/OperandOwnership.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ OPERAND_OWNERSHIP(TrivialUse, ProjectBlockStorage)
170170
OPERAND_OWNERSHIP(TrivialUse, ProjectValueBuffer)
171171
OPERAND_OWNERSHIP(TrivialUse, RawPointerToRef)
172172
OPERAND_OWNERSHIP(TrivialUse, SelectEnumAddr)
173-
OPERAND_OWNERSHIP(TrivialUse, SelectValue)
174173
OPERAND_OWNERSHIP(TrivialUse, StructElementAddr)
175174
OPERAND_OWNERSHIP(TrivialUse, SwitchEnumAddr)
176175
OPERAND_OWNERSHIP(TrivialUse, SwitchValue)
@@ -388,6 +387,23 @@ OperandOwnershipClassifier::visitSelectEnumInst(SelectEnumInst *i) {
388387
/*allowUnowned*/true);
389388
}
390389

390+
OperandOwnership
391+
OperandOwnershipClassifier::visitSelectValueInst(SelectValueInst *i) {
392+
if (getValue() == i->getDefaultResult())
393+
return OperandOwnership::ForwardingBorrow;
394+
395+
for (unsigned idx = 0, endIdx = i->getNumCases(); idx < endIdx; ++idx) {
396+
SILValue casevalue;
397+
SILValue result;
398+
std::tie(casevalue, result) = i->getCase(idx);
399+
400+
if (getValue() == casevalue) {
401+
return OperandOwnership::ForwardingBorrow;
402+
}
403+
}
404+
return OperandOwnership::TrivialUse;
405+
}
406+
391407
OperandOwnership OperandOwnershipClassifier::visitBranchInst(BranchInst *bi) {
392408
ValueOwnershipKind destBlockArgOwnershipKind =
393409
bi->getDestBB()->getArgument(getOperandIndex())->getOwnershipKind();

lib/SIL/IR/SILInstructions.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,17 +1777,15 @@ SwitchValueInst *SwitchValueInst::create(
17771777

17781778
SelectValueInst::SelectValueInst(SILDebugLocation DebugLoc, SILValue Operand,
17791779
SILType Type, SILValue DefaultResult,
1780-
ArrayRef<SILValue> CaseValuesAndResults,
1781-
ValueOwnershipKind forwardingOwnership)
1780+
ArrayRef<SILValue> CaseValuesAndResults)
17821781
: InstructionBaseWithTrailingOperands(Operand, CaseValuesAndResults,
1783-
DebugLoc, Type, forwardingOwnership) {
1784-
}
1782+
DebugLoc, Type) {}
17851783

17861784
SelectValueInst *
17871785
SelectValueInst::create(SILDebugLocation Loc, SILValue Operand, SILType Type,
17881786
SILValue DefaultResult,
17891787
ArrayRef<std::pair<SILValue, SILValue>> CaseValues,
1790-
SILModule &M, ValueOwnershipKind forwardingOwnership) {
1788+
SILModule &M) {
17911789
// Allocate enough room for the instruction with tail-allocated data for all
17921790
// the case values and the SILSuccessor arrays. There are `CaseBBs.size()`
17931791
// SILValues and `CaseBBs.size() + (DefaultBB ? 1 : 0)` successors.
@@ -1802,8 +1800,8 @@ SelectValueInst::create(SILDebugLocation Loc, SILValue Operand, SILType Type,
18021800

18031801
auto Size = totalSizeToAlloc<swift::Operand>(CaseValuesAndResults.size() + 1);
18041802
auto Buf = M.allocateInst(Size, alignof(SelectValueInst));
1805-
return ::new (Buf) SelectValueInst(Loc, Operand, Type, DefaultResult,
1806-
CaseValuesAndResults, forwardingOwnership);
1803+
return ::new (Buf)
1804+
SelectValueInst(Loc, Operand, Type, DefaultResult, CaseValuesAndResults);
18071805
}
18081806

18091807
template <typename SELECT_ENUM_INST>

lib/SIL/IR/SILPrinter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2363,7 +2363,6 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
23632363
*this << ", default " << Ctx.getID(SVI->getDefaultResult());
23642364

23652365
*this << " : " << SVI->getType();
2366-
printForwardingOwnershipKind(SVI, SVI->getOperand());
23672366
}
23682367

23692368
void visitDynamicMethodBranchInst(DynamicMethodBranchInst *DMBI) {

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ CONSTANT_OWNERSHIP_INST(None, ExtractExecutor)
167167
}
168168
CONSTANT_OR_NONE_OWNERSHIP_INST(Guaranteed, StructExtract)
169169
CONSTANT_OR_NONE_OWNERSHIP_INST(Guaranteed, TupleExtract)
170+
CONSTANT_OR_NONE_OWNERSHIP_INST(Guaranteed, SelectValue)
170171
CONSTANT_OR_NONE_OWNERSHIP_INST(Guaranteed, DifferentiableFunctionExtract)
171172
CONSTANT_OR_NONE_OWNERSHIP_INST(Guaranteed, LinearFunctionExtract)
172173
// OpenExistentialValue opens the boxed value inside an existential
@@ -252,7 +253,6 @@ FORWARDING_OWNERSHIP_INST(BridgeObjectToRef)
252253
FORWARDING_OWNERSHIP_INST(ConvertFunction)
253254
FORWARDING_OWNERSHIP_INST(OpenExistentialRef)
254255
FORWARDING_OWNERSHIP_INST(RefToBridgeObject)
255-
FORWARDING_OWNERSHIP_INST(SelectValue)
256256
FORWARDING_OWNERSHIP_INST(Object)
257257
FORWARDING_OWNERSHIP_INST(Struct)
258258
FORWARDING_OWNERSHIP_INST(Tuple)

0 commit comments

Comments
 (0)