Skip to content

target_feature(enable = ...) interferes with core::hint::cold_path #156859

@orlp

Description

@orlp

I tried this code:

pub unsafe fn normal(x: &[u8]) -> bool {
    if x.len() < 16 {
        std::hint::cold_path();
        slow();
        return false;
    }

    x[..16] == [0; 16]
}

#[target_feature(enable = "sse2")]
pub unsafe fn with_feature(x: &[u8]) -> bool {
    if x.len() < 16 {
        std::hint::cold_path();
        slow();
        return false;
    }

    x[..16] == [0; 16]
}

unsafe extern "C" {
    safe fn slow();
}

I expected to see this happen: I expected both to have identical code generation.

Instead, this happened: when target_feature is enabled it compiles as if the cold_path() isn't there. See this godbolt:

example[88559e5427b91048]::normal:
        cmp     rsi, 15
        jbe     .LBB1_1
        mov     rax, qword ptr [rdi]
        or      rax, qword ptr [rdi + 8]
        sete    al
        ret
.LBB1_1:
        push    rax
        call    qword ptr [rip + slow@GOTPCREL]
        xor     eax, eax
        add     rsp, 8
        ret

example[88559e5427b91048]::with_feature:
        cmp     rsi, 16
        jae     .LBB0_1
        push    rax
        call    qword ptr [rip + slow@GOTPCREL]
        xor     eax, eax
        add     rsp, 8
        ret
.LBB0_1:
        mov     rax, qword ptr [rdi]
        or      rax, qword ptr [rdi + 8]
        sete    al
        ret

Note that target_feature(enable = "sse2") should be a no-op, both functions are compiled with SSE2 support anyway.

Meta

Occurs on both latest nightly and 1.95.

Metadata

Metadata

Assignees

Labels

A-codegenArea: Code generationC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions