Skip to content

Fixes for #dsohandle default arguments [4.0] #10349

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
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
26 changes: 24 additions & 2 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,29 @@ 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);
Expand Down Expand Up @@ -1004,7 +1026,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
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2927,7 +2927,7 @@ visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) {
auto DSOGlobal = SGF.SGM.M.lookUpGlobalVariable("__dso_handle");
if (!DSOGlobal)
DSOGlobal = SILGlobalVariable::create(SGF.SGM.M,
SILLinkage::HiddenExternal,
SILLinkage::PublicExternal,
IsNotSerialized, "__dso_handle",
BuiltinRawPtrTy);
auto DSOAddr = SGF.B.createGlobalAddr(SILLoc, DSOGlobal);
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
17 changes: 9 additions & 8 deletions test/SILGen/dso_handle.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -emit-silgen %s | %FileCheck %s

// CHECK: sil_global hidden_external [[DSO:@__dso_handle]] : $Builtin.RawPointer
// CHECK: sil_global [[DSO:@__dso_handle]] : $Builtin.RawPointer

// CHECK-LABEL: sil @main : $@convention(c)
// CHECK: bb0
// 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-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()
@_inlineable public func printDSOHandleInlineable(dso: UnsafeRawPointer = #dsohandle) -> UnsafeRawPointer {
return dso
}

@_inlineable public func callsPrintDSOHandleInlineable() {
printDSOHandleInlineable()
}

_ = printDSOHandle()