Skip to content

Commit

Permalink
Merge pull request #504 from swiftwasm/master
Browse files Browse the repository at this point in the history
[pull] swiftwasm from master
  • Loading branch information
pull[bot] authored Mar 26, 2020
2 parents 84373d3 + 4981da4 commit 882f797
Show file tree
Hide file tree
Showing 67 changed files with 1,213 additions and 104 deletions.
12 changes: 5 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1041,13 +1041,11 @@ if(SWIFT_BUILD_SYNTAXPARSERLIB OR SWIFT_BUILD_SOURCEKIT)
set(SOURCEKIT_RUNTIME_DIR lib)
endif()
add_dependencies(sourcekit-inproc BlocksRuntime dispatch)
if("${SWIFT_HOST_VARIANT_SDK}" MATCHES "OSX|WINDOWS")
swift_install_in_component(FILES
$<TARGET_FILE:dispatch>
$<TARGET_FILE:BlocksRuntime>
DESTINATION ${SOURCEKIT_RUNTIME_DIR}
COMPONENT sourcekit-inproc)
endif()
swift_install_in_component(FILES
$<TARGET_FILE:dispatch>
$<TARGET_FILE:BlocksRuntime>
DESTINATION ${SOURCEKIT_RUNTIME_DIR}
COMPONENT sourcekit-inproc)
if(SWIFT_HOST_VARIANT_SDK STREQUAL WINDOWS)
swift_install_in_component(FILES
$<TARGET_LINKER_FILE:dispatch>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Please make sure you use Python 2.x. Python 3.x is not supported currently.

#### macOS

To build for macOS, you need [Xcode 11.3](https://developer.apple.com/xcode/downloads/).
To build for macOS, you need [Xcode 11.4](https://developer.apple.com/xcode/downloads/).
The required version of Xcode changes frequently, and is often a beta release.
Check this document or the host information on <https://ci.swift.org> for the
current required version.
Expand Down
50 changes: 50 additions & 0 deletions docs/SIL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5811,6 +5811,37 @@ In raw SIL, the ``with_derivative`` clause is optional. In canonical SIL, the
``with_derivative`` clause is mandatory.


linear_function
```````````````
::

sil-instruction ::= 'linear_function'
sil-linear-function-parameter-indices
sil-value ':' sil-type
sil-linear-function-transpose-function-clause?

sil-linear-function-parameter-indices ::=
'[' 'parameters' [0-9]+ (' ' [0-9]+)* ']'
sil-linear-transpose-function-clause ::=
with_transpose sil-value ':' sil-type

linear_function [parameters 0] %0 : $(T) -> T with_transpose %1 : $(T) -> T

Bundles a function with its transpose function into a
``@differentiable(linear)`` function.

``[parameters ...]`` specifies parameter indices that the original function is
linear with respect to.

A ``with_transpose`` clause specifies the transpose function associated
with the original function. When a ``with_transpose`` clause is not specified,
the mandatory differentiation transform will add a ``with_transpose`` clause to
the instruction.

In raw SIL, the ``with_transpose`` clause is optional. In canonical SIL,
the ``with_transpose`` clause is mandatory.


differentiable_function_extract
```````````````````````````````
::
Expand All @@ -5835,6 +5866,25 @@ Extracts the original function or a derivative function from the given
In lowered SIL, an explicit extractee type may be provided. This is currently
used by the LoadableByAddress transformation, which rewrites function types.


linear_function_extract
```````````````````````
::

sil-instruction ::= 'linear_function_extract'
'[' sil-linear-function-extractee ']'
sil-value ':' sil-type

sil-linear-function-extractee ::= 'original' | 'transpose'

linear_function_extract [original] %0 : $@differentiable(linear) (T) -> T
linear_function_extract [transpose] %0 : $@differentiable(linear) (T) -> T

Extracts the original function or a transpose function from the given
``@differentiable(linear)`` function. The extractee is one of the following:
``[original]`` or ``[transpose]``.


differentiability_witness_function
``````````````````````````````````
::
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,9 @@ ERROR(sil_inst_autodiff_operand_list_expected_rbrace,PointsToFirstBadToken,
ERROR(sil_inst_autodiff_expected_differentiable_extractee_kind,PointsToFirstBadToken,
"expected an extractee kind attribute, which can be one of '[original]', "
"'[jvp]', and '[vjp]'", ())
ERROR(sil_inst_autodiff_expected_linear_extractee_kind,PointsToFirstBadToken,
"expected an extractee kind attribute, which can be one of '[original]' "
"and '[transpose]'", ())
ERROR(sil_inst_autodiff_expected_function_type_operand,PointsToFirstBadToken,
"expected an operand of a function type", ())
ERROR(sil_inst_autodiff_expected_differentiability_witness_kind,PointsToFirstBadToken,
Expand Down
41 changes: 33 additions & 8 deletions include/swift/Basic/BlotMapVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,40 @@ class BlotMapVector {
return Vector[Pair.first->second].getValue().second;
}

std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &InsertPair) {
auto Pair = Map.insert(std::make_pair(InsertPair.first, size_t(0)));
if (Pair.second) {
size_t Num = Vector.size();
Pair.first->second = Num;
Vector.push_back(InsertPair);
return std::make_pair(Vector.begin() + Num, true);
template <typename... Ts>
std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&... Args) {
auto Pair = Map.insert(std::make_pair(std::move(Key), size_t(0)));
if (!Pair.second) {
return std::make_pair(Vector.begin() + Pair.first->second, false);
}

size_t Num = Vector.size();
Pair.first->second = Num;
Vector.emplace_back(
std::make_pair(Pair.first->first, ValueT(std::forward<Ts>(Args)...)));
return std::make_pair(Vector.begin() + Num, true);
}

template <typename... Ts>
std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&... Args) {
auto Pair = Map.insert(std::make_pair(std::move(Key), size_t(0)));
if (!Pair.second) {
return std::make_pair(Vector.begin() + Pair.first->second, false);
}
return std::make_pair(Vector.begin() + Pair.first->second, false);

size_t Num = Vector.size();
Pair.first->second = Num;
Vector.emplace_back(
std::make_pair(Pair.first->first, ValueT(std::forward<Ts>(Args)...)));
return std::make_pair(Vector.begin() + Num, true);
}

std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &InsertPair) {
return try_emplace(InsertPair.first, InsertPair.second);
}

std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &InsertPair) {
return try_emplace(InsertPair.first, InsertPair.second);
}

iterator find(const KeyT &Key) {
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class FrontendOptions {
/// Emit index data for imported serialized swift system modules.
bool IndexSystemModules = false;

/// If indexing system modules, don't index the stdlib.
bool IndexIgnoreStdlib = false;

/// The module for which we should verify all of the generic signatures.
std::string VerifyGenericSignaturesInModule;

Expand Down
13 changes: 10 additions & 3 deletions include/swift/Index/IndexRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ namespace index {
/// \param indexSystemModules If true, emit index data for imported serialized
/// swift system modules.
///
/// \param skipStdlib If indexing system modules, don't index the standard
/// library.
///
/// \param isDebugCompilation true for non-optimized compiler invocation.
///
/// \param targetTriple The target for this compilation.
///
/// \param dependencyTracker The set of dependencies seen while building.
bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
StringRef indexStorePath, bool indexSystemModules,
bool isDebugCompilation, StringRef targetTriple,
bool skipStdlib, bool isDebugCompilation,
StringRef targetTriple,
const DependencyTracker &dependencyTracker);

/// Index the given module and store the results to \p indexStorePath.
Expand All @@ -64,15 +68,18 @@ bool indexAndRecord(SourceFile *primarySourceFile, StringRef indexUnitToken,
/// \param indexSystemModules If true, emit index data for imported serialized
/// swift system modules.
///
/// \param skipStdlib If indexing system modules, don't index the standard
/// library.
///
/// \param isDebugCompilation true for non-optimized compiler invocation.
///
/// \param targetTriple The target for this compilation.
///
/// \param dependencyTracker The set of dependencies seen while building.
bool indexAndRecord(ModuleDecl *module, ArrayRef<std::string> indexUnitTokens,
StringRef moduleUnitToken, StringRef indexStorePath,
bool indexSystemModules, bool isDebugCompilation,
StringRef targetTriple,
bool indexSystemModules, bool skipStdlib,
bool isDebugCompilation, StringRef targetTriple,
const DependencyTracker &dependencyTracker);
// FIXME: indexUnitTokens could be StringRef, but that creates an impedance
// mismatch in the caller.
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,10 @@ def external_pass_pipeline_filename : Separate<["-"], "external-pass-pipeline-fi
def index_system_modules : Flag<["-"], "index-system-modules">,
HelpText<"Emit index data for imported serialized swift system modules">;

def index_ignore_stdlib :
Flag<["-"], "index-ignore-stdlib">,
HelpText<"Avoid emitting index data for the standard library.">;

def dump_interface_hash : Flag<["-"], "dump-interface-hash">,
HelpText<"Parse input file(s) and dump interface token hash(es)">,
ModeOpt;
Expand Down
6 changes: 5 additions & 1 deletion include/swift/Reflection/ReflectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,11 @@ class ReflectionContext

/// Return a description of the layout of a value with the given type.
const TypeInfo *getTypeInfo(const TypeRef *TR) {
return getBuilder().getTypeConverter().getTypeInfo(TR);
if (TR == nullptr) {
return nullptr;
} else {
return getBuilder().getTypeConverter().getTypeInfo(TR);
}
}

private:
Expand Down
15 changes: 15 additions & 0 deletions include/swift/SIL/SILBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,14 @@ class SILBuilder {
OriginalFunction, JVPAndVJPFunctions, hasOwnership()));
}

LinearFunctionInst *createLinearFunction(
SILLocation Loc, IndexSubset *ParameterIndices, SILValue OriginalFunction,
Optional<SILValue> TransposeFunction = None) {
return insert(LinearFunctionInst::create(
getModule(), getSILDebugLocation(Loc), ParameterIndices,
OriginalFunction, TransposeFunction, hasOwnership()));
}

/// Note: explicit extractee type may be specified only in lowered SIL.
DifferentiableFunctionExtractInst *createDifferentiableFunctionExtract(
SILLocation Loc, NormalDifferentiableFunctionTypeComponent Extractee,
Expand All @@ -2181,6 +2189,13 @@ class SILBuilder {
ExtracteeType));
}

LinearFunctionExtractInst *createLinearFunctionExtract(
SILLocation Loc, LinearDifferentiableFunctionTypeComponent Extractee,
SILValue TheFunction) {
return insert(new (getModule()) LinearFunctionExtractInst(
getModule(), getSILDebugLocation(Loc), Extractee, TheFunction));
}

/// Note: explicit function type may be specified only in lowered SIL.
DifferentiabilityWitnessFunctionInst *createDifferentiabilityWitnessFunction(
SILLocation Loc, DifferentiabilityWitnessFunctionKind WitnessKind,
Expand Down
22 changes: 22 additions & 0 deletions include/swift/SIL/SILCloner.h
Original file line number Diff line number Diff line change
Expand Up @@ -2841,6 +2841,18 @@ void SILCloner<ImplClass>::visitDifferentiableFunctionInst(
getOpValue(Inst->getOriginalFunction()), derivativeFns));
}

template<typename ImplClass>
void SILCloner<ImplClass>::visitLinearFunctionInst(LinearFunctionInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
auto transpose = Inst->getOptionalTransposeFunction();
if (transpose)
transpose = getOpValue(*transpose);
recordClonedInstruction(
Inst, getBuilder().createLinearFunction(
getOpLocation(Inst->getLoc()), Inst->getParameterIndices(),
getOpValue(Inst->getOriginalFunction()), transpose));
}

template <typename ImplClass>
void SILCloner<ImplClass>::visitDifferentiableFunctionExtractInst(
DifferentiableFunctionExtractInst *Inst) {
Expand All @@ -2854,6 +2866,16 @@ void SILCloner<ImplClass>::visitDifferentiableFunctionExtractInst(
getOpValue(Inst->getOperand()), explicitExtracteeType));
}

template<typename ImplClass>
void SILCloner<ImplClass>::
visitLinearFunctionExtractInst(LinearFunctionExtractInst *Inst) {
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
recordClonedInstruction(
Inst, getBuilder().createLinearFunctionExtract(
getOpLocation(Inst->getLoc()), Inst->getExtractee(),
getOpValue(Inst->getFunctionOperand())));
}

template <typename ImplClass>
void SILCloner<ImplClass>::visitDifferentiabilityWitnessFunctionInst(
DifferentiabilityWitnessFunctionInst *Inst) {
Expand Down
72 changes: 72 additions & 0 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -8057,6 +8057,45 @@ class DifferentiableFunctionInst final
}
};

/// LinearFunctionInst - given a function, its derivative and traspose functions,
/// create an `@differentiable(linear)` function that represents a bundle of these.
class LinearFunctionInst final :
public InstructionBaseWithTrailingOperands<
SILInstructionKind::LinearFunctionInst,
LinearFunctionInst, OwnershipForwardingSingleValueInst> {
private:
friend SILBuilder;
/// Parameters to differentiate with respect to.
IndexSubset *ParameterIndices;
/// Indicates whether a transpose function exists.
bool HasTransposeFunction;

static SILType getLinearFunctionType(
SILValue OriginalFunction, IndexSubset *ParameterIndices);

public:
LinearFunctionInst(SILDebugLocation Loc, IndexSubset *ParameterIndices,
SILValue OriginalFunction,
Optional<SILValue> TransposeFunction, bool HasOwnership);

static LinearFunctionInst *create(SILModule &Module, SILDebugLocation Loc,
IndexSubset *ParameterIndices,
SILValue OriginalFunction,
Optional<SILValue> TransposeFunction,
bool HasOwnership);

IndexSubset *getParameterIndices() const { return ParameterIndices; }
bool hasTransposeFunction() const { return HasTransposeFunction; }
SILValue getOriginalFunction() const { return getOperand(0); }
Optional<SILValue> getOptionalTransposeFunction() const {
return HasTransposeFunction ? Optional<SILValue>(getOperand(1)) : None;
}
SILValue getTransposeFunction() const {
assert(HasTransposeFunction);
return getOperand(1);
}
};

/// DifferentiableFunctionExtractInst - extracts either the original or
/// derivative function value from a `@differentiable` function.
class DifferentiableFunctionExtractInst
Expand Down Expand Up @@ -8094,6 +8133,39 @@ class DifferentiableFunctionExtractInst
bool hasExplicitExtracteeType() const { return HasExplicitExtracteeType; }
};

/// LinearFunctionExtractInst - given an `@differentiable(linear)` function
/// representing a bundle of the original function and the transpose function,
/// extract the specified function.
class LinearFunctionExtractInst
: public InstructionBase<
SILInstructionKind::LinearFunctionExtractInst,
SingleValueInstruction> {
private:
/// The extractee.
LinearDifferentiableFunctionTypeComponent extractee;
/// The list containing the `@differentiable(linear)` function operand.
FixedOperandList<1> operands;

static SILType
getExtracteeType(SILValue function,
LinearDifferentiableFunctionTypeComponent extractee,
SILModule &module);

public:
explicit LinearFunctionExtractInst(
SILModule &module, SILDebugLocation debugLoc,
LinearDifferentiableFunctionTypeComponent extractee,
SILValue theFunction);

LinearDifferentiableFunctionTypeComponent getExtractee() const {
return extractee;
}

SILValue getFunctionOperand() const { return operands[0].get(); }
ArrayRef<Operand> getAllOperands() const { return operands.asArray(); }
MutableArrayRef<Operand> getAllOperands() { return operands.asArray(); }
};

/// DifferentiabilityWitnessFunctionInst - Looks up a differentiability witness
/// function for a given original function.
class DifferentiabilityWitnessFunctionInst
Expand Down
5 changes: 5 additions & 0 deletions include/swift/SIL/SILNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,14 @@ ABSTRACT_VALUE_AND_INST(SingleValueInstruction, ValueBase, SILInstruction)
// Differentiable programming
SINGLE_VALUE_INST(DifferentiableFunctionInst, differentiable_function,
SingleValueInstruction, None, DoesNotRelease)
SINGLE_VALUE_INST(LinearFunctionInst, linear_function,
SingleValueInstruction, None, DoesNotRelease)
SINGLE_VALUE_INST(DifferentiableFunctionExtractInst,
differentiable_function_extract,
SingleValueInstruction, None, DoesNotRelease)
SINGLE_VALUE_INST(LinearFunctionExtractInst,
linear_function_extract,
SingleValueInstruction, None, DoesNotRelease)
SINGLE_VALUE_INST(DifferentiabilityWitnessFunctionInst,
differentiability_witness_function,
SingleValueInstruction, None, DoesNotRelease)
Expand Down
Loading

0 comments on commit 882f797

Please sign in to comment.