|
| 1 | +# Troubleshooting |
| 2 | + |
| 3 | +To increase logging verbosity of the CUDA.jl compiler, launch Julia with the `JULIA_DEBUG` |
| 4 | +environment variable set to `CUDA`. |
| 5 | + |
| 6 | + |
| 7 | +## InvalidIRError: compiling ... resulted in invalid LLVM IR |
| 8 | + |
| 9 | +Not all of Julia is supported by CUDA.jl. Several commonly-used features, like strings or |
| 10 | +exceptions, will not compile to GPU code, because of their interactions with the CPU-only |
| 11 | +runtime library. |
| 12 | + |
| 13 | +For example, say we define and try to execute the following kernel: |
| 14 | + |
| 15 | +```julia |
| 16 | +julia> function kernel(a) |
| 17 | + @inbounds a[threadId().x] = 0 |
| 18 | + return |
| 19 | + end |
| 20 | + |
| 21 | +julia> @cuda kernel(CuArray([1])) |
| 22 | +ERROR: InvalidIRError: compiling kernel kernel(CuDeviceArray{Int64,1,1}) resulted in invalid LLVM IR |
| 23 | +Reason: unsupported dynamic function invocation (call to setindex!) |
| 24 | +Stacktrace: |
| 25 | + [1] kernel at REPL[2]:2 |
| 26 | +Reason: unsupported dynamic function invocation (call to getproperty) |
| 27 | +Stacktrace: |
| 28 | + [1] kernel at REPL[2]:2 |
| 29 | +Reason: unsupported use of an undefined name (use of 'threadId') |
| 30 | +Stacktrace: |
| 31 | + [1] kernel at REPL[2]:2 |
| 32 | +``` |
| 33 | + |
| 34 | +CUDA.jl does its best to decode the unsupported IR and figure out where it came from. In |
| 35 | +this case, there's two so-called dynamic invocations, which happen when a function call |
| 36 | +cannot be statically resolved (often because the compiler could not fully infer the call, |
| 37 | +e.g., due to inaccurate or instable type information). These are a red herring, and the real |
| 38 | +cause is listed last: a typo in the use of the `threadIdx` function! If we fix this, the IR |
| 39 | +error disappears and our kernel successfully compiles and executes. |
| 40 | + |
| 41 | + |
| 42 | +## KernelError: kernel returns a value of type `Union{}` |
| 43 | + |
| 44 | +Where the previous section clearly pointed to the source of invalid IR, in other cases your |
| 45 | +function will return an error. This is encoded by the Julia compiler as a return value of |
| 46 | +type `Union{}`: |
| 47 | + |
| 48 | +```julia |
| 49 | +julia> function kernel(a) |
| 50 | + @inbounds a[threadId().x] = CUDA.sin(a[threadIdx().x]) |
| 51 | + return |
| 52 | + end |
| 53 | + |
| 54 | +julia> @cuda kernel(CuArray([1])) |
| 55 | +ERROR: GPU compilation of kernel kernel(CuDeviceArray{Int64,1,1}) failed |
| 56 | +KernelError: kernel returns a value of type `Union{}` |
| 57 | +``` |
| 58 | + |
| 59 | +Now we don't know where this error came from, and we will have to take a look ourselves at |
| 60 | +the generated code. This is easily done using the `@device_code` introspection macros, which |
| 61 | +mimic their Base counterparts (e.g. `@device_code_llvm` instead of `@code_llvm`, etc). |
| 62 | + |
| 63 | +To debug an error returned by a kernel, we should use `@device_code_warntype` to inspect the |
| 64 | +Julia IR. Furthermore, this macro has an `interactive` mode, which further facilitates |
| 65 | +inspecting this IR using Cthulhu.jl. First, install and import this package, and then try to |
| 66 | +execute the kernel again prefixed by `@device_code_warntype interactive=true`: |
| 67 | + |
| 68 | +```julia |
| 69 | +julia> using Cthulhu |
| 70 | + |
| 71 | +julia> @device_code_warntype interactive=true @cuda kernel(CuArray([1])) |
| 72 | +Variables |
| 73 | + #self#::Core.Compiler.Const(kernel, false) |
| 74 | + a::CuDeviceArray{Int64,1,1} |
| 75 | + val::Union{} |
| 76 | + |
| 77 | +Body::Union{} |
| 78 | +1 ─ %1 = CUDA.sin::Core.Compiler.Const(CUDA.sin, false) |
| 79 | +│ ... |
| 80 | +│ %14 = (...)::Int64 |
| 81 | +└── goto #2 |
| 82 | +2 ─ (%1)(%14) |
| 83 | +└── $(Expr(:unreachable)) |
| 84 | + |
| 85 | +Select a call to descend into or ↩ to ascend. |
| 86 | + • %17 = call CUDA.sin(::Int64)::Union{} |
| 87 | +``` |
| 88 | + |
| 89 | +Both from the IR and the list of calls Cthulhu offers to inspect further, we can see that |
| 90 | +the call to `CUDA.sin(::Int64)` results in an error: in the IR it is immediately followed by |
| 91 | +an `unreachable`, while in the list of calls it is inferred to return `Union{}`. Now we know |
| 92 | +where to look, it's easy to figure out what's wrong: |
| 93 | + |
| 94 | +```julia |
| 95 | +help?> CUDA.sin |
| 96 | + # 2 methods for generic function "sin": |
| 97 | + [1] sin(x::Float32) in CUDA at /home/tim/Julia/pkg/CUDA/src/device/intrinsics/math.jl:13 |
| 98 | + [2] sin(x::Float64) in CUDA at /home/tim/Julia/pkg/CUDA/src/device/intrinsics/math.jl:12 |
| 99 | +``` |
| 100 | + |
| 101 | +There's no method of `CUDA.sin` that accepts an Int64, and thus the function was determined |
| 102 | +to unconditionally throw a method error. For now, we disallow these situations and refuse to |
| 103 | +compile, but in the spirit of dynamic languages we might change this behavior to just throw |
| 104 | +an error at run time. |
| 105 | + |
| 106 | + |
| 107 | +## Debug info and line-number information |
| 108 | + |
| 109 | +On Julia debug level 1, which is the default setting if unspecified, CUDA.jl emits line |
| 110 | +number information corresponding to `nvcc -lineinfo`. This information does not hurt |
| 111 | +performance, and is used by a variety of tools to improve the debugging experience. |
| 112 | + |
| 113 | +To emit actual debug info as `nvcc -G` does, you need to start Julia on debug level 2 by |
| 114 | +passing the flag `-g2`. Support for emitting PTX-compatible debug info is a recent addition |
| 115 | +to the NVPTX LLVM back-end, so it's possible this information is incorrect or otherwise |
| 116 | +affects compilation. |
| 117 | + |
| 118 | + !!! warning |
| 119 | + |
| 120 | + Due to bugs in LLVM and/or CUDA, the debug info as emitted by LLVM 8.0 or higher |
| 121 | + results in crashed when loading the compiled code. As a result, all types of debug info |
| 122 | + are disabled by CUDA.jl on Julia 1.4 or above. If you need line number information, you |
| 123 | + need to revert to using Julia 1.3 which uses LLVM 6.0 (note that actual debug info is |
| 124 | + not supported by LLVM 6.0). |
| 125 | + |
| 126 | +To disable all debug info emission, start Julia with the flag `-g0`. |
| 127 | + |
| 128 | + |
| 129 | +## Stack trace information |
| 130 | + |
| 131 | +The Julia debug level is also used to emit determine how much backtrace information to embed |
| 132 | +in the module. This information is used when displaying exceptions on the device, e.g., when |
| 133 | +going out of bounds: |
| 134 | + |
| 135 | +```julia |
| 136 | +julia> function kernel(a) |
| 137 | + a[threadIdx().x] = 0 |
| 138 | + return |
| 139 | + end |
| 140 | +kernel (generic function with 1 method) |
| 141 | + |
| 142 | +julia> @cuda threads=2 kernel(CuArray([1])) |
| 143 | +``` |
| 144 | + |
| 145 | +On the default debug level of 1, an simple error message will be displayed: |
| 146 | + |
| 147 | +``` |
| 148 | +ERROR: a exception was thrown during kernel execution. |
| 149 | +Run Julia on debug level 2 for device stack traces. |
| 150 | +``` |
| 151 | + |
| 152 | +If we set the debug level to 2, by passing `-g2` to `julia`, we see: |
| 153 | + |
| 154 | +``` |
| 155 | +ERROR: a exception was thrown during kernel execution. |
| 156 | +Stacktrace: |
| 157 | + [1] throw_boundserror at abstractarray.jl:541 |
| 158 | + [2] checkbounds at abstractarray.jl:506 |
| 159 | + [3] arrayset at /home/tim/Julia/pkg/CUDA/src/device/array.jl:84 |
| 160 | + [4] setindex! at /home/tim/Julia/pkg/CUDA/src/device/array.jl:101 |
| 161 | + [5] kernel at REPL[4]:2 |
| 162 | +``` |
| 163 | + |
| 164 | +Note that these messages are embedded in the module (CUDA does not support stack unwinding), |
| 165 | +and thus bloat its size. To avoid any overhead, you can disable these messages by setting |
| 166 | +the debug level to 0 (passing `-g0` to `julia`). This disabled any device-side message, but |
| 167 | +retains the host-side detection: |
| 168 | + |
| 169 | +``` |
| 170 | +julia> @cuda threads=2 kernel(CuArray([1])) |
| 171 | +# no device-side error message! |
| 172 | +
|
| 173 | +julia> synchronize() |
| 174 | +ERROR: KernelException: exception thrown during kernel execution |
| 175 | +``` |
0 commit comments