From d72f9f6f72a7a74d7466a1e301e1853fea5daa25 Mon Sep 17 00:00:00 2001 From: cheneym2 Date: Mon, 5 Aug 2024 15:37:46 -0400 Subject: Initial support for precompiled DXIL in slang-modules (#4755) * Add embedded precompiled binary IR ops Add IR operations to embed precompiled DXIL or SPIR-V blobs into IR. Adds a BlobLit literal that is mostly identical to StringLit except for its inability to be displayed, e.g. in dumped IR. In the future, the blob might be dumped as hexadecimal, but for now it is summarized as "". * EmbeddedDXIL and SPIR-V options The options, '-embed-dxil' and '-embed-spirv' in slangc, will cause a target dxil or spirv to be compiled and stored in the translation unit IR when written to a slang-module. Subsequent changes actually implement the options. * Per-translation unit DXIL precompilation When -embed-dxil is specified, perform a precompilation to DXIL of each TU, linked only with stdlib. Embed the resulting DXIL for the TU in a IR op. Being part of IR, the precompiled DXIL can be serialized to disk in a slang-module. Upon loading slang-modules, the new IR op will be searched for and the precompiled DXIL blob is saved with the loaded Module. During linking, if all the Modules have precompiled blobs they will be sent to the downstream compile commands as libraries instead of source, skipping the downstream compilation, using DXC only for linking. Fixes Issue #4580 * Remove placeholder embedded SPIRV support Code was added only to sketch out how other precompiled bins will be supported. * Remove the rest of the SPIRV placeholder support * Fix warnings, test error on non-windows * Remove lib_6_6 hack, add dxil_lib capability * Allocate blob value from irmodule memarena * Add null check after memarena allocation * Restore the request->e2erequest code path for generatewholeprogram * Update capability handling, move EmbedDXIL enum to end to preserve abi * Remove lib_6_6 hack * Move ICompileRequest functions to end --- source/slang/slang-ir.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'source/slang/slang-ir.cpp') diff --git a/source/slang/slang-ir.cpp b/source/slang/slang-ir.cpp index 1fc15f185..bfd6c20cf 100644 --- a/source/slang/slang-ir.cpp +++ b/source/slang/slang-ir.cpp @@ -2059,7 +2059,7 @@ namespace Slang UnownedStringSlice IRConstant::getStringSlice() { - assert(getOp() == kIROp_StringLit); + assert(getOp() == kIROp_StringLit || getOp() == kIROp_BlobLit); // If the transitory decoration is set, then this is uses the transitoryStringVal for the text storage. // This is typically used when we are using a transitory IRInst held on the stack (such that it can be looked up in cached), // that just points to a string elsewhere, and NOT the typical normal style, where the string is held after the instruction in memory. @@ -2133,6 +2133,7 @@ namespace Slang { return value.ptrVal == rhs->value.ptrVal; } + case kIROp_BlobLit: case kIROp_StringLit: { return getStringSlice() == rhs->getStringSlice(); @@ -2174,6 +2175,7 @@ namespace Slang { return combineHash(code, Slang::getHashCode(value.ptrVal)); } + case kIROp_BlobLit: case kIROp_StringLit: { const UnownedStringSlice slice = getStringSlice(); @@ -2264,6 +2266,7 @@ namespace Slang irValue->value.ptrVal = keyInst.value.ptrVal; break; } + case kIROp_BlobLit: case kIROp_StringLit: { const UnownedStringSlice slice = keyInst.getStringSlice(); @@ -2387,6 +2390,36 @@ namespace Slang return static_cast(_findOrEmitConstant(keyInst)); } + IRBlobLit* IRBuilder::getBlobValue(ISlangBlob* blob) + { + IRConstant keyInst; + memset(&keyInst, 0, sizeof(keyInst)); + + char* buffer = (char*)(getModule()->getMemoryArena().allocate(blob->getBufferSize())); + if (!buffer) + { + return nullptr; + } + memcpy(buffer, blob->getBufferPointer(), blob->getBufferSize()); + + UnownedStringSlice inSlice(buffer, blob->getBufferSize()); + + // Mark that this is on the stack... + IRDecoration stackDecoration; + memset(&stackDecoration, 0, sizeof(stackDecoration)); + stackDecoration.m_op = kIROp_TransitoryDecoration; + stackDecoration.insertAtEnd(&keyInst); + + keyInst.m_op = kIROp_BlobLit; + keyInst.typeUse.usedValue = nullptr; // not used + + IRConstant::StringSliceValue& dstSlice = keyInst.value.transitoryStringVal; + dstSlice.chars = const_cast(inSlice.begin()); + dstSlice.numChars = uint32_t(inSlice.getLength()); + + return static_cast(_findOrEmitConstant(keyInst)); + } + IRPtrLit* IRBuilder::_getPtrValue(void* data) { auto type = getPtrType(getVoidType()); @@ -3800,6 +3833,13 @@ namespace Slang return nullptr; } + IRInst* IRBuilder::emitEmbeddedDXIL(ISlangBlob *blob) + { + IRInst* args[] = { getBlobValue(blob) }; + + return emitIntrinsicInst(getVoidType(), kIROp_EmbeddedDXIL, 1, args); + } + enum class TypeCastStyle { Unknown = -1, @@ -7098,6 +7138,10 @@ namespace Slang dump(context, irConst->value.intVal ? "true" : "false"); return; + case kIROp_BlobLit: + dump(context, ""); + return; + case kIROp_StringLit: dumpEncodeString(context, irConst->getStringSlice()); return; -- cgit v1.2.3