Skip to content

Remove explicit checks against kp::Memory::type()#455

Merged
axsaucedo merged 2 commits intoKomputeProject:masterfrom
ahem:fix-algorithm-descriptor-type-check
Apr 13, 2026
Merged

Remove explicit checks against kp::Memory::type()#455
axsaucedo merged 2 commits intoKomputeProject:masterfrom
ahem:fix-algorithm-descriptor-type-check

Conversation

@ahem
Copy link
Copy Markdown
Contributor

@ahem ahem commented Apr 13, 2026

There is a few places in the codebase where assumptions are being made based on what a memory objects virtual kp::Memory::type() method returns. This makes it hard to create new subclasses because there is an implicit assumption that if type() returns memory::Type::eImage then it is kp::Image and will behave like that.

This PR fixes two of those issues:

  • Update Algorithm.cpp to call mem->getDescriptorType() instead of mem->type() when allocating descriptor pools. With the current subclasses of kp::Memory this does not make a difference, but the explicit checks against memory type makes it hard to create new subclasses.
  • Update OpAlgoDispatch.cpp to actually check if the memory object is safe to cast to kp::Image instead of assuming that if mem->type() returns memory::Type::eImage this casting is safe.

With these changes it becomes safe to create new subclasses of kp::Memory that wraps images and uses image descriptors internally, without forcing those to subclass kp::Image (which isn't virtual and can't be subclassed anyway).

Those new subclasses will not really work with the strong-typed copy methods but they can implement their own copy methods for that if they feel like it.

@ahem ahem force-pushed the fix-algorithm-descriptor-type-check branch from 27697df to 401d1b6 Compare April 13, 2026 11:03
The function that was prevously used, `mem->type()`, is not explicitly
the same as the descriptor type, so using `mem->getDescriptorType()` is
more accurate.

It also makes it easier to create new image-like subclasses of
`kp::Memory` because as long as they specify the correct descriptor
type, the correct pools will be created.

Signed-off-by: Anders Hellerup Madsen <anders@hellerup-madsen.dk>
@ahem ahem force-pushed the fix-algorithm-descriptor-type-check branch from 401d1b6 to b888179 Compare April 13, 2026 11:04
The if statement used to assume that if `mem->type() ==
memory::Type::eImage` then it was safe to assume that the memory object
is an instance of `kp::Image`. However, any subclass of `kp::Memory`
could implement the virtual `type()` method to return
`memory::Type::eImage`, so this is an invalid assumption.

This commit fixes the issue by only casting if the memory object
actually is an instance of `kp::Memory`

Signed-off-by: Anders Hellerup Madsen <anders@hellerup-madsen.dk>
@ahem ahem force-pushed the fix-algorithm-descriptor-type-check branch from dbdfab5 to 94a1449 Compare April 13, 2026 12:52
@ahem ahem changed the title algorithm: check DescriptorType when creating pool Remove explicit checks against kp::Memory::type() Apr 13, 2026
@ahem
Copy link
Copy Markdown
Contributor Author

ahem commented Apr 13, 2026

@axsaucedo Sorry for updating the PR a few times. It is ready for review now.

@axsaucedo
Copy link
Copy Markdown
Member

Great catch - thanks for the contribution!

@axsaucedo axsaucedo merged commit 7f81331 into KomputeProject:master Apr 13, 2026
9 checks passed
@ahem ahem deleted the fix-algorithm-descriptor-type-check branch April 13, 2026 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants