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

Fix issue with specialization using arithmetic expressions #6084

Merged
merged 2 commits into from
Jan 14, 2025
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
45 changes: 45 additions & 0 deletions source/slang/slang-ir-specialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,42 @@ struct SpecializationContext
module->getContainerPool().free(&cleanInsts);
}

bool isUnsimplifiedArithmeticInst(IRInst* inst)
{
switch (inst->getOp())
{
case kIROp_Add:
case kIROp_Sub:
case kIROp_Mul:
case kIROp_Div:
case kIROp_Neg:
case kIROp_Not:
case kIROp_Eql:
case kIROp_Neq:
case kIROp_Leq:
case kIROp_Geq:
case kIROp_Less:
case kIROp_IRem:
case kIROp_FRem:
case kIROp_Greater:
case kIROp_Lsh:
case kIROp_Rsh:
case kIROp_BitAnd:
case kIROp_BitOr:
case kIROp_BitXor:
case kIROp_BitNot:
case kIROp_BitCast:
case kIROp_CastIntToFloat:
case kIROp_CastFloatToInt:
case kIROp_IntCast:
case kIROp_FloatCast:
case kIROp_Select:
return true;
default:
return false;
}
}

// An instruction is then fully specialized if and only
// if it is in our set.
//
Expand Down Expand Up @@ -133,6 +169,14 @@ struct SpecializationContext
return areAllOperandsFullySpecialized(inst);
}

if (isUnsimplifiedArithmeticInst(inst))
{
// For arithmetic insts, we want to wait for simplification before specialization,
// since different insts can simplify to the same value.
//
return false;
}

// The default case is that a global value is always specialized.
if (inst->getParent() == module->getModuleInst())
{
Expand Down Expand Up @@ -1092,6 +1136,7 @@ struct SpecializationContext
{
this->changed = true;
eliminateDeadCode(module->getModuleInst());
applySparseConditionalConstantPropagationForGlobalScope(this->module, this->sink);
}

// Once the work list has gone dry, we should have the invariant
Expand Down
3 changes: 2 additions & 1 deletion source/slang/slang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,8 @@ DeclRef<Decl> Linkage::specializeWithArgTypes(
DiagnosticSink* sink)
{
SemanticsVisitor visitor(getSemanticsForReflection());
visitor = visitor.withSink(sink);
SemanticsVisitor::ExprLocalScope scope;
visitor = visitor.withSink(sink).withExprLocalScope(&scope);

SLANG_AST_BUILDER_RAII(getASTBuilder());

Expand Down
Loading