From 147ceb1991454b7a5ba6f3ec0c149dd40360a31d Mon Sep 17 00:00:00 2001 From: Sai Praveen Bangaru <31557731+saipraveenb25@users.noreply.github.com> Date: Thu, 14 Nov 2024 12:06:17 -0500 Subject: [PATCH] Fix issue with raw default constructors in SPIRV emit (#5556) --- source/slang/slang-emit.cpp | 14 ++++++ source/slang/slang-ir-insts.h | 7 +++ .../slang-ir-strip-default-construct.cpp | 45 +++++++++++++++++++ .../slang/slang-ir-strip-default-construct.h | 11 +++++ 4 files changed, 77 insertions(+) create mode 100644 source/slang/slang-ir-strip-default-construct.cpp create mode 100644 source/slang/slang-ir-strip-default-construct.h diff --git a/source/slang/slang-emit.cpp b/source/slang/slang-emit.cpp index 05bb12ecc7..010b4bc921 100644 --- a/source/slang/slang-emit.cpp +++ b/source/slang/slang-emit.cpp @@ -89,6 +89,7 @@ #include "slang-ir-ssa.h" #include "slang-ir-string-hash.h" #include "slang-ir-strip-cached-dict.h" +#include "slang-ir-strip-default-construct.h" #include "slang-ir-strip-witness-tables.h" #include "slang-ir-strip.h" #include "slang-ir-synthesize-active-mask.h" @@ -1419,6 +1420,19 @@ Result linkAndOptimizeIR( // we will need to disable this pass. stripWitnessTables(irModule); + switch (target) + { + // On targets that don't support default initialization, remove 'raw' default construct + // insts because our code-gen will not have any way to emit them. + // + case CodeGenTarget::SPIRV: + if (targetProgram->shouldEmitSPIRVDirectly()) + removeRawDefaultConstructors(irModule); + break; + default: + break; + } + #if 0 dumpIRIfEnabled(codeGenContext, irModule, "AFTER STRIP WITNESS TABLES"); #endif diff --git a/source/slang/slang-ir-insts.h b/source/slang/slang-ir-insts.h index 4baa786e3a..9ccfce51cd 100644 --- a/source/slang/slang-ir-insts.h +++ b/source/slang/slang-ir-insts.h @@ -2894,6 +2894,13 @@ struct IRUndefined : IRInst { }; +// Special inst for targets that support default initialization, +// like the braces '= {}' in C/HLSL +struct IRDefaultConstruct : IRInst +{ + IR_LEAF_ISA(DefaultConstruct) +}; + // A global-scope generic parameter (a type parameter, a // constraint parameter, etc.) struct IRGlobalGenericParam : IRInst diff --git a/source/slang/slang-ir-strip-default-construct.cpp b/source/slang/slang-ir-strip-default-construct.cpp new file mode 100644 index 0000000000..ea141d2fd0 --- /dev/null +++ b/source/slang/slang-ir-strip-default-construct.cpp @@ -0,0 +1,45 @@ +// slang-ir-strip-default-construct.cpp +#include "slang-ir-strip-default-construct.h" + +#include "slang-ir-inst-pass-base.h" +#include "slang-ir-insts.h" +#include "slang-ir.h" + +namespace Slang +{ + +struct RemoveDefaultConstructInsts : InstPassBase +{ + RemoveDefaultConstructInsts(IRModule* module) + : InstPassBase(module) + { + } + void processModule() + { + processInstsOfType( + kIROp_DefaultConstruct, + [&](IRDefaultConstruct* defaultConstruct) + { + List instsToRemove; + for (auto use = defaultConstruct->firstUse; use; use = use->nextUse) + { + if (as(use->getUser())) + instsToRemove.add(use->getUser()); + else + return; // Ignore this inst if there are non-store uses. + } + + for (auto inst : instsToRemove) + inst->removeAndDeallocate(); + + defaultConstruct->removeAndDeallocate(); + }); + } +}; + +void removeRawDefaultConstructors(IRModule* module) +{ + RemoveDefaultConstructInsts(module).processModule(); +} + +} // namespace Slang diff --git a/source/slang/slang-ir-strip-default-construct.h b/source/slang/slang-ir-strip-default-construct.h new file mode 100644 index 0000000000..ea3984d8bb --- /dev/null +++ b/source/slang/slang-ir-strip-default-construct.h @@ -0,0 +1,11 @@ +// slang-ir-strip-default-construct.h +#pragma once + +namespace Slang +{ +struct IRModule; + +/// Strip the contents of all witness table instructions from the given IR `module` +void removeRawDefaultConstructors(IRModule* module); + +} // namespace Slang