Implement support for pre-visitor callbacks.#84
Open
tsymalla wants to merge 1 commit intoGPUOpen-Drivers:devfrom
Open
Implement support for pre-visitor callbacks.#84tsymalla wants to merge 1 commit intoGPUOpen-Drivers:devfrom
tsymalla wants to merge 1 commit intoGPUOpen-Drivers:devfrom
Conversation
c499e94 to
73945aa
Compare
This change implements a mechanism to run pre-visit callbacks on each operation it stumbles upon. This can be generalized for every op that is being visited, for the current nesting level, or for a given `OpSet`. The code simplifies writing visitors in cases where generic code is written for a set operations, like resetting the insertion point of an `IRBuilder`.
73945aa to
d7f2314
Compare
nhaehnle
reviewed
Feb 2, 2024
Member
nhaehnle
left a comment
There was a problem hiding this comment.
I don't know about this API. I think I'd prefer something that is more clearly structured. Something along the lines of:
static const auto visitor = ...
.add(&Blah::visitFoo)
.withPreVisit(
&Blah:somePreVisit, // somePreVisit(Instruction &inst)
[](VisitorBuilder<Blah> &b) {
b.add(&Blah::visitBar);
b.add(&Blah::visitBaz);
})
...There's also a question of what, if anything, the pre-visitor should return. I think it could be one of:
- Continue -- go to the visitor that is "guarded" by the pre-visitor
- Stop -- stop visiting entirely
- SKip -- skip the "guarded" visitor(s) but potentially continue to other matching visitors
| template <typename... OpTs> static const OpSet get() { | ||
| static OpSet set; | ||
| (... && appendT<OpTs>(set)); | ||
| (void)(... && appendT<OpTs>(set)); |
Member
|
Or perhaps it shouldn't be a "pre-visitor" but a sort of scope guard: .withGuard(&Blah::someGuard, [](VisitorBuilder<Blah> &b) { ... })
...
VisitorResult Blah::someGuard(Instruction &inst, llvm::function_ref<VisitorResult()> visit) {
if (should skip)
return VisitorResult::Continue;
do some setup
auto result = visit(); // this calls the visitors that were registered inside of the "withGuard" block
do some teardown
return result;
} |
Contributor
Having separate setup and teardown callbacks per-op (or for all ops) as part of the guards sounds like a good way, and we'd nest all visitor callbacks in a top-level guard with a setup callback to do something like setting the Builder insertion point. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change implements a mechanism to run pre-visit callbacks on each operation it stumbles upon. This can be generalized for every op that is being visited, for the current nesting level, or for a given
OpSet.The code simplifies writing visitors in cases where generic code is written for a set operations, like resetting the insertion point of an
IRBuilder.Note: the support is currently basic. When adding a pre-visitor callback for a given nesting level without an operation, this will operate for each instruction types with visitor callbacks. I'd like to extend this so we only operate on the set of operations registered for the current visitor nesting level or its parents, but I'm afraid we don't support that yet.
In addition, I plan on cleaning up all of the forwarding code and surroundings next, since it became a bit of a mess with all of the repetetive code.
@nhaehnle