@@ -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?)
7879void 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);
0 commit comments