Skip to content

Commit

Permalink
Fix issue with raw default constructors in SPIRV emit (#5556)
Browse files Browse the repository at this point in the history
  • Loading branch information
saipraveenb25 authored Nov 14, 2024
1 parent e58ba6b commit 147ceb1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
14 changes: 14 additions & 0 deletions source/slang/slang-emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions source/slang/slang-ir-insts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions source/slang/slang-ir-strip-default-construct.cpp
Original file line number Diff line number Diff line change
@@ -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<IRDefaultConstruct>(
kIROp_DefaultConstruct,
[&](IRDefaultConstruct* defaultConstruct)
{
List<IRInst*> instsToRemove;
for (auto use = defaultConstruct->firstUse; use; use = use->nextUse)
{
if (as<IRStore>(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
11 changes: 11 additions & 0 deletions source/slang/slang-ir-strip-default-construct.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 147ceb1

Please sign in to comment.