Skip to content

Change SIL ref_element_addr getFieldNo() to return a unique index. #34074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/SIL/PatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ template <typename LTy> struct tupleextractoperation_ty {

template <typename ITy> bool match(ITy *V) {
if (auto *TEI = dyn_cast<TupleExtractInst>(V)) {
return TEI->getFieldNo() == index &&
return TEI->getFieldIndex() == index &&
L.match((ValueBase *)TEI->getOperand());
}

Expand Down
17 changes: 8 additions & 9 deletions include/swift/SIL/Projection.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ struct ProjectionIndex {
}
case ValueKind::StructElementAddrInst: {
StructElementAddrInst *SEA = cast<StructElementAddrInst>(V);
Index = SEA->getFieldNo();
Index = SEA->getFieldIndex();
Aggregate = SEA->getOperand();
break;
}
case ValueKind::RefElementAddrInst: {
RefElementAddrInst *REA = cast<RefElementAddrInst>(V);
Index = REA->getFieldNo();
Index = REA->getFieldIndex();
Aggregate = REA->getOperand();
break;
}
Expand All @@ -177,19 +177,19 @@ struct ProjectionIndex {
}
case ValueKind::TupleElementAddrInst: {
TupleElementAddrInst *TEA = cast<TupleElementAddrInst>(V);
Index = TEA->getFieldNo();
Index = TEA->getFieldIndex();
Aggregate = TEA->getOperand();
break;
}
case ValueKind::StructExtractInst: {
StructExtractInst *SEA = cast<StructExtractInst>(V);
Index = SEA->getFieldNo();
Index = SEA->getFieldIndex();
Aggregate = SEA->getOperand();
break;
}
case ValueKind::TupleExtractInst: {
TupleExtractInst *TEA = cast<TupleExtractInst>(V);
Index = TEA->getFieldNo();
Index = TEA->getFieldIndex();
Aggregate = TEA->getOperand();
break;
}
Expand Down Expand Up @@ -302,10 +302,9 @@ class Projection {
assert(isValid());
assert((getKind() == ProjectionKind::Struct ||
getKind() == ProjectionKind::Class));
assert(BaseType.getNominalOrBoundGenericNominal() &&
"This should only be called with a nominal type");
auto *NDecl = BaseType.getNominalOrBoundGenericNominal();
return NDecl->getStoredProperties()[getIndex()];
auto *nominalDecl = BaseType.getNominalOrBoundGenericNominal();
assert(nominalDecl && "This should only be called with a nominal type");
return getIndexedField(nominalDecl, getIndex());
}

EnumElementDecl *getEnumElementDecl(SILType BaseType) const {
Expand Down
4 changes: 2 additions & 2 deletions include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,7 @@ SILCloner<ImplClass>::visitTupleExtractInst(TupleExtractInst *Inst) {
recordClonedInstruction(
Inst, getBuilder().createTupleExtract(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
Inst->getFieldNo(), getOpType(Inst->getType())));
Inst->getFieldIndex(), getOpType(Inst->getType())));
}

template<typename ImplClass>
Expand All @@ -1956,7 +1956,7 @@ SILCloner<ImplClass>::visitTupleElementAddrInst(TupleElementAddrInst *Inst) {
recordClonedInstruction(
Inst, getBuilder().createTupleElementAddr(
getOpLocation(Inst->getLoc()), getOpValue(Inst->getOperand()),
Inst->getFieldNo(), getOpType(Inst->getType())));
Inst->getFieldIndex(), getOpType(Inst->getType())));
}

template<typename ImplClass>
Expand Down
27 changes: 23 additions & 4 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -5715,7 +5715,7 @@ class TupleExtractInst
}

public:
unsigned getFieldNo() const {
unsigned getFieldIndex() const {
return SILInstruction::Bits.TupleExtractInst.FieldNo;
}

Expand Down Expand Up @@ -5747,7 +5747,7 @@ class TupleElementAddrInst
}

public:
unsigned getFieldNo() const {
unsigned getFieldIndex() const {
return SILInstruction::Bits.TupleElementAddrInst.FieldNo;
}

Expand All @@ -5757,6 +5757,26 @@ class TupleElementAddrInst
}
};

/// Get a unique index for a struct or class field in layout order.
///
/// Precondition: \p decl must be a non-resilient struct or class.
///
/// Precondition: \p field must be a stored property declared in \p decl,
/// not in a superclass.
///
/// Postcondition: The returned index is unique across all properties in the
/// object, including properties declared in a superclass.
unsigned getFieldIndex(NominalTypeDecl *decl, VarDecl *property);

/// Get the property for a struct or class by its unique index.
///
/// Precondition: \p decl must be a non-resilient struct or class.
///
/// Precondition: \p index must be the index of a stored property
/// (as returned by getFieldIndex()) which is declared
/// in \p decl, not in a superclass.
VarDecl *getIndexedField(NominalTypeDecl *decl, unsigned index);

/// A common base for instructions that require a cached field index.
///
/// "Field" is a term used here to refer to the ordered, accessible stored
Expand Down Expand Up @@ -5791,8 +5811,7 @@ class FieldIndexCacheBase : public SingleValueInstruction {

VarDecl *getField() const { return field; }

// FIXME: this should be called getFieldIndex().
unsigned getFieldNo() const {
unsigned getFieldIndex() const {
unsigned idx = SILInstruction::Bits.FieldIndexCacheBase.FieldIndex;
if (idx != InvalidFieldIndex)
return idx;
Expand Down
4 changes: 2 additions & 2 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3787,7 +3787,7 @@ void IRGenSILFunction::visitTupleExtractInst(swift::TupleExtractInst *i) {
projectTupleElementFromExplosion(*this,
baseType,
fullTuple,
i->getFieldNo(),
i->getFieldIndex(),
output);
(void)fullTuple.claimAll();
setLoweredExplosion(i, output);
Expand All @@ -3799,7 +3799,7 @@ void IRGenSILFunction::visitTupleElementAddrInst(swift::TupleElementAddrInst *i)
SILType baseType = i->getOperand()->getType();

Address field = projectTupleElementAddress(*this, base, baseType,
i->getFieldNo());
i->getFieldIndex());
setLoweredAddress(i, field);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/SIL/IR/SILGlobalVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ BuiltinInst *SILGlobalVariable::getOffsetSubtract(const TupleExtractInst *TE,
// Match the pattern:
// tuple_extract(usub_with_overflow(x, integer_literal, integer_literal 0), 0)

if (TE->getFieldNo() != 0)
if (TE->getFieldIndex() != 0)
return nullptr;

auto *BI = dyn_cast<BuiltinInst>(TE->getOperand());
Expand Down
4 changes: 2 additions & 2 deletions lib/SIL/IR/SILInstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ namespace {
auto *X = cast<TupleExtractInst>(LHS);
if (X->getTupleType() != RHS->getTupleType())
return false;
if (X->getFieldNo() != RHS->getFieldNo())
if (X->getFieldIndex() != RHS->getFieldIndex())
return false;
return true;
}
Expand All @@ -590,7 +590,7 @@ namespace {
auto *X = cast<TupleElementAddrInst>(LHS);
if (X->getTupleType() != RHS->getTupleType())
return false;
if (X->getFieldNo() != RHS->getFieldNo())
if (X->getFieldIndex() != RHS->getFieldIndex())
return false;
return true;
}
Expand Down
45 changes: 35 additions & 10 deletions lib/SIL/IR/SILInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ bool TupleExtractInst::isTrivialEltOfOneRCIDTuple() const {
// parent tuple has only one non-trivial field.
bool FoundNonTrivialField = false;
SILType OpTy = getOperand()->getType();
unsigned FieldNo = getFieldNo();
unsigned FieldNo = getFieldIndex();

// For each element index of the tuple...
for (unsigned i = 0, e = getNumTupleElts(); i != e; ++i) {
Expand Down Expand Up @@ -1300,7 +1300,7 @@ bool TupleExtractInst::isEltOnlyNonTrivialElt() const {
// Ok, we know that the elt we are extracting is non-trivial. Make sure that
// we have no other non-trivial elts.
SILType OpTy = getOperand()->getType();
unsigned FieldNo = getFieldNo();
unsigned FieldNo = getFieldIndex();

// For each element index of the tuple...
for (unsigned i = 0, e = getNumTupleElts(); i != e; ++i) {
Expand All @@ -1323,18 +1323,43 @@ bool TupleExtractInst::isEltOnlyNonTrivialElt() const {
return true;
}

unsigned FieldIndexCacheBase::cacheFieldIndex() {
unsigned i = 0;
for (VarDecl *property : getParentDecl()->getStoredProperties()) {
/// Get a unique index for a struct or class field in layout order.
unsigned swift::getFieldIndex(NominalTypeDecl *decl, VarDecl *field) {
unsigned index = 0;
if (auto *classDecl = dyn_cast<ClassDecl>(decl)) {
for (auto *superDecl = classDecl->getSuperclassDecl(); superDecl != nullptr;
superDecl = superDecl->getSuperclassDecl()) {
index += superDecl->getStoredProperties().size();
}
}
for (VarDecl *property : decl->getStoredProperties()) {
if (field == property) {
SILInstruction::Bits.FieldIndexCacheBase.FieldIndex = i;
return i;
return index;
}
++i;
++index;
}
llvm_unreachable("The field decl for a struct_extract, struct_element_addr, "
"or ref_element_addr must be an accessible stored property "
"of the operand's type");
"or ref_element_addr must be an accessible stored "
"property of the operand type");
}

/// Get the property for a struct or class by its unique index.
VarDecl *swift::getIndexedField(NominalTypeDecl *decl, unsigned index) {
if (auto *classDecl = dyn_cast<ClassDecl>(decl)) {
for (auto *superDecl = classDecl->getSuperclassDecl(); superDecl != nullptr;
superDecl = superDecl->getSuperclassDecl()) {
assert(index >= superDecl->getStoredProperties().size()
&& "field index cannot refer to a superclass field");
index -= superDecl->getStoredProperties().size();
}
}
return decl->getStoredProperties()[index];
}

unsigned FieldIndexCacheBase::cacheFieldIndex() {
unsigned index = ::getFieldIndex(getParentDecl(), getField());
SILInstruction::Bits.FieldIndexCacheBase.FieldIndex = index;
return index;
}

// FIXME: this should be cached during cacheFieldIndex().
Expand Down
4 changes: 2 additions & 2 deletions lib/SIL/IR/SILPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1774,11 +1774,11 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
}

void visitTupleExtractInst(TupleExtractInst *EI) {
*this << getIDAndType(EI->getOperand()) << ", " << EI->getFieldNo();
*this << getIDAndType(EI->getOperand()) << ", " << EI->getFieldIndex();
}

void visitTupleElementAddrInst(TupleElementAddrInst *EI) {
*this << getIDAndType(EI->getOperand()) << ", " << EI->getFieldNo();
*this << getIDAndType(EI->getOperand()) << ", " << EI->getFieldIndex();
}
void visitStructExtractInst(StructExtractInst *EI) {
*this << getIDAndType(EI->getOperand()) << ", #";
Expand Down
4 changes: 2 additions & 2 deletions lib/SIL/Utils/MemAccessUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ AccessedStorage::AccessedStorage(SILValue base, Kind kind) {
// conservative given that classes are not "uniquely identified".
auto *REA = cast<RefElementAddrInst>(base);
value = stripBorrow(REA->getOperand());
setElementIndex(REA->getFieldNo());
setElementIndex(REA->getFieldIndex());
break;
}
case Tail: {
Expand Down Expand Up @@ -156,7 +156,7 @@ const ValueDecl *AccessedStorage::getDecl() const {

case Class: {
auto *decl = getObject()->getType().getNominalOrBoundGenericNominal();
return decl->getStoredProperties()[getPropertyIndex()];
return getIndexedField(decl, getPropertyIndex());
}
case Tail:
return nullptr;
Expand Down
24 changes: 14 additions & 10 deletions lib/SIL/Utils/Projection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,23 @@ Projection::Projection(SingleValueInstruction *I) : Value() {
return;
case SILInstructionKind::StructElementAddrInst: {
auto *SEAI = cast<StructElementAddrInst>(I);
Value = ValueTy(ProjectionKind::Struct, SEAI->getFieldNo());
Value = ValueTy(ProjectionKind::Struct, SEAI->getFieldIndex());
assert(getKind() == ProjectionKind::Struct);
assert(getIndex() == SEAI->getFieldNo());
assert(getIndex() == SEAI->getFieldIndex());
break;
}
case SILInstructionKind::StructExtractInst: {
auto *SEI = cast<StructExtractInst>(I);
Value = ValueTy(ProjectionKind::Struct, SEI->getFieldNo());
Value = ValueTy(ProjectionKind::Struct, SEI->getFieldIndex());
assert(getKind() == ProjectionKind::Struct);
assert(getIndex() == SEI->getFieldNo());
assert(getIndex() == SEI->getFieldIndex());
break;
}
case SILInstructionKind::RefElementAddrInst: {
auto *REAI = cast<RefElementAddrInst>(I);
Value = ValueTy(ProjectionKind::Class, REAI->getFieldNo());
Value = ValueTy(ProjectionKind::Class, REAI->getFieldIndex());
assert(getKind() == ProjectionKind::Class);
assert(getIndex() == REAI->getFieldNo());
assert(getIndex() == REAI->getFieldIndex());
break;
}
case SILInstructionKind::RefTailAddrInst: {
Expand All @@ -106,16 +106,16 @@ Projection::Projection(SingleValueInstruction *I) : Value() {
}
case SILInstructionKind::TupleExtractInst: {
auto *TEI = cast<TupleExtractInst>(I);
Value = ValueTy(ProjectionKind::Tuple, TEI->getFieldNo());
Value = ValueTy(ProjectionKind::Tuple, TEI->getFieldIndex());
assert(getKind() == ProjectionKind::Tuple);
assert(getIndex() == TEI->getFieldNo());
assert(getIndex() == TEI->getFieldIndex());
break;
}
case SILInstructionKind::TupleElementAddrInst: {
auto *TEAI = cast<TupleElementAddrInst>(I);
Value = ValueTy(ProjectionKind::Tuple, TEAI->getFieldNo());
Value = ValueTy(ProjectionKind::Tuple, TEAI->getFieldIndex());
assert(getKind() == ProjectionKind::Tuple);
assert(getIndex() == TEAI->getFieldNo());
assert(getIndex() == TEAI->getFieldIndex());
break;
}
case SILInstructionKind::UncheckedEnumDataInst: {
Expand Down Expand Up @@ -321,6 +321,10 @@ void Projection::getFirstLevelProjections(

if (auto *C = Ty.getClassOrBoundGenericClass()) {
unsigned Count = 0;
for (auto *superDecl = C->getSuperclassDecl(); superDecl != nullptr;
superDecl = superDecl->getSuperclassDecl()) {
Count += superDecl->getStoredProperties().size();
}
for (auto *VDecl : C->getStoredProperties()) {
(void) VDecl;
Projection P(ProjectionKind::Class, Count++);
Expand Down
4 changes: 2 additions & 2 deletions lib/SIL/Verifier/MemoryLifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,14 @@ bool MemoryLocations::analyzeLocationUsesRecursively(SILValue V, unsigned locIdx
switch (user->getKind()) {
case SILInstructionKind::StructElementAddrInst: {
auto SEAI = cast<StructElementAddrInst>(user);
if (!analyzeAddrProjection(SEAI, locIdx, SEAI->getFieldNo(),
if (!analyzeAddrProjection(SEAI, locIdx, SEAI->getFieldIndex(),
collectedVals, subLocationMap))
return false;
break;
}
case SILInstructionKind::TupleElementAddrInst: {
auto *TEAI = cast<TupleElementAddrInst>(user);
if (!analyzeAddrProjection(TEAI, locIdx, TEAI->getFieldNo(),
if (!analyzeAddrProjection(TEAI, locIdx, TEAI->getFieldIndex(),
collectedVals, subLocationMap))
return false;
break;
Expand Down
10 changes: 5 additions & 5 deletions lib/SIL/Verifier/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2747,11 +2747,11 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
require(EI->getType().isObject(),
"result of tuple_extract must be object");

require(EI->getFieldNo() < operandTy->getNumElements(),
require(EI->getFieldIndex() < operandTy->getNumElements(),
"invalid field index for tuple_extract instruction");
if (EI->getModule().getStage() != SILStage::Lowered) {
requireSameType(EI->getType().getASTType(),
operandTy.getElementType(EI->getFieldNo()),
operandTy.getElementType(EI->getFieldIndex()),
"type of tuple_extract does not match type of element");
}
}
Expand Down Expand Up @@ -2793,12 +2793,12 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
"must derive tuple_element_addr from tuple");

ArrayRef<TupleTypeElt> fields = operandTy.castTo<TupleType>()->getElements();
require(EI->getFieldNo() < fields.size(),
require(EI->getFieldIndex() < fields.size(),
"invalid field index for element_addr instruction");
if (EI->getModule().getStage() != SILStage::Lowered) {
requireSameType(
EI->getType().getASTType(),
CanType(fields[EI->getFieldNo()].getType()),
CanType(fields[EI->getFieldIndex()].getType()),
"type of tuple_element_addr does not match type of element");
}
}
Expand Down Expand Up @@ -2856,7 +2856,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
loweredFieldTy, EI->getType(),
"result of ref_element_addr does not match type of field");
}
EI->getFieldNo(); // Make sure we can access the field without crashing.
EI->getFieldIndex(); // Make sure we can access the field without crashing.
}

void checkRefTailAddrInst(RefTailAddrInst *RTAI) {
Expand Down
Loading