Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic parameter inference fails in the presence of arithmetic operations within generic interfaces #6463

Open
saipraveenb25 opened this issue Feb 26, 2025 · 4 comments
Assignees
Labels
goal:client exploration Speculative work for a potential use case.

Comments

@saipraveenb25
Copy link
Collaborator

Minimal reproducer:

interface IModule<T, int N, int M>
{
    T eval(T x);
}

float foo<
    int N,
    Decoder : IModule<half, N + 6, 3>>
    (Decoder decoder, no_diff float3 wi )
{
    return wi.x;
}

void bar<
    int N,
    Encoder : IModule<half, 3, N>,
    Decoder : IModule<half, N + 6, 3>>
    (Encoder encoder, Decoder decoder, no_diff float3 wi)
{
    foo(decoder, wi);
}

Workaround is to be explicit about generic arguments, by invoking foo<N, Decoder> instead of just foo.

@saipraveenb25 saipraveenb25 added goal:client support Feature or fix needed for a current slang user. and removed goal:client support Feature or fix needed for a current slang user. labels Feb 26, 2025
@csyonghe
Copy link
Collaborator

I worried that supporting this is the beginning to set foot in a scarry territory of trying to solve complex symbolic linear systems in the Slang type system. I would like us to avoid having to deal with this complex can of worms if possible.

@ccummingsNV
Copy link
Contributor

ccummingsNV commented Feb 27, 2025

This came through as a potential issue with SlangPy as well, which turned out to be due to the underlying resolution problem. I've added a test for it to the SlangPy function resolution that runs with/without the work around (and is currently disabled for the without case): https://github.com/shader-slang/slangpy/blob/main/slangpy/tests/test_generics.py

Once the issue's resolved (either with a confirmed fix, or a confirmed won't-fix) I'll update the test accordingly.

@saipraveenb25
Copy link
Collaborator Author

saipraveenb25 commented Feb 27, 2025

@csyonghe: I agree, it feels like things like inferring N from N + 6 is simple enough, but it does get quickly harder when we start seeing constraints like N + M + 5 etc..

I was hoping we could do exact structural matches: N + 6 can match with M + 6 and is effectively an equality constraint (N == M).
Alternatively, we could provide a better error like "Cannot infer parameter "N" from constraints, use explicitly specialization with <...> ".
Another approach is to allow partially-specialized generics like foo<N>(decoder) where "Decoder" can be pulled from "decoder" but "N" is set explicitly.

@saipraveenb25
Copy link
Collaborator Author

Another comment is that these errors occur even if there is an additional equality constraint for N that can be used for inference instead.

E.g.: in

void bar<
    int N,
    Encoder : IModule<half, 3, N>,
    Decoder : IModule<half, N + 6, 3>>
    (Encoder encoder, Decoder decoder, no_diff float3 wi)
{
    foo(decoder, wi);
}

I think it's fine to not use N+6 for inference, but Encoder does have an N, so we can use that to infer N. Right now, I believe this doesn't work.

@bmillsNV bmillsNV added this to the Q2 2025 (Spring) milestone Feb 27, 2025
@bmillsNV bmillsNV added the goal:client exploration Speculative work for a potential use case. label Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
goal:client exploration Speculative work for a potential use case.
Projects
None yet
Development

No branches or pull requests

4 participants