Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove 'raw' default construct insts for SPIRV emit #5556

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the raw default constructor has uses? If we just remove them the rest of the IR will be ill-formed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see the code is detecting store(var, defaultConstruct()) and remove the store along with the defaultConstruct.

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
Loading