This is an archive of the discontinued LLVM Phabricator instance.

[LLVM][TableGen] Support combined cells in jupyter kernel
ClosedPublic

Authored by DavidSpickett on Aug 25 2022, 4:45 AM.

Details

Summary

This changes the default mode to cache the code blocks we're
asked to compile until we see the new %reset magic to clear that cache.

This means that if you run several cells in sequence, at the end you're
compiling the code from all the cells at once.

This emulates what the ipython kernel does where it uses a persistent
interpreter state by default.

%reset will only be acted on when it's in the cell we're asked to run
(the newest code).

%args we will use the most recent value we have cached.

The example notebook has been updated to explain that.

Depends on D132378

Diff Detail

Event Timeline

DavidSpickett created this revision.Aug 25 2022, 4:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 25 2022, 4:45 AM
DavidSpickett requested review of this revision.Aug 25 2022, 4:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 25 2022, 4:45 AM

This looks nicer and I can see appeal for literate style. One question comes to mind: what happens when you click runs out of order? E.g., click 1,2,3,2,2 ? In python one ends up rendering and clobbering, what would one get here?

Yes it behaves like the Python kernel, the order that you run cells in matters.

jpienaar accepted this revision.Dec 5 2022, 3:33 PM

Mostly looks good, not sure about collapsing magic at top, but beyond that.

llvm/utils/TableGen/jupyter/tablegen_kernel/kernel.py
86

Wouldn't this collapse all magic to top rather than allowing different magic per section? And in the examples, it seems we have magic at the top only per sections of code.

This revision is now accepted and ready to land.Dec 5 2022, 3:33 PM
DavidSpickett added inline comments.Dec 6 2022, 9:03 AM
llvm/utils/TableGen/jupyter/tablegen_kernel/kernel.py
86

By section do you mean block of code _within_ a cell? If so you are right and this is how I intend that to work, for now at least. This is what I meant by the comment further down about only supporting "cell magic".

If you're wondering if magic in the middle of a cell will it be sort of moved up and made to apply to the whole cell, it won't. As shown in this test:

>>> k.get_magic("%foo a b\\n \\n%foo c d")
        (' \\n%foo c d', {'foo': ['a', 'b']})

After you break the run of lines beginning with % we assume it's all source code.

If you're instead talking about what happens across cells then it goes like this.

If the cell does a reset then the magic is cleaned and it can set new values as needed. Otherwise we'll merge the magic from the most recent cell with the set of cached magic. For example:

%args --disable-the-warning
// some code that only compiles with a warning disabled.
% args
// some more code, which will be added to the previous cell content

The first cell will compile the second one won't.

Since the model is based around giving llvm-tblgen one source file and one set of arguments.

Hopefully that makes sense, if the code gives a different impression I'll fix it and make it clearer.

156

Here is where the magic from the most recent cell updates the cached magic.

LGTM

llvm/utils/TableGen/jupyter/tablegen_kernel/kernel.py
86

Ah I thought it collect all arguments and collect all source and pass in to one invocation. Rather, it concatenates source and overrides args (if provided) as you go.

DavidSpickett added inline comments.Jan 12 2023, 2:15 AM
llvm/utils/TableGen/jupyter/tablegen_kernel/kernel.py
86

Exactly.

So you'd need to restate things sometimes. If you had done %args --foo and want to pass --bar you'd need to do %args --foo --bar. Given that arguments to llvm-tblgen are last value wins, we could append to a list of arguments instead and avoid the need to do that.

Either way, there aren't too many arguments you'd want to pass anyway. So it's not a big burden to restate them.

This revision was landed with ongoing or failed builds.Jan 23 2023, 6:19 AM
This revision was automatically updated to reflect the committed changes.