-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fix suboptimal codegen for short or patterns in is-expressions #82429
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
stevenelliottjr
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
stevenelliottjr:fix/pattern-match-codegen-regression
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.
+262
−183
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
6dcf4e6
Fix suboptimal codegen for short `or` patterns in is-expressions (#80…
stevenelliottjr 8e13d09
Fix inverted linear sequence skipping variable bindings in or patterns
stevenelliottjr c98d9c9
Address review feedback: rephrase comment, use IsEmpty, assert M2 IL,…
stevenelliottjr 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
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
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 |
|---|---|---|
|
|
@@ -5505,23 +5505,18 @@ public static void Main() | |
| var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput); | ||
| compVerifier.VerifyIL("C.M1", """ | ||
| { | ||
| // Code size 26 (0x1a) | ||
| .maxstack 1 | ||
| .locals init (bool V_0) | ||
| // Code size 21 (0x15) | ||
| .maxstack 2 | ||
| IL_0000: ldarg.0 | ||
| IL_0001: isinst "int" | ||
| IL_0006: brtrue.s IL_0012 | ||
| IL_0006: brtrue.s IL_0013 | ||
| IL_0008: ldarg.0 | ||
| IL_0009: isinst "long" | ||
| IL_000e: brtrue.s IL_0012 | ||
| IL_0010: br.s IL_0016 | ||
| IL_0012: ldc.i4.1 | ||
| IL_0013: stloc.0 | ||
| IL_0014: br.s IL_0018 | ||
| IL_0016: ldc.i4.0 | ||
| IL_0017: stloc.0 | ||
| IL_0018: ldloc.0 | ||
| IL_0019: ret | ||
| IL_000e: ldnull | ||
| IL_000f: cgt.un | ||
| IL_0011: br.s IL_0014 | ||
| IL_0013: ldc.i4.1 | ||
| IL_0014: ret | ||
| } | ||
| """); | ||
| compVerifier.VerifyIL("C.M2", """ | ||
|
|
@@ -5544,26 +5539,22 @@ .maxstack 2 | |
| compilation = CreateCompilation(source, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularWithPatternCombinators); | ||
| compilation.VerifyDiagnostics(); | ||
| compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput); | ||
| compVerifier.VerifyIL("C.M1", """ | ||
| compVerifier.VerifyIL("C.M1", @" | ||
| { | ||
| // Code size 24 (0x18) | ||
| .maxstack 1 | ||
| .locals init (bool V_0) | ||
| // Code size 20 (0x14) | ||
| .maxstack 2 | ||
| IL_0000: ldarg.0 | ||
| IL_0001: isinst "int" | ||
| IL_0006: brtrue.s IL_0010 | ||
| IL_0001: isinst ""int"" | ||
| IL_0006: brtrue.s IL_0012 | ||
| IL_0008: ldarg.0 | ||
| IL_0009: isinst "long" | ||
| IL_000e: brfalse.s IL_0014 | ||
| IL_0010: ldc.i4.1 | ||
| IL_0011: stloc.0 | ||
| IL_0012: br.s IL_0016 | ||
| IL_0014: ldc.i4.0 | ||
| IL_0015: stloc.0 | ||
| IL_0016: ldloc.0 | ||
| IL_0017: ret | ||
| IL_0009: isinst ""long"" | ||
| IL_000e: ldnull | ||
| IL_000f: cgt.un | ||
| IL_0011: ret | ||
| IL_0012: ldc.i4.1 | ||
| IL_0013: ret | ||
| } | ||
| """); | ||
| "); | ||
| compVerifier.VerifyIL("C.M2", @" | ||
| { | ||
| // Code size 20 (0x14) | ||
|
|
@@ -5605,27 +5596,18 @@ public static void Main() | |
| var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput); | ||
| compVerifier.VerifyIL("C.M1", """ | ||
| { | ||
| // Code size 40 (0x28) | ||
| // Code size 29 (0x1d) | ||
| .maxstack 1 | ||
| .locals init (bool V_0) | ||
| IL_0000: ldarg.0 | ||
| IL_0001: isinst "int" | ||
| IL_0006: brtrue.s IL_0012 | ||
| IL_0006: brtrue.s IL_0017 | ||
| IL_0008: ldarg.0 | ||
| IL_0009: isinst "long" | ||
| IL_000e: brtrue.s IL_0012 | ||
| IL_0010: br.s IL_0016 | ||
| IL_0012: ldc.i4.1 | ||
| IL_0013: stloc.0 | ||
| IL_0014: br.s IL_0018 | ||
| IL_0016: ldc.i4.0 | ||
| IL_0017: stloc.0 | ||
| IL_0018: ldloc.0 | ||
| IL_0019: brtrue.s IL_0022 | ||
| IL_001b: ldstr "False" | ||
| IL_0020: br.s IL_0027 | ||
| IL_0022: ldstr "True" | ||
| IL_0027: ret | ||
| IL_000e: brtrue.s IL_0017 | ||
| IL_0010: ldstr "False" | ||
| IL_0015: br.s IL_001c | ||
| IL_0017: ldstr "True" | ||
| IL_001c: ret | ||
| } | ||
| """); | ||
| compVerifier.VerifyIL("C.M2", """ | ||
|
|
@@ -5648,30 +5630,22 @@ .maxstack 1 | |
| compilation = CreateCompilation(source, options: TestOptions.ReleaseExe, parseOptions: TestOptions.RegularWithPatternCombinators); | ||
| compilation.VerifyDiagnostics(); | ||
| compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput); | ||
| compVerifier.VerifyIL("C.M1", """ | ||
| compVerifier.VerifyIL("C.M1", @" | ||
| { | ||
| // Code size 37 (0x25) | ||
| // Code size 28 (0x1c) | ||
| .maxstack 1 | ||
| .locals init (bool V_0) | ||
| IL_0000: ldarg.0 | ||
| IL_0001: isinst "int" | ||
| IL_0006: brtrue.s IL_0010 | ||
| IL_0001: isinst ""int"" | ||
| IL_0006: brtrue.s IL_0016 | ||
| IL_0008: ldarg.0 | ||
| IL_0009: isinst "long" | ||
| IL_000e: brfalse.s IL_0014 | ||
| IL_0010: ldc.i4.1 | ||
| IL_0011: stloc.0 | ||
| IL_0012: br.s IL_0016 | ||
| IL_0014: ldc.i4.0 | ||
| IL_0015: stloc.0 | ||
| IL_0016: ldloc.0 | ||
| IL_0017: brtrue.s IL_001f | ||
| IL_0019: ldstr "False" | ||
| IL_001e: ret | ||
| IL_001f: ldstr "True" | ||
| IL_0024: ret | ||
| IL_0009: isinst ""long"" | ||
| IL_000e: brtrue.s IL_0016 | ||
| IL_0010: ldstr ""False"" | ||
| IL_0015: ret | ||
| IL_0016: ldstr ""True"" | ||
| IL_001b: ret | ||
| } | ||
| """); | ||
| "); | ||
| compVerifier.VerifyIL("C.M2", @" | ||
| { | ||
| // Code size 28 (0x1c) | ||
|
|
@@ -6157,6 +6131,72 @@ .maxstack 2 | |
| "); | ||
| } | ||
|
|
||
| [Fact, WorkItem(80052, "https://github.com/dotnet/roslyn/issues/80052")] | ||
| public void IsPatternDisjunct_OrPatternInAndExpression() | ||
| { | ||
| // Regression test: `or` pattern used within `&&` should not introduce unnecessary bool temporaries. | ||
| var source = @" | ||
| using System; | ||
| class C | ||
| { | ||
| static bool M1(int x, char c) => x > 0 && c is '/' or '\\'; | ||
| static bool M2(int x, char c) => x > 0 && (c == '/' || c == '\\'); | ||
| public static void Main() | ||
| { | ||
| Console.Write(M1(1, '/')); | ||
| Console.Write(M1(1, '\\')); | ||
| Console.Write(M1(1, 'a')); | ||
| Console.Write(M1(0, '/')); | ||
| } | ||
| }"; | ||
| var compilation = CreateCompilation(source, options: TestOptions.ReleaseExe); | ||
| compilation.VerifyDiagnostics(); | ||
| var expectedOutput = @"TrueTrueFalseFalse"; | ||
| var compVerifier = CompileAndVerify(compilation, expectedOutput: expectedOutput); | ||
| // The `is '/' or '\\'` pattern should produce comparable IL to `c == '/' || c == '\\'` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done — added a |
||
| // without introducing a bool temporary or SpillSequence. | ||
| compVerifier.VerifyIL("C.M1", @" | ||
| { | ||
| // Code size 19 (0x13) | ||
| .maxstack 2 | ||
| IL_0000: ldarg.0 | ||
| IL_0001: ldc.i4.0 | ||
| IL_0002: ble.s IL_0011 | ||
| IL_0004: ldarg.1 | ||
| IL_0005: ldc.i4.s 47 | ||
| IL_0007: beq.s IL_000f | ||
| IL_0009: ldarg.1 | ||
| IL_000a: ldc.i4.s 92 | ||
| IL_000c: ceq | ||
| IL_000e: ret | ||
| IL_000f: ldc.i4.1 | ||
| IL_0010: ret | ||
| IL_0011: ldc.i4.0 | ||
| IL_0012: ret | ||
| } | ||
| "); | ||
| compVerifier.VerifyIL("C.M2", @" | ||
| { | ||
| // Code size 19 (0x13) | ||
| .maxstack 2 | ||
| IL_0000: ldarg.0 | ||
| IL_0001: ldc.i4.0 | ||
| IL_0002: ble.s IL_0011 | ||
| IL_0004: ldarg.1 | ||
| IL_0005: ldc.i4.s 47 | ||
| IL_0007: beq.s IL_000f | ||
| IL_0009: ldarg.1 | ||
| IL_000a: ldc.i4.s 92 | ||
| IL_000c: ceq | ||
| IL_000e: ret | ||
| IL_000f: ldc.i4.1 | ||
| IL_0010: ret | ||
| IL_0011: ldc.i4.0 | ||
| IL_0012: ret | ||
| } | ||
| "); | ||
| } | ||
|
|
||
| [Fact, WorkItem(46536, "https://github.com/dotnet/roslyn/issues/46536")] | ||
| public void MultiplePathsToNode_SwitchDispatch_01() | ||
| { | ||
|
|
||
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.
Consider making this a local function in
VisitIsPatternExpression. Otherwise, this name should start with a capital letter to follow our naming conventions.