From a293bab4f7accddf9cd7e62b11ac8b5cf575b445 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Wed, 12 Aug 2026 18:37:19 -0400 Subject: [PATCH 1/4] Fix LinearScan Windows unwind crash in Dual ODE RHS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ±1 walk used `@inbounds if` around `cond && return` / `cond && continue`. That control-flow shape produced invalid Windows unwind information (ntdll!RtlVirtualUnwind2 ACCESS_VIOLATION) when DataInterpolations evaluated KIND_LINEAR_SCAN from an OrdinaryDiffEq Dual Jacobian. Rewrite the walk as a straight if/return loop, @inbounds only the loads, and mark the kernels @noinline so they are not inlined into the Dual RHS. Search results are unchanged. Co-Authored-By: Chris Rackauckas --- NEWS.md | 9 ++++++ Project.toml | 2 +- src/kernels.jl | 76 +++++++++++++++++++++++++++------------------- test/core_tests.jl | 28 +++++++++++++++++ 4 files changed, 83 insertions(+), 32 deletions(-) diff --git a/NEWS.md b/NEWS.md index 91f3cb7..2f388c9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,14 @@ # FindFirstFunctions.jl NEWS +## 3.2.2 + +`LinearScan` (`KIND_LINEAR_SCAN`) no longer wraps its ±1 walk in +`@inbounds if` / `cond && return`. That control-flow shape produced invalid +Windows unwind information (`ntdll!RtlVirtualUnwind2` ACCESS_VIOLATION) +when the walk was reached from an OrdinaryDiffEq Dual right-hand side +(DataInterpolations.jl#580). The kernels are also `@noinline` so the walk +is not inlined into a Dual Jacobian. Search results are unchanged. + ## 3.0.0 The 2.x sorted-search API was a single generic diff --git a/Project.toml b/Project.toml index d3acbae..e368656 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "FindFirstFunctions" uuid = "64ca27bc-2ba2-4a57-88aa-44e436879224" -version = "3.2.1" +version = "3.2.2" authors = ["Chris Elrod and contributors"] [deps] diff --git a/src/kernels.jl b/src/kernels.jl index 8951beb..261f45f 100644 --- a/src/kernels.jl +++ b/src/kernels.jl @@ -142,54 +142,62 @@ end # Kernel: LinearScan — walk ±1 from the hint. # =========================================================================== -function _kernel_last_linear_scan( +# `@noinline` plus a straight `if`/`return` body: `@inbounds if` wrapping +# `cond && return` inside the walk produced invalid Windows unwind info. +@noinline function _kernel_last_linear_scan( v::AbstractVector, x, hint::Integer, order::Base.Order.Ordering, ) - lo, hi = firstindex(v), lastindex(v) - if hi < lo - return lo - 1 # empty vector - end + lo = firstindex(v) + hi = lastindex(v) + hi < lo && return lo - 1 i = clamp(hint, lo, hi) - @inbounds if Base.Order.lt(order, x, v[i]) - # v[i] > x → retreat + @inbounds vi = v[i] + if Base.Order.lt(order, x, vi) while i > lo i -= 1 - !Base.Order.lt(order, x, v[i]) && return i + @inbounds vi = v[i] + if !Base.Order.lt(order, x, vi) + return i + end end - return lo - 1 # x precedes all of v - else - # v[i] ≤ x → try to advance - while i < hi - Base.Order.lt(order, x, v[i + 1]) && return i - i += 1 + return lo - 1 + end + while i < hi + @inbounds vip1 = v[i + 1] + if Base.Order.lt(order, x, vip1) + return i end - return hi + i += 1 end + return hi end -function _kernel_first_linear_scan( +@noinline function _kernel_first_linear_scan( v::AbstractVector, x, hint::Integer, order::Base.Order.Ordering, ) - lo, hi = firstindex(v), lastindex(v) - if hi < lo - return lo - end + lo = firstindex(v) + hi = lastindex(v) + hi < lo && return lo i = clamp(hint, lo, hi) - @inbounds if Base.Order.lt(order, v[i], x) - # v[i] < x → advance + @inbounds vi = v[i] + if Base.Order.lt(order, vi, x) while i < hi i += 1 - !Base.Order.lt(order, v[i], x) && return i + @inbounds vi = v[i] + if !Base.Order.lt(order, vi, x) + return i + end end - return hi + 1 # x exceeds all of v - else - # v[i] ≥ x → try to retreat - while i > lo - !Base.Order.lt(order, v[i - 1], x) && (i -= 1; continue) + return hi + 1 + end + while i > lo + @inbounds vim1 = v[i - 1] + if Base.Order.lt(order, vim1, x) return i end - return lo + i -= 1 end + return lo end # =========================================================================== @@ -211,7 +219,10 @@ end # `v[i]` is past the answer in this ordering — backward walk (scalar). while i > lo i -= 1 - @inbounds !Base.Order.lt(order, x, v[i]) && return i + @inbounds vi = v[i] + if !Base.Order.lt(order, x, vi) + return i + end end return lo - 1 end @@ -248,7 +259,10 @@ end return offset < 0 ? hi + 1 : start + offset end while i > lo - @inbounds Base.Order.lt(order, v[i - 1], x) && return i + @inbounds vim1 = v[i - 1] + if Base.Order.lt(order, vim1, x) + return i + end i -= 1 end return lo diff --git a/test/core_tests.jl b/test/core_tests.jl index cfc8086..a353533 100644 --- a/test/core_tests.jl +++ b/test/core_tests.jl @@ -692,6 +692,34 @@ end end end +@safetestset "LinearScan with Dual queries on Float64 knots" begin + using FindFirstFunctions + using FindFirstFunctions: KIND_LINEAR_SCAN, searchsorted_last, searchsorted_first + using ForwardDiff + using Test + + # ODE-Jacobian shape: Float64 knots, Dual query, Int hint (Guesser idx_prev). + t = [0.0, 0.3, 1.1, 2.0, 3.5, 5.0, 6.2, 8.0] + for (q, hint) in ( + (1.1, 1), (3.5, 4), (8.0, 8), (0.0, 1), (-1.0, 1), (9.0, 8), (4.2, 3), + ) + x = ForwardDiff.Dual(q, 1.0) + @test searchsorted_last(KIND_LINEAR_SCAN, t, x, hint) == searchsortedlast(t, x) + @test searchsorted_first(KIND_LINEAR_SCAN, t, x, hint) == searchsortedfirst(t, x) + @test searchsorted_last(KIND_LINEAR_SCAN, t, x, hint) isa Int + end + + d = ForwardDiff.derivative(1.0) do p + acc = 0 + for k in 1:64 + q = 0.1 * k * p + acc += searchsorted_last(KIND_LINEAR_SCAN, t, q, 1) + end + Float64(acc) + end + @test isfinite(d) +end + @safetestset "Auto{T} parametric + props-aware UniformStep kernel" begin using FindFirstFunctions using FindFirstFunctions: From 1ead50779e4aa1a482c91914dd5bb30dc9077398 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Wed, 12 Aug 2026 19:16:38 -0400 Subject: [PATCH 2/4] Keep LinearScan @inline; @noinline was 2x on fused lerp A search-only microbench hid the call. On get_idx + slope (the DataInterpolations path) @noinline is 7.62 vs 3.57 ns Float64 lerp and blocks Dual fusion. The CFG rewrite is the unwind fix; the kernels stay @inline like the other strategy kernels. Co-Authored-By: Chris Rackauckas --- NEWS.md | 4 ++-- src/kernels.jl | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/NEWS.md b/NEWS.md index 2f388c9..1402ed3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,8 +6,8 @@ `@inbounds if` / `cond && return`. That control-flow shape produced invalid Windows unwind information (`ntdll!RtlVirtualUnwind2` ACCESS_VIOLATION) when the walk was reached from an OrdinaryDiffEq Dual right-hand side -(DataInterpolations.jl#580). The kernels are also `@noinline` so the walk -is not inlined into a Dual Jacobian. Search results are unchanged. +(DataInterpolations.jl#580). The kernels stay `@inline` so they still fuse +into a lerp / Dual Jacobian. Search results are unchanged. ## 3.0.0 diff --git a/src/kernels.jl b/src/kernels.jl index 261f45f..47848b3 100644 --- a/src/kernels.jl +++ b/src/kernels.jl @@ -142,9 +142,10 @@ end # Kernel: LinearScan — walk ±1 from the hint. # =========================================================================== -# `@noinline` plus a straight `if`/`return` body: `@inbounds if` wrapping -# `cond && return` inside the walk produced invalid Windows unwind info. -@noinline function _kernel_last_linear_scan( +# Straight `if`/`return` body: `@inbounds if` wrapping `cond && return` +# inside the walk produced invalid Windows unwind info. Keep `@inline` +# so the walk fuses into DataInterpolations lerp / Dual Jacobian. +@inline function _kernel_last_linear_scan( v::AbstractVector, x, hint::Integer, order::Base.Order.Ordering, ) lo = firstindex(v) @@ -172,7 +173,7 @@ end return hi end -@noinline function _kernel_first_linear_scan( +@inline function _kernel_first_linear_scan( v::AbstractVector, x, hint::Integer, order::Base.Order.Ordering, ) lo = firstindex(v) From 8515ad9a095fda1f938d8a80a7ef6b48c336d4cf Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Wed, 12 Aug 2026 19:25:07 -0400 Subject: [PATCH 3/4] Expand LinearScan short-circuit returns to if/return Co-Authored-By: Chris Rackauckas --- NEWS.md | 9 ------ Project.toml | 2 +- src/kernels.jl | 79 +++++++++++++++++++++------------------------- test/core_tests.jl | 28 ---------------- 4 files changed, 37 insertions(+), 81 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1402ed3..91f3cb7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,14 +1,5 @@ # FindFirstFunctions.jl NEWS -## 3.2.2 - -`LinearScan` (`KIND_LINEAR_SCAN`) no longer wraps its ±1 walk in -`@inbounds if` / `cond && return`. That control-flow shape produced invalid -Windows unwind information (`ntdll!RtlVirtualUnwind2` ACCESS_VIOLATION) -when the walk was reached from an OrdinaryDiffEq Dual right-hand side -(DataInterpolations.jl#580). The kernels stay `@inline` so they still fuse -into a lerp / Dual Jacobian. Search results are unchanged. - ## 3.0.0 The 2.x sorted-search API was a single generic diff --git a/Project.toml b/Project.toml index e368656..d3acbae 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "FindFirstFunctions" uuid = "64ca27bc-2ba2-4a57-88aa-44e436879224" -version = "3.2.2" +version = "3.2.1" authors = ["Chris Elrod and contributors"] [deps] diff --git a/src/kernels.jl b/src/kernels.jl index 47848b3..196e40a 100644 --- a/src/kernels.jl +++ b/src/kernels.jl @@ -142,63 +142,62 @@ end # Kernel: LinearScan — walk ±1 from the hint. # =========================================================================== -# Straight `if`/`return` body: `@inbounds if` wrapping `cond && return` -# inside the walk produced invalid Windows unwind info. Keep `@inline` -# so the walk fuses into DataInterpolations lerp / Dual Jacobian. -@inline function _kernel_last_linear_scan( +function _kernel_last_linear_scan( v::AbstractVector, x, hint::Integer, order::Base.Order.Ordering, ) - lo = firstindex(v) - hi = lastindex(v) - hi < lo && return lo - 1 + lo, hi = firstindex(v), lastindex(v) + if hi < lo + return lo - 1 # empty vector + end i = clamp(hint, lo, hi) - @inbounds vi = v[i] - if Base.Order.lt(order, x, vi) + @inbounds if Base.Order.lt(order, x, v[i]) + # v[i] > x → retreat while i > lo i -= 1 - @inbounds vi = v[i] - if !Base.Order.lt(order, x, vi) + if !Base.Order.lt(order, x, v[i]) return i end end - return lo - 1 - end - while i < hi - @inbounds vip1 = v[i + 1] - if Base.Order.lt(order, x, vip1) - return i + return lo - 1 # x precedes all of v + else + # v[i] ≤ x → try to advance + while i < hi + if Base.Order.lt(order, x, v[i + 1]) + return i + end + i += 1 end - i += 1 + return hi end - return hi end -@inline function _kernel_first_linear_scan( +function _kernel_first_linear_scan( v::AbstractVector, x, hint::Integer, order::Base.Order.Ordering, ) - lo = firstindex(v) - hi = lastindex(v) - hi < lo && return lo + lo, hi = firstindex(v), lastindex(v) + if hi < lo + return lo + end i = clamp(hint, lo, hi) - @inbounds vi = v[i] - if Base.Order.lt(order, vi, x) + @inbounds if Base.Order.lt(order, v[i], x) + # v[i] < x → advance while i < hi i += 1 - @inbounds vi = v[i] - if !Base.Order.lt(order, vi, x) + if !Base.Order.lt(order, v[i], x) return i end end - return hi + 1 - end - while i > lo - @inbounds vim1 = v[i - 1] - if Base.Order.lt(order, vim1, x) - return i + return hi + 1 # x exceeds all of v + else + # v[i] ≥ x → try to retreat + while i > lo + if Base.Order.lt(order, v[i - 1], x) + return i + end + i -= 1 end - i -= 1 + return lo end - return lo end # =========================================================================== @@ -220,10 +219,7 @@ end # `v[i]` is past the answer in this ordering — backward walk (scalar). while i > lo i -= 1 - @inbounds vi = v[i] - if !Base.Order.lt(order, x, vi) - return i - end + @inbounds !Base.Order.lt(order, x, v[i]) && return i end return lo - 1 end @@ -260,10 +256,7 @@ end return offset < 0 ? hi + 1 : start + offset end while i > lo - @inbounds vim1 = v[i - 1] - if Base.Order.lt(order, vim1, x) - return i - end + @inbounds Base.Order.lt(order, v[i - 1], x) && return i i -= 1 end return lo diff --git a/test/core_tests.jl b/test/core_tests.jl index a353533..cfc8086 100644 --- a/test/core_tests.jl +++ b/test/core_tests.jl @@ -692,34 +692,6 @@ end end end -@safetestset "LinearScan with Dual queries on Float64 knots" begin - using FindFirstFunctions - using FindFirstFunctions: KIND_LINEAR_SCAN, searchsorted_last, searchsorted_first - using ForwardDiff - using Test - - # ODE-Jacobian shape: Float64 knots, Dual query, Int hint (Guesser idx_prev). - t = [0.0, 0.3, 1.1, 2.0, 3.5, 5.0, 6.2, 8.0] - for (q, hint) in ( - (1.1, 1), (3.5, 4), (8.0, 8), (0.0, 1), (-1.0, 1), (9.0, 8), (4.2, 3), - ) - x = ForwardDiff.Dual(q, 1.0) - @test searchsorted_last(KIND_LINEAR_SCAN, t, x, hint) == searchsortedlast(t, x) - @test searchsorted_first(KIND_LINEAR_SCAN, t, x, hint) == searchsortedfirst(t, x) - @test searchsorted_last(KIND_LINEAR_SCAN, t, x, hint) isa Int - end - - d = ForwardDiff.derivative(1.0) do p - acc = 0 - for k in 1:64 - q = 0.1 * k * p - acc += searchsorted_last(KIND_LINEAR_SCAN, t, q, 1) - end - Float64(acc) - end - @test isfinite(d) -end - @safetestset "Auto{T} parametric + props-aware UniformStep kernel" begin using FindFirstFunctions using FindFirstFunctions: From ff2d2abd14a968bcad84855cbc6a4195470ff3b1 Mon Sep 17 00:00:00 2001 From: ChrisRackauckas-Claude Date: Wed, 12 Aug 2026 19:34:06 -0400 Subject: [PATCH 4/4] Stop wrapping LinearScan early returns in @inbounds if Co-Authored-By: Chris Rackauckas --- src/kernels.jl | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/kernels.jl b/src/kernels.jl index 196e40a..3db8163 100644 --- a/src/kernels.jl +++ b/src/kernels.jl @@ -150,21 +150,17 @@ function _kernel_last_linear_scan( return lo - 1 # empty vector end i = clamp(hint, lo, hi) - @inbounds if Base.Order.lt(order, x, v[i]) + if @inbounds Base.Order.lt(order, x, v[i]) # v[i] > x → retreat while i > lo i -= 1 - if !Base.Order.lt(order, x, v[i]) - return i - end + @inbounds !Base.Order.lt(order, x, v[i]) && return i end return lo - 1 # x precedes all of v else # v[i] ≤ x → try to advance while i < hi - if Base.Order.lt(order, x, v[i + 1]) - return i - end + @inbounds Base.Order.lt(order, x, v[i + 1]) && return i i += 1 end return hi @@ -179,22 +175,18 @@ function _kernel_first_linear_scan( return lo end i = clamp(hint, lo, hi) - @inbounds if Base.Order.lt(order, v[i], x) + if @inbounds Base.Order.lt(order, v[i], x) # v[i] < x → advance while i < hi i += 1 - if !Base.Order.lt(order, v[i], x) - return i - end + @inbounds !Base.Order.lt(order, v[i], x) && return i end return hi + 1 # x exceeds all of v else # v[i] ≥ x → try to retreat while i > lo - if Base.Order.lt(order, v[i - 1], x) - return i - end - i -= 1 + @inbounds !Base.Order.lt(order, v[i - 1], x) && (i -= 1; continue) + return i end return lo end