@@ -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
2802
for (PyFrameObject *pyFrame = PyThreadState_GetFrame (tstate);
2804
- pyFrame != nullptr && count < framesLimit ;
2803
+ pyFrame != nullptr && count < kMaxFrames ;
2805
2804
next = PyFrame_GetBack (pyFrame), Py_XDECREF (pyFrame), pyFrame = next) {
2806
2805
PyCodeObject *code = PyFrame_GetCode (pyFrame);
2807
2806
auto fileNameStr =
@@ -2834,8 +2833,10 @@ MlirLocation tracebackToLocation(MlirContext ctx) {
2834
2833
2835
2834
frames[count] = mlirLocationNameGet (ctx, wrap (funcName), loc);
2836
2835
++count;
2837
- if (count > framesLimit)
2836
+ if (count > framesLimit) {
2837
+ Py_XDECREF (pyFrame);
2838
2838
break ;
2839
+ }
2839
2840
}
2840
2841
2841
2842
if (count == 0 )
@@ -2856,22 +2857,15 @@ MlirLocation tracebackToLocation(MlirContext ctx) {
2856
2857
2857
2858
PyLocation
2858
2859
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};
2860
+ if (location.has_value ())
2861
+ return location.value ();
2862
+ if (!PyGlobals::get ().getTracebackLoc ().locTracebacksEnabled ())
2863
+ return DefaultingPyLocation::resolve ();
2864
+
2865
+ PyMlirContext &ctx = DefaultingPyMlirContext::resolve ();
2866
+ MlirLocation mlirLoc = tracebackToLocation (ctx.get ());
2867
+ PyMlirContextRef ref = PyMlirContext::forContext (ctx.get ());
2868
+ return {ref, mlirLoc};
2875
2869
}
2876
2870
2877
2871
} // namespace
@@ -3325,7 +3319,7 @@ void mlir::python::populateIRCore(nb::module_ &m) {
3325
3319
kModuleParseDocstring )
3326
3320
.def_static (
3327
3321
" create" ,
3328
- [](std::optional<PyLocation> loc) {
3322
+ [](const std::optional<PyLocation> & loc) {
3329
3323
PyLocation pyLoc = maybeGetTracebackLocation (loc);
3330
3324
MlirModule module = mlirModuleCreateEmpty (pyLoc.get ());
3331
3325
return PyModule::forModule (module ).releaseObject ();
@@ -3540,8 +3534,8 @@ void mlir::python::populateIRCore(nb::module_ &m) {
3540
3534
std::optional<std::vector<PyValue *>> operands,
3541
3535
std::optional<nb::dict> attributes,
3542
3536
std::optional<std::vector<PyBlock *>> successors, int regions,
3543
- std::optional<PyLocation> location, const nb::object &maybeIp ,
3544
- bool inferType) {
3537
+ const std::optional<PyLocation> &location ,
3538
+ const nb::object &maybeIp, bool inferType) {
3545
3539
// Unpack/validate operands.
3546
3540
llvm::SmallVector<MlirValue, 4 > mlirOperands;
3547
3541
if (operands) {
@@ -3599,7 +3593,8 @@ void mlir::python::populateIRCore(nb::module_ &m) {
3599
3593
std::optional<nb::list> resultTypeList, nb::list operandList,
3600
3594
std::optional<nb::dict> attributes,
3601
3595
std::optional<std::vector<PyBlock *>> successors,
3602
- std::optional<int > regions, std::optional<PyLocation> location,
3596
+ std::optional<int > regions,
3597
+ const std::optional<PyLocation> &location,
3603
3598
const nb::object &maybeIp) {
3604
3599
PyLocation pyLoc = maybeGetTracebackLocation (location);
3605
3600
new (self) PyOpView (PyOpView::buildGeneric (
0 commit comments