Odin icon indicating copy to clipboard operation
Odin copied to clipboard

Bad interaction of intrinsics.read_cycle_counter(), vendor:sdl2, context.logger, -opt:3 on darwin ARM64

Open StanislavNikolov opened this issue 2 years ago • 1 comments

intrinsics.read_cycle_counter() + vendor:sdl2 + context.logger + -opt:3 somehow make the produced binary not start correctly, but removing any of them fixes it. Possibly related to #1615

Look at the following code samples:

A

package main

import "core:fmt"
import "core:log"
import "core:math/rand"

import "vendor:sdl2"
CTX :: struct { window: ^sdl2.Window, }
ctx := CTX{}

main :: proc() {
	context.logger = log.create_console_logger()

	fmt.println("BEGIN")
	fmt.println(int(rand.int31_max(1000)))
}

B (same as A, 2 lines commented out)

package main

import "core:fmt"
// import "core:log"
import "core:math/rand"

import "vendor:sdl2"
CTX :: struct { window: ^sdl2.Window, }
ctx := CTX{}

main :: proc() {
	// context.logger = log.create_console_logger()

	fmt.println("BEGIN")
	fmt.println(int(rand.int31_max(1000)))
}

C (same as A, 3 lines commented out)

package main

import "core:fmt"
import "core:log"
import "core:math/rand"

// import "vendor:sdl2"
// CTX :: struct { window: ^sdl2.Window, }
// ctx := CTX{}

main :: proc() {
	context.logger = log.create_console_logger()

	fmt.println("BEGIN")
	fmt.println(int(rand.int31_max(1000)))
}

A, B and C compile successfully with -opt:3, but only B and C manage to execute successfully. Here's what happens with A

$ odin build A.odin -extra-linker-flags:-L/opt/homebrew/lib -vet -opt:3 
$ lldb ./A   
(lldb) target create "./A"
Current executable set to '/Users/stjo/projects/toys/road/A' (arm64).
(lldb) run
Process 58316 launched: '/Users/stjo/projects/toys/road/A' (arm64)
Process 58316 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x46f8de3bf53)
    frame #0: 0x0000000100005e64 A`__$startup_runtime + 12748
A`__$startup_runtime:
->  0x100005e64 <+12748>: stp    x10, x8, [x9, #0x48]
    0x100005e68 <+12752>: adr    x8, #0x32d38              ; rand.global_rand-1131
    0x100005e6c <+12756>: nop    
    0x100005e70 <+12760>: stur   q6, [x1, #0xf8]
Target 0: (A) stopped.
(lldb) thread backtrace
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x46f8de3bf53)
  * frame #0: 0x0000000100005e64 A`__$startup_runtime + 12748
    frame #1: 0x0000000100029950 A`main + 208
    frame #2: 0x00000001000450f4 dyld`start + 520

If in core/math/rand.odin the line

global_rand := create(u64(intrinsics.read_cycle_counter()))

is replaced with

global_rand := create(u64(0))

then all the programs above execute correctly.

Output of odin report:

Odin: dev-2022-03:1d147ba
OS:   macOS 12.0.3 Monterey -- this is incorrect, it is actually 12.2.1
CPU:  ARM64
RAM:  16384 MiB

StanislavNikolov avatar Mar 15 '22 19:03 StanislavNikolov

Hello!

I am marking this issue as stale as it has not received any engagement from the community or maintainers 120 days. That does not imply that the issue has no merit! If you feel strongly about this issue

  • open a PR referencing and resolving the issue;
  • leave a comment on it and discuss ideas how you could contribute towards resolving it;
  • leave a comment and describe in detail why this issue is critical for your use case;
  • open a new issue with updated details and a plan on resolving the issue.

The motivation for this automation is to help prioritize issues in the backlog and not ignore, reject, or belittle anyone..

github-actions[bot] avatar Jul 24 '22 22:07 github-actions[bot]