1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-09 20:12:21 +02:00

Fixed duplicate global variables when used as default args

This commit is contained in:
Brian Fiete 2021-02-11 06:48:51 -08:00
parent 503590cea5
commit fb0bace727
4 changed files with 73 additions and 60 deletions

View file

@ -615,17 +615,7 @@ BfIRValue BfIRConstHolder::CreateConst(BfConstant* fromConst, BfIRConstHolder* f
else if (fromConst->mConstType == BfConstType_GlobalVar)
{
auto fromGlobalVar = (BfGlobalVar*)fromConst;
auto constGV = mTempAlloc.Alloc<BfGlobalVar>();
chunkId = mTempAlloc.GetChunkedId(constGV);
constGV->mStreamId = -1;
constGV->mConstType = BfConstType_GlobalVar;
constGV->mType = fromGlobalVar->mType;
constGV->mIsConst = fromGlobalVar->mIsConst;
constGV->mLinkageType = fromGlobalVar->mLinkageType;
constGV->mInitializer = fromGlobalVar->mInitializer;
constGV->mName = AllocStr(fromGlobalVar->mName);
constGV->mIsTLS = fromGlobalVar->mIsTLS;
copiedConst = (BfConstant*)constGV;
return CreateGlobalVariableConstant(fromGlobalVar->mType, fromGlobalVar->mIsConst, fromGlobalVar->mLinkageType, fromGlobalVar->mInitializer, fromGlobalVar->mName, fromGlobalVar->mIsTLS);
}
else if (fromConst->mConstType == BfConstType_GEP32_2)
{
@ -1875,6 +1865,7 @@ void BfIRBuilder::ClearConstData()
mTempAlloc.Clear();
mConstMemMap.Clear();
mFunctionMap.Clear();
mGlobalVarMap.Clear();
BF_ASSERT(mMethodTypeMap.GetCount() == 0);
BF_ASSERT(mTypeMap.GetCount() == 0);
BF_ASSERT(mDITemporaryTypes.size() == 0);
@ -1889,6 +1880,7 @@ void BfIRBuilder::ClearNonConstData()
{
mMethodTypeMap.Clear();
mFunctionMap.Clear();
mGlobalVarMap.Clear();
mTypeMap.Clear();
mConstMemMap.Clear();
mDITemporaryTypes.Clear();
@ -2996,11 +2988,6 @@ void BfIRBuilder::CreateDbgTypeDefinition(BfType* type)
if (fieldInstance->mConstIdx != -1)
{
if (fieldInstance->GetFieldDef()->mName == "mMembers")
{
NOP;
}
constant = typeInstance->mConstHolder->GetConstantById(fieldInstance->mConstIdx);
staticValue = mModule->ConstantToCurrent(constant, typeInstance->mConstHolder, resolvedFieldType);
}
@ -4743,8 +4730,32 @@ BfIRValue BfIRBuilder::CreateStackRestore(BfIRValue stackVal)
return retVal;
}
BfIRValue BfIRBuilder::CreateGlobalVariable(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS)
void BfIRBuilder::CreateGlobalVariable(BfIRValue irValue)
{
auto globalVar = (BfGlobalVar*)GetConstant(irValue);
if (!mIgnoreWrites)
{
BF_ASSERT(globalVar->mStreamId == -1);
if (globalVar->mInitializer)
mHasGlobalDefs = true;
BfIRValue retVal = WriteCmd(BfIRCmd_GlobalVariable, globalVar->mType, globalVar->mIsConst, (uint8)globalVar->mLinkageType, String(globalVar->mName), globalVar->mIsTLS, globalVar->mInitializer);
globalVar->mStreamId = retVal.mId;
NEW_CMD_INSERTED_IRVALUE;
}
}
BfIRValue BfIRConstHolder::CreateGlobalVariableConstant(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS)
{
BfIRValue* valuePtr = NULL;
if ((!mGlobalVarMap.TryAdd(name, NULL, &valuePtr)) && (!initializer))
{
return *valuePtr;
}
BF_ASSERT(varType);
auto constGV = mTempAlloc.Alloc<BfGlobalVar>();
@ -4758,23 +4769,15 @@ BfIRValue BfIRBuilder::CreateGlobalVariable(BfIRType varType, bool isConstant, B
constGV->mName = AllocStr(name);
constGV->mIsTLS = isTLS;
if (!mIgnoreWrites)
{
if (initializer)
mHasGlobalDefs = true;
auto irValue = BfIRValue(BfIRValueFlags_Const, chunkId);;
*valuePtr = irValue;
return irValue;
}
BfIRValue retVal = WriteCmd(BfIRCmd_GlobalVariable, varType, isConstant, (uint8)linkageType, name, isTLS, initializer);
constGV->mStreamId = retVal.mId;
retVal.mFlags = BfIRValueFlags_Const;
#ifdef CHECK_CONSTHOLDER
retVal.mHolder = this;
#endif
retVal.mId = chunkId;
NEW_CMD_INSERTED_IRVALUE;
return retVal;
}
auto irValue = BfIRValue(BfIRValueFlags_Const, chunkId);
BfIRValue BfIRBuilder::CreateGlobalVariable(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS)
{
auto irValue = CreateGlobalVariableConstant(varType, isConstant, linkageType, initializer, name);
CreateGlobalVariable(irValue);
return irValue;
}

View file

@ -901,6 +901,7 @@ class BfIRConstHolder
public:
BumpAllocatorT<256> mTempAlloc;
BfModule* mModule;
Dictionary<String, BfIRValue> mGlobalVarMap;
public:
void FixTypeCode(BfTypeCode& typeCode);
@ -936,6 +937,7 @@ public:
BfIRValue CreateTypeOf(BfType* type);
BfIRValue CreateTypeOf(BfType* type, BfIRValue typeData);
BfIRValue GetUndefConstValue(BfIRType type);
BfIRValue CreateGlobalVariableConstant(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS = false);
bool WriteConstant(BfIRValue val, void* ptr, BfType* type);
BfIRValue ReadConstant(void* ptr, BfType* type);
@ -1227,6 +1229,7 @@ public:
BfIRValue CreateStackRestore(BfIRValue stackVal);
BfIRValue CreateGlobalVariable(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS = false);
void CreateGlobalVariable(BfIRValue irValue);
void GlobalVar_SetUnnamedAddr(BfIRValue val, bool unnamedAddr);
void GlobalVar_SetInitializer(BfIRValue globalVar, BfIRValue initVal);
void GlobalVar_SetAlignment(BfIRValue globalVar, int alignment);

View file

@ -809,6 +809,9 @@ void BfIRCodeGen::Read(llvm::Value*& llvmValue, BfIRCodeGenEntry** codeGenEntry,
llvm::GlobalVariable* globalVariable = mLLVMModule->getGlobalVariable(name.c_str(), true);
if (globalVariable == NULL)
{
globalVariable = mLLVMModule->getGlobalVariable(name.c_str());
if (globalVariable == NULL)
{
globalVariable = new llvm::GlobalVariable(
*mLLVMModule,
@ -818,6 +821,7 @@ void BfIRCodeGen::Read(llvm::Value*& llvmValue, BfIRCodeGenEntry** codeGenEntry,
initializer,
name.c_str(), NULL, isTLS ? llvm::GlobalValue::GeneralDynamicTLSModel : llvm::GlobalValue::NotThreadLocal);
}
}
llvmValue = globalVariable;
SetResult(streamId, globalVariable);
@ -2390,13 +2394,17 @@ void BfIRCodeGen::HandleNextCmd()
CMD_PARAM(bool, isTLS);
CMD_PARAM(llvm::Constant*, initializer);
auto globalVariable = new llvm::GlobalVariable(
auto globalVariable = mLLVMModule->getGlobalVariable(name.c_str());
if (globalVariable == NULL)
{
globalVariable = new llvm::GlobalVariable(
*mLLVMModule,
varType,
isConstant,
LLVMMapLinkageType(linkageType),
initializer,
name.c_str(), NULL, isTLS ? llvm::GlobalValue::GeneralDynamicTLSModel : llvm::GlobalValue::NotThreadLocal);
}
SetResult(curId, globalVariable);
}
break;

View file

@ -13722,6 +13722,10 @@ BfTypedValue BfModule::ReferenceStaticField(BfFieldInstance* fieldInstance)
{
globalValue = *globalValuePtr;
BF_ASSERT(globalValue);
auto globalVar = (BfGlobalVar*)mBfIRBuilder->GetConstant(globalValue);
if ((globalVar->mStreamId == -1) && (!mBfIRBuilder->mIgnoreWrites))
mBfIRBuilder->CreateGlobalVariable(globalValue);
}
else
{
@ -13754,15 +13758,10 @@ BfTypedValue BfModule::ReferenceStaticField(BfFieldInstance* fieldInstance)
staticVarName,
IsThreadLocal(fieldInstance));
if (!mBfIRBuilder->mIgnoreWrites)
{
// Only store this if we actually did the creation
BF_ASSERT(globalValue);
mStaticFieldRefs[fieldInstance] = globalValue;
}
BfLogSysM("Mod:%p Type:%p ReferenceStaticField %p -> %p\n", this, fieldInstance->mOwner, fieldInstance, globalValue);
}
}