Skip to content

Commit 7fd69a0

Browse files
authored
Merge pull request #37058 from eeckstein/libswift
libswift: Start implementing SIL optimizations in Swift
2 parents 1a97f24 + e8d408d commit 7fd69a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+3135
-60
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ set(SWIFT_TOOLS_ENABLE_LTO OFF CACHE STRING "Build Swift tools with LTO. One
191191
option only affects the tools that run on the host (the compiler), and has
192192
no effect on the target libraries (the standard library and the runtime).")
193193

194+
option(SWIFT_TOOLS_ENABLE_LIBSWIFT
195+
"Enable building libswift and linking libswift into the compiler itself."
196+
FALSE)
197+
194198
# The following only works with the Ninja generator in CMake >= 3.0.
195199
set(SWIFT_PARALLEL_LINK_JOBS "" CACHE STRING
196200
"Define the maximum number of linker jobs for swift.")
@@ -891,6 +895,7 @@ if(SWIFT_INCLUDE_TOOLS)
891895
message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
892896
message(STATUS " Assertions: ${LLVM_ENABLE_ASSERTIONS}")
893897
message(STATUS " LTO: ${SWIFT_TOOLS_ENABLE_LTO}")
898+
message(STATUS " libswift: ${SWIFT_TOOLS_ENABLE_LIBSWIFT}")
894899
message(STATUS "")
895900
else()
896901
message(STATUS "Not building host Swift tools")
@@ -1034,6 +1039,11 @@ add_subdirectory(include)
10341039
if(SWIFT_INCLUDE_TOOLS)
10351040
add_subdirectory(lib)
10361041

1042+
# "libswift" must come before "tools".
1043+
# It adds libswift module names to the global property "libswift_modules"
1044+
# which is used in add_swift_host_tool for the lldb workaround.
1045+
add_subdirectory(libswift)
1046+
10371047
# Always include this after including stdlib/!
10381048
# Refer to the large comment above the add_subdirectory(stdlib) call.
10391049
# https://bugs.swift.org/browse/SR-5975

cmake/modules/AddSwift.cmake

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,121 @@ function(add_swift_host_library name)
607607
endif()
608608
endfunction()
609609
610+
# Add a module of libswift
611+
#
612+
# Creates a target to compile a module which is part of libswift.
613+
# Adds the module name to the global property "libswift_modules".
614+
#
615+
# This is a temporary workaround until it's possible to compile libswift with
616+
# cmake's builtin swift support.
617+
function(add_libswift_module module)
618+
cmake_parse_arguments(ALSM
619+
""
620+
"DEPENDS"
621+
""
622+
${ARGN})
623+
set(sources ${ALSM_UNPARSED_ARGUMENTS})
624+
list(TRANSFORM sources PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/")
625+
626+
set(target_name "LibSwift${module}")
627+
628+
# Add a target which depends on the actual compilation target, which
629+
# will be created in add_libswift.
630+
# This target is mainly used to add properties, like the list of source files.
631+
add_custom_target(
632+
${target_name}
633+
SOURCES ${sources}
634+
COMMENT "libswift module ${module}")
635+
636+
set_property(TARGET ${target_name} PROPERTY "module_name" ${module})
637+
set_property(TARGET ${target_name} PROPERTY "module_depends" ${ALSM_DEPENDS})
638+
639+
get_property(modules GLOBAL PROPERTY "libswift_modules")
640+
set_property(GLOBAL PROPERTY "libswift_modules" ${modules} ${module})
641+
endfunction()
642+
643+
# Add source files to a libswift module.
644+
#
645+
# This is a temporary workaround until it's possible to compile libswift with
646+
# cmake's builtin swift support.
647+
function(libswift_sources module)
648+
cmake_parse_arguments(LSS
649+
""
650+
""
651+
""
652+
${ARGN})
653+
set(sources ${LSS_UNPARSED_ARGUMENTS})
654+
list(TRANSFORM sources PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/")
655+
656+
set(target_name "LibSwift${module}")
657+
set_property(TARGET "LibSwift${module}" APPEND PROPERTY SOURCES ${sources})
658+
endfunction()
659+
660+
# Add the libswift library.
661+
#
662+
# Adds targets to compile all modules of libswift and a target for the
663+
# libswift library itself.
664+
#
665+
# This is a temporary workaround until it's possible to compile libswift with
666+
# cmake's builtin swift support.
667+
function(add_libswift name)
668+
if(CMAKE_BUILD_TYPE STREQUAL Debug)
669+
set(libswift_compile_options "-g")
670+
else()
671+
set(libswift_compile_options "-O" "-cross-module-optimization")
672+
endif()
673+
674+
set(build_dir ${CMAKE_CURRENT_BINARY_DIR})
675+
676+
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_APPLE_PLATFORMS)
677+
set(deployment_version "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
678+
endif()
679+
get_versioned_target_triple(target ${SWIFT_HOST_VARIANT_SDK}
680+
${SWIFT_HOST_VARIANT_ARCH} "${deployment_version}")
681+
682+
get_property(modules GLOBAL PROPERTY "libswift_modules")
683+
foreach(module ${modules})
684+
685+
set(module_target "LibSwift${module}")
686+
get_target_property(module ${module_target} "module_name")
687+
get_target_property(sources ${module_target} SOURCES)
688+
get_target_property(dependencies ${module_target} "module_depends")
689+
if(dependencies)
690+
list(TRANSFORM dependencies PREPEND "LibSwift")
691+
else()
692+
set(dependencies "")
693+
endif()
694+
695+
set(module_obj_file "${build_dir}/${module}.o")
696+
set(module_file "${build_dir}/${module}.swiftmodule")
697+
set_property(TARGET ${module_target} PROPERTY "module_file" "${module_file}")
698+
699+
set(all_obj_files ${all_obj_files} ${module_obj_file})
700+
701+
# Compile the libswift module into an object file
702+
add_custom_command_target(dep_target OUTPUT ${module_obj_file}
703+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
704+
DEPENDS ${sources} ${dependencies}
705+
COMMAND ${CMAKE_Swift_COMPILER} "-c" "-o" ${module_obj_file}
706+
"-sdk" "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}"
707+
"-target" ${target}
708+
"-module-name" ${module} "-emit-module"
709+
"-emit-module-path" "${build_dir}/${module}.swiftmodule"
710+
"-parse-as-library" ${sources}
711+
"-wmo" ${libswift_compile_options}
712+
"-I" "${CMAKE_SOURCE_DIR}/include/swift"
713+
"-I" "${build_dir}"
714+
COMMENT "Building libswift module ${module}")
715+
716+
add_dependencies(${module_target} ${dep_target})
717+
718+
endforeach()
719+
720+
# Create a static libswift library containing all module object files.
721+
add_library(${name} STATIC ${all_obj_files})
722+
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX)
723+
endfunction()
724+
610725
macro(add_swift_tool_subdirectory name)
611726
add_llvm_subdirectory(SWIFT TOOL ${name})
612727
endmacro()
@@ -616,7 +731,7 @@ macro(add_swift_lib_subdirectory name)
616731
endmacro()
617732
618733
function(add_swift_host_tool executable)
619-
set(options)
734+
set(options HAS_LIBSWIFT)
620735
set(single_parameter_options SWIFT_COMPONENT)
621736
set(multiple_parameter_options LLVM_LINK_COMPONENTS)
622737
@@ -668,6 +783,24 @@ function(add_swift_host_tool executable)
668783
get_filename_component(TOOLCHAIN_BIN_DIR ${CMAKE_Swift_COMPILER} DIRECTORY)
669784
get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/macosx" ABSOLUTE)
670785
target_link_directories(${executable} PUBLIC ${TOOLCHAIN_LIB_DIR})
786+
787+
if (ASHT_HAS_LIBSWIFT AND SWIFT_TOOLS_ENABLE_LIBSWIFT)
788+
# Workaround to make lldb happy: we have to explicitly add all libswift modules
789+
# to the linker command line.
790+
set(libswift_ast_path_flags "-Wl")
791+
get_property(modules GLOBAL PROPERTY "libswift_modules")
792+
foreach(module ${modules})
793+
get_target_property(module_file "LibSwift${module}" "module_file")
794+
string(APPEND libswift_ast_path_flags ",-add_ast_path,${module_file}")
795+
endforeach()
796+
797+
set_property(TARGET ${executable} APPEND_STRING PROPERTY
798+
LINK_FLAGS ${libswift_ast_path_flags})
799+
800+
# Workaround for a linker crash related to autolinking: rdar://77839981
801+
set_property(TARGET ${executable} APPEND_STRING PROPERTY
802+
LINK_FLAGS " -lobjc ")
803+
endif()
671804
endif()
672805
673806
# Lists of rpaths that we are going to add to our executables.
@@ -687,6 +820,22 @@ function(add_swift_host_tool executable)
687820
set_target_properties(${executable} PROPERTIES
688821
BUILD_WITH_INSTALL_RPATH YES
689822
INSTALL_RPATH "${RPATH_LIST}")
823+
824+
elseif(SWIFT_HOST_VARIANT_SDK STREQUAL "LINUX")
825+
if (ASHT_HAS_LIBSWIFT AND SWIFT_TOOLS_ENABLE_LIBSWIFT)
826+
# At build time and and run time, link against the swift libraries in the
827+
# installed host toolchain.
828+
get_filename_component(swift_bin_dir ${CMAKE_Swift_COMPILER} DIRECTORY)
829+
get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY)
830+
set(host_lib_dir "${swift_dir}/lib/swift/linux")
831+
832+
target_link_libraries(${executable} PRIVATE "swiftCore")
833+
834+
target_link_directories(${executable} PRIVATE ${host_lib_dir})
835+
set_target_properties(${executable} PROPERTIES
836+
BUILD_WITH_INSTALL_RPATH YES
837+
INSTALL_RPATH "${host_lib_dir}")
838+
endif()
690839
endif()
691840
692841
llvm_update_compile_flags(${executable})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===--- InitializeLibSwift.h -----------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_BASIC_INITIALIZELIBSWIFT_H
14+
#define SWIFT_BASIC_INITIALIZELIBSWIFT_H
15+
16+
#ifdef __cplusplus
17+
extern "C" {
18+
#endif
19+
20+
void initializeLibSwift();
21+
22+
#ifdef __cplusplus
23+
}
24+
#endif
25+
26+
#endif // SWIFT_BASIC_INITIALIZELIBSWIFT_H
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===--- BridgedSwiftObject.h - C header which defines SwiftObject --------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This is a C header, which defines the SwiftObject header. For the C++ version
14+
// see SwiftObjectHeader.h.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef SWIFT_SIL_BRIDGEDSWIFTOBJECT_H
19+
#define SWIFT_SIL_BRIDGEDSWIFTOBJECT_H
20+
21+
#include <stdint.h>
22+
23+
#if !__has_feature(nullability)
24+
# define _Nullable
25+
# define _Nonnull
26+
# define _Null_unspecified
27+
#endif
28+
29+
typedef const void * _Nonnull SwiftMetatype;
30+
31+
/// The header of a Swift object.
32+
///
33+
/// This must be in sync with HeapObject, which is defined in the runtime lib.
34+
/// It must be layout compatible with the Swift object header.
35+
struct BridgedSwiftObject {
36+
SwiftMetatype metatype;
37+
int64_t refCounts;
38+
};
39+
40+
typedef struct BridgedSwiftObject * _Nonnull SwiftObject;
41+
typedef struct BridgedSwiftObject * _Nullable OptionalSwiftObject;
42+
43+
#endif

include/swift/SIL/SILArgument.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ class SILArgument : public ValueBase {
196196
}
197197
};
198198

199+
inline SILArgument *castToArgument(SwiftObject argument) {
200+
return static_cast<SILArgument *>(argument);
201+
}
202+
199203
class SILPhiArgument : public SILArgument {
200204
friend class SILBasicBlock;
201205

include/swift/SIL/SILBasicBlock.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/SIL/SILArgumentArrayRef.h"
2323
#include "swift/SIL/SILInstruction.h"
2424
#include "swift/SIL/SILArgument.h"
25+
#include "swift/SIL/SwiftObjectHeader.h"
2526
#include "llvm/ADT/TinyPtrVector.h"
2627

2728
namespace swift {
@@ -31,13 +32,16 @@ class SILArgument;
3132
class SILPrintContext;
3233

3334
class SILBasicBlock :
34-
public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
35+
public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock>,
36+
public SwiftObjectHeader {
3537
friend class SILSuccessor;
3638
friend class SILFunction;
3739
friend class SILGlobalVariable;
3840
template <typename, unsigned> friend class BasicBlockData;
3941
friend class BasicBlockBitfield;
4042

43+
static SwiftMetatype registeredMetatype;
44+
4145
public:
4246
using InstListType = llvm::iplist<SILInstruction>;
4347
private:
@@ -47,7 +51,7 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
4751
/// PrevList - This is a list of all of the terminator operands that are
4852
/// branching to this block, forming the predecessor list. This is
4953
/// automatically managed by the SILSuccessor class.
50-
SILSuccessor *PredList;
54+
SILSuccessor *PredList = nullptr;
5155

5256
/// This is the list of basic block arguments for this block.
5357
/// A TinyPtrVector is the right choice, because ~98% of blocks have 0 or 1
@@ -82,14 +86,18 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
8286
uint64_t lastInitializedBitfieldID = 0;
8387

8488
friend struct llvm::ilist_traits<SILBasicBlock>;
85-
SILBasicBlock() : Parent(nullptr) {}
86-
void operator=(const SILBasicBlock &) = delete;
8789

88-
void operator delete(void *Ptr, size_t) = delete;
90+
SILBasicBlock();
91+
SILBasicBlock(SILFunction *parent);
8992

90-
SILBasicBlock(SILFunction *parent) : Parent(parent), PredList(nullptr) { }
93+
void operator=(const SILBasicBlock &) = delete;
94+
void operator delete(void *Ptr, size_t) = delete;
9195

9296
public:
97+
static void registerBridgedMetatype(SwiftMetatype metatype) {
98+
registeredMetatype = metatype;
99+
}
100+
93101
~SILBasicBlock();
94102

95103
/// Gets the ID (= index in the function's block list) of the block.

0 commit comments

Comments
 (0)