Skip to content

[pull] swiftwasm from main #3601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3f01f85
[Demangling] Add error handling to the remangler.
al45tair Sep 1, 2021
fc3322f
[Demangling] More remangler error handling.
al45tair Sep 3, 2021
145f281
[Demangling][Tests] Fix tests after remangler initial error handling.
al45tair Sep 3, 2021
fbf082b
[Demangling] Make Demangle::getUnspecialized() return errors too.
al45tair Sep 3, 2021
cc869b9
[Demangling] Remangling error handling for the OldRemangler.
al45tair Sep 6, 2021
227b438
[Demangling] Added DEMANGLER_ASSERT to replace assert() calls.
al45tair Sep 6, 2021
b8a8799
[Demangler] Tidy up a bit, and add line numbers to ManglingErrors.
al45tair Sep 6, 2021
9013083
[Demangler] Display error codes in various places.
al45tair Sep 6, 2021
22f8e13
[Demangling] Include the necessary header for SWIFT_NODISCARD.
al45tair Sep 7, 2021
59cfe35
[Demangling] Improve DEMANGLE_ASSERT, enable some disabled tests.
al45tair Sep 7, 2021
bd31af0
[Demangling] Use basic syntax regex for sed.
al45tair Sep 9, 2021
af13791
[Tests][Demangling] Remove the sed.
al45tair Sep 9, 2021
11f2650
[SIL] Renamed flag for viewing canonical cfg.
nate-chandler Sep 22, 2021
4ff34aa
[SIL] Enabled printing canonical module.
nate-chandler Sep 22, 2021
5dae683
[swift-api-extract] Handle @_spi interfaces with correct APIAccess
cachemeifyoucan Sep 24, 2021
8be084f
Merge pull request #39187 from al45tair/problem/79725187
al45tair Sep 27, 2021
c119e4d
Merge pull request #39448 from cachemeifyoucan/eng/PR-83506338
cachemeifyoucan Sep 27, 2021
b1236e9
swift_build_support: Add WASI to StdlibDeploymentTarget (#39244)
MaxDesiatov Sep 27, 2021
7c5b160
[nfc][cxx-interop] Fix regex in reference irgen test.
zoecarver Sep 27, 2021
293e47a
Handle missing Sendable conformances in SILFunctionType substitutions.
DougGregor Sep 27, 2021
87cc490
Allow identification of OSLogMessage by a @_semantics attribute inste…
kubamracek Sep 27, 2021
28daeae
Merge pull request #39393 from nate-chandler/sil/print-guaranteed-module
nate-chandler Sep 27, 2021
f4aa626
arm64 macOS was introduced with version Swift
DougGregor Sep 27, 2021
7ad0c3c
RequirementMachine: Add a mode allowing unresolved name symbols in re…
slavapestov Sep 27, 2021
2923a33
RequirementMachine: Remove unused method
slavapestov Sep 25, 2021
5f3d781
RequirementMachine: Homotopy reduction tweaks
slavapestov Sep 25, 2021
64d1931
RequirementMachine: Sketch out "generating conformances" algorithm
slavapestov Sep 27, 2021
735e262
Merge pull request #39469 from DougGregor/missing-conformances-silfun…
swift-ci Sep 27, 2021
6f70afe
Merge pull request #39470 from DougGregor/arm64-macos-runtime-floor
swift-ci Sep 27, 2021
dd5e254
Merge pull request #39467 from zoecarver/fix-no-assert-bots
zoecarver Sep 28, 2021
b654bf0
Merge pull request #39474 from slavapestov/rqm-generating-conformances
slavapestov Sep 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/AST/SemanticAttrs.def
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_GENERIC_SIZE_NEVER,
SEMANTICS_ATTR(OPTIMIZE_SIL_SPECIALIZE_OWNED2GUARANTEE_NEVER,
"optimize.sil.specialize.owned2guarantee.never")

SEMANTICS_ATTR(OSLOG_MESSAGE_TYPE, "oslog.message.type")
SEMANTICS_ATTR(OSLOG_MESSAGE_INIT_INTERPOLATION, "oslog.message.init_interpolation")
SEMANTICS_ATTR(OSLOG_MESSAGE_INIT_STRING_LITERAL, "oslog.message.init_stringliteral")
SEMANTICS_ATTR(OSLOG_REQUIRES_CONSTANT_ARGUMENTS, "oslog.requires_constant_arguments")
Expand Down
96 changes: 84 additions & 12 deletions include/swift/Demangling/Demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
#ifndef SWIFT_DEMANGLING_DEMANGLE_H
#define SWIFT_DEMANGLING_DEMANGLE_H

#include <memory>
#include <string>
#include "swift/Demangling/NamespaceMacros.h"
#include "swift/Runtime/Config.h"
#include "llvm/ADT/StringRef.h"
#include <cassert>
#include <cstdint>
#include "llvm/ADT/StringRef.h"
#include "swift/Demangling/NamespaceMacros.h"
#include <memory>
#include <string>

namespace llvm {
class raw_ostream;
Expand Down Expand Up @@ -512,42 +513,113 @@ enum class OperatorKind {
Infix,
};

/// A mangling error, which consists of an error code and a Node pointer
struct SWIFT_NODISCARD ManglingError {
enum Code {
Success = 0,
AssertionFailed,
Uninitialized,
TooComplex,
BadNodeKind,
BadNominalTypeKind,
NotAStorageNode,
UnsupportedNodeKind,
UnexpectedBuiltinVectorType,
UnexpectedBuiltinType,
MultipleChildNodes,
WrongNodeType,
WrongDependentMemberType,
BadDirectness,
UnknownEncoding,
InvalidImplCalleeConvention,
InvalidImplDifferentiability,
InvalidImplFunctionAttribute,
InvalidImplParameterConvention,
InvalidMetatypeRepresentation,
MultiByteRelatedEntity,
BadValueWitnessKind,
NotAContextNode,
};

Code code;
NodePointer node;
unsigned line;

ManglingError() : code(Uninitialized), node(nullptr) {}
ManglingError(Code c) : code(c), node(nullptr), line(0) {}
ManglingError(Code c, NodePointer n, unsigned l) : code(c), node(n), line(l) {}

bool isSuccess() const { return code == Success; }
};

#define MANGLING_ERROR(c,n) ManglingError((c), (n), __LINE__)

/// Used as a return type for mangling functions that may fail
template <typename T>
class SWIFT_NODISCARD ManglingErrorOr {
private:
ManglingError err_;
T value_;

public:
ManglingErrorOr() : err_() {}
ManglingErrorOr(ManglingError::Code code,
NodePointer node = nullptr,
unsigned line = 0)
: err_(code, node, line) {}
ManglingErrorOr(const ManglingError &err) : err_(err) {}
ManglingErrorOr(const T &t) : err_(ManglingError::Success), value_(t) {}
ManglingErrorOr(T &&t) : err_(ManglingError::Success), value_(std::move(t)) {}

bool isSuccess() const { return err_.code == ManglingError::Success; }

const ManglingError &error() const { return err_; }

const T &result() const {
assert(isSuccess());
return value_;
}
};

/// Remangle a demangled parse tree.
std::string mangleNode(NodePointer root);
ManglingErrorOr<std::string> mangleNode(NodePointer root);

using SymbolicResolver =
llvm::function_ref<Demangle::NodePointer (SymbolicReferenceKind,
const void *)>;

/// Remangle a demangled parse tree, using a callback to resolve
/// symbolic references.
std::string mangleNode(NodePointer root, SymbolicResolver resolver);
ManglingErrorOr<std::string> mangleNode(NodePointer root, SymbolicResolver resolver);

/// Remangle a demangled parse tree, using a callback to resolve
/// symbolic references.
///
/// The returned string is owned by \p Factory. This means \p Factory must stay
/// alive as long as the returned string is used.
llvm::StringRef mangleNode(NodePointer root, SymbolicResolver resolver,
NodeFactory &Factory);
ManglingErrorOr<llvm::StringRef> mangleNode(NodePointer root,
SymbolicResolver resolver,
NodeFactory &Factory);

/// Remangle in the old mangling scheme.
///
/// This is only used for objc-runtime names.
std::string mangleNodeOld(NodePointer root);
ManglingErrorOr<std::string> mangleNodeOld(NodePointer root);

/// Remangle in the old mangling scheme.
///
/// This is only used for objc-runtime names.
/// The returned string is owned by \p Factory. This means \p Factory must stay
/// alive as long as the returned string is used.
llvm::StringRef mangleNodeOld(NodePointer node, NodeFactory &Factory);
ManglingErrorOr<llvm::StringRef> mangleNodeOld(NodePointer node,
NodeFactory &Factory);

/// Remangle in the old mangling scheme and embed the name in "_Tt<name>_".
///
/// The returned string is null terminated and owned by \p Factory. This means
/// \p Factory must stay alive as long as the returned string is used.
const char *mangleNodeAsObjcCString(NodePointer node, NodeFactory &Factory);
ManglingErrorOr<const char *> mangleNodeAsObjcCString(NodePointer node,
NodeFactory &Factory);

/// Transform the node structure to a string.
///
Expand Down Expand Up @@ -626,7 +698,7 @@ bool nodeConsumesGenericArgs(Node *node);

bool isSpecialized(Node *node);

NodePointer getUnspecialized(Node *node, NodeFactory &Factory);
ManglingErrorOr<NodePointer> getUnspecialized(Node *node, NodeFactory &Factory);

/// Returns true if the node \p kind refers to a context node, e.g. a nominal
/// type or a function.
Expand Down
16 changes: 13 additions & 3 deletions include/swift/Demangling/TypeDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,14 @@ class TypeDecoder {
return decodeMangledType(genericArgs->getChild(0), depth + 1);
}
case NodeKind::BuiltinTypeName: {
auto mangledName = Demangle::mangleNode(Node);
return Builder.createBuiltinType(Node->getText().str(), mangledName);
auto mangling = Demangle::mangleNode(Node);
if (!mangling.isSuccess()) {
return MAKE_NODE_TYPE_ERROR(Node,
"failed to mangle node (%d:%u)",
mangling.error().code,
mangling.error().line);
}
return Builder.createBuiltinType(Node->getText().str(), mangling.result());
}
case NodeKind::Metatype:
case NodeKind::ExistentialMetatype: {
Expand Down Expand Up @@ -1373,7 +1379,11 @@ return {}; // Not Implemented!
parent = decodeMangledType(parentContext, depth + 1).getType();
// Remove any generic arguments from the context node, producing a
// node that references the nominal type declaration.
declNode = Demangle::getUnspecialized(node, Builder.getNodeFactory());
auto unspec =
Demangle::getUnspecialized(node, Builder.getNodeFactory());
if (!unspec.isSuccess())
return TypeLookupError("Failed to unspecialize type");
declNode = unspec.result();
break;
}
}
Expand Down
4 changes: 4 additions & 0 deletions include/swift/IRGen/Linking.h
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,10 @@ class LinkEntity {
std::string mangleAsString() const;
SILLinkage getLinkage(ForDefinition_t isDefinition) const;

bool hasDecl() const {
return isDeclKind(getKind());
}

const ValueDecl *getDecl() const {
assert(isDeclKind(getKind()));
return reinterpret_cast<ValueDecl*>(Pointer);
Expand Down
26 changes: 19 additions & 7 deletions include/swift/Reflection/TypeRefBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class TypeRefBuilder {
void clearNodeFactory() { Dem.clear(); }

BuiltType decodeMangledType(Node *node);

///
/// Factory methods for all TypeRef kinds
///
Expand All @@ -300,12 +300,20 @@ class TypeRefBuilder {
}

llvm::Optional<std::string> createTypeDecl(Node *node, bool &typeAlias) {
return Demangle::mangleNode(node);
auto mangling = Demangle::mangleNode(node);
if (!mangling.isSuccess()) {
return llvm::None;
}
return mangling.result();
}

BuiltProtocolDecl
createProtocolDecl(Node *node) {
return std::make_pair(Demangle::mangleNode(node), false);
auto mangling = Demangle::mangleNode(node);
if (!mangling.isSuccess()) {
return llvm::None;
}
return std::make_pair(mangling.result(), false);
}

BuiltProtocolDecl
Expand Down Expand Up @@ -395,12 +403,16 @@ class TypeRefBuilder {

return underlyingTy->subst(*this, subs);
}


auto mangling = mangleNode(opaqueDescriptor,
SymbolicResolver(),
Dem);
if (!mangling.isSuccess())
return nullptr;

// Otherwise, build a type ref that represents the opaque type.
return OpaqueArchetypeTypeRef::create(*this,
mangleNode(opaqueDescriptor,
SymbolicResolver(),
Dem),
mangling.result(),
nodeToString(opaqueDescriptor),
ordinal,
genericArgs);
Expand Down
6 changes: 5 additions & 1 deletion include/swift/Remote/MetadataReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,11 @@ class MetadataReader {
if (!node || node->getKind() != Node::Kind::Type)
return BuiltType();

auto name = Demangle::mangleNode(node);
auto mangling = Demangle::mangleNode(node);
if (!mangling.isSuccess())
return BuiltType();
auto name = mangling.result();

auto BuiltForeign = Builder.createForeignClassType(std::move(name));
TypeCache[MetadataAddress] = BuiltForeign;
return BuiltForeign;
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 @@ -308,6 +308,8 @@ PASS(Outliner, "outliner",
"Function Outlining Optimization")
PASS(OwnershipModelEliminator, "ownership-model-eliminator",
"Eliminate Ownership Annotation of SIL")
PASS(ModulePrinter, "module-printer",
"Print the module")
PASS(NestedSemanticFunctionCheck, "nested-semantic-function-check",
"Diagnose improperly nested '@_semantics' functions")
PASS(NonTransparentFunctionOwnershipModelEliminator,
Expand Down
12 changes: 9 additions & 3 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,11 @@ Type ASTBuilder::resolveOpaqueType(NodePointer opaqueDescriptor,
auto definingDecl = opaqueDescriptor->getChild(0);
auto definingGlobal = Factory.createNode(Node::Kind::Global);
definingGlobal->addChild(definingDecl, Factory);
auto mangledName = mangleNode(definingGlobal);

auto mangling = mangleNode(definingGlobal);
if (!mangling.isSuccess())
return Type();
auto mangledName = mangling.result();

auto moduleNode = findModuleNode(definingDecl);
if (!moduleNode)
return Type();
Expand Down Expand Up @@ -935,7 +938,10 @@ ASTBuilder::findDeclContext(NodePointer node) {
if (!module) return nullptr;

// Look up the local type by its mangling.
auto mangledName = Demangle::mangleNode(node);
auto mangling = Demangle::mangleNode(node);
if (!mangling.isSuccess()) return nullptr;
auto mangledName = mangling.result();

auto decl = module->lookupLocalType(mangledName);
if (!decl) return nullptr;

Expand Down
13 changes: 10 additions & 3 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,11 @@ std::string ASTMangler::mangleSILDifferentiabilityWitness(StringRef originalName
Demangler demangler;
auto *node = mangleSILDifferentiabilityWitnessAsNode(
originalName, kind, config, demangler);
return mangleNode(node);
auto mangling = mangleNode(node);
if (!mangling.isSuccess()) {
llvm_unreachable("unexpected mangling failure");
}
return mangling.result();
}
beginManglingWithoutPrefix();
appendOperator(originalName);
Expand Down Expand Up @@ -721,8 +725,11 @@ std::string ASTMangler::mangleObjCRuntimeName(const NominalTypeDecl *Nominal) {
TyMangling->addChild(Ty, Dem);
Node *NewGlobal = Dem.createNode(Node::Kind::Global);
NewGlobal->addChild(TyMangling, Dem);
std::string OldName = mangleNodeOld(NewGlobal);
return OldName;
auto mangling = mangleNodeOld(NewGlobal);
if (!mangling.isSuccess()) {
llvm_unreachable("unexpected mangling failure");
}
return mangling.result();
#endif
}

Expand Down
1 change: 1 addition & 0 deletions lib/AST/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ add_swift_host_library(swiftAST STATIC
RawComment.cpp
RequirementEnvironment.cpp
RequirementMachine/HomotopyReduction.cpp
RequirementMachine/GeneratingConformances.cpp
RequirementMachine/GenericSignatureQueries.cpp
RequirementMachine/PropertyMap.cpp
RequirementMachine/ProtocolGraph.cpp
Expand Down
5 changes: 4 additions & 1 deletion lib/AST/RequirementMachine/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ enum class DebugFlags : unsigned {
ConcretizeNestedTypes = (1<<5),

/// Print debug output from the homotopy reduction algorithm.
HomotopyReduction = (1<<6)
HomotopyReduction = (1<<6),

/// Print debug output from the generating conformances algorithm.
GeneratingConformances = (1<<7),
};

using DebugOptions = OptionSet<DebugFlags>;
Expand Down
Loading