Skip to content

Commit 3592e21

Browse files
author
Zak Kent
committed
[SILGen] Emit toplevel code through emitFunctionDefinition
Emit SILDeclRefs representing entry points with SourceFile source correctly.
1 parent aaf3dc5 commit 3592e21

24 files changed

+420
-397
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,14 @@ void SILGenModule::emitFunctionDefinition(SILDeclRef constant, SILFunction *f) {
10951095
case SILDeclRef::Kind::AsyncEntryPoint:
10961096
case SILDeclRef::Kind::EntryPoint: {
10971097
f->setBare(IsBare);
1098+
if (constant.hasFileUnit()) {
1099+
auto *File = constant.getFileUnit();
1100+
// In script mode
1101+
assert(isa<SourceFile>(File) && "Emitting entry-point of non source file?!");
1102+
auto *SF = dyn_cast<SourceFile>(File);
1103+
emitEntryPoint(SF, f);
1104+
return;
1105+
}
10981106

10991107
// TODO: Handle main SourceFile emission (currently done by
11001108
// SourceFileScope).
@@ -1920,15 +1928,9 @@ void SILGenModule::visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
19201928
// Nothing to do for #error/#warning; they've already been emitted.
19211929
}
19221930

1923-
void SILGenModule::emitEntryPoint(SourceFile *SF) {
1924-
assert(!M.lookUpFunction(getASTContext().getEntryPointFunctionName()) &&
1925-
"already emitted toplevel?!");
1926-
1927-
auto mainEntryRef = SILDeclRef::getMainFileEntryPoint(SF);
1928-
SILFunction *TopLevel = getFunction(mainEntryRef, ForDefinition);
1929-
TopLevel->setBare(IsBare);
1930-
SILDeclRef EntryRef = mainEntryRef;
1931+
void SILGenModule::emitEntryPoint(SourceFile *SF, SILFunction *TopLevel) {
19311932

1933+
auto EntryRef = SILDeclRef::getMainFileEntryPoint(SF);
19321934
bool isAsyncTopLevel = false;
19331935
if (SF->isAsyncContext()) {
19341936
isAsyncTopLevel = true;
@@ -1968,38 +1970,11 @@ void SILGenModule::emitEntryPoint(SourceFile *SF) {
19681970
entry->createFunctionArgument(*std::next(paramTypeIter));
19691971
}
19701972

1971-
llvm::Optional<Scope> S;
1972-
S.emplace(TopLevelSGF.Cleanups, moduleCleanupLoc);
1973-
1974-
SILGenTopLevel SGT(TopLevelSGF);
1975-
1976-
for (auto *D : SF->getTopLevelDecls()) {
1977-
// Emit auxiliary decls.
1978-
D->visitAuxiliaryDecls(
1979-
[&](Decl *AuxiliaryDecl) { SGT.visit(AuxiliaryDecl); });
1980-
SGT.visit(D);
1981-
}
1982-
1983-
if (auto *synthesizedFile = SF->getSynthesizedFile()) {
1984-
for (auto *D : synthesizedFile->getTopLevelDecls()) {
1985-
if (isa<ExtensionDecl>(D)) {
1986-
SGT.visit(D);
1987-
}
1988-
}
1989-
}
1990-
1991-
for (Decl *D : SF->getHoistedDecls()) {
1992-
SGT.visit(D);
1993-
}
1994-
1995-
for (TypeDecl *TD : SF->LocalTypeDecls) {
1996-
if (TD->getDeclContext()->getInnermostSkippedFunctionContext())
1997-
continue;
1998-
SGT.visit(TD);
1973+
{
1974+
Scope S(TopLevelSGF.Cleanups, moduleCleanupLoc);
1975+
SILGenTopLevel(TopLevelSGF).visitSourceFile(SF);
19991976
}
20001977

2001-
S.reset();
2002-
20031978
// Unregister the top-level function emitter.
20041979
TopLevelSGF.stopEmittingTopLevelCode();
20051980

@@ -2108,6 +2083,16 @@ void SILGenModule::emitEntryPoint(SourceFile *SF) {
21082083
emitLazyConformancesForFunction(&toplevel);
21092084
}
21102085

2086+
void SILGenModule::emitEntryPoint(SourceFile *SF) {
2087+
assert(!M.lookUpFunction(getASTContext().getEntryPointFunctionName()) &&
2088+
"already emitted toplevel?!");
2089+
2090+
auto mainEntryRef = SILDeclRef::getMainFileEntryPoint(SF);
2091+
SILFunction *TopLevel = getFunction(mainEntryRef, ForDefinition);
2092+
TopLevel->setBare(IsBare);
2093+
emitEntryPoint(SF, TopLevel);
2094+
}
2095+
21112096
namespace {
21122097

21132098
// An RAII object that constructs a \c SILGenModule instance.

lib/SILGen/SILGen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
293293
void visitMacroExpansionDecl(MacroExpansionDecl *d);
294294

295295
void emitEntryPoint(SourceFile *SF);
296+
void emitEntryPoint(SourceFile *SF, SILFunction *TopLevel);
296297

297298
void emitAbstractFuncDecl(AbstractFunctionDecl *AFD);
298299

lib/SILGen/SILGenTopLevel.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,32 @@ static void emitMarkFunctionEscape(SILGenFunction &SGF,
2020

2121
SILGenTopLevel::SILGenTopLevel(SILGenFunction &SGF) : SGF(SGF) {}
2222

23+
void SILGenTopLevel::visitSourceFile(SourceFile *SF) {
24+
25+
for (auto *D : SF->getTopLevelDecls()) {
26+
D->visitAuxiliaryDecls([&](Decl *AuxiliaryDecl) { visit(AuxiliaryDecl); });
27+
visit(D);
28+
}
29+
30+
if (auto *SynthesizedFile = SF->getSynthesizedFile()) {
31+
for (auto *D : SynthesizedFile->getTopLevelDecls()) {
32+
if (isa<ExtensionDecl>(D)) {
33+
visit(D);
34+
}
35+
}
36+
}
37+
38+
for (Decl *D : SF->getHoistedDecls()) {
39+
visit(D);
40+
}
41+
42+
for (TypeDecl *TD : SF->LocalTypeDecls) {
43+
if (TD->getDeclContext()->getInnermostSkippedFunctionContext())
44+
continue;
45+
visit(TD);
46+
}
47+
}
48+
2349
void SILGenTopLevel::visitNominalTypeDecl(NominalTypeDecl *NTD) {
2450
TypeVisitor(SGF).emit(NTD);
2551
}

lib/SILGen/SILGenTopLevel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "SILGen.h"
1717
#include "swift/AST/ASTVisitor.h"
18+
#include "swift/AST/SourceFile.h"
1819
#include "swift/AST/TypeMemberVisitor.h"
1920

2021
namespace swift {
@@ -27,6 +28,7 @@ class SILGenTopLevel : public ASTVisitor<SILGenTopLevel> {
2728
/// Generate SIL for toplevel code into `SGF`
2829
SILGenTopLevel(SILGenFunction &SGF);
2930

31+
void visitSourceFile(SourceFile *SF);
3032
void visitDecl(Decl *D) {}
3133
void visitNominalTypeDecl(NominalTypeDecl *NTD);
3234
void visitExtensionDecl(ExtensionDecl *ED);

test/DebugInfo/catch_let.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public func multiBinding() {
4242
catch let error as MyError, let error as MyError {
4343
// CHECK: call void @llvm.dbg.declare(metadata ptr %{{.*}}, metadata ![[MULTI_BINDING_ERROR:[0-9]+]],
4444
// CHECK-NOT: call void @llvm.dbg.declare(metadata ptr %{{.*}}
45+
// CHECK: define {{.*}}MyError{{.*}}
4546
use(error)
4647
} catch {
4748
use(error)

test/DebugInfo/protocol.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class Point : PointUtils {
1919

2020
}
2121

22-
// CHECK: define hidden {{.*}}i64 @"$s8protocol4mains5Int64VyF"() {{.*}} {
23-
func main() -> Int64 {
22+
// CHECK: define {{.*}}i64 @"$s8protocol4mains5Int64VyF"() {{.*}} {
23+
public func main() -> Int64 {
2424
var pt = Point(_x: 2.5, _y: 4.25)
2525
// CHECK: [[LOC2D:%[a-zA-Z0-9]+]] = alloca %T8protocol10PointUtilsP, align {{(4|8)}}
2626
// CHECK: call void @llvm.dbg.declare(metadata {{.*}} [[LOC2D]], metadata ![[LOC:.*]], metadata !DIExpression())
@@ -35,6 +35,3 @@ func main() -> Int64 {
3535
// CHECK-SAME: DIFlagArtificial
3636

3737
// CHECK: ![[LOC]] = !DILocalVariable(name: "loc2d",{{.*}} line: [[@LINE-10]]
38-
39-
main()
40-

test/DebugInfo/struct_resilience.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public func takesSize(_ s: Size) {}
1818

1919
// CHECK-LABEL: define{{.*}} swiftcc void @"$s17struct_resilience1fyyF"()
2020
// CHECK-LLDB-LABEL: define{{.*}} swiftcc void @"$s17struct_resilience1fyyF"()
21-
func f() {
21+
public func f() {
2222
let s1 = Size(w: 1, h: 2)
2323
takesSize(s1)
2424
// CHECK: %[[ADDR:.*]] = alloca ptr
@@ -28,7 +28,6 @@ func f() {
2828
// CHECK: %[[S1:.*]] = alloca i8,
2929
// CHECK: store ptr %[[S1]], ptr %[[ADDR]]
3030
}
31-
f()
3231

3332
// CHECK: ![[TY:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "$s16resilient_struct4SizeVD",
3433
// CHECK: ![[LET_TY:[0-9]+]] = !DIDerivedType(tag: DW_TAG_const_type,

test/DebugInfo/test-foundation.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import ObjectiveC
1212
import Foundation
1313

1414
class MyObject : NSObject {
15+
16+
1517
// Ensure we don't emit linetable entries for ObjC thunks.
1618
// LOC-CHECK: define {{.*}} @"$s4main8MyObjectC0B3ArrSo7NSArrayCvgTo"
1719
// LOC-CHECK: ret {{.*}}, !dbg ![[DBG:.*]]
@@ -27,9 +29,8 @@ class MyObject : NSObject {
2729
// IMPORT-CHECK-DAG: !DIImportedEntity(tag: DW_TAG_imported_module, {{.*}}entity: ![[OVERLAY]]
2830

2931
// ALLOCCTOR-CHECK: ![[F:.*]] = !DIFile(filename: "<compiler-generated>",
30-
// ALLOCCTOR-CHECK: distinct !DISubprogram(name: "init",
31-
// ALLOCCTOR-CHECK-SAME: linkageName: "$sSo7NSArrayCABycfC",
32-
// ALLOCCTOR-CHECK-SAME: file: ![[F]],
32+
// ALLOCCTOR-CHECK: distinct !DISubprogram(name: "init", linkageName: "$sSo7NSArrayCABycfC"
33+
// ALLOCCTOR-CHECK-SAME: file: ![[F]],
3334
@objc func foo(_ obj: MyObject) {
3435
return obj.foo(obj)
3536
}

test/DebuggerTestingTransform/basic-assignments.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,30 @@ print("a = \(a)") // CHECK-E2E: a = 1002
3333
a = 1004
3434
print("a = \(a)") // CHECK-E2E-NEXT: a = 1004
3535

36-
// CHECK-SIL-LABEL: sil private @$s1M2f1yyFyyXEfU3_
36+
// CHECK-SIL-LABEL: sil hidden @$s1M2f1yyF
37+
// CHECK-SIL-NOT: _debuggerTestingCheckExpect
3738
func f1() {
39+
// We don't attempt to instrument in this case because we don't try
40+
// to prove that the var decl is definitively initialized.
41+
var e: Int
42+
e = 5001
43+
print("e = \(e)") // CHECK-E2E-NEXT: e = 5001
44+
}
45+
46+
f1()
47+
48+
// CHECK-SIL-LABEL: sil private @$s1M2f2yyFyyXEfU3_
49+
func f2() {
3850
var b = 2001
3951
b = 2002
4052
// CHECK-SIL: function_ref {{.*}}_debuggerTestingCheckExpectyySS_SStF
4153
print("b = \(b)") // CHECK-E2E-NEXT: b = 2002
4254
}
4355

44-
f1()
56+
f2()
4557

46-
// CHECK-SIL-LABEL: sil private @$s1M2f2yyFyyXEfU_yyXEfU4_
47-
func f2() {
58+
// CHECK-SIL-LABEL: sil private @$s1M2f3yyFyyXEfU_yyXEfU4_
59+
func f3() {
4860
var c: Int = 3001
4961
({ () -> () in
5062
c = 3002
@@ -53,29 +65,17 @@ func f2() {
5365
})()
5466
}
5567

56-
f2()
68+
f3()
5769

58-
// CHECK-SIL-LABEL: sil private @$s1M2f3yySaySiGzFyyXEfU5_
59-
func f3(_ d: inout [Int]) {
70+
// CHECK-SIL-LABEL: sil private @$s1M2f4yySaySiGzFyyXEfU5_
71+
func f4(_ d: inout [Int]) {
6072
d[0] = 4002
6173
// CHECK-SIL: function_ref {{.*}}_debuggerTestingCheckExpectyySS_SStF
6274
print("d[0] = \(d[0])") // CHECK-E2E-NEXT: d[0] = 4002
6375
}
6476

6577
var d: [Int] = [4001]
66-
f3(&d)
67-
68-
// CHECK-SIL-LABEL: sil hidden @$s1M2f4yyF
69-
// CHECK-SIL-NOT: _debuggerTestingCheckExpect
70-
func f4() {
71-
// We don't attempt to instrument in this case because we don't try
72-
// to prove that the var decl is definitively initialized.
73-
var e: Int
74-
e = 5001
75-
print("e = \(e)") // CHECK-E2E-NEXT: e = 5001
76-
}
77-
78-
f4()
78+
f4(&d)
7979

8080
// CHECK-SIL-LABEL: sil private @$s1M2f5yySSzFyyXEfU6_
8181
func f5(_ v: inout String) {

test/Interop/Cxx/exceptions/trap-on-exception-irgen-itanium.swift

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -282,23 +282,25 @@ func testStructWithDefaultDestructor() -> CInt {
282282
return result
283283
}
284284

285-
let _ = testFreeFunctionNoThrowOnly()
286-
let _ = testFreeFunctionCalls()
287-
let _ = testMethodCalls()
288-
testTemplateCalls()
289-
testFuncPtrCall()
290-
testCFuncPtrCall()
291-
testProtocolConformanceThunkInvoke()
292-
let _ = testSubscriptThunkInvoke()
293-
testClassWithDestructor()
294-
testClassWithThrowingDestructor()
295-
let _ = testClassWithCopyConstructor()
296-
let _ = testClassWithThrowingCopyConstructor()
297-
let _ = testClassWithThrowingConstructor()
298-
let _ = testClassWithNoThrowingConstructor()
299-
let _ = testStructWithDefaultConstructor()
300-
let _ = testStructWithDefaultCopyConstructor()
301-
let _ = testStructWithDefaultDestructor()
285+
public func test() {
286+
let _ = testFreeFunctionNoThrowOnly()
287+
let _ = testFreeFunctionCalls()
288+
let _ = testMethodCalls()
289+
testTemplateCalls()
290+
testFuncPtrCall()
291+
testCFuncPtrCall()
292+
testProtocolConformanceThunkInvoke()
293+
let _ = testSubscriptThunkInvoke()
294+
testClassWithDestructor()
295+
testClassWithThrowingDestructor()
296+
let _ = testClassWithCopyConstructor()
297+
let _ = testClassWithThrowingCopyConstructor()
298+
let _ = testClassWithThrowingConstructor()
299+
let _ = testClassWithNoThrowingConstructor()
300+
let _ = testStructWithDefaultConstructor()
301+
let _ = testStructWithDefaultCopyConstructor()
302+
let _ = testStructWithDefaultDestructor()
303+
}
302304

303305
// CHECK: define {{.*}} @"$s4test0A23FreeFunctionNoThrowOnlys5Int32VyF"() #[[#SWIFTMETA:]] {
304306
// CHECK-NEXT: :

test/SILGen/default_arguments.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
// CHECK-LABEL: sil [ossa] @main
77
// CHECK: string_literal utf8 "default_arguments"
88

9+
// Test at top level.
10+
testMagicLiterals()
11+
closure { testMagicLiterals() }
12+
autoclosure(testMagicLiterals())
13+
14+
// CHECK: string_literal utf8 "default_arguments"
15+
let y : String = #function
16+
917
// Default argument for first parameter.
1018
// CHECK-LABEL: sil hidden [ossa] @$s17default_arguments7defarg11i1d1sySi_SdSStFfA_ : $@convention(thin) () -> Int
1119
// CHECK: [[LIT:%[0-9]+]] = integer_literal $Builtin.IntLiteral, 17
@@ -163,14 +171,6 @@ class Foo {
163171

164172
}
165173

166-
// Test at top level.
167-
testMagicLiterals()
168-
closure { testMagicLiterals() }
169-
autoclosure(testMagicLiterals())
170-
171-
// CHECK: string_literal utf8 "default_arguments"
172-
let y : String = #function
173-
174174
// CHECK-LABEL: sil hidden [ossa] @$s17default_arguments16testSelectorCall_17withMagicLiteralsySi_SitF
175175
// CHECK: string_literal utf8 "testSelectorCall(_:withMagicLiterals:)"
176176
func testSelectorCall(_ x: Int, withMagicLiterals y: Int) {

test/SILGen/dynamically_replaceable.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
// RUN: %target-swift-emit-silgen -swift-version 5 %s -enable-implicit-dynamic | %FileCheck %s --check-prefix=IMPLICIT
33
// RUN: %target-swift-emit-silgen -swift-version 5 %s -disable-previous-implementation-calls-in-dynamic-replacements | %FileCheck %s --check-prefix=NOPREVIOUS
44

5+
// IMPLICIT-LABEL: sil private [ossa] @$s23dynamically_replaceable6$deferL_yyF
6+
var x = 10
7+
defer {
8+
let y = x
9+
}
10+
511
// CHECK-LABEL: sil hidden [ossa] @$s23dynamically_replaceable014maybe_dynamic_B0yyF : $@convention(thin) () -> () {
612
// IMPLICIT-LABEL: sil hidden [dynamically_replacable] [ossa] @$s23dynamically_replaceable014maybe_dynamic_B0yyF : $@convention(thin) () -> () {
713
func maybe_dynamic_replaceable() {
@@ -384,12 +390,6 @@ dynamic func funcWithDefaultArg(_ arg : String = String("hello")) {
384390
func foobar() {
385391
}
386392

387-
// IMPLICIT-LABEL: sil private [ossa] @$s23dynamically_replaceable6$deferL_yyF
388-
var x = 10
389-
defer {
390-
let y = x
391-
}
392-
393393
// IMPLICIT-LABEL: sil [dynamically_replacable] [ossa] @$s23dynamically_replaceable16testWithLocalFunyyF
394394
// IMPLICIT-LABEL: sil private [ossa] @$s23dynamically_replaceable16testWithLocalFunyyF05localF0L_yyF
395395
// IMPLICIT-LABEL: sil private [ossa] @$s23dynamically_replaceable16testWithLocalFunyyF05localF0L_yyF0geF0L_yyF

0 commit comments

Comments
 (0)