Splitting work from https://github.com/dotnet/runtime/issues/64554 since it's rather non-trivial change and should be solved separately.