Skip to content

Commit f64fb8a

Browse files
committed
[Heavy] Fix nbdl ADT identity types
1 parent d2542fa commit f64fb8a

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

heavy/include/nbdl_gen/Nbdl.td

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@ def Nbdl_Dialect : Dialect {
2121
def Nbdl_Empty : TypeDef<Nbdl_Dialect, "Empty", []> {
2222
let mnemonic = "empty";
2323
let description = [{
24-
We define empty_type as a type with no information either
25-
compile-time or run-time.
24+
We define empty_type as a type whose with no
25+
state either compile-time or run-time.
2626

27-
This serves as the identity element for product types.
27+
This serves as the identity element for sum types.
2828
}];
2929
}
3030

31-
def Nbdl_Void : TypeDef<Nbdl_Dialect, "Void", []> {
32-
let mnemonic = "void";
31+
def Nbdl_Unit : TypeDef<Nbdl_Dialect, "Unit", []> {
32+
let mnemonic = "unit";
3333
let description = [{
34-
We define void_type as a unit type with no run-time information
35-
and no run-time instantiation.
34+
We define unit_type to have a single possible
35+
compile-time and run-time state.
3636

37-
This serves as the identity element for sum types.
37+
This serves as the identity element for product types.
3838
}];
3939
}
4040

@@ -113,9 +113,9 @@ def Nbdl_Symbol : TypeDef<Nbdl_Dialect, "Symbol"> {
113113
// Keys are optional, but we are using variadic arguments
114114
// so we resort to using this sum type.
115115
def Nbdl_KeyType : AnyTypeOf<[Nbdl_Opaque, Nbdl_Tag,
116-
Nbdl_Symbol, Nbdl_Empty]>;
116+
Nbdl_Symbol, Nbdl_Unit]>;
117117

118-
def Nbdl_Type : AnyTypeOf<[Nbdl_Void, Nbdl_Empty, Nbdl_Opaque,
118+
def Nbdl_Type : AnyTypeOf<[Nbdl_Unit, Nbdl_Empty, Nbdl_Opaque,
119119
Nbdl_Store, Nbdl_Tag]>;
120120

121121
class Nbdl_Op<string mnemonic, list<Trait> traits = []> :
@@ -312,6 +312,15 @@ def Nbdl_EmptyOp : Nbdl_Op<"empty", []> {
312312
let results = (outs Nbdl_Empty:$result);
313313
}
314314

315+
def Nbdl_UnitOp : Nbdl_Op<"unit", []> {
316+
let description = [{
317+
An operation that produces an object of the unit type.
318+
}];
319+
320+
let arguments = (ins);
321+
let results = (outs Nbdl_Unit:$result);
322+
}
323+
315324
def Nbdl_ApplyOp : Nbdl_Op<"apply", []> {
316325
let description = [{
317326
Apply a function.

heavy/lib/Nbdl/NbdlWriter.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class NbdlWriter {
209209
else if (isa<ConstexprOp>(Op))
210210
return VisitType(cast<ConstexprOp>(Op));
211211
else
212-
SetError("unhandled operation (VisitType)", Op);
212+
SetError("unhandled operation (VisitType): {}", Op);
213213
} else {
214214
// Handle mlir::BlockArgument.
215215
// Arguments cannot be `decltype(auto)`
@@ -285,8 +285,11 @@ class FuncWriter : public NbdlWriter<FuncWriter> {
285285
else if (isa<FuncOp>(Op)) return Visit(cast<FuncOp>(Op));
286286
else if (isa<MemberNameOp>(Op)) return Visit(cast<MemberNameOp>(Op));
287287
else if (isa<ConstexprOp, LiteralOp>(Op)) return;
288+
else if (isa<UnitOp>(Op)) return;
289+
else if (isa<EmptyOp>(Op))
290+
return SetError("empty type does not map to c++", Op);
288291
else
289-
SetError("unhandled operation", Op);
292+
return SetError("unhandled operation: {}", Op);
290293
}
291294

292295
void VisitRegion(mlir::Region& R) {
@@ -342,8 +345,10 @@ class FuncWriter : public NbdlWriter<FuncWriter> {
342345
} else {
343346
OS << "nbdl::get(";
344347
WriteForwardedExpr(Op.getState());
345-
OS << ',';
346-
WriteForwardedExpr(Op.getKey());
348+
if (!isa<nbdl_gen::UnitType>(Op.getKey().getType())) {
349+
OS << ", ";
350+
WriteForwardedExpr(Op.getKey());
351+
}
347352
OS << ");\n";
348353
}
349354
}
@@ -361,8 +366,10 @@ class FuncWriter : public NbdlWriter<FuncWriter> {
361366
void Visit(MatchOp Op) {
362367
OS << "nbdl::match(";
363368
WriteForwardedExpr(Op.getStore());
364-
OS << ", ";
365-
WriteForwardedExpr(Op.getKey());
369+
if (!isa<nbdl_gen::UnitType>(Op.getKey().getType())) {
370+
OS << ", ";
371+
WriteForwardedExpr(Op.getKey());
372+
}
366373
OS << ", ";
367374
OS << "\nboost::hana::overload_linearly(";
368375

@@ -494,7 +501,7 @@ class ContextWriter : public NbdlWriter<ContextWriter> {
494501
return;
495502
} else {
496503
mlir::Operation* Irr = Val.getDefiningOp();
497-
SetError("unhandled operation", Irr);
504+
SetError("unhandled operation: (WriteMemberDecl) {}", Irr);
498505
}
499506
}
500507

@@ -533,7 +540,6 @@ class ContextWriter : public NbdlWriter<ContextWriter> {
533540
if (!ContOp)
534541
return;
535542

536-
537543
OS << Op.getName();
538544
OS << '(';
539545
llvm::interleaveComma(Op.getBody().getArguments(), OS,

0 commit comments

Comments
 (0)