Skip to content

IRGen: Don't use relative pointers for value witness tables under relative procotol witness tables mode #63298

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
14 changes: 12 additions & 2 deletions lib/IRGen/GenOpaque.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,25 +334,34 @@ Address irgen::slotForLoadOfOpaqueWitness(IRGenFunction &IGF,
/// Load a specific witness from a known table. The result is
/// always an i8*.
llvm::Value *irgen::emitInvariantLoadOfOpaqueWitness(IRGenFunction &IGF,
bool isProtocolWitness,
llvm::Value *table,
WitnessIndex index,
llvm::Value **slotPtr) {
auto isRelativeTable = IGF.IGM.IRGen.Opts.UseRelativeProtocolWitnessTables;
// Is this is a load of a relative protocol witness table entry.
auto isRelativeTable = IGF.IGM.IRGen.Opts.UseRelativeProtocolWitnessTables &&
isProtocolWitness;

auto slot = slotForLoadOfOpaqueWitness(IGF, table, index, isRelativeTable);
if (slotPtr) *slotPtr = slot.getAddress();

if (isRelativeTable) {
return IGF.emitLoadOfRelativePointer(slot, false, IGF.IGM.Int8Ty);
}

return IGF.emitInvariantLoad(slot);
}

/// Load a specific witness from a known table. The result is
/// always an i8*.
llvm::Value *irgen::emitInvariantLoadOfOpaqueWitness(IRGenFunction &IGF,
bool isProtocolWitness,
llvm::Value *table,
llvm::Value *index,
llvm::Value **slotPtr) {
assert(table->getType() == IGF.IGM.WitnessTablePtrTy);
assert(!isProtocolWitness &&
"This function does not yet support relative protocol witnesses");

// GEP to the appropriate index.
llvm::Value *slot =
Expand Down Expand Up @@ -438,7 +447,8 @@ static FunctionPointer emitLoadOfValueWitnessFunction(IRGenFunction &IGF,

llvm::Value *slot;
llvm::Value *witness =
emitInvariantLoadOfOpaqueWitness(IGF, table, windex, &slot);
emitInvariantLoadOfOpaqueWitness(IGF, /*isProtocolWitness*/false, table,
windex, &slot);
auto label = getValueWitnessLabel(index);
auto signature = IGF.IGM.getValueWitnessSignature(index);

Expand Down
2 changes: 2 additions & 0 deletions lib/IRGen/GenOpaque.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace irgen {
/// The load is marked invariant. This should not be used in contexts where
/// the referenced witness table is still undergoing initialization.
llvm::Value *emitInvariantLoadOfOpaqueWitness(IRGenFunction &IGF,
bool isProtocolWitness,
llvm::Value *table,
WitnessIndex index,
llvm::Value **slot = nullptr);
Expand All @@ -62,6 +63,7 @@ namespace irgen {
/// The load is marked invariant. This should not be used in contexts where
/// the referenced witness table is still undergoing initialization.
llvm::Value *emitInvariantLoadOfOpaqueWitness(IRGenFunction &IGF,
bool isProtocolWitness,
llvm::Value *table,
llvm::Value *index,
llvm::Value **slot = nullptr);
Expand Down
9 changes: 6 additions & 3 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2634,7 +2634,8 @@ llvm::Value *irgen::loadParentProtocolWitnessTable(IRGenFunction &IGF,
auto &IGM = IGF.IGM;
if (!IGM.IRGen.Opts.UseRelativeProtocolWitnessTables) {
auto baseWTable =
emitInvariantLoadOfOpaqueWitness(IGF, wtable, index);
emitInvariantLoadOfOpaqueWitness(IGF,/*isProtocolWitness*/true, wtable,
index);
return baseWTable;
}
auto &Builder = IGF.Builder;
Expand All @@ -2659,7 +2660,8 @@ llvm::Value *irgen::loadParentProtocolWitnessTable(IRGenFunction &IGF,

Builder.emitBlock(isNotCondBB);
auto baseWTable2 =
emitInvariantLoadOfOpaqueWitness(IGF, wtable, index);
emitInvariantLoadOfOpaqueWitness(IGF,/*isProtocolWitness*/true, wtable,
index);
baseWTable2 = IGF.Builder.CreateBitCast(baseWTable2,
IGF.IGM.WitnessTablePtrTy);
Builder.CreateBr(endBB);
Expand All @@ -2677,7 +2679,8 @@ llvm::Value *irgen::loadConditionalConformance(IRGenFunction &IGF,

auto &IGM = IGF.IGM;
if (!IGM.IRGen.Opts.UseRelativeProtocolWitnessTables) {
return emitInvariantLoadOfOpaqueWitness(IGF, wtable, index);
return emitInvariantLoadOfOpaqueWitness(IGF, /*isProtocolWitness*/true,
wtable, index);
}

auto &Builder = IGF.Builder;
Expand Down
16 changes: 16 additions & 0 deletions test/IRGen/relative_protocol_witness_table.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
// REQUIRES: CPU=x86_64 || CPU=arm64
// UNSUPPORTED: CPU=arm64e

func testVWT<T>(_ t: T) {
var local = t
}

protocol FuncOnly {
func a()
func b()
Expand Down Expand Up @@ -185,6 +189,18 @@ func instantiate_conditional_conformance_2nd<T>(_ t : T) where T: Sub, T.S == T
// CHECK-SAME: i64 ptrtoint (i32* getelementptr inbounds ([4 x i32], [4 x i32]* @"$s1A17ConditionalStructVyxGAA20WithAssocConformanceA2A8FuncOnlyRzAA5InitPRzlWP", i32 0, i32 3) to i64)) to i32)
// CHECK-SAME: ], align 8

// Make sure value witness table lookup is done right.

// CHECK: define{{.*}} swiftcc void @"$s1A7testVWTyyxlF"(%swift.opaque* {{.*}}, %swift.type* [[T:%.*]])
// CHECK: [[T0:%.*]] = bitcast %swift.type* [[T]] to i8***
// CHECK: [[VWT_ADDR:%.*]] = getelementptr inbounds i8**, i8*** [[T0]], i64 -1
// CHECK: [[VWT_PTR:%.*]] = load i8**, i8*** [[VWT_ADDR]]
// CHECK: [[T1:%.*]] = getelementptr inbounds i8*, i8** [[VWT_PTR]], i32 2
// CHECK: [[T2:%.*]] = load i8*, i8** [[T1]]
// CHECK: [[T3:%.*]] = bitcast i8* [[T2]] to %swift.opaque* (%swift.opaque*, %swift.opaque*, %swift.type*)*
// CHECK: call %swift.opaque* [[T3]](%swift.opaque* {{.*}}, %swift.opaque* {{.*}}, %swift.type* [[T]])


// Simple witness entry access.

// CHECK: define{{.*}} swiftcc void @"$s1A14requireWitnessyyxAA8FuncOnlyRzlF"(%swift.opaque* noalias nocapture {{%.*}}, %swift.type* {{%.*}}, i8** [[PWT:%.*]])
Expand Down