diff options
Diffstat (limited to 'source/slang')
| -rw-r--r-- | source/slang/emit.cpp | 16 | ||||
| -rw-r--r-- | source/slang/ir-insts.h | 47 | ||||
| -rw-r--r-- | source/slang/ir-legalize-types.cpp | 2 | ||||
| -rw-r--r-- | source/slang/ir.cpp | 23 | ||||
| -rw-r--r-- | source/slang/ir.h | 73 | ||||
| -rw-r--r-- | source/slang/lower-to-ir.cpp | 14 | ||||
| -rw-r--r-- | source/slang/memory_pool.cpp | 52 | ||||
| -rw-r--r-- | source/slang/memory_pool.h | 19 | ||||
| -rw-r--r-- | source/slang/slang.vcxproj | 2 | ||||
| -rw-r--r-- | source/slang/slang.vcxproj.filters | 8 |
10 files changed, 93 insertions, 163 deletions
diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 1d121d69b..2d774d8d8 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -1389,7 +1389,7 @@ struct EmitVisitor bool isTargetIntrinsicModifierApplicable( IRTargetIntrinsicDecoration* decoration) { - auto targetName = decoration->targetName; + auto targetName = String(decoration->targetName); // If no target name was specified, then the modifier implicitly // applies to all targets. @@ -2133,7 +2133,7 @@ struct EmitVisitor // then use that name instead. if(auto intrinsicDecoration = findTargetIntrinsicDecoration(context, inst)) { - return intrinsicDecoration->definition; + return String(intrinsicDecoration->definition); } // If we have a name hint on the instruction, then we will try to use that @@ -2945,7 +2945,7 @@ struct EmitVisitor args++; argCount--; - auto name = targetIntrinsic->definition; + auto name = String(targetIntrinsic->definition); if(isOrdinaryName(name)) @@ -3845,11 +3845,11 @@ struct EmitVisitor if(auto layoutDecoration = inst->findDecoration<IRLayoutDecoration>()) { auto layout = layoutDecoration->layout; - if(auto varLayout = layout.As<VarLayout>()) + if(auto varLayout = layout->dynamicCast<VarLayout>()) { emitIRSemantics(ctx, varLayout); } - else if (auto entryPointLayout = layout.As<EntryPointLayout>()) + else if (auto entryPointLayout = layout->dynamicCast<EntryPointLayout>()) { emitIRSemantics(ctx, entryPointLayout->resultLayout); } @@ -3864,7 +3864,7 @@ struct EmitVisitor if (!decoration) return nullptr; - return (VarLayout*) decoration->layout.Ptr(); + return (VarLayout*) decoration->layout; } void emitIRLayoutSemantics( @@ -4757,7 +4757,7 @@ struct EmitVisitor { if( auto layoutDecoration = func->findDecoration<IRLayoutDecoration>() ) { - return layoutDecoration->layout.As<EntryPointLayout>(); + return layoutDecoration->layout->dynamicCast<EntryPointLayout>(); } return nullptr; } @@ -4766,7 +4766,7 @@ struct EmitVisitor { if (auto layoutDecoration = func->findDecoration<IRLayoutDecoration>()) { - if (auto entryPointLayout = layoutDecoration->layout.As<EntryPointLayout>()) + if (auto entryPointLayout = layoutDecoration->layout->dynamicCast<EntryPointLayout>()) { return entryPointLayout; } diff --git a/source/slang/ir-insts.h b/source/slang/ir-insts.h index e7ca08c6d..644036e89 100644 --- a/source/slang/ir-insts.h +++ b/source/slang/ir-insts.h @@ -32,12 +32,7 @@ struct IRLayoutDecoration : IRDecoration { enum { kDecorationOp = kIRDecorationOp_Layout }; - RefPtr<Layout> layout; - virtual void dispose() override - { - IRDecoration::dispose(); - layout = nullptr; - } + Layout* layout; }; enum IRLoopControl @@ -56,12 +51,7 @@ struct IRLoopControlDecoration : IRDecoration struct IRTargetSpecificDecoration : IRDecoration { // TODO: have a more structured representation of target specifiers - String targetName; - virtual void dispose()override - { - IRDecoration::dispose(); - targetName = String(); - } + StringRepresentation* targetName; }; struct IRTargetDecoration : IRTargetSpecificDecoration @@ -73,12 +63,7 @@ struct IRTargetIntrinsicDecoration : IRTargetSpecificDecoration { enum { kDecorationOp = kIRDecorationOp_TargetIntrinsic }; - String definition; - virtual void dispose()override - { - IRTargetSpecificDecoration::dispose(); - definition = String(); - } + StringRepresentation* definition; }; struct IRGLSLOuterArrayDecoration : IRDecoration @@ -138,12 +123,7 @@ struct IRNameHintDecoration : IRDecoration // declaration. struct IRDeclRef : IRInst { - DeclRef<Decl> declRef; - virtual void dispose() override - { - IRInst::dispose(); - declRef = decltype(declRef)(); - } + Decl* declRef; }; // An instruction that specializes another IR value @@ -838,17 +818,32 @@ struct IRBuilder { SLANG_ASSERT(getModule()); auto decorationSize = sizeof(T); - auto decoration = (T*)getModule()->memoryPool.allocZero(decorationSize); + auto decoration = (T*)getModule()->memoryArena.allocateAndZero(decorationSize); + + // TODO: Do we need to run ctor after zeroing? new(decoration)T(); decoration->op = op; decoration->next = value->firstDecoration; value->firstDecoration = decoration; - getModule()->irObjectsToFree.Add(decoration); return decoration; } + template <typename T> + T* addRefObjectToFree(T* ptr) + { + getModule()->addRefObjectToFree(ptr); + return ptr; + } + StringRepresentation* addStringToFree(const String& string) + { + StringRepresentation* stringRep = string.getStringRepresentation(); + getModule()->addRefObjectToFree(stringRep); + return stringRep; + } + + template<typename T> T* addDecoration(IRInst* value) { diff --git a/source/slang/ir-legalize-types.cpp b/source/slang/ir-legalize-types.cpp index 9136ad671..931ceb85a 100644 --- a/source/slang/ir-legalize-types.cpp +++ b/source/slang/ir-legalize-types.cpp @@ -844,7 +844,7 @@ static LegalVal legalizeInst( RefPtr<VarLayout> findVarLayout(IRInst* value) { if (auto layoutDecoration = value->findDecoration<IRLayoutDecoration>()) - return layoutDecoration->layout.As<VarLayout>(); + return layoutDecoration->layout->dynamicCast<VarLayout>(); return nullptr; } diff --git a/source/slang/ir.cpp b/source/slang/ir.cpp index 83735d6df..6b2939987 100644 --- a/source/slang/ir.cpp +++ b/source/slang/ir.cpp @@ -763,7 +763,7 @@ namespace Slang // Create an IR instruction/value and initialize it. // - // In this case `argCount` and `args` represnt the + // In this case `argCount` and `args` represent the // arguments *after* the type (which is a mandatory // argument for all instructions). template<typename T> @@ -791,9 +791,10 @@ namespace Slang } SLANG_ASSERT(module); - T* inst = (T*)module->memoryPool.allocZero(size); - new(inst)T(); + T* inst = (T*)module->memoryArena.allocateAndZero(size); + // TODO: Do we need to run ctor after zeroing? + new(inst)T(); inst->operandCount = (uint32_t)(fixedArgCount + varArgCount); @@ -833,7 +834,6 @@ namespace Slang operand++; } } - module->irObjectsToFree.Add(inst); return inst; } @@ -2285,7 +2285,7 @@ namespace Slang IRLayoutDecoration* IRBuilder::addLayoutDecoration(IRInst* inst, Layout* layout) { auto decoration = addDecoration<IRLayoutDecoration>(inst); - decoration->layout = layout; + decoration->layout = addRefObjectToFree(layout); return decoration; } @@ -2534,7 +2534,7 @@ namespace Slang dump(context, "\n"); dumpIndent(context); dump(context, "[target("); - dump(context, decoration->targetName.Buffer()); + dump(context, StringRepresentation::getData(decoration->targetName)); dump(context, ")]"); } break; @@ -2910,11 +2910,6 @@ namespace Slang ff->debugValidate(); } - void IRInst::dispose() - { - IRObject::dispose(); - } - // Insert this instruction into the same basic block // as `other`, right before it. void IRInst::insertBefore(IRInst* other) @@ -4223,7 +4218,7 @@ namespace Slang continue; auto decoration = (IRTargetIntrinsicDecoration*) dd; - if(decoration->targetName == targetName) + if(String(decoration->targetName) == targetName) return decoration; } @@ -4469,7 +4464,7 @@ namespace Slang if(!decoration) continue; - if(decoration->definition != "EmitVertex()") + if(StringRepresentation::asSlice(decoration->definition) != UnownedStringSlice::fromLiteral("EmitVertex()")) { continue; } @@ -5425,7 +5420,7 @@ namespace Slang continue; auto decoration = (IRTargetDecoration*) dd; - if(decoration->targetName == targetName) + if(String(decoration->targetName) == targetName) return TargetSpecializationLevel::specializedForTarget; result = TargetSpecializationLevel::specializedForOtherTarget; diff --git a/source/slang/ir.h b/source/slang/ir.h index 4c601ce5d..a7f2cb4fb 100644 --- a/source/slang/ir.h +++ b/source/slang/ir.h @@ -10,7 +10,9 @@ #include "../core/basic.h" #include "source-loc.h" -#include "memory_pool.h" + +#include "../core/slang-memory-arena.h" + #include "type-system-shared.h" namespace Slang { @@ -128,18 +130,9 @@ enum IRDecorationOp : uint16_t kIRDecorationOp_NameHint, }; -// represents an object allocated in an IR memory pool +// represents an object allocated in an IR memory arena struct IRObject { - bool isDestroyed = false; - virtual void dispose() - { - isDestroyed = true; - } - virtual ~IRObject() - { - isDestroyed = true; - } }; // A "decoration" that gets applied to an instruction. @@ -247,9 +240,6 @@ struct IRInst : public IRObject // that this value will now have no uses. void replaceUsesWith(IRInst* other); - // Clean up any non-pool resources used by this instruction - virtual void dispose() override; - // Insert this instruction into the same basic block // as `other`, right before/after it. void insertBefore(IRInst* other); @@ -332,8 +322,8 @@ struct IRInstListBase - IRInst* first = 0; - IRInst* last = 0; + IRInst* first = nullptr; + IRInst* last = nullptr; IRInst* getFirst() { return first; } IRInst* getLast() { return last; } @@ -991,23 +981,50 @@ struct IRModuleInst : IRParentInst struct IRModule : RefObject { - // The compilation session in use. - Session* session; - MemoryPool memoryPool; - List<IRObject*> irObjectsToFree; // list of ir objects to run destructor upon destruction + enum + { + kMemoryArenaBlockSize = 16 * 1024, ///< Use 16k block size for memory arena + }; - IRModuleInst* moduleInst; - IRModuleInst* getModuleInst() { return moduleInst; } + SLANG_FORCE_INLINE Session* getSession() const { return session; } + SLANG_FORCE_INLINE IRModuleInst* getModuleInst() const { return moduleInst; } - IRInstListBase getGlobalInsts() { return getModuleInst()->getChildren(); } + IRInstListBase getGlobalInsts() const { return getModuleInst()->getChildren(); } - ~IRModule() + void addRefObjectToFree(RefObject* obj) + { + if (obj) + { + obj->addReference(); + m_refObjectsToFree.Add(obj); + } + } + + /// Ctor + IRModule(): + memoryArena(kMemoryArenaBlockSize) { - for (auto val : irObjectsToFree) - if (!val->isDestroyed) - val->dispose(); - irObjectsToFree = List<IRObject*>(); } + + ~IRModule() + { + // Release all ref objects + for (RefObject* ptr: m_refObjectsToFree) + { + ptr->releaseReference(); + } + // Clear any memory too + m_refObjectsToFree = List<RefObject*>(); + } + + MemoryArena memoryArena; + + // The compilation session in use. + Session* session; + IRModuleInst* moduleInst; + + protected: + List<RefObject*> m_refObjectsToFree; }; void printSlangIRAssembly(StringBuilder& builder, IRModule* module); diff --git a/source/slang/lower-to-ir.cpp b/source/slang/lower-to-ir.cpp index 280b89fb8..d57872fc9 100644 --- a/source/slang/lower-to-ir.cpp +++ b/source/slang/lower-to-ir.cpp @@ -4549,19 +4549,21 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> IRInst* irInst, Decl* decl) { + auto builder = getBuilder(); + for (auto targetMod : decl->GetModifiersOfType<TargetIntrinsicModifier>()) { - auto decoration = getBuilder()->addDecoration<IRTargetIntrinsicDecoration>(irInst); - decoration->targetName = targetMod->targetToken.Content; - + auto decoration = builder->addDecoration<IRTargetIntrinsicDecoration>(irInst); + decoration->targetName = builder->addStringToFree(targetMod->targetToken.Content); + auto definitionToken = targetMod->definitionToken; if (definitionToken.type == TokenType::StringLiteral) { - decoration->definition = getStringLiteralTokenValue(definitionToken); + decoration->definition = builder->addStringToFree(getStringLiteralTokenValue(definitionToken)); } else { - decoration->definition = definitionToken.Content; + decoration->definition = builder->addStringToFree(definitionToken.Content); } } } @@ -4900,7 +4902,7 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo> // target, and we need to reflect that at the IR level. auto decoration = getBuilder()->addDecoration<IRTargetDecoration>(irFunc); - decoration->targetName = targetMod->targetToken.Content; + decoration->targetName = getBuilder()->addStringToFree(targetMod->targetToken.Content); } // If this declaration was marked as having a target-specific lowering diff --git a/source/slang/memory_pool.cpp b/source/slang/memory_pool.cpp deleted file mode 100644 index 884c9cfe4..000000000 --- a/source/slang/memory_pool.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "memory_pool.h" - -namespace Slang -{ - const size_t kPoolSegmentSize = 4 << 20; // use 4MB segments - - struct MemoryPoolSegment - { - unsigned char* data = nullptr; - size_t allocPtr = 0; - MemoryPoolSegment* nextSegment = nullptr; - }; - - MemoryPool::~MemoryPool() - { - while (curSegment) - { - auto nxtSegment = curSegment->nextSegment; - free(curSegment->data); - delete curSegment; - curSegment = nxtSegment; - } - } - - void newSegment(MemoryPool* pool) - { - auto seg = new MemoryPoolSegment(); - seg->nextSegment = pool->curSegment; - seg->data = (unsigned char*)malloc(kPoolSegmentSize); - pool->curSegment = seg; - } - - void * MemoryPool::alloc(size_t size) - { - assert(size < kPoolSegmentSize); - // ensure there is a segment available - if (!curSegment) - newSegment(this); - if (curSegment->allocPtr + size > kPoolSegmentSize) - newSegment(this); - // alloc memory from current segment - void* rs = curSegment->data + curSegment->allocPtr; - curSegment->allocPtr += size; - return rs; - } - void * MemoryPool::allocZero(size_t size) - { - auto rs = alloc(size); - memset(rs, 0, size); - return rs; - } -} diff --git a/source/slang/memory_pool.h b/source/slang/memory_pool.h deleted file mode 100644 index ee3ee1aa3..000000000 --- a/source/slang/memory_pool.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef SLANG_MEMORY_POOL_H -#define SLANG_MEMORY_POOL_H - -#include "../core/basic.h" - -namespace Slang -{ - struct MemoryPoolSegment; - - struct MemoryPool : public RefObject - { - MemoryPoolSegment* curSegment = nullptr; - ~MemoryPool(); - void* alloc(size_t size); - void* allocZero(size_t size); - }; -} - -#endif
\ No newline at end of file diff --git a/source/slang/slang.vcxproj b/source/slang/slang.vcxproj index 284907018..eeaf7410a 100644 --- a/source/slang/slang.vcxproj +++ b/source/slang/slang.vcxproj @@ -195,7 +195,6 @@ <ClInclude Include="lookup.h" /> <ClInclude Include="lower-to-ir.h" /> <ClInclude Include="mangle.h" /> - <ClInclude Include="memory_pool.h" /> <ClInclude Include="modifier-defs.h" /> <ClInclude Include="name.h" /> <ClInclude Include="object-meta-begin.h" /> @@ -241,7 +240,6 @@ <ClCompile Include="lookup.cpp" /> <ClCompile Include="lower-to-ir.cpp" /> <ClCompile Include="mangle.cpp" /> - <ClCompile Include="memory_pool.cpp" /> <ClCompile Include="name.cpp" /> <ClCompile Include="options.cpp" /> <ClCompile Include="parameter-binding.cpp" /> diff --git a/source/slang/slang.vcxproj.filters b/source/slang/slang.vcxproj.filters index f12163f27..e2b17530e 100644 --- a/source/slang/slang.vcxproj.filters +++ b/source/slang/slang.vcxproj.filters @@ -1,4 +1,4 @@ -<?xml version="1.0" encoding="utf-8"?> +<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup> <Filter Include="Header Files"> @@ -84,9 +84,6 @@ <ClInclude Include="mangle.h"> <Filter>Header Files</Filter> </ClInclude> - <ClInclude Include="memory_pool.h"> - <Filter>Header Files</Filter> - </ClInclude> <ClInclude Include="modifier-defs.h"> <Filter>Header Files</Filter> </ClInclude> @@ -218,9 +215,6 @@ <ClCompile Include="mangle.cpp"> <Filter>Source Files</Filter> </ClCompile> - <ClCompile Include="memory_pool.cpp"> - <Filter>Source Files</Filter> - </ClCompile> <ClCompile Include="name.cpp"> <Filter>Source Files</Filter> </ClCompile> |
