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

[Metal] Add correct addrspace to global constants #648

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

tgymnich
Copy link
Member

@tgymnich tgymnich commented Nov 17, 2024

Global constants need to be located in address space 2 in Metal:

This fixes the following use case:

using Metal
using StaticArrays

const xs = SVector(1.0f0, 2.0f0, 3.0f0)

function kernel_fma(a, b, x, out)
    i = thread_position_in_grid_1d()
    a_val = a[i]
    b_val = b[i]
    x_val = x[i]
    
    out[i] = fma(a_val, b_val, xs[x_val])

    return
end

len=1024
a = MtlArray(rand(Float32, len))
b = MtlArray(rand(Float32, len))
x = MtlArray(rand(1:3, len))
out = similar(a)

threads = 1024

@device_code_llvm dump_module=true raw=true @metal threads=threads kernel_fma(a, b, x, out)

@tgymnich tgymnich self-assigned this Nov 17, 2024
@tgymnich tgymnich added the metal Stuff about the Apple metal back-end. label Nov 17, 2024
@tgymnich tgymnich force-pushed the tg/metal-global-constant-addrspace branch 5 times, most recently from 528b439 to aa03c15 Compare November 17, 2024 22:05
@tgymnich tgymnich force-pushed the tg/metal-global-constant-addrspace branch from aa03c15 to e2e8153 Compare November 17, 2024 22:09
src/metal.jl Outdated Show resolved Hide resolved
src/metal.jl Outdated Show resolved Hide resolved
@maleadt maleadt assigned maleadt and unassigned tgymnich Nov 26, 2024
@maleadt
Copy link
Member

maleadt commented Nov 26, 2024

Good thing I added a test; looks like my changes trip up an LLVM assertion:

julia: /workspace/srcdir/llvm-project/llvm/lib/IR/Constants.cpp:2231: static llvm::Constant* llvm::ConstantExpr::getBitCast(llvm::Constant*, llvm::Type*, bool): Assertion `CastInst::castIsValid(Instruction::BitCast, C, DstTy) && "Invalid constantexpr bitcast!"' failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metal Stuff about the Apple metal back-end.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants