vulkan: Detect pNext chain loops in vk_foreach_struct()

This implements the "tortoise and the hare" algorithm for detecting
cycles in graphs.  We use the caller's iterator as the hare and our own
internal copy as the tortoise.  Conveniently, VkBaseOutStructure (and
VkBaseInStructure which is identical except the pointer type on pNext)
have a pointer we can use for the tortoise and an sType which we can use
for a counter to ensure we only increment the tortose every other loop
iteration.

There are more efficient algorithms than tortoise and hare but they
require allocating memory for something like a hash set of seen nodes.
Since this for debug purposes only, it's ok for it to be a bit
inefficient in the case where it hits the assert.  In the usual case of
no loops, it's the same runtime efficiency as the unchecked version
except that it does a tiny bit of math and 50% more pointer chases.

Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com>
Reviewed-by: Jesse Natalie <jenatali@microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/17596>
This commit is contained in:
Jason Ekstrand 2022-07-18 09:05:59 -05:00 committed by Marge Bot
parent 94bd06256a
commit 4c56b535f5
1 changed files with 39 additions and 2 deletions

View File

@ -37,13 +37,50 @@ extern "C" {
#include <vulkan/vulkan.h>
#ifdef NDEBUG
#define vk_foreach_struct(__iter, __start) \
for (struct VkBaseOutStructure *__iter = (struct VkBaseOutStructure *)(__start); \
for (VkBaseOutStructure *__iter = (VkBaseOutStructure *)(__start); \
__iter; __iter = __iter->pNext)
#define vk_foreach_struct_const(__iter, __start) \
for (const struct VkBaseInStructure *__iter = (const struct VkBaseInStructure *)(__start); \
for (const VkBaseInStructure *__iter = (const VkBaseInStructure *)(__start); \
__iter; __iter = __iter->pNext)
#else
static inline void
__vk_foreach_debug_next(VkBaseOutStructure **iter,
VkBaseOutStructure *chaser)
{
assert(*iter);
*iter = (*iter)->pNext;
if ((int)chaser->sType & 1) {
/** This the "tortoise and the hare" algorithm. We increment
* chaser->pNext every other time *iter gets incremented. Because *iter
* is incrementing twice as fast as chaser->pNext, the distance between
* them in the list increases by one for each time we get here. If we
* have a loop, eventually, both iterators will be inside the loop and
* this distance will be an integer multiple of the loop length, at
* which point the two pointers will be equal.
*/
chaser->pNext = chaser->pNext->pNext;
if (chaser->pNext == *iter)
assert(!"Vulkan input pNext chain has a loop!");
}
chaser->sType = (VkStructureType)((int)chaser->sType + 1);
}
#define vk_foreach_struct(__iter, __start) \
for (VkBaseOutStructure *__iter = (VkBaseOutStructure *)(__start), \
__chaser = { (VkStructureType)0, (VkBaseOutStructure *)(__start) }; \
__iter; __vk_foreach_debug_next(&__iter, &__chaser))
#define vk_foreach_struct_const(__iter, __start) \
for (const VkBaseInStructure *__iter = (const VkBaseInStructure *)(__start), \
__chaser = { (VkStructureType)0, (const VkBaseInStructure *)(__start) }; \
__iter; __vk_foreach_debug_next((VkBaseOutStructure **)&__iter, \
(VkBaseOutStructure *)&__chaser))
#endif
/**
* A wrapper for a Vulkan output array. A Vulkan output array is one that