Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_attr_parsing/src/attributes/unroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use super::prelude::*;

pub(crate) struct UnrollParser;
impl SingleAttributeParser for UnrollParser {
const PATH: &[Symbol] = &[sym::unroll];
// FIXME(#159429): temporarily renamed to mitigate `#[unroll]` nameres ambiguity.
const PATH: &[Symbol] = &[sym::rustc_unroll];
const ALLOWED_TARGETS: AllowedTargets<'_> = AllowedTargets::AllowList(&[
Allow(Target::Loop),
Allow(Target::ForLoop),
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,12 @@ pub static BUILTIN_ATTRIBUTES: &[Symbol] = &[
// - https://github.com/rust-lang/rust/issues/153629
sym::splat,

// The `#[unroll]` attribute.
// The `#[rustc_unroll]` attribute.
//
// - https://github.com/rust-lang/rust/pull/156816
sym::unroll,
//
// FIXME(#159429): temporarily renamed to mitigate `#[unroll]` nameres ambiguity
sym::rustc_unroll,

// `#[instrument_fn = "on|off"]` to insert or inhibit instrumentation function
// calls inside a function, usually around the prologue.
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir/src/attrs/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,8 @@ pub enum AttributeKind {
limit: Limit,
},

/// Represents `#[unroll]`
/// Represents `#[rustc_unroll]`
// FIXME(#159429): temporarily renamed from `#[unroll]` to mitigate nameres ambiguity
Unroll(UnrollAttr),

/// Represents `#[unstable_feature_bound]`.
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,8 @@ symbols! {
rustc_test_marker,
rustc_then_this_would_need,
rustc_trivial_field_reads,
// FIXME(#159429): temporary rename to avoid `#[unroll]` nameres ambiguity
rustc_unroll,
rustc_unsafe_specialization_marker,
rustdoc,
rustdoc_internals,
Expand Down Expand Up @@ -2248,7 +2250,6 @@ symbols! {
unreachable_display,
unreachable_macro,
unrestricted_attribute_tokens,
unroll,
unsafe_attributes,
unsafe_binders,
unsafe_block_in_unsafe_fn,
Expand Down
12 changes: 8 additions & 4 deletions src/doc/unstable-book/src/language-features/loop-hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ The tracking issue for this feature is: [#156874]

------

<!--
FIXME(#159429): temporarily renamed `#[unroll]` to mitigate a nameres ambiguity
-->

Loop unrolling can be a powerful optimization but like inlining, it is sometimes useful to
manually provide hints to optimizations.

`#[unroll]` will encourage unrolling of a loop.
`#[rustc_unroll]` will encourage unrolling of a loop.

`#[unroll(full)]` is a stronger hint and can cause optimizations to completely ignore the code
`#[rustc_unroll(full)]` is a stronger hint and can cause optimizations to completely ignore the code
side growth from repeating a loop body.

`#[unroll(never)]` is a strong hint to not unroll the loop at all. Note that other loop
`#[rustc_unroll(never)]` is a strong hint to not unroll the loop at all. Note that other loop
optimizations may still be applied.

`#[unroll(N)]` is a hint to unroll `N` iterations of the loop.
`#[rustc_unroll(N)]` is a hint to unroll `N` iterations of the loop.

In all cases these are just hints and may be ignored. But unlike function inlining hints,
loops tend to be heavily modified during compilation, which can make obeying hints challenging.
Expand Down
8 changes: 4 additions & 4 deletions tests/codegen-llvm/loop-attrs/unroll-for-metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ unsafe extern "C" {
pub fn unroll_hint() {
// CHECK-LABEL: @unroll_hint
// CHECK: !llvm.loop ![[HINT:[0-9]+]]
#[unroll]
#[rustc_unroll]
for _ in 0..10 {
unsafe { maybe_has_side_effect() }
}
Expand All @@ -25,7 +25,7 @@ pub fn unroll_hint() {
pub fn unroll_full() {
// CHECK-LABEL: @unroll_full
// CHECK: !llvm.loop ![[FULL:[0-9]+]]
#[unroll(full)]
#[rustc_unroll(full)]
for _ in 0..10 {
unsafe { maybe_has_side_effect() }
}
Expand All @@ -35,7 +35,7 @@ pub fn unroll_full() {
pub fn unroll_never() {
// CHECK-LABEL: @unroll_never
// CHECK: !llvm.loop ![[DISABLE:[0-9]+]]
#[unroll(never)]
#[rustc_unroll(never)]
for _ in 0..10 {
unsafe { maybe_has_side_effect() }
}
Expand All @@ -45,7 +45,7 @@ pub fn unroll_never() {
pub fn unroll_count() {
// CHECK-LABEL: @unroll_count
// CHECK: !llvm.loop ![[COUNT:[0-9]+]]
#[unroll(5)]
#[rustc_unroll(5)]
for _ in 0..10 {
unsafe { maybe_has_side_effect() }
}
Expand Down
6 changes: 3 additions & 3 deletions tests/codegen-llvm/loop-attrs/unroll-for-works.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ unsafe extern "C" {
pub fn unroll_full() {
// CHECK-LABEL: @unroll_full
// CHECK-COUNT-512: tail call void @maybe_has_side_effect()
#[unroll(full)]
#[rustc_unroll(full)]
for _ in 0..512 {
unsafe { maybe_has_side_effect() }
}
Expand All @@ -22,7 +22,7 @@ pub fn unroll_never() {
// CHECK-LABEL: @unroll_never
// CHECK: tail call void @maybe_has_side_effect()
// CHECK-NOT: tail call void @maybe_has_side_effect()
#[unroll(never)]
#[rustc_unroll(never)]
for _ in 0..3 {
unsafe { maybe_has_side_effect() }
}
Expand All @@ -32,7 +32,7 @@ pub fn unroll_never() {
pub fn unroll_count() {
// CHECK-LABEL: @unroll_count
// CHECK-COUNT-5: tail call void @maybe_has_side_effect()
#[unroll(5)]
#[rustc_unroll(5)]
for _ in 0..10 {
unsafe { maybe_has_side_effect() }
}
Expand Down
8 changes: 4 additions & 4 deletions tests/codegen-llvm/loop-attrs/unroll-loop-metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn unroll_hint() {
// CHECK-LABEL: @unroll_hint
// CHECK: !llvm.loop ![[HINT:[0-9]+]]
let mut i = 0;
#[unroll]
#[rustc_unroll]
loop {
unsafe { maybe_has_side_effect() }
i += 1;
Expand All @@ -35,7 +35,7 @@ pub fn unroll_full() {
// CHECK-LABEL: @unroll_full
// CHECK: !llvm.loop ![[FULL:[0-9]+]]
let mut i = 0;
let _return = (#[unroll(full)]
let _return = (#[rustc_unroll(full)]
loop {
unsafe { maybe_has_side_effect() }
i += 1;
Expand All @@ -50,7 +50,7 @@ pub fn unroll_never() {
// CHECK-LABEL: @unroll_never
// CHECK: !llvm.loop ![[DISABLE:[0-9]+]]
let mut i = 0;
let _return = (1 + #[unroll(never)]
let _return = (1 + #[rustc_unroll(never)]
loop {
unsafe { maybe_has_side_effect() }
i += 1;
Expand All @@ -65,7 +65,7 @@ pub fn unroll_count() {
// CHECK-LABEL: @unroll_count
// CHECK: !llvm.loop ![[COUNT:[0-9]+]]
let mut i = 0;
#[unroll(5)]
#[rustc_unroll(5)]
loop {
unsafe { maybe_has_side_effect() }
i += 1;
Expand Down
8 changes: 4 additions & 4 deletions tests/codegen-llvm/loop-attrs/unroll-while-metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn unroll_hint() {
// CHECK-LABEL: @unroll_hint
// CHECK: !llvm.loop ![[HINT:[0-9]+]]
let mut i = 0;
#[unroll]
#[rustc_unroll]
while i < 10 {
unsafe { maybe_has_side_effect() }
i += 1;
Expand All @@ -28,7 +28,7 @@ pub fn unroll_full() {
// CHECK-LABEL: @unroll_full
// CHECK: !llvm.loop ![[FULL:[0-9]+]]
let mut i = 0;
#[unroll(full)]
#[rustc_unroll(full)]
while i < 10 {
unsafe { maybe_has_side_effect() }
i += 1;
Expand All @@ -40,7 +40,7 @@ pub fn unroll_never() {
// CHECK-LABEL: @unroll_never
// CHECK: !llvm.loop ![[DISABLE:[0-9]+]]
let mut i = 0;
#[unroll(never)]
#[rustc_unroll(never)]
while i < 10 {
unsafe { maybe_has_side_effect() }
i += 1;
Expand All @@ -52,7 +52,7 @@ pub fn unroll_count() {
// CHECK-LABEL: @unroll_count
// CHECK: !llvm.loop ![[COUNT:[0-9]+]]
let mut i = 0;
#[unroll(5)]
#[rustc_unroll(5)]
while i < 10 {
unsafe { maybe_has_side_effect() }
i += 1;
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/attributes/unroll/invalid-unroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
#![crate_type = "lib"]

pub fn main() {
#[unroll(please)] //~ ERROR malformed `unroll` attribute input
#[rustc_unroll(please)] //~ ERROR malformed `rustc_unroll` attribute input
for _ in 0..10 {}

#[unroll("never")] //~ ERROR malformed `unroll` attribute input
#[rustc_unroll("never")] //~ ERROR malformed `rustc_unroll` attribute input
for _ in 0..10 {}

#[unroll()] //~ ERROR malformed `unroll` attribute input
#[rustc_unroll()] //~ ERROR malformed `rustc_unroll` attribute input
for _ in 0..10 {}

#[unroll(-1)] //~ ERROR expected a literal
#[rustc_unroll(-1)] //~ ERROR expected a literal
for _ in 0..10 {}

#[unroll(1.5)] //~ ERROR malformed `unroll` attribute input
#[rustc_unroll(1.5)] //~ ERROR malformed `rustc_unroll` attribute input
for _ in 0..10 {}
}
50 changes: 25 additions & 25 deletions tests/ui/attributes/unroll/invalid-unroll.stderr
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
error[E0539]: malformed `unroll` attribute input
error[E0539]: malformed `rustc_unroll` attribute input
--> $DIR/invalid-unroll.rs:5:7
|
LL | #[unroll(please)]
| ^^^^^^^------^
| |
| valid arguments are `full` or `never`
LL | #[rustc_unroll(please)]
| ^^^^^^^^^^^^^------^
| |
| valid arguments are `full` or `never`

error[E0539]: malformed `unroll` attribute input
error[E0539]: malformed `rustc_unroll` attribute input
--> $DIR/invalid-unroll.rs:8:7
|
LL | #[unroll("never")]
| ^^^^^^^-------^
| |
| valid arguments are `full` or `never`
LL | #[rustc_unroll("never")]
| ^^^^^^^^^^^^^-------^
| |
| valid arguments are `full` or `never`

error[E0805]: malformed `unroll` attribute input
error[E0805]: malformed `rustc_unroll` attribute input
--> $DIR/invalid-unroll.rs:11:7
|
LL | #[unroll()]
| ^^^^^^--
| |
| expected an argument here
LL | #[rustc_unroll()]
| ^^^^^^^^^^^^--
| |
| expected an argument here

error: expected a literal (`1u8`, `1.0f32`, `"string"`, etc.) here, found expression
--> $DIR/invalid-unroll.rs:14:14
--> $DIR/invalid-unroll.rs:14:20
|
LL | #[unroll(-1)]
| ^^ expressions are not allowed here
LL | #[rustc_unroll(-1)]
| ^^ expressions are not allowed here
|
help: negative numbers are not literals, try removing the `-` sign
|
LL - #[unroll(-1)]
LL + #[unroll(1)]
LL - #[rustc_unroll(-1)]
LL + #[rustc_unroll(1)]
|

error[E0539]: malformed `unroll` attribute input
error[E0539]: malformed `rustc_unroll` attribute input
--> $DIR/invalid-unroll.rs:17:7
|
LL | #[unroll(1.5)]
| ^^^^^^^---^
| |
| valid arguments are `full` or `never`
LL | #[rustc_unroll(1.5)]
| ^^^^^^^^^^^^^---^
| |
| valid arguments are `full` or `never`

error: aborting due to 5 previous errors

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/feature-gates/feature-gate-loop-hints.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
#[unroll] //~ ERROR the `unroll` attribute is an experimental feature
#[rustc_unroll] //~ ERROR the `rustc_unroll` attribute is an experimental feature
for _ in 0..10 {}
}
6 changes: 3 additions & 3 deletions tests/ui/feature-gates/feature-gate-loop-hints.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0658]: the `unroll` attribute is an experimental feature
error[E0658]: the `rustc_unroll` attribute is an experimental feature
--> $DIR/feature-gate-loop-hints.rs:2:7
|
LL | #[unroll]
| ^^^^^^
LL | #[rustc_unroll]
| ^^^^^^^^^^^^
|
= note: see issue #156874 <https://github.com/rust-lang/rust/issues/156874> for more information
= help: add `#![feature(loop_hints)]` to the crate attributes to enable
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/feature-gates/feature-gate-rustc-attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ error: cannot find attribute `rustc_unknown` in this scope
|
LL | #[rustc_unknown]
| ^^^^^^^^^^^^^
|
help: a built-in attribute with a similar name exists
|
LL - #[rustc_unknown]
LL + #[rustc_unroll]
|

error[E0658]: use of an internal attribute
--> $DIR/feature-gate-rustc-attrs.rs:20:3
Expand Down
Loading