setAlreadyVectorized does not delete obsolete metadata?

I noticed that LoopVectorizeHints::setAlreadyVectorized never deletes old "llvm.loop...." metadata. It just appends more, possibly contradicting the old metadata. E.g., after vectorization, a loop previously marked with llvm.loop.vectorize.width ends up with *two* such annotations, like this:

    br i1 %exitcond.1, label %for.end.loopexit.unr-lcssa, label %for.body, !llvm.loop !8
    ...
    !6 = metadata !{metadata !"llvm.loop.vectorize.width", i32 1}
    !7 = metadata !{metadata !"llvm.loop.interleave.count", i32 1}
    !8 = metadata !{metadata !8, metadata !9, metadata !6, metadata !7}
    !9 = metadata !{metadata !"llvm.loop.vectorize.width", i32 4}

!6 and !9 are in the list for loopID !8 and they specify conflicting widths. Is this a bug, or is there a deliberate convention that if there are multiple llvm.loop.vectorize.width annotations, only the last one counts?

- Arch D. Robison
  Intel Corporation

It's a bug.

The vectorizer reads the metadata and writes it back with width = 1 to
stop it from being vectorized again (also avoids wasting time if it
wasn't worth it then), but it should not have two of the same and
especially not two *different* values. It could be a simple bug in the
writing back of the data, and should be simple to fix. Or it could be
that the loop was fused from two other loops with different metadata,
or inlining of code, etc, making it a bit more challenging.

Creating a bug with the appropriate sources and ways to reproduce it
is the best course of action.

Thanks!
-renato

Filed as Bugzilla 20655, with a .ll source file and instructions for reproducing it.

- Arch

Thanks!