Skip to content

Warn if feature settings may break compilation #89586

Open
@workingjubilee

Description

@workingjubilee
Member

Currently, if a user instructs the compiler to adjust the features for compilation, or uses #[target_feature] to set unusual feature settings for a function, or introduces a custom target, this can trigger miscompilations if the settings are improperly "aligned" with each other (to do correct parameter passing and so on). This mostly impacts x86, due to its particular architectural extensions, but it could affect other platforms.

Some examples we definitely want to warn on:

Some of these issues are currently caught by LLVM, but Rust programmers often find underlying LLVM errors surfacing to be mysterious and cryptic, and in this case we can definitely detect them and warn about them ourselves.

In addition, due to the desire for binary floating point conformance per #10087, we probably want to emit a warning for any build configuration such that, despite having an FPU we consider to be conformant and desirable, disables the ability to use such a floating point unit, such that it could introduce non-conformant floating point code. However, that can be extended into a future issue when all the known-100%-bad bases are covered.

Activity

added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
A-diagnosticsArea: Messages for errors, warnings, and lints
O-x86_64Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64)
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
and removed
E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
on Oct 6, 2021
workingjubilee

workingjubilee commented on Oct 6, 2021

@workingjubilee
MemberAuthor

This requires digging deep into the target_feature code and likely refactoring it. Much of it lives in https://github.com/rust-lang/rust/tree/master/compiler/rustc_codegen_llvm, most particularly this file, which handles things like the interface to LLVM's target features:
https://github.com/rust-lang/rust/blob/master/compiler/rustc_codegen_llvm/src/llvm_util.rs
Especially this:

/// The list of LLVM features computed from CLI flags (`-Ctarget-cpu`, `-Ctarget-feature`,
/// `--target` and similar).
// FIXME(nagisa): Cache the output of this somehow? Maybe make this a query? We're calling this
// for every function that has `#[target_feature]` on it. The global features won't change between
// the functions; only crates, maybe…
pub fn llvm_global_features(sess: &Session) -> Vec<String> {
// FIXME(nagisa): this should definitely be available more centrally and to other codegen backends.
/// These features control behaviour of rustc rather than llvm.
const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];

And this related function may also need to be reviewed, as it handles function-level setting of codegen features, like with #[target_feature]:
/// Composite function which sets LLVM attributes for function depending on its AST (`#[attribute]`)
/// attributes.
pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::Instance<'tcx>) {

And at least some of this logic should probably be, as part of this, hoisted into https://github.com/rust-lang/rust/tree/master/compiler/rustc_codegen_ssa. Accordingly, while it is a scoped piece of work, it is not a trivial task either.

added
E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
on Dec 6, 2021
Wardenfar

Wardenfar commented on Feb 26, 2022

@Wardenfar
Contributor

Hello @workingjubilee,

I'm trying to understand the issue and i have some questions.

  • For the first sub-task : i need to detect imcompatible global features or at function level ?
    and i have not found a way to detect x87 FPU.
  • To report an error : should i use sess.err(...) ?

Thanks

bstrie

bstrie commented on Mar 14, 2022

@bstrie
Contributor

@Wardenfar You may want to try asking on the #t-compiler Zulip channel: https://rust-lang.zulipchat.com/#streams/131828/t-compiler

15 remaining items

added
A-ABIArea: Concerning the application binary interface (ABI)
and removed
O-x86_64Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64)
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
O-x86_32Target: x86 processors, 32 bit (like i686-*) (IA-32)
on Nov 23, 2024
RalfJung

RalfJung commented on Nov 23, 2024

@RalfJung
Member

Oh damnit, I don't know what happened with the labels... I just added ABI and target-feature, and then github removed all the others. Not sure why.

added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
A-diagnosticsArea: Messages for errors, warnings, and lints
O-x86_64Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64)
O-x86_32Target: x86 processors, 32 bit (like i686-*) (IA-32)
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Nov 23, 2024
added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
and removed
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
on Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-ABIArea: Concerning the application binary interface (ABI)A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-target-featureArea: Enabling/disabling target features like AVX, Neon, etc.C-enhancementCategory: An issue proposing an enhancement or a PR with one.O-x86_32Target: x86 processors, 32 bit (like i686-*) (IA-32)O-x86_64Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @RalfJung@bstrie@fmease@Wardenfar@workingjubilee

      Issue actions

        Warn if feature settings may break compilation · Issue #89586 · rust-lang/rust