Skip to content

Lang proposal: Allow #[cfg(...)] within asm! #140279

Open
@tgross35

Description

@tgross35

Background

Currently there isn't an easy way to enable or disable portions of assembly based on configuration. This can be a annoyance for asm! that can be worked around with multiple assembly blocks, but it is especially a problem in global_asm! and naked_asm! because small config-based changes mean the entire assembly block needs to be duplicated.

Another workaround is possible by defining multiple macro_rules! of the same name that are enabled/disabled based on the desired config and expand to strings. This option is inconvenient and fragments the assembly.

Proposal

Allow #[cfg(...)] in all assembly macros:

asm!( // or global_asm! or naked_asm!
    "nop",
    #[cfg(target_feature = "sse2")]
    "nop",
    // ...
    #[cfg(target_feature = "sse2")]
    a = const 123, // only used on sse2
);

The configuration applies to a single comma-separated segment.

We may want to also support blocks in order to allow grouping instructions without merging strings, or grouping directives:

asm!(
    "nop",
    #[cfg(target_feature = "sse2")] {
        "nop",
        "nop",
        "nop",
    } // does allowing/requiring a comma here make sense?
    "nop",
    #[cfg(target_feature = "sse2")] {
        a = const 123,
        a = const 123,
    }
);

This design has some concerns and there may be a better option, see discussion in the thread.

Zulip discussion: #project-inline-asm > `cfg` in assembly blocks

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inline-assemblyArea: Inline assembly (`asm!(…)`)C-feature-requestCategory: A feature request, i.e: not implemented / a PR.I-lang-radarItems that are on lang's radar and will need eventual work or consideration.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language 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

    Issue actions