From b325474c4aba52cca7e0bcd4eae02d23ca4ab9a3 Mon Sep 17 00:00:00 2001 From: kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> Date: Thu, 15 May 2025 11:59:15 -0500 Subject: 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 { float buffer[N]; } MyStruct 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. --- source/slang/slang-lower-to-ir.cpp | 49 ++++++++++---------------------------- 1 file changed, 12 insertions(+), 37 deletions(-) (limited to 'source/slang/slang-lower-to-ir.cpp') 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 { IRGenContext* context; @@ -1596,14 +1585,12 @@ struct ValLoweringVisitor : ValVisitorgetFullType(); } auto funcType = lowerType(context, val->getFuncType()); - auto resVal = emitCallToDeclRef( - context, - as(funcType)->getResultType(), - val->getFuncDeclRef(), - funcType, - args, - tryEnv); - maybePropagateRate(getBuilder(), specConstRateType, resVal.val); + auto funcResType = maybeAddRateType( + getBuilder(), + specConstRateType, + as(funcType)->getResultType()); + auto resVal = + emitCallToDeclRef(context, funcResType, val->getFuncDeclRef(), funcType, args, tryEnv); return resVal; } @@ -1613,8 +1600,8 @@ struct ValLoweringVisitor : ValVisitorgetType()); + 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 : ValVisitorgetParam()).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 : ValVisitorgetElementType()); 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 { -- cgit v1.2.3