Skip to content
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

Don't emit some unnecessary kinds of default argument generators #10200

Merged
merged 2 commits into from
Jun 12, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 24 additions & 6 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,15 +916,33 @@ void SILGenModule::emitDestructor(ClassDecl *cd, DestructorDecl *dd) {
}
}

void SILGenModule::emitDefaultArgGenerator(SILDeclRef constant, Expr *arg) {
void SILGenModule::emitDefaultArgGenerator(SILDeclRef constant, Expr *arg,
DefaultArgumentKind kind) {
switch (kind) {
case DefaultArgumentKind::None:
llvm_unreachable("No default argument here?");

case DefaultArgumentKind::Normal:
break;

case DefaultArgumentKind::Inherited:
return;

case DefaultArgumentKind::Column:
case DefaultArgumentKind::File:
case DefaultArgumentKind::Line:
case DefaultArgumentKind::Function:
case DefaultArgumentKind::DSOHandle:
case DefaultArgumentKind::Nil:
case DefaultArgumentKind::EmptyArray:
case DefaultArgumentKind::EmptyDictionary:
return;
}

emitOrDelayFunction(*this, constant, [this,constant,arg](SILFunction *f) {
preEmitFunction(constant, arg, f, arg);
PrettyStackTraceSILFunction X("silgen emitDefaultArgGenerator ", f);
SILGenFunction SGF(*this, *f);
// Override location for #file, #line etc. to an invalid one so that we
// don't put extra strings into the default argument generator function that
// is not going to be ever used anyway.
SGF.overrideLocationForMagicIdentifiers = SourceLoc();
SGF.emitGeneratorFunction(constant, arg);
postEmitFunction(constant, f);
});
Expand Down Expand Up @@ -1004,7 +1022,7 @@ void SILGenModule::emitDefaultArgGenerators(SILDeclRef::Loc decl,
for (auto param : *paramList) {
if (auto defaultArg = param->getDefaultValue())
emitDefaultArgGenerator(SILDeclRef::getDefaultArgGenerator(decl, index),
defaultArg);
defaultArg, param->getDefaultArgumentKind());
++index;
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/SILGen/SILGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
void emitEnumConstructor(EnumElementDecl *decl);

/// Emits the default argument generator with the given expression.
void emitDefaultArgGenerator(SILDeclRef constant, Expr *arg);
void emitDefaultArgGenerator(SILDeclRef constant, Expr *arg,
DefaultArgumentKind kind);

/// Emits the stored property initializer for the given pattern.
void emitStoredPropertyInitialization(PatternBindingDecl *pd, unsigned i);
Expand Down
9 changes: 1 addition & 8 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4758,14 +4758,7 @@ RValue SILGenFunction::emitLiteral(LiteralExpr *literal, SGFContext C) {
init = stringLiteral->getInitializer();
} else {
ASTContext &ctx = getASTContext();
SourceLoc loc;

// If "overrideLocationForMagicIdentifiers" is set, then we use it as the
// location point for these magic identifiers.
if (overrideLocationForMagicIdentifiers)
loc = overrideLocationForMagicIdentifiers.getValue();
else
loc = literal->getStartLoc();
SourceLoc loc = literal->getStartLoc();

auto magicLiteral = cast<MagicIdentifierLiteralExpr>(literal);
switch (magicLiteral->getKind()) {
Expand Down
9 changes: 1 addition & 8 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2933,14 +2933,7 @@ RValue RValueEmitter::
visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) {
ASTContext &Ctx = SGF.getASTContext();
SILType Ty = SGF.getLoweredLoadableType(E->getType());
SourceLoc Loc;

// If "overrideLocationForMagicIdentifiers" is set, then we use it as the
// location point for these magic identifiers.
if (SGF.overrideLocationForMagicIdentifiers)
Loc = SGF.overrideLocationForMagicIdentifiers.getValue();
else
Loc = E->getStartLoc();
SourceLoc Loc = E->getStartLoc();

switch (E->getKind()) {
case MagicIdentifierLiteralExpr::File:
Expand Down
6 changes: 0 additions & 6 deletions lib/SILGen/SILGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,6 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
/// function is valid.
bool allowsVoidReturn() const { return ReturnDest.getBlock()->args_empty(); }

/// This location, when set, is used as an override location for magic
/// identifier expansion (e.g. #file). This allows default argument
/// expansion to report the location of the call, instead of the location
/// of the original expr.
Optional<SourceLoc> overrideLocationForMagicIdentifiers;

/// Emit code to increment a counter for profiling.
void emitProfilerIncrement(ASTNode N) {
if (SGM.Profiler && SGM.Profiler->hasRegionCounters())
Expand Down
13 changes: 5 additions & 8 deletions test/SILGen/default_arguments.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -emit-silgen %s | %FileCheck %s
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -emit-silgen %s | %FileCheck %s --check-prefix=NEGATIVE

// __FUNCTION__ used as top-level parameter produces the module name.
// CHECK-LABEL: sil @main
Expand Down Expand Up @@ -74,17 +75,13 @@ func testMagicLiterals(file: String = #file,
// Check that default argument generator functions don't leak information about
// user's source.
//
// CHECK-LABEL: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA_
// CHECK: string_literal utf16 ""
// NEGATIVE-NOT: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA_
//
// CHECK-LABEL: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA0_
// CHECK: string_literal utf16 ""
// NEGATIVE-NOT: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA0_
//
// CHECK-LABEL: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA1_
// CHECK: integer_literal $Builtin.Int2048, 0
// NEGATIVE-NOT: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA1_
//
// CHECK-LABEL: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA2_
// CHECK: integer_literal $Builtin.Int2048, 0
// NEGATIVE-NOT: sil hidden @_T017default_arguments17testMagicLiteralsySS4file_SS8functionSi4lineSi6columntFfA2_

func closure(_: () -> ()) {}
func autoclosure(_: @autoclosure () -> ()) {}
Expand Down
7 changes: 0 additions & 7 deletions test/SILGen/dso_handle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,9 @@
// CHECK-NEXT: [[DSOPtr:%[0-9]+]] = address_to_pointer [[DSOAddr]] : $*Builtin.RawPointer to $Builtin.RawPointer
// CHECK-NEXT: [[DSOPtrStruct:[0-9]+]] = struct $UnsafeRawPointer ([[DSOPtr]] : $Builtin.RawPointer)


// CHECK-LABEL: sil hidden @_T010dso_handle14printDSOHandleS2V0A0_tFfA_
// CHECK: [[DSOAddr:%[0-9]+]] = global_addr [[DSO]] : $*Builtin.RawPointer
// CHECK-NEXT: [[DSOPtr:%[0-9]+]] = address_to_pointer [[DSOAddr]] : $*Builtin.RawPointer to $Builtin.RawPointer
// CHECK-NEXT: [[DSOPtrStruct:%[0-9]+]] = struct $UnsafeRawPointer ([[DSOPtr]] : $Builtin.RawPointer)
// CHECK-NEXT: return [[DSOPtrStruct]] : $UnsafeRawPointer
func printDSOHandle(dso: UnsafeRawPointer = #dsohandle) -> UnsafeRawPointer {
print(dso)
return dso
}

_ = printDSOHandle()