odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
7k stars 621 forks source link

Transmuting a non-shadowed matrix parameter to an array causes a segfault at runtime #4392

Open amjoshuamichael opened 1 month ago

amjoshuamichael commented 1 month ago

Context

Odin: dev-2024-10 OS: Arch Linux, Linux 6.11.3-arch1-1 CPU: Intel(R) Core(TM) i9-14900HX RAM: 15693 MiB Backend: LLVM 18.1.8

Expected Behavior

The matrix should be transmuted to a simd vector, so I can process it as such. A function that does this exact operation has been working in my project for months. I'm not quite sure the exact specification for how odin shadowing should work with transmutes and large arguments, but I think it makes sense that this should work the way it has been, and that I shouldn't have to shadow the variable, since arrays work without shadowing.

Current Behavior

A segfault occurs on the line with the transmute.

Failure Information (for bugs)

Here's the lldb output when running the minimal reproducible example below.

transmuting to an array in a function works fine!
shadowing the matrix first is totally fine!
an array to simd? also fine.
Process 91562 launched: ...
Process 91562 stopped
* thread #1, name = 'odinbug', stop reason = signal SIGSEGV: invalid address (fault address: 0x0)
    frame #0: 0x000000000040363e odinbug`odinbug.transmute_to_simd(mat="") at main.odin:31:5
   28   }
   29   
   30   transmute_to_simd :: proc(mat: matrix[4,4]u8) { 
-> 31      t := transmute(#simd[16]u8)mat 
   32   }

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

package odinbug

import "core:fmt"

main :: proc() {
    mymat: matrix[4,4]u8 = 0
    transmute_to_array(mymat)
    fmt.println("transmuting to an array in a function works fine!")
    transmute_to_simd_good(mymat)
    fmt.println("shadowing the matrix first is totally fine!")
    transmute_array_to_simd({})
    fmt.println("an array to simd? also fine.")
    transmute_to_simd(mymat)
    fmt.println("simd will crash. (this doesn't get printed)")
}

transmute_to_array :: proc(mat: matrix[4,4]u8) { 
    t := transmute([16]u8)mat 
}

transmute_to_simd_good :: proc(mat: matrix[4,4]u8) { 
    mat := mat
    t := transmute(#simd[16]u8)mat 
}

transmute_array_to_simd :: proc(mat: [16]u8) { 
    t := transmute(#simd[16]u8)mat 
}

transmute_to_simd :: proc(mat: matrix[4,4]u8) { 
    t := transmute(#simd[16]u8)mat 
}
laytan commented 1 month ago

I can't reproduce it on MacOS (arm and Rosette/amd64), I will try testing on Linux when I have that machine turned on.

amjoshuamichael commented 1 month ago

Hmm, that's odd. It's possible that the issue is on my end, but with something like this it seems unlikely. Still, if you need me to do any more tests, please let me know.

tf2spi commented 1 month ago

I'm also having a hard time reproducing this on Debian :(