Skip to content

IRGen: Ensure collocation of relative pointers in resilient witness tables #15793

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
9 changes: 8 additions & 1 deletion lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ static bool hasCodeCoverageInstrumentation(SILFunction &f, SILModule &m) {
return f.getProfiler() && m.getOptions().EmitProfileCoverageMapping;
}

void IRGenerator::emitGlobalTopLevel() {
void IRGenerator::emitGlobalTopLevel(bool emitForParallelEmission) {
// Generate order numbers for the functions in the SIL module that
// correspond to definitions in the LLVM module.
unsigned nextOrderNumber = 0;
Expand All @@ -1058,6 +1058,13 @@ void IRGenerator::emitGlobalTopLevel() {
FunctionOrder.insert(std::make_pair(&silFn, nextOrderNumber++));
}

// Ensure that relative symbols are collocated in the same LLVM module.
for (SILWitnessTable &wt : PrimaryIGM->getSILModule().getWitnessTableList()) {
CurrentIGMPtr IGM = getGenModule(wt.getConformance()->getDeclContext());
if (emitForParallelEmission)
IGM->ensureRelativeSymbolCollocation(wt);
}

for (SILGlobalVariable &v : PrimaryIGM->getSILModule().getSILGlobals()) {
Decl *decl = v.getDecl();
CurrentIGMPtr IGM = getGenModule(decl ? decl->getDeclContext() : nullptr);
Expand Down
19 changes: 15 additions & 4 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1832,10 +1832,6 @@ static llvm::Constant *emitResilientWitnessTable(IRGenModule &IGM,
SILFunction *Func = entry.getMethodWitness().Witness;
llvm::Constant *witness;
if (Func) {
// Force the thunk to be emitted in the current translation unit
// when in multi-threaded mode.
IGM.IRGen.forceLocalEmitOfLazyFunction(Func);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was the only usage of forceLocalEmitOfLazyFunction so you can remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now use it in ensureRelativeSymbolCollocation.


witness = IGM.getAddrOfSILFunction(Func, NotForDefinition);
} else {
// The method is removed by dead method elimination.
Expand Down Expand Up @@ -2046,6 +2042,21 @@ llvm::Constant *WitnessTableBuilder::buildInstantiationFunction() {
return fn;
}

void IRGenModule::ensureRelativeSymbolCollocation(SILWitnessTable &wt) {
// Only resilient conformances use relative pointers for witness methods.
if (wt.isDeclaration() || isAvailableExternally(wt.getLinkage()) ||
!isResilientConformance(wt.getConformance()))
return;

for (auto &entry : wt.getEntries()) {
if (entry.getKind() != SILWitnessTable::Method)
continue;
auto *witness = entry.getMethodWitness().Witness;
if (witness)
IRGen.forceLocalEmitOfLazyFunction(witness);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using IRGen.forceLocalEmitOfLazyFunction here.

}
}

/// Do a memoized witness-table layout for a protocol.
const ProtocolInfo &IRGenModule::getProtocolInfo(ProtocolDecl *protocol) {
return Types.getProtocolInfo(protocol);
Expand Down
4 changes: 2 additions & 2 deletions lib/IRGen/IRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,8 @@ static void performParallelIRGeneration(
}

// Emit the module contents.
irgen.emitGlobalTopLevel();
irgen.emitGlobalTopLevel(true /*emitForParallelEmission*/);

for (auto *File : M->getFiles()) {
if (auto *SF = dyn_cast<SourceFile>(File)) {
IRGenModule *IGM = irgen.getGenModule(SF);
Expand Down
8 changes: 7 additions & 1 deletion lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,11 @@ class IRGenerator {

/// Emit functions, variables and tables which are needed anyway, e.g. because
/// they are externally visible.
void emitGlobalTopLevel();
/// If \p emitForParallelEmission is true ensures that symbols that are
/// expressed as relative pointers are collocated in the same output module
/// with their base symbol. For example, witness methods need to be collocated
/// with the witness table in the same LLVM module.
void emitGlobalTopLevel(bool emitForParallelEmission = false);

/// Emit references to each of the protocol descriptors defined in this
/// IR module.
Expand Down Expand Up @@ -1257,6 +1261,8 @@ private: \

void emitSharedContextDescriptor(DeclContext *dc);

void ensureRelativeSymbolCollocation(SILWitnessTable &wt);

private:
llvm::Constant *
getAddrOfSharedContextDescriptor(LinkEntity entity,
Expand Down
3 changes: 3 additions & 0 deletions test/multifile/Inputs/resilient-witness-2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public func test() {
inlineThis(Character("1"))
}
45 changes: 45 additions & 0 deletions test/multifile/resilient-witness.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -module-name test -num-threads 1 -O -c -g %S/Inputs/resilient-witness-2.swift %s -o %t/resilient-witness-2.o -o %t/resilient-witness.o
// RUN: %target-swift-frontend -module-name test -num-threads 1 -O -emit-ir -g %S/Inputs/resilient-witness-2.swift %s -o %t/resilient-witness-2.ll -o %t/resilient-witness.ll
// RUN: %FileCheck %s < %t/resilient-witness.ll

// We used to crash on this test case during code generation because the
// resilient witness table and the witness of the equatable conformance was
// emitted into different files. Because the witness is expressed as a relative
// pointer this was problematic.

class Context {
enum SomeEnum: Int {
case One = 1
case MinusOne = -1

init?(_ delimiter: Character) {
switch delimiter {
case "1":
self = .One
case "-":
self = .MinusOne
default:
return nil
}
}
}
private struct SomeEnumStruct {
var e: SomeEnum?
}

private var arr: [SomeEnumStruct?] = []
}

public func inlineThis(_ char: Character) {
if Context.SomeEnum(char) == .MinusOne {
print("foobar")
}
}

// The witness table and witness definition need to be in the same file.

// CHECK: @"$S4test7ContextC8SomeEnumOs9EquatableAAWr" = {{.*}}sub {{.*}} @"$S4test7ContextC8SomeEnumOs9EquatableAAsAFP2eeoiySbx_xtFZTW" {{.*}} @"$S4test7ContextC8SomeEnumOs9EquatableAAWr"

// CHECK: define{{.*}}@"$S4test7ContextC8SomeEnumOs9EquatableAAsAFP2eeoiySbx_xtFZTW"({{.*}}){{.*}} {
// CHECK-NEXT: entry: