Skip to content

Commit 8aaea2b

Browse files
committed
RequirementMachine: Preliminary support for layout requirements
1 parent bc5e26b commit 8aaea2b

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

include/swift/AST/LayoutConstraint.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ class LayoutConstraint {
284284
bool operator!=(LayoutConstraint rhs) const {
285285
return !(*this == rhs);
286286
}
287+
288+
/// Defines a somewhat arbitrary linear order on layout constraints.
289+
/// -1 if this < rhs, 0 if this == rhs, 1 if this > rhs.
290+
int compare(LayoutConstraint rhs) const;
287291
};
288292

289293
// Permit direct uses of isa/cast/dyn_cast on LayoutConstraint.

include/swift/AST/RewriteSystem.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "swift/AST/Decl.h"
1717
#include "swift/AST/Identifier.h"
18+
#include "swift/AST/LayoutConstraint.h"
1819
#include "swift/AST/Types.h"
1920
#include "llvm/ADT/PointerUnion.h"
2021
#include "llvm/ADT/SmallVector.h"
@@ -31,7 +32,9 @@ using ProtocolOrder = std::function<int (const ProtocolDecl *,
3132
const ProtocolDecl *)>;
3233

3334
class Atom final {
34-
using Storage = llvm::PointerUnion<Identifier, GenericTypeParamType *>;
35+
using Storage = llvm::PointerUnion<Identifier,
36+
GenericTypeParamType *,
37+
LayoutConstraint>;
3538

3639
const ProtocolDecl *Proto;
3740
Storage Value;
@@ -62,11 +65,17 @@ class Atom final {
6265
return Atom(nullptr, param);
6366
}
6467

68+
static Atom forLayout(LayoutConstraint layout) {
69+
assert(layout->isKnownLayout());
70+
return Atom(nullptr, layout);
71+
}
72+
6573
enum class Kind : uint8_t {
6674
AssociatedType,
6775
GenericParam,
6876
Name,
6977
Protocol,
78+
Layout
7079
};
7180

7281
Kind getKind() const {
@@ -86,6 +95,11 @@ class Atom final {
8695
return Kind::GenericParam;
8796
}
8897

98+
if (Value.is<LayoutConstraint>()) {
99+
assert(Proto == nullptr);
100+
return Kind::Layout;
101+
}
102+
89103
llvm_unreachable("Bad term rewriting atom");
90104
}
91105

@@ -106,6 +120,11 @@ class Atom final {
106120
return Value.get<GenericTypeParamType *>();
107121
}
108122

123+
LayoutConstraint getLayoutConstraint() const {
124+
assert(getKind() == Kind::Layout);
125+
return Value.get<LayoutConstraint>();
126+
}
127+
109128
int compare(Atom other, ProtocolOrder compare) const;
110129

111130
void dump(llvm::raw_ostream &out) const;

lib/AST/LayoutConstraint.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,18 @@ LayoutConstraintInfo LayoutConstraintInfo::NativeClassConstraintInfo(
358358
LayoutConstraintInfo LayoutConstraintInfo::TrivialConstraintInfo(
359359
LayoutConstraintKind::Trivial);
360360

361+
int LayoutConstraint::compare(LayoutConstraint rhs) const {
362+
if (Ptr->getKind() != rhs->getKind())
363+
return int(rhs->getKind()) - int(Ptr->getKind());
364+
365+
if (Ptr->SizeInBits != rhs->SizeInBits)
366+
return int(rhs->SizeInBits) - int(Ptr->SizeInBits);
367+
368+
if (Ptr->Alignment != rhs->Alignment)
369+
return int(rhs->Alignment) - int(Ptr->Alignment);
370+
371+
assert(*this == rhs);
372+
return 0;
373+
}
374+
361375
} // end namespace swift

lib/AST/RequirementMachine.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,13 @@ void RewriteSystemBuilder::addRequirement(const Requirement &req,
284284
}
285285
case RequirementKind::Superclass:
286286
break;
287-
case RequirementKind::Layout:
287+
case RequirementKind::Layout: {
288+
auto constraintTerm = subjectTerm;
289+
constraintTerm.add(Atom::forLayout(req.getLayoutConstraint()));
290+
291+
Rules.emplace_back(subjectTerm, constraintTerm);
288292
break;
293+
}
289294
case RequirementKind::SameType: {
290295
auto otherType = CanType(req.getSecondType());
291296

lib/AST/RewriteSystem.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ int Atom::compare(Atom other, ProtocolOrder protocolOrder) const {
5353

5454
return 0;
5555
}
56+
57+
case Kind::Layout: {
58+
return getLayoutConstraint().compare(other.getLayoutConstraint());
59+
}
5660
}
5761

5862
llvm_unreachable("Bad atom kind");
@@ -77,6 +81,11 @@ void Atom::dump(llvm::raw_ostream &out) const {
7781
case Kind::GenericParam:
7882
out << Type(getGenericParam());
7983
return;
84+
85+
case Kind::Layout:
86+
out << "[layout: ";
87+
getLayoutConstraint()->print(out);
88+
out << "]";
8089
}
8190

8291
llvm_unreachable("Bad atom kind");

0 commit comments

Comments
 (0)