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

Support for exceptions #69

Open
nicolamos opened this issue Dec 30, 2022 · 6 comments
Open

Support for exceptions #69

nicolamos opened this issue Dec 30, 2022 · 6 comments
Labels
enhancement New feature or request kernels Things about kernels and how they are compiled.

Comments

@nicolamos
Copy link

For a simple situation where I need to take the floor of a number and convert it to a integer, the compilation step through GPUCompiler fails with

ERROR: InvalidIRError: compiling kernel #36#37(MtlDeviceVector{Float32, 1}) resulted in invalid LLVM IR
Reason: unsupported call to an unknown function (call to gpu_malloc)

The failing code is the following:

import Metal
Metal.@sync Metal.@metal (xs-> (x = xs[1]; k = Int(x); xs[1] = k; nothing))(Metal.MtlVector(Float32[1,2,3]))
@maleadt maleadt changed the title Conversion from float to int Support for exceptions Jan 2, 2023
@maleadt
Copy link
Member

maleadt commented Jan 2, 2023

Yeah, this is currently expected, due to Metal.jl not supporting exceptions yet. You can work around this by doing a non-throwing conversion, e.g. trunc(Int, x) or x % Int.

@maleadt maleadt added the enhancement New feature or request label Jan 2, 2023
@nicolamos
Copy link
Author

Thank you for the suggestion!

@nicolamos
Copy link
Author

Sorry, but I'm getting the same error.

  • x % Int is not defined when x <: AbstractFloat;
  • trunc(Int, x) does throw as well;
  • unsafe_trunc(Int, x) can not be ported to the GPU, I guess;
  • MT.trunc_fast does not accept an output type argument.

The error is the following:

ERROR: InvalidIRError: compiling kernel #55#56(Metal.MtlDeviceVector{Float32, 1}) resulted in invalid LLVM IR
Reason: unsupported call to an unknown function (call to gpu_malloc)
Stacktrace:
 [1] malloc
   @ ~/.julia/packages/GPUCompiler/hi5Wg/src/runtime.jl:89
 [2] macro expansion
   @ ~/.julia/packages/GPUCompiler/hi5Wg/src/runtime.jl:184
 [3] macro expansion
   @ ./none:0
 [4] box
   @ ./none:0
 [5] box_float32
   @ ~/.julia/packages/GPUCompiler/hi5Wg/src/runtime.jl:213
 [6] Int64
   @ ./float.jl:788
 [7] convert
   @ ./number.jl:7
 [8] #55
   @ ./REPL[32]:1
Hint: catch this exception as `err` and call `code_typed(err; interactive = true)` to introspect the erronous code with Cthulhu.jl
Stacktrace:
  [1] check_ir(job::GPUCompiler.CompilerJob{GPUCompiler.MetalCompilerTarget, Metal.MetalCompilerParams, GPUCompiler.FunctionSpec{var"#55#56", Tuple{Metal.MtlDeviceVector{Float32, 1}}}}, args::LLVM.Module)
    @ GPUCompiler ~/.julia/packages/GPUCompiler/hi5Wg/src/validation.jl:141
  [2] macro expansion
    @ ~/.julia/packages/GPUCompiler/hi5Wg/src/driver.jl:418 [inlined]
  [3] macro expansion
    @ ~/.julia/packages/TimerOutputs/LHjFw/src/TimerOutput.jl:253 [inlined]
  [4] macro expansion
    @ ~/.julia/packages/GPUCompiler/hi5Wg/src/driver.jl:416 [inlined]
  [5] emit_asm(job::GPUCompiler.CompilerJob, ir::LLVM.Module; strip::Bool, validate::Bool, format::LLVM.API.LLVMCodeGenFileType)
    @ GPUCompiler ~/.julia/packages/GPUCompiler/hi5Wg/src/utils.jl:68
  [6] mtlfunction_compile(job::GPUCompiler.CompilerJob, ctx::LLVM.Context)
    @ Metal ~/.julia/packages/Metal/pfCxO/src/compiler/execution.jl:168
  [7] #32
    @ ~/.julia/packages/Metal/pfCxO/src/compiler/execution.jl:161 [inlined]
  [8] JuliaContext(f::Metal.var"#32#33"{GPUCompiler.CompilerJob{GPUCompiler.MetalCompilerTarget, Metal.MetalCompilerParams, GPUCompiler.FunctionSpec{var"#55#56", Tuple{Metal.MtlDeviceVector{Float32, 1}}}}})
    @ GPUCompiler ~/.julia/packages/GPUCompiler/hi5Wg/src/driver.jl:76
  [9] mtlfunction_compile(job::GPUCompiler.CompilerJob)
    @ Metal ~/.julia/packages/Metal/pfCxO/src/compiler/execution.jl:160
 [10] cached_compilation(cache::Dict{UInt64, Any}, job::GPUCompiler.CompilerJob, compiler::typeof(Metal.mtlfunction_compile), linker::typeof(Metal.mtlfunction_link))
    @ GPUCompiler ~/.julia/packages/GPUCompiler/hi5Wg/src/cache.jl:90
 [11] mtlfunction(f::var"#55#56", tt::Type{Tuple{Metal.MtlDeviceVector{Float32, 1}}}; name::Nothing, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Metal ~/.julia/packages/Metal/pfCxO/src/compiler/execution.jl:148
 [12] mtlfunction(f::var"#55#56", tt::Type{Tuple{Metal.MtlDeviceVector{Float32, 1}}})
    @ Metal ~/.julia/packages/Metal/pfCxO/src/compiler/execution.jl:141
 [13] macro expansion
    @ ~/.julia/packages/Metal/pfCxO/src/compiler/execution.jl:64 [inlined]
 [14] top-level scope
    @ ~/.julia/packages/GPUCompiler/hi5Wg/src/reflection.jl:201

@maleadt
Copy link
Member

maleadt commented Jan 2, 2023

unsafe_trunc(Int, x) can not be ported to the GPU, I guess;

Sorry, that's the one I meant, not plain trunc. Why does that not work for you on the GPU? k = unsafe_trunc(Int, x) does the job here.

@nicolamos
Copy link
Author

Yes sorry, it works. I don’t know what was the issue… maybe I forgot the Int argument somewhere.

I also have another issue. I don’t really understand how can I use a function from another package. If I define the function in the same module (Main in this case), it works; but if import it from another package Metal complains that it is not defined.

@maleadt
Copy link
Member

maleadt commented Jan 2, 2023

I also have another issue. I don’t really understand how can I use a function from another package. If I define the function in the same module (Main in this case), it works; but if import it from another package Metal complains that it is not defined.

That obviously should work fine. The bug tracker is not really suited for such queries though, but you can open a thread on Discourse with some actual code to demonstrate the issue.

@maxwindiff maxwindiff mentioned this issue Mar 20, 2023
4 tasks
@maleadt maleadt added the kernels Things about kernels and how they are compiled. label May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request kernels Things about kernels and how they are compiled.
Projects
None yet
Development

No branches or pull requests

2 participants