Skip to content

Commit 7e33575

Browse files
kubamracekcompnerd
andauthored
Re-import LLVMSupport from llvm-project and enforce header includes only being used from the Swift fork when building stdlib (#40173)
* Enforce using headers from Swift's LLVMSupport fork and not llvm-project when building stdlib * [LLVMSupport] Re-import LLVMSupport .cpp and .h files from 9ff3a9759b7c2f146e7f46e4aebc60453c577c5a from apple/llvm-project Done via the following commands, while having llvm-project checked out at 9ff3a9759b7c2f146e7f46e4aebc60453c577c5a, a commit on the stable/20210726 branch of apple/llvm-project, <swiftlang/llvm-project@9ff3a97>: for i in swift/stdlib/public/LLVMSupport/*.cpp ; do cp llvm-project/llvm/lib/Support/$(basename $i) $i ; done for i in swift/stdlib/include/llvm/ADT/*.h; do cp llvm-project/llvm/include/llvm/ADT/$(basename $i) $i ; done for i in swift/stdlib/include/llvm/Support/*.h; do cp llvm-project/llvm/include/llvm/Support/$(basename $i) $i ; done cp llvm-project/llvm/include/llvm/ADT/ScopeExit.h swift/stdlib/include/llvm/ADT/ScopeExit.h cp llvm-project/llvm/include/llvm/ADT/Twine.h swift/stdlib/include/llvm/ADT/Twine.h cp llvm-project/llvm/include/llvm/Support/raw_ostream.h swift/stdlib/include/llvm/Support/raw_ostream.h * [LLVMSupport] Re-namespace the LLVMSupport fork after re-forking by re-applying b72788c More precisely: 1) git cherry-pick b72788c 2) manually resolve the conflict in AlignOf.h by keeping the HEAD's version of the chunk and discarding the cherry-pick's change 3) git add AlignOf.h 4) git status | grep "deleted by us" | awk '{print($4)}' | xargs git rm 5) git cherry-pick --continue Original namespacing commit message: > This adds the `__swift::__runtime` inline namespace to the LLVMSupport > interfaces. This avoids an ODR violation when LLVM and Swift are in the > same address space. It also will aid in the process of pruning the > LLVMSupport library by ensuring that accidental leakage of the llvm > namespace does not allow us to remove symbols which we rely on. * [LLVMSupport] Re-apply "pruning" on re-forked LLVMSupport from bb10270 This re-applies the "pruning" commit from bb10270, which did the following: - Remove many whole files, - Remove "epoch tracking" and "reverse iteration" support from ADT containers - Remove "ABI break checking" support from STLExtras - Remove float parsing functions from StringExtras.h - Remove APInt/APSInt dependencies from StringRef.h + StringRef.cpp (edit distance, int parsing) - Remove some variants of error handling and dependency of dbgs() from ErrorHandling.h and ErrorHandling.cpp We don't need to do the whole-file-removal step, because that's already done, but the rest is re-applied by doing: 1) git cherry-pick bb10270 2) manually resolving conflict in ADT/DenseMap.h by keeping HEAD's version of the chunk and removing epoch tracking from it 3) manually resolving conflict in ADT/STLExtras.h by keeping HEAD's version of the chunk and removing ABI check checking from it 4) manually resolving conflict in ADT/StringExtras.h by deleting the whole chunk (removing APInt/APSInt dependent functions) 5) manually resolving conflict in ErrorHandling.cpp by force-applying the cherry-pick's version (removing write() calls and OOM callback) 6) manually resolving the three conflicts in CMakeLists.txt files by keeping HEAD's version completely 7) git add stdlib/include/llvm/{ADT/StringSwitch.h,ADT/Twine.h,Support/raw_ostream.h} Original commit description: > Reduce LLVMSupport to the subset required for the runtime. This reduces > the TCB and the overheads of the runtime. The inline namespace's > preservation ensures that ODR violations do not occur. * [LLVMSupport] Re-apply all post-import modifications on LLVMSupport that the Swift's fork has Since the previous commits re-imported "vanilla" versions of LLVMSupport, we need to re-apply all modifications that the Swift's fork has made since the last import. More precisely: 1) git diff 7b70120..origin/main -- stdlib/include/llvm stdlib/public/LLVMSupport | git apply -3 --exclude "stdlib/include/llvm/Support/DataTypes.h" --exclude "stdlib/include/llvm/Config/llvm-config.h.cmake" 2) manually resolve conflict in STLExtras.h by applying the "__swift::__runtime" prefix to HEAD's version 3) manually resolve conflicts in StringSwitch.h by keeping HEAD's version (removing the Unicode BOM marker at the beginning of the file, keeping LLVM's version of the string functions) 4) manually resolve conflict in SwapByteOrder.h by adding the `defined(__wasi__)` part into the #if * [LLVMSupport] Drop remaining dependencies on APSInt.h, Error.h, DataTypes.h and STLForwardCompat.h Most cases can drop the #includes without any changes, in some cases there are straighforward replacements (climits, cstdint). For STLForwardCompat.h, we need to bring in parts of STLForwardCompat.h from llvm-project. * [LLVMSupport] Remove raw_ostream.h and drop dependencies to it from the runtime * [LLVMSupport] Simplify error reporting in SmallVector and avoid using std::string when producing fatal errors messages Co-authored-by: Saleem Abdulrasool <compnerd@compnerd.org>
1 parent 8f50a2a commit 7e33575

37 files changed

+1880
-714
lines changed

include/swift/AST/Decl.h

+3
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ enum class AssociatedValueCheck {
207207
/// Diagnostic printing of \c StaticSpellingKind.
208208
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, StaticSpellingKind SSK);
209209

210+
/// Diagnostic printing of \c ReferenceOwnership.
211+
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ReferenceOwnership RO);
212+
210213
/// Encapsulation of the overload signature of a given declaration,
211214
/// which is used to determine uniqueness of a declaration within a
212215
/// given context.

include/swift/AST/LayoutConstraintKind.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
#include "llvm/Support/DataTypes.h"
17+
#include <cstdint>
1818

1919
#ifndef SWIFT_LAYOUT_CONSTRAINTKIND_H
2020
#define SWIFT_LAYOUT_CONSTRAINTKIND_H

include/swift/AST/Ownership.h

-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "llvm/ADT/StringRef.h"
2424
#include "llvm/Support/Compiler.h"
2525
#include "llvm/Support/ErrorHandling.h"
26-
#include "llvm/Support/raw_ostream.h"
2726
#include <assert.h>
2827
#include <limits.h>
2928
#include <stdint.h>
@@ -118,9 +117,6 @@ optionalityOf(ReferenceOwnership ownership) {
118117
llvm_unreachable("impossible");
119118
}
120119

121-
/// Diagnostic printing of \c StaticSpellingKind.
122-
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ReferenceOwnership RO);
123-
124120
/// Different kinds of value ownership supported by Swift.
125121
enum class ValueOwnership : uint8_t {
126122
/// the context-dependent default ownership (sometimes shared,

stdlib/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ set(SWIFT_CONCURRENCY_GLOBAL_EXECUTOR
178178
# End of user-configurable options.
179179
#
180180

181+
# Remove llvm-project/llvm/include directory from -I search part when building
182+
# the stdlib. We have our own fork of LLVM includes (Support, ADT) in
183+
# stdlib/include/llvm and we don't want to silently use headers from LLVM.
184+
get_property(stdlib_include_directories DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
185+
list(REMOVE_ITEM stdlib_include_directories "${LLVM_MAIN_INCLUDE_DIR}")
186+
set_property(DIRECTORY PROPERTY INCLUDE_DIRECTORIES ${stdlib_include_directories})
187+
181188
include(AddSwiftStdlib)
182189

183190
# Create convenience targets for the Swift standard library.

stdlib/include/llvm/ADT/ArrayRef.h

+51-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
inline namespace __swift { inline namespace __runtime {
2828
namespace llvm {
2929

30+
template<typename T> struct DenseMapInfo;
31+
3032
/// ArrayRef - Represent a constant reference to an array (0 or more elements
3133
/// consecutively in memory), i.e. a start pointer and a length. It allows
3234
/// various APIs to take consecutive elements easily and conveniently.
@@ -41,10 +43,17 @@ namespace llvm {
4143
template<typename T>
4244
class LLVM_GSL_POINTER LLVM_NODISCARD ArrayRef {
4345
public:
44-
using iterator = const T *;
45-
using const_iterator = const T *;
46-
using size_type = size_t;
46+
using value_type = T;
47+
using pointer = value_type *;
48+
using const_pointer = const value_type *;
49+
using reference = value_type &;
50+
using const_reference = const value_type &;
51+
using iterator = const_pointer;
52+
using const_iterator = const_pointer;
4753
using reverse_iterator = std::reverse_iterator<iterator>;
54+
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
55+
using size_type = size_t;
56+
using difference_type = ptrdiff_t;
4857

4958
private:
5059
/// The start of the array, in an external buffer.
@@ -298,8 +307,17 @@ namespace llvm {
298307
template<typename T>
299308
class LLVM_NODISCARD MutableArrayRef : public ArrayRef<T> {
300309
public:
301-
using iterator = T *;
310+
using value_type = T;
311+
using pointer = value_type *;
312+
using const_pointer = const value_type *;
313+
using reference = value_type &;
314+
using const_reference = const value_type &;
315+
using iterator = pointer;
316+
using const_iterator = const_pointer;
302317
using reverse_iterator = std::reverse_iterator<iterator>;
318+
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
319+
using size_type = size_t;
320+
using difference_type = ptrdiff_t;
303321

304322
/// Construct an empty MutableArrayRef.
305323
/*implicit*/ MutableArrayRef() = default;
@@ -554,6 +572,35 @@ namespace llvm {
554572
return hash_combine_range(S.begin(), S.end());
555573
}
556574

575+
// Provide DenseMapInfo for ArrayRefs.
576+
template <typename T> struct DenseMapInfo<ArrayRef<T>> {
577+
static inline ArrayRef<T> getEmptyKey() {
578+
return ArrayRef<T>(
579+
reinterpret_cast<const T *>(~static_cast<uintptr_t>(0)), size_t(0));
580+
}
581+
582+
static inline ArrayRef<T> getTombstoneKey() {
583+
return ArrayRef<T>(
584+
reinterpret_cast<const T *>(~static_cast<uintptr_t>(1)), size_t(0));
585+
}
586+
587+
static unsigned getHashValue(ArrayRef<T> Val) {
588+
assert(Val.data() != getEmptyKey().data() &&
589+
"Cannot hash the empty key!");
590+
assert(Val.data() != getTombstoneKey().data() &&
591+
"Cannot hash the tombstone key!");
592+
return (unsigned)(hash_value(Val));
593+
}
594+
595+
static bool isEqual(ArrayRef<T> LHS, ArrayRef<T> RHS) {
596+
if (RHS.data() == getEmptyKey().data())
597+
return LHS.data() == getEmptyKey().data();
598+
if (RHS.data() == getTombstoneKey().data())
599+
return LHS.data() == getTombstoneKey().data();
600+
return LHS == RHS;
601+
}
602+
};
603+
557604
} // end namespace llvm
558605
}} // namespace swift::runtime
559606

stdlib/include/llvm/ADT/DenseMap.h

+18-14
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ class DenseMapBase {
381381
setNumEntries(other.getNumEntries());
382382
setNumTombstones(other.getNumTombstones());
383383

384-
if (is_trivially_copyable<KeyT>::value &&
385-
is_trivially_copyable<ValueT>::value)
384+
if (std::is_trivially_copyable<KeyT>::value &&
385+
std::is_trivially_copyable<ValueT>::value)
386386
memcpy(reinterpret_cast<void *>(getBuckets()), other.getBuckets(),
387387
getNumBuckets() * sizeof(BucketT));
388388
else
@@ -667,7 +667,7 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
667667
unsigned NumBuckets;
668668

669669
public:
670-
/// Create a DenseMap wth an optional \p InitialReserve that guarantee that
670+
/// Create a DenseMap with an optional \p InitialReserve that guarantee that
671671
/// this number of elements can be inserted in the map without grow()
672672
explicit DenseMap(unsigned InitialReserve = 0) { init(InitialReserve); }
673673

@@ -864,6 +864,9 @@ class SmallDenseMap
864864
this->insert(I, E);
865865
}
866866

867+
SmallDenseMap(std::initializer_list<typename BaseT::value_type> Vals)
868+
: SmallDenseMap(Vals.begin(), Vals.end()) {}
869+
867870
~SmallDenseMap() {
868871
this->destroyAll();
869872
deallocateBuckets();
@@ -894,7 +897,7 @@ class SmallDenseMap
894897
std::swap(*LHSB, *RHSB);
895898
continue;
896899
}
897-
// Swap separately and handle any assymetry.
900+
// Swap separately and handle any asymmetry.
898901
std::swap(LHSB->getFirst(), RHSB->getFirst());
899902
if (hasLHSValue) {
900903
::new (&RHSB->getSecond()) ValueT(std::move(LHSB->getSecond()));
@@ -982,7 +985,7 @@ class SmallDenseMap
982985
if (Small) {
983986
// First move the inline buckets into a temporary storage.
984987
AlignedCharArrayUnion<BucketT[InlineBuckets]> TmpStorage;
985-
BucketT *TmpBegin = reinterpret_cast<BucketT *>(TmpStorage.buffer);
988+
BucketT *TmpBegin = reinterpret_cast<BucketT *>(&TmpStorage);
986989
BucketT *TmpEnd = TmpBegin;
987990

988991
// Loop over the buckets, moving non-empty, non-tombstones into the
@@ -1072,8 +1075,8 @@ class SmallDenseMap
10721075
assert(Small);
10731076
// Note that this cast does not violate aliasing rules as we assert that
10741077
// the memory's dynamic type is the small, inline bucket buffer, and the
1075-
// 'storage.buffer' static type is 'char *'.
1076-
return reinterpret_cast<const BucketT *>(storage.buffer);
1078+
// 'storage' is a POD containing a char buffer.
1079+
return reinterpret_cast<const BucketT *>(&storage);
10771080
}
10781081

10791082
BucketT *getInlineBuckets() {
@@ -1084,7 +1087,7 @@ class SmallDenseMap
10841087
const LargeRep *getLargeRep() const {
10851088
assert(!Small);
10861089
// Note, same rule about aliasing as with getInlineBuckets.
1087-
return reinterpret_cast<const LargeRep *>(storage.buffer);
1090+
return reinterpret_cast<const LargeRep *>(&storage);
10881091
}
10891092

10901093
LargeRep *getLargeRep() {
@@ -1130,8 +1133,6 @@ class DenseMapIterator {
11301133
friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, true>;
11311134
friend class DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, false>;
11321135

1133-
using ConstIterator = DenseMapIterator<KeyT, ValueT, KeyInfoT, Bucket, true>;
1134-
11351136
public:
11361137
using difference_type = ptrdiff_t;
11371138
using value_type =
@@ -1171,11 +1172,14 @@ class DenseMapIterator {
11711172
return Ptr;
11721173
}
11731174

1174-
bool operator==(const ConstIterator &RHS) const {
1175-
return Ptr == RHS.Ptr;
1175+
friend bool operator==(const DenseMapIterator &LHS,
1176+
const DenseMapIterator &RHS) {
1177+
return LHS.Ptr == RHS.Ptr;
11761178
}
1177-
bool operator!=(const ConstIterator &RHS) const {
1178-
return Ptr != RHS.Ptr;
1179+
1180+
friend bool operator!=(const DenseMapIterator &LHS,
1181+
const DenseMapIterator &RHS) {
1182+
return !(LHS == RHS);
11791183
}
11801184

11811185
inline DenseMapIterator& operator++() { // Preincrement

stdlib/include/llvm/ADT/DenseMapInfo.h

+3-60
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
#ifndef LLVM_ADT_DENSEMAPINFO_H
1414
#define LLVM_ADT_DENSEMAPINFO_H
1515

16-
#include "llvm/ADT/ArrayRef.h"
1716
#include "llvm/ADT/Hashing.h"
18-
#include "llvm/ADT/StringRef.h"
1917
#include <cassert>
2018
#include <cstddef>
2119
#include <cstdint>
20+
#include <tuple>
2221
#include <utility>
2322

2423
inline namespace __swift { inline namespace __runtime {
@@ -257,7 +256,7 @@ template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
257256
}
258257

259258
template <unsigned I>
260-
static unsigned getHashValueImpl(const Tuple &values, std::true_type) {
259+
static unsigned getHashValueImpl(const Tuple &, std::true_type) {
261260
return 0;
262261
}
263262

@@ -275,7 +274,7 @@ template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
275274
}
276275

277276
template <unsigned I>
278-
static bool isEqualImpl(const Tuple &lhs, const Tuple &rhs, std::true_type) {
277+
static bool isEqualImpl(const Tuple &, const Tuple &, std::true_type) {
279278
return true;
280279
}
281280

@@ -285,62 +284,6 @@ template <typename... Ts> struct DenseMapInfo<std::tuple<Ts...>> {
285284
}
286285
};
287286

288-
// Provide DenseMapInfo for StringRefs.
289-
template <> struct DenseMapInfo<StringRef> {
290-
static inline StringRef getEmptyKey() {
291-
return StringRef(reinterpret_cast<const char *>(~static_cast<uintptr_t>(0)),
292-
0);
293-
}
294-
295-
static inline StringRef getTombstoneKey() {
296-
return StringRef(reinterpret_cast<const char *>(~static_cast<uintptr_t>(1)),
297-
0);
298-
}
299-
300-
static unsigned getHashValue(StringRef Val) {
301-
assert(Val.data() != getEmptyKey().data() && "Cannot hash the empty key!");
302-
assert(Val.data() != getTombstoneKey().data() &&
303-
"Cannot hash the tombstone key!");
304-
return (unsigned)(hash_value(Val));
305-
}
306-
307-
static bool isEqual(StringRef LHS, StringRef RHS) {
308-
if (RHS.data() == getEmptyKey().data())
309-
return LHS.data() == getEmptyKey().data();
310-
if (RHS.data() == getTombstoneKey().data())
311-
return LHS.data() == getTombstoneKey().data();
312-
return LHS == RHS;
313-
}
314-
};
315-
316-
// Provide DenseMapInfo for ArrayRefs.
317-
template <typename T> struct DenseMapInfo<ArrayRef<T>> {
318-
static inline ArrayRef<T> getEmptyKey() {
319-
return ArrayRef<T>(reinterpret_cast<const T *>(~static_cast<uintptr_t>(0)),
320-
size_t(0));
321-
}
322-
323-
static inline ArrayRef<T> getTombstoneKey() {
324-
return ArrayRef<T>(reinterpret_cast<const T *>(~static_cast<uintptr_t>(1)),
325-
size_t(0));
326-
}
327-
328-
static unsigned getHashValue(ArrayRef<T> Val) {
329-
assert(Val.data() != getEmptyKey().data() && "Cannot hash the empty key!");
330-
assert(Val.data() != getTombstoneKey().data() &&
331-
"Cannot hash the tombstone key!");
332-
return (unsigned)(hash_value(Val));
333-
}
334-
335-
static bool isEqual(ArrayRef<T> LHS, ArrayRef<T> RHS) {
336-
if (RHS.data() == getEmptyKey().data())
337-
return LHS.data() == getEmptyKey().data();
338-
if (RHS.data() == getTombstoneKey().data())
339-
return LHS.data() == getTombstoneKey().data();
340-
return LHS == RHS;
341-
}
342-
};
343-
344287
template <> struct DenseMapInfo<hash_code> {
345288
static inline hash_code getEmptyKey() { return hash_code(-1); }
346289
static inline hash_code getTombstoneKey() { return hash_code(-2); }

stdlib/include/llvm/ADT/DenseSet.h

+17-4
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,12 @@ class DenseSetImpl {
131131

132132
Iterator& operator++() { ++I; return *this; }
133133
Iterator operator++(int) { auto T = *this; ++I; return T; }
134-
bool operator==(const ConstIterator& X) const { return I == X.I; }
135-
bool operator!=(const ConstIterator& X) const { return I != X.I; }
134+
friend bool operator==(const Iterator &X, const Iterator &Y) {
135+
return X.I == Y.I;
136+
}
137+
friend bool operator!=(const Iterator &X, const Iterator &Y) {
138+
return X.I != Y.I;
139+
}
136140
};
137141

138142
class ConstIterator {
@@ -156,8 +160,12 @@ class DenseSetImpl {
156160

157161
ConstIterator& operator++() { ++I; return *this; }
158162
ConstIterator operator++(int) { auto T = *this; ++I; return T; }
159-
bool operator==(const ConstIterator& X) const { return I == X.I; }
160-
bool operator!=(const ConstIterator& X) const { return I != X.I; }
163+
friend bool operator==(const ConstIterator &X, const ConstIterator &Y) {
164+
return X.I == Y.I;
165+
}
166+
friend bool operator!=(const ConstIterator &X, const ConstIterator &Y) {
167+
return X.I != Y.I;
168+
}
161169
};
162170

163171
using iterator = Iterator;
@@ -174,6 +182,11 @@ class DenseSetImpl {
174182
return ConstIterator(TheMap.find(V));
175183
}
176184

185+
/// Check if the set contains the given element.
186+
bool contains(const_arg_type_t<ValueT> V) const {
187+
return TheMap.find(V) != TheMap.end();
188+
}
189+
177190
/// Alternative version of find() which allows a different, and possibly less
178191
/// expensive, key type.
179192
/// The DenseMapInfo is responsible for supplying methods

0 commit comments

Comments
 (0)