Skip to content

IRGen: Outline some of the relative protocol witness table code generation to save on code size #64000

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 1 commit into from
Mar 1, 2023
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
105 changes: 84 additions & 21 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2660,7 +2660,19 @@ llvm::Value *irgen::loadParentProtocolWitnessTable(IRGenFunction &IGF,
index);
return baseWTable;
}
auto &Builder = IGF.Builder;

llvm::SmallString<40> fnName;
llvm::raw_svector_ostream(fnName)
<< "__swift_relative_protocol_witness_table_parent_"
<< index.getValue();

auto helperFn = cast<llvm::Function>(IGM.getOrCreateHelperFunction(
fnName, IGM.WitnessTablePtrTy, {IGM.WitnessTablePtrTy},
[&](IRGenFunction &subIGF) {

auto it = subIGF.CurFn->arg_begin();
llvm::Value *wtable = &*it;
auto &Builder = subIGF.Builder;
auto *ptrVal = Builder.CreatePtrToInt(wtable, IGM.IntPtrTy);
auto *one = llvm::ConstantInt::get(IGM.IntPtrTy, 1);
auto *isCond = Builder.CreateAnd(ptrVal, one);
Expand All @@ -2674,30 +2686,30 @@ llvm::Value *irgen::loadParentProtocolWitnessTable(IRGenFunction &IGF,
auto *mask = Builder.CreateNot(one);
auto *wtableAddr = Builder.CreateAnd(ptrVal, mask);
wtableAddr = Builder.CreateIntToPtr(wtableAddr, IGM.WitnessTablePtrTy);
auto addr = slotForLoadOfOpaqueWitness(IGF, wtableAddr, index,
auto addr = slotForLoadOfOpaqueWitness(subIGF, wtableAddr, index,
false /*isRelative*/);
llvm::Value *baseWTable = Builder.CreateLoad(addr);
baseWTable = IGF.Builder.CreateBitCast(baseWTable, IGF.IGM.WitnessTablePtrTy);
baseWTable = subIGF.Builder.CreateBitCast(baseWTable, IGM.WitnessTablePtrTy);
Builder.CreateBr(endBB);

Builder.emitBlock(isNotCondBB);
if (auto &schema = IGF.getOptions().PointerAuth.RelativeProtocolWitnessTable) {
auto info = PointerAuthInfo::emit(IGF, schema, nullptr,
if (auto &schema = subIGF.getOptions().PointerAuth.RelativeProtocolWitnessTable) {
auto info = PointerAuthInfo::emit(subIGF, schema, nullptr,
PointerAuthEntity());
wtable = emitPointerAuthAuth(IGF, wtable, info);
wtable = emitPointerAuthAuth(subIGF, wtable, info);
}
auto baseWTable2 =
emitInvariantLoadOfOpaqueWitness(IGF,/*isProtocolWitness*/true, wtable,
emitInvariantLoadOfOpaqueWitness(subIGF,/*isProtocolWitness*/true, wtable,
index);
baseWTable2 = IGF.Builder.CreateBitCast(baseWTable2,
IGF.IGM.WitnessTablePtrTy);
if (auto &schema = IGF.getOptions().PointerAuth.RelativeProtocolWitnessTable) {
auto info = PointerAuthInfo::emit(IGF, schema, nullptr,
baseWTable2 = subIGF.Builder.CreateBitCast(baseWTable2,
subIGF.IGM.WitnessTablePtrTy);
if (auto &schema = subIGF.getOptions().PointerAuth.RelativeProtocolWitnessTable) {
auto info = PointerAuthInfo::emit(subIGF, schema, nullptr,
PointerAuthEntity());
baseWTable2 = emitPointerAuthSign(IGF, baseWTable2, info);
baseWTable2 = emitPointerAuthSign(subIGF, baseWTable2, info);

baseWTable2 = IGF.Builder.CreateBitCast(baseWTable2,
IGF.IGM.WitnessTablePtrTy);
baseWTable2 = subIGF.Builder.CreateBitCast(baseWTable2,
IGM.WitnessTablePtrTy);
}

Builder.CreateBr(endBB);
Expand All @@ -2706,7 +2718,15 @@ llvm::Value *irgen::loadParentProtocolWitnessTable(IRGenFunction &IGF,
auto *phi = Builder.CreatePHI(wtable->getType(), 2);
phi->addIncoming(baseWTable, isCondBB);
phi->addIncoming(baseWTable2, isNotCondBB);
return phi;
Builder.CreateRet(phi);

}, true /*noinline*/));

auto *call = IGF.Builder.CreateCallWithoutDbgLoc(
helperFn->getFunctionType(), helperFn, {wtable});
call->setCallingConv(IGF.IGM.DefaultCC);
call->setDoesNotThrow();
return call;
}

llvm::Value *irgen::loadConditionalConformance(IRGenFunction &IGF,
Expand Down Expand Up @@ -3827,6 +3847,49 @@ static llvm::Value *emitWTableSlotLoad(IRGenFunction &IGF, llvm::Value *wtable,
return IGF.emitInvariantLoad(slot);
}

static FunctionPointer emitRelativeProtocolWitnessTableAccess(IRGenFunction &IGF,
WitnessIndex index,
llvm::Value *wtable,
SILDeclRef member) {
auto witnessTableTy = wtable->getType();
auto &IGM = IGF.IGM;
llvm::SmallString<40> fnName;
auto entity = LinkEntity::forMethodDescriptor(member);
auto mangled = entity.mangleAsString();
llvm::raw_svector_ostream(fnName)
<< "__swift_relative_protocol_witness_table_access_"
<< index.forProtocolWitnessTable().getValue()
<< "_" << mangled;

auto fnType = IGF.IGM.getSILTypes().getConstantFunctionType(
IGF.IGM.getMaximalTypeExpansionContext(), member);
Signature signature = IGF.IGM.getSignature(fnType);

auto helperFn = cast<llvm::Function>(IGM.getOrCreateHelperFunction(
fnName, IGM.Int8PtrTy, {witnessTableTy},
[&](IRGenFunction &subIGF) {

auto it = subIGF.CurFn->arg_begin();
llvm::Value *wtable = &*it;
wtable = subIGF.optionallyLoadFromConditionalProtocolWitnessTable(wtable);
auto slot = slotForLoadOfOpaqueWitness(subIGF, wtable,
index.forProtocolWitnessTable(),
true);
llvm::Value *witnessFnPtr = emitWTableSlotLoad(subIGF, wtable, member, slot,
true);

subIGF.Builder.CreateRet(witnessFnPtr);

}, true /*noinline*/));

auto *call = IGF.Builder.CreateCallWithoutDbgLoc(
helperFn->getFunctionType(), helperFn, {wtable});
call->setCallingConv(IGF.IGM.DefaultCC);
call->setDoesNotThrow();
auto fn = IGF.Builder.CreateBitCast(call, signature.getType()->getPointerTo());
return FunctionPointer::createUnsigned(fnType, fn, signature, true);
}

FunctionPointer irgen::emitWitnessMethodValue(IRGenFunction &IGF,
llvm::Value *wtable,
SILDeclRef member) {
Expand All @@ -3839,22 +3902,22 @@ FunctionPointer irgen::emitWitnessMethodValue(IRGenFunction &IGF,
auto &fnProtoInfo = IGF.IGM.getProtocolInfo(proto, ProtocolInfoKind::Full);
auto index = fnProtoInfo.getFunctionIndex(member);
auto isRelativeTable = IGF.IGM.IRGen.Opts.UseRelativeProtocolWitnessTables;
if (isRelativeTable) {
return emitRelativeProtocolWitnessTableAccess(IGF, index, wtable, member);
}

wtable = IGF.optionallyLoadFromConditionalProtocolWitnessTable(wtable);
auto slot =
slotForLoadOfOpaqueWitness(IGF, wtable, index.forProtocolWitnessTable(),
isRelativeTable);
false/*isRelativeTable*/);
llvm::Value *witnessFnPtr = emitWTableSlotLoad(IGF, wtable, member, slot,
isRelativeTable);
false/*isRelativeTable*/);

auto fnType = IGF.IGM.getSILTypes().getConstantFunctionType(
IGF.IGM.getMaximalTypeExpansionContext(), member);
Signature signature = IGF.IGM.getSignature(fnType);
witnessFnPtr = IGF.Builder.CreateBitCast(witnessFnPtr,
signature.getType()->getPointerTo());
if (isRelativeTable) {
return FunctionPointer::createUnsigned(fnType, witnessFnPtr, signature,
true);
}

auto &schema = fnType->isAsync()
? IGF.getOptions().PointerAuth.AsyncProtocolWitnesses
Expand Down
47 changes: 11 additions & 36 deletions test/IRGen/relative_protocol_witness_table.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ func instantiate_conditional_conformance_2nd<T>(_ t : T) where T: Sub, T.S == T

// CHECK: define{{.*}} swiftcc void @"$s1A14requireWitnessyyxAA8FuncOnlyRzlF"(%swift.opaque* noalias nocapture {{%.*}}, %swift.type* {{%.*}}, i8** [[PWT:%.*]])
// CHECK:[[ENTRY:.*]]:
// CHECK: [[T4:%.*]] = call i8* @"__swift_relative_protocol_witness_table_access_1_$s1A8FuncOnlyP1ayyFTq"(i8** [[PWT]])
// CHECK: [[T5:%.*]] = bitcast i8* [[T4]] to void (%swift.opaque*, %swift.type*, i8**)*
// CHECK: call{{.*}} swiftcc void [[T5]]

// CHECK: define{{.*}} hidden i8* @"__swift_relative_protocol_witness_table_access_1_$s1A8FuncOnlyP1ayyFTq"(i8** [[PWT:%.*]])
// CHECK: [[T0:%.*]] = ptrtoint i8** [[PWT]] to i64
// CHECK: [[T1:%.*]] = and i64 [[T0]], 1
// CHECK: [[C:%.*]] = icmp eq i64 [[T1]], 1
Expand All @@ -229,12 +234,15 @@ func instantiate_conditional_conformance_2nd<T>(_ t : T) where T: Sub, T.S == T
// CHECK: [[T2:%.*]] = ptrtoint i32* [[SLOT]] to i64
// CHECK: [[T3:%.*]] = add i64 [[T2]], [[T1]]
// CHECK: [[T4:%.*]] = inttoptr i64 [[T3]] to i8*
// CHECK: [[T5:%.*]] = bitcast i8* [[T4]] to void (%swift.opaque*, %swift.type*, i8**)*
// CHECK: call{{.*}} swiftcc void [[T5]]
// CHECK: ret i8* [[T4]]

// Parent witness entry access.

// CHECK: define hidden swiftcc void @"$s1A15requireWitness2yyxAA9InheritedRzlF"(%swift.opaque* noalias nocapture {{%.*}}, %swift.type* {{%.*}}, i8** [[T_INHERITED:%.*]])
// CHECK: [[T_FUNCONLY:%.*]] = call i8** @__swift_relative_protocol_witness_table_parent_1(i8** [[T_INHERITED]])
// CHECK: call i8* @"__swift_relative_protocol_witness_table_access_1_$s1A8FuncOnlyP1ayyFTq"(i8** [[T_FUNCONLY]])

// CHECK: define{{.*}} hidden i8** @__swift_relative_protocol_witness_table_parent_1(i8** [[T_INHERITED:%.*]])
// CHECK: [[T0:%.*]] = ptrtoint i8** [[T_INHERITED]] to i64
// CHECK: [[T1:%.*]] = and i64 [[T0]], 1
// CHECK: [[T2:%.*]] = icmp eq i64 [[T1]], 1
Expand Down Expand Up @@ -323,40 +331,7 @@ func instantiate_conditional_conformance_2nd<T>(_ t : T) where T: Sub, T.S == T
// CHECK: [[T2:%.*]] = bitcast i8** [[C1]] to i8***
// CHECK: [[T3:%.*]] = getelementptr inbounds i8**, i8*** [[T2]], i32 0
// CHECK: [[T4:%.*]] = load i8**, i8*** [[T3]]
// CHECK: [[T5:%.*]] = ptrtoint i8** [[T4]] to i64
// CHECK: [[T6:%.*]] = and i64 [[T5]], 1
// CHECK: [[T7:%.*]] = icmp eq i64 [[T6]], 1
// CHECK: br i1 [[T7]], label %[[T8:.*]], label %[[T14:.*]]

// CHECK: [[T8]]:
// CHECK: [[T9:%.*]] = and i64 [[T5]], -2
// CHECK: [[T10:%.*]] = inttoptr i64 [[T9]] to i8**
// CHECK: [[T11:%.*]] = getelementptr inbounds i8*, i8** [[T10]], i32 1
// CHECK: [[T12:%.*]] = load i8*, i8** [[T11]]
// CHECK: [[T13:%.*]] = bitcast i8* [[T12]] to i8**
// CHECK: br label %[[T23:.*]]

// CHECK: [[T14]]:
// CHECK-arm64e: [[P0:%.*]] = ptrtoint i8** [[T4]] to i64
// CHECK-arm64e: [[P1:%.*]] = call i64 @llvm.ptrauth.auth(i64 [[P0]], i32 2, i64 47152)
// CHECK-arm64e: [[P2:%.*]] = inttoptr i64 [[P1]] to i8**
// CHECK-arm64e: [[T15:%.*]] = bitcast i8** [[P2]] to i32*
// CHECK-x86_64: [[T15:%.*]] = bitcast i8** [[T4]] to i32*
// CHECK-arm64: [[T15:%.*]] = bitcast i8** [[T4]] to i32*
// CHECK: [[T16:%.*]] = getelementptr inbounds i32, i32* [[T15]], i32 1
// CHECK: [[T17:%.*]] = load i32, i32* [[T16]]
// CHECK: [[T18:%.*]] = sext i32 [[T17]] to i64
// CHECK: [[T19:%.*]] = ptrtoint i32* [[T16]] to i64
// CHECK: [[T20:%.*]] = add i64 [[T19]], [[T18]]
// CHECK: [[T21:%.*]] = inttoptr i64 [[T20]] to i8*
// CHECK: [[T22:%.*]] = bitcast i8* [[T21]] to i8**
// CHECK-arm64e: [[P0:%.*]] = ptrtoint i8** [[T22]] to i64
// CHECK-arm64e: [[P1:%.*]] = call i64 @llvm.ptrauth.sign(i64 [[P0]], i32 2, i64 47152)
// CHECK-arm64e: [[T22:%.*]] = inttoptr i64 [[P1]] to i8**
// CHECK: br label %[[T23]]

// CHECK: [[T23]]:
// CHECK: [[TBASE:%.*]] = phi i8** [ [[T13]], %[[T8]] ], [ [[T22]], %[[T14]] ]
// CHECK: [[TBASE:%.*]] = call i8** @__swift_relative_protocol_witness_table_parent_1(i8** [[T4]])
// CHECK: [[T24:%.*]] = getelementptr inbounds [1 x i8**], [1 x i8**]* [[C2]], i32 0, i32 0
// CHECK: [[T25:%.*]] = getelementptr inbounds i8**, i8*** [[T24]], i32 0
// CHECK: store i8** [[TBASE]], i8*** [[T25]]
Expand Down