Skip to content

Commit 2e3688a

Browse files
authored
Dynamic dispatch bug fixes. (shader-slang#1541)
Co-authored-by: Yong He <yhe@nvidia.com>
1 parent 5461671 commit 2e3688a

4 files changed

+19
-8
lines changed

source/slang/slang-emit-cpp.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1724,6 +1724,10 @@ void CPPSourceEmitter::emitRTTIObject(IRRTTIObject* rttiObject)
17241724
void CPPSourceEmitter::_maybeEmitWitnessTableTypeDefinition(
17251725
IRInterfaceType* interfaceType)
17261726
{
1727+
if (m_interfaceTypesEmitted.Contains(interfaceType))
1728+
return;
1729+
m_interfaceTypesEmitted.Add(interfaceType);
1730+
17271731
m_writer->emit("struct ");
17281732
emitSimpleType(interfaceType);
17291733
m_writer->emit("\n{\n");

source/slang/slang-emit-cpp.h

+2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ class CPPSourceEmitter: public CLikeSourceEmitter
137137

138138
HashSet<const HLSLIntrinsic*> m_intrinsicEmitted;
139139

140+
HashSet<IRInterfaceType*> m_interfaceTypesEmitted;
141+
140142
StringSlicePool m_slicePool;
141143

142144
SemanticUsedFlags m_semanticUsedFlags;

source/slang/slang-ir-any-value-marshalling.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,14 @@ namespace Slang
126126
auto vectorType = static_cast<IRVectorType*>(dataType);
127127
auto elementType = vectorType->getElementType();
128128
auto elementCount = getIntVal(vectorType->getElementCount());
129+
auto elementPtrType = builder->getPtrType(elementType);
129130
for (IRIntegerValue i = 0; i < elementCount; i++)
130131
{
131-
auto elementVal = builder->emitElementExtract(
132-
elementType,
132+
auto elementAddr = builder->emitElementAddress(
133+
elementPtrType,
133134
concreteTypedVar,
134135
builder->getIntValue(builder->getIntType(), i));
135-
emitMarshallingCode(builder, context, elementVal);
136+
emitMarshallingCode(builder, context, elementAddr);
136137
}
137138
break;
138139
}
@@ -469,6 +470,10 @@ namespace Slang
469470
if (auto anyValueType = as<IRAnyValueType>(inst))
470471
processAnyValueType(anyValueType);
471472
}
473+
// Because we replaced all `AnyValueType` uses, some old type definitions (e.g. PtrType(AnyValueType))
474+
// will become duplicates with new types we introduced (e.g. PtrType(AnyValueStruct)), and therefore
475+
// invalidates our `globalValueNumberingMap` hash map. We need to rebuild it.
476+
sharedContext->sharedBuilderStorage.deduplicateAndRebuildGlobalNumberingMap();
472477
}
473478
};
474479

source/slang/slang-ir-deduplicate.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace Slang
1717
IRInst* addConstantValue(IRConstant* value)
1818
{
1919
IRConstantKey key = { value };
20+
value->setFullType((IRType*)addValue(value->getFullType()));
2021
if (auto newValue = builder->constantMap.TryGetValue(key))
2122
return *newValue;
2223
builder->constantMap[key] = value;
@@ -34,15 +35,14 @@ namespace Slang
3435
break;
3536
}
3637

37-
IRInstKey key = { value };
38-
if (auto newValue = builder->globalValueNumberingMap.TryGetValue(key))
39-
return *newValue;
40-
4138
for (UInt i = 0; i < value->getOperandCount(); i++)
4239
{
4340
value->setOperand(i, addValue(value->getOperand(i)));
4441
}
4542
value->setFullType((IRType*)addValue(value->getFullType()));
43+
IRInstKey key = { value };
44+
if (auto newValue = builder->globalValueNumberingMap.TryGetValue(key))
45+
return *newValue;
4646
builder->globalValueNumberingMap[key] = value;
4747
return value;
4848
}
@@ -53,14 +53,14 @@ namespace Slang
5353
context.builder = this;
5454
bool changed = true;
5555
constantMap.Clear();
56+
globalValueNumberingMap.Clear();
5657
for (auto inst : module->getGlobalInsts())
5758
{
5859
if (auto constVal = as<IRConstant>(inst))
5960
{
6061
context.addConstantValue(constVal);
6162
}
6263
}
63-
globalValueNumberingMap.Clear();
6464
List<IRInst*> instToRemove;
6565
for (auto inst : module->getGlobalInsts())
6666
{

0 commit comments

Comments
 (0)