-
Notifications
You must be signed in to change notification settings - Fork 67
Replace ExceptionInfo with lightweight packed UInt64 atomic #894
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
Merged
+133
−124
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eaa826b
Replace heavyweight ExceptionInfo with single atomic UInt64
gbaraldi fafcac8
Add codegen test for lightweight exception handling
gbaraldi 51358c5
Add bounds check codegen test for lightweight exceptions
gbaraldi 74b56a4
Use kernel=true in codegen tests to test actual kernel assembly
gbaraldi 635d884
Update src/exception_handler.jl
gbaraldi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,114 +1,97 @@ | ||
| """ | ||
| - `status::Int32`: whether exception has been thrown (0 - no, 1 - yes). | ||
| """ | ||
| struct ExceptionInfo | ||
| status::Int32 | ||
| output_lock::Int32 | ||
| # Exception reason codes — encoded in bits [7:0] of the packed exception UInt64 | ||
| # 0 means no exception | ||
| module ExceptionCode | ||
| const NONE = UInt8(0) | ||
| const UNKNOWN = UInt8(1) | ||
| const BOUNDS_ERROR = UInt8(2) | ||
| const DOMAIN_ERROR = UInt8(3) | ||
| const OVERFLOW_ERROR = UInt8(4) | ||
| const INEXACT_ERROR = UInt8(5) | ||
| const ARGUMENT_ERROR = UInt8(6) | ||
| const DIVIDE_ERROR = UInt8(7) | ||
| const DIM_MISMATCH = UInt8(8) | ||
| end | ||
|
|
||
| thread::@NamedTuple{x::UInt32, y::UInt32, z::UInt32} | ||
| block::@NamedTuple{x::UInt32, y::UInt32, z::UInt32} | ||
| const EXCEPTION_REASON_STRINGS = Dict{UInt8, String}( | ||
| ExceptionCode.NONE => "No exception", | ||
| ExceptionCode.UNKNOWN => "Unknown exception", | ||
| ExceptionCode.BOUNDS_ERROR => "BoundsError: Out-of-bounds array access", | ||
| ExceptionCode.DOMAIN_ERROR => "DomainError", | ||
| ExceptionCode.OVERFLOW_ERROR => "OverflowError", | ||
| ExceptionCode.INEXACT_ERROR => "InexactError: Inexact conversion", | ||
| ExceptionCode.ARGUMENT_ERROR => "ArgumentError", | ||
| ExceptionCode.DIVIDE_ERROR => "DivideError: Integer division error", | ||
| ExceptionCode.DIM_MISMATCH => "DimensionMismatch", | ||
| ) | ||
|
|
||
| reason::LLVMPtr{UInt8, AS.Global} | ||
| reason_length::Int64 | ||
| # Packed exception format (UInt64): | ||
| # [63:48] workgroup_x (16 bits) | ||
| # [47:32] workgroup_y (16 bits) | ||
| # [31:16] workgroup_z (16 bits) | ||
| # [15:8] reserved | ||
| # [7:0] error code (non-zero = exception occurred) | ||
|
|
||
| ExceptionInfo() = new( | ||
| Int32(0), Int32(0), | ||
| (; x=UInt32(0), y=UInt32(0), z=UInt32(0)), | ||
| (; x=UInt32(0), y=UInt32(0), z=UInt32(0)), | ||
| LLVMPtr{UInt8, AS.Global}(), 0) | ||
| @inline function pack_exception(code::UInt8) | ||
| wg = workgroupIdx() | ||
| wg_x = UInt64(wg.x % UInt16) << 48 | ||
| wg_y = UInt64(wg.y % UInt16) << 32 | ||
| wg_z = UInt64(wg.z % UInt16) << 16 | ||
| return wg_x | wg_y | wg_z | UInt64(code) | ||
| end | ||
|
|
||
| @inline function Base.getproperty(ei::Ptr{ExceptionInfo}, field::Symbol) | ||
| if field == :status | ||
| unsafe_load(convert(Ptr{Int32}, ei)) | ||
| elseif field == :output_lock | ||
| unsafe_load(convert(Ptr{Int32}, ei + sizeof(Int32))) | ||
| elseif field == :output_lock_ptr | ||
| reinterpret(LLVMPtr{Int32, AS.Generic}, ei + sizeof(Int32)) | ||
| elseif field == :thread | ||
| offset = 2 * sizeof(Int32) | ||
| unsafe_load(convert(Ptr{@NamedTuple{x::UInt32, y::UInt32, z::UInt32}}, ei + offset)) | ||
| elseif field == :block | ||
| offset = 2 * sizeof(Int32) + sizeof(@NamedTuple{x::UInt32, y::UInt32, z::UInt32}) | ||
| unsafe_load(convert(Ptr{@NamedTuple{x::UInt32, y::UInt32, z::UInt32}}, ei + offset)) | ||
| elseif field == :reason | ||
| offset = | ||
| 2 * sizeof(Int32) + | ||
| 2 * sizeof(@NamedTuple{x::UInt32, y::UInt32, z::UInt32}) | ||
| unsafe_load(convert(Ptr{LLVMPtr{UInt8, AS.Global}}, ei + offset)) | ||
| elseif field == :reason_length | ||
| offset = | ||
| 2 * sizeof(Int32) + | ||
| 2 * sizeof(@NamedTuple{x::UInt32, y::UInt32, z::UInt32}) + | ||
| sizeof(LLVMPtr{UInt8, AS.Global}) | ||
| unsafe_load(convert(Ptr{Int64}, ei + offset)) | ||
| else | ||
| getfield(ei, field) | ||
| end | ||
| @inline function unpack_exception(packed::UInt64) | ||
| wg_x = UInt16((packed >> 48) & 0xFFFF) | ||
| wg_y = UInt16((packed >> 32) & 0xFFFF) | ||
| wg_z = UInt16((packed >> 16) & 0xFFFF) | ||
| code = UInt8(packed & 0xFF) | ||
| return (; wg_x, wg_y, wg_z, code) | ||
| end | ||
|
|
||
| @inline function Base.setproperty!(ei::Ptr{ExceptionInfo}, field::Symbol, value) | ||
| if field == :status | ||
| unsafe_store!(convert(Ptr{Int32}, ei), value) | ||
| elseif field == :output_lock | ||
| unsafe_store!(convert(Ptr{Int32}, ei + sizeof(Int32)), value) | ||
| elseif field == :thread | ||
| offset = 2 * sizeof(Int32) | ||
| unsafe_store!(convert(Ptr{@NamedTuple{x::UInt32, y::UInt32, z::UInt32}}, ei + offset), value) | ||
| elseif field == :block | ||
| offset = 2 * sizeof(Int32) + sizeof(@NamedTuple{x::UInt32, y::UInt32, z::UInt32}) | ||
| unsafe_store!(convert(Ptr{@NamedTuple{x::UInt32, y::UInt32, z::UInt32}}, ei + offset), value) | ||
| elseif field == :reason | ||
| offset = | ||
| 2 * sizeof(Int32) + | ||
| 2 * sizeof(@NamedTuple{x::UInt32, y::UInt32, z::UInt32}) | ||
| unsafe_store!(convert(Ptr{LLVMPtr{UInt8, AS.Global}}, ei + offset), value) | ||
| elseif field == :reason_length | ||
| offset = | ||
| 2 * sizeof(Int32) + | ||
| 2 * sizeof(@NamedTuple{x::UInt32, y::UInt32, z::UInt32}) + | ||
| sizeof(LLVMPtr{UInt8, AS.Global}) | ||
| unsafe_store!(convert(Ptr{Int64}, ei + offset), value) | ||
| else | ||
| setfield!(ei, field, value) | ||
| end | ||
| end | ||
| # Legacy compat — ExceptionInfo is now just a UInt64 | ||
| const ExceptionInfo = UInt64 | ||
|
|
||
| function alloc_exception_info() | ||
| ei_ptr = Mem.HostBuffer(sizeof(ExceptionInfo), HIP.hipHostAllocDefault) | ||
| unsafe_store!(convert(Ptr{ExceptionInfo}, ei_ptr), ExceptionInfo()) | ||
| ei_ptr = Mem.HostBuffer(sizeof(UInt64), HIP.hipHostAllocDefault) | ||
| unsafe_store!(convert(Ptr{UInt64}, ei_ptr), UInt64(0)) | ||
| return ei_ptr | ||
| end | ||
|
|
||
| @inline function lock_output!(ei::Ptr{ExceptionInfo}) | ||
| # if llvm_atomic_cas(ei.output_lock_ptr, zero(Int32), one(Int32)) == zero(Int32) | ||
| if llvm_atomic_cas(ei.output_lock_ptr, Int32(0x0), Int32(0x1)) == Int32(0x0) | ||
| # Take the lock & write thread info. | ||
| ei.thread = workitemIdx() | ||
| ei.block = workgroupIdx() | ||
| sync_workgroup() | ||
| return true | ||
| elseif ( | ||
| ei.output_lock == Int32(0x1) && | ||
| ei.thread == workitemIdx() && | ||
| ei.block == workgroupIdx() | ||
| ) | ||
| # Thread already has the lock. | ||
| return true | ||
| else | ||
| # Other thread has the lock. | ||
| return false | ||
| end | ||
| @inline function signal_exception!(ei::Ptr{UInt64}, code::UInt8) | ||
| packed = pack_exception(code) | ||
| # First writer wins via atomic CAS; losers are no-ops. | ||
| ei_llvm = reinterpret(LLVMPtr{UInt64, AS.Generic}, ei) | ||
| llvm_atomic_cas(ei_llvm, UInt64(0), packed) | ||
| endpgm() | ||
| return | ||
| end | ||
|
|
||
| macro gpu_throw(reason) | ||
| code = _reason_to_code(reason) | ||
| quote | ||
| ei = kernel_state().exception_info | ||
| if lock_output!(ei) | ||
| reason_ptr, reason_length = @strptr $reason | ||
| ei.reason = reason_ptr | ||
| ei.reason_length = reason_length | ||
| end | ||
| throw(nothing) | ||
| signal_exception!(ei, $code) | ||
| throw(nothing) # unreachable, but keeps Julia's type system happy | ||
| end | ||
| end | ||
|
|
||
| # Map reason strings to error codes at macro expansion time | ||
| function _reason_to_code(reason::String) | ||
| if startswith(reason, "BoundsError") | ||
| ExceptionCode.BOUNDS_ERROR | ||
| elseif startswith(reason, "DomainError") | ||
| ExceptionCode.DOMAIN_ERROR | ||
| elseif startswith(reason, "OverflowError") | ||
| ExceptionCode.OVERFLOW_ERROR | ||
| elseif startswith(reason, "InexactError") | ||
| ExceptionCode.INEXACT_ERROR | ||
| elseif startswith(reason, "ArgumentError") | ||
| ExceptionCode.ARGUMENT_ERROR | ||
| elseif startswith(reason, "DivideError") | ||
| ExceptionCode.DIVIDE_ERROR | ||
| elseif startswith(reason, "DimensionMismatch") | ||
| ExceptionCode.DIM_MISMATCH | ||
| else | ||
| ExceptionCode.UNKNOWN | ||
| end | ||
| end | ||
| _reason_to_code(reason) = ExceptionCode.UNKNOWN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should start using FileCheck.jl for these.