Skip to content

Commit 0734b43

Browse files
committed
[Heavy] Replace Tagged with Any
1 parent 0f06425 commit 0734b43

File tree

11 files changed

+211
-141
lines changed

11 files changed

+211
-141
lines changed

heavy/include/heavy/Context.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,15 @@ class Context : public ContinuationStack<Context>,
343343
EnvFrame* CreateEnvFrame(llvm::ArrayRef<Symbol*> Names);
344344

345345
template <typename T>
346-
Tagged* CreateTagged(Symbol* Sym, T Obj) {
346+
Any* CreateAny(T Obj) {
347347
static_assert(alignof(T) <= alignof(Value),
348348
"object storage alignment is too large");
349349
static_assert(std::is_trivially_copyable<T>::value,
350350
"F must be trivially_copyable");
351351
llvm::StringRef ObjData(reinterpret_cast<char const*>(&Obj), sizeof(Obj));
352-
void* Mem = Tagged::allocate(getAllocator(), ObjData);
353-
Tagged* New = new (Mem) Tagged(Sym, ObjData);
352+
void* Mem = Any::allocate(getAllocator(), ObjData);
353+
void const* TypeId = &AnyTypeId<T>::Id;
354+
Any* New = new (Mem) Any(TypeId, ObjData);
354355

355356
return New;
356357
}

heavy/include/heavy/MlirHelper.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,6 @@ extern heavy::ContextLocal current_builder;
3030
}
3131

3232
namespace heavy::mlir_helper {
33-
namespace kind {
34-
// Omit mlir.op since mlir::Operation* is already embedded in heavy::Value.
35-
constexpr char const* mlir_context = "mlir.context";
36-
constexpr char const* mlir_type = "mlir.type";
37-
constexpr char const* mlir_attr = "mlir.attr";
38-
constexpr char const* mlir_region = "mlir.region";
39-
constexpr char const* mlir_block = "mlir.block";
40-
constexpr char const* mlir_value = "mlir.value";
41-
constexpr char const* mlir_builder = "mlir.builder"; // OpBuilder
42-
}
43-
44-
// Create OpaquePtr tagged with a string for mlir objects.
45-
template <typename T>
46-
heavy::Value createTagged(heavy::Context& C, llvm::StringRef Kind, T Obj) {
47-
return C.CreateTagged(C.CreateSymbol(Kind), Obj);
48-
}
49-
50-
// Get mlir Type/Attribute from tagged OpaquePtr.
51-
template <typename T>
52-
T getTagged(heavy::Context& C, llvm::StringRef Kind, heavy::Value Value) {
53-
if (auto* Tagged = heavy::dyn_cast<heavy::Tagged>(Value)) {
54-
heavy::Symbol* KindSym = C.CreateSymbol(Kind);
55-
if (Tagged->isa(KindSym))
56-
return Tagged->cast<T>();
57-
}
58-
59-
return T(nullptr);
60-
}
61-
6233
mlir::MLIRContext* getCurrentContext(heavy::Context& C);
6334
mlir::OpBuilder* getBuilder(heavy::Context& C, heavy::Value V);
6435
mlir::OpBuilder* getCurrentBuilder(heavy::Context& C);

heavy/include/heavy/Value.h

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ enum class ValueKind {
134134
Symbol,
135135
Syntax,
136136
SyntaxClosure,
137-
Tagged,
137+
Any,
138138
Vector,
139139
};
140140

@@ -1296,14 +1296,23 @@ class Vector final
12961296

12971297
};
12981298

1299-
class Tagged final :
1299+
1300+
template <typename T>
1301+
struct AnyTypeId {
1302+
static constexpr int Id = 0;
1303+
};
1304+
template <typename T>
1305+
constexpr int AnyTypeId<T>::Id;
1306+
1307+
class Any final :
13001308
public ValueBase,
1301-
private llvm::TrailingObjects<Tagged, char> {
1309+
private llvm::TrailingObjects<Any, char> {
13021310

1303-
friend class llvm::TrailingObjects<Tagged, char>;
1311+
friend class llvm::TrailingObjects<Any, char>;
1312+
friend class CopyCollector;
13041313
friend Context;
13051314

1306-
Symbol* Tag;
1315+
void const* TypeId;
13071316
size_t StorageLen = 0;
13081317

13091318
size_t numTrailingObjects(OverloadToken<char> const) const {
@@ -1315,9 +1324,9 @@ class Tagged final :
13151324
}
13161325

13171326
public:
1318-
Tagged(Symbol* TagSymbol, llvm::StringRef ObjData)
1319-
: ValueBase(ValueKind::Tagged),
1320-
Tag(TagSymbol),
1327+
Any(void const* TypeId, llvm::StringRef ObjData)
1328+
: ValueBase(ValueKind::Any),
1329+
TypeId(TypeId),
13211330
StorageLen(ObjData.size())
13221331
{
13231332
// Storage
@@ -1333,17 +1342,16 @@ class Tagged final :
13331342
return llvm::StringRef(static_cast<char*>(getOpaquePtr()), getObjectSize());
13341343
};
13351344

1336-
Symbol* getTag() const { return Tag; }
1337-
1338-
bool isa(Symbol* Sym) {
1339-
return Sym->getString() == Tag->getString();
1345+
template <typename T>
1346+
bool isa() {
1347+
return TypeId == &AnyTypeId<T>::Id;
13401348
}
13411349

1342-
// Return a reference to the stored object.
1343-
// It is the users responsible to check the tag.
1350+
// DEPRECATED use heavy::any_cast
13441351
template <typename T>
1345-
T& cast() {
1346-
return *reinterpret_cast<T*>(getOpaquePtr());
1352+
T* cast() {
1353+
assert(isa<T>() && "should be a T");
1354+
return static_cast<T*>(getOpaquePtr());
13471355
}
13481356

13491357
template <typename Allocator>
@@ -1354,11 +1362,28 @@ class Tagged final :
13541362
}
13551363

13561364
static bool classof(Value V) {
1357-
return V.getKind() == ValueKind::Tagged;
1365+
return V.getKind() == ValueKind::Any;
13581366
}
1359-
static ValueKind getKind() { return ValueKind::Tagged; }
1367+
static ValueKind getKind() { return ValueKind::Any; }
13601368
};
13611369

1370+
template <typename T>
1371+
T any_cast(heavy::Value V) {
1372+
if (auto* Any = dyn_cast<heavy::Any>(V))
1373+
if (Any->isa<T>())
1374+
return *Any->cast<T>();
1375+
return T{};
1376+
}
1377+
1378+
template <typename T>
1379+
T* any_cast(heavy::Value const* VP) {
1380+
heavy::Value V = *VP;
1381+
if (auto* Any = dyn_cast<heavy::Any>(V))
1382+
if (Any->isa<T>())
1383+
return Any->cast<T>();
1384+
return nullptr;
1385+
}
1386+
13621387
// EnvEntry - Used to store lookup results for
13631388
struct EnvEntry {
13641389
heavy::Value Value;
@@ -1880,7 +1905,7 @@ inline llvm::StringRef getKindName(heavy::ValueKind Kind) {
18801905
GET_KIND_NAME_CASE(Symbol)
18811906
GET_KIND_NAME_CASE(Syntax)
18821907
GET_KIND_NAME_CASE(SyntaxClosure)
1883-
GET_KIND_NAME_CASE(Tagged)
1908+
GET_KIND_NAME_CASE(Any)
18841909
GET_KIND_NAME_CASE(Vector)
18851910
default:
18861911
return llvm::StringRef("?????");
@@ -1934,9 +1959,9 @@ void* Lambda::allocate(Allocator& Alloc, OpaqueFn FnData,
19341959
}
19351960

19361961
template <typename Allocator>
1937-
void* Tagged::allocate(Allocator& Alloc, llvm::StringRef ObjData) {
1938-
size_t size = Tagged::sizeToAlloc(ObjData.size());
1939-
return heavy::allocate(Alloc, size, alignof(Tagged));
1962+
void* Any::allocate(Allocator& Alloc, llvm::StringRef ObjData) {
1963+
size_t size = Any::sizeToAlloc(ObjData.size());
1964+
return heavy::allocate(Alloc, size, alignof(Any));
19401965
}
19411966

19421967
template <typename Allocator>

heavy/include/heavy/ValueVisitor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ValueVisitor {
7575
VISIT_FN(Symbol)
7676
VISIT_FN(Syntax)
7777
VISIT_FN(SyntaxClosure)
78-
VISIT_FN(Tagged)
78+
VISIT_FN(Any)
7979
VISIT_FN(Vector)
8080

8181
template <typename ...Args>
@@ -117,7 +117,7 @@ class ValueVisitor {
117117
case ValueKind::Symbol: DISPATCH(Symbol);
118118
case ValueKind::Syntax: DISPATCH(Syntax);
119119
case ValueKind::SyntaxClosure: DISPATCH(SyntaxClosure);
120-
case ValueKind::Tagged: DISPATCH(Tagged);
120+
case ValueKind::Any: DISPATCH(Any);
121121
case ValueKind::Vector: DISPATCH(Vector);
122122
}
123123
llvm_unreachable("unhandled case");

heavy/lib/Context.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,9 @@ class Writer : public ValueVisitor<Writer> {
560560
}
561561
}
562562

563-
void VisitTagged(Tagged* T) {
564-
OS << "#tag{";
565-
OS << T->getTag()->getStringRef()
566-
<< ' '
567-
<< uintptr_t(T->getOpaquePtr())
563+
void VisitAny(Any* T) {
564+
OS << "#any{";
565+
OS << uintptr_t(T->getOpaquePtr())
568566
<< '}';
569567
}
570568

heavy/lib/Heap.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ class CopyCollector : private ValueVisitor<CopyCollector, heavy::Value> {
230230
return new (NewHeap, Xs) heavy::Vector(Xs);
231231
}
232232

233-
heavy::Value VisitTagged(heavy::Tagged* Tagged) {
234-
heavy::Symbol* Tag = cast<Symbol>(Visit(Tagged->getTag()));
235-
llvm::StringRef ObjData = Tagged->getObjData();
236-
void* Mem = Tagged::allocate(getAllocator(), ObjData);
237-
return new (Mem) heavy::Tagged(Tag, ObjData);
233+
heavy::Value VisitAny(heavy::Any* Any) {
234+
void const* TypeId = Any->TypeId;
235+
llvm::StringRef ObjData = Any->getObjData();
236+
void* Mem = Any::allocate(getAllocator(), ObjData);
237+
return new (Mem) heavy::Any(TypeId, ObjData);
238238
}
239239

240240
template <typename ...Args>

0 commit comments

Comments
 (0)