Skip to content

Commit dfd0cb5

Browse files
committed
[Heavy] Add source location argument for create-op syntax
1 parent d61fa95 commit dfd0cb5

File tree

2 files changed

+95
-14
lines changed

2 files changed

+95
-14
lines changed

heavy/lib/Mlir.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,22 @@ namespace heavy::mlir_bind {
7070
// Provide function to support create_op syntax.
7171
// Require type checking as a precondition.
7272
// (%create-op _name_
73+
// _loc_
7374
// _attrs_ : vector?
7475
// _operands_ : vector?
7576
// _regions_ : number?
7677
// _result_types_ : vector?
7778
// _successors_ : vector?)
7879
void create_op_impl(Context& C, ValueRefs Args) {
79-
if (Args.size() != 6)
80+
if (Args.size() != 7)
8081
return C.RaiseError("invalid arity");
8182

82-
heavy::Vector* Attributes = cast<heavy::Vector>(Args[1]);
83-
heavy::Vector* Operands = cast<heavy::Vector>(Args[2]);
84-
int NumRegions = cast<heavy::Int>(Args[3]);
85-
heavy::Vector* ResultTypes = cast<heavy::Vector>(Args[4]);
86-
heavy::Vector* Successors = cast<heavy::Vector>(Args[5]);
83+
heavy::SourceLocation Loc = Args[1].getSourceLocation();
84+
heavy::Vector* Attributes = cast<heavy::Vector>(Args[2]);
85+
heavy::Vector* Operands = cast<heavy::Vector>(Args[3]);
86+
int NumRegions = cast<heavy::Int>(Args[4]);
87+
heavy::Vector* ResultTypes = cast<heavy::Vector>(Args[5]);
88+
heavy::Vector* Successors = cast<heavy::Vector>(Args[6]);
8789

8890
llvm::StringRef OpName = Args[0].getStringRef();
8991
if (OpName.empty())
@@ -94,7 +96,8 @@ void create_op_impl(Context& C, ValueRefs Args) {
9496
mlir::OpBuilder* Builder = getCurrentBuilder(C);
9597
if (!Builder) return;
9698

97-
heavy::SourceLocation Loc = C.getLoc();
99+
if (!Loc.isValid())
100+
Loc = C.getLoc();
98101
mlir::Location MLoc = mlir::OpaqueLoc::get(Loc.getOpaqueEncoding(),
99102
Builder->getContext());
100103
auto OpState = mlir::OperationState(MLoc, OpName);
@@ -189,6 +192,7 @@ void create_op(Context& C, ValueRefs Args) { // Syntax
189192
heavy::SourceLocation CallLoc = cast<Pair>(Args[0])
190193
->Car.getSourceLocation();
191194
heavy::OpGen& OpGen = *C.OpGen;
195+
heavy::Value SpecifiedLoc = heavy::Empty();
192196
heavy::Value Attributes = heavy::Empty();
193197
heavy::Value Operands = heavy::Empty();
194198
heavy::Value NumRegions = heavy::Int(0);
@@ -214,24 +218,31 @@ void create_op(Context& C, ValueRefs Args) { // Syntax
214218
Arg = P->Cdr;
215219
}
216220

217-
if (ArgName == "attributes")
221+
if (ArgName == "loc") {
222+
SpecifiedLoc = Arg;
223+
// Support improper list.
224+
if (auto* PArg = dyn_cast<Pair>(Arg))
225+
SpecifiedLoc = PArg->Car;
226+
else
227+
SpecifiedLoc = Arg;
228+
} else if (ArgName == "attributes") {
218229
Attributes = Arg;
219-
else if (ArgName == "operands")
230+
} else if (ArgName == "operands") {
220231
Operands = Arg;
221-
else if (ArgName == "regions") {
232+
} else if (ArgName == "regions") {
222233
// Support improper list.
223234
if (auto* PArg = dyn_cast<Pair>(Arg))
224235
NumRegions = PArg->Car;
225236
else
226237
NumRegions = Arg;
227-
}
228-
else if (ArgName == "result-types")
238+
} else if (ArgName == "result-types") {
229239
ResultTypes = Arg;
230-
else if (ArgName == "successors")
240+
} else if (ArgName == "successors") {
231241
Successors = Arg;
232-
else
242+
} else {
233243
return C.RaiseError("expecting named argument "
234244
"[attributes, operands, regions, result-types, successors]");
245+
}
235246
}
236247

237248
llvm::SmallVector<mlir::Value, 5> Vals;
@@ -256,6 +267,7 @@ void create_op(Context& C, ValueRefs Args) { // Syntax
256267

257268
mlir::Value AttrVals = createInputVector(Attributes);
258269
mlir::Value OperandVals = createInputVector(Operands);
270+
mlir::Value SpecifiedLocVal = OpGen.GetSingleResult(SpecifiedLoc);
259271
mlir::Value NumRegionsVal = OpGen.GetSingleResult(NumRegions);
260272
if (OpGen.CheckError())
261273
return;
@@ -265,6 +277,7 @@ void create_op(Context& C, ValueRefs Args) { // Syntax
265277
assert(Vals.empty());
266278
// Reuse Vals as arguments to %create-op
267279
Vals.push_back(OpName);
280+
Vals.push_back(SpecifiedLocVal);
268281
Vals.push_back(AttrVals);
269282
Vals.push_back(OperandVals);
270283
Vals.push_back(NumRegionsVal);

heavy/test/Nbdl/context_error.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// RUN: clang++ --std=c++23 -I %S/Inputs -fsyntax-only -Xclang -fheavy -Xclang -verify %s
2+
3+
heavy_scheme {
4+
(import (heavy base)
5+
(heavy clang)
6+
(heavy mlir)
7+
(nbdl comp))
8+
9+
(load-dialect "func")
10+
(load-dialect "heavy")
11+
(load-dialect "nbdl")
12+
13+
(define !nbdl.opaque (type "!nbdl.opaque"))
14+
(define !nbdl.store (type "!nbdl.store"))
15+
(define !nbdl.tag (type "!nbdl.tag"))
16+
(define !nbdl.symbol (type "!nbdl.symbol"))
17+
(define !nbdl.empty (type "!nbdl.empty"))
18+
(define i32 (type "i32"))
19+
20+
(define (build-member-name name)
21+
(result
22+
(create-op "nbdl.member_name"
23+
(attributes
24+
`("name", (string-attr name)))
25+
(result-types !nbdl.symbol))))
26+
27+
(%build-context
28+
'my_context
29+
0 ; num_params
30+
(lambda ()
31+
(define parent
32+
(result
33+
(create-op "nbdl.empty"
34+
(result-types !nbdl.empty))))
35+
(define (build-member key typename . init-args)
36+
(define store
37+
(result
38+
(create-op "nbdl.store"
39+
(loc typename)
40+
(attributes `("name", (flat-symbolref-attr typename)))
41+
(operands init-args)
42+
(result-types !nbdl.store)
43+
)))
44+
(set! parent
45+
(result
46+
(create-op "nbdl.store_compose"
47+
(loc typename)
48+
(operands key store parent)
49+
(result-types !nbdl.store)))))
50+
(define foo-input
51+
(result
52+
(create-op "nbdl.literal"
53+
(attributes
54+
`("value", (attr "42" i32)))
55+
(result-types !nbdl.opaque))))
56+
57+
(build-member (build-member-name 'foo)
58+
'::moo::foo_t)
59+
(create-op "nbdl.cont"
60+
(operands parent))
61+
))
62+
(define my_context
63+
(module-lookup current-nbdl-module "my_context"))
64+
(translate-cpp my_context lexer-writer)
65+
}
66+
// expected-error@-8 {{no member named 'moo'}}
67+
68+
int main() { }

0 commit comments

Comments
 (0)