Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branching inside hot loop based on const condition outside loop? #6

Open
expikr opened this issue Nov 10, 2023 · 0 comments
Open

Branching inside hot loop based on const condition outside loop? #6

expikr opened this issue Nov 10, 2023 · 0 comments

Comments

@expikr
Copy link

expikr commented Nov 10, 2023

pub fn dgemm(
    trans_a: u8,
    trans_b: u8,
    m: usize,
    n: usize,
    l: usize,
    alpha: f64,
    a: [*]const f64,
    lda: usize,
    b: [*]const f64,
    ldb: usize,
    beta: f64,
    c: [*]f64,
    ldc: usize,
) void {
    if ( m==0 or n==0 or ( ((alpha==0) or (l==0)) and (beta==1) ) ) return;
    const a_t: bool = (trans_a=='n' or trans_a=='N');
    const b_t: bool = (trans_b=='n' or trans_b=='N');
    const clr: bool = (beta==0);
    const skp: bool = (alpha==0);
    for (0..m) |j| {
        for (0..n) |i| {
            const c_index = i+ldc*j;
            c[c_index] = if (clr) 0 else beta*c[c_index];
            if (skp) continue;
            for (0..l) |k| {
                const a_index = if (a_t) k+lda*j else i+lda*k;
                const b_index = if (b_t) i+ldb*k else k+ldb*j;
                c[c_index] = @mulAdd(T, alpha*a[a_index], b[b_index], c[c_index]);
            }
        }
    }
}

In the above hot loop, the four conditionals within the hot loop have their condition pre-evaluated outside, only the value determined within.

There are two things to wonder:

  1. since the loop indices are runtime-known, would the compiler be able recognize it as a hot loop and just expand the above "loop of cases" into "cases of loops"?
  2. suppose the compiler keeps the loop branch structure as-is, how much do the const pre-declarations help out the branch predictor?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant