-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Use br
instead of a conditional when switching on a constant boolean
#120650
Merged
+80
−5
Merged
Changes from all commits
Commits
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 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 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,67 @@ | ||
//@ compile-flags: -Zmir-opt-level=0 -C no-prepopulate-passes -Copt-level=0 | ||
// make sure that branching on a constant does not emit a conditional | ||
// branch or a switch | ||
|
||
#![crate_type = "lib"] | ||
|
||
// CHECK-LABEL: @if_bool | ||
#[no_mangle] | ||
pub fn if_bool() { | ||
// CHECK: br label %{{.+}} | ||
_ = if true { | ||
0 | ||
} else { | ||
1 | ||
}; | ||
|
||
// CHECK: br label %{{.+}} | ||
_ = if false { | ||
0 | ||
} else { | ||
1 | ||
}; | ||
} | ||
|
||
// CHECK-LABEL: @if_constant_int_eq | ||
#[no_mangle] | ||
pub fn if_constant_int_eq() { | ||
let val = 0; | ||
// CHECK: br label %{{.+}} | ||
_ = if val == 0 { | ||
0 | ||
} else { | ||
1 | ||
}; | ||
|
||
// CHECK: br label %{{.+}} | ||
_ = if val == 1 { | ||
0 | ||
} else { | ||
1 | ||
}; | ||
} | ||
|
||
// CHECK-LABEL: @if_constant_match | ||
#[no_mangle] | ||
pub fn if_constant_match() { | ||
// CHECK: br label %{{.+}} | ||
_ = match 1 { | ||
1 => 2, | ||
2 => 3, | ||
_ => 4 | ||
}; | ||
|
||
// CHECK: br label %{{.+}} | ||
_ = match 1 { | ||
2 => 3, | ||
_ => 4 | ||
}; | ||
|
||
// CHECK: br label %[[MINUS1:.+]] | ||
_ = match -1 { | ||
// CHECK: [[MINUS1]]: | ||
// CHECK: store i32 1 | ||
-1 => 1, | ||
_ => 0, | ||
} | ||
} |
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.
The other thing you could consider doing here is checking directly whether the
discr
is a constant, and if so using https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.SwitchTargets.html#method.target_for_value to decide the jump target without ever needing to talk to LLVM about it. Maybe that would help avoid worrying about thesign_ext
and such, since you'll get aScalarInt
from the const that'll already have theu128
you need in it.(And if somehow you get something that's not a scalar int, I think you can just ICE, since we obviously shouldn't be
SwitchInt
ing on aScalar::Ptr
.)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.
target_for_value is already being called below--the problem here is that it takes a u128, and consts are stored in the compiler as scalars in the MIR or backend-specific values here.
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.
Would checking if it's a constant then
Const::try_to_bits
be sufficient?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.
Hmm, I took a closer look. I think it'd be
Operand::Const
to get aConstOperand
ConstValue
(I think the value you need is exactly the
data: u128
in aScalarInt
, but there doesn't seem to be a way to get that out directly. But going this way means you stay in Rust, rather than making an LLVM value that won't be used.)c-e is right that I missed that you're already using
target_for_value
. I just like having an excuse to mention it, I guess 🙃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.
try_to_bits should work
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.
Looking into it, it looks like at this point we've already codegenned the
i1 true
, asif true
creates a local which hastrue
assigned to it (which wouldn't be safe to remove without checking for other uses of the local).codegen_operand
here then doesn't actually talk to LLVM, we go down tomaybe_codegen_consume_direct
which loads thei1 true
reference directly fromself.locals
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.
While that is true for
bool
, other types will need to generate an llvm value and load from it.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.
Looking at this again today, I still think we shouldn't need to
codegen_operand
here at all, rather match for a constant to do the special case before doing the LLVM stuff at all. (AKA not even have a.immediate()
to get.)