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

Merged
merged 5 commits into from
Nov 29, 2024

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 requested a review from maleadt November 17, 2024 11:11
@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 6 times, most recently 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.

@maleadt maleadt force-pushed the tg/metal-global-constant-addrspace branch from 18b9c3d to 526f2ec Compare November 29, 2024 11:16
@maleadt maleadt merged commit a849e8a into master Nov 29, 2024
17 of 18 checks passed
@maleadt maleadt deleted the tg/metal-global-constant-addrspace branch November 29, 2024 12:12
maleadt referenced this pull request Jan 10, 2025
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