Skip to content

Commit 49b0816

Browse files
committed
Specialize generic retrieved from witness table
1 parent 061665a commit 49b0816

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

source/slang/slang-ir-specialize.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,28 @@ struct SpecializationContext
880880
IRInst* satisfyingVal = nullptr;
881881

882882
if (witnessTable)
883-
satisfyingVal = findWitnessVal(witnessTable, requirementKey);
883+
{
884+
satisfyingVal = findWitnessTableEntry(witnessTable, requirementKey);
885+
if (const auto genericEntry = as<IRGeneric>(satisfyingVal))
886+
{
887+
IRBuilder builder(module);
888+
// Collect generic params.
889+
List<IRInst*> genericParams;
890+
for (auto param : genericEntry->getParams())
891+
genericParams.add(param);
892+
893+
auto specializedFuncType = builder.emitSpecializeInst(
894+
builder.getTypeKind(),
895+
genericEntry->getFullType(),
896+
(UInt)genericParams.getCount(),
897+
genericParams.getBuffer());
898+
satisfyingVal = builder.emitSpecializeInst(
899+
(IRType*)specializedFuncType,
900+
genericEntry,
901+
(UInt)genericParams.getCount(),
902+
genericParams.getBuffer());
903+
}
904+
}
884905
else
885906
{
886907
// If we are specializing ThisTypeWitness, the result of the specialization
@@ -935,27 +956,6 @@ struct SpecializationContext
935956
return instChanged;
936957
}
937958

938-
// The above subroutine needed a way to look up
939-
// the satisfying value for a given requirement
940-
// key in a concrete witness table, so let's
941-
// define that now.
942-
//
943-
IRInst* findWitnessVal(IRWitnessTable* witnessTable, IRInst* requirementKey)
944-
{
945-
// A witness table is basically just a container
946-
// for key-value pairs, and so the best we can
947-
// do for now is a naive linear search.
948-
//
949-
for (auto entry : witnessTable->getEntries())
950-
{
951-
if (requirementKey == entry->getRequirementKey())
952-
{
953-
return entry->getSatisfyingVal();
954-
}
955-
}
956-
return nullptr;
957-
}
958-
959959
template<typename TDict>
960960
void _readSpecializationDictionaryImpl(TDict& dict, IRInst* dictInst)
961961
{

tests/language-feature/enums/nested-enum.slang

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ int testGeneric(int val)
7979
{
8080
OuterGeneric<int> outer;
8181
outer.value = val;
82-
outer.channel = OuterGeneric<int>.Channel(val);
82+
outer.channel = OuterGeneric<int>.Channel(val % 4);
8383
int result = 0;
8484
if (outer.channel == OuterGeneric<int>.Channel.Red) result += 1;
85-
if (outer.channel == OuterGeneric<int>.Channel.Green) result += 16;
86-
if (outer.channel == OuterGeneric<int>.Channel.Blue) result += 16*16;
87-
if (outer.channel == OuterGeneric<int>.Channel.Alpha) result += 16*16*16;
85+
if (outer.channel == OuterGeneric<int>.Channel.Green) result += 2;
86+
if (outer.channel == OuterGeneric<int>.Channel.Blue) result += 3;
87+
if (outer.channel == OuterGeneric<int>.Channel.Alpha) result += 4;
8888

8989
return result;
9090
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
1072
2-
1076
3-
1426
4-
4166
2+
1062
3+
1173
4+
74

0 commit comments

Comments
 (0)