Skip to content

Commit 4c367ee

Browse files
authored
Merge pull request #4116 from swiftwasm/main
[pull] swiftwasm from main
2 parents 2662e9e + be39f38 commit 4c367ee

19 files changed

+293
-39
lines changed

CMakeLists.txt

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

195-
# NOTE: We do not currently support building the swift compiler modules with the Xcode generator.
196-
cmake_dependent_option(BOOTSTRAPPING_MODE [=[
195+
option(BOOTSTRAPPING_MODE [=[
197196
How to build the swift compiler modules. Possible values are
198197
OFF: build without swift modules
199198
HOSTTOOLS: build with a pre-installed toolchain
@@ -204,7 +203,7 @@ How to build the swift compiler modules. Possible values are
204203
`SWIFT_NATIVE_SWIFT_TOOLS_PATH` (non-Darwin only)
205204
CROSSCOMPILE-WITH-HOSTLIBS: build with a bootstrapping-with-hostlibs compiled
206205
compiler, provided in `SWIFT_NATIVE_SWIFT_TOOLS_PATH`
207-
]=] OFF "NOT XCODE" OFF)
206+
]=] OFF)
208207

209208
# The following only works with the Ninja generator in CMake >= 3.0.
210209
set(SWIFT_PARALLEL_LINK_JOBS "" CACHE STRING
@@ -1115,8 +1114,6 @@ if(SWIFT_INCLUDE_TOOLS)
11151114
# SwiftCompilerSources must come before "tools".
11161115
# It adds swift module names to the global property "swift_compiler_modules"
11171116
# which is used in add_swift_host_tool for the lldb workaround.
1118-
#
1119-
# NOTE: We do not currently support SwiftCompilerSources with the Xcode generator.
11201117
add_subdirectory(SwiftCompilerSources)
11211118

11221119
# Always include this after including stdlib/!

SwiftCompilerSources/CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ function(add_swift_compiler_modules_library name)
8989
set(sdk_option "")
9090

9191
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
92-
set(deployment_version "10.15") # TODO: once #38675 lands, replace this with
93-
# set(deployment_version "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
92+
set(deployment_version "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
9493
set(sdk_option "-sdk" "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}")
9594
if(${BOOTSTRAPPING_MODE} STREQUAL "CROSSCOMPILE-WITH-HOSTLIBS")
9695
# Let the cross-compiled compile don't pick up the compiled stdlib by providing
@@ -158,6 +157,15 @@ function(add_swift_compiler_modules_library name)
158157
add_dependencies(${name} ${all_module_targets})
159158
set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX)
160159
set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${name})
160+
161+
# Xcode does not compile libraries that contain only object files.
162+
# Therefore, it fails to create the static library. As a workaround,
163+
# we add a dummy script phase to the target.
164+
if (XCODE)
165+
add_custom_command(TARGET ${name} POST_BUILD
166+
COMMAND ""
167+
COMMENT "Dummy script phase to force building this target")
168+
endif()
161169
endfunction()
162170

163171

docs/DifferentiableProgrammingImplementation.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ This document explains how differentiation is implemented in the Swift compiler,
3434

3535
```swift
3636
// From the standard library:
37-
// func gradient<T, R>(at x: T, in f: @differentiable (T) -> R) -> T.TangentVector
37+
// func gradient<T, R>(at x: T, of f: @differentiable (T) -> R) -> T.TangentVector
3838
// where R: FloatingPoint, R.TangentVector == R
3939

4040
// 1. What does the `@differentiable` attribute do?
@@ -51,7 +51,7 @@ func cubed(_ x: Float) -> Float {
5151
// transform generates derivative functions for the closure expression. The
5252
// `gradient` higher-order function extracts and applies a derivative function to
5353
// evaluate a gradient value.
54-
gradient(at: 4, in: { x in x * x * x }) // 48.0
54+
gradient(at: 4, of: { x in x * x * x }) // 48.0
5555
```
5656

5757
> NOTE:
@@ -397,13 +397,13 @@ func cubed(_ x: Float) -> Float {
397397
// %cubed = differentiable_function [wrt 0] %closure_ref
398398
// with {%closure_jvp_ref, %closure_vjp_ref}
399399

400-
gradient(at: Float(4), in: { x in x * x * x })
400+
gradient(at: Float(4), of: { x in x * x * x })
401401

402402
// Swift supports implicit function conversions, which happens above.
403403
// Below is what the conversion looks like explicitly:
404404
// let foo: (Float) -> Float = { x in x * x * x }
405405
// let cubed: @differentiable (Float) -> Float = foo
406-
// gradient(at: Float(4), in: cubed)
406+
// gradient(at: Float(4), of: cubed)
407407
```
408408

409409
> TODO: Update the \[differentiable\] attribute directly above the Swift declaration of `cubed(_:)` with a SIL differentiability witness.

lib/IRGen/GenClangDecl.cpp

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class ClangDeclFinder
4040
isa<clang::VarDecl>(DRE->getDecl())) {
4141
callback(DRE->getDecl());
4242
}
43+
4344
return true;
4445
}
4546

@@ -56,6 +57,17 @@ class ClangDeclFinder
5657
callback(CXXCE->getConstructor());
5758
return true;
5859
}
60+
61+
bool VisitVarDecl(clang::VarDecl *VD) {
62+
if (auto cxxRecord = VD->getType()->getAsCXXRecordDecl())
63+
if (auto dtor = cxxRecord->getDestructor())
64+
callback(dtor);
65+
66+
return true;
67+
}
68+
69+
bool shouldVisitTemplateInstantiations() const { return true; }
70+
bool shouldVisitImplicitCode() const { return true; }
5971
};
6072

6173
// If any (re)declaration of `decl` contains executable code, returns that
@@ -65,6 +77,11 @@ class ClangDeclFinder
6577
// the initializer of the variable.
6678
clang::Decl *getDeclWithExecutableCode(clang::Decl *decl) {
6779
if (auto fd = dyn_cast<clang::FunctionDecl>(decl)) {
80+
// If this is a potentially not-yet-instanciated template, we might
81+
// still have a body.
82+
if (fd->getTemplateInstantiationPattern())
83+
return fd;
84+
6885
const clang::FunctionDecl *definition;
6986
if (fd->hasBody(definition)) {
7087
return const_cast<clang::FunctionDecl *>(definition);
@@ -100,7 +117,7 @@ void IRGenModule::emitClangDecl(const clang::Decl *decl) {
100117
SmallVector<const clang::Decl *, 8> stack;
101118
stack.push_back(decl);
102119

103-
ClangDeclFinder refFinder([&](const clang::Decl *D) {
120+
auto callback = [&](const clang::Decl *D) {
104121
for (auto *DC = D->getDeclContext();; DC = DC->getParent()) {
105122
// Check that this is not a local declaration inside a function.
106123
if (DC->isFunctionOrMethod()) {
@@ -117,31 +134,55 @@ void IRGenModule::emitClangDecl(const clang::Decl *decl) {
117134
if (!GlobalClangDecls.insert(D->getCanonicalDecl()).second) {
118135
return;
119136
}
137+
120138
stack.push_back(D);
121-
});
139+
};
140+
141+
ClangDeclFinder refFinder(callback);
122142

123143
while (!stack.empty()) {
124144
auto *next = const_cast<clang::Decl *>(stack.pop_back_val());
145+
146+
// If a function calls another method in a class template specialization, we
147+
// need to instantiate that other function. Do that here.
148+
if (auto *fn = dyn_cast<clang::FunctionDecl>(next)) {
149+
// Make sure that this method is part of a class template specialization.
150+
if (fn->getTemplateInstantiationPattern())
151+
Context.getClangModuleLoader()
152+
->getClangSema()
153+
.InstantiateFunctionDefinition(fn->getLocation(), fn);
154+
}
155+
125156
if (clang::Decl *executableDecl = getDeclWithExecutableCode(next)) {
126157
refFinder.TraverseDecl(executableDecl);
127158
next = executableDecl;
128159
}
129160

161+
// Unfortunately, implicitly defined CXXDestructorDecls don't have a real
162+
// body, so we need to traverse these manually.
163+
if (auto *dtor = dyn_cast<clang::CXXDestructorDecl>(next)) {
164+
auto cxxRecord = dtor->getParent();
165+
166+
for (auto field : cxxRecord->fields()) {
167+
if (auto fieldCxxRecord = field->getType()->getAsCXXRecordDecl())
168+
if (auto *fieldDtor = fieldCxxRecord->getDestructor())
169+
callback(fieldDtor);
170+
}
171+
172+
for (auto base : cxxRecord->bases()) {
173+
if (auto baseCxxRecord = base.getType()->getAsCXXRecordDecl())
174+
if (auto *baseDtor = baseCxxRecord->getDestructor())
175+
callback(baseDtor);
176+
}
177+
}
178+
130179
if (auto var = dyn_cast<clang::VarDecl>(next))
131180
if (!var->isFileVarDecl())
132181
continue;
133182
if (isa<clang::FieldDecl>(next)) {
134183
continue;
135184
}
136-
// If a method calls another method in a class template specialization, we
137-
// need to instantiate that other method. Do that here.
138-
if (auto *method = dyn_cast<clang::CXXMethodDecl>(next)) {
139-
// Make sure that this method is part of a class template specialization.
140-
if (method->getTemplateInstantiationPattern())
141-
Context.getClangModuleLoader()
142-
->getClangSema()
143-
.InstantiateFunctionDefinition(method->getLocation(), method);
144-
}
185+
145186
ClangCodeGen->HandleTopLevelDecl(clang::DeclGroupRef(next));
146187
}
147188
}

lib/IRGen/GenDecl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,10 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
468468

469469
if (ObjCInterop)
470470
this->addLinkLibrary(LinkLibrary("objc", LibraryKind::Library));
471+
472+
// Automatically with libc++ when possible.
473+
if (Context.LangOpts.EnableCXXInterop && Context.LangOpts.Target.isOSDarwin())
474+
this->addLinkLibrary(LinkLibrary("c++", LibraryKind::Library));
471475

472476
// FIXME: It'd be better to have the driver invocation or build system that
473477
// executes the linker introduce these compatibility libraries, since at

lib/IRGen/GenStruct.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,7 @@ static clang::CXXDestructorDecl *getCXXDestructor(SILType type) {
8181
dyn_cast<clang::CXXRecordDecl>(structDecl->getClangDecl());
8282
if (!cxxRecordDecl)
8383
return nullptr;
84-
for (auto member : cxxRecordDecl->methods()) {
85-
if (auto dest = dyn_cast<clang::CXXDestructorDecl>(member)) {
86-
return dest;
87-
}
88-
}
89-
return nullptr;
84+
return cxxRecordDecl->getDestructor();
9085
}
9186

9287
namespace {

lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2888,6 +2888,9 @@ bool swift::optimizeMemoryAccesses(SILFunction &fn) {
28882888
}
28892889

28902890
bool swift::eliminateDeadAllocations(SILFunction &fn) {
2891+
if (!fn.hasOwnership())
2892+
return false;
2893+
28912894
bool changed = false;
28922895
DeadEndBlocks deadEndBlocks(&fn);
28932896

lib/SILOptimizer/Transforms/GenericSpecializer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ optimizeInst(SILInstruction *inst, SILOptFunctionBuilder &funcBuilder,
253253
if (!callee || callee->isTransparent() == IsNotTransparent)
254254
return true;
255255

256+
if (callee->isExternalDeclaration())
257+
getModule()->loadFunction(callee);
258+
259+
if (callee->isExternalDeclaration())
260+
return true;
261+
256262
// If the de-virtualized callee is a transparent function, inline it.
257263
SILInliner::inlineFullApply(newFAS, SILInliner::InlineKind::MandatoryInline,
258264
funcBuilder, deleter);
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
// RUN: %target-swift-emit-sil %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s
2-
//
3-
// We can't yet call member functions correctly on Windows (SR-13129).
4-
// XFAIL: OS=windows-msvc
1+
// RUN: %target-swift-emit-sil %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s -check-prefix CHECK-%target-abi
52

63
import MemberOutOfLine
74

85
public func add(_ lhs: LoadableIntWrapper, _ rhs: LoadableIntWrapper) -> LoadableIntWrapper { lhs + rhs }
96

10-
// CHECK: bb0([[LHS:%.*]] : $LoadableIntWrapper, [[RHS:%.*]] : $LoadableIntWrapper):
11-
// CHECK: [[FUNC:%.*]] = function_ref [[NAME:@_ZNK18LoadableIntWrapperplES_]] : $@convention(c) (@in LoadableIntWrapper, LoadableIntWrapper) -> LoadableIntWrapper
12-
// CHECK: apply [[FUNC]]([[ACCESS:%.*]], [[RHS]]) : $@convention(c) (@in LoadableIntWrapper, LoadableIntWrapper) -> LoadableIntWrapper
7+
// CHECK-SYSV: bb0([[LHS:%.*]] : $LoadableIntWrapper, [[RHS:%.*]] : $LoadableIntWrapper):
8+
// CHECK-SYSV: [[FUNC:%.*]] = function_ref [[NAME:@_ZNK18LoadableIntWrapperplES_]] : $@convention(c) (@in LoadableIntWrapper, LoadableIntWrapper) -> LoadableIntWrapper
9+
// CHECK-SYSV: apply [[FUNC]]([[ACCESS:%.*]], [[RHS]]) : $@convention(c) (@in LoadableIntWrapper, LoadableIntWrapper) -> LoadableIntWrapper
1310

14-
// CHECK: sil [clang LoadableIntWrapper."+"] [[NAME]] : $@convention(c) (@in LoadableIntWrapper, LoadableIntWrapper) -> LoadableIntWrapper
11+
// CHECK-SYSV: sil [clang LoadableIntWrapper."+"] [[NAME]] : $@convention(c) (@in LoadableIntWrapper, LoadableIntWrapper) -> LoadableIntWrapper
12+
13+
// CHECK-WIN: bb0([[LHS:%.*]] : $LoadableIntWrapper, [[RHS:%.*]] : $LoadableIntWrapper):
14+
// CHECK-WIN: [[FUNC:%.*]] = function_ref [[NAME:@\?\?HLoadableIntWrapper@@QEBA\?AU0@U0@@Z]] : $@convention(c) (@in LoadableIntWrapper, LoadableIntWrapper) -> LoadableIntWrapper
15+
// CHECK-WIN: apply [[FUNC]]([[ACCESS:%.*]], [[RHS]]) : $@convention(c) (@in LoadableIntWrapper, LoadableIntWrapper) -> LoadableIntWrapper
16+
17+
// CHECK-WIN: sil [clang LoadableIntWrapper."+"] [[NAME]] : $@convention(c) (@in LoadableIntWrapper, LoadableIntWrapper) -> LoadableIntWrapper
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module StdVector {
2+
header "std-vector.h"
3+
}

0 commit comments

Comments
 (0)