Skip to content

Port swift/remote to Windows and MSVC #6025

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 1 commit into from
Dec 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 39 additions & 0 deletions include/swift/Basic/Unreachable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//===--- Unreachable.h - Implements swift_unrachable ------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines swift_unreachable, an LLVM-independent implementation of
// llvm_unreachable.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_BASIC_UNREACHABLE_H
#define SWIFT_BASIC_UNREACHABLE_H

#include <assert.h>
#include <stdlib.h>

#ifdef __GNUC__
#define SWIFT_ATTRIBUTE_NORETURN __attribute__((noreturn))
#elif defined(_MSC_VER)
#define SWIFT_ATTRIBUTE_NORETURN __declspec(noreturn)
#else
#define SWIFT_ATTRIBUTE_NORETURN
#endif

SWIFT_ATTRIBUTE_NORETURN
inline static void swift_unreachable(const char* msg) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, please don't name this swift_unreachable—we still want people to prefer llvm_unreachable when possible. How about swift_runtime_unreachable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

…if there isn't already such a thing somewhere in the real runtime, in which case we should use that.

Copy link
Contributor

@jckarter jckarter Dec 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just use llvm_unreachable with a runtime-local implementation of llvm_unreachable_internal for debug builds? Unreachable branches should be optimized as really unreachable in release builds of the runtime.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could be even better. I'm honestly fairly unhappy about us having runtime things in include/swift/Basic/, but maybe I should be unhappy about having runtime libraries in lib/.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking we'd just reimplement llvm_unreachable_internal locally inside the runtime. We already have to provide a reimplementation of some LLVM hashing stuff to be able to use DenseMap inside the runtime.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't realize we'd actually bitten the bullet and put DenseMap in. Should we just link llvmSupport and let the linker figure it out? ;-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrose-apple, while you propose that (probably) in jest, that would help with another issue that recently came up. The ABI breaking validation for LLVM uses a value in llvmSuppprt. We ended up having to add another build option to disable it and force disable for swift.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth a shot. If linking llvmSupport into the runtime doesn't affect the binary size too much, it'd certainly be preferable to cherry-picking certain definitions we need.

assert(false && msg);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you get an unused variable warning when building release? (_NDEBUG).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, haven't done a release build - I've fixed this

(void)msg;
abort();
}

#endif // SWIFT_BASIC_UNREACHABLE_H
3 changes: 3 additions & 0 deletions include/swift/Remote/Failure.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "swift/Remote/RemoteAddress.h"

#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include <cassert>
#include <string>
#include <cstring>
Expand Down Expand Up @@ -107,6 +108,8 @@ class Failure {
case Kind::KIND: return TEXT;
#include "swift/Remote/FailureKinds.def"
}

llvm_unreachable("Unhandled FailureKind in switch.");
}

union ArgStorage {
Expand Down
8 changes: 2 additions & 6 deletions include/swift/Remote/InProcessMemoryReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

#include "swift/Remote/MemoryReader.h"

#include <memory>
#include <dlfcn.h>
#include <cstring>

namespace swift {
namespace remote {
Expand All @@ -36,10 +35,7 @@ class InProcessMemoryReader final : public MemoryReader {
return sizeof(size_t);
}

RemoteAddress getSymbolAddress(const std::string &name) override {
auto pointer = dlsym(RTLD_DEFAULT, name.c_str());
return RemoteAddress(pointer);
}
RemoteAddress getSymbolAddress(const std::string &name) override;

bool readString(RemoteAddress address, std::string &dest) override {
dest = address.getLocalPointer<char>();
Expand Down
4 changes: 3 additions & 1 deletion include/swift/Remote/MetadataReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "swift/Remote/MemoryReader.h"
#include "swift/Basic/Demangle.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Unreachable.h"

#include <vector>
#include <unordered_map>
Expand Down Expand Up @@ -761,6 +762,8 @@ class MetadataReader {
return BuiltOpaque;
}
}

swift_unreachable("Unhandled MetadataKind in switch");
}

BuiltType readTypeFromMangledName(const char *MangledTypeName,
Expand Down Expand Up @@ -1259,4 +1262,3 @@ namespace llvm {
}

#endif // SWIFT_REFLECTION_READER_H

1 change: 1 addition & 0 deletions lib/RemoteAST/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_swift_library(swiftRemoteAST STATIC
RemoteAST.cpp
InProcessMemoryReader.cpp
LINK_LIBRARIES
swiftSema swiftIRGen)
39 changes: 39 additions & 0 deletions lib/RemoteAST/InProcessMemoryReader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//===--- InProcessMemoryReader.cpp - Reads local memory ---------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements the abstract interface for working with remote memory.
// This method cannot be implemented in the header, as we must avoid importing
// <windows.h> in a header, which causes conflicts with Swift definitions.
//
//===----------------------------------------------------------------------===//

#include "swift/Remote/InProcessMemoryReader.h"

#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#else
#include <dlfcn.h>
#endif

using namespace swift;
using namespace swift::remote;

RemoteAddress InProcessMemoryReader::getSymbolAddress(const std::string &name) {
#if defined(_WIN32)
auto pointer = GetProcAddress(GetModuleHandle(NULL), name.c_str());
#else
auto pointer = dlsym(RTLD_DEFAULT, name.c_str());
#endif
return RemoteAddress(pointer);
}
10 changes: 5 additions & 5 deletions lib/RemoteAST/RemoteAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,11 @@ class RemoteASTContextImpl {
return getBuilder().getFailureAsResult<T>(Failure::Unknown);
}

template <class T, class KindTy, class... ArgTys>
Result<T> fail(KindTy kind, ArgTys &&...args) {
return Result<T>::emplaceFailure(kind, std::forward<ArgTys>(args)...);
}

private:
virtual RemoteASTTypeBuilder &getBuilder() = 0;
virtual MemoryReader &getReader() = 0;
Expand All @@ -716,11 +721,6 @@ class RemoteASTContextImpl {
return IRGen.get();
}

template <class T, class KindTy, class... ArgTys>
Result<T> fail(KindTy kind, ArgTys &&...args) {
return Result<T>::emplaceFailure(kind, std::forward<ArgTys>(args)...);
}

Result<uint64_t>
getOffsetOfField(Type type, NominalTypeDecl *typeDecl,
RemoteAddress optMetadata, StringRef memberName) {
Expand Down
9 changes: 6 additions & 3 deletions stdlib/public/SwiftRemoteMirror/SwiftRemoteMirror.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#include "swift/Basic/Unreachable.h"
#include "swift/Reflection/ReflectionContext.h"
#include "swift/Reflection/TypeLowering.h"
#include "swift/Remote/CMemoryReader.h"
Expand All @@ -20,7 +21,7 @@ using namespace swift::reflection;
using namespace swift::remote;

using NativeReflectionContext
= ReflectionContext<External<RuntimeTarget<sizeof(uintptr_t)>>>;
= swift::reflection::ReflectionContext<External<RuntimeTarget<sizeof(uintptr_t)>>>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


uint16_t
swift_reflection_getSupportedMetadataVersion() {
Expand All @@ -45,12 +46,12 @@ swift_reflection_createReflectionContext(void *ReaderContext,

auto Reader = std::make_shared<CMemoryReader>(ReaderImpl);
auto Context
= new ReflectionContext<External<RuntimeTarget<sizeof(uintptr_t)>>>(Reader);
= new swift::reflection::ReflectionContext<External<RuntimeTarget<sizeof(uintptr_t)>>>(Reader);
return reinterpret_cast<SwiftReflectionContextRef>(Context);
}

void swift_reflection_destroyReflectionContext(SwiftReflectionContextRef ContextRef) {
auto Context = reinterpret_cast<ReflectionContext<InProcess> *>(ContextRef);
auto Context = reinterpret_cast<swift::reflection::ReflectionContext<InProcess> *>(ContextRef);
delete Context;
}

Expand Down Expand Up @@ -175,6 +176,8 @@ swift_layout_kind_t getTypeInfoKind(const TypeInfo &TI) {
}
}
}

swift_unreachable("Unhandled TypeInfoKind in switch");
}

static swift_typeinfo_t convertTypeInfo(const TypeInfo *TI) {
Expand Down