From 83f176ba8a3bae5533470aed6a90663653f894b8 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 29 May 2024 11:14:22 -0700 Subject: Add options to speedup compilation. (#4240) * Add options to speedup compilation. * Fix. * Plumb options to DCE pass. * Revert debug change. * Fix regressions. * More optimizations. * more cleanup and fixes. * remove comment. * Fixes. * Another fix. * Fix errors. * Fix errors. * Add comments. --- source/slang/slang-ir-specialize.cpp | 69 ++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 31 deletions(-) (limited to 'source/slang/slang-ir-specialize.cpp') diff --git a/source/slang/slang-ir-specialize.cpp b/source/slang/slang-ir-specialize.cpp index 6137a7158..8fac31a2b 100644 --- a/source/slang/slang-ir-specialize.cpp +++ b/source/slang/slang-ir-specialize.cpp @@ -56,11 +56,17 @@ struct SpecializationContext SpecializationContext(IRModule* inModule, TargetProgram* target) : workList(*inModule->getContainerPool().getList()) , workListSet(*inModule->getContainerPool().getHashSet()) - , cleanInsts(*module->getContainerPool().getHashSet()) + , cleanInsts(*inModule->getContainerPool().getHashSet()) , module(inModule) , targetProgram(target) { } + ~SpecializationContext() + { + module->getContainerPool().free(&workList); + module->getContainerPool().free(&workListSet); + module->getContainerPool().free(&cleanInsts); + } // An instruction is then fully specialized if and only // if it is in our set. @@ -442,7 +448,7 @@ struct SpecializationContext // that our args are fully specialized/concrete. // UCount argCount = specInst->getArgCount(); - List args; + ShortList args; for (UIndex ii = 0; ii < argCount; ii++) args.add(specInst->getArg(ii)); @@ -454,7 +460,7 @@ struct SpecializationContext builder.getTypeKind(), specDiffFunc->getBase()->getDataType(), argCount, - args.getBuffer()); + args.getArrayView().getBuffer()); // Specialize the custom derivative function with the original arguments. builder.setInsertBefore(specInst); @@ -462,7 +468,7 @@ struct SpecializationContext (IRType*)newDiffFuncType, specDiffFunc->getBase(), argCount, - args.getBuffer()); + args.getArrayView().getBuffer()); // Add the new spec insts to the list so they get specialized with // the usual logic. @@ -722,6 +728,7 @@ struct SpecializationContext builder.setInsertInto(moduleInst); auto dictInst = builder.emitIntrinsicInst(nullptr, dictOp, 0, nullptr); builder.setInsertInto(dictInst); + List args; for (const auto& [key, value] : dict) { if (!value->parent) @@ -731,10 +738,10 @@ struct SpecializationContext if (!keyVal->parent) goto next; } { - List args; + args.clear(); args.add(value); args.addRange(key.vals); - builder.emitIntrinsicInst(nullptr, kIROp_SpecializationDictionaryItem, args.getCount(), args.getBuffer()); + builder.emitIntrinsicInst(nullptr, kIROp_SpecializationDictionaryItem, (UInt)args.getCount(), args.getBuffer()); } next:; } @@ -955,11 +962,11 @@ struct SpecializationContext IRBuilder builder(module); builder.setInsertBefore(inst); - List args; + ShortList args; args.add(wrapExistential->getWrappedValue()); for (UInt i = 1; i < inst->getArgCount(); i++) args.add(inst->getArg(i)); - List slotOperands; + ShortList slotOperands; UInt slotOperandCount = wrapExistential->getSlotOperandCount(); for (UInt ii = 0; ii < slotOperandCount; ++ii) { @@ -977,9 +984,9 @@ struct SpecializationContext innerResultType = builder.getPtrType(elementType); } auto newCallee = getNewSpecializedBufferLoadCallee(inst->getCallee(), sbType, innerResultType); - auto newCall = builder.emitCallInst(innerResultType, newCallee, args); + auto newCall = builder.emitCallInst(innerResultType, newCallee, (UInt)args.getCount(), args.getArrayView().getBuffer()); auto newWrapExistential = builder.emitWrapExistential( - resultType, newCall, slotOperandCount, slotOperands.getBuffer()); + resultType, newCall, slotOperandCount, slotOperands.getArrayView().getBuffer()); inst->replaceUsesWith(newWrapExistential); workList.remove(inst); inst->removeAndDeallocate(); @@ -1147,7 +1154,7 @@ struct SpecializationContext // We will start by constructing the argument list for the new call. // argCounter = 0; - List newArgs; + ShortList newArgs; for (auto param : calleeFunc->getParams()) { auto arg = inst->getArg(argCounter++); @@ -1193,7 +1200,7 @@ struct SpecializationContext builder->setInsertBefore(inst); auto newCall = builder->emitCallInst( - inst->getFullType(), specializedCallee, newArgs); + inst->getFullType(), specializedCallee, (UInt)newArgs.getCount(), newArgs.getArrayView().getBuffer()); // We will completely replace the old `call` instruction with the // new one, and will go so far as to transfer any decorations @@ -1235,6 +1242,7 @@ struct SpecializationContext } // Test if a type is compile time constant. + HashSet seenTypeSet; bool isCompileTimeConstantType(IRInst* inst) { // TODO: We probably need/want a more robust test here. @@ -1243,10 +1251,11 @@ struct SpecializationContext if (!isInstFullySpecialized(inst)) return false; - List localWorkList; - HashSet processedInsts; + ShortList localWorkList; + seenTypeSet.clear(); + localWorkList.add(inst); - processedInsts.add(inst); + seenTypeSet.add(inst); while (localWorkList.getCount() != 0) { @@ -1269,10 +1278,8 @@ struct SpecializationContext for (UInt i = 0; i < curInst->getOperandCount(); ++i) { auto operand = curInst->getOperand(i); - if (processedInsts.add(operand)) - { + if (seenTypeSet.add(operand)) localWorkList.add(operand); - } } } return true; @@ -1375,7 +1382,7 @@ struct SpecializationContext // the lists here because we don't yet have a basic // block, or even a function, to insert them into. // - List newParams; + ShortList newParams; UInt argCounter = 0; for (auto oldParam : oldFunc->getParams()) { @@ -1481,14 +1488,14 @@ struct SpecializationContext // In order to construct the type of the new function, we // need to extract the types of all its parameters. // - List newParamTypes; + ShortList newParamTypes; for (auto newParam : newParams) { newParamTypes.add(newParam->getFullType()); } IRType* newFuncType = builder->getFuncType( newParamTypes.getCount(), - newParamTypes.getBuffer(), + newParamTypes.getArrayView().getBuffer(), oldFunc->getResultType()); newFunc->setFullType(newFuncType); @@ -1690,7 +1697,7 @@ struct SpecializationContext return false; - List slotOperands; + ShortList slotOperands; UInt slotOperandCount = wrapInst->getSlotOperandCount(); for (UInt ii = 0; ii < slotOperandCount; ++ii) { @@ -1702,7 +1709,7 @@ struct SpecializationContext resultType, newLoadInst, slotOperandCount, - slotOperands.getBuffer()); + slotOperands.getArrayView().getBuffer()); addUsersToWorkList(inst); @@ -1823,7 +1830,7 @@ struct SpecializationContext auto foundFieldType = foundField->getFieldType(); - List slotOperands; + ShortList slotOperands; UInt slotOperandCount = calcExistentialBoxSlotCount(foundFieldType); for (UInt ii = 0; ii < slotOperandCount; ++ii) @@ -1840,7 +1847,7 @@ struct SpecializationContext resultType, newGetField, slotOperandCount, - slotOperands.getBuffer()); + slotOperands.getArrayView().getBuffer()); addUsersToWorkList(inst); inst->replaceUsesWith(newWrapExistentialInst); @@ -1913,7 +1920,7 @@ struct SpecializationContext auto foundFieldType = foundField->getFieldType(); - List slotOperands; + ShortList slotOperands; UInt slotOperandCount = calcExistentialBoxSlotCount(foundFieldType); for (UInt ii = 0; ii < slotOperandCount; ++ii) @@ -1930,7 +1937,7 @@ struct SpecializationContext resultType, newGetFieldAddr, slotOperandCount, - slotOperands.getBuffer()); + slotOperands.getArrayView().getBuffer()); addUsersToWorkList(inst); inst->replaceUsesWith(newWrapExistentialInst); @@ -1958,7 +1965,7 @@ struct SpecializationContext auto elementType = cast(val->getDataType())->getElementType(); - List slotOperands; + ShortList slotOperands; UInt slotOperandCount = wrapInst->getSlotOperandCount(); for (UInt ii = 0; ii < slotOperandCount; ++ii) @@ -1969,7 +1976,7 @@ struct SpecializationContext auto newGetElement = builder.emitElementExtract(elementType, val, index); auto newWrapExistentialInst = builder.emitWrapExistential( - resultType, newGetElement, slotOperandCount, slotOperands.getBuffer()); + resultType, newGetElement, slotOperandCount, slotOperands.getArrayView().getBuffer()); addUsersToWorkList(inst); inst->replaceUsesWith(newWrapExistentialInst); @@ -1999,7 +2006,7 @@ struct SpecializationContext IRBuilder builder(module); builder.setInsertBefore(inst); - List slotOperands; + ShortList slotOperands; UInt slotOperandCount = wrapInst->getSlotOperandCount(); for (UInt ii = 0; ii < slotOperandCount; ++ii) @@ -2011,7 +2018,7 @@ struct SpecializationContext auto newElementAddr = builder.emitElementAddress(elementPtrType, val, index); auto newWrapExistentialInst = builder.emitWrapExistential( - resultType, newElementAddr, slotOperandCount, slotOperands.getBuffer()); + resultType, newElementAddr, slotOperandCount, slotOperands.getArrayView().getBuffer()); addUsersToWorkList(inst); inst->replaceUsesWith(newWrapExistentialInst); -- cgit v1.2.3