Skip to content

Commit

Permalink
Merge pull request #927 from swiftwasm/master
Browse files Browse the repository at this point in the history
[pull] swiftwasm from master
  • Loading branch information
pull[bot] authored May 8, 2020
2 parents 8641689 + 7e6d62d commit 868f858
Show file tree
Hide file tree
Showing 70 changed files with 882 additions and 525 deletions.
13 changes: 13 additions & 0 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ include(SwiftWindowsSupport)
include(SwiftAndroidSupport)

function(_swift_gyb_target_sources target scope)
file(GLOB GYB_UNICODE_DATA ${SWIFT_SOURCE_DIR}/utils/UnicodeData/*)
file(GLOB GYB_STDLIB_SUPPORT ${SWIFT_SOURCE_DIR}/utils/gyb_stdlib_support.py)
file(GLOB GYB_SYNTAX_SUPPORT ${SWIFT_SOURCE_DIR}/utils/gyb_syntax_support/*)
file(GLOB GYB_SOURCEKIT_SUPPORT ${SWIFT_SOURCE_DIR}/utils/gyb_sourcekit_support/*)
set(GYB_SOURCES
${SWIFT_SOURCE_DIR}/utils/GYBUnicodeDataUtils.py
${SWIFT_SOURCE_DIR}/utils/SwiftIntTypes.py
${GYB_UNICODE_DATA}
${GYB_STDLIB_SUPPORT}
${GYB_SYNTAX_SUPPORT}
${GYB_SOURCEKIT_SUPPORT})

foreach(source ${ARGN})
get_filename_component(generated ${source} NAME_WLE)
get_filename_component(absolute ${source} REALPATH)
Expand All @@ -17,6 +29,7 @@ function(_swift_gyb_target_sources target scope)
COMMAND
${CMAKE_COMMAND} -E remove ${CMAKE_CURRENT_BINARY_DIR}/${generated}.tmp
DEPENDS
${GYB_SOURCES}
${absolute})
set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${generated} PROPERTIES
GENERATED TRUE)
Expand Down
303 changes: 160 additions & 143 deletions docs/DebuggingTheCompiler.md

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions docs/OptimizationTips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,30 +283,37 @@ through the usage of ``inout`` parameters:
var a = [1, 2, 3]
append_one_in_place(&a)

Unchecked operations
Wrapping operations
====================

Swift eliminates integer overflow bugs by checking for overflow when performing
normal arithmetic. These checks are not appropriate in high performance code
where one knows that no memory safety issues can result.
normal arithmetic. These checks may not be appropriate in high performance code
if one either knows that overflow cannot occur, or that the result of
allowing the operation to wrap around is correct.

Advice: Use unchecked integer arithmetic when you can prove that overflow cannot occur
Advice: Use wrapping integer arithmetic when you can prove that overflow cannot occur
---------------------------------------------------------------------------------------

In performance-critical code you can elide overflow checks if you know it is
safe.
In performance-critical code you can use wrapping arithmetic to avoid overflow
checks if you know it is safe.

::

a: [Int]
b: [Int]
c: [Int]

// Precondition: for all a[i], b[i]: a[i] + b[i] does not overflow!
// Precondition: for all a[i], b[i]: a[i] + b[i] either does not overflow,
// or the result of wrapping is desired.
for i in 0 ... n {
c[i] = a[i] &+ b[i]
}

It's important to note that the behavior of the ``&+``, ``&-``, and ``&*``
operators is fully-defined; the result simply wraps around if it would overflow.
Thus, ``Int.max &+ 1`` is guaranteed to be ``Int.min`` (unlike in C, where
``INT_MAX + 1`` is undefined behavior).

Generics
========

Expand Down
4 changes: 4 additions & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,10 @@ class ASTContext final {

/// Retrieve the IRGen specific SIL passes.
SILTransformCtors getIRGenSILTransforms() const;

/// Check whether a given string would be considered "pure ASCII" by the
/// standard library's String implementation.
bool isASCIIString(StringRef s) const;

private:
friend Decl;
Expand Down
13 changes: 13 additions & 0 deletions include/swift/AST/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2209,6 +2209,19 @@ class DeclAttributes {
make_range(begin(), end()), ToAttributeKind<ATTR, AllowInvalid>());
}

/// Return the range of semantics attributes attached to this attribute set.
auto getSemanticsAttrs() const
-> decltype(getAttributes<SemanticsAttr>()) {
return getAttributes<SemanticsAttr>();
}

/// Return whether this attribute set includes the given semantics attribute.
bool hasSemanticsAttr(StringRef attrValue) const {
return llvm::any_of(getSemanticsAttrs(), [&](const SemanticsAttr *attr) {
return attrValue.equals(attr->Value);
});
}

// Remove the given attribute from the list of attributes. Used when
// the attribute was semantically invalid.
void removeAttribute(const DeclAttribute *attr) {
Expand Down
9 changes: 4 additions & 5 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3524,14 +3524,12 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {

/// Return the range of semantics attributes attached to this NominalTypeDecl.
auto getSemanticsAttrs() const
-> decltype(getAttrs().getAttributes<SemanticsAttr>()) {
return getAttrs().getAttributes<SemanticsAttr>();
-> decltype(getAttrs().getSemanticsAttrs()) {
return getAttrs().getSemanticsAttrs();
}

bool hasSemanticsAttr(StringRef attrValue) const {
return llvm::any_of(getSemanticsAttrs(), [&](const SemanticsAttr *attr) {
return attrValue.equals(attr->Value);
});
return getAttrs().hasSemanticsAttr(attrValue);
}

/// Whether this declaration has a synthesized memberwise initializer.
Expand Down Expand Up @@ -7187,6 +7185,7 @@ class OperatorDecl : public Decl {
case DeclKind::PostfixOperator:
return OperatorFixity::Postfix;
}
llvm_unreachable("inavlid decl kind");
}

SourceLoc getOperatorLoc() const { return OperatorLoc; }
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/EvaluatorDependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ inline DependencyScope getScopeForAccessLevel(AccessLevel l) {
case AccessLevel::Open:
return DependencyScope::Cascading;
}
llvm_unreachable("invalid access level kind");
}

// A \c DependencySource is currently defined to be a parent source file and
Expand Down Expand Up @@ -258,6 +259,7 @@ struct DependencyCollector {
case Mode::ExperimentalPrivateDependencies:
return false;
}
llvm_unreachable("invalid mode");
}
};
} // end namespace evaluator
Expand Down
29 changes: 28 additions & 1 deletion include/swift/AST/SILGenRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void reportEvaluatedRequest(UnifiedStatsReporter &stats,
const Request &request);

struct SILGenDescriptor {
llvm::PointerUnion<ModuleDecl *, FileUnit *> context;
llvm::PointerUnion<FileUnit *, ModuleDecl *> context;
Lowering::TypeConverter &conv;
const SILOptions &opts;

Expand Down Expand Up @@ -73,6 +73,17 @@ struct SILGenDescriptor {
const SILOptions &opts) {
return SILGenDescriptor{mod, conv, opts};
}

/// For a single file input, returns a single element array containing that
/// file. Otherwise returns an array of each file in the module.
ArrayRef<FileUnit *> getFiles() const;

/// If the module or file contains SIL that needs parsing, returns the file
/// to be parsed, or \c nullptr if parsing isn't required.
SourceFile *getSourceFileToParse() const;

/// Whether the SIL is being emitted for a whole module.
bool isWholeModule() const;
};

void simple_display(llvm::raw_ostream &out, const SILGenDescriptor &d);
Expand Down Expand Up @@ -120,6 +131,22 @@ class SILGenWholeModuleRequest :
bool isCached() const { return true; }
};

/// Parses a .sil file into a SILModule.
class ParseSILModuleRequest
: public SimpleRequest<ParseSILModuleRequest,
std::unique_ptr<SILModule>(SILGenDescriptor),
RequestFlags::Uncached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
std::unique_ptr<SILModule>
evaluate(Evaluator &evaluator, SILGenDescriptor desc) const;
};

/// The zone number for SILGen.
#define SWIFT_TYPEID_ZONE SILGen
#define SWIFT_TYPEID_HEADER "swift/AST/SILGenTypeIDZone.def"
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/SILGenTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ SWIFT_REQUEST(SILGen, SILGenSourceFileRequest,
SWIFT_REQUEST(SILGen, SILGenWholeModuleRequest,
std::unique_ptr<SILModule>(SILGenDescriptor),
Uncached, NoLocationInfo)
SWIFT_REQUEST(SILGen, ParseSILModuleRequest,
std::unique_ptr<SILModule>(SILGenDescriptor),
Uncached, NoLocationInfo)
2 changes: 2 additions & 0 deletions include/swift/AST/SemanticAttrs.def
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,7 @@ SEMANTICS_ATTR(SLOWPATH, "slowpath")
SEMANTICS_ATTR(PROGRAMTERMINATION_POINT, "programtermination_point")
SEMANTICS_ATTR(CONVERT_TO_OBJECTIVE_C, "convertToObjectiveC")

SEMANTICS_ATTR(KEYPATH_KVC_KEY_PATH_STRING, "keypath.kvcKeyPathString")

#undef SEMANTICS_ATTR

1 change: 1 addition & 0 deletions include/swift/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,7 @@ class CaseStmt final
return SourceRange(getLoc(), getLoc());
}
}
llvm_unreachable("invalid parent kind");
}

bool isDefault() { return getCaseLabelItems()[0].isDefault(); }
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Basic/FrozenMultiMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ class FrozenMultiMap {

/// Return a range of (key, ArrayRef<Value>) pairs. The keys are guaranteed to
/// be in key sorted order and the ArrayRef<Value> are in insertion order.
///
/// The range skips all erased (key, ArrayRef<Value>) entries.
RangeType getRange() const {
assert(isFrozen() &&
"Can not create range until data structure is frozen?!");
Expand Down
16 changes: 0 additions & 16 deletions include/swift/Frontend/Frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ class CompilerInstance {
DiagnosticEngine Diagnostics{SourceMgr};
std::unique_ptr<ASTContext> Context;
std::unique_ptr<Lowering::TypeConverter> TheSILTypes;
std::unique_ptr<SILModule> TheSILModule;
std::unique_ptr<DiagnosticVerifier> DiagVerifier;

/// Null if no tracker.
Expand Down Expand Up @@ -498,8 +497,6 @@ class CompilerInstance {

Lowering::TypeConverter &getSILTypes();

void createSILModule();

void addDiagnosticConsumer(DiagnosticConsumer *DC) {
Diagnostics.addConsumer(*DC);
}
Expand All @@ -517,16 +514,6 @@ class CompilerInstance {

UnifiedStatsReporter *getStatsReporter() const { return Stats.get(); }

SILModule *getSILModule() {
return TheSILModule.get();
}

std::unique_ptr<SILModule> takeSILModule();

bool hasSILModule() {
return static_cast<bool>(TheSILModule);
}

ModuleDecl *getMainModule() const;

MemoryBufferSerializedModuleLoader *
Expand Down Expand Up @@ -659,9 +646,6 @@ class CompilerInstance {
public:
void freeASTContext();

/// Frees up the SILModule that this instance is holding on to.
void freeSILModule();

private:
/// Load stdlib & return true if should continue, i.e. no error
bool loadStdlib();
Expand Down
3 changes: 2 additions & 1 deletion include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,8 @@ class Parser {
void parseTopLevel(SmallVectorImpl<Decl *> &decls);

/// Parse the top-level SIL decls into the SIL module.
void parseTopLevelSIL();
/// \returns \c true if there was a parsing error.
bool parseTopLevelSIL();

/// Flags that control the parsing of declarations.
enum ParseDeclFlags {
Expand Down
6 changes: 6 additions & 0 deletions include/swift/SILOptimizer/Utils/KeyPathProjector.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

namespace swift {

class KeyPathInst;

/// Projects a statically known key path expression to
/// a direct property access.
class KeyPathProjector {
Expand Down Expand Up @@ -53,6 +55,10 @@ class KeyPathProjector {
static std::unique_ptr<KeyPathProjector>
create(SILValue keyPath, SILValue root, SILLocation loc, SILBuilder &builder);

/// Extract the literal KeyPathInst underlying a value, or return null if there is none.
static KeyPathInst *
getLiteralKeyPath(SILValue keyPath);

/// Projects the key path to an address. Sets up the projection,
/// invokes the callback, then tears down the projection.
/// \param accessType The access type of the projected address.
Expand Down
3 changes: 0 additions & 3 deletions include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ namespace swift {

/// @}

/// Parse a source file's SIL declarations into a given SIL module.
void parseSourceFileSIL(SourceFile &SF, SILParserState *sil);

/// Finish the code completion.
void performCodeCompletionSecondPass(SourceFile &SF,
CodeCompletionCallbacksFactory &Factory);
Expand Down
8 changes: 8 additions & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4827,3 +4827,11 @@ llvm::LLVMContext &ASTContext::getIntrinsicScratchContext() const {
#endif
}

bool ASTContext::isASCIIString(StringRef s) const {
for (unsigned char c : s) {
if (c > 127) {
return false;
}
}
return true;
}
1 change: 1 addition & 0 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ getParameterDifferentiability(ImplParameterDifferentiability diffKind) {
case ImplParameterDifferentiability::NotDifferentiable:
return SILParameterDifferentiability::NotDifferentiable;
}
llvm_unreachable("unknown differentiability kind");
}

static ResultConvention getResultConvention(ImplResultConvention conv) {
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/AutoDiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ NormalDifferentiableFunctionTypeComponent::getAsDerivativeFunctionKind() const {
case VJP:
return {AutoDiffDerivativeFunctionKind::VJP};
}
llvm_unreachable("invalid derivative kind");
}

LinearDifferentiableFunctionTypeComponent::
Expand Down Expand Up @@ -93,6 +94,7 @@ DifferentiabilityWitnessFunctionKind::getAsDerivativeFunctionKind() const {
case Transpose:
return None;
}
llvm_unreachable("invalid derivative kind");
}

void SILAutoDiffIndices::print(llvm::raw_ostream &s) const {
Expand Down Expand Up @@ -375,6 +377,7 @@ Type TangentSpace::getType() const {
case Kind::Tuple:
return value.tupleType;
}
llvm_unreachable("invalid tangent space kind");
}

CanType TangentSpace::getCanonicalType() const {
Expand Down
1 change: 1 addition & 0 deletions lib/AST/ClangTypeConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ const clang::Type *ClangTypeConverter::getFunctionType(
case AnyFunctionType::Representation::Thin:
llvm_unreachable("Expected a C-compatible representation.");
}
llvm_unreachable("invalid representation");
}

clang::QualType ClangTypeConverter::convertMemberType(NominalTypeDecl *DC,
Expand Down
2 changes: 2 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ static_assert(sizeof(checkSourceLocType(&ID##Decl::getLoc)) == 2, \
case FileUnitKind::DWARFModule:
return SourceLoc();
}
llvm_unreachable("invalid file kind");
}

Expr *AbstractFunctionDecl::getSingleExpressionBody() const {
Expand Down Expand Up @@ -6297,6 +6298,7 @@ bool ParamDecl::hasCallerSideDefaultExpr() const {
case DefaultArgumentKind::EmptyDictionary:
return true;
}
llvm_unreachable("invalid default argument kind");
}

Expr *ParamDecl::getTypeCheckedDefaultExpr() const {
Expand Down
2 changes: 2 additions & 0 deletions lib/AST/ModuleDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ ModuleDependenciesCache::getDependenciesMap(ModuleDependenciesKind kind) {
case ModuleDependenciesKind::Clang:
return ClangModuleDependencies;
}
llvm_unreachable("invalid dependency kind");
}

const llvm::StringMap<ModuleDependencies> &
Expand All @@ -117,6 +118,7 @@ ModuleDependenciesCache::getDependenciesMap(ModuleDependenciesKind kind) const {
case ModuleDependenciesKind::Clang:
return ClangModuleDependencies;
}
llvm_unreachable("invalid dependency kind");
}

bool ModuleDependenciesCache::hasDependencies(
Expand Down
Loading

0 comments on commit 868f858

Please sign in to comment.