Skip to content

Keep aligned_sizeof a compile-time constant under --check-bounds=no - #1256

Open
JorgeG94 wants to merge 1 commit into
JuliaGPU:mainfrom
JorgeG94:fix-aligned-sizeof-checkbounds
Open

JorgeG94 wants to merge 1 commit into
JuliaGPU:mainfrom
JorgeG94:fix-aligned-sizeof-checkbounds

Conversation

@JorgeG94

Copy link
Copy Markdown

Keep aligned_sizeof a compile-time constant under --check-bounds=no

With --check-bounds=no, Julia (seen on 1.13.4) stops constant-folding Base.aligned_sizeof(T). It compiles to a run-time load of the type's layout from the DataType object:

  julia --check-bounds=no -e 'using InteractiveUtils; f() = Base.aligned_sizeof(Float32); code_llvm(f, ())'
    %.layout = load atomic ptr, ptr getelementptr inbounds nuw (i8, ptr @"+Core.Float32#283.jit", i64 40) unordered, align 8

Since Julia 1.11, AMDGPU uses Base.aligned_sizeof directly. A ROCDeviceArray built in device code computes its maxsize with it, and local memory (@LocalMem, @ROCStaticLocalArray) builds one there. So under --check-bounds=no, any kernel using local memory loads the DataType from host memory and faults with HSA_STATUS_ERROR_MEMORY_FAULT. In GCN the fault shows as an s_getpc_b64/@rel32 load of …_Core_Float32… followed by a flat_load … offset:40.

Every reduction uses local memory, so sum, any and mapreduce all fault. So does the precompile workload of AcceleratedKernels' AMDGPU extension, which then never caches. Seen with AMDGPU 2.13.1, KernelAbstractions 0.10.9 and AcceleratedKernels 0.5.14, on MI250X (Frontier) and MI210, both gfx90a.

Minimal repro:

  # julia --check-bounds=no repro.jl
  using AMDGPU
  function k!(a)
      i = workitemIdx().x
      s = @ROCStaticLocalArray(Float32, 64)
      s[i] = a[i]; sync_workgroup(); a[i] = s[i] + 1f0
      return
  end
  a = AMDGPU.ones(Float32, 64)
  @roc groupsize=64 k!(a); AMDGPU.synchronize()

The fix uses the @generated aligned_sizeof, which was already there for Julia < 1.11, on every version. The added test runs that kernel in a --check-bounds=no subprocess. Note that the subprocess may have to precompile under that flag on a cold depot; with warm caches it takes about 5 s.

🤖 Generated with Claude Code

With `--check-bounds=no`, Julia no longer folds `Base.aligned_sizeof(T)`: it
reads the element type's layout from the `DataType` at run time. A
`ROCDeviceArray` built in device code, as local memory is (`@localmem`,
`@ROCStaticLocalArray`), computes its `maxsize` with it, so the kernel loaded
the type object from host memory and faulted (HSA_STATUS_ERROR_MEMORY_FAULT).
Every reduction uses local memory, so `sum`, `any` and the precompile workload
of AcceleratedKernels' AMDGPU extension all faulted.

Use the generated `aligned_sizeof` on every Julia version, not only before 1.11.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant