Skip to content

Commit cdd5e66

Browse files
authored
Retain int casts when unifying generic params (shader-slang#3145)
1 parent 036a2b7 commit cdd5e66

File tree

3 files changed

+58
-23
lines changed

3 files changed

+58
-23
lines changed

source/slang/slang-check-constraint.cpp

+17-23
Original file line numberDiff line numberDiff line change
@@ -526,30 +526,24 @@ namespace Slang
526526
}
527527

528528
// Check if both are integer values in general
529-
if (auto fstInt = as<IntVal>(fst))
529+
const auto fstInt = as<IntVal>(fst);
530+
const auto sndInt = as<IntVal>(snd);
531+
if (fstInt && sndInt)
530532
{
531-
if (auto tc = as<TypeCastIntVal>(fstInt))
532-
fstInt = as<IntVal>(tc->getBase());
533-
if (auto sndInt = as<IntVal>(snd))
534-
{
535-
if (auto tc = as<TypeCastIntVal>(sndInt))
536-
sndInt = as<IntVal>(tc->getBase());
537-
auto fstParam = as<GenericParamIntVal>(fstInt);
538-
auto sndParam = as<GenericParamIntVal>(sndInt);
539-
540-
bool okay = false;
541-
if (fstParam)
542-
{
543-
if(TryUnifyIntParam(constraints, fstParam->getDeclRef(), sndInt))
544-
okay = true;
545-
}
546-
if (sndParam)
547-
{
548-
if(TryUnifyIntParam(constraints, sndParam->getDeclRef(), fstInt))
549-
okay = true;
550-
}
551-
return okay;
552-
}
533+
const auto paramUnderCast = [](IntVal* i){
534+
if(const auto c = as<TypeCastIntVal>(i))
535+
i = as<IntVal>(c->getBase());
536+
return as<GenericParamIntVal>(i);
537+
};
538+
auto fstParam = paramUnderCast(fstInt);
539+
auto sndParam = paramUnderCast(sndInt);
540+
541+
bool okay = false;
542+
if (fstParam)
543+
okay |= TryUnifyIntParam(constraints, fstParam->getDeclRef(), sndInt);
544+
if (sndParam)
545+
okay |= TryUnifyIntParam(constraints, sndParam->getDeclRef(), fstInt);
546+
return okay;
553547
}
554548

555549
if (auto fstWit = as<DeclaredSubtypeWitness>(fst))

tests/bugs/generic-param-cast.slang

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//TEST(smoke,compute):COMPARE_COMPUTE:-cpu -shaderobj
2+
3+
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
4+
RWStructuredBuffer<int> outputBuffer;
5+
6+
struct A<let I : int>
7+
{
8+
int f() { return I; }
9+
};
10+
11+
struct B<let U : uint>
12+
{
13+
A<U> a;
14+
};
15+
16+
int foo<let I : int>(A<I> a)
17+
{
18+
return a.f();
19+
}
20+
21+
int bar<let U : uint>(B<U> b)
22+
{
23+
return foo(b.a);
24+
// We previously were inferring the type at which to call `foo` as `U`
25+
// instead of `int(U)`. This then cause the typechecker to impmediately
26+
// fail because `U` does not unify with the type of `B<U>`'s `a` member,
27+
// namely `int(U)`.
28+
}
29+
30+
[numthreads(4, 1, 1)]
31+
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
32+
{
33+
A<1> a;
34+
B<1> b;
35+
b.a = a;
36+
outputBuffer[dispatchThreadID.x] = bar<1>(b);
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1
2+
1
3+
1
4+
1

0 commit comments

Comments
 (0)