Skip to content

Cpp miscompilation when using mutable arguments #447

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

Open
truelossless opened this issue Jan 22, 2025 · 3 comments
Open

Cpp miscompilation when using mutable arguments #447

truelossless opened this issue Jan 22, 2025 · 3 comments

Comments

@truelossless
Copy link

Hello,

Thanks for the great work on CubeCL!
I've noticed that using mutable arguments on a #[cube] kernel causes a cpp miscompilation while using CUDA as the backend.

Example code:

This cube function...

#[cube]
pub fn foo(mut idx: u64) -> u64 {
    idx += 1;
    idx
}

...compiles to...

const uint64 l_2 = output_0[idxGlobal];
const uint64 l_2 = l_2 + uint64(1);
output_0[idxGlobal] = l_2;

...which causes

    default_program(23): error: "l_2" has already been declared in the current scope
      const uint64 l_2 = l_2 + uint64(1);
                   ^
    default_program(23): warning #549-D: variable "l_2" is used before its value is set
      const uint64 l_2 = l_2 + uint64(1);
                         ^
    Remark: The warnings can be suppressed with "-diag-suppress <warning-number>"
    default_program(22): warning #177-D: variable "l_2" was declared but never referenced
      const uint64 l_2 = output_0[idxGlobal];
                   ^
    1 error detected in the compilation of "default_program".

Copying the argument in the function instead of mutating it works fine:

#[cube]
pub fn foo(idx: u64) -> u64 {
    let mut idx2 = idx;
    idx2 += 1;
    idx2
}
@maxtremblay
Copy link
Collaborator

Can you validate if you are running the latest version of cubecl? I fixed a bunch of these lately.

@truelossless
Copy link
Author

truelossless commented Jan 23, 2025

Yes, it still happens in #a43015e2.
I haven't tested it toroughly, but I think variable shadowing can miscompile too. Nevermind, it was on my side.

@nathanielsimard
Copy link
Member

I don't think we parse mutability correctly in CubeCL yet. It's because there is comptime mutability (which we handle correctly), referring to the manipulation of symbols at compile time, and "real" mutability, which involves actually changing a register's value at runtime. For now, you can use let mut variable = variable.runtime() to declare the latter.

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

4 participants
@nathanielsimard @truelossless @maxtremblay and others