Skip to content

Commit 8de4cdd

Browse files
committed
[Heavy] Fix error messages; Improve create-op syntax
1 parent e9c48dc commit 8de4cdd

File tree

6 files changed

+60
-55
lines changed

6 files changed

+60
-55
lines changed

heavy/include/heavy/Value.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ class Module : public ValueBase {
16411641
}
16421642

16431643
void Insert(String* Id, String* MangledName) {
1644-
assert(!MangledName->getStringRef().empty());
1644+
// assert(!MangledName->getStringRef().empty());
16451645
Map[Id] = MangledName;
16461646
}
16471647

heavy/include/heavy/mlir.sld

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
(map InitRegion Is BlockArgTypesList UserFns))
2222

2323
(define-syntax create-op
24-
(syntax-rules (: loc attributes operands result-types region)
24+
(syntax-rules (: loc: attributes: operands: result-types: region:)
2525
((create-op Name
26-
(loc Loc)
27-
(operands Operands ...)
28-
(attributes (AttrName Attr) ...)
29-
(result-types ResultTypes ...)
30-
(region RegionName ((BlockArg : BlockArgType) ...)
26+
(loc: Loc)
27+
(operands: Operands ...)
28+
(attributes: (AttrName Attr) ...)
29+
(result-types: ResultTypes ...)
30+
(region: RegionName ((BlockArg : BlockArgType) ...)
3131
RegionBody ...) ...)
3232
(let ((Op
3333
(old-create-op Name

heavy/lib/Context.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,10 +1302,10 @@ void Context::Raise(Value Obj) {
13021302

13031303
void Context::RaiseError(String* Msg, llvm::ArrayRef<Value> IrrArgs) {
13041304
Value IrrList = Empty();
1305-
for (Value Irr : llvm::reverse(IrrArgs))
1305+
for (Value Irr : llvm::reverse(IrrArgs)) {
13061306
setLoc(Irr.getSourceLocation());
1307-
for (Value Irr : IrrArgs)
13081307
IrrList = CreatePair(Irr, IrrList);
1308+
}
13091309
Value Error = CreateError(this->Loc, Msg, IrrList);
13101310
Raise(Error);
13111311
}

heavy/lib/Mlir.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void create_op_impl(Context& C, ValueRefs Args) {
9999

100100
llvm::StringRef OpName = Args[0].getStringRef();
101101
if (OpName.empty())
102-
return C.RaiseError("expecting operation name");
102+
return C.RaiseError("expecting operation name: {}", Args[0]);
103103

104104
Args = Args.drop_front();
105105

@@ -114,14 +114,15 @@ void create_op_impl(Context& C, ValueRefs Args) {
114114
llvm::StringRef Name;
115115
if (auto* P = dyn_cast<heavy::Pair>(V)) {
116116
Name = P->Car.getStringRef();
117+
if (Name.empty())
118+
return C.RaiseError(
119+
"expecting name-value pair for attribute: {}", Value(P));
117120
if (auto* P2 = dyn_cast<heavy::Pair>(P->Cdr)) {
118121
V = P2->Car;
119122
if (!isa<heavy::Empty>(P2->Cdr))
120123
Name = ""; // Clear the name so we raise error below.
121124
}
122125
}
123-
if (Name.empty())
124-
return C.RaiseError("expecting name-value pair for attribute", V);
125126

126127
auto Attr = any_cast<mlir::Attribute>(V);
127128

@@ -141,14 +142,14 @@ void create_op_impl(Context& C, ValueRefs Args) {
141142
for (heavy::Value V2 : V) {
142143
auto MVal = any_cast<mlir::Value>(V2);
143144
if (!MVal)
144-
return C.RaiseError("expecting mlir.value", V2);
145+
return C.RaiseError("expecting mlir.value: {}", V2);
145146
OpState.operands.push_back(MVal);
146147
}
147148
break;
148149
}
149150
auto MVal = any_cast<mlir::Value>(V);
150151
if (!MVal)
151-
return C.RaiseError("expecting mlir.value", V);
152+
return C.RaiseError("expecting mlir.value: {}", V);
152153
OpState.operands.push_back(MVal);
153154
}
154155

@@ -160,15 +161,15 @@ void create_op_impl(Context& C, ValueRefs Args) {
160161
for (heavy::Value V : ResultTypes->getElements()) {
161162
auto MType = any_cast<mlir::Type>(V);
162163
if (!MType)
163-
return C.RaiseError("expecting mlir.type", V);
164+
return C.RaiseError("expecting mlir.type: {}", V);
164165
OpState.types.push_back(MType);
165166
}
166167

167168
// successors
168169
for (heavy::Value V : Successors->getElements()) {
169170
mlir::Block* Block = any_cast<mlir::Block*>(V);
170171
if (Block == nullptr)
171-
return C.RaiseError("expecting mlir.block", V);
172+
return C.RaiseError("expecting mlir.block: {}", V);
172173
OpState.successors.push_back(Block);
173174
}
174175

heavy/lib/OpGen.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,8 @@ mlir::Value OpGen::createSyntaxRules(SourceLocation Loc,
846846

847847
// Create a sequence of operations in the current block.
848848
mlir::Value OpGen::createSequence(SourceLocation Loc, Value Body) {
849+
if (CheckError())
850+
return mlir::Value();
849851
if (!isa<Pair>(Body)) {
850852
return SetError(Loc, "sequence must contain an expression", Body);
851853
}
@@ -922,6 +924,8 @@ bool OpGen::WalkDefineInits(Value Env, IdSet& LocalIds) {
922924
}
923925

924926
void OpGen::FinishLocalDefines() {
927+
if (CheckError())
928+
return;
925929
TailPosScope TPS(*this);
926930
IsTailPos = false;
927931
IsLocalDefineAllowed = false;

heavy/test/Evaluate/create-op.scm

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,42 @@
77
; CHECK: #op{%0 = "heavy.literal"() {info = #heavy<"\22foo\22">} : () -> !heavy.value
88
(write
99
(create-op "heavy.literal"
10-
(loc 0)
11-
(operands)
12-
(attributes
13-
(info (value-attr "foo")))
14-
(result-types !heavy.value)))
10+
(loc: 0)
11+
(operands:)
12+
(attributes:
13+
("info" (value-attr "foo")))
14+
(result-types: !heavy.value)))
1515

1616
(newline)
1717

1818
; CHECK: #op{%0 = "heavy.literal"() {info = #heavy<"5">} : () -> !heavy.value
1919
(write
2020
(create-op "heavy.literal"
21-
(loc 0)
22-
(operands)
23-
(attributes
24-
(info (value-attr 5)))
25-
(result-types !heavy.value)
21+
(loc: 0)
22+
(operands:)
23+
(attributes:
24+
("info" (value-attr 5)))
25+
(result-types: !heavy.value)
2626
))
2727

2828
(newline)
2929

3030
; CHECK: #op{%0 = "heavy.literal"() {info = #heavy<"5000">} : () -> !heavy.value
3131
(write
3232
(create-op "heavy.literal"
33-
(loc 0)
34-
(operands)
35-
(attributes
36-
(info (attr "#heavy<\"5000\">" !heavy.value)))
37-
(result-types !heavy.value)))
33+
(loc: 0)
34+
(operands:)
35+
(attributes:
36+
("info" (attr "#heavy<\"5000\">" !heavy.value)))
37+
(result-types: !heavy.value)))
3838

3939
(define the-answer
4040
(create-op "heavy.literal"
41-
(loc 0)
42-
(operands)
43-
(attributes
44-
(info (value-attr 41)))
45-
(result-types !heavy.value)))
41+
(loc: 0)
42+
(operands:)
43+
(attributes:
44+
("info" (value-attr 41)))
45+
(result-types: !heavy.value)))
4646

4747
(newline)
4848
; CHECK: non-parent:()
@@ -60,31 +60,31 @@
6060

6161
(define command
6262
(create-op "heavy.command"
63-
(loc 0)
64-
(operands)
65-
(attributes)
66-
(result-types)
67-
(region "body" ()
63+
(loc: 0)
64+
(operands:)
65+
(attributes:)
66+
(result-types:)
67+
(region: "body" ()
6868
(define callee
6969
(create-op "heavy.load_global"
70-
(loc 0)
71-
(operands)
72-
(attributes
73-
(name (flat-symbolref-attr "_HEAVYL5SheavyL4SbaseV5Swrite")))
74-
(result-types !heavy.value)
70+
(loc: 0)
71+
(operands:)
72+
(attributes:
73+
("name" (flat-symbolref-attr "_HEAVYL5SheavyL4SbaseV5Swrite")))
74+
(result-types: !heavy.value)
7575
))
7676
(define arg1
7777
(create-op "heavy.literal"
78-
(loc 0)
79-
(operands)
80-
(attributes
81-
(info (value-attr 42)))
82-
(result-types !heavy.value)))
78+
(loc: 0)
79+
(operands:)
80+
(attributes:
81+
("info" (value-attr 42)))
82+
(result-types: !heavy.value)))
8383
(create-op "heavy.apply"
84-
(loc 0)
85-
(operands (result callee) (result arg1))
86-
(attributes)
87-
(result-types))
84+
(loc: 0)
85+
(operands: (result callee) (result arg1))
86+
(attributes:)
87+
(result-types:))
8888
)))
8989
(write command)
9090
(newline)

0 commit comments

Comments
 (0)