This is an archive of the discontinued LLVM Phabricator instance.

[mlir][tblgen] Fix undefined behaviour found by MSVC debug iterators
ClosedPublic

Authored by zero9178 on Jan 14 2023, 5:16 AM.

Details

Summary

Incrementing past the end iterator of any container in C++ is immediate undefined behaviour.
This is guaranteed to occur in the loop condition due to the expression cur = earlyIncIt++, which when earlyIncIt is the end iterator (aka we just did the last iteration of the loop), will do an increment on the end iterator.

To fix this, the patch refactors the loop to a more conventional loop using iterators, with the only difference being that the increment happens through the erase operation, which conveniently returns an iterator to the element after the erased element. Thanks to that guarantee there is also no need to use std::list over std::vector.
I also opted to reduce the inner loop find_if, because I felt splitting the "search" and the effects of if it was successful made the code (subjectively) nicer, and also avoided having to add an extra "bool erased" to the outer loop body.

Diff Detail

Event Timeline

zero9178 created this revision.Jan 14 2023, 5:16 AM
zero9178 requested review of this revision.Jan 14 2023, 5:16 AM
mehdi_amini accepted this revision.Jan 14 2023, 5:50 AM
This revision is now accepted and ready to land.Jan 14 2023, 5:50 AM
jpienaar accepted this revision.Jan 14 2023, 8:40 AM

Thanks

This revision was landed with ongoing or failed builds.Jan 14 2023, 8:49 AM
This revision was automatically updated to reflect the committed changes.