Skip to content

[mlir-python-mcp] Evaluate a trailing expression once in execute_python - #795

Merged
makslevental merged 1 commit into
llvm:mainfrom
Prateekshit73:fix-mlir-mcp-double-eval
Nov 12, 2026
Merged

makslevental merged 1 commit into
llvm:mainfrom
Prateekshit73:fix-mlir-mcp-double-eval

Conversation

@Prateekshit73

@Prateekshit73 Prateekshit73 commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

execute_python executed the whole cell and then, if nothing was printed, ran the cell's last line a second time to show its value. A last line with side effects therefore ran twice:

  • xs = [] then xs.append(1) leaves xs == [1, 1].
  • it = iter([1, 2, 3]) then next(it) shows 2 instead of 1.
  • An op builder given an explicit insertion point, e.g. arith.constant(i32, 9, ip=ip), inserts the op twice.

The trailing expression is now split off with ast: the rest of the cell is exec'd, then the expression is evaluated once, inside the same Context/Location as the rest of the cell. Its value is still shown only when the cell printed nothing. This also fixes a trailing expression that spans several lines, which the last-line approach could not parse; it reported "Executed successfully (no output)" instead of the value.

@makslevental

Copy link
Copy Markdown
Contributor

LGTM (I'll approve after tests pass) but can you remove this part of the PR description:

Tests: test_trailing_expression_runs_once and test_multiline_trailing_expression, both of which fail before this change. The mlir-python-mcp suite passes locally with 100% coverage (116 tests).

The tests are evident from the PR...

I used Claude Code to investigate and help me with this change; I reviewed and tested it.

I don't care about this :)

@makslevental
makslevental self-requested a review November 5, 2026 06:03
@Prateekshit73

Copy link
Copy Markdown
Contributor Author

Done, removed both. The two failing builds don't touch this change: macOS dies in brew install (exit 82), and wasm hits duplicate mlir::DataFlowSolver symbols at link time. The update-llvm PR hits the same wasm error. Happy to rebase once CI is fixed.

@makslevental

Copy link
Copy Markdown
Contributor

wasm hits duplicate mlir::DataFlowSolver symbols at link time

I'm almost done fixing that so it should merge today. I'll come back here and rebase when it does.

@makslevental
makslevental force-pushed the fix-mlir-mcp-double-eval branch from ffd80b8 to e16c2a9 Compare November 6, 2026 02:52
@Prateekshit73

Copy link
Copy Markdown
Contributor Author

Small correction: macOS doesn't fail in brew install. Exit 82 comes from unzip -qqP "${{ secrets.MLIR_PYTHON_BINDINGS_PAYLOAD }}" Payload.zip; 82 is unzip's "bad password" code. The log shows unzip -qqP "" Payload.zip, because GitHub doesn't pass secrets to pull_request runs from forks. That's why macOS fails here but passes on main and update-llvm.

@makslevental

Copy link
Copy Markdown
Contributor

The log shows unzip -qqP "" Payload.zip, because GitHub doesn't pass secrets to pull_request runs from forks. That's why macOS fails here but passes on main and update-llvm.

That's fine - the mcp doesn't touch that Payload thing so I'll force merge once the other tests pass

@Prateekshit73

Copy link
Copy Markdown
Contributor Author

Thanks! Heads-up: the Linux builds in the latest run died with "The hosted runner lost communication with the server" (infra, not a test failure), so they'll need a re-run.

@Prateekshit73

Copy link
Copy Markdown
Contributor Author

Re-run looks good: Linux x86_64/aarch64, wasm32 and Windows builds all pass. The test jobs are skipped because they needs: [build-mlir-python-bindings], so the macOS leg failing on fork PRs skips all of them.

@makslevental
makslevental force-pushed the fix-mlir-mcp-double-eval branch from 75fa8fc to 3c7cd71 Compare November 11, 2026 05:43
@Prateekshit73

Copy link
Copy Markdown
Contributor Author

Heads-up: after the rebase, the Linux and Windows builds fail in Build standalone-python-bindings with StandaloneTypes.td:21:7: error: Value 'cppIncludes' unknown!. That comes from the LLVM bumps that landed since Nov 6 (#796–#798), not from this PR, which only touches execute_python in mlir-python-mcp.

execute_python exec'd the whole cell and then, if nothing was printed,
eval'd its last line again to show a value, so a side-effecting last
line (xs.append(1), or an op builder that inserts into the IR) ran twice.
Split a trailing expression off with ast and eval it once after exec'ing
the rest. This also handles a multi-line trailing expression, which the
last-line approach could not parse.
@makslevental
makslevental force-pushed the fix-mlir-mcp-double-eval branch from ba39bf9 to 808ad39 Compare November 12, 2026 17:45

@makslevental makslevental left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@makslevental
makslevental merged commit fa523d4 into llvm:main Nov 12, 2026
48 checks passed
@Prateekshit73
Prateekshit73 deleted the fix-mlir-mcp-double-eval branch November 13, 2026 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants