Browse Source

Create memory helper functions on demand

cl-refactor
Paweł Bylica 10 years ago
parent
commit
3fe31f0b80
  1. 171
      libevmjit/Memory.cpp
  2. 15
      libevmjit/Memory.h

171
libevmjit/Memory.cpp

@ -26,78 +26,77 @@ namespace jit
Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter): Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter):
RuntimeHelper(_runtimeManager), // TODO: RuntimeHelper not needed RuntimeHelper(_runtimeManager), // TODO: RuntimeHelper not needed
m_gasMeter(_gasMeter) m_gasMeter(_gasMeter)
{ {}
llvm::Type* resizeArgs[] = {Type::RuntimePtr, Type::WordPtr};
m_resize = llvm::Function::Create(llvm::FunctionType::get(Type::BytePtr, resizeArgs, false), llvm::Function::ExternalLinkage, "mem_resize", getModule());
llvm::AttrBuilder attrBuilder;
attrBuilder.addAttribute(llvm::Attribute::NoAlias).addAttribute(llvm::Attribute::NoCapture).addAttribute(llvm::Attribute::NonNull).addAttribute(llvm::Attribute::ReadOnly);
m_resize->setAttributes(llvm::AttributeSet::get(m_resize->getContext(), 1, attrBuilder));
m_require = createRequireFunc(_gasMeter);
m_loadWord = createFunc(false, Type::Word, _gasMeter);
m_storeWord = createFunc(true, Type::Word, _gasMeter);
m_storeByte = createFunc(true, Type::Byte, _gasMeter);
}
llvm::Function* Memory::createRequireFunc(GasMeter& _gasMeter) llvm::Function* Memory::getRequireFunc()
{ {
llvm::Type* argTypes[] = {Type::RuntimePtr, Type::Word, Type::Word}; auto& func = m_require;
auto func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::PrivateLinkage, "mem.require", getModule()); if (!func)
auto rt = func->arg_begin(); {
rt->setName("rt"); llvm::Type* argTypes[] = {Type::RuntimePtr, Type::Word, Type::Word};
auto offset = rt->getNextNode(); func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::PrivateLinkage, "mem.require", getModule());
offset->setName("offset"); auto rt = func->arg_begin();
auto size = offset->getNextNode(); rt->setName("rt");
size->setName("size"); auto offset = rt->getNextNode();
offset->setName("offset");
auto preBB = llvm::BasicBlock::Create(func->getContext(), "Pre", func); auto size = offset->getNextNode();
auto checkBB = llvm::BasicBlock::Create(func->getContext(), "Check", func); size->setName("size");
auto resizeBB = llvm::BasicBlock::Create(func->getContext(), "Resize", func);
auto returnBB = llvm::BasicBlock::Create(func->getContext(), "Return", func); llvm::Type* resizeArgs[] = {Type::RuntimePtr, Type::WordPtr};
auto resize = llvm::Function::Create(llvm::FunctionType::get(Type::BytePtr, resizeArgs, false), llvm::Function::ExternalLinkage, "mem_resize", getModule());
InsertPointGuard guard(m_builder); // Restores insert point at function exit llvm::AttrBuilder attrBuilder;
attrBuilder.addAttribute(llvm::Attribute::NoAlias).addAttribute(llvm::Attribute::NoCapture).addAttribute(llvm::Attribute::NonNull).addAttribute(llvm::Attribute::ReadOnly);
// BB "Pre": Ignore checks with size 0 resize->setAttributes(llvm::AttributeSet::get(resize->getContext(), 1, attrBuilder));
m_builder.SetInsertPoint(preBB);
auto sizeIsZero = m_builder.CreateICmpEQ(size, Constant::get(0)); auto preBB = llvm::BasicBlock::Create(func->getContext(), "Pre", func);
m_builder.CreateCondBr(sizeIsZero, returnBB, checkBB); auto checkBB = llvm::BasicBlock::Create(func->getContext(), "Check", func);
auto resizeBB = llvm::BasicBlock::Create(func->getContext(), "Resize", func);
// BB "Check" auto returnBB = llvm::BasicBlock::Create(func->getContext(), "Return", func);
m_builder.SetInsertPoint(checkBB);
auto uaddWO = llvm::Intrinsic::getDeclaration(getModule(), llvm::Intrinsic::uadd_with_overflow, Type::Word); InsertPointGuard guard(m_builder); // Restores insert point at function exit
auto uaddRes = m_builder.CreateCall2(uaddWO, offset, size, "res");
auto sizeRequired = m_builder.CreateExtractValue(uaddRes, 0, "sizeReq"); // BB "Pre": Ignore checks with size 0
auto overflow1 = m_builder.CreateExtractValue(uaddRes, 1, "overflow1"); m_builder.SetInsertPoint(preBB);
auto rtPtr = getRuntimeManager().getRuntimePtr(); auto sizeIsZero = m_builder.CreateICmpEQ(size, Constant::get(0));
auto sizePtr = m_builder.CreateStructGEP(rtPtr, 4); m_builder.CreateCondBr(sizeIsZero, returnBB, checkBB);
auto currSize = m_builder.CreateLoad(sizePtr, "currSize");
auto tooSmall = m_builder.CreateICmpULE(currSize, sizeRequired, "tooSmall"); // BB "Check"
auto resizeNeeded = m_builder.CreateOr(tooSmall, overflow1, "resizeNeeded"); m_builder.SetInsertPoint(checkBB);
m_builder.CreateCondBr(resizeNeeded, resizeBB, returnBB); // OPT branch weights? auto uaddWO = llvm::Intrinsic::getDeclaration(getModule(), llvm::Intrinsic::uadd_with_overflow, Type::Word);
auto uaddRes = m_builder.CreateCall2(uaddWO, offset, size, "res");
// BB "Resize" auto sizeRequired = m_builder.CreateExtractValue(uaddRes, 0, "sizeReq");
m_builder.SetInsertPoint(resizeBB); auto overflow1 = m_builder.CreateExtractValue(uaddRes, 1, "overflow1");
// Check gas first auto rtPtr = getRuntimeManager().getRuntimePtr();
uaddRes = m_builder.CreateCall2(uaddWO, sizeRequired, Constant::get(31), "res"); auto sizePtr = m_builder.CreateStructGEP(rtPtr, 4);
auto wordsRequired = m_builder.CreateExtractValue(uaddRes, 0); auto currSize = m_builder.CreateLoad(sizePtr, "currSize");
auto overflow2 = m_builder.CreateExtractValue(uaddRes, 1, "overflow2"); auto tooSmall = m_builder.CreateICmpULE(currSize, sizeRequired, "tooSmall");
auto overflow = m_builder.CreateOr(overflow1, overflow2, "overflow"); auto resizeNeeded = m_builder.CreateOr(tooSmall, overflow1, "resizeNeeded");
wordsRequired = m_builder.CreateSelect(overflow, Constant::get(-1), wordsRequired); m_builder.CreateCondBr(resizeNeeded, resizeBB, returnBB); // OPT branch weights?
wordsRequired = m_builder.CreateUDiv(wordsRequired, Constant::get(32), "wordsReq");
sizeRequired = m_builder.CreateMul(wordsRequired, Constant::get(32), "roundedSizeReq"); // BB "Resize"
auto words = m_builder.CreateUDiv(currSize, Constant::get(32), "words"); // size is always 32*k m_builder.SetInsertPoint(resizeBB);
auto newWords = m_builder.CreateSub(wordsRequired, words, "addtionalWords"); // Check gas first
_gasMeter.countMemory(newWords); uaddRes = m_builder.CreateCall2(uaddWO, sizeRequired, Constant::get(31), "res");
// Resize auto wordsRequired = m_builder.CreateExtractValue(uaddRes, 0);
m_builder.CreateStore(sizeRequired, sizePtr); auto overflow2 = m_builder.CreateExtractValue(uaddRes, 1, "overflow2");
auto newData = m_builder.CreateCall2(m_resize, rt, sizePtr, "newData"); auto overflow = m_builder.CreateOr(overflow1, overflow2, "overflow");
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); wordsRequired = m_builder.CreateSelect(overflow, Constant::get(-1), wordsRequired);
m_builder.CreateStore(newData, dataPtr); wordsRequired = m_builder.CreateUDiv(wordsRequired, Constant::get(32), "wordsReq");
m_builder.CreateBr(returnBB); sizeRequired = m_builder.CreateMul(wordsRequired, Constant::get(32), "roundedSizeReq");
auto words = m_builder.CreateUDiv(currSize, Constant::get(32), "words"); // size is always 32*k
// BB "Return" auto newWords = m_builder.CreateSub(wordsRequired, words, "addtionalWords");
m_builder.SetInsertPoint(returnBB); m_gasMeter.countMemory(newWords);
m_builder.CreateRetVoid(); // Resize
m_builder.CreateStore(sizeRequired, sizePtr);
auto newData = m_builder.CreateCall2(resize, rt, sizePtr, "newData");
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3);
m_builder.CreateStore(newData, dataPtr);
m_builder.CreateBr(returnBB);
// BB "Return"
m_builder.SetInsertPoint(returnBB);
m_builder.CreateRetVoid();
}
return func; return func;
} }
@ -143,21 +142,45 @@ llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, GasMet
return func; return func;
} }
llvm::Function* Memory::getLoadWordFunc()
{
auto& func = m_loadWord;
if (!func)
func = createFunc(false, Type::Word, m_gasMeter);
return func;
}
llvm::Function* Memory::getStoreWordFunc()
{
auto& func = m_storeWord;
if (!func)
func = createFunc(true, Type::Word, m_gasMeter);
return func;
}
llvm::Function* Memory::getStoreByteFunc()
{
auto& func = m_storeByte;
if (!func)
func = createFunc(true, Type::Byte, m_gasMeter);
return func;
}
llvm::Value* Memory::loadWord(llvm::Value* _addr) llvm::Value* Memory::loadWord(llvm::Value* _addr)
{ {
return createCall(m_loadWord, {getRuntimeManager().getRuntimePtr(), _addr}); return createCall(getLoadWordFunc(), {getRuntimeManager().getRuntimePtr(), _addr});
} }
void Memory::storeWord(llvm::Value* _addr, llvm::Value* _word) void Memory::storeWord(llvm::Value* _addr, llvm::Value* _word)
{ {
createCall(m_storeWord, {getRuntimeManager().getRuntimePtr(), _addr, _word}); createCall(getStoreWordFunc(), {getRuntimeManager().getRuntimePtr(), _addr, _word});
} }
void Memory::storeByte(llvm::Value* _addr, llvm::Value* _word) void Memory::storeByte(llvm::Value* _addr, llvm::Value* _word)
{ {
auto byte = m_builder.CreateTrunc(_word, Type::Byte, "byte"); auto byte = m_builder.CreateTrunc(_word, Type::Byte, "byte");
createCall(m_storeByte, {getRuntimeManager().getRuntimePtr(), _addr, byte}); createCall(getStoreByteFunc(), {getRuntimeManager().getRuntimePtr(), _addr, byte});
} }
llvm::Value* Memory::getData() llvm::Value* Memory::getData()
@ -187,7 +210,7 @@ void Memory::require(llvm::Value* _offset, llvm::Value* _size)
if (!constant->getValue()) if (!constant->getValue())
return; return;
} }
createCall(m_require, {getRuntimeManager().getRuntimePtr(), _offset, _size}); createCall(getRequireFunc(), {getRuntimeManager().getRuntimePtr(), _offset, _size});
} }
void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx, void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx,

15
libevmjit/Memory.h

@ -31,13 +31,16 @@ private:
GasMeter& m_gasMeter; GasMeter& m_gasMeter;
llvm::Function* createFunc(bool _isStore, llvm::Type* _type, GasMeter& _gasMeter); llvm::Function* createFunc(bool _isStore, llvm::Type* _type, GasMeter& _gasMeter);
llvm::Function* createRequireFunc(GasMeter& _gasMeter);
llvm::Function* m_resize; llvm::Function* getRequireFunc();
llvm::Function* m_require; llvm::Function* getLoadWordFunc();
llvm::Function* m_loadWord; llvm::Function* getStoreWordFunc();
llvm::Function* m_storeWord; llvm::Function* getStoreByteFunc();
llvm::Function* m_storeByte;
llvm::Function* m_require = nullptr;
llvm::Function* m_loadWord = nullptr;
llvm::Function* m_storeWord = nullptr;
llvm::Function* m_storeByte = nullptr;
}; };
} }

Loading…
Cancel
Save