EnzymeAD / Enzyme.jl

Julia bindings for the Enzyme automatic differentiator
https://enzyme.mit.edu
MIT License
428 stars 59 forks source link

enumerate not working in 0.12 #1405

Closed just-walk closed 2 months ago

just-walk commented 3 months ago

The latest version of Enzyme can't handle the enumerate function. I tried testing the following function with enumerate and with a simple range 1:n_points. In 0.11, both work, but in 0.12, I get the error below for a loop over a range output from enumerate.

INPUT:

function summat(p::Real)
    n = 2
    A = Matrix{typeof(p)}(undef, n, n)
    n_points = n^2
    interval = 8 * π
    z_range = range(
        start = -interval / 2, stop = interval / 2, length = n_points)

    for (i, z) in enumerate(z_range)
        A[i] = i
    end
    return sum(A)
end

@show autodiff(ReverseWithPrimal, summat, Active, Active(3.0))

OUTPUT:

  Enzyme compilation failed due to illegal type analysis.
  Current scope: 
  ; Function Attrs: mustprogress willreturn
  define internal fastcc void @preprocess_julia_splitprec_2895([2 x double]* noalias nocapture nofree noundef nonnull writeonly sret([2 x double]) align 8 dereferenceable(16) "enzyme_type"="{[-1]:Pointer, [-1,-1]:Float@double}" %0, i128 signext "enzyme_inactive" "enzyme_type"="{[-1]:Integer}" "enzymejl_parmtype"="128537484931168" "enzymejl_parmtype_ref"="0" %1) unnamed_addr #36 !dbg !2527 {
  top:
    %2 = call {}*** @julia.get_pgcstack() #37
    %ptls_field29 = getelementptr inbounds {}**, {}*** %2, i64 2
    %3 = bitcast {}*** %ptls_field29 to i64***
    %ptls_load3031 = load i64**, i64*** %3, align 8, !tbaa !27
    %4 = getelementptr inbounds i64*, i64** %ptls_load3031, i64 2
    %safepoint = load i64*, i64** %4, align 8, !tbaa !31
    fence syncscope("singlethread") seq_cst
    call void @julia.safepoint(i64* %safepoint) #37, !dbg !2528
    fence syncscope("singlethread") seq_cst
    %5 = ashr i128 %1, 127, !dbg !2529
    %6 = trunc i128 %5 to i64, !dbg !2533
    %7 = shl nsw i64 %6, 63, !dbg !2534
    %8 = add i128 %5, %1, !dbg !2536
    %9 = xor i128 %8, %5, !dbg !2536
    %10 = icmp ugt i128 %9, 20282409603651670423947251286015, !dbg !2540
    br i1 %10, label %L29, label %L12, !dbg !2541

  L12:                                              ; preds = %top
    %11 = trunc i128 %9 to i64, !dbg !2542
    %12 = and i64 %11, 4503599627370495, !dbg !2544
    %13 = or i64 %12, 4841369599423283200, !dbg !2545
    %bitcast_coercion = bitcast i64 %13 to double, !dbg !2547
    %14 = fadd double %bitcast_coercion, 0xC330000000000000, !dbg !2548
    %15 = lshr i128 %9, 52, !dbg !2549
    %16 = trunc i128 %15 to i64, !dbg !2552
    %17 = or i64 %16, 5075556780046548992, !dbg !2553
    %bitcast_coercion2 = bitcast i64 %17 to double, !dbg !2555
    %18 = fadd double %bitcast_coercion2, 0xC670000000000000, !dbg !2556
    %19 = fadd double %14, %18, !dbg !2557
    br label %L54, !dbg !2559

  L29:                                              ; preds = %top
    %sum.shift32 = lshr i128 %9, 24, !dbg !2560
    %20 = trunc i128 %sum.shift32 to i64, !dbg !2560
    %21 = and i64 %20, 4503599627370495, !dbg !2560
    %22 = trunc i128 %9 to i64, !dbg !2563
    %23 = and i64 %22, 16777215, !dbg !2564
    %24 = or i64 %23, %21, !dbg !2566
    %25 = or i64 %24, 4949455990480175104, !dbg !2566
    %bitcast_coercion23 = bitcast i64 %25 to double, !dbg !2568
    %26 = fadd double %bitcast_coercion23, 0xC4B0000000000000, !dbg !2569
    %27 = lshr i128 %9, 76, !dbg !2570
    %28 = trunc i128 %27 to i64, !dbg !2573
    %29 = or i64 %28, 5183643171103440896, !dbg !2574
    %bitcast_coercion24 = bitcast i64 %29 to double, !dbg !2576
    %30 = fadd double %bitcast_coercion24, 0xC7F0000000000000, !dbg !2577
    %31 = fadd double %30, %26, !dbg !2578
    br label %L54, !dbg !2580

  L54:                                              ; preds = %L29, %L12
    %bitcast_coercion3.pn.in = phi double [ %19, %L12 ], [ %31, %L29 ]
    %bitcast_coercion3.pn = bitcast double %bitcast_coercion3.pn.in to i64, !dbg !2581
    %bitcast_coercion3.pn.masked = and i64 %bitcast_coercion3.pn, -134217728, !dbg !2582
    %32 = or i64 %bitcast_coercion3.pn.masked, %7, !dbg !2582
    %bitcast_coercion6 = bitcast i64 %32 to double, !dbg !2585
    %33 = fcmp ult double %bitcast_coercion6, 0xC7E0000000000000, !dbg !2586
    %34 = fcmp uge double %bitcast_coercion6, 0x47E0000000000000, !dbg !2587
    %35 = or i1 %33, %34, !dbg !2587
    br i1 %35, label %L118, label %L65, !dbg !2587

  L65:                                              ; preds = %L54
    %36 = call double @llvm.trunc.f64(double %bitcast_coercion6) #37, !dbg !2591
    %37 = fsub double %bitcast_coercion6, %36, !dbg !2595
    %38 = fcmp une double %37, 0.000000e+00, !dbg !2596
    br i1 %38, label %L118, label %L85, !dbg !2587

  L85:                                              ; preds = %L65
    %39 = lshr i64 %bitcast_coercion3.pn, 52, !dbg !2598
    %40 = and i64 %39, 2047, !dbg !2603
    %41 = and i64 %bitcast_coercion3.pn, 4503599493152768, !dbg !2605
    %42 = or i64 %41, 4503599627370496, !dbg !2607
    %43 = icmp ugt i64 %40, 1075, !dbg !2608
    br i1 %43, label %L101, label %L91, !dbg !2609

  L91:                                              ; preds = %L85
    %44 = sub nuw nsw i64 1075, %40, !dbg !2610
    %45 = lshr i64 %42, %44, !dbg !2612
    %46 = icmp ult i64 %40, 1012, !dbg !2612
    %47 = select i1 %46, i64 0, i64 %45, !dbg !2612
    %48 = zext i64 %47 to i128, !dbg !2614
    br label %L110, !dbg !2614

  L101:                                             ; preds = %L85
    %49 = add nsw i64 %40, -1075, !dbg !2616
    %50 = zext i64 %42 to i128, !dbg !2617
    %51 = zext i64 %49 to i128, !dbg !2620
    %52 = shl i128 %50, %51, !dbg !2620
    %53 = icmp ugt i64 %49, 127, !dbg !2620
    %54 = select i1 %53, i128 0, i128 %52, !dbg !2620
    br label %L110, !dbg !2622

  L110:                                             ; preds = %L101, %L91
    %value_phi9 = phi i128 [ %48, %L91 ], [ %54, %L101 ]
    %55 = sext i64 %32 to i128, !dbg !2623
    %56 = xor i128 %value_phi9, %55, !dbg !2627
    %57 = ashr i128 %56, 127, !dbg !2629
    %58 = add i128 %57, %value_phi9, !dbg !2629
    %59 = xor i128 %58, %57, !dbg !2629
    %60 = sub i128 %1, %59, !dbg !2630
    %61 = ashr i128 %60, 127, !dbg !2632
    %62 = add i128 %61, %60, !dbg !2635
    %63 = xor i128 %62, %61, !dbg !2635
    %64 = icmp ugt i128 %63, 20282409603651670423947251286015, !dbg !2639
    br i1 %64, label %L152, label %L135, !dbg !2640

  L118:                                             ; preds = %L65, %L54
    %current_task128 = getelementptr inbounds {}**, {}*** %2, i64 -14
    %current_task1 = bitcast {}*** %current_task128 to {}**
    %box = call noalias nonnull dereferenceable(8) {} addrspace(10)* @julia.gc_alloc_obj({}** nonnull %current_task1, i64 noundef 8, {} addrspace(10)* noundef addrspacecast ({}* inttoptr (i64 128537556952992 to {}*) to {} addrspace(10)*)) #38, !dbg !2641
    %65 = bitcast {} addrspace(10)* %box to i64 addrspace(10)*, !dbg !2641
    store i64 %32, i64 addrspace(10)* %65, align 8, !dbg !2641, !tbaa !639, !alias.scope !111, !noalias !2642
    %66 = call nonnull {} addrspace(10)* ({} addrspace(10)* ({} addrspace(10)*, {} addrspace(10)**, i32, {} addrspace(10)*)*, {} addrspace(10)*, {} addrspace(10)*, ...) @julia.call2({} addrspace(10)* ({} addrspace(10)*, {} addrspace(10)**, i32, {} addrspace(10)*)* noundef nonnull @ijl_invoke, {} addrspace(10)* noundef addrspacecast ({}* inttoptr (i64 128537537854736 to {}*) to {} addrspace(10)*), {} addrspace(10)* noundef addrspacecast ({}* inttoptr (i64 128537484962240 to {}*) to {} addrspace(10)*), {} addrspace(10)* addrspacecast ({}* inttoptr (i64 128537713982584 to {}*) to {} addrspace(10)*), {} addrspace(10)* addrspacecast ({}* inttoptr (i64 128537484931168 to {}*) to {} addrspace(10)*), {} addrspace(10)* nofree nonnull %box) #39, !dbg !2641
    %67 = addrspacecast {} addrspace(10)* %66 to {} addrspace(12)*, !dbg !2641
    call void @ijl_throw({} addrspace(12)* %67) #40, !dbg !2641
    unreachable, !dbg !2641

  L135:                                             ; preds = %L110
    %68 = trunc i128 %63 to i64, !dbg !2645
    %69 = and i64 %68, 4503599627370495, !dbg !2647
    %70 = or i64 %69, 4841369599423283200, !dbg !2648
    %bitcast_coercion11 = bitcast i64 %70 to double, !dbg !2650
    %71 = fadd double %bitcast_coercion11, 0xC330000000000000, !dbg !2651
    %72 = lshr i128 %63, 52, !dbg !2652
    %73 = trunc i128 %72 to i64, !dbg !2655
    %74 = or i64 %73, 5075556780046548992, !dbg !2656
    %bitcast_coercion12 = bitcast i64 %74 to double, !dbg !2658
    %75 = fadd double %bitcast_coercion12, 0xC670000000000000, !dbg !2659
    %76 = fadd double %71, %75, !dbg !2660
    br label %L177, !dbg !2662

  L152:                                             ; preds = %L110
    %sum.shift = lshr i128 %63, 24, !dbg !2663
    %77 = trunc i128 %sum.shift to i64, !dbg !2663
    %78 = and i64 %77, 4503599627370495, !dbg !2663
    %79 = trunc i128 %63 to i64, !dbg !2666
    %80 = and i64 %79, 16777215, !dbg !2667
    %81 = or i64 %80, %78, !dbg !2669
    %82 = or i64 %81, 4949455990480175104, !dbg !2669
    %bitcast_coercion16 = bitcast i64 %82 to double, !dbg !2671
    %83 = fadd double %bitcast_coercion16, 0xC4B0000000000000, !dbg !2672
    %84 = lshr i128 %63, 76, !dbg !2673
    %85 = trunc i128 %84 to i64, !dbg !2676
    %86 = or i64 %85, 5183643171103440896, !dbg !2677
    %bitcast_coercion17 = bitcast i64 %86 to double, !dbg !2679
    %87 = fadd double %bitcast_coercion17, 0xC7F0000000000000, !dbg !2680
    %88 = fadd double %87, %83, !dbg !2681
    br label %L177, !dbg !2683

  L177:                                             ; preds = %L152, %L135
    %bitcast_coercion13.pn.in = phi double [ %76, %L135 ], [ %88, %L152 ]
    %89 = trunc i128 %61 to i64, !dbg !2684
    %90 = shl nsw i64 %89, 63, !dbg !2685
    %bitcast_coercion13.pn = bitcast double %bitcast_coercion13.pn.in to i64, !dbg !2687
    %value_phi15.in = or i64 %90, %bitcast_coercion13.pn, !dbg !2687
    %91 = bitcast [2 x double]* %0 to i64*, !dbg !2631
    store i64 %32, i64* %91, align 8, !dbg !2631, !noalias !2688
    %newstruct.sroa.2.0..sroa_idx27 = getelementptr inbounds [2 x double], [2 x double]* %0, i64 0, i64 1, !dbg !2631
    %92 = bitcast double* %newstruct.sroa.2.0..sroa_idx27 to i64*, !dbg !2631
    store i64 %value_phi15.in, i64* %92, align 8, !dbg !2631, !noalias !2688
    ret void, !dbg !2631
  }

   Type analysis state: 
  <analysis>
  i64 4503599627370496: {[-1]:Anything}, intvals: {4503599627370496,}
    %37 = fsub double %bitcast_coercion6, %36, !dbg !128: {[-1]:Float@double}, intvals: {}
    %38 = fcmp une double %37, 0.000000e+00, !dbg !129: {[-1]:Integer}, intvals: {}
    %39 = lshr i64 %bitcast_coercion3.pn, 52, !dbg !132: {}, intvals: {}
  i64 5075556780046548992: {[-1]:Anything}, intvals: {5075556780046548992,}
    %14 = fadd double %bitcast_coercion, 0xC330000000000000, !dbg !67: {[-1]:Float@double}, intvals: {}
    %15 = lshr i128 %9, 52, !dbg !69: {[-1]:Integer}, intvals: {}
    %16 = trunc i128 %15 to i64, !dbg !72: {[-1]:Integer}, intvals: {}
  [2 x double]* %0: {[-1]:Pointer, [-1,-1]:Float@double}, intvals: {}
  i128 %1: {[-1]:Integer}, intvals: {}
  i64 1012: {[-1]:Integer}, intvals: {1012,}
    %3 = bitcast {}*** %ptls_field29 to i64***: {[-1]:Pointer}, intvals: {}
    %bitcast_coercion3.pn = bitcast double %bitcast_coercion3.pn.in to i64, !dbg !102: {[-1]:Float@double}, intvals: {}
    %bitcast_coercion6 = bitcast i64 %32 to double, !dbg !108: {[-1]:Float@double}, intvals: {}
  {}* inttoptr (i64 128537556952992 to {}*): {[-1]:Anything}, intvals: {}
  i64 52: {[-1]:Integer}, intvals: {52,}
  i64 4949455990480175104: {[-1]:Anything}, intvals: {4949455990480175104,}
    %bitcast_coercion3.pn.in = phi double [ %19, %L12 ], [ %31, %L29 ]: {[-1]:Float@double}, intvals: {}
    %68 = trunc i128 %63 to i64, !dbg !199: {}, intvals: {}
    %40 = and i64 %39, 2047, !dbg !138: {[-1]:Integer}, intvals: {}
    %41 = and i64 %bitcast_coercion3.pn, 4503599493152768, !dbg !140: {}, intvals: {}
    %42 = or i64 %41, 4503599627370496, !dbg !142: {}, intvals: {}
    %43 = icmp ugt i64 %40, 1075, !dbg !143: {[-1]:Integer}, intvals: {}
    %44 = sub nuw nsw i64 1075, %40, !dbg !146: {[-1]:Integer}, intvals: {}
    %45 = lshr i64 %42, %44, !dbg !149: {}, intvals: {}
    %4 = getelementptr inbounds i64*, i64** %ptls_load3031, i64 2: {[-1]:Pointer}, intvals: {}
    %36 = call double @llvm.trunc.f64(double %bitcast_coercion6) #37, !dbg !119: {[-1]:Float@double}, intvals: {}
  i64 16777215: {[-1]:Anything}, intvals: {16777215,}
  i64 -134217728: {[-1]:Integer}, intvals: {-134217728,}
  double 0xC7E0000000000000: {[-1]:Float@double}, intvals: {}
    %84 = lshr i128 %63, 76, !dbg !227: {}, intvals: {}
    %85 = trunc i128 %84 to i64, !dbg !230: {}, intvals: {}
    %86 = or i64 %85, 5183643171103440896, !dbg !231: {}, intvals: {}
    %bitcast_coercion17 = bitcast i64 %86 to double, !dbg !233: {[-1]:Float@double}, intvals: {}
    %87 = fadd double %bitcast_coercion17, 0xC7F0000000000000, !dbg !234: {[-1]:Float@double}, intvals: {}
    %88 = fadd double %87, %83, !dbg !235: {[-1]:Float@double}, intvals: {}
    %72 = lshr i128 %63, 52, !dbg !206: {}, intvals: {}
    %74 = or i64 %73, 5075556780046548992, !dbg !210: {}, intvals: {}
    %bitcast_coercion12 = bitcast i64 %74 to double, !dbg !212: {[-1]:Float@double}, intvals: {}
    %75 = fadd double %bitcast_coercion12, 0xC670000000000000, !dbg !213: {[-1]:Float@double}, intvals: {}
    %76 = fadd double %71, %75, !dbg !214: {[-1]:Float@double}, intvals: {}
    %sum.shift = lshr i128 %63, 24, !dbg !217: {}, intvals: {}
  i128 52: {[-1]:Integer}, intvals: {52,}
  i64 4503599627370495: {[-1]:Anything}, intvals: {4503599627370495,}
  i64 -1075: {[-1]:Integer}, intvals: {-1075,}
    %46 = icmp ult i64 %40, 1012, !dbg !149: {[-1]:Integer}, intvals: {}
    %47 = select i1 %46, i64 0, i64 %45, !dbg !149: {}, intvals: {}
    %48 = zext i64 %47 to i128, !dbg !151: {}, intvals: {}
    %49 = add nsw i64 %40, -1075, !dbg !156: {[-1]:Integer}, intvals: {}
    %50 = zext i64 %42 to i128, !dbg !157: {}, intvals: {}
    %51 = zext i64 %49 to i128, !dbg !160: {[-1]:Integer}, intvals: {}
    %bitcast_coercion13.pn.in = phi double [ %76, %L135 ], [ %88, %L152 ]: {[-1]:Float@double}, intvals: {}
  i64 0: {[-1]:Anything}, intvals: {0,}
  double 0xC670000000000000: {[-1]:Float@double}, intvals: {}
    %bitcast_coercion23 = bitcast i64 %25 to double, !dbg !89: {[-1]:Anything}, intvals: {}
    %26 = fadd double %bitcast_coercion23, 0xC4B0000000000000, !dbg !90: {[-1]:Float@double}, intvals: {}
    %27 = lshr i128 %9, 76, !dbg !91: {[-1]:Integer}, intvals: {}
    %28 = trunc i128 %27 to i64, !dbg !94: {[-1]:Integer}, intvals: {}
    %2 = call {}*** @julia.get_pgcstack() #37: {}, intvals: {}
    %5 = ashr i128 %1, 127, !dbg !34: {[-1]:Integer}, intvals: {}
    %7 = shl nsw i64 %6, 63, !dbg !44: {[-1]:Integer}, intvals: {}
    %19 = fadd double %14, %18, !dbg !77: {[-1]:Float@double}, intvals: {}
    %sum.shift32 = lshr i128 %9, 24, !dbg !81: {[-1]:Integer}, intvals: {}
    %21 = and i64 %20, 4503599627370495, !dbg !81: {[-1]:Anything}, intvals: {}
    %12 = and i64 %11, 4503599627370495, !dbg !59: {[-1]:Anything}, intvals: {}
    %13 = or i64 %12, 4841369599423283200, !dbg !61: {[-1]:Anything}, intvals: {}
    %bitcast_coercion = bitcast i64 %13 to double, !dbg !64: {[-1]:Anything}, intvals: {}
  i64 8: {[-1]:Integer}, intvals: {8,}
  i128 20282409603651670423947251286015: {[-1]:Anything}, intvals: {}
  i64 1075: {[-1]:Integer}, intvals: {1075,}
    %box = call noalias nonnull dereferenceable(8) {} addrspace(10)* @julia.gc_alloc_obj({}** nonnull %current_task1, i64 noundef 8, {} addrspace(10)* noundef addrspacecast ({}* inttoptr (i64 128537556952992 to {}*) to {} addrspace(10)*)) #38, !dbg !184: {[-1,-1]:Float@double}, intvals: {}
  double 0x47E0000000000000: {[-1]:Float@double}, intvals: {}
  i128 127: {[-1]:Integer}, intvals: {127,}
    %29 = or i64 %28, 5183643171103440896, !dbg !95: {[-1]:Anything}, intvals: {}
    %bitcast_coercion24 = bitcast i64 %29 to double, !dbg !97: {[-1]:Anything}, intvals: {}
    %30 = fadd double %bitcast_coercion24, 0xC7F0000000000000, !dbg !98: {[-1]:Float@double}, intvals: {}
    %31 = fadd double %30, %26, !dbg !99: {[-1]:Float@double}, intvals: {}
    %57 = ashr i128 %56, 127, !dbg !172: {}, intvals: {}
    %58 = add i128 %57, %value_phi9, !dbg !172: {}, intvals: {}
    %59 = xor i128 %58, %57, !dbg !172: {}, intvals: {}
    %60 = sub i128 %1, %59, !dbg !173: {}, intvals: {}
    %61 = ashr i128 %60, 127, !dbg !175: {}, intvals: {}
    %62 = add i128 %61, %60, !dbg !178: {}, intvals: {}
    %63 = xor i128 %62, %61, !dbg !178: {}, intvals: {}
  i64 4841369599423283200: {[-1]:Anything}, intvals: {4841369599423283200,}
    %17 = or i64 %16, 5075556780046548992, !dbg !73: {[-1]:Anything}, intvals: {}
    %bitcast_coercion2 = bitcast i64 %17 to double, !dbg !75: {[-1]:Anything}, intvals: {}
    %18 = fadd double %bitcast_coercion2, 0xC670000000000000, !dbg !76: {[-1]:Float@double}, intvals: {}
  i64 127: {[-1]:Integer}, intvals: {127,}
  i64 5183643171103440896: {[-1]:Anything}, intvals: {5183643171103440896,}
  i128 0: {[-1]:Anything}, intvals: {0,}
    %8 = add i128 %5, %1, !dbg !47: {[-1]:Integer}, intvals: {}
    %9 = xor i128 %8, %5, !dbg !47: {[-1]:Integer}, intvals: {}
    %22 = trunc i128 %9 to i64, !dbg !84: {[-1]:Integer}, intvals: {}
    %23 = and i64 %22, 16777215, !dbg !85: {[-1]:Anything}, intvals: {}
    %24 = or i64 %23, %21, !dbg !87: {[-1]:Anything}, intvals: {}
    %25 = or i64 %24, 4949455990480175104, !dbg !87: {[-1]:Anything}, intvals: {}
    %33 = fcmp ult double %bitcast_coercion6, 0xC7E0000000000000, !dbg !109: {[-1]:Integer}, intvals: {}
    %10 = icmp ugt i128 %9, 20282409603651670423947251286015, !dbg !54: {[-1]:Integer}, intvals: {}
    %20 = trunc i128 %sum.shift32 to i64, !dbg !81: {[-1]:Integer}, intvals: {}
    %ptls_load3031 = load i64**, i64*** %3, align 8, !tbaa !27: {}, intvals: {}
    %safepoint = load i64*, i64** %4, align 8, !tbaa !31: {}, intvals: {}
    %6 = trunc i128 %5 to i64, !dbg !42: {[-1]:Integer}, intvals: {}
  i128 24: {[-1]:Integer}, intvals: {24,}
  i64 2047: {[-1]:Integer}, intvals: {2047,}
    %52 = shl i128 %50, %51, !dbg !160: {}, intvals: {}
    %53 = icmp ugt i64 %49, 127, !dbg !160: {[-1]:Integer}, intvals: {}
    %54 = select i1 %53, i128 0, i128 %52, !dbg !160: {}, intvals: {}
    %value_phi9 = phi i128 [ %48, %L91 ], [ %54, %L101 ]: {}, intvals: {}
    %55 = sext i64 %32 to i128, !dbg !164: {[-1]:Integer}, intvals: {}
    %56 = xor i128 %value_phi9, %55, !dbg !170: {}, intvals: {}
  double 0xC4B0000000000000: {[-1]:Float@double}, intvals: {}
    %89 = trunc i128 %61 to i64, !dbg !238: {}, intvals: {}
    %90 = shl nsw i64 %89, 63, !dbg !239: {}, intvals: {}
    %bitcast_coercion13.pn = bitcast double %bitcast_coercion13.pn.in to i64, !dbg !241: {[-1]:Float@double}, intvals: {}
    %value_phi15.in = or i64 %90, %bitcast_coercion13.pn, !dbg !241: {}, intvals: {}
    %91 = bitcast [2 x double]* %0 to i64*, !dbg !174: {[-1]:Pointer, [-1,-1]:Float@double}, intvals: {}
    %bitcast_coercion3.pn.masked = and i64 %bitcast_coercion3.pn, -134217728, !dbg !103: {}, intvals: {}
    %32 = or i64 %bitcast_coercion3.pn.masked, %7, !dbg !103: {[-1]:Integer}, intvals: {}
    %34 = fcmp uge double %bitcast_coercion6, 0x47E0000000000000, !dbg !111: {[-1]:Integer}, intvals: {}
    %35 = or i1 %33, %34, !dbg !111: {[-1]:Integer}, intvals: {}
  double 0.000000e+00: {[-1]:Anything}, intvals: {}
    %77 = trunc i128 %sum.shift to i64, !dbg !217: {}, intvals: {}
    %78 = and i64 %77, 4503599627370495, !dbg !217: {}, intvals: {}
    %79 = trunc i128 %63 to i64, !dbg !220: {}, intvals: {}
    %80 = and i64 %79, 16777215, !dbg !221: {}, intvals: {}
    %81 = or i64 %80, %78, !dbg !223: {}, intvals: {}
    %82 = or i64 %81, 4949455990480175104, !dbg !223: {}, intvals: {}
    %bitcast_coercion16 = bitcast i64 %82 to double, !dbg !225: {[-1]:Float@double}, intvals: {}
    %83 = fadd double %bitcast_coercion16, 0xC4B0000000000000, !dbg !226: {[-1]:Float@double}, intvals: {}
  {} addrspace(10)* addrspacecast ({}* inttoptr (i64 128537556952992 to {}*) to {} addrspace(10)*): {[-1]:Anything}, intvals: {}
  i128 76: {[-1]:Integer}, intvals: {76,}
    %ptls_field29 = getelementptr inbounds {}**, {}*** %2, i64 2: {}, intvals: {}
  i64 63: {[-1]:Integer}, intvals: {63,}
  i64 4503599493152768: {[-1]:Anything}, intvals: {4503599493152768,}
    %64 = icmp ugt i128 %63, 20282409603651670423947251286015, !dbg !182: {[-1]:Integer}, intvals: {}
    %current_task1 = bitcast {}*** %current_task128 to {}**: {}, intvals: {}
    %69 = and i64 %68, 4503599627370495, !dbg !201: {}, intvals: {}
    %70 = or i64 %69, 4841369599423283200, !dbg !202: {}, intvals: {}
    %bitcast_coercion11 = bitcast i64 %70 to double, !dbg !204: {[-1]:Float@double}, intvals: {}
    %71 = fadd double %bitcast_coercion11, 0xC330000000000000, !dbg !205: {[-1]:Float@double}, intvals: {}
    %73 = trunc i128 %72 to i64, !dbg !209: {}, intvals: {}
  double 0xC330000000000000: {[-1]:Float@double}, intvals: {}
    %11 = trunc i128 %9 to i64, !dbg !57: {[-1]:Integer}, intvals: {}
  double 0xC7F0000000000000: {[-1]:Float@double}, intvals: {}
  </analysis>

  Illegal updateAnalysis prev:{[-1]:Pointer, [-1,-1]:Float@double} new: {[-1]:Pointer, [-1,0]:Integer, [-1,1]:Integer, [-1,2]:Integer, [-1,3]:Integer, [-1,4]:Integer, [-1,5]:Integer, [-1,6]:Integer, [-1,7]:Integer}
  val:   %91 = bitcast [2 x double]* %0 to i64*, !dbg !174 origin=  store i64 %32, i64* %91, align 8, !dbg !174, !noalias !242
  MethodInstance for Base.splitprec(::Type{Float64}, ::Int128)

  Caused by:
  Stacktrace:
   [1] splitprec
     @ ./twiceprecision.jl:25

  Stacktrace:
    [1] julia_error(cstr::Cstring, val::Ptr{LLVM.API.LLVMOpaqueValue}, errtype::Enzyme.API.ErrorType, data::Ptr{Nothing}, data2::Ptr{LLVM.API.LLVMOpaqueValue}, B::Ptr{LLVM.API.LLVMOpaqueBuilder})
      @ Enzyme.Compiler ~/.julia/packages/Enzyme/MIIMf/src/compiler.jl:1652
    [2] EnzymeCreatePrimalAndGradient(logic::Enzyme.Logic, todiff::LLVM.Function, retType::Enzyme.API.CDIFFE_TYPE, constant_args::Vector{Enzyme.API.CDIFFE_TYPE}, TA::Enzyme.TypeAnalysis, returnValue::Bool, dretUsed::Bool, mode::Enzyme.API.CDerivativeMode, width::Int64, additionalArg::Ptr{Nothing}, forceAnonymousTape::Bool, typeInfo::Enzyme.FnTypeInfo, uncacheable_args::Vector{Bool}, augmented::Ptr{Nothing}, atomicAdd::Bool)
      @ Enzyme.API ~/.julia/packages/Enzyme/MIIMf/src/api.jl:154
    [3] enzyme!(job::GPUCompiler.CompilerJob{Enzyme.Compiler.EnzymeTarget, Enzyme.Compiler.EnzymeCompilerParams}, mod::LLVM.Module, primalf::LLVM.Function, TT::Type, mode::Enzyme.API.CDerivativeMode, width::Int64, parallel::Bool, actualRetType::Type, wrap::Bool, modifiedBetween::Tuple{Bool, Bool}, returnPrimal::Bool, expectedTapeType::Type, loweredArgs::Set{Int64}, boxedArgs::Set{Int64})
      @ Enzyme.Compiler ~/.julia/packages/Enzyme/MIIMf/src/compiler.jl:3109
    [4] codegen(output::Symbol, job::GPUCompiler.CompilerJob{Enzyme.Compiler.EnzymeTarget, Enzyme.Compiler.EnzymeCompilerParams}; libraries::Bool, deferred_codegen::Bool, optimize::Bool, toplevel::Bool, strip::Bool, validate::Bool, only_entry::Bool, parent_job::Nothing)
      @ Enzyme.Compiler ~/.julia/packages/Enzyme/MIIMf/src/compiler.jl:4964
    [5] codegen
      @ ~/.julia/packages/Enzyme/MIIMf/src/compiler.jl:4391 [inlined]
    [6] _thunk(job::GPUCompiler.CompilerJob{Enzyme.Compiler.EnzymeTarget, Enzyme.Compiler.EnzymeCompilerParams}, postopt::Bool)
      @ Enzyme.Compiler ~/.julia/packages/Enzyme/MIIMf/src/compiler.jl:5646
    [7] _thunk
      @ ~/.julia/packages/Enzyme/MIIMf/src/compiler.jl:5646 [inlined]
    [8] cached_compilation
      @ ~/.julia/packages/Enzyme/MIIMf/src/compiler.jl:5680 [inlined]
    [9] (::Enzyme.Compiler.var"#532#533"{DataType, UnionAll, DataType, Enzyme.API.CDerivativeMode, Tuple{Bool, Bool}, Int64, Bool, Bool, UInt64, DataType})(ctx::LLVM.Context)
      @ Enzyme.Compiler ~/.julia/packages/Enzyme/MIIMf/src/compiler.jl:5746
   [10] JuliaContext(f::Enzyme.Compiler.var"#532#533"{DataType, UnionAll, DataType, Enzyme.API.CDerivativeMode, Tuple{Bool, Bool}, Int64, Bool, Bool, UInt64, DataType}; kwargs::@Kwargs{})
      @ GPUCompiler ~/.julia/packages/GPUCompiler/kqxyC/src/driver.jl:52
   [11] JuliaContext(f::Function)
      @ GPUCompiler ~/.julia/packages/GPUCompiler/kqxyC/src/driver.jl:42
   [12] #s1926#531
      @ ~/.julia/packages/Enzyme/MIIMf/src/compiler.jl:5698 [inlined]
   [13] var"#s1926#531"(FA::Any, A::Any, TT::Any, Mode::Any, ModifiedBetween::Any, width::Any, ReturnPrimal::Any, ShadowInit::Any, World::Any, ABI::Any, ::Any, ::Type, ::Type, ::Type, tt::Any, ::Type, ::Type, ::Type, ::Type, ::Type, ::Any)
      @ Enzyme.Compiler ./none:0
   [14] (::Core.GeneratedFunctionStub)(::UInt64, ::LineNumberNode, ::Any, ::Vararg{Any})
      @ Core ./boot.jl:602
   [15] autodiff
      @ ~/.julia/packages/Enzyme/MIIMf/src/Enzyme.jl:270 [inlined]
   [16] autodiff(mode::ReverseMode{true, FFIABI, false}, f::typeof(summat), ::Type{Active}, args::Active{Float64})
      @ Enzyme ~/.julia/packages/Enzyme/MIIMf/src/Enzyme.jl:287
   [17] macro expansion
      @ show.jl:1181 [inlined]
   [18] macro expansion
      @ ~/.julia/dev/EnzymeTests/test/mat_tests.jl:16 [inlined]
   [19] macro expansion
      @ /usr/share/julia/stdlib/v1.10/Test/src/Test.jl:1577 [inlined]
   [20] top-level scope
      @ ~/.julia/dev/EnzymeTests/test/mat_tests.jl:16
   [21] include(fname::String)
      @ Base.MainInclude ./client.jl:489
   [22] macro expansion
      @ ~/.julia/dev/EnzymeTests/test/runtests.jl:6 [inlined]
   [23] macro expansion
      @ /usr/share/julia/stdlib/v1.10/Test/src/Test.jl:1577 [inlined]
   [24] top-level scope
      @ ~/.julia/dev/EnzymeTests/test/runtests.jl:6
   [25] include(fname::String)
      @ Base.MainInclude ./client.jl:489
   [26] top-level scope
      @ none:6
   [27] eval
      @ ./boot.jl:385 [inlined]
   [28] exec_options(opts::Base.JLOptions)
      @ Base ./client.jl:291
   [29] _start()
      @ Base ./client.jl:552
just-walk commented 2 months ago

Also I get a similar error with this input in 0.11.20 and 0.12. Enzyme doesn't like using a value from a range in a loop.

INPUT:

function summat(p::Real)
    n = 2
    A = Matrix{typeof(p)}(undef, n, n)
    n_points = n^2
    interval = 2 * π
    z_range = range(start = -interval / 2, stop = interval / 2, length = n_points)

    for (i, z) in enumerate(z_range)
        A[i] = i + z
    end
    return sum(A)
end

@show autodiff(ReverseWithPrimal, summat, Active, Active(3.0))
wsmoses commented 2 months ago

@just-walk I think the issue here is the float range, not the enumerate. This is known to have issues by under the hood being implemented with TwicePrecision which is not supported atm.

Duplicate of https://github.com/EnzymeAD/Enzyme.jl/issues/274