© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
21 replies
InsertPi

✅ Modify delegate for ILGPU acceptance

This is a long one, strap in!

So I’m incorporating ILGPU into a library I’m writing for very simple but powerful/expressive/flexible parallel programming. ILGPU is gonna help power the upcoming GPU API.

With that said, ILGPU accepts either a delegate or a static method as a GPU kernel. The method called has to incorporate an instantiation of the
ILGPU.IIndex
ILGPU.IIndex
interface as the first parameter. For the sake of my API, I would like to avoid passing around ILGPU datatypes to delegates I accept, because that exposes implementation details to users of my library.

With that the background out of the way, ILGPU kernels, if they are a delegate, do not support captures or objects. This poses a problem for me, since I need to pass a delegate to the ILGPU library with an
IIndex
IIndex
but abstract that away in my API. This problem exists with other parameters, too.

What I want to be able to do is have something like:
delegate<Index1D, ArrayView<T>, void> kernel = (idx, av) =>
{
    int i = idx.X; //simple example
    var g = new GPUArray(av);
    action_received_from_user(i, g);
}
delegate<Index1D, ArrayView<T>, void> kernel = (idx, av) =>
{
    int i = idx.X; //simple example
    var g = new GPUArray(av);
    action_received_from_user(i, g);
}
but the problem here is that the
kernel
kernel
delegate captures
action_received_from_user
action_received_from_user
, which ILGPU doesn’t allow.

I tried passing in a struct as one of the kernel arguments that contains
action_received_from_user
action_received_from_user
as a member, but because delegates are managed types, they cannot be passed in as a kernel argument.

What I am thinking of is one of the following:
1. A way to on-the-fly inline (inline in the C/C++ sense)
action_received_from_user
action_received_from_user
into my kernel body.
2. A way to use Mono.Cecil or some other reflection API to manually inject CIL into the method body and change the argument types (I’m very familiar with CIL, less so with reflection)
3. Use Roslyn -> ??? -> Profit…?
4. Some other way to accomplish this.

Phew, that was long. I’ve been trying to think of solutions for days and I have tried just about everything under the sun, but none of them have worked. Any ideas?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Delegate type mismatch
C#CC# / help
3y ago
❔ CS0123No overload for 'CanvasMain_PreviewMouseMove' matches delegate for 'EventMouseHandler'
C#CC# / help
3y ago
Delegate with Task problem
C#CC# / help
2y ago
Delegate function with params
C#CC# / help
2y ago