Skip to content

IRGen: Initialize single case enum extrainhabitant value witness in generated code #24293

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
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
12 changes: 12 additions & 0 deletions lib/IRGen/GenEnum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,18 @@ namespace {
IGF.Builder.CreateCall(
IGF.IGM.getInitEnumMetadataSingleCaseFn(),
{metadata, flags, payloadLayout});

// Pre swift-5.1 runtimes were missing the initialization of the
// the extraInhabitantCount field. Do it here instead.
auto payloadRef = IGF.Builder.CreateBitOrPointerCast(
payloadLayout, IGF.IGM.TypeLayoutTy->getPointerTo());
auto payloadExtraInhabitantCount =
IGF.Builder.CreateLoad(IGF.Builder.CreateStructGEP(
Address(payloadRef, Alignment(1)), 3,
Size(IGF.IGM.DataLayout.getTypeAllocSize(IGF.IGM.SizeTy) * 2 +
IGF.IGM.DataLayout.getTypeAllocSize(IGF.IGM.Int32Ty))));
emitStoreOfExtraInhabitantCount(IGF, payloadExtraInhabitantCount,
metadata);
}

bool mayHaveExtraInhabitants(IRGenModule &IGM) const override {
Expand Down
25 changes: 20 additions & 5 deletions lib/IRGen/GenOpaque.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,9 @@ llvm::Value *irgen::emitInvariantLoadOfOpaqueWitness(IRGenFunction &IGF,
return witness;
}

/// Given a value witness table, load one of the value witnesses.
/// The result has the appropriate type for the witness.
static llvm::Value *emitLoadOfValueWitnessValue(IRGenFunction &IGF,
llvm::Value *table,
ValueWitness witness) {
static Address emitAddressOfValueWitnessTableValue(IRGenFunction &IGF,
llvm::Value *table,
ValueWitness witness) {
assert(!isValueWitnessFunction(witness));
assert(unsigned(witness) <= unsigned(ValueWitness::ExtraInhabitantCount) &&
"extraInhabitantCount not the last non-function value witness");
Expand All @@ -368,7 +366,15 @@ static llvm::Value *emitLoadOfValueWitnessValue(IRGenFunction &IGF,
Address addr = Address(table, IGF.IGM.getPointerAlignment());
addr = IGF.Builder.CreateBitCast(addr, IGF.IGM.getValueWitnessTablePtrTy());
addr = IGF.Builder.CreateStructGEP(addr, unsigned(witness), offset);
return addr;
}

/// Given a value witness table, load one of the value witnesses.
/// The result has the appropriate type for the witness.
static llvm::Value *emitLoadOfValueWitnessValue(IRGenFunction &IGF,
llvm::Value *table,
ValueWitness witness) {
auto addr = emitAddressOfValueWitnessTableValue(IGF, table, witness);
auto load = IGF.Builder.CreateLoad(addr, getValueWitnessLabel(witness));
IGF.setInvariantLoad(load);
return load;
Expand Down Expand Up @@ -912,6 +918,15 @@ llvm::Value *irgen::emitLoadOfExtraInhabitantCount(IRGenFunction &IGF,
return IGF.emitValueWitnessValue(T, ValueWitness::ExtraInhabitantCount);
}

void irgen::emitStoreOfExtraInhabitantCount(IRGenFunction &IGF,
llvm::Value *value,
llvm::Value *metadata) {
auto vwTable = IGF.emitValueWitnessTableRefForMetadata(metadata);
auto addr = emitAddressOfValueWitnessTableValue(
IGF, vwTable, ValueWitness::ExtraInhabitantCount);
IGF.Builder.CreateStore(value, addr);
}

std::pair<llvm::Value *, llvm::Value *>
irgen::emitLoadOfIsInline(IRGenFunction &IGF, llvm::Value *metadata) {
auto *flags = emitLoadOfValueWitnessValueFromMetadata(IGF, metadata,
Expand Down
4 changes: 4 additions & 0 deletions lib/IRGen/GenOpaque.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ namespace irgen {
/// Emit a load of the 'extraInhabitantCount' value witness.
llvm::Value *emitLoadOfExtraInhabitantCount(IRGenFunction &IGF, SILType T);

/// Emit a stored to the 'extraInhabitantCount' value witness.
void emitStoreOfExtraInhabitantCount(IRGenFunction &IGF, llvm::Value *val,
llvm::Value *metadata);

/// Returns the IsInline flag and the loaded flags value.
std::pair<llvm::Value *, llvm::Value *>
emitLoadOfIsInline(IRGenFunction &IGF, llvm::Value *metadata);
Expand Down
7 changes: 7 additions & 0 deletions lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
SizeTy // extra inhabitant flags (optional)
});

TypeLayoutTy = createStructType(*this, "swift.type_layout", {
SizeTy, // size
SizeTy, // stride
Int32Ty, // flags
Int32Ty // extra inhabitant count
});

// A protocol descriptor describes a protocol. It is not type metadata in
// and of itself, but is referenced in the structure of existential type
// metadata records.
Expand Down
1 change: 1 addition & 0 deletions lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ class IRGenModule {
};
llvm::StructType *OffsetPairTy; /// { iSize, iSize }
llvm::StructType *FullTypeLayoutTy; /// %swift.full_type_layout = { ... }
llvm::StructType *TypeLayoutTy; /// %swift.type_layout = { ... }
llvm::PointerType *TupleTypeMetadataPtrTy; /// %swift.tuple_type*
llvm::StructType *FullHeapMetadataStructTy; /// %swift.full_heapmetadata = type { ... }
llvm::PointerType *FullHeapMetadataPtrTy;/// %swift.full_heapmetadata*
Expand Down
9 changes: 9 additions & 0 deletions test/IRGen/enum.sil
Original file line number Diff line number Diff line change
Expand Up @@ -2683,6 +2683,15 @@ entry(%x : $*MyOptional):
// CHECK: [[T_VWT:%.*]] = load i8**, i8*** [[T_VWT_ADDR]]
// CHECK: [[T_LAYOUT:%.*]] = getelementptr inbounds i8*, i8** [[T_VWT]], i32 8
// CHECK: call void @swift_initEnumMetadataSingleCase(%swift.type* [[METADATA]], [[WORD]] 0, i8** [[T_LAYOUT]])
// CHECK: [[PAYLOAD_TYPELAYOUT:%.*]] = bitcast i8** [[T_LAYOUT]] to %swift.type_layout*
// CHECK: [[PAYLOAD_EXTRAINHABITANTCNT:%.*]] = getelementptr inbounds %swift.type_layout, %swift.type_layout* [[PAYLOAD_TYPELAYOUT]], i32 0, i32 3
// CHECK: [[CNT:%.*]] = load i32, i32* [[PAYLOAD_EXTRAINHABITANTCNT]]
// CHECK: [[METADATA2:%.*]] = bitcast %swift.type* [[METADATA]] to i8***
// CHECK: [[VWT_ADDR:%.*]] = getelementptr inbounds i8**, i8*** [[METADATA2]], [[WORD]] -1
// CHECK: [[VWT:%.*]] = load i8**, i8*** [[VWT_ADDR]]
// CHECK: [[VWT2:%.*]] = bitcast i8** [[VWT]] to %swift.vwtable*
// CHECK: [[XIC_ADDR:%.*]] = getelementptr inbounds %swift.vwtable, %swift.vwtable* [[VWT2]], i32 0, i32 11
// CHECK: store i32 [[CNT]], i32* [[XIC_ADDR]]
// CHECK: ret %swift.metadata_response

// -- Fill function for dynamic single-payload. Call into the runtime to
Expand Down