summaryrefslogtreecommitdiff
path: root/source/slang/slang-lower-to-ir.cpp
diff options
context:
space:
mode:
authorkaizhangNV <149626564+kaizhangNV@users.noreply.github.com>2025-05-15 11:59:15 -0500
committerGitHub <noreply@github.com>2025-05-15 09:59:15 -0700
commitb325474c4aba52cca7e0bcd4eae02d23ca4ab9a3 (patch)
treed0790bb56bec865743c95895aedaeb14f63312d7 /source/slang/slang-lower-to-ir.cpp
parented837e205f3e67c4ae112f544cfe486ca3cc8455 (diff)
Implement spec const for generic parameter (#7121)
Close #6840. This PR add supports to use specialize constant in generic parameter, and that parameter can also be used as array size, e.g. following code should work: ``` struct MyStruct<let N: int> { float buffer[N]; } MyStruct<SpecConstVar> s; ``` - Loose the restriction from Link-Time to SpecializationConstant when extract generic argument - Tweak the logic of how we decide whether a inst is hoistable. Besides checking existing hoistable flag of each IRInst, when we detect a IRInst's type is SpecConstRateType, we will treat that inst hoistable. Because IRInst in global scope can be deduplicated, and every SpecConstRateType inst should be in the global scope or IRGeneric scope (which will be at global scope after specialization). - Remove the SpecConstIntVal to IRInst map in IR lowering logic, because we already have way to deduplicate the global scope IR.
Diffstat (limited to 'source/slang/slang-lower-to-ir.cpp')
-rw-r--r--source/slang/slang-lower-to-ir.cpp49
1 files changed, 12 insertions, 37 deletions
diff --git a/source/slang/slang-lower-to-ir.cpp b/source/slang/slang-lower-to-ir.cpp
index af285f221..f8946f5dc 100644
--- a/source/slang/slang-lower-to-ir.cpp
+++ b/source/slang/slang-lower-to-ir.cpp
@@ -1552,17 +1552,6 @@ static bool _isTrivialLookupFromInterfaceThis(IRGenContext* context, DeclRefBase
return context->thisTypeWitness == nullptr;
}
-
-//
-static void maybePropagateRate(IRBuilder* builder, IRType* rateQulifiedType, IRInst* inst)
-{
- if (isSpecConstRateType(rateQulifiedType))
- {
- inst->setFullType(
- builder->getRateQualifiedType(builder->getSpecConstRate(), inst->getFullType()));
- }
-}
-
struct ValLoweringVisitor : ValVisitor<ValLoweringVisitor, LoweredValInfo, LoweredValInfo>
{
IRGenContext* context;
@@ -1596,14 +1585,12 @@ struct ValLoweringVisitor : ValVisitor<ValLoweringVisitor, LoweredValInfo, Lower
specConstRateType = loweredArg.val->getFullType();
}
auto funcType = lowerType(context, val->getFuncType());
- auto resVal = emitCallToDeclRef(
- context,
- as<IRFuncType>(funcType)->getResultType(),
- val->getFuncDeclRef(),
- funcType,
- args,
- tryEnv);
- maybePropagateRate(getBuilder(), specConstRateType, resVal.val);
+ auto funcResType = maybeAddRateType(
+ getBuilder(),
+ specConstRateType,
+ as<IRFuncType>(funcType)->getResultType());
+ auto resVal =
+ emitCallToDeclRef(context, funcResType, val->getFuncDeclRef(), funcType, args, tryEnv);
return resVal;
}
@@ -1613,8 +1600,8 @@ struct ValLoweringVisitor : ValVisitor<ValLoweringVisitor, LoweredValInfo, Lower
SLANG_ASSERT(baseVal.flavor == LoweredValInfo::Flavor::Simple);
auto type = lowerType(context, val->getType());
+ type = maybeAddRateType(getBuilder(), baseVal.val->getFullType(), type);
auto resVal = LoweredValInfo::simple(getBuilder()->emitCast(type, baseVal.val));
- maybePropagateRate(getBuilder(), baseVal.val->getFullType(), resVal.val);
return resVal;
}
@@ -1641,12 +1628,10 @@ struct ValLoweringVisitor : ValVisitor<ValLoweringVisitor, LoweredValInfo, Lower
auto factorVal = lowerVal(context, factor->getParam()).val;
for (IntegerLiteralValue i = 0; i < factor->getPower(); i++)
{
- termVal = irBuilder->emitMul(factorVal->getDataType(), termVal, factorVal);
+ termVal = irBuilder->emitMul(factorVal->getFullType(), termVal, factorVal);
}
- maybePropagateRate(getBuilder(), factorVal->getFullType(), termVal);
}
- resultVal = irBuilder->emitAdd(termVal->getDataType(), resultVal, termVal);
- maybePropagateRate(getBuilder(), termVal->getFullType(), resultVal);
+ resultVal = irBuilder->emitAdd(termVal->getFullType(), resultVal, termVal);
}
return LoweredValInfo::simple(resultVal);
}
@@ -2076,19 +2061,9 @@ struct ValLoweringVisitor : ValVisitor<ValLoweringVisitor, LoweredValInfo, Lower
auto elementType = lowerType(context, type->getElementType());
if (!type->isUnsized())
{
- IRInst* elementCount = nullptr;
- auto sizeVal = type->getElementCount();
- auto sharedContext = context->shared;
- if (!sharedContext->mapSpecConstValToIRInst.tryGetValue(sizeVal, elementCount))
- {
- elementCount = lowerSimpleVal(context, sizeVal);
- if (isSpecConstRateType(elementCount->getFullType()))
- {
- sharedContext->mapSpecConstValToIRInst.add(sizeVal, elementCount);
- hoistInstAndOperandsToGlobal(getBuilder(), elementCount);
- }
- }
- return getBuilder()->getArrayType(elementType, elementCount);
+ return getBuilder()->getArrayType(
+ elementType,
+ lowerSimpleVal(context, type->getElementCount()));
}
else
{