-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Handle exceptions from parallel ReadyToRun compilation #132282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
agocke
wants to merge
2
commits into
dotnet:main
Choose a base branch
from
agocke:bail-out-on-exception
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+581
−431
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
198 changes: 198 additions & 0 deletions
198
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/CompilationWorklist.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Runtime.ExceptionServices; | ||
| using System.Threading; | ||
|
|
||
| using ILCompiler.DependencyAnalysis; | ||
| using ILCompiler.DependencyAnalysisFramework; | ||
|
|
||
| namespace ILCompiler | ||
| { | ||
| internal delegate void CompilationWorkerAction<TWorkerState>( | ||
| DependencyNodeCore<NodeFactory> item, | ||
| ref TWorkerState workerState); | ||
|
|
||
| internal sealed class CompilationWorklist<TWorkerState> : IDisposable | ||
| { | ||
| private sealed class Worker | ||
| { | ||
| public readonly SemaphoreSlim Start = new(0); | ||
| public readonly Thread Thread; | ||
| public TWorkerState State; | ||
| private readonly CompilationWorklist<TWorkerState> _worklist; | ||
|
|
||
| public Worker(CompilationWorklist<TWorkerState> worklist) | ||
| { | ||
| _worklist = worklist; | ||
| Thread = new Thread(Run); | ||
| } | ||
|
|
||
| private void Run() => _worklist.RunWorkerLoop(this); | ||
| } | ||
|
|
||
| private readonly ManualResetEventSlim _complete = new(); | ||
| private readonly int _parallelism; | ||
| private readonly CompilationWorkerAction<TWorkerState> _processItem; | ||
| private TWorkerState _mainWorkerState; | ||
| private IReadOnlyList<DependencyNodeCore<NodeFactory>> _items; | ||
| private ExceptionDispatchInfo _exception; | ||
| private Worker[] _workers; | ||
| private int _nextIndex; | ||
| private int _startedWorkerCount; | ||
| private int _workersRemaining; | ||
| private volatile bool _stopping; | ||
|
|
||
| public CompilationWorklist(int parallelism, CompilationWorkerAction<TWorkerState> processItem) | ||
| { | ||
| _parallelism = parallelism; | ||
| _processItem = processItem; | ||
| } | ||
|
agocke marked this conversation as resolved.
|
||
|
|
||
| public void Run(IReadOnlyList<DependencyNodeCore<NodeFactory>> items) | ||
| { | ||
| EnsureWorkers(); | ||
| Debug.Assert(_items is null); | ||
|
|
||
| _items = items; | ||
| _exception = null; | ||
| _nextIndex = -1; | ||
| _workersRemaining = _parallelism; | ||
| _complete.Reset(); | ||
|
|
||
| foreach (Worker worker in _workers) | ||
| { | ||
| worker.Start.Release(); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| ProcessItems(ref _mainWorkerState); | ||
| } | ||
| finally | ||
| { | ||
| WaitForCompletion()?.Throw(); | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| if (_stopping) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _stopping = true; | ||
|
|
||
| if (_workers is not null) | ||
| { | ||
| for (int i = 0; i < _startedWorkerCount; i++) | ||
| { | ||
| _workers[i].Start.Release(); | ||
| } | ||
|
|
||
| for (int i = 0; i < _startedWorkerCount; i++) | ||
| { | ||
| Worker worker = _workers[i]; | ||
| worker.Thread.Join(); | ||
| worker.Start.Dispose(); | ||
| worker.State = default; | ||
| } | ||
| } | ||
|
|
||
| _mainWorkerState = default; | ||
| _complete.Dispose(); | ||
| } | ||
|
|
||
| private void EnsureWorkers() | ||
| { | ||
| if (_workers is not null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _workers = new Worker[_parallelism - 1]; | ||
| for (int i = 0; i < _workers.Length; i++) | ||
| { | ||
| Worker worker = new(this); | ||
| _workers[i] = worker; | ||
| worker.Thread.Start(); | ||
| _startedWorkerCount++; | ||
| } | ||
| } | ||
|
|
||
| private void RunWorkerLoop(Worker worker) | ||
| { | ||
| while (true) | ||
| { | ||
| worker.Start.Wait(); | ||
| if (_stopping) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| ProcessItems(ref worker.State); | ||
| } | ||
| } | ||
|
|
||
| private void ProcessItems(ref TWorkerState workerState) | ||
| { | ||
| try | ||
| { | ||
| while (TryTake(out DependencyNodeCore<NodeFactory> item)) | ||
| { | ||
| _processItem(item, ref workerState); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| lock (this) | ||
| { | ||
| _exception ??= ExceptionDispatchInfo.Capture(ex); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| if (Interlocked.Decrement(ref _workersRemaining) == 0) | ||
| { | ||
| _complete.Set(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private bool TryTake(out DependencyNodeCore<NodeFactory> item) | ||
| { | ||
| lock (this) | ||
| { | ||
| if (_exception is not null) | ||
| { | ||
| item = null; | ||
| return false; | ||
| } | ||
|
|
||
| int index = ++_nextIndex; | ||
| IReadOnlyList<DependencyNodeCore<NodeFactory>> items = _items; | ||
| if ((uint)index >= (uint)items.Count) | ||
| { | ||
| item = null; | ||
| return false; | ||
| } | ||
|
|
||
| item = items[index]; | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| private ExceptionDispatchInfo WaitForCompletion() | ||
| { | ||
| _complete.Wait(); | ||
| _items = null; | ||
| ExceptionDispatchInfo exception = _exception; | ||
| _exception = null; | ||
| return exception; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NativeAOT has equivalent logic implemented using
Parallel.ForEach:runtime/src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/RyuJitCompilation.cs
Lines 166 to 169 in 2580b59
Can we use that instead of implementing our own custom MT scheduler?
In any case, both R2R and NAOT should be on the same plan if we are refactoring this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense to me. I was starting with refactoring, but keeping the existing scheduler, but I can go further and try to get us down to just Parallel.ForEach.