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-compiler.h | 3 ++ source/slang/slang-emit-cpp.cpp | 65 ++++++++++++++++++++++++++++++++++++++ source/slang/slang-lower-to-ir.cpp | 6 ++++ source/slang/slang-options.cpp | 4 +++ 4 files changed, 78 insertions(+) (limited to 'source') diff --git a/source/slang/slang-compiler.h b/source/slang/slang-compiler.h index 146cf9ed0..19f56031d 100755 --- a/source/slang/slang-compiler.h +++ b/source/slang/slang-compiler.h @@ -1265,6 +1265,9 @@ namespace Slang bool m_obfuscateCode = false; + // Determine whether to output heterogeneity-related code + bool m_heterogeneous = false; + // Name pool for looking up names NamePool namePool; 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. diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp index b115b8222..b931212fd 100644 --- a/source/slang/slang-lower-to-ir.cpp +++ b/source/slang/slang-lower-to-ir.cpp @@ -1416,6 +1416,12 @@ struct ValLoweringVisitor : ValVisitorgetValueType()); + return getBuilder()->getPtrType(valueType); + } + IRType* visitDeclRefType(DeclRefType* type) { auto declRef = type->declRef; diff --git a/source/slang/slang-options.cpp b/source/slang/slang-options.cpp index 6b4d5adf2..5da87bde6 100644 --- a/source/slang/slang-options.cpp +++ b/source/slang/slang-options.cpp @@ -664,6 +664,10 @@ struct OptionsParser rawEntryPoints.add(rawEntryPoint); } + else if (argStr == "-heterogeneous") + { + requestImpl->getLinkage()->m_heterogeneous = true; + } else if (argStr == "-lang") { String name; -- cgit v1.2.3