This is an archive of the discontinued LLVM Phabricator instance.

[VFS] Add a "redirecting-with" field to overlays
ClosedPublic

Authored by bnbarham on Jan 21 2022, 4:53 PM.

Details

Summary

Extend "fallthrough" to allow a third option: "fallback". Fallthrough
allows the original path to used if the redirected (or mapped) path
fails. Fallback is the reverse of this, ie. use the original path and
fallback to the mapped path otherwise.

While this result *can* be achieved today using multiple overlays, this
adds a much more intuitive option. As an example, take two directories
"A" and "B". We would like files from "A" to be used, unless they don't
exist, in which case the VFS should fallback to those in "B".

With the current fallthrough option this is possible by adding two
overlays: one mapping from A -> B and another mapping from B -> A. Since
the frontend *nests* the two RedirectingFileSystems, the result will
be that "A" is mapped to "B" and back to "A", unless it isn't in "A" in
which case it fallsthrough to "B" (or fails if it exists in neither).

Using "fallback" semantics allows a single overlay instead: one mapping
from "A" to "B" but only using that mapping if the operation in "A"
fails first.

"redirect-only" is used to represent the current "fallthrough: false"
case.

Diff Detail

Event Timeline

bnbarham created this revision.Jan 21 2022, 4:53 PM
bnbarham requested review of this revision.Jan 21 2022, 4:53 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJan 21 2022, 4:53 PM

Note: I am in no way attached to the name I came up with and would be very open to something better.

Haven't had a chance to look at the code (maybe @keith/@vsapsai/others will have time before I do) but at a high-level this seems pretty sound / SGTM.

The scenario you describe reminds me of the testcase in https://reviews.llvm.org/D117730 ; can you explain if/how this patch relates to that one?

The scenario you describe reminds me of the testcase in https://reviews.llvm.org/D117730 ; can you explain if/how this patch relates to that one?

So a "fallback" is what we actually want, ie. there's a folder A that should be used first, falling back to B if the files don't exist. It just so happens that you can "hack" this by providing overlays that I mention in the commit (ie. A -> B and B -> A), but as you mentioned in the other PR it's an "extra layer of complexity" and I'm not sure it was really ever intended to work that way. It also really depends on how the frontend deals with multiple overlays, eg. it used to actually merge them (added them to an OverlayFileSystem instead of just nesting them). In that case you would need to provide A -> A and then A -> B.

Right now, the A -> B and B -> A overlays "hack" to achieve a fallback is broken due to the bug when using multiple overlays (as per the other PR). I think we should fix that bug regardless, but IMO adding a "fallback" makes more sense for what we actually need anyway.

keith accepted this revision.Jan 28 2022, 12:09 PM

I'm definitely not a VFS expert, but I do think this is much nicer than trying to do the other workarounds with multiple VFSs that you described, I left some minor comments, I reviewed carefully but this logic was and is quite dense so I'm heavily relying on the test coverage.

llvm/include/llvm/Support/VirtualFileSystem.h
576

I think redirecting-with is fine, and I can't come up with something better

llvm/lib/Support/VirtualFileSystem.cpp
1378

I know we can only get here if the redirection != redirectOnly, but I think it might be more readable if we had an explicit else if here with fallthrough, and then added an else with an unreachable to make it clear, and make sure if we ever move stuff around we revisit this ordering

1460–1461

Should this case error because the set value was invalid / a typo? Maybe this bubbles up actually?

1876

Should we error in the case that both this key and fallthrough are present? Maybe that's too strict for backwards compat, but right now I guess fallthrough could overwrite this value depending on order?

This revision is now accepted and ready to land.Jan 28 2022, 12:09 PM

Thanks for taking a look at that @keith, I appreciate it :)! I'll make those changes today hopefully.

llvm/include/llvm/Support/VirtualFileSystem.h
576

Thanks. Do you know if this format is documented anywhere that I would need to update?

llvm/lib/Support/VirtualFileSystem.cpp
1378

Ah yep, I really should have done that in the first place. Thanks!

1460–1461

It ends up returning false down in parse but I need to add a call to error to actually describe the error.

1876

I intentionally allowed both for backwards compatibility, though I'm not sure if that is a real concern after more thinking about it - you'd have to use the new key for it to matter, in which case we don't need to worry about backwards compatibility. So yes, I think we should error if both are present.

I need to add tests for all these error cases as well (both keys present and passing an incorrect value).

keith added inline comments.Jan 28 2022, 1:43 PM
llvm/include/llvm/Support/VirtualFileSystem.h
576

Folks here used to joke that the source was the documentation, and based on a quick search that still seems to be the case, I don't see any places where the others are mentioned

bnbarham updated this revision to Diff 404194.Jan 28 2022, 4:29 PM

Updated to address Keith's comments. Added an error for an invalid redirect kind as well as when both redirect-with and fallthrough are given. Also added tests for these cases.

bnbarham marked 5 inline comments as done.Jan 28 2022, 4:30 PM
bnbarham added inline comments.
llvm/include/llvm/Support/VirtualFileSystem.h
576

Not sure if I should be happy that I don't need to update anything or sad that this is the case 😆

bnbarham marked an inline comment as done.Jan 31 2022, 9:57 AM

Failures look unrelated

bnbarham updated this revision to Diff 404999.Feb 1 2022, 10:42 AM
bnbarham added a reviewer: nathawes.

Noticed one of the unit tests wasn't actually testing the right thing.

@keith I don't have commit access, would you be able to merge this if you're happy with it?

This revision was landed with ongoing or failed builds.Feb 3 2022, 1:10 PM
This revision was automatically updated to reflect the committed changes.