Adjointing around Mutating Part

I have the following toy problem that imitates my more complex problem. (A follow up from this question.)

function triangulate(x::Matrix{T}) where T
    m, n = size(x)
    a = x[:,1] .* [1; zeros(T, m-1)]
    for i in 2:n
        a = accumulate(+, a) .* [x[1:i, i]; zeros(T, m-i)]    # Offending Line

x1 = (1:4).*(1:4)'
gradient(x->triangulate(x)[end], x1)[1]    

This throws Error: Mutating arrays is not supported. I was wondering since the offending line is obvious in the above code. How can I extract that line as a method and write the custom adjoint for myself. How would the API for that look like?

This would save me from writing gradient for the entire function, to just writing it for that one line. Right?