From 011a743668e7cd0b7cf97d27e3bed7d519794aeb Mon Sep 17 00:00:00 2001 From: Dietrich Geisler Date: Fri, 31 Jul 2020 17:51:52 -0400 Subject: Binary for Heterogeneous Example (#1467) * Binary Heterogeneous Example This PR introduces the ability to insert the binary of a non-CPU target by using the -heterogeneous flag. Specifically, this PR updates the emitting logic to produce a variable of name `__[name_of_entryPoint]` when the heterogeneous flag is present. * Prelude path fix Co-authored-by: Tim Foley --- source/slang/slang-emit-cpp.cpp | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (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 c949075fb..14033866b 100644 --- a/source/slang/slang-emit-cpp.cpp +++ b/source/slang/slang-emit-cpp.cpp @@ -2529,6 +2529,71 @@ void CPPSourceEmitter::_emitForwardDeclarations(const List& actions) void CPPSourceEmitter::emitModuleImpl(IRModule* module) { + // If we are emitting a heterogeneous program + // Emit the binary blob of each non-CPP target + ComponentType* program = m_compileRequest->getProgram(); + auto linkage = m_compileRequest->getLinkage(); + if (linkage->m_heterogeneous) + { + for (auto inst : module->getGlobalInsts()) + { + auto func = as(inst); + if (!func) + continue; + if (auto entryPointDecoration = func->findDecoration()) + { + String someName = entryPointDecoration->getName()->getStringSlice(); + for (int index = 0; index < program->getEntryPointCount(); index++) + { + auto entryPoint = program->getEntryPoint(index); + if (someName == entryPoint->getName()->text) + { + for (auto targetRequest : linkage->targets) + { + // Emit for all non-CPU targets + switch (targetRequest->getTarget()) + { + case(CodeGenTarget::CPPSource): + case(CodeGenTarget::CSource): + case(CodeGenTarget::HostCallable): + case(CodeGenTarget::CUDASource): + + break; + + default: + + auto targetProgram = program->getTargetProgram(targetRequest); + DiagnosticSink sink(linkage->getSourceManager()); + CompileResult result = + targetProgram->getOrCreateEntryPointResult(index, &sink); + + Slang::ComPtr blob; + result.getBlob(blob); + auto ptr = (const unsigned char*)blob->getBufferPointer(); + + m_writer->emit("size_t __"); + m_writer->emit(someName); + m_writer->emit("Size = "); + m_writer->emitInt64(blob->getBufferSize()); + m_writer->emit(";\n"); + + m_writer->emit("unsigned char __"); + m_writer->emit(someName); + m_writer->emit("[] = {"); + for (unsigned int i = 0; i < blob->getBufferSize() - 1; i++) { + m_writer->emitUInt64(ptr[i]); + m_writer->emit(", "); + } + m_writer->emitUInt64(ptr[blob->getBufferSize() - 1]); + m_writer->emit("};\n"); + } + } + } + } + } + } + } + // 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. -- cgit v1.2.3