summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2020-01-06 15:36:11 -0500
committerGitHub <noreply@github.com>2020-01-06 15:36:11 -0500
commit0c87001d7fb9dabaa17f9784e99d7438592d2373 (patch)
tree0cd893463a0734928a45e63850c1fabb8e4d4129 /source
parent79b52bb8ac2a6059f5bbdc17be22725400b74aad (diff)
Fix scoping issue around use of IRTypeSet (#1160)
* WIP use IRTypeSet in CPPSourceEmitter - doesn't work because of a cloning issue, causing a crash on exit. * Fix destruction of module issue for IRTypeSet usage in CPPEmitter. * Fix out definition emitting ordering that was removed. * Disable cuda output test.
Diffstat (limited to 'source')
-rw-r--r--source/slang/slang-emit-cpp.cpp81
-rw-r--r--source/slang/slang-emit-cpp.h7
-rw-r--r--source/slang/slang-emit-cuda.cpp18
-rw-r--r--source/slang/slang-emit-cuda.h2
-rw-r--r--source/slang/slang-emit.cpp6
-rw-r--r--source/slang/slang-ir-type-set.cpp63
-rw-r--r--source/slang/slang-ir-type-set.h8
7 files changed, 150 insertions, 35 deletions
diff --git a/source/slang/slang-emit-cpp.cpp b/source/slang/slang-emit-cpp.cpp
index 0d5d1d293..3f7f3eeb3 100644
--- a/source/slang/slang-emit-cpp.cpp
+++ b/source/slang/slang-emit-cpp.cpp
@@ -204,11 +204,6 @@ void CPPSourceEmitter::emitTypeDefinition(IRType* inType)
}
IRType* type = m_typeSet.getType(inType);
- if (m_typeEmittedMap.TryGetValue(type))
- {
- return;
- }
-
if (!m_typeSet.isOwned(type))
{
// If defined in a different module, we assume they are emitted already. (Assumed to
@@ -249,8 +244,6 @@ void CPPSourceEmitter::emitTypeDefinition(IRType* inType)
writer->dedent();
writer->emit("};\n\n");
-
- m_typeEmittedMap.Add(type, true);
break;
}
case kIROp_MatrixType:
@@ -261,8 +254,7 @@ void CPPSourceEmitter::emitTypeDefinition(IRType* inType)
const auto colCount = int(GetIntVal(matType->getColumnCount()));
IRType* vecType = m_typeSet.addVectorType(matType->getElementType(), colCount);
- emitTypeDefinition(vecType);
-
+
UnownedStringSlice typeName = _getTypeName(type);
UnownedStringSlice rowTypeName = _getTypeName(vecType);
@@ -278,8 +270,6 @@ void CPPSourceEmitter::emitTypeDefinition(IRType* inType)
writer->dedent();
writer->emit("};\n\n");
-
- m_typeEmittedMap.Add(type, true);
break;
}
case kIROp_PtrType:
@@ -319,18 +309,6 @@ UnownedStringSlice CPPSourceEmitter::_getTypeName(IRType* inType)
return m_slicePool.getSlice(handle);
}
- if (type->op == kIROp_MatrixType)
- {
- auto matType = static_cast<IRMatrixType*>(type);
-
- auto elementType = matType->getElementType();
- const auto rowCount = int(GetIntVal(matType->getRowCount()));
- const auto colCount = int(GetIntVal(matType->getColumnCount()));
-
- // Make sure the vector type the matrix is built on is added
- useType(m_typeSet.addVectorType(elementType, colCount));
- }
-
StringBuilder builder;
if (SLANG_SUCCEEDED(_calcTypeName(type, m_target, builder)))
{
@@ -1211,7 +1189,7 @@ void CPPSourceEmitter::emitSpecializedOperationDefinition(const HLSLIntrinsic* s
typedef HLSLIntrinsic::Op Op;
// Check if it's been emitted already, if not add it.
- if (!m_intrinsicEmittedMap.AddIfNotExists(specOp, true))
+ if (!m_intrinsicEmitted.Add(specOp))
{
return;
}
@@ -1948,20 +1926,62 @@ bool CPPSourceEmitter::tryEmitInstExprImpl(IRInst* inst, const EmitOpInfo& inOut
}
}
+// We want order of built in types (typically output nothing), vector, matrix, other types
+// Types that aren't output have negative indices
+static Index _calcTypeOrder(IRType* a)
+{
+ switch (a->op)
+ {
+ case kIROp_FuncType:
+ {
+ return -2;
+ }
+ case kIROp_VectorType: return 1;
+ case kIROp_MatrixType: return 2;
+ default:
+ {
+ if (as<IRBasicType>(a))
+ {
+ return -1;
+ }
+ return 3;
+ }
+ }
+}
+
void CPPSourceEmitter::emitPreprocessorDirectivesImpl()
{
SourceWriter* writer = getSourceWriter();
writer->emit("\n");
- // Emit the type definitions
- for (const auto& keyValue : m_typeNameMap)
+ if (m_target == CodeGenTarget::CSource)
{
- emitTypeDefinition(keyValue.Key);
+ // For C output we need to emit type definitions.
+ List<IRType*> types;
+ m_typeSet.getTypes(types);
+
+ // Remove ones we don't need to emit
+ for (Index i = 0; i < types.getCount(); ++i)
+ {
+ if (_calcTypeOrder(types[i]) < 0)
+ {
+ types.fastRemoveAt(i);
+ --i;
+ }
+ }
+
+ // Sort them so that vectors come before matrices and everything else after that
+ types.sort([&](IRType* a, IRType* b) { return _calcTypeOrder(a) < _calcTypeOrder(b); });
+
+ // Emit the type definitions
+ for (auto type : types)
+ {
+ emitTypeDefinition(type);
+ }
}
// Emit all the intrinsics that were used
-
for (const auto& keyValue : m_intrinsicNameMap)
{
emitSpecializedOperationDefinition(keyValue.Key);
@@ -2297,6 +2317,11 @@ void CPPSourceEmitter::_emitInitAxisValues(const Int sizeAlongAxis[kThreadGroupA
void CPPSourceEmitter::emitModuleImpl(IRModule* module)
{
+ // Setup all built in types used in the module
+ m_typeSet.addAllBuiltinTypes(module);
+ // If any matrix types are used, then we need appropriate vector types too.
+ m_typeSet.addVectorForMatrixTypes();
+
List<EmitAction> actions;
computeEmitActions(module, actions);
diff --git a/source/slang/slang-emit-cpp.h b/source/slang/slang-emit-cpp.h
index 45a9ee35a..784696b08 100644
--- a/source/slang/slang-emit-cpp.h
+++ b/source/slang/slang-emit-cpp.h
@@ -101,7 +101,7 @@ protected:
StringSlicePool::Handle _calcFuncName(const HLSLIntrinsic* specOp);
UnownedStringSlice _getTypeName(IRType* type);
- StringSlicePool::Handle _calcTypeName(IRType* type);
+ //StringSlicePool::Handle _calcTypeName(IRType* type);
SlangResult _calcTypeName(IRType* type, CodeGenTarget target, StringBuilder& out);
@@ -122,9 +122,8 @@ protected:
IRTypeSet m_typeSet;
RefPtr<HLSLIntrinsicOpLookup> m_opLookup;
HLSLIntrinsicSet m_intrinsicSet;
-
- Dictionary<IRType*, bool> m_typeEmittedMap;
- Dictionary<const HLSLIntrinsic*, bool> m_intrinsicEmittedMap;
+
+ HashSet<const HLSLIntrinsic*> m_intrinsicEmitted;
StringSlicePool m_slicePool;
diff --git a/source/slang/slang-emit-cuda.cpp b/source/slang/slang-emit-cuda.cpp
index feafc4e4e..37d5b1946 100644
--- a/source/slang/slang-emit-cuda.cpp
+++ b/source/slang/slang-emit-cuda.cpp
@@ -227,6 +227,24 @@ SlangResult CUDASourceEmitter::_calcCUDATypeName(IRType* type, StringBuilder& ou
}
}
+#if 0
+ switch (type->op)
+ {
+ case kIROp_HLSLStructuredBufferType:
+ case kIROp_HLSLRWStructuredBufferType:
+ {
+ auto structuredBufferType = as<IRHLSLStructuredBufferType>(type);
+ auto elementType = structuredBufferType->getElementType();
+
+ // Is the same as a pointer to the item
+
+
+
+ }
+ default: break;
+ }
+#endif
+
// If _getResourceTypePrefix returns something, we assume can output any specialization after it in order.
{
UnownedStringSlice prefix = _getCUDAResourceTypePrefix(type->op);
diff --git a/source/slang/slang-emit-cuda.h b/source/slang/slang-emit-cuda.h
index 6de2f5ea3..5d5a624f1 100644
--- a/source/slang/slang-emit-cuda.h
+++ b/source/slang/slang-emit-cuda.h
@@ -70,6 +70,8 @@ protected:
UnownedStringSlice _getCUDATypeName(IRType* inType);
SlangResult _calcCUDATextureTypeName(IRTextureTypeBase* texType, StringBuilder& outName);
+
+
Dictionary<IRType*, StringSlicePool::Handle> m_typeNameMap;
StringSlicePool m_slicePool;
diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp
index 19c3cad68..66e5714c3 100644
--- a/source/slang/slang-emit.cpp
+++ b/source/slang/slang-emit.cpp
@@ -466,6 +466,10 @@ String emitEntryPointSourceFromIR(
desc.effectiveProfile = getEffectiveProfile(entryPoint, targetRequest);
desc.sourceWriter = &sourceWriter;
+ // Define here, because must be in scope longer than the sourceEmitter, as sourceEmitter might reference
+ // items in the linkedIR module
+ LinkedIR linkedIR;
+
RefPtr<CLikeSourceEmitter> sourceEmitter;
typedef CLikeSourceEmitter::SourceStyle SourceStyle;
@@ -502,8 +506,6 @@ String emitEntryPointSourceFromIR(
return String();
}
- // Outside because we want to keep IR in scope whilst we are processing emits
- LinkedIR linkedIR;
{
LinkingAndOptimizationOptions linkingAndOptimizationOptions;
diff --git a/source/slang/slang-ir-type-set.cpp b/source/slang/slang-ir-type-set.cpp
index 7beeaced4..a4ebf8242 100644
--- a/source/slang/slang-ir-type-set.cpp
+++ b/source/slang/slang-ir-type-set.cpp
@@ -23,6 +23,22 @@ IRTypeSet::IRTypeSet(Session* session)
IRTypeSet::~IRTypeSet()
{
+ _clearTypes();
+}
+
+void IRTypeSet::clear()
+{
+ _clearTypes();
+
+ m_cloneMap.Clear();
+
+ m_module = m_builder.createModule();
+ m_sharedBuilder.module = m_module;
+ m_builder.setInsertInto(m_module->getModuleInst());
+}
+
+void IRTypeSet::_clearTypes()
+{
List<IRType*> types;
getTypes(types);
@@ -226,4 +242,51 @@ void IRTypeSet::addVectorForMatrixTypes()
}
}
+static bool _hasNominalOperand(IRInst* inst)
+{
+ const Index operandCount = Index(inst->getOperandCount());
+ auto operands = inst->getOperands();
+
+ for (Index i = 0; i < operandCount; ++i)
+ {
+ IRInst* operand = operands[i].get();
+ if (isNominalOp(operand->op))
+ {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+void IRTypeSet::_addAllBuiltinTypesRec(IRInst* inst)
+{
+ for (IRInst* child = inst->getFirstDecorationOrChild(); child; child = child->getNextInst())
+ {
+ IRType* type = nullptr;
+
+ if (auto vectorType = as<IRVectorType>(child))
+ {
+ type = vectorType;
+ }
+ else if (auto matrixType = as<IRMatrixType>(child))
+ {
+ type = matrixType;
+ }
+ if (type && !_hasNominalOperand(type))
+ {
+ add(type);
+ }
+ else
+ {
+ _addAllBuiltinTypesRec(child);
+ }
+ }
+}
+
+void IRTypeSet::addAllBuiltinTypes(IRModule* module)
+{
+ _addAllBuiltinTypesRec(module->getModuleInst());
+}
+
}
diff --git a/source/slang/slang-ir-type-set.h b/source/slang/slang-ir-type-set.h
index 016513258..09abdf2ad 100644
--- a/source/slang/slang-ir-type-set.h
+++ b/source/slang/slang-ir-type-set.h
@@ -50,6 +50,8 @@ public:
IRType* add(IRType* type);
IRType* addVectorType(IRType* elementType, int colsCount);
+ void addAllBuiltinTypes(IRModule* module);
+
void addVectorForMatrixTypes();
void getTypes(List<IRType*>& outTypes) const;
@@ -66,11 +68,15 @@ public:
IRBuilder& getBuilder() { return m_builder; }
IRModule* getModule() const { return m_module; }
+ void clear();
+
IRTypeSet(Session* session);
~IRTypeSet();
protected:
-
+ void _addAllBuiltinTypesRec(IRInst* inst);
+ void _clearTypes();
+
// Maps insts from source modules into m_module.
// NOTE! That nominal types are not cloned, as they are identified by pointer. They are just
Dictionary<IRInst*, IRInst*> m_cloneMap;