@@ -193,31 +193,6 @@ static MlirStringRef toMlirStringRef(const std::string &s) {
193
193
return mlirStringRefCreate (s.data (), s.size ());
194
194
}
195
195
196
- // / Create a block, using the current location context if no locations are
197
- // / specified.
198
- static MlirBlock createBlock (const py::sequence &pyArgTypes,
199
- const std::optional<py::sequence> &pyArgLocs) {
200
- SmallVector<MlirType> argTypes;
201
- argTypes.reserve (pyArgTypes.size ());
202
- for (const auto &pyType : pyArgTypes)
203
- argTypes.push_back (pyType.cast <PyType &>());
204
-
205
- SmallVector<MlirLocation> argLocs;
206
- if (pyArgLocs) {
207
- argLocs.reserve (pyArgLocs->size ());
208
- for (const auto &pyLoc : *pyArgLocs)
209
- argLocs.push_back (pyLoc.cast <PyLocation &>());
210
- } else if (!argTypes.empty ()) {
211
- argLocs.assign (argTypes.size (), DefaultingPyLocation::resolve ());
212
- }
213
-
214
- if (argTypes.size () != argLocs.size ())
215
- throw py::value_error ((" Expected " + Twine (argTypes.size ()) +
216
- " locations, got: " + Twine (argLocs.size ()))
217
- .str ());
218
- return mlirBlockCreate (argTypes.size (), argTypes.data (), argLocs.data ());
219
- }
220
-
221
196
// / Wrapper for the global LLVM debugging flag.
222
197
struct PyGlobalDebugFlag {
223
198
static void set (py::object &o, bool enable) { mlirEnableGlobalDebug (enable); }
@@ -389,10 +364,21 @@ class PyBlockList {
389
364
throw SetPyError (PyExc_IndexError, " attempt to access out of bounds block" );
390
365
}
391
366
392
- PyBlock appendBlock (const py::args &pyArgTypes,
393
- const std::optional<py::sequence> &pyArgLocs) {
367
+ PyBlock appendBlock (const py::args &pyArgTypes) {
394
368
operation->checkValid ();
395
- MlirBlock block = createBlock (pyArgTypes, pyArgLocs);
369
+ llvm::SmallVector<MlirType, 4 > argTypes;
370
+ llvm::SmallVector<MlirLocation, 4 > argLocs;
371
+ argTypes.reserve (pyArgTypes.size ());
372
+ argLocs.reserve (pyArgTypes.size ());
373
+ for (auto &pyArg : pyArgTypes) {
374
+ argTypes.push_back (pyArg.cast <PyType &>());
375
+ // TODO: Pass in a proper location here.
376
+ argLocs.push_back (
377
+ mlirLocationUnknownGet (mlirTypeGetContext (argTypes.back ())));
378
+ }
379
+
380
+ MlirBlock block =
381
+ mlirBlockCreate (argTypes.size (), argTypes.data (), argLocs.data ());
396
382
mlirRegionAppendOwnedBlock (region, block);
397
383
return PyBlock (operation, block);
398
384
}
@@ -402,8 +388,7 @@ class PyBlockList {
402
388
.def (" __getitem__" , &PyBlockList::dunderGetItem)
403
389
.def (" __iter__" , &PyBlockList::dunderIter)
404
390
.def (" __len__" , &PyBlockList::dunderLen)
405
- .def (" append" , &PyBlockList::appendBlock, kAppendBlockDocstring ,
406
- py::arg (" arg_locs" ) = std::nullopt);
391
+ .def (" append" , &PyBlockList::appendBlock, kAppendBlockDocstring );
407
392
}
408
393
409
394
private:
@@ -2981,17 +2966,27 @@ void mlir::python::populateIRCore(py::module &m) {
2981
2966
" Returns a forward-optimized sequence of operations." )
2982
2967
.def_static (
2983
2968
" create_at_start" ,
2984
- [](PyRegion &parent, const py::list &pyArgTypes,
2985
- const std::optional<py::sequence> &pyArgLocs) {
2969
+ [](PyRegion &parent, py::list pyArgTypes) {
2986
2970
parent.checkValid ();
2987
- MlirBlock block = createBlock (pyArgTypes, pyArgLocs);
2971
+ llvm::SmallVector<MlirType, 4 > argTypes;
2972
+ llvm::SmallVector<MlirLocation, 4 > argLocs;
2973
+ argTypes.reserve (pyArgTypes.size ());
2974
+ argLocs.reserve (pyArgTypes.size ());
2975
+ for (auto &pyArg : pyArgTypes) {
2976
+ argTypes.push_back (pyArg.cast <PyType &>());
2977
+ // TODO: Pass in a proper location here.
2978
+ argLocs.push_back (
2979
+ mlirLocationUnknownGet (mlirTypeGetContext (argTypes.back ())));
2980
+ }
2981
+
2982
+ MlirBlock block = mlirBlockCreate (argTypes.size (), argTypes.data (),
2983
+ argLocs.data ());
2988
2984
mlirRegionInsertOwnedBlock (parent, 0 , block);
2989
2985
return PyBlock (parent.getParentOperation (), block);
2990
2986
},
2991
2987
py::arg (" parent" ), py::arg (" arg_types" ) = py::list (),
2992
- py::arg (" arg_locs" ) = std::nullopt,
2993
2988
" Creates and returns a new Block at the beginning of the given "
2994
- " region (with given argument types and locations )." )
2989
+ " region (with given argument types)." )
2995
2990
.def (
2996
2991
" append_to" ,
2997
2992
[](PyBlock &self, PyRegion ®ion) {
@@ -3003,30 +2998,50 @@ void mlir::python::populateIRCore(py::module &m) {
3003
2998
" Append this block to a region, transferring ownership if necessary" )
3004
2999
.def (
3005
3000
" create_before" ,
3006
- [](PyBlock &self, const py::args &pyArgTypes,
3007
- const std::optional<py::sequence> &pyArgLocs) {
3001
+ [](PyBlock &self, py::args pyArgTypes) {
3008
3002
self.checkValid ();
3009
- MlirBlock block = createBlock (pyArgTypes, pyArgLocs);
3003
+ llvm::SmallVector<MlirType, 4 > argTypes;
3004
+ llvm::SmallVector<MlirLocation, 4 > argLocs;
3005
+ argTypes.reserve (pyArgTypes.size ());
3006
+ argLocs.reserve (pyArgTypes.size ());
3007
+ for (auto &pyArg : pyArgTypes) {
3008
+ argTypes.push_back (pyArg.cast <PyType &>());
3009
+ // TODO: Pass in a proper location here.
3010
+ argLocs.push_back (
3011
+ mlirLocationUnknownGet (mlirTypeGetContext (argTypes.back ())));
3012
+ }
3013
+
3014
+ MlirBlock block = mlirBlockCreate (argTypes.size (), argTypes.data (),
3015
+ argLocs.data ());
3010
3016
MlirRegion region = mlirBlockGetParentRegion (self.get ());
3011
3017
mlirRegionInsertOwnedBlockBefore (region, self.get (), block);
3012
3018
return PyBlock (self.getParentOperation (), block);
3013
3019
},
3014
- py::arg (" arg_locs" ) = std::nullopt,
3015
3020
" Creates and returns a new Block before this block "
3016
- " (with given argument types and locations )." )
3021
+ " (with given argument types)." )
3017
3022
.def (
3018
3023
" create_after" ,
3019
- [](PyBlock &self, const py::args &pyArgTypes,
3020
- const std::optional<py::sequence> &pyArgLocs) {
3024
+ [](PyBlock &self, py::args pyArgTypes) {
3021
3025
self.checkValid ();
3022
- MlirBlock block = createBlock (pyArgTypes, pyArgLocs);
3026
+ llvm::SmallVector<MlirType, 4 > argTypes;
3027
+ llvm::SmallVector<MlirLocation, 4 > argLocs;
3028
+ argTypes.reserve (pyArgTypes.size ());
3029
+ argLocs.reserve (pyArgTypes.size ());
3030
+ for (auto &pyArg : pyArgTypes) {
3031
+ argTypes.push_back (pyArg.cast <PyType &>());
3032
+
3033
+ // TODO: Pass in a proper location here.
3034
+ argLocs.push_back (
3035
+ mlirLocationUnknownGet (mlirTypeGetContext (argTypes.back ())));
3036
+ }
3037
+ MlirBlock block = mlirBlockCreate (argTypes.size (), argTypes.data (),
3038
+ argLocs.data ());
3023
3039
MlirRegion region = mlirBlockGetParentRegion (self.get ());
3024
3040
mlirRegionInsertOwnedBlockAfter (region, self.get (), block);
3025
3041
return PyBlock (self.getParentOperation (), block);
3026
3042
},
3027
- py::arg (" arg_locs" ) = std::nullopt,
3028
3043
" Creates and returns a new Block after this block "
3029
- " (with given argument types and locations )." )
3044
+ " (with given argument types)." )
3030
3045
.def (
3031
3046
" __iter__" ,
3032
3047
[](PyBlock &self) {
0 commit comments