Skip to content

Commit 754ee83

Browse files
committed
[Heavy] Add MemberOp
1 parent f594d64 commit 754ee83

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

heavy/include/nbdl_gen/Nbdl.td

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def Nbdl_Dialect : Dialect {
1818
let useDefaultTypePrinterParser = 1;
1919
}
2020

21+
// FIXME Maybe we should just use MLIR's NoneType for this.
2122
def Nbdl_Empty : TypeDef<Nbdl_Dialect, "Empty", []> {
2223
let mnemonic = "empty";
2324
let description = [{
@@ -52,7 +53,21 @@ class Nbdl_TypeBase<string name, string type_mnemonic,
5253
def Nbdl_Opaque : TypeDef<Nbdl_Dialect, "Opaque", []> {
5354
let mnemonic = "opaque";
5455
let description = [{
55-
Represent an unknown C++ type.
56+
Represent an placeholder C++ type (typically `auto&&`).
57+
Values of this type imply an immediate construction.
58+
That is, it represents a run-time value in the context
59+
where it appears (e.g. an argument to a function).
60+
}];
61+
}
62+
63+
def Nbdl_Store : TypeDef<Nbdl_Dialect, "Store", []> {
64+
let mnemonic = "store";
65+
let description = [{
66+
An Object of store type represents a member of our state graph.
67+
Since a store object can exist as an inactive alternative
68+
of a variant, it is not necessarily have an immediate
69+
construction. Hence, we require it to be semiregular.
70+
(FIXME or just default constructible?)
5671
}];
5772
}
5873

@@ -64,7 +79,6 @@ def Nbdl_Struct : Nbdl_TypeBase<"Struct", "struct"> {
6479
are accessed by their name.
6580
}];
6681
}
67-
*/
6882

6983
def Nbdl_State : TypeDef<Nbdl_Dialect, "State", []> {
7084
let mnemonic = "state";
@@ -73,19 +87,13 @@ def Nbdl_State : TypeDef<Nbdl_Dialect, "State", []> {
7387
}];
7488
}
7589

76-
def Nbdl_Store : TypeDef<Nbdl_Dialect, "Store", []> {
77-
let mnemonic = "store";
78-
let description = [{
79-
A type satisfying the nbdl::Store concept
80-
}];
81-
}
82-
8390
def Nbdl_Variant : TypeDef<Nbdl_Dialect, "Variant", []> {
8491
let mnemonic = "variant";
8592
let description = [{
8693
A type satisfying the nbdl::Variant concept
8794
}];
8895
}
96+
*/
8997

9098
def Nbdl_Tag : TypeDef<Nbdl_Dialect, "Tag", []> {
9199
let mnemonic = "tag";
@@ -95,12 +103,11 @@ def Nbdl_Tag : TypeDef<Nbdl_Dialect, "Tag", []> {
95103
}];
96104
}
97105

98-
def Nbdl_Symbol : Nbdl_TypeBase<"Symbol", "symbol"> {
106+
def Nbdl_Symbol : TypeDef<Nbdl_Dialect, "Symbol"> {
107+
let mnemonic = "symbol";
99108
let description = [{
100109
Represent a string that is a valid C++ identifier.
101110
The intended primary use case is to specify a member of a Struct.
102-
103-
FIXME using this?
104111
}];
105112
}
106113

@@ -110,7 +117,7 @@ def Nbdl_KeyType : AnyTypeOf<[Nbdl_Opaque, Nbdl_Tag,
110117
Nbdl_Symbol, NoneType]>;
111118

112119
def Nbdl_Type : AnyTypeOf<[Nbdl_Void, Nbdl_Empty, Nbdl_Opaque,
113-
Nbdl_Store, Nbdl_State, Nbdl_Tag]>;
120+
Nbdl_Store, Nbdl_Tag]>;
114121

115122
class Nbdl_Op<string mnemonic, list<Trait> traits = []> :
116123
Op<Nbdl_Dialect, mnemonic, traits>;
@@ -209,6 +216,15 @@ def Nbdl_ConstexprOp : Nbdl_Op<"constexpr", []> {
209216
let results = (outs Nbdl_Type:$result);
210217
}
211218

219+
def Nbdl_MemberNameOp : Nbdl_Op<"member_name", []> {
220+
let description = [{
221+
Denote a member name as a value.
222+
}];
223+
224+
let arguments = (ins StrAttr:$name);
225+
let results = (outs Nbdl_Symbol:$result);
226+
}
227+
212228
def Nbdl_StoreComposeOp : Nbdl_Op<"store_compose", []> {
213229
let description = [{
214230
Add a store $lhs as a member to another store $rhs using a key $key
@@ -231,7 +247,7 @@ def Nbdl_VariantOp : Nbdl_Op<"variant", []> {
231247
Create a sum type.
232248
}];
233249
let arguments = (ins Variadic<Nbdl_Type>:$args);
234-
let results = (outs Nbdl_Variant:$result);
250+
let results = (outs Nbdl_Type:$result);
235251
}
236252

237253
def Nbdl_CreateStoreOp : Nbdl_Op<"create_store", [Symbol, IsolatedFromAbove]> {

heavy/lib/NbdlWriter.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class NbdlWriter {
148148
else if (isa<ConstexprOp>(Op)) return Visit(cast<ConstexprOp>(Op));
149149
else if (isa<FuncOp>(Op)) return Visit(cast<FuncOp>(Op));
150150
else if (isa<LiteralOp>(Op)) return Visit(cast<LiteralOp>(Op));
151+
else if (isa<MemberNameOp>(Op)) return Visit(cast<MemberNameOp>(Op));
151152
else
152153
SetError("unhandled operation", Op);
153154
}
@@ -281,13 +282,21 @@ class NbdlWriter {
281282
}
282283

283284
void Visit(GetOp Op) {
284-
OS << "decltype(auto) "
285-
<< SetLocalVarName(Op.getResult(), "get_")
286-
<< " = nbdl::get(";
287-
WriteForwardedExpr(Op.getState());
288-
OS << ',';
289-
WriteForwardedExpr(Op.getKey());
290-
OS << ");\n";
285+
auto MemberNameOp = Op.getKey().getDefiningOp<nbdl_gen::MemberNameOp>();
286+
OS << "decltype(auto) "
287+
<< SetLocalVarName(Op.getResult(), "get_")
288+
<< " = ";
289+
if (MemberNameOp) {
290+
WriteForwardedExpr(Op.getState());
291+
OS << '.' << MemberNameOp.getName()
292+
<< ";\n";
293+
} else {
294+
OS << "nbdl::get(";
295+
WriteForwardedExpr(Op.getState());
296+
OS << ',';
297+
WriteForwardedExpr(Op.getKey());
298+
OS << ");\n";
299+
}
291300
}
292301

293302
void Visit(VisitOp Op) {
@@ -360,6 +369,13 @@ class NbdlWriter {
360369
OS << ");\n";
361370
}
362371

372+
void Visit(MemberNameOp Op) {
373+
// Member name is meaningless without the parent object
374+
// so we print it in GetOp.
375+
// We could implement in MatchOp, but it is a very
376+
// unlikely use case.
377+
}
378+
363379
void Visit(NoOp Op) {
364380
// Do nothing.
365381
}

0 commit comments

Comments
 (0)