@@ -18,3 +18,48 @@ using oneAPI
1818 mask = oneArray (Bool[true , false , true , false , false , true ])
1919 @test Array (data[mask]) == collect (1 : 6 )[findall (Bool[true , false , true , false , false , true ])]
2020end
21+
22+ @testset " CartesianIndices with mapreduce" begin
23+ # Test for bug fix: mapreduce with CartesianIndices and tuple reduction
24+ # Previously failed due to SPIR-V codegen issues with nested insertvalue instructions
25+ # when combining tuples of (bool, CartesianIndex) in reduction operations.
26+ # The fix involved properly handling nested struct insertions in SPIR-V codegen.
27+
28+ # Test that we can zip CartesianIndices with array values in a mapreduce
29+ # This tests the fix for nested tuple operations in SPIR-V codegen
30+
31+ # Simple test: sum of values while tracking indices
32+ x = oneArray (ones (Int, 2 , 2 ))
33+ indices = CartesianIndices ((2 , 2 ))
34+
35+ # Map to tuple of (value, index), then reduce by summing the values
36+ result = mapreduce (tuple, (t1, t2) -> (t1[1 ] + t2[1 ], t1[2 ]), x, indices;
37+ init = (0 , CartesianIndex (0 , 0 )))
38+ @test result[1 ] == 4 # sum of four 1s
39+
40+ # Test with 1D array
41+ y = oneArray (ones (Int, 4 ))
42+ indices_1d = CartesianIndices ((4 ,))
43+ result_1d = mapreduce (tuple, (t1, t2) -> (t1[1 ] + t2[1 ], t1[2 ]), y, indices_1d;
44+ init = (0 , CartesianIndex (0 ,)))
45+ @test result_1d[1 ] == 4
46+
47+ # Test with boolean array and index comparison (closer to original failure case)
48+ # This pattern is similar to what findfirst would use internally
49+ z = oneArray ([false , true , false , true ])
50+ indices_z = CartesianIndices ((4 ,))
51+ result_z = mapreduce (tuple,
52+ (t1, t2) -> begin
53+ (found1, idx1), (found2, idx2) = t1, t2
54+ # Return the first found index (smallest index if both found)
55+ if found1
56+ return (found1, idx1)
57+ else
58+ return (found2, idx2)
59+ end
60+ end ,
61+ z, indices_z;
62+ init = (false , CartesianIndex (0 ,)))
63+ @test result_z[1 ] == true # Found a true value
64+ @test result_z[2 ] == CartesianIndex (2 ,) # First true is at index 2
65+ end
0 commit comments