From ade2c39fa3675504ed135ef8abe7b53cfd06ee84 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Mon, 22 Jul 2019 17:03:10 -0400 Subject: Use C++ template types in code generation (#1000) * WIP: Adding support for C/C++ compilation to slang API. * Removed BackEndType in test harness -> use SlangPassThrough to identify backends Only require stage for targets that require it. Detection of all different backends. * Windows/Unix create temporary filename. * WIP: Output CPU binaries. * Added a pass-through c/c++ test. * Compile C++/C and store in temporary file. * Read the binary back into memory. * Set debug info and optimization flags for C/C++. Make the CPPCompiler debug/optimization levels match slangs. * Handling of include paths and math precision. * Dumping c++/c source and exe/shared library. * Put hex dump into own util. * End to end pass through c compilation test. * WIP: Simple execute test working on Linux/Unix. * Fix typo on linux. * WIP: To compile slang to cpp shared library. Report backend compiler errors. * Compiles slang -> cpp and loads as shared library. * Fix problem on c-cross-compile test because prelude is now included with <> quotes. * Run slang generated cpp code - using hard coded data. * Added cpp-execute-simple, and test output. * Fix warning that broke win32 build. * Fix compilation problem on osx. * When C++ output use template types. * Require C++14 if using partial specialization for matrix/vector types. * Merge of master + add c-cross-compile.slang.expected * Remove c-cross-compile.slang.expected --- source/slang/slang-emit-cpp.cpp | 68 ++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 22 deletions(-) (limited to 'source/slang/slang-emit-cpp.cpp') diff --git a/source/slang/slang-emit-cpp.cpp b/source/slang/slang-emit-cpp.cpp index d039715e3..2ec087eef 100644 --- a/source/slang/slang-emit-cpp.cpp +++ b/source/slang/slang-emit-cpp.cpp @@ -209,6 +209,12 @@ CPPSourceEmitter::IntrinsicOp CPPSourceEmitter::getOperationByName(const Unowned void CPPSourceEmitter::emitTypeDefinition(IRType* inType) { + if (m_target == CodeGenTarget::CPPSource) + { + // All types are templates in C++ + return; + } + IRType* type = _cloneType(inType); if (m_typeEmittedMap.TryGetValue(type)) { @@ -362,17 +368,26 @@ StringSlicePool::Handle CPPSourceEmitter::_calcTypeName(IRType* type) auto vecCount = int(GetIntVal(vecType->getElementCount())); const IROp elemType = vecType->getElementType()->op; - StringBuilder builder; - builder << "Vec"; - UnownedStringSlice postFix = _getCTypeVecPostFix(elemType); - - builder << postFix; - if (postFix.size() > 1) + if (m_target == CodeGenTarget::CPPSource) { - builder << "_"; + StringBuilder builder; + builder << "Vector<" << getBuiltinTypeName(elemType) << ", " << vecCount << ">"; + return m_slicePool.add(builder); + } + else + { + StringBuilder builder; + builder << "Vec"; + UnownedStringSlice postFix = _getCTypeVecPostFix(elemType); + + builder << postFix; + if (postFix.size() > 1) + { + builder << "_"; + } + builder << vecCount; + return m_slicePool.add(builder); } - builder << vecCount; - return m_slicePool.add(builder); } case kIROp_MatrixType: { @@ -382,22 +397,31 @@ StringSlicePool::Handle CPPSourceEmitter::_calcTypeName(IRType* type) const auto rowCount = int(GetIntVal(matType->getRowCount())); const auto colCount = int(GetIntVal(matType->getColumnCount())); - // Make sure there is the vector name too - _getTypeName(_getVecType(elementType, colCount)); - - StringBuilder builder; - - builder << "Mat"; - const UnownedStringSlice postFix = _getCTypeVecPostFix(_getCType(elementType->op)); - builder << postFix; - if (postFix.size() > 1) + if (m_target == CodeGenTarget::CPPSource) { - builder << "_"; + StringBuilder builder; + builder << "Matrix<" << getBuiltinTypeName(elementType->op) << ", " << rowCount << ", " << colCount << ">"; + return m_slicePool.add(builder); } - builder << rowCount; - builder << colCount; + else + { + // Make sure there is the vector name too + _getTypeName(_getVecType(elementType, colCount)); - return m_slicePool.add(builder); + StringBuilder builder; + + builder << "Mat"; + const UnownedStringSlice postFix = _getCTypeVecPostFix(_getCType(elementType->op)); + builder << postFix; + if (postFix.size() > 1) + { + builder << "_"; + } + builder << rowCount; + builder << colCount; + + return m_slicePool.add(builder); + } } case kIROp_HLSLRWStructuredBufferType: { -- cgit v1.2.3