@@ -1520,7 +1520,7 @@ nb::object PyOperation::create(std::string_view name,
15201520                               llvm::ArrayRef<MlirValue> operands,
15211521                               std::optional<nb::dict> attributes,
15221522                               std::optional<std::vector<PyBlock *>> successors,
1523-                                int  regions, PyLocation location,
1523+                                int  regions, PyLocation & location,
15241524                               const  nb::object &maybeIp, bool  inferType) {
15251525  llvm::SmallVector<MlirType, 4 > mlirResults;
15261526  llvm::SmallVector<MlirBlock, 4 > mlirSuccessors;
@@ -1934,7 +1934,7 @@ nb::object PyOpView::buildGeneric(
19341934    std::optional<nb::list> resultTypeList, nb::list operandList,
19351935    std::optional<nb::dict> attributes,
19361936    std::optional<std::vector<PyBlock *>> successors,
1937-     std::optional<int > regions, PyLocation location,
1937+     std::optional<int > regions, PyLocation & location,
19381938    const  nb::object &maybeIp) {
19391939  PyMlirContextRef context = location.getContext ();
19401940
@@ -2795,13 +2795,12 @@ MlirLocation tracebackToLocation(MlirContext ctx) {
27952795  thread_local  std::array<MlirLocation, kMaxFrames > frames;
27962796  size_t  count = 0 ;
27972797
2798-   assert (PyGILState_Check ());
2799- 
2798+   nb::gil_scoped_acquire acquire;
28002799  PyThreadState *tstate = PyThreadState_GET ();
28012800
28022801  PyFrameObject *next;
2803-   for  ( PyFrameObject *pyFrame = PyThreadState_GetFrame (tstate);
2804-         pyFrame != nullptr  && count < framesLimit;
2802+   PyFrameObject *pyFrame = PyThreadState_GetFrame (tstate);
2803+   for  (;  pyFrame != nullptr  && count < framesLimit;
28052804       next = PyFrame_GetBack (pyFrame), Py_XDECREF (pyFrame), pyFrame = next) {
28062805    PyCodeObject *code = PyFrame_GetCode (pyFrame);
28072806    auto  fileNameStr =
@@ -2834,9 +2833,8 @@ MlirLocation tracebackToLocation(MlirContext ctx) {
28342833
28352834    frames[count] = mlirLocationNameGet (ctx, wrap (funcName), loc);
28362835    ++count;
2837-     if  (count > framesLimit)
2838-       break ;
28392836  }
2837+   Py_XDECREF (pyFrame);
28402838
28412839  if  (count == 0 )
28422840    return  mlirLocationUnknownGet (ctx);
@@ -2856,22 +2854,15 @@ MlirLocation tracebackToLocation(MlirContext ctx) {
28562854
28572855PyLocation
28582856maybeGetTracebackLocation (const  std::optional<PyLocation> &location) {
2859-   MlirLocation mlirLoc;
2860-   MlirContext mlirCtx;
2861-   if  (!location.has_value () &&
2862-       PyGlobals::get ().getTracebackLoc ().locTracebacksEnabled ()) {
2863-     mlirCtx = DefaultingPyMlirContext::resolve ().get ();
2864-     mlirLoc = tracebackToLocation (mlirCtx);
2865-   } else  if  (!location.has_value ()) {
2866-     mlirLoc = DefaultingPyLocation::resolve ();
2867-     mlirCtx = mlirLocationGetContext (mlirLoc);
2868-   } else  {
2869-     mlirLoc = *location;
2870-     mlirCtx = mlirLocationGetContext (mlirLoc);
2871-   }
2872-   assert (!mlirLocationIsNull (mlirLoc) && " expected non-null mlirLoc"  );
2873-   PyMlirContextRef ctx = PyMlirContext::forContext (mlirCtx);
2874-   return  {ctx, mlirLoc};
2857+   if  (location.has_value ())
2858+     return  location.value ();
2859+   if  (!PyGlobals::get ().getTracebackLoc ().locTracebacksEnabled ())
2860+     return  DefaultingPyLocation::resolve ();
2861+ 
2862+   PyMlirContext &ctx = DefaultingPyMlirContext::resolve ();
2863+   MlirLocation mlirLoc = tracebackToLocation (ctx.get ());
2864+   PyMlirContextRef ref = PyMlirContext::forContext (ctx.get ());
2865+   return  {ref, mlirLoc};
28752866}
28762867
28772868} //  namespace
@@ -3325,7 +3316,7 @@ void mlir::python::populateIRCore(nb::module_ &m) {
33253316          kModuleParseDocstring )
33263317      .def_static (
33273318          " create"  ,
3328-           [](std::optional<PyLocation> loc) {
3319+           [](const   std::optional<PyLocation> & loc) {
33293320            PyLocation pyLoc = maybeGetTracebackLocation (loc);
33303321            MlirModule module  = mlirModuleCreateEmpty (pyLoc.get ());
33313322            return  PyModule::forModule (module ).releaseObject ();
@@ -3540,8 +3531,8 @@ void mlir::python::populateIRCore(nb::module_ &m) {
35403531             std::optional<std::vector<PyValue *>> operands,
35413532             std::optional<nb::dict> attributes,
35423533             std::optional<std::vector<PyBlock *>> successors, int  regions,
3543-              std::optional<PyLocation> location,  const  nb::object &maybeIp ,
3544-              bool  inferType) {
3534+              const   std::optional<PyLocation> &location ,
3535+              const  nb::object &maybeIp,  bool  inferType) {
35453536            //  Unpack/validate operands.
35463537            llvm::SmallVector<MlirValue, 4 > mlirOperands;
35473538            if  (operands) {
@@ -3599,7 +3590,8 @@ void mlir::python::populateIRCore(nb::module_ &m) {
35993590                 std::optional<nb::list> resultTypeList, nb::list operandList,
36003591                 std::optional<nb::dict> attributes,
36013592                 std::optional<std::vector<PyBlock *>> successors,
3602-                  std::optional<int > regions, std::optional<PyLocation> location,
3593+                  std::optional<int > regions,
3594+                  const  std::optional<PyLocation> &location,
36033595                 const  nb::object &maybeIp) {
36043596                PyLocation pyLoc = maybeGetTracebackLocation (location);
36053597                new  (self) PyOpView (PyOpView::buildGeneric (
0 commit comments