Skip to content

Commit

Permalink
Merge pull request #41 from apple/master
Browse files Browse the repository at this point in the history
[pull] swiftwasm from apple:master
  • Loading branch information
pull[bot] authored Jan 27, 2020
2 parents f257acf + efa526e commit b0dd5f1
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 90 deletions.
97 changes: 37 additions & 60 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -708,14 +708,6 @@ function(_add_swift_lipo_target)
endif()
endfunction()

function(swift_target_link_search_directories target directories)
set(STLD_FLAGS "")
foreach(directory ${directories})
set(STLD_FLAGS "${STLD_FLAGS} \"${CMAKE_LIBRARY_PATH_FLAG}${directory}\"")
endforeach()
set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS ${STLD_FLAGS})
endfunction()

# Add a single variant of a new Swift library.
#
# Usage:
Expand Down Expand Up @@ -892,22 +884,10 @@ function(_add_swift_library_single target name)
if(SWIFT_EMBED_BITCODE_SECTION AND NOT SWIFTLIB_SINGLE_DONT_EMBED_BITCODE)
if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "IOS" OR "${SWIFTLIB_SINGLE_SDK}" STREQUAL "TVOS" OR "${SWIFTLIB_SINGLE_SDK}" STREQUAL "WATCHOS")
list(APPEND SWIFTLIB_SINGLE_C_COMPILE_FLAGS "-fembed-bitcode")
list(APPEND SWIFTLIB_SINGLE_LINK_FLAGS "-Xlinker" "-bitcode_bundle" "-Xlinker" "-lto_library" "-Xlinker" "${LLVM_LIBRARY_DIR}/libLTO.dylib")
# If we are asked to hide symbols, pass the obfuscation flag to libLTO.
if (SWIFT_EMBED_BITCODE_SECTION_HIDE_SYMBOLS)
list(APPEND SWIFTLIB_SINGLE_LINK_FLAGS "-Xlinker" "-bitcode_hide_symbols")
endif()
set(embed_bitcode_arg EMBED_BITCODE)
endif()
endif()

if(${SWIFTLIB_SINGLE_SDK} IN_LIST SWIFT_APPLE_PLATFORMS)
list(APPEND SWIFTLIB_SINGLE_LINK_FLAGS "-Xlinker" "-compatibility_version" "-Xlinker" "1")
if (SWIFT_COMPILER_VERSION)
list(APPEND SWIFTLIB_SINGLE_LINK_FLAGS "-Xlinker" "-current_version" "-Xlinker" "${SWIFT_COMPILER_VERSION}" )
endif()
endif()

if(XCODE)
string(REGEX MATCHALL "/[^/]+" split_path ${CMAKE_CURRENT_SOURCE_DIR})
list(GET split_path -1 dir)
Expand Down Expand Up @@ -1281,23 +1261,6 @@ function(_add_swift_library_single target name)
"${swift_module_dependency_target}"
${LLVM_COMMON_DEPENDS})

# HACK: On some systems or build directory setups, CMake will not find static
# archives of Clang libraries in the Clang build directory, and it will pass
# them as '-lclangFoo'. Some other logic in CMake would reorder libraries
# specified with this syntax, which breaks linking.
set(prefixed_link_libraries)
foreach(dep ${SWIFTLIB_SINGLE_LINK_LIBRARIES})
if("${dep}" MATCHES "^clang")
if("${SWIFT_HOST_VARIANT_SDK}" STREQUAL "WINDOWS")
set(dep "${LLVM_LIBRARY_OUTPUT_INTDIR}/${dep}.lib")
else()
set(dep "${LLVM_LIBRARY_OUTPUT_INTDIR}/lib${dep}.a")
endif()
endif()
list(APPEND prefixed_link_libraries "${dep}")
endforeach()
set(SWIFTLIB_SINGLE_LINK_LIBRARIES "${prefixed_link_libraries}")

if("${libkind}" STREQUAL "SHARED")
target_link_libraries("${target}" PRIVATE ${SWIFTLIB_SINGLE_LINK_LIBRARIES})
elseif("${libkind}" STREQUAL "OBJECT")
Expand Down Expand Up @@ -1463,17 +1426,32 @@ function(_add_swift_library_single target name)
set(PLIST_INFO_BUILD_VERSION)
endif()

# Convert variables to space-separated strings.
_list_escape_for_shell("${c_compile_flags}" c_compile_flags)
_list_escape_for_shell("${link_flags}" link_flags)

# Set compilation and link flags.
set_property(TARGET "${target}" APPEND_STRING PROPERTY
COMPILE_FLAGS " ${c_compile_flags}")
set_property(TARGET "${target}" APPEND_STRING PROPERTY
LINK_FLAGS " ${link_flags}")
set_property(TARGET "${target}" APPEND PROPERTY LINK_LIBRARIES ${link_libraries})
swift_target_link_search_directories("${target}" "${library_search_directories}")
target_compile_options(${target} PRIVATE
${c_compile_flags})
target_link_options(${target} PRIVATE
${link_flags})
if(${SWIFTLIB_SINGLE_SDK} IN_LIST SWIFT_APPLE_PLATFORMS)
target_link_options(${target} PRIVATE
"LINKER:-compatibility_version,1")
if(SWIFT_COMPILER_VERSION)
target_link_options(${target} PRIVATE
"LINKER:-current_version,${SWIFT_COMPILER_VERSION}")
endif()
# Include LLVM Bitcode slices for iOS, Watch OS, and Apple TV OS device libraries.
if(SWIFT_EMBED_BITCODE_SECTION AND NOT SWIFTLIB_SINGLE_DONT_EMBED_BITCODE)
if(${SWIFTLIB_SINGLE_SDK} MATCHES "(I|TV|WATCH)OS")
target_link_options(${target} PRIVATE
"LINKER:-bitcode_bundle"
$<$<BOOL:SWIFT_EMBED_BITCODE_SECTION_HIDE_SYMBOLS>:"LINKER:-bitcode_hide_symbols">
"LINKER:-lto_library,${LLVM_LIBRARY_DIR}/libLTO.dylib")
endif()
endif()
endif()
target_link_libraries(${target} PRIVATE
${link_libraries})
target_link_directories(${target} PRIVATE
${library_search_directories})

# Adjust the linked libraries for windows targets. On Windows, the link is
# performed against the import library, and the runtime uses the dll. Not
Expand Down Expand Up @@ -1523,8 +1501,8 @@ function(_add_swift_library_single target name)
endif()

if(target_static)
set_property(TARGET "${target_static}" APPEND_STRING PROPERTY
COMPILE_FLAGS " ${c_compile_flags}")
target_link_options(${target_static} PRIVATE
${c_compile_flags})
# FIXME: The fallback paths here are going to be dynamic libraries.

if(SWIFTLIB_INSTALL_WITH_SHARED)
Expand All @@ -1536,7 +1514,8 @@ function(_add_swift_library_single target name)
"${search_base_dir}/${SWIFTLIB_SINGLE_SUBDIR}"
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFTLIB_SINGLE_SUBDIR}"
"${SWIFT_NATIVE_SWIFT_TOOLS_PATH}/../lib/swift/${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_LIB_SUBDIR}")
swift_target_link_search_directories("${target_static}" "${library_search_directories}")
target_link_directories(${target_static} PRIVATE
${library_search_directories})
target_link_libraries("${target_static}" PRIVATE
${SWIFTLIB_SINGLE_PRIVATE_LINK_LIBRARIES})
endif()
Expand Down Expand Up @@ -2637,16 +2616,14 @@ function(_add_swift_executable_single name)
${SWIFTEXE_SINGLE_DEPENDS})
llvm_update_compile_flags("${name}")

# Convert variables to space-separated strings.
_list_escape_for_shell("${c_compile_flags}" c_compile_flags)
_list_escape_for_shell("${link_flags}" link_flags)

set_property(TARGET ${name} APPEND_STRING PROPERTY
COMPILE_FLAGS " ${c_compile_flags}")
swift_target_link_search_directories("${name}" "${library_search_directories}")
set_property(TARGET ${name} APPEND_STRING PROPERTY
LINK_FLAGS " ${link_flags}")
set_property(TARGET ${name} APPEND PROPERTY LINK_LIBRARIES ${link_libraries})
target_compile_options(${name} PRIVATE
${c_compile_flags})
target_link_directories(${name} PRIVATE
${library_search_directories})
target_link_options(${name} PRIVATE
${link_flags})
target_link_libraries(${name} PRIVATE
${link_libraries})
if (SWIFT_PARALLEL_LINK_JOBS)
set_property(TARGET ${name} PROPERTY JOB_POOL_LINK swift_link_job_pool)
endif()
Expand Down
9 changes: 0 additions & 9 deletions cmake/modules/SwiftList.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ function(_list_add_string_suffix input_list suffix result_var_name)
set("${result_var_name}" "${result}" PARENT_SCOPE)
endfunction()

function(_list_escape_for_shell input_list result_var_name)
set(result "")
foreach(element ${input_list})
string(REPLACE " " "\\ " element "${element}")
set(result "${result}${element} ")
endforeach()
set("${result_var_name}" "${result}" PARENT_SCOPE)
endfunction()

function(list_replace input_list old new)
set(replaced_list)
foreach(item ${${input_list}})
Expand Down
2 changes: 2 additions & 0 deletions include/swift/SILOptimizer/PassManager/Passes.def
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ PASS(LICM, "licm",
"Loop Invariant Code Motion")
PASS(LateCodeMotion, "late-codemotion",
"Late Code Motion with Release Hoisting")
PASS(LateDeadFunctionElimination, "late-deadfuncelim",
"Late Dead Function Elimination")
PASS(LateInliner, "late-inline",
"Late Function Inlining")
PASS(LoopCanonicalizer, "loop-canonicalizer",
Expand Down
33 changes: 27 additions & 6 deletions lib/SILOptimizer/IPO/DeadFunctionElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class FunctionLivenessComputation {

llvm::SmallPtrSet<void *, 32> AliveFunctionsAndTables;

bool keepExternalWitnessTablesAlive;

/// Checks is a function is alive, e.g. because it is visible externally.
bool isAnchorFunction(SILFunction *F) {

Expand Down Expand Up @@ -148,6 +150,11 @@ class FunctionLivenessComputation {
/// Marks all contained functions and witness tables of a witness table as
/// alive.
void makeAlive(SILWitnessTable *WT) {
if (isAvailableExternally(WT->getLinkage()) &&
!keepExternalWitnessTablesAlive) {
return;
}

LLVM_DEBUG(llvm::dbgs() << " scan witness table " << WT->getName()
<< '\n');

Expand Down Expand Up @@ -393,8 +400,10 @@ class FunctionLivenessComputation {
}

public:
FunctionLivenessComputation(SILModule *module) :
Module(module) {}
FunctionLivenessComputation(SILModule *module,
bool keepExternalWitnessTablesAlive) :
Module(module),
keepExternalWitnessTablesAlive(keepExternalWitnessTablesAlive) {}

/// The main entry point of the optimization.
bool findAliveFunctions() {
Expand Down Expand Up @@ -635,8 +644,8 @@ class DeadFunctionElimination : FunctionLivenessComputation {
}

public:
DeadFunctionElimination(SILModule *module)
: FunctionLivenessComputation(module) {}
DeadFunctionElimination(SILModule *module, bool keepExternalWitnessTablesAlive)
: FunctionLivenessComputation(module, keepExternalWitnessTablesAlive) {}

/// The main entry point of the optimization.
void eliminateFunctions(SILModuleTransform *DFEPass) {
Expand Down Expand Up @@ -693,6 +702,13 @@ class DeadFunctionElimination : FunctionLivenessComputation {
namespace {

class SILDeadFuncElimination : public SILModuleTransform {

private:
bool isLateDFE;

public:
SILDeadFuncElimination(bool isLateDFE) : isLateDFE(isLateDFE) { }

void run() override {
LLVM_DEBUG(llvm::dbgs() << "Running DeadFuncElimination\n");

Expand All @@ -703,15 +719,20 @@ class SILDeadFuncElimination : public SILModuleTransform {
// can eliminate such functions.
getModule()->invalidateSILLoaderCaches();

DeadFunctionElimination deadFunctionElimination(getModule());
DeadFunctionElimination deadFunctionElimination(getModule(),
/*keepExternalWitnessTablesAlive*/ !isLateDFE);
deadFunctionElimination.eliminateFunctions(this);
}
};

} // end anonymous namespace

SILTransform *swift::createDeadFunctionElimination() {
return new SILDeadFuncElimination();
return new SILDeadFuncElimination(/*isLateDFE*/ false);
}

SILTransform *swift::createLateDeadFunctionElimination() {
return new SILDeadFuncElimination(/*isLateDFE*/ true);
}

void swift::performSILDeadFunctionElimination(SILModule *M) {
Expand Down
4 changes: 3 additions & 1 deletion lib/SILOptimizer/PassManager/PassPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ static void addLateLoopOptPassPipeline(SILPassPipelinePlan &P) {
P.startPipeline("LateLoopOpt");

// Delete dead code and drop the bodies of shared functions.
P.addDeadFunctionElimination();
// Also, remove externally available witness tables. They are not needed
// anymore after the last devirtualizer run.
P.addLateDeadFunctionElimination();

// Perform the final lowering transformations.
P.addCodeSinking();
Expand Down
2 changes: 1 addition & 1 deletion lib/SymbolGraphGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ add_swift_host_library(swiftSymbolGraphGen STATIC
SymbolGraphGen.cpp
SymbolGraphASTWalker.cpp)

target_link_libraries(swiftSymbolGraphGen
target_link_libraries(swiftSymbolGraphGen PRIVATE
swiftAST
swiftFrontend
swiftMarkup)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -sil-inline-threshold 0 %S/Inputs/init_existential_inst_deserializes_witness_tables_input.swift -o %t/Swift.swiftmodule -emit-module -parse-as-library -parse-stdlib -module-link-name swiftCore -module-name Swift -O
// RUN: %target-swift-frontend -I %t -O %s -emit-sil -o - | %FileCheck %s
// RUN: %target-swift-frontend -I %t -O %s -Xllvm -sil-disable-pass=late-deadfuncelim -emit-sil -o - | %FileCheck %s

// CHECK: sil_witness_table public_external X: P module Swift {

Expand Down
2 changes: 1 addition & 1 deletion test/SILOptimizer/devirt_opaque_witness.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/opaque_conformance.swiftmodule -primary-file %S/Inputs/opaque_conformance.swift
// RUN: %target-swift-frontend -O -emit-sil -primary-file %s -I %t | %FileCheck %s
// RUN: %target-swift-frontend -O -emit-sil -primary-file %s -I %t -Xllvm -sil-disable-pass=late-deadfuncelim | %FileCheck %s

import opaque_conformance

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module %S/Inputs/sil_witness_tables_external_input.swift -o %t/Swift.swiftmodule -parse-stdlib -parse-as-library -module-name Swift -module-link-name swiftCore
// RUN: %target-swift-frontend -O -I %t %s -emit-sil | %FileCheck %s
// RUN: %target-swift-frontend -O -I %t %s -Xllvm -sil-disable-pass=late-deadfuncelim -emit-sil | %FileCheck %s
// RUN: %target-swift-frontend -O -I %t %s -emit-sil | %FileCheck -check-prefix=CHECK-DFE %s

import Swift

// Make sure the specializer produces an external witness table.
//
// CHECK: sil_witness_table public_external X: P module Swift {

// Also check that late dead-function-elimination is removing externally
// available witness tables.
//
// CHECK-DFE-NOT: sil_witness_table public_external

func doSomething<T : P>(_ t : T) -> Y {
return t.doSomething()
}
Expand Down
18 changes: 8 additions & 10 deletions tools/SourceKit/cmake/modules/AddSwiftSourceKit.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,15 @@ function(add_sourcekit_default_compiler_flags target)
LINK_LIBRARIES_VAR_NAME link_libraries
LIBRARY_SEARCH_DIRECTORIES_VAR_NAME library_search_directories)

# Convert variables to space-separated strings.
_list_escape_for_shell("${c_compile_flags}" c_compile_flags)
_list_escape_for_shell("${link_flags}" link_flags)

# Set compilation and link flags.
set_property(TARGET "${target}" APPEND_STRING PROPERTY
COMPILE_FLAGS " ${c_compile_flags} -fblocks")
set_property(TARGET "${target}" APPEND_STRING PROPERTY
LINK_FLAGS " ${link_flags}")
set_property(TARGET "${target}" APPEND PROPERTY LINK_LIBRARIES ${link_libraries})
swift_target_link_search_directories("${target}" "${library_search_directories}")
target_compile_options(${target} PRIVATE
-fblocks)
target_link_options(${target} PRIVATE
${link_flags})
target_link_directories(${target} PRIVATE
${library_search_directories})
target_link_libraries(${target} PRIVATE
${link_libraries})
endfunction()

# Add a new SourceKit library.
Expand Down

0 comments on commit b0dd5f1

Please sign in to comment.