-
Notifications
You must be signed in to change notification settings - Fork 128
Simplified version of PR #506 #528
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
Merged
Merged
Conversation
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
akoeplinger
pushed a commit
to mono/mono
that referenced
this pull request
Apr 24, 2019
## Summary
This change allows LLVM to identify unnecessary null checks. We mark GOT accesses (including those made by LDSTR) and new object calls as nonnull.
Since LLVM won't propagate this, we propagate from definition to usage, across casts, and from usage to definition (conditionally).
This enables it to remove a significant portion of some benchmarks.
In real-world code, we can expect to see this remove the unnecessary null checks made by private methods.
## Example
### C#:
Note that the constant strings are trivially non-null. This PR spots and propagates that.
```
static void ThrowIfNull(string s)
{
if (s == null)
ThrowArgumentNullException();
}
static void ThrowArgumentNullException()
{
throw new ArgumentNullException();
}
[MethodImpl(MethodImplOptions.NoInlining)]
static int Bench(string a, string b, string c, string d)
{
ThrowIfNull(a);
ThrowIfNull(b);
ThrowIfNull(c);
ThrowIfNull(d);
return a.Length + b.Length + c.Length + d.Length;
}
[Benchmark(Description = nameof(NoThrowInline))]
public int Test() => Bench("a", "bc", "def", "ghij");
```
### Before:
```
define hidden monocc i32 @NoThrowInline_MainClass_Bench_string_string_string_string(i64* %arg_a, i64* %arg_b, i64* %arg_c, i64* %arg_d) #6 gc "mono" {
BB0:
br label %INIT_BB1
INIT_BB1: ; preds = %BB0
br label %INITED_BB2
INITED_BB2: ; preds = %INIT_BB1
br label %BB3
BB3: ; preds = %INITED_BB2
br label %BB2
BB2: ; preds = %BB3
notail call monocc void @NoThrowInline_MainClass_ThrowIfNull_string(i64* %arg_a)
notail call monocc void @NoThrowInline_MainClass_ThrowIfNull_string(i64* %arg_b)
notail call monocc void @NoThrowInline_MainClass_ThrowIfNull_string(i64* %arg_c)
notail call monocc void @NoThrowInline_MainClass_ThrowIfNull_string(i64* %arg_d)
%0 = bitcast i64* %arg_a to i32*
%1 = getelementptr i32, i32* %0, i32 4
%t50 = load volatile i32, i32* %1
%2 = bitcast i64* %arg_b to i32*
%3 = getelementptr i32, i32* %2, i32 4
%t52 = load volatile i32, i32* %3
%t53 = add i32 %t50, %t52
%4 = bitcast i64* %arg_c to i32*
%5 = getelementptr i32, i32* %4, i32 4
%t55 = load volatile i32, i32* %5
%t56 = add i32 %t53, %t55
%6 = bitcast i64* %arg_d to i32*
%7 = getelementptr i32, i32* %6, i32 4
%t58 = load volatile i32, i32* %7
%t60 = add i32 %t56, %t58
br label %BB1
BB1: ; preds = %BB2
ret i32 %t60
}
```
### After:
Note: safepoint in below code is added by backend, not part of this change
```
define hidden monocc i32 @NoThrowInline_MainClass_Bench_string_string_string_string(i64* nonnull %arg_a, i64* nonnull %arg_b, i64* nonnull %arg_c, i64* nonnull %arg_d) #6 gc "mono" {
BB0:
%0 = getelementptr i64, i64* %arg_a, i64 2
%1 = bitcast i64* %0 to i32*
%t50 = load volatile i32, i32* %1, align 4
%2 = getelementptr i64, i64* %arg_b, i64 2
%3 = bitcast i64* %2 to i32*
%t52 = load volatile i32, i32* %3, align 4
%t53 = add i32 %t52, %t50
%4 = getelementptr i64, i64* %arg_c, i64 2
%5 = bitcast i64* %4 to i32*
%t55 = load volatile i32, i32* %5, align 4
%t56 = add i32 %t53, %t55
%6 = getelementptr i64, i64* %arg_d, i64 2
%7 = bitcast i64* %6 to i32*
%t58 = load volatile i32, i32* %7, align 4
%t60 = add i32 %t56, %t58
%8 = load i64*, i64** getelementptr inbounds ([37 x i64*], [37 x i64*]* @mono_aot_NoThrowInline_llvm_got, i64 0, i64 7), align 8
%9 = load i64, i64* %8, align 4
%10 = icmp eq i64 %9, 0
br i1 %10, label %gc.safepoint_poll.exit, label %gc.safepoint_poll.poll.i
gc.safepoint_poll.poll.i: ; preds = %BB0
%11 = load void ()*, void ()** bitcast (i64** getelementptr inbounds ([37 x i64*], [37 x i64*]* @mono_aot_NoThrowInline_llvm_got, i64 0, i64 25) to void ()**), align 8
call void %11() #8
br label %gc.safepoint_poll.exit
gc.safepoint_poll.exit: ; preds = %BB0, %gc.safepoint_poll.poll.i
ret i32 %t60
}
```
## Dependencies
This depends on dotnet/linker#528.
tkapin
pushed a commit
to tkapin/runtime
that referenced
this pull request
Jan 31, 2023
Commit migrated from dotnet/linker@e053b68
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.
No description provided.