From b7946d16bbfc80e8d743e2c3f62794c8f3295b64 Mon Sep 17 00:00:00 2001 From: Andrew Woloszyn Date: Mon, 18 Jan 2016 09:23:56 -0500 Subject: [PATCH] Free memory associated with SPIR-V generation. --- SPIRV/SpvBuilder.cpp | 136 ++++++++++++++-------------- SPIRV/SpvBuilder.h | 24 +++-- SPIRV/spvIR.h | 30 +++--- glslang/Include/intermediate.h | 4 +- glslang/MachineIndependent/Intermediate.cpp | 8 +- 5 files changed, 109 insertions(+), 93 deletions(-) diff --git a/SPIRV/SpvBuilder.cpp b/SPIRV/SpvBuilder.cpp index 969a6d7..7b17e17 100755 --- a/SPIRV/SpvBuilder.cpp +++ b/SPIRV/SpvBuilder.cpp @@ -77,7 +77,7 @@ Id Builder::import(const char* name) Instruction* import = new Instruction(getUniqueId(), NoType, OpExtInstImport); import->addStringOperand(name); - imports.push_back(import); + imports.push_back(std::unique_ptr(import)); return import->getResultId(); } @@ -88,7 +88,7 @@ Id Builder::makeVoidType() if (groupedTypes[OpTypeVoid].size() == 0) { type = new Instruction(getUniqueId(), NoType, OpTypeVoid); groupedTypes[OpTypeVoid].push_back(type); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); } else type = groupedTypes[OpTypeVoid].back(); @@ -102,7 +102,7 @@ Id Builder::makeBoolType() if (groupedTypes[OpTypeBool].size() == 0) { type = new Instruction(getUniqueId(), NoType, OpTypeBool); groupedTypes[OpTypeBool].push_back(type); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); } else type = groupedTypes[OpTypeBool].back(); @@ -116,7 +116,7 @@ Id Builder::makeSamplerType() if (groupedTypes[OpTypeSampler].size() == 0) { type = new Instruction(getUniqueId(), NoType, OpTypeSampler); groupedTypes[OpTypeSampler].push_back(type); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); } else type = groupedTypes[OpTypeSampler].back(); @@ -140,7 +140,7 @@ Id Builder::makePointer(StorageClass storageClass, Id pointee) type->addImmediateOperand(storageClass); type->addIdOperand(pointee); groupedTypes[OpTypePointer].push_back(type); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); return type->getResultId(); @@ -162,7 +162,7 @@ Id Builder::makeIntegerType(int width, bool hasSign) type->addImmediateOperand(width); type->addImmediateOperand(hasSign ? 1 : 0); groupedTypes[OpTypeInt].push_back(type); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); return type->getResultId(); @@ -182,7 +182,7 @@ Id Builder::makeFloatType(int width) type = new Instruction(getUniqueId(), NoType, OpTypeFloat); type->addImmediateOperand(width); groupedTypes[OpTypeFloat].push_back(type); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); return type->getResultId(); @@ -202,7 +202,7 @@ Id Builder::makeStructType(std::vector& members, const char* name) for (int op = 0; op < (int)members.size(); ++op) type->addIdOperand(members[op]); groupedTypes[OpTypeStruct].push_back(type); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); addName(type->getResultId(), name); @@ -249,7 +249,7 @@ Id Builder::makeVectorType(Id component, int size) type->addIdOperand(component); type->addImmediateOperand(size); groupedTypes[OpTypeVector].push_back(type); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); return type->getResultId(); @@ -275,7 +275,7 @@ Id Builder::makeMatrixType(Id component, int cols, int rows) type->addIdOperand(column); type->addImmediateOperand(cols); groupedTypes[OpTypeMatrix].push_back(type); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); return type->getResultId(); @@ -305,7 +305,7 @@ Id Builder::makeArrayType(Id element, unsigned size, int stride) type->addIdOperand(element); type->addIdOperand(sizeId); groupedTypes[OpTypeArray].push_back(type); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); return type->getResultId(); @@ -315,7 +315,7 @@ Id Builder::makeRuntimeArray(Id element) { Instruction* type = new Instruction(getUniqueId(), NoType, OpTypeRuntimeArray); type->addIdOperand(element); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); return type->getResultId(); @@ -346,7 +346,7 @@ Id Builder::makeFunctionType(Id returnType, std::vector& paramTypes) for (int p = 0; p < (int)paramTypes.size(); ++p) type->addIdOperand(paramTypes[p]); groupedTypes[OpTypeFunction].push_back(type); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); return type->getResultId(); @@ -379,7 +379,7 @@ Id Builder::makeImageType(Id sampledType, Dim dim, bool depth, bool arrayed, boo type->addImmediateOperand((unsigned int)format); groupedTypes[OpTypeImage].push_back(type); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); return type->getResultId(); @@ -400,7 +400,7 @@ Id Builder::makeSampledImageType(Id imageType) type->addIdOperand(imageType); groupedTypes[OpTypeSampledImage].push_back(type); - constantsTypesGlobals.push_back(type); + constantsTypesGlobals.push_back(std::unique_ptr(type)); module.mapInstruction(type); return type->getResultId(); @@ -594,7 +594,7 @@ Id Builder::makeBoolConstant(bool b, bool specConstant) // Make it Instruction* c = new Instruction(getUniqueId(), typeId, opcode); - constantsTypesGlobals.push_back(c); + constantsTypesGlobals.push_back(std::unique_ptr(c)); groupedConstants[OpTypeBool].push_back(c); module.mapInstruction(c); @@ -610,7 +610,7 @@ Id Builder::makeIntConstant(Id typeId, unsigned value, bool specConstant) Instruction* c = new Instruction(getUniqueId(), typeId, opcode); c->addImmediateOperand(value); - constantsTypesGlobals.push_back(c); + constantsTypesGlobals.push_back(std::unique_ptr(c)); groupedConstants[OpTypeInt].push_back(c); module.mapInstruction(c); @@ -628,7 +628,7 @@ Id Builder::makeFloatConstant(float f, bool specConstant) Instruction* c = new Instruction(getUniqueId(), typeId, opcode); c->addImmediateOperand(value); - constantsTypesGlobals.push_back(c); + constantsTypesGlobals.push_back(std::unique_ptr(c)); groupedConstants[OpTypeFloat].push_back(c); module.mapInstruction(c); @@ -649,7 +649,7 @@ Id Builder::makeDoubleConstant(double d, bool specConstant) Instruction* c = new Instruction(getUniqueId(), typeId, opcode); c->addImmediateOperand(op1); c->addImmediateOperand(op2); - constantsTypesGlobals.push_back(c); + constantsTypesGlobals.push_back(std::unique_ptr(c)); groupedConstants[OpTypeFloat].push_back(c); module.mapInstruction(c); @@ -708,7 +708,7 @@ Id Builder::makeCompositeConstant(Id typeId, std::vector& members) Instruction* c = new Instruction(getUniqueId(), typeId, OpConstantComposite); for (int op = 0; op < (int)members.size(); ++op) c->addIdOperand(members[op]); - constantsTypesGlobals.push_back(c); + constantsTypesGlobals.push_back(std::unique_ptr(c)); groupedConstants[typeClass].push_back(c); module.mapInstruction(c); @@ -722,7 +722,7 @@ Instruction* Builder::addEntryPoint(ExecutionModel model, Function* function, co entryPoint->addIdOperand(function->getId()); entryPoint->addStringOperand(name); - entryPoints.push_back(entryPoint); + entryPoints.push_back(std::unique_ptr(entryPoint)); return entryPoint; } @@ -740,7 +740,7 @@ void Builder::addExecutionMode(Function* entryPoint, ExecutionMode mode, int val if (value3 >= 0) instr->addImmediateOperand(value3); - executionModes.push_back(instr); + executionModes.push_back(std::unique_ptr(instr)); } void Builder::addName(Id id, const char* string) @@ -749,7 +749,7 @@ void Builder::addName(Id id, const char* string) name->addIdOperand(id); name->addStringOperand(string); - names.push_back(name); + names.push_back(std::unique_ptr(name)); } void Builder::addMemberName(Id id, int memberNumber, const char* string) @@ -759,7 +759,7 @@ void Builder::addMemberName(Id id, int memberNumber, const char* string) name->addImmediateOperand(memberNumber); name->addStringOperand(string); - names.push_back(name); + names.push_back(std::unique_ptr(name)); } void Builder::addLine(Id target, Id fileName, int lineNum, int column) @@ -770,7 +770,7 @@ void Builder::addLine(Id target, Id fileName, int lineNum, int column) line->addImmediateOperand(lineNum); line->addImmediateOperand(column); - lines.push_back(line); + lines.push_back(std::unique_ptr(line)); } void Builder::addDecoration(Id id, Decoration decoration, int num) @@ -783,7 +783,7 @@ void Builder::addDecoration(Id id, Decoration decoration, int num) if (num >= 0) dec->addImmediateOperand(num); - decorations.push_back(dec); + decorations.push_back(std::unique_ptr(dec)); } void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decoration, int num) @@ -795,7 +795,7 @@ void Builder::addMemberDecoration(Id id, unsigned int member, Decoration decorat if (num >= 0) dec->addImmediateOperand(num); - decorations.push_back(dec); + decorations.push_back(std::unique_ptr(dec)); } // Comments in header @@ -827,6 +827,8 @@ Function* Builder::makeFunctionEntry(Id returnType, const char* name, std::vecto if (name) addName(function->getId(), name); + functions.push_back(std::unique_ptr(function)); + return function; } @@ -836,9 +838,9 @@ void Builder::makeReturn(bool implicit, Id retVal) if (retVal) { Instruction* inst = new Instruction(NoResult, NoType, OpReturnValue); inst->addIdOperand(retVal); - buildPoint->addInstruction(inst); + buildPoint->addInstruction(std::unique_ptr(inst)); } else - buildPoint->addInstruction(new Instruction(NoResult, NoType, OpReturn)); + buildPoint->addInstruction(std::unique_ptr(new Instruction(NoResult, NoType, OpReturn))); if (! implicit) createAndSetNoPredecessorBlock("post-return"); @@ -878,7 +880,7 @@ void Builder::leaveFunction() // Comments in header void Builder::makeDiscard() { - buildPoint->addInstruction(new Instruction(OpKill)); + buildPoint->addInstruction(std::unique_ptr(new Instruction(OpKill))); createAndSetNoPredecessorBlock("post-discard"); } @@ -892,11 +894,11 @@ Id Builder::createVariable(StorageClass storageClass, Id type, const char* name) switch (storageClass) { case StorageClassFunction: // Validation rules require the declaration in the entry block - buildPoint->getParent().addLocalVariable(inst); + buildPoint->getParent().addLocalVariable(std::unique_ptr(inst)); break; default: - constantsTypesGlobals.push_back(inst); + constantsTypesGlobals.push_back(std::unique_ptr(inst)); module.mapInstruction(inst); break; } @@ -911,7 +913,7 @@ Id Builder::createVariable(StorageClass storageClass, Id type, const char* name) Id Builder::createUndefined(Id type) { Instruction* inst = new Instruction(getUniqueId(), type, OpUndef); - buildPoint->addInstruction(inst); + buildPoint->addInstruction(std::unique_ptr(inst)); return inst->getResultId(); } @@ -921,7 +923,7 @@ void Builder::createStore(Id rValue, Id lValue) Instruction* store = new Instruction(OpStore); store->addIdOperand(lValue); store->addIdOperand(rValue); - buildPoint->addInstruction(store); + buildPoint->addInstruction(std::unique_ptr(store)); } // Comments in header @@ -929,7 +931,7 @@ Id Builder::createLoad(Id lValue) { Instruction* load = new Instruction(getUniqueId(), getDerefTypeId(lValue), OpLoad); load->addIdOperand(lValue); - buildPoint->addInstruction(load); + buildPoint->addInstruction(std::unique_ptr(load)); return load->getResultId(); } @@ -955,7 +957,7 @@ Id Builder::createAccessChain(StorageClass storageClass, Id base, std::vectoraddIdOperand(base); for (int i = 0; i < (int)offsets.size(); ++i) chain->addIdOperand(offsets[i]); - buildPoint->addInstruction(chain); + buildPoint->addInstruction(std::unique_ptr(chain)); return chain->getResultId(); } @@ -965,7 +967,7 @@ Id Builder::createArrayLength(Id base, unsigned int member) Instruction* length = new Instruction(getUniqueId(), makeIntType(32), OpArrayLength); length->addIdOperand(base); length->addImmediateOperand(member); - buildPoint->addInstruction(length); + buildPoint->addInstruction(std::unique_ptr(length)); return length->getResultId(); } @@ -975,7 +977,7 @@ Id Builder::createCompositeExtract(Id composite, Id typeId, unsigned index) Instruction* extract = new Instruction(getUniqueId(), typeId, OpCompositeExtract); extract->addIdOperand(composite); extract->addImmediateOperand(index); - buildPoint->addInstruction(extract); + buildPoint->addInstruction(std::unique_ptr(extract)); return extract->getResultId(); } @@ -986,7 +988,7 @@ Id Builder::createCompositeExtract(Id composite, Id typeId, std::vectoraddIdOperand(composite); for (int i = 0; i < (int)indexes.size(); ++i) extract->addImmediateOperand(indexes[i]); - buildPoint->addInstruction(extract); + buildPoint->addInstruction(std::unique_ptr(extract)); return extract->getResultId(); } @@ -997,7 +999,7 @@ Id Builder::createCompositeInsert(Id object, Id composite, Id typeId, unsigned i insert->addIdOperand(object); insert->addIdOperand(composite); insert->addImmediateOperand(index); - buildPoint->addInstruction(insert); + buildPoint->addInstruction(std::unique_ptr(insert)); return insert->getResultId(); } @@ -1009,7 +1011,7 @@ Id Builder::createCompositeInsert(Id object, Id composite, Id typeId, std::vecto insert->addIdOperand(composite); for (int i = 0; i < (int)indexes.size(); ++i) insert->addImmediateOperand(indexes[i]); - buildPoint->addInstruction(insert); + buildPoint->addInstruction(std::unique_ptr(insert)); return insert->getResultId(); } @@ -1019,7 +1021,7 @@ Id Builder::createVectorExtractDynamic(Id vector, Id typeId, Id componentIndex) Instruction* extract = new Instruction(getUniqueId(), typeId, OpVectorExtractDynamic); extract->addIdOperand(vector); extract->addIdOperand(componentIndex); - buildPoint->addInstruction(extract); + buildPoint->addInstruction(std::unique_ptr(extract)); return extract->getResultId(); } @@ -1030,7 +1032,7 @@ Id Builder::createVectorInsertDynamic(Id vector, Id typeId, Id component, Id com insert->addIdOperand(vector); insert->addIdOperand(component); insert->addIdOperand(componentIndex); - buildPoint->addInstruction(insert); + buildPoint->addInstruction(std::unique_ptr(insert)); return insert->getResultId(); } @@ -1039,7 +1041,7 @@ Id Builder::createVectorInsertDynamic(Id vector, Id typeId, Id component, Id com void Builder::createNoResultOp(Op opCode) { Instruction* op = new Instruction(opCode); - buildPoint->addInstruction(op); + buildPoint->addInstruction(std::unique_ptr(op)); } // An opcode that has one operand, no result id, and no type @@ -1047,7 +1049,7 @@ void Builder::createNoResultOp(Op opCode, Id operand) { Instruction* op = new Instruction(opCode); op->addIdOperand(operand); - buildPoint->addInstruction(op); + buildPoint->addInstruction(std::unique_ptr(op)); } // An opcode that has one operand, no result id, and no type @@ -1056,7 +1058,7 @@ void Builder::createNoResultOp(Op opCode, const std::vector& operands) Instruction* op = new Instruction(opCode); for (auto operand : operands) op->addIdOperand(operand); - buildPoint->addInstruction(op); + buildPoint->addInstruction(std::unique_ptr(op)); } void Builder::createControlBarrier(Scope execution, Scope memory, MemorySemanticsMask semantics) @@ -1065,7 +1067,7 @@ void Builder::createControlBarrier(Scope execution, Scope memory, MemorySemantic op->addImmediateOperand(makeUintConstant(execution)); op->addImmediateOperand(makeUintConstant(memory)); op->addImmediateOperand(makeUintConstant(semantics)); - buildPoint->addInstruction(op); + buildPoint->addInstruction(std::unique_ptr(op)); } void Builder::createMemoryBarrier(unsigned executionScope, unsigned memorySemantics) @@ -1073,7 +1075,7 @@ void Builder::createMemoryBarrier(unsigned executionScope, unsigned memorySemant Instruction* op = new Instruction(OpMemoryBarrier); op->addImmediateOperand(makeUintConstant(executionScope)); op->addImmediateOperand(makeUintConstant(memorySemantics)); - buildPoint->addInstruction(op); + buildPoint->addInstruction(std::unique_ptr(op)); } // An opcode that has one operands, a result id, and a type @@ -1081,7 +1083,7 @@ Id Builder::createUnaryOp(Op opCode, Id typeId, Id operand) { Instruction* op = new Instruction(getUniqueId(), typeId, opCode); op->addIdOperand(operand); - buildPoint->addInstruction(op); + buildPoint->addInstruction(std::unique_ptr(op)); return op->getResultId(); } @@ -1091,7 +1093,7 @@ Id Builder::createBinOp(Op opCode, Id typeId, Id left, Id right) Instruction* op = new Instruction(getUniqueId(), typeId, opCode); op->addIdOperand(left); op->addIdOperand(right); - buildPoint->addInstruction(op); + buildPoint->addInstruction(std::unique_ptr(op)); return op->getResultId(); } @@ -1102,7 +1104,7 @@ Id Builder::createTriOp(Op opCode, Id typeId, Id op1, Id op2, Id op3) op->addIdOperand(op1); op->addIdOperand(op2); op->addIdOperand(op3); - buildPoint->addInstruction(op); + buildPoint->addInstruction(std::unique_ptr(op)); return op->getResultId(); } @@ -1112,7 +1114,7 @@ Id Builder::createOp(Op opCode, Id typeId, const std::vector& operands) Instruction* op = new Instruction(getUniqueId(), typeId, opCode); for (auto operand : operands) op->addIdOperand(operand); - buildPoint->addInstruction(op); + buildPoint->addInstruction(std::unique_ptr(op)); return op->getResultId(); } @@ -1123,7 +1125,7 @@ Id Builder::createFunctionCall(spv::Function* function, std::vector& ar op->addIdOperand(function->getId()); for (int a = 0; a < (int)args.size(); ++a) op->addIdOperand(args[a]); - buildPoint->addInstruction(op); + buildPoint->addInstruction(std::unique_ptr(op)); return op->getResultId(); } @@ -1140,7 +1142,7 @@ Id Builder::createRvalueSwizzle(Id typeId, Id source, std::vector& cha swizzle->addIdOperand(source); for (int i = 0; i < (int)channels.size(); ++i) swizzle->addImmediateOperand(channels[i]); - buildPoint->addInstruction(swizzle); + buildPoint->addInstruction(std::unique_ptr(swizzle)); return swizzle->getResultId(); } @@ -1171,7 +1173,7 @@ Id Builder::createLvalueSwizzle(Id typeId, Id target, Id source, std::vectoraddImmediateOperand(components[i]); - buildPoint->addInstruction(swizzle); + buildPoint->addInstruction(std::unique_ptr(swizzle)); return swizzle->getResultId(); } @@ -1202,7 +1204,7 @@ Id Builder::smearScalar(Decoration /*precision*/, Id scalar, Id vectorType) Instruction* smear = new Instruction(getUniqueId(), vectorType, OpCompositeConstruct); for (int c = 0; c < numComponents; ++c) smear->addIdOperand(scalar); - buildPoint->addInstruction(smear); + buildPoint->addInstruction(std::unique_ptr(smear)); return smear->getResultId(); } @@ -1216,7 +1218,7 @@ Id Builder::createBuiltinCall(Decoration /*precision*/, Id resultType, Id builti for (int arg = 0; arg < (int)args.size(); ++arg) inst->addIdOperand(args[arg]); - buildPoint->addInstruction(inst); + buildPoint->addInstruction(std::unique_ptr(inst)); return inst->getResultId(); } @@ -1344,7 +1346,7 @@ Id Builder::createTextureCall(Decoration precision, Id resultType, bool fetch, b for (int op = optArgNum + 1; op < numArgs; ++op) textureInst->addIdOperand(texArgs[op]); setPrecision(textureInst->getResultId(), precision); - buildPoint->addInstruction(textureInst); + buildPoint->addInstruction(std::unique_ptr(textureInst)); Id resultId = textureInst->getResultId(); @@ -1412,7 +1414,7 @@ Id Builder::createTextureQueryCall(Op opCode, const TextureParameters& parameter query->addIdOperand(parameters.coords); if (parameters.lod) query->addIdOperand(parameters.lod); - buildPoint->addInstruction(query); + buildPoint->addInstruction(std::unique_ptr(query)); return query->getResultId(); } @@ -1494,7 +1496,7 @@ Id Builder::createCompositeConstruct(Id typeId, std::vector& constituents) Instruction* op = new Instruction(getUniqueId(), typeId, OpCompositeConstruct); for (int c = 0; c < (int)constituents.size(); ++c) op->addIdOperand(constituents[c]); - buildPoint->addInstruction(op); + buildPoint->addInstruction(std::unique_ptr(op)); return op->getResultId(); } @@ -1710,7 +1712,7 @@ void Builder::makeSwitch(Id selector, int numSegments, std::vector& caseVal switchInst->addImmediateOperand(caseValues[i]); switchInst->addIdOperand(segmentBlocks[valueIndexToSegment[i]]->getId()); } - buildPoint->addInstruction(switchInst); + buildPoint->addInstruction(std::unique_ptr(switchInst)); // push the merge block switchMerges.push(mergeBlock); @@ -1781,7 +1783,7 @@ void Builder::makeNewLoop(bool loopTestFirst) // but we don't yet know where they will come from. loop.isFirstIteration->addIdOperand(makeBoolConstant(true)); loop.isFirstIteration->addIdOperand(preheader->getId()); - getBuildPoint()->addInstruction(loop.isFirstIteration); + getBuildPoint()->addInstruction(std::unique_ptr(loop.isFirstIteration)); // Mark the end of the structured loop. This must exist in the loop header block. createLoopMerge(loop.merge, loop.header, LoopControlMaskNone); @@ -2203,7 +2205,7 @@ void Builder::createBranch(Block* block) { Instruction* branch = new Instruction(OpBranch); branch->addIdOperand(block->getId()); - buildPoint->addInstruction(branch); + buildPoint->addInstruction(std::unique_ptr(branch)); block->addPredecessor(buildPoint); } @@ -2212,7 +2214,7 @@ void Builder::createSelectionMerge(Block* mergeBlock, unsigned int control) Instruction* merge = new Instruction(OpSelectionMerge); merge->addIdOperand(mergeBlock->getId()); merge->addImmediateOperand(control); - buildPoint->addInstruction(merge); + buildPoint->addInstruction(std::unique_ptr(merge)); } void Builder::createLoopMerge(Block* mergeBlock, Block* continueBlock, unsigned int control) @@ -2221,7 +2223,7 @@ void Builder::createLoopMerge(Block* mergeBlock, Block* continueBlock, unsigned merge->addIdOperand(mergeBlock->getId()); merge->addIdOperand(continueBlock->getId()); merge->addImmediateOperand(control); - buildPoint->addInstruction(merge); + buildPoint->addInstruction(std::unique_ptr(merge)); } void Builder::createConditionalBranch(Id condition, Block* thenBlock, Block* elseBlock) @@ -2230,12 +2232,12 @@ void Builder::createConditionalBranch(Id condition, Block* thenBlock, Block* els branch->addIdOperand(condition); branch->addIdOperand(thenBlock->getId()); branch->addIdOperand(elseBlock->getId()); - buildPoint->addInstruction(branch); + buildPoint->addInstruction(std::unique_ptr(branch)); thenBlock->addPredecessor(buildPoint); elseBlock->addPredecessor(buildPoint); } -void Builder::dumpInstructions(std::vector& out, const std::vector& instructions) const +void Builder::dumpInstructions(std::vector& out, const std::vector >& instructions) const { for (int i = 0; i < (int)instructions.size(); ++i) { instructions[i]->dump(out); diff --git a/SPIRV/SpvBuilder.h b/SPIRV/SpvBuilder.h index 7bf4396..026e81d5 100755 --- a/SPIRV/SpvBuilder.h +++ b/SPIRV/SpvBuilder.h @@ -52,6 +52,7 @@ #include "spvIR.h" #include +#include #include #include @@ -201,11 +202,13 @@ public: void setBuildPoint(Block* bp) { buildPoint = bp; } Block* getBuildPoint() const { return buildPoint; } - // Make the main function. + // Make the main function. The returned pointer is only valid + // for the lifetime of this builder. Function* makeMain(); // Make a shader-style function, and create its entry block if entry is non-zero. // Return the function, pass back the entry. + // The returned pointer is only valid for the lifetime of this builder. Function* makeFunctionEntry(Id returnType, const char* name, std::vector& paramTypes, Block **entry = 0); // Create a return. An 'implicit' return is one not appearing in the source @@ -516,7 +519,7 @@ protected: void createSelectionMerge(Block* mergeBlock, unsigned int control); void createLoopMerge(Block* mergeBlock, Block* continueBlock, unsigned int control); void createConditionalBranch(Id condition, Block* thenBlock, Block* elseBlock); - void dumpInstructions(std::vector&, const std::vector&) const; + void dumpInstructions(std::vector&, const std::vector >&) const; struct Loop; // Defined below. void createBranchToLoopHeaderFromInside(const Loop& loop); @@ -535,14 +538,15 @@ protected: AccessChain accessChain; // special blocks of instructions for output - std::vector imports; - std::vector entryPoints; - std::vector executionModes; - std::vector names; - std::vector lines; - std::vector decorations; - std::vector constantsTypesGlobals; - std::vector externals; + std::vector > imports; + std::vector > entryPoints; + std::vector > executionModes; + std::vector > names; + std::vector > lines; + std::vector > decorations; + std::vector > constantsTypesGlobals; + std::vector > externals; + std::vector > functions; // not output, internally used for quick & dirty canonical (unique) creation std::vector groupedConstants[OpConstant]; // all types appear before OpConstant diff --git a/SPIRV/spvIR.h b/SPIRV/spvIR.h index 9291185..e763dbb 100755 --- a/SPIRV/spvIR.h +++ b/SPIRV/spvIR.h @@ -54,6 +54,7 @@ #include #include +#include #include namespace spv { @@ -155,15 +156,14 @@ public: Block(Id id, Function& parent); virtual ~Block() { - // TODO: free instructions } Id getId() { return instructions.front()->getResultId(); } Function& getParent() const { return parent; } - void addInstruction(Instruction* inst); + void addInstruction(std::unique_ptr inst); void addPredecessor(Block* pred) { predecessors.push_back(pred); } - void addLocalVariable(Instruction* inst) { localVariables.push_back(inst); } + void addLocalVariable(std::unique_ptr inst) { localVariables.push_back(std::move(inst)); } int getNumPredecessors() const { return (int)predecessors.size(); } void setUnreachable() { unreachable = true; } bool isUnreachable() const { return unreachable; } @@ -205,9 +205,9 @@ protected: // To enforce keeping parent and ownership in sync: friend Function; - std::vector instructions; + std::vector > instructions; std::vector predecessors; - std::vector localVariables; + std::vector > localVariables; Function& parent; // track whether this block is known to be uncreachable (not necessarily @@ -240,7 +240,7 @@ public: Module& getParent() const { return parent; } Block* getEntryBlock() const { return blocks.front(); } Block* getLastBlock() const { return blocks.back(); } - void addLocalVariable(Instruction* inst); + void addLocalVariable(std::unique_ptr inst); Id getReturnType() const { return functionInstruction.getTypeId(); } void dump(std::vector& out) const { @@ -341,22 +341,24 @@ __inline Function::Function(Id id, Id resultType, Id functionType, Id firstParam } } -__inline void Function::addLocalVariable(Instruction* inst) +__inline void Function::addLocalVariable(std::unique_ptr inst) { - blocks[0]->addLocalVariable(inst); - parent.mapInstruction(inst); + Instruction* raw_instruction = inst.get(); + blocks[0]->addLocalVariable(std::move(inst)); + parent.mapInstruction(raw_instruction); } __inline Block::Block(Id id, Function& parent) : parent(parent), unreachable(false) { - instructions.push_back(new Instruction(id, NoType, OpLabel)); + instructions.push_back(std::unique_ptr(new Instruction(id, NoType, OpLabel))); } -__inline void Block::addInstruction(Instruction* inst) +__inline void Block::addInstruction(std::unique_ptr inst) { - instructions.push_back(inst); - if (inst->getResultId()) - parent.getParent().mapInstruction(inst); + Instruction* raw_instruction = inst.get(); + instructions.push_back(std::move(inst)); + if (raw_instruction->getResultId()) + parent.getParent().mapInstruction(raw_instruction); } }; // end spv namespace diff --git a/glslang/Include/intermediate.h b/glslang/Include/intermediate.h index 59955fc..ebd4439 100644 --- a/glslang/Include/intermediate.h +++ b/glslang/Include/intermediate.h @@ -784,7 +784,9 @@ class TIntermAggregate : public TIntermOperator { public: TIntermAggregate() : TIntermOperator(EOpNull), userDefined(false), pragmaTable(0) { } TIntermAggregate(TOperator o) : TIntermOperator(o), pragmaTable(0) { } - ~TIntermAggregate() { delete pragmaTable; } + // Since pragmaTable is allocated with the PoolAllocator, we + // only want to destroy it, not free the associated memory. + ~TIntermAggregate() { pragmaTable->~TPragmaTable(); } virtual TIntermAggregate* getAsAggregate() { return this; } virtual const TIntermAggregate* getAsAggregate() const { return this; } virtual void setOperator(TOperator o) { op = o; } diff --git a/glslang/MachineIndependent/Intermediate.cpp b/glslang/MachineIndependent/Intermediate.cpp index 2e9fa85..4d60dcd 100644 --- a/glslang/MachineIndependent/Intermediate.cpp +++ b/glslang/MachineIndependent/Intermediate.cpp @@ -1613,7 +1613,13 @@ TIntermTyped* TIntermediate::promoteConstantUnion(TBasicType promoteTo, TIntermC void TIntermAggregate::addToPragmaTable(const TPragmaTable& pTable) { assert(!pragmaTable); - pragmaTable = new TPragmaTable(); + + // We allocate this with the thread-pool allocator because the destructors + // for TIntermNode's are never called. When TIntermNodes are no longer + // needed, the pool allocator destroys all memory at once without + // destruction. + void* memory = GetThreadPoolAllocator().allocate(sizeof(TPragmaTable)); + pragmaTable = new(memory) TPragmaTable(); *pragmaTable = pTable; } -- 2.7.4