@@ -1520,7 +1520,7 @@ nb::object PyOperation::create(std::string_view name,
1520
1520
llvm::ArrayRef<MlirValue> operands,
1521
1521
std::optional<nb::dict> attributes,
1522
1522
std::optional<std::vector<PyBlock *>> successors,
1523
- int regions, PyLocation location,
1523
+ int regions, PyLocation & location,
1524
1524
const nb::object &maybeIp, bool inferType) {
1525
1525
llvm::SmallVector<MlirType, 4 > mlirResults;
1526
1526
llvm::SmallVector<MlirBlock, 4 > mlirSuccessors;
@@ -1934,7 +1934,7 @@ nb::object PyOpView::buildGeneric(
1934
1934
std::optional<nb::list> resultTypeList, nb::list operandList,
1935
1935
std::optional<nb::dict> attributes,
1936
1936
std::optional<std::vector<PyBlock *>> successors,
1937
- std::optional<int > regions, PyLocation location,
1937
+ std::optional<int > regions, PyLocation & location,
1938
1938
const nb::object &maybeIp) {
1939
1939
PyMlirContextRef context = location.getContext ();
1940
1940
@@ -2795,13 +2795,12 @@ MlirLocation tracebackToLocation(MlirContext ctx) {
2795
2795
thread_local std::array<MlirLocation, kMaxFrames > frames;
2796
2796
size_t count = 0 ;
2797
2797
2798
- assert (PyGILState_Check ());
2799
-
2798
+ nb::gil_scoped_acquire acquire;
2800
2799
PyThreadState *tstate = PyThreadState_GET ();
2801
2800
2802
2801
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;
2805
2804
next = PyFrame_GetBack (pyFrame), Py_XDECREF (pyFrame), pyFrame = next) {
2806
2805
PyCodeObject *code = PyFrame_GetCode (pyFrame);
2807
2806
auto fileNameStr =
@@ -2834,9 +2833,8 @@ MlirLocation tracebackToLocation(MlirContext ctx) {
2834
2833
2835
2834
frames[count] = mlirLocationNameGet (ctx, wrap (funcName), loc);
2836
2835
++count;
2837
- if (count > framesLimit)
2838
- break ;
2839
2836
}
2837
+ Py_XDECREF (pyFrame);
2840
2838
2841
2839
if (count == 0 )
2842
2840
return mlirLocationUnknownGet (ctx);
@@ -2856,22 +2854,15 @@ MlirLocation tracebackToLocation(MlirContext ctx) {
2856
2854
2857
2855
PyLocation
2858
2856
maybeGetTracebackLocation (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};
2875
2866
}
2876
2867
2877
2868
} // namespace
@@ -3325,7 +3316,7 @@ void mlir::python::populateIRCore(nb::module_ &m) {
3325
3316
kModuleParseDocstring )
3326
3317
.def_static (
3327
3318
" create" ,
3328
- [](std::optional<PyLocation> loc) {
3319
+ [](const std::optional<PyLocation> & loc) {
3329
3320
PyLocation pyLoc = maybeGetTracebackLocation (loc);
3330
3321
MlirModule module = mlirModuleCreateEmpty (pyLoc.get ());
3331
3322
return PyModule::forModule (module ).releaseObject ();
@@ -3540,8 +3531,8 @@ void mlir::python::populateIRCore(nb::module_ &m) {
3540
3531
std::optional<std::vector<PyValue *>> operands,
3541
3532
std::optional<nb::dict> attributes,
3542
3533
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) {
3545
3536
// Unpack/validate operands.
3546
3537
llvm::SmallVector<MlirValue, 4 > mlirOperands;
3547
3538
if (operands) {
@@ -3599,7 +3590,8 @@ void mlir::python::populateIRCore(nb::module_ &m) {
3599
3590
std::optional<nb::list> resultTypeList, nb::list operandList,
3600
3591
std::optional<nb::dict> attributes,
3601
3592
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,
3603
3595
const nb::object &maybeIp) {
3604
3596
PyLocation pyLoc = maybeGetTracebackLocation (location);
3605
3597
new (self) PyOpView (PyOpView::buildGeneric (
0 commit comments