Skip to content

Commit 94c00c4

Browse files
authored
Merge pull request #10200 from slavapestov/default-args-tweak
Don't emit some unnecessary kinds of default argument generators
2 parents 8287835 + 46bc2b1 commit 94c00c4

File tree

7 files changed

+33
-44
lines changed

7 files changed

+33
-44
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -916,15 +916,33 @@ void SILGenModule::emitDestructor(ClassDecl *cd, DestructorDecl *dd) {
916916
}
917917
}
918918

919-
void SILGenModule::emitDefaultArgGenerator(SILDeclRef constant, Expr *arg) {
919+
void SILGenModule::emitDefaultArgGenerator(SILDeclRef constant, Expr *arg,
920+
DefaultArgumentKind kind) {
921+
switch (kind) {
922+
case DefaultArgumentKind::None:
923+
llvm_unreachable("No default argument here?");
924+
925+
case DefaultArgumentKind::Normal:
926+
break;
927+
928+
case DefaultArgumentKind::Inherited:
929+
return;
930+
931+
case DefaultArgumentKind::Column:
932+
case DefaultArgumentKind::File:
933+
case DefaultArgumentKind::Line:
934+
case DefaultArgumentKind::Function:
935+
case DefaultArgumentKind::DSOHandle:
936+
case DefaultArgumentKind::Nil:
937+
case DefaultArgumentKind::EmptyArray:
938+
case DefaultArgumentKind::EmptyDictionary:
939+
return;
940+
}
941+
920942
emitOrDelayFunction(*this, constant, [this,constant,arg](SILFunction *f) {
921943
preEmitFunction(constant, arg, f, arg);
922944
PrettyStackTraceSILFunction X("silgen emitDefaultArgGenerator ", f);
923945
SILGenFunction SGF(*this, *f);
924-
// Override location for #file, #line etc. to an invalid one so that we
925-
// don't put extra strings into the default argument generator function that
926-
// is not going to be ever used anyway.
927-
SGF.overrideLocationForMagicIdentifiers = SourceLoc();
928946
SGF.emitGeneratorFunction(constant, arg);
929947
postEmitFunction(constant, f);
930948
});
@@ -1004,7 +1022,7 @@ void SILGenModule::emitDefaultArgGenerators(SILDeclRef::Loc decl,
10041022
for (auto param : *paramList) {
10051023
if (auto defaultArg = param->getDefaultValue())
10061024
emitDefaultArgGenerator(SILDeclRef::getDefaultArgGenerator(decl, index),
1007-
defaultArg);
1025+
defaultArg, param->getDefaultArgumentKind());
10081026
++index;
10091027
}
10101028
}

lib/SILGen/SILGen.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
264264
void emitEnumConstructor(EnumElementDecl *decl);
265265

266266
/// Emits the default argument generator with the given expression.
267-
void emitDefaultArgGenerator(SILDeclRef constant, Expr *arg);
267+
void emitDefaultArgGenerator(SILDeclRef constant, Expr *arg,
268+
DefaultArgumentKind kind);
268269

269270
/// Emits the stored property initializer for the given pattern.
270271
void emitStoredPropertyInitialization(PatternBindingDecl *pd, unsigned i);

lib/SILGen/SILGenApply.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4758,14 +4758,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
47584758
init = stringLiteral->getInitializer();
47594759
} else {
47604760
ASTContext &ctx = getASTContext();
4761-
SourceLoc loc;
4762-
4763-
// If "overrideLocationForMagicIdentifiers" is set, then we use it as the
4764-
// location point for these magic identifiers.
4765-
if (overrideLocationForMagicIdentifiers)
4766-
loc = overrideLocationForMagicIdentifiers.getValue();
4767-
else
4768-
loc = literal->getStartLoc();
4761+
SourceLoc loc = literal->getStartLoc();
47694762

47704763
auto magicLiteral = cast<MagicIdentifierLiteralExpr>(literal);
47714764
switch (magicLiteral->getKind()) {

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2933,14 +2933,7 @@ RValue RValueEmitter::
29332933
visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) {
29342934
ASTContext &Ctx = SGF.getASTContext();
29352935
SILType Ty = SGF.getLoweredLoadableType(E->getType());
2936-
SourceLoc Loc;
2937-
2938-
// If "overrideLocationForMagicIdentifiers" is set, then we use it as the
2939-
// location point for these magic identifiers.
2940-
if (SGF.overrideLocationForMagicIdentifiers)
2941-
Loc = SGF.overrideLocationForMagicIdentifiers.getValue();
2942-
else
2943-
Loc = E->getStartLoc();
2936+
SourceLoc Loc = E->getStartLoc();
29442937

29452938
switch (E->getKind()) {
29462939
case MagicIdentifierLiteralExpr::File:

lib/SILGen/SILGenFunction.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,12 +470,6 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
470470
/// function is valid.
471471
bool allowsVoidReturn() const { return ReturnDest.getBlock()->args_empty(); }
472472

473-
/// This location, when set, is used as an override location for magic
474-
/// identifier expansion (e.g. #file). This allows default argument
475-
/// expansion to report the location of the call, instead of the location
476-
/// of the original expr.
477-
Optional<SourceLoc> overrideLocationForMagicIdentifiers;
478-
479473
/// Emit code to increment a counter for profiling.
480474
void emitProfilerIncrement(ASTNode N) {
481475
if (SGM.Profiler && SGM.Profiler->hasRegionCounters())

test/SILGen/default_arguments.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -emit-silgen %s | %FileCheck %s
2+
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -emit-silgen %s | %FileCheck %s --check-prefix=NEGATIVE
23

34
// __FUNCTION__ used as top-level parameter produces the module name.
45
// CHECK-LABEL: sil @main
@@ -74,17 +75,13 @@ func testMagicLiterals(file: String = #file,
7475
// Check that default argument generator functions don't leak information about
7576
// user's source.
7677
//
77-
// CHECK-LABEL: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA_
78-
// CHECK: string_literal utf16 ""
78+
// NEGATIVE-NOT: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA_
7979
//
80-
// CHECK-LABEL: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA0_
81-
// CHECK: string_literal utf16 ""
80+
// NEGATIVE-NOT: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA0_
8281
//
83-
// CHECK-LABEL: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA1_
84-
// CHECK: integer_literal $Builtin.Int2048, 0
82+
// NEGATIVE-NOT: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA1_
8583
//
86-
// CHECK-LABEL: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA2_
87-
// CHECK: integer_literal $Builtin.Int2048, 0
84+
// NEGATIVE-NOT: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA2_
8885

8986
func closure(_: () -> ()) {}
9087
func autoclosure(_: @autoclosure () -> ()) {}

test/SILGen/dso_handle.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,9 @@
88
// CHECK-NEXT: [[DSOPtr:%[0-9]+]] = address_to_pointer [[DSOAddr]] : $*Builtin.RawPointer to $Builtin.RawPointer
99
// CHECK-NEXT: [[DSOPtrStruct:[0-9]+]] = struct $UnsafeRawPointer ([[DSOPtr]] : $Builtin.RawPointer)
1010

11-
12-
// CHECK-LABEL: sil hidden @_T010dso_handle14printDSOHandleS2V0A0_tFfA_
13-
// CHECK: [[DSOAddr:%[0-9]+]] = global_addr [[DSO]] : $*Builtin.RawPointer
14-
// CHECK-NEXT: [[DSOPtr:%[0-9]+]] = address_to_pointer [[DSOAddr]] : $*Builtin.RawPointer to $Builtin.RawPointer
15-
// CHECK-NEXT: [[DSOPtrStruct:%[0-9]+]] = struct $UnsafeRawPointer ([[DSOPtr]] : $Builtin.RawPointer)
16-
// CHECK-NEXT: return [[DSOPtrStruct]] : $UnsafeRawPointer
1711
func printDSOHandle(dso: UnsafeRawPointer = #dsohandle) -> UnsafeRawPointer {
1812
print(dso)
1913
return dso
2014
}
2115

2216
_ = printDSOHandle()
23-

0 commit comments

Comments
 (0)