Skip to content

Commit 7b814be

Browse files
Rollup merge of rust-lang#146900 - taiki-e:avr-target-feature, r=workingjubilee
Add avr_target_feature This adds the following unstable target features (tracking issue: rust-lang#146889): - The following two are particularly important for properly supporting inline assembly: - `tinyencoding`: AVR has devices that reduce the number of registers, similar to RISC-V's RV32E. This feature is necessary to support inline assembly in such devices. (see also rust-lang#146901) - `lowbytefirst`: AVR's memory access is per 8-bit, and when writing 16-bit ports, the bytes must be written in a specific order. This order depends on devices, making this feature necessary to write proper inline assembly for such use cases. (see also llvm/llvm-project@2a52876) - The followings help recognizing whether specific instructions are available: - `addsubiw` - `break` - `eijmpcall` - `elpm` - `elpmx` - `ijmpcall` - `jmpcall` - `lpm` - `lpmx` - `movw` - `mul` - `rmw` - `spm` - `spmx` Of these, all except `addsubiw`, `break`, `ijmpcall`, `lpm`, `rmw`, `spm`, and `spmx` have [corresponding conditional codes in avr-libc](https://github.com/search?q=repo%3Aavrdudes%2Favr-libc+%2F__AVR_HAVE_%2F&type=code&p=1). LLVM also has `des` feature, but I excluded it from this PR because [DES](https://en.wikipedia.org/wiki/Data_Encryption_Standard) is insecure. - Report future-incompatible warning (rust-lang#116344) for -C target-feature=-sram and -C target-cpu=<device_without_sram> cases because SRAM is minimum requirement for non-assembly language in both avr-gcc and LLVM. - See rust-lang#146900 (comment) for details. LLVM also has `smallstack`, `wrappingrjmp`, and `memmappedregs` features, but I skipped them because they didn't seem to belong to either of the above categories, but I might have missed something. (The feature names are match with [definitions in LLVM](https://github.com/llvm/llvm-project/blob/llvmorg-21.1.0/llvm/lib/Target/AVR/AVRDevices.td).) cc @Patryk27 @Rahix r? workingjubilee @rustbot label +O-AVR +A-target-feature
2 parents 56aaf58 + 895ea0c commit 7b814be

10 files changed

Lines changed: 90 additions & 9 deletions

File tree

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ declare_features! (
373373
(unstable, async_for_loop, "1.77.0", Some(118898)),
374374
/// Allows `async` trait bound modifier.
375375
(unstable, async_trait_bounds, "1.85.0", Some(62290)),
376+
/// Target features on avr.
377+
(unstable, avr_target_feature, "CURRENT_RUSTC_VERSION", Some(146889)),
376378
/// Allows using Intel AVX10 target features and intrinsics
377379
(unstable, avx10_target_feature, "1.88.0", Some(138843)),
378380
/// Target features on bpf.

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@ symbols! {
577577
automatically_derived,
578578
available_externally,
579579
avr,
580+
avr_target_feature,
580581
avx,
581582
avx10_target_feature,
582583
avx512_target_feature,

compiler/rustc_target/src/target_features.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,28 @@ static M68K_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
898898
// tidy-alphabetical-end
899899
];
900900

901+
static AVR_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
902+
// tidy-alphabetical-start
903+
("addsubiw", Unstable(sym::avr_target_feature), &[]),
904+
("break", Unstable(sym::avr_target_feature), &[]),
905+
("eijmpcall", Unstable(sym::avr_target_feature), &[]),
906+
("elpm", Unstable(sym::avr_target_feature), &[]),
907+
("elpmx", Unstable(sym::avr_target_feature), &[]),
908+
("ijmpcall", Unstable(sym::avr_target_feature), &[]),
909+
("jmpcall", Unstable(sym::avr_target_feature), &[]),
910+
("lowbytefirst", Unstable(sym::avr_target_feature), &[]),
911+
("lpm", Unstable(sym::avr_target_feature), &[]),
912+
("lpmx", Unstable(sym::avr_target_feature), &[]),
913+
("movw", Unstable(sym::avr_target_feature), &[]),
914+
("mul", Unstable(sym::avr_target_feature), &[]),
915+
("rmw", Unstable(sym::avr_target_feature), &[]),
916+
("spm", Unstable(sym::avr_target_feature), &[]),
917+
("spmx", Unstable(sym::avr_target_feature), &[]),
918+
("sram", Forbidden { reason: "devices that have no SRAM are unsupported" }, &[]),
919+
("tinyencoding", Unstable(sym::avr_target_feature), &[]),
920+
// tidy-alphabetical-end
921+
];
922+
901923
/// When rustdoc is running, provide a list of all known features so that all their respective
902924
/// primitives may be documented.
903925
///
@@ -919,6 +941,7 @@ pub fn all_rust_features() -> impl Iterator<Item = (&'static str, Stability)> {
919941
.chain(IBMZ_FEATURES)
920942
.chain(SPARC_FEATURES)
921943
.chain(M68K_FEATURES)
944+
.chain(AVR_FEATURES)
922945
.cloned()
923946
.map(|(f, s, _)| (f, s))
924947
}
@@ -996,12 +1019,8 @@ impl Target {
9961019
Arch::S390x => IBMZ_FEATURES,
9971020
Arch::Sparc | Arch::Sparc64 => SPARC_FEATURES,
9981021
Arch::M68k => M68K_FEATURES,
999-
Arch::AmdGpu
1000-
| Arch::Avr
1001-
| Arch::Msp430
1002-
| Arch::SpirV
1003-
| Arch::Xtensa
1004-
| Arch::Other(_) => &[],
1022+
Arch::Avr => AVR_FEATURES,
1023+
Arch::AmdGpu | Arch::Msp430 | Arch::SpirV | Arch::Xtensa | Arch::Other(_) => &[],
10051024
}
10061025
}
10071026

@@ -1023,11 +1042,11 @@ impl Target {
10231042
MIPS_FEATURES_FOR_CORRECT_FIXED_LENGTH_VECTOR_ABI
10241043
}
10251044
Arch::AmdGpu => AMDGPU_FEATURES_FOR_CORRECT_FIXED_LENGTH_VECTOR_ABI,
1026-
Arch::Nvptx64 | Arch::Bpf | Arch::M68k => &[], // no vector ABI
1045+
Arch::Nvptx64 | Arch::Bpf | Arch::M68k | Arch::Avr => &[], // no vector ABI
10271046
Arch::CSky => CSKY_FEATURES_FOR_CORRECT_FIXED_LENGTH_VECTOR_ABI,
10281047
// FIXME: for some tier3 targets, we are overly cautious and always give warnings
10291048
// when passing args in vector registers.
1030-
Arch::Avr | Arch::Msp430 | Arch::SpirV | Arch::Xtensa | Arch::Other(_) => &[],
1049+
Arch::Msp430 | Arch::SpirV | Arch::Xtensa | Arch::Other(_) => &[],
10311050
}
10321051
}
10331052

@@ -1224,6 +1243,12 @@ impl Target {
12241243
// because the vector and float registers overlap.
12251244
FeatureConstraints { required: &[], incompatible: &["soft-float"] }
12261245
}
1246+
Arch::Avr => {
1247+
// SRAM is minimum requirement for C/C++ in both avr-gcc and Clang,
1248+
// and backends of them only support assembly for devices have no SRAM.
1249+
// See the discussion in https://github.com/rust-lang/rust/pull/146900 for more.
1250+
FeatureConstraints { required: &["sram"], incompatible: &[] }
1251+
}
12271252
_ => NOTHING,
12281253
}
12291254
}

src/doc/rustc/src/platform-support/avr-none.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ the possible variants:
6868

6969
https://github.com/llvm/llvm-project/blob/093d4db2f3c874d4683fb01194b00dbb20e5c713/clang/lib/Basic/Targets/AVR.cpp#L32
7070

71+
Note that devices that have no SRAM are not supported, same as when compiling C/C++ programs with avr-gcc or Clang.
72+
7173
## Testing
7274

7375
You can use [`simavr`](https://github.com/buserror/simavr) to emulate the
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
warning: target feature `sram` cannot be disabled with `-Ctarget-feature`: devices that have no SRAM are unsupported
2+
|
3+
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4+
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
5+
6+
warning: target feature `sram` must be enabled to ensure that the ABI of the current target can be implemented correctly
7+
|
8+
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
9+
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
10+
11+
warning: 2 warnings emitted
12+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
warning: target feature `sram` must be enabled to ensure that the ABI of the current target can be implemented correctly
2+
|
3+
= note: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
4+
= note: for more information, see issue #116344 <https://github.com/rust-lang/rust/issues/116344>
5+
6+
warning: 1 warning emitted
7+

tests/ui/abi/avr-sram.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ revisions: has_sram no_sram disable_sram
2+
//@ build-pass
3+
//@[has_sram] compile-flags: --target avr-none -C target-cpu=atmega328p
4+
//@[has_sram] needs-llvm-components: avr
5+
//@[no_sram] compile-flags: --target avr-none -C target-cpu=attiny11
6+
//@[no_sram] needs-llvm-components: avr
7+
//@[disable_sram] compile-flags: --target avr-none -C target-cpu=atmega328p -C target-feature=-sram
8+
//@[disable_sram] needs-llvm-components: avr
9+
//@ ignore-backends: gcc
10+
//[no_sram,disable_sram]~? WARN target feature `sram` must be enabled
11+
//[disable_sram]~? WARN target feature `sram` cannot be disabled with `-Ctarget-feature`
12+
13+
#![feature(no_core)]
14+
#![no_core]
15+
#![crate_type = "lib"]

tests/ui/check-cfg/target_feature.stderr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
1414
`7e10`
1515
`a`
1616
`aclass`
17+
`addsubiw`
1718
`adx`
1819
`aes`
1920
`altivec`
@@ -57,6 +58,7 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
5758
`bf16`
5859
`bmi1`
5960
`bmi2`
61+
`break`
6062
`bti`
6163
`bulk-memory`
6264
`c`
@@ -83,6 +85,9 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
8385
`e2`
8486
`ecv`
8587
`edsp`
88+
`eijmpcall`
89+
`elpm`
90+
`elpmx`
8691
`elrw`
8792
`enhanced-sort`
8893
`ermsb`
@@ -148,6 +153,7 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
148153
`hvxv79`
149154
`hwdiv`
150155
`i8mm`
156+
`ijmpcall`
151157
`isa-68000`
152158
`isa-68010`
153159
`isa-68020`
@@ -156,6 +162,7 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
156162
`isa-68060`
157163
`isa-68881`
158164
`isa-68882`
165+
`jmpcall`
159166
`jsconv`
160167
`kl`
161168
`lahfsahf`
@@ -166,6 +173,9 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
166173
`ld-seq-sa`
167174
`leoncasa`
168175
`lor`
176+
`lowbytefirst`
177+
`lpm`
178+
`lpmx`
169179
`lse`
170180
`lse128`
171181
`lse2`
@@ -187,11 +197,13 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
187197
`mops`
188198
`movbe`
189199
`movrs`
200+
`movw`
190201
`mp`
191202
`mp1e2`
192203
`msa`
193204
`msync`
194205
`mte`
206+
`mul`
195207
`multivalue`
196208
`mutable-globals`
197209
`neon`
@@ -256,6 +268,7 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
256268
`reference-types`
257269
`relax`
258270
`relaxed-simd`
271+
`rmw`
259272
`rtm`
260273
`rva23u64`
261274
`sb`
@@ -308,6 +321,8 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
308321
`sme2p1`
309322
`soft-float`
310323
`spe`
324+
`spm`
325+
`spmx`
311326
`ssbs`
312327
`sse`
313328
`sse2`
@@ -332,6 +347,7 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
332347
`tbm`
333348
`thumb-mode`
334349
`thumb2`
350+
`tinyencoding`
335351
`tme`
336352
`transactional-execution`
337353
`trust`

tests/ui/target-feature/gate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// gate-test-sparc_target_feature
2121
// gate-test-x87_target_feature
2222
// gate-test-m68k_target_feature
23+
// gate-test-avr_target_feature
2324

2425
#[target_feature(enable = "x87")]
2526
//~^ ERROR: currently unstable

tests/ui/target-feature/gate.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: the target feature `x87` is currently unstable
2-
--> $DIR/gate.rs:24:18
2+
--> $DIR/gate.rs:25:18
33
|
44
LL | #[target_feature(enable = "x87")]
55
| ^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)