From b2b964489ef8623e5d2c4782cf5b366546b67892 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Sat, 16 Jul 2022 06:03:35 -0400 Subject: [PATCH] Fixed append alignment with unaligned stomp allocator --- BeefLibs/corlib/src/Allocator.bf | 6 +++--- IDEHelper/Compiler/BfModule.cpp | 19 ++++++++++++------- IDEHelper/Compiler/BfModule.h | 6 +++--- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/BeefLibs/corlib/src/Allocator.bf b/BeefLibs/corlib/src/Allocator.bf index 9efd2f80..9f2fab0d 100644 --- a/BeefLibs/corlib/src/Allocator.bf +++ b/BeefLibs/corlib/src/Allocator.bf @@ -5,13 +5,13 @@ namespace System { interface IRawAllocator { - void* Alloc(int size, int align); - void Free(void* ptr); + void* Alloc(int size, int align) mut; + void Free(void* ptr) mut; } interface ITypedAllocator : IRawAllocator { - void* AllocTyped(Type type, int size, int align); + void* AllocTyped(Type type, int size, int align) mut; } struct StdAllocator : IRawAllocator diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index b5ec1688..ac4677fe 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -9094,6 +9094,13 @@ void BfModule::InitTypeInst(BfTypedValue typedValue, BfScopeData* scopeData, boo } } +bool BfModule::IsAllocatorAligned() +{ + if (mCompiler->mOptions.mMallocLinkName == "StompAlloc") + return false; + return true; +} + BfIRValue BfModule::AllocBytes(BfAstNode* refNode, const BfAllocTarget& allocTarget, BfType* type, BfIRValue sizeValue, BfIRValue alignValue, BfAllocFlags allocFlags/*, bool zeroMemory, bool defaultToMalloc*/) { BfIRValue result; @@ -10010,19 +10017,17 @@ void BfModule::ValidateAllocation(BfType* type, BfAstNode* refNode) Fail(StrFormat("Unable to allocate opaque type '%s'", TypeToString(type).c_str()), refNode); } -void BfModule::EmitAlign(BfIRValue& appendCurIdx, int align) -{ - appendCurIdx = mBfIRBuilder->CreateAdd(appendCurIdx, GetConstValue(align - 1)); - appendCurIdx = mBfIRBuilder->CreateAnd(appendCurIdx, GetConstValue(~(align - 1))); -} - void BfModule::EmitAppendAlign(int align, int sizeMultiple) { if (sizeMultiple == 0) sizeMultiple = align; if ((mCurMethodState->mCurAppendAlign != 0) && (mCurMethodState->mCurAppendAlign % align != 0)) { - if (mCurMethodInstance->mMethodDef->mMethodType == BfMethodType_Ctor) + if (!IsAllocatorAligned()) + { + // Don't align + } + else if (mCurMethodInstance->mMethodDef->mMethodType == BfMethodType_Ctor) { auto localVar = mCurMethodState->GetRootMethodState()->mLocals[1]; BF_ASSERT(localVar->mName == "appendIdx"); diff --git a/IDEHelper/Compiler/BfModule.h b/IDEHelper/Compiler/BfModule.h index 71321eba..c86959ff 100644 --- a/IDEHelper/Compiler/BfModule.h +++ b/IDEHelper/Compiler/BfModule.h @@ -1662,7 +1662,7 @@ public: BfVariant TypedValueToVariant(BfAstNode* refNode, const BfTypedValue& value, bool allowUndef = false); BfTypedValue FlushNullConditional(BfTypedValue result, bool ignoreNullable = false); - void NewScopeState(bool createLexicalBlock = true, bool flushValueScope = true); // returns prev scope data + void NewScopeState(bool createLexicalBlock = true, bool flushValueScope = true); // returns prev scope data BfIRValue CreateAlloca(BfType* type, bool addLifetime = true, const char* name = NULL, BfIRValue arraySize = BfIRValue()); BfIRValue CreateAllocaInst(BfTypeInstance* typeInst, bool addLifetime = true, const char* name = NULL); BfDeferredCallEntry* AddStackAlloc(BfTypedValue val, BfIRValue arraySize, BfAstNode* refNode, BfScopeData* scope, bool condAlloca = false, bool mayEscape = false, BfIRBlock valBlock = BfIRBlock()); @@ -1707,13 +1707,13 @@ public: void AssertErrorState(); void AssertParseErrorState(); void InitTypeInst(BfTypedValue typedValue, BfScopeData* scope, bool zeroMemory, BfIRValue dataSize); + bool IsAllocatorAligned(); BfIRValue AllocBytes(BfAstNode* refNode, const BfAllocTarget& allocTarget, BfType* type, BfIRValue sizeValue, BfIRValue alignValue, BfAllocFlags allocFlags/*bool zeroMemory, bool defaultToMalloc*/); BfIRValue GetMarkFuncPtr(BfType* type); BfIRValue GetDbgRawAllocData(BfType* type); BfIRValue AllocFromType(BfType* type, const BfAllocTarget& allocTarget, BfIRValue appendSizeValue = BfIRValue(), BfIRValue arraySize = BfIRValue(), int arrayDim = 0, /*bool isRawArrayAlloc = false, bool zeroMemory = true*/BfAllocFlags allocFlags = BfAllocFlags_ZeroMemory, int alignOverride = -1); void ValidateAllocation(BfType* type, BfAstNode* refNode); - bool IsOptimized(); - void EmitAlign(BfIRValue& appendCurIdx, int align); + bool IsOptimized(); void EmitAppendAlign(int align, int sizeMultiple = 0); BfIRValue AppendAllocFromType(BfType* type, BfIRValue appendSizeValue = BfIRValue(), int appendAllocAlign = 0, BfIRValue arraySize = BfIRValue(), int arrayDim = 0, bool isRawArrayAlloc = false, bool zeroMemory = true); bool IsTargetingBeefBackend();