Commit bd2e2315 by John Porto

Subzero. Uses unique_ptrs in the emit queue.

Because explicit memory ownership is awesome! BUG= R=stichnot@chromium.org Review URL: https://codereview.chromium.org/1804133002 .
parent a78e4baa
...@@ -174,7 +174,9 @@ public: ...@@ -174,7 +174,9 @@ public:
template <typename T = Assembler> T *getAssembler() const { template <typename T = Assembler> T *getAssembler() const {
return llvm::dyn_cast<T>(TargetAssembler.get()); return llvm::dyn_cast<T>(TargetAssembler.get());
} }
Assembler *releaseAssembler() { return TargetAssembler.release(); } std::unique_ptr<Assembler> releaseAssembler() {
return std::move(TargetAssembler);
}
bool hasComputedFrame() const; bool hasComputedFrame() const;
bool getFocusedTiming() const { return FocusedTiming; } bool getFocusedTiming() const { return FocusedTiming; }
void setFocusedTiming() { FocusedTiming = true; } void setFocusedTiming() { FocusedTiming = true; }
......
...@@ -319,14 +319,14 @@ void GlobalContext::translateFunctions() { ...@@ -319,14 +319,14 @@ void GlobalContext::translateFunctions() {
} }
Func->translate(); Func->translate();
EmitterWorkItem *Item = nullptr; std::unique_ptr<EmitterWorkItem> Item;
if (Func->hasError()) { if (Func->hasError()) {
getErrorStatus()->assign(EC_Translation); getErrorStatus()->assign(EC_Translation);
OstreamLocker L(this); OstreamLocker L(this);
getStrError() << "ICE translation error: " << Func->getFunctionName() getStrError() << "ICE translation error: " << Func->getFunctionName()
<< ": " << Func->getError() << ": " << ": " << Func->getError() << ": "
<< Func->getFunctionNameAndSize() << "\n"; << Func->getFunctionNameAndSize() << "\n";
Item = new EmitterWorkItem(Func->getSequenceNumber()); Item = makeUnique<EmitterWorkItem>(Func->getSequenceNumber());
} else { } else {
Func->getAssembler<>()->setInternal(Func->getInternal()); Func->getAssembler<>()->setInternal(Func->getInternal());
switch (getFlags().getOutFileType()) { switch (getFlags().getOutFileType()) {
...@@ -337,10 +337,11 @@ void GlobalContext::translateFunctions() { ...@@ -337,10 +337,11 @@ void GlobalContext::translateFunctions() {
// stats have been fully collected into this thread's TLS. // stats have been fully collected into this thread's TLS.
// Dump them before TLS is reset for the next Cfg. // Dump them before TLS is reset for the next Cfg.
dumpStats(Func->getFunctionNameAndSize()); dumpStats(Func->getFunctionNameAndSize());
Assembler *Asm = Func->releaseAssembler(); auto Asm = Func->releaseAssembler();
// Copy relevant fields into Asm before Func is deleted. // Copy relevant fields into Asm before Func is deleted.
Asm->setFunctionName(Func->getFunctionName()); Asm->setFunctionName(Func->getFunctionName());
Item = new EmitterWorkItem(Func->getSequenceNumber(), Asm); Item = makeUnique<EmitterWorkItem>(Func->getSequenceNumber(),
std::move(Asm));
Item->setGlobalInits(Func->getGlobalInits()); Item->setGlobalInits(Func->getGlobalInits());
} break; } break;
case FT_Asm: case FT_Asm:
...@@ -348,13 +349,14 @@ void GlobalContext::translateFunctions() { ...@@ -348,13 +349,14 @@ void GlobalContext::translateFunctions() {
// to be dumped. // to be dumped.
std::unique_ptr<VariableDeclarationList> GlobalInits = std::unique_ptr<VariableDeclarationList> GlobalInits =
Func->getGlobalInits(); Func->getGlobalInits();
Item = new EmitterWorkItem(Func->getSequenceNumber(), Func.release()); Item = makeUnique<EmitterWorkItem>(Func->getSequenceNumber(),
std::move(Func));
Item->setGlobalInits(std::move(GlobalInits)); Item->setGlobalInits(std::move(GlobalInits));
break; break;
} }
} }
assert(Item); assert(Item != nullptr);
emitQueueBlockingPush(Item); emitQueueBlockingPush(std::move(Item));
// The Cfg now gets deleted as Func goes out of scope. // The Cfg now gets deleted as Func goes out of scope.
} }
} }
...@@ -362,9 +364,10 @@ void GlobalContext::translateFunctions() { ...@@ -362,9 +364,10 @@ void GlobalContext::translateFunctions() {
namespace { namespace {
// Ensure Pending is large enough that Pending[Index] is valid. // Ensure Pending is large enough that Pending[Index] is valid.
void resizePending(std::vector<EmitterWorkItem *> &Pending, uint32_t Index) { void resizePending(std::vector<std::unique_ptr<EmitterWorkItem>> *Pending,
if (Index >= Pending.size()) uint32_t Index) {
Utils::reserveAndResize(Pending, Index + 1); if (Index >= Pending->size())
Utils::reserveAndResize(*Pending, Index + 1);
} }
} // end of anonymous namespace } // end of anonymous namespace
...@@ -471,7 +474,7 @@ void GlobalContext::emitItems() { ...@@ -471,7 +474,7 @@ void GlobalContext::emitItems() {
// the work queue, and if it's not the item we're waiting for, we // the work queue, and if it's not the item we're waiting for, we
// insert it into Pending and repeat. The work item is deleted // insert it into Pending and repeat. The work item is deleted
// after it is processed. // after it is processed.
std::vector<EmitterWorkItem *> Pending; std::vector<std::unique_ptr<EmitterWorkItem>> Pending;
uint32_t DesiredSequenceNumber = getFirstSequenceNumber(); uint32_t DesiredSequenceNumber = getFirstSequenceNumber();
uint32_t ShuffleStartIndex = DesiredSequenceNumber; uint32_t ShuffleStartIndex = DesiredSequenceNumber;
uint32_t ShuffleEndIndex = DesiredSequenceNumber; uint32_t ShuffleEndIndex = DesiredSequenceNumber;
...@@ -483,12 +486,11 @@ void GlobalContext::emitItems() { ...@@ -483,12 +486,11 @@ void GlobalContext::emitItems() {
RandomNumberGenerator RNG(getFlags().getRandomSeed(), RPE_FunctionReordering); RandomNumberGenerator RNG(getFlags().getRandomSeed(), RPE_FunctionReordering);
while (!EmitQueueEmpty) { while (!EmitQueueEmpty) {
resizePending(Pending, DesiredSequenceNumber); resizePending(&Pending, DesiredSequenceNumber);
// See if Pending contains DesiredSequenceNumber. // See if Pending contains DesiredSequenceNumber.
EmitterWorkItem *RawItem = Pending[DesiredSequenceNumber]; if (Pending[DesiredSequenceNumber] == nullptr) {
if (RawItem == nullptr) {
// We need to fetch an EmitterWorkItem from the queue. // We need to fetch an EmitterWorkItem from the queue.
RawItem = emitQueueBlockingPop(); auto RawItem = emitQueueBlockingPop();
if (RawItem == nullptr) { if (RawItem == nullptr) {
// This is the notifier for an empty queue. // This is the notifier for an empty queue.
EmitQueueEmpty = true; EmitQueueEmpty = true;
...@@ -498,16 +500,18 @@ void GlobalContext::emitItems() { ...@@ -498,16 +500,18 @@ void GlobalContext::emitItems() {
if (Threaded && ItemSeq != DesiredSequenceNumber) { if (Threaded && ItemSeq != DesiredSequenceNumber) {
// Not the desired one, add it to Pending but do not increase // Not the desired one, add it to Pending but do not increase
// DesiredSequenceNumber. Continue the loop, do not emit the item. // DesiredSequenceNumber. Continue the loop, do not emit the item.
resizePending(Pending, ItemSeq); resizePending(&Pending, ItemSeq);
Pending[ItemSeq] = RawItem; Pending[ItemSeq] = std::move(RawItem);
continue; continue;
} }
// ItemSeq == DesiredSequenceNumber, we need to check if we should // ItemSeq == DesiredSequenceNumber, we need to check if we should
// emit it or not. If !Threaded, we're OK with ItemSeq != // emit it or not. If !Threaded, we're OK with ItemSeq !=
// DesiredSequenceNumber. // DesiredSequenceNumber.
Pending[DesiredSequenceNumber] = RawItem; Pending[DesiredSequenceNumber] = std::move(RawItem);
} }
} }
const auto *CurrentWorkItem = Pending[DesiredSequenceNumber].get();
// We have the desired EmitterWorkItem or nullptr as the end notifier. // We have the desired EmitterWorkItem or nullptr as the end notifier.
// If the emitter queue is not empty, increase DesiredSequenceNumber and // If the emitter queue is not empty, increase DesiredSequenceNumber and
// ShuffleEndIndex. // ShuffleEndIndex.
...@@ -524,7 +528,7 @@ void GlobalContext::emitItems() { ...@@ -524,7 +528,7 @@ void GlobalContext::emitItems() {
// holding an arbitrarily large GlobalDeclarationList. // holding an arbitrarily large GlobalDeclarationList.
if (!EmitQueueEmpty && if (!EmitQueueEmpty &&
ShuffleEndIndex - ShuffleStartIndex < ShuffleWindowSize && ShuffleEndIndex - ShuffleStartIndex < ShuffleWindowSize &&
RawItem->getKind() != EmitterWorkItem::WI_GlobalInits) CurrentWorkItem->getKind() != EmitterWorkItem::WI_GlobalInits)
continue; continue;
// Emit the EmitterWorkItem between Pending[ShuffleStartIndex] to // Emit the EmitterWorkItem between Pending[ShuffleStartIndex] to
...@@ -538,7 +542,7 @@ void GlobalContext::emitItems() { ...@@ -538,7 +542,7 @@ void GlobalContext::emitItems() {
// Emit the item from ShuffleStartIndex to ShuffleEndIndex. // Emit the item from ShuffleStartIndex to ShuffleEndIndex.
for (uint32_t I = ShuffleStartIndex; I < ShuffleEndIndex; I++) { for (uint32_t I = ShuffleStartIndex; I < ShuffleEndIndex; I++) {
std::unique_ptr<EmitterWorkItem> Item(Pending[I]); std::unique_ptr<EmitterWorkItem> Item = std::move(Pending[I]);
switch (Item->getKind()) { switch (Item->getKind()) {
case EmitterWorkItem::WI_Nop: case EmitterWorkItem::WI_Nop:
...@@ -862,7 +866,7 @@ void GlobalContext::optQueueBlockingPush(std::unique_ptr<Cfg> Func) { ...@@ -862,7 +866,7 @@ void GlobalContext::optQueueBlockingPush(std::unique_ptr<Cfg> Func) {
assert(Func); assert(Func);
{ {
TimerMarker _(TimerStack::TT_qTransPush, this); TimerMarker _(TimerStack::TT_qTransPush, this);
OptQ.blockingPush(Func.release()); OptQ.blockingPush(std::move(Func));
} }
if (getFlags().isSequential()) if (getFlags().isSequential())
translateFunctions(); translateFunctions();
...@@ -873,17 +877,18 @@ std::unique_ptr<Cfg> GlobalContext::optQueueBlockingPop() { ...@@ -873,17 +877,18 @@ std::unique_ptr<Cfg> GlobalContext::optQueueBlockingPop() {
return std::unique_ptr<Cfg>(OptQ.blockingPop()); return std::unique_ptr<Cfg>(OptQ.blockingPop());
} }
void GlobalContext::emitQueueBlockingPush(EmitterWorkItem *Item) { void GlobalContext::emitQueueBlockingPush(
std::unique_ptr<EmitterWorkItem> Item) {
assert(Item); assert(Item);
{ {
TimerMarker _(TimerStack::TT_qEmitPush, this); TimerMarker _(TimerStack::TT_qEmitPush, this);
EmitQ.blockingPush(Item); EmitQ.blockingPush(std::move(Item));
} }
if (getFlags().isSequential()) if (getFlags().isSequential())
emitItems(); emitItems();
} }
EmitterWorkItem *GlobalContext::emitQueueBlockingPop() { std::unique_ptr<EmitterWorkItem> GlobalContext::emitQueueBlockingPop() {
TimerMarker _(TimerStack::TT_qEmitPop, this); TimerMarker _(TimerStack::TT_qEmitPop, this);
return EmitQ.blockingPop(); return EmitQ.blockingPop();
} }
......
...@@ -28,9 +28,11 @@ ...@@ -28,9 +28,11 @@
#include <array> #include <array>
#include <functional> #include <functional>
#include <memory>
#include <mutex> #include <mutex>
#include <thread> #include <thread>
#include <type_traits> #include <type_traits>
#include <utility>
#include <vector> #include <vector>
namespace Ice { namespace Ice {
...@@ -336,8 +338,8 @@ public: ...@@ -336,8 +338,8 @@ public:
void lowerJumpTables(); void lowerJumpTables();
void emitQueueBlockingPush(EmitterWorkItem *Item); void emitQueueBlockingPush(std::unique_ptr<EmitterWorkItem> Item);
EmitterWorkItem *emitQueueBlockingPop(); std::unique_ptr<EmitterWorkItem> emitQueueBlockingPop();
void emitQueueNotifyEnd() { EmitQ.notifyEnd(); } void emitQueueNotifyEnd() { EmitQ.notifyEnd(); }
void initParserThread() { void initParserThread() {
......
...@@ -22,15 +22,16 @@ namespace Ice { ...@@ -22,15 +22,16 @@ namespace Ice {
EmitterWorkItem::EmitterWorkItem(uint32_t Seq) EmitterWorkItem::EmitterWorkItem(uint32_t Seq)
: Sequence(Seq), Kind(WI_Nop), GlobalInits(nullptr), Function(nullptr), : Sequence(Seq), Kind(WI_Nop), GlobalInits(nullptr), Function(nullptr),
RawFunc(nullptr) {} RawFunc(nullptr) {}
EmitterWorkItem::EmitterWorkItem(uint32_t Seq, VariableDeclarationList *D) EmitterWorkItem::EmitterWorkItem(uint32_t Seq,
: Sequence(Seq), Kind(WI_GlobalInits), GlobalInits(D), Function(nullptr), std::unique_ptr<VariableDeclarationList> D)
: Sequence(Seq), Kind(WI_GlobalInits), GlobalInits(std::move(D)),
Function(nullptr), RawFunc(nullptr) {}
EmitterWorkItem::EmitterWorkItem(uint32_t Seq, std::unique_ptr<Assembler> A)
: Sequence(Seq), Kind(WI_Asm), GlobalInits(nullptr), Function(std::move(A)),
RawFunc(nullptr) {} RawFunc(nullptr) {}
EmitterWorkItem::EmitterWorkItem(uint32_t Seq, Assembler *A) EmitterWorkItem::EmitterWorkItem(uint32_t Seq, std::unique_ptr<Cfg> F)
: Sequence(Seq), Kind(WI_Asm), GlobalInits(nullptr), Function(A),
RawFunc(nullptr) {}
EmitterWorkItem::EmitterWorkItem(uint32_t Seq, Cfg *F)
: Sequence(Seq), Kind(WI_Cfg), GlobalInits(nullptr), Function(nullptr), : Sequence(Seq), Kind(WI_Cfg), GlobalInits(nullptr), Function(nullptr),
RawFunc(F) {} RawFunc(std::move(F)) {}
void EmitterWorkItem::setGlobalInits( void EmitterWorkItem::setGlobalInits(
std::unique_ptr<VariableDeclarationList> GloblInits) { std::unique_ptr<VariableDeclarationList> GloblInits) {
......
...@@ -18,7 +18,9 @@ ...@@ -18,7 +18,9 @@
#include "IceDefs.h" #include "IceDefs.h"
#include <condition_variable> #include <condition_variable>
#include <memory>
#include <mutex> #include <mutex>
#include <utility>
namespace Ice { namespace Ice {
...@@ -55,18 +57,18 @@ class BoundedProducerConsumerQueue { ...@@ -55,18 +57,18 @@ class BoundedProducerConsumerQueue {
public: public:
BoundedProducerConsumerQueue(bool Sequential, size_t MaxSize = MaxStaticSize) BoundedProducerConsumerQueue(bool Sequential, size_t MaxSize = MaxStaticSize)
: MaxSize(std::min(MaxSize, MaxStaticSize)), Sequential(Sequential) {} : MaxSize(std::min(MaxSize, MaxStaticSize)), Sequential(Sequential) {}
void blockingPush(T *Item) { void blockingPush(std::unique_ptr<T> Item) {
{ {
std::unique_lock<GlobalLockType> L(Lock); std::unique_lock<GlobalLockType> L(Lock);
// If the work queue is already "full", wait for a consumer to grab an // If the work queue is already "full", wait for a consumer to grab an
// element and shrink the queue. // element and shrink the queue.
Shrunk.wait(L, [this] { return size() < MaxSize || Sequential; }); Shrunk.wait(L, [this] { return size() < MaxSize || Sequential; });
push(Item); push(std::move(Item));
} }
GrewOrEnded.notify_one(); GrewOrEnded.notify_one();
} }
T *blockingPop() { std::unique_ptr<T> blockingPop() {
T *Item = nullptr; std::unique_ptr<T> Item;
bool ShouldNotifyProducer = false; bool ShouldNotifyProducer = false;
{ {
std::unique_lock<GlobalLockType> L(Lock); std::unique_lock<GlobalLockType> L(Lock);
...@@ -95,7 +97,7 @@ private: ...@@ -95,7 +97,7 @@ private:
ICE_CACHELINE_BOUNDARY; ICE_CACHELINE_BOUNDARY;
/// WorkItems and Lock are read/written by all. /// WorkItems and Lock are read/written by all.
T *WorkItems[MaxStaticSize]; std::unique_ptr<T> WorkItems[MaxStaticSize];
ICE_CACHELINE_BOUNDARY; ICE_CACHELINE_BOUNDARY;
/// Lock guards access to WorkItems, Front, Back, and IsEnded. /// Lock guards access to WorkItems, Front, Back, and IsEnded.
GlobalLockType Lock; GlobalLockType Lock;
...@@ -131,13 +133,13 @@ private: ...@@ -131,13 +133,13 @@ private:
/// The lock must be held when the following methods are called. /// The lock must be held when the following methods are called.
bool empty() const { return Front == Back; } bool empty() const { return Front == Back; }
size_t size() const { return Back - Front; } size_t size() const { return Back - Front; }
void push(T *Item) { void push(std::unique_ptr<T> Item) {
WorkItems[Back++ & MaxStaticSizeMask] = Item; WorkItems[Back++ & MaxStaticSizeMask] = std::move(Item);
assert(size() <= MaxStaticSize); assert(size() <= MaxStaticSize);
} }
T *pop() { std::unique_ptr<T> pop() {
assert(!empty()); assert(!empty());
return WorkItems[Front++ & MaxStaticSizeMask]; return std::move(WorkItems[Front++ & MaxStaticSizeMask]);
} }
}; };
...@@ -174,11 +176,11 @@ public: ...@@ -174,11 +176,11 @@ public:
/// Constructor for a WI_Nop work item. /// Constructor for a WI_Nop work item.
explicit EmitterWorkItem(uint32_t Seq); explicit EmitterWorkItem(uint32_t Seq);
/// Constructor for a WI_GlobalInits work item. /// Constructor for a WI_GlobalInits work item.
EmitterWorkItem(uint32_t Seq, VariableDeclarationList *D); EmitterWorkItem(uint32_t Seq, std::unique_ptr<VariableDeclarationList> D);
/// Constructor for a WI_Asm work item. /// Constructor for a WI_Asm work item.
EmitterWorkItem(uint32_t Seq, Assembler *A); EmitterWorkItem(uint32_t Seq, std::unique_ptr<Assembler> A);
/// Constructor for a WI_Cfg work item. /// Constructor for a WI_Cfg work item.
EmitterWorkItem(uint32_t Seq, Cfg *F); EmitterWorkItem(uint32_t Seq, std::unique_ptr<Cfg> F);
uint32_t getSequenceNumber() const { return Sequence; } uint32_t getSequenceNumber() const { return Sequence; }
ItemKind getKind() const { return Kind; } ItemKind getKind() const { return Kind; }
void setGlobalInits(std::unique_ptr<VariableDeclarationList> GloblInits); void setGlobalInits(std::unique_ptr<VariableDeclarationList> GloblInits);
......
...@@ -14,13 +14,15 @@ ...@@ -14,13 +14,15 @@
#include "IceTranslator.h" #include "IceTranslator.h"
#include "IceDefs.h"
#include "IceCfg.h" #include "IceCfg.h"
#include "IceClFlags.h" #include "IceClFlags.h"
#include "IceDefs.h"
#include "IceGlobalInits.h" #include "IceGlobalInits.h"
#include "IceTargetLowering.h" #include "IceTargetLowering.h"
using namespace Ice; #include <utility>
namespace Ice {
Translator::Translator(GlobalContext *Ctx) Translator::Translator(GlobalContext *Ctx)
: Ctx(Ctx), NextSequenceNumber(GlobalContext::getFirstSequenceNumber()), : Ctx(Ctx), NextSequenceNumber(GlobalContext::getFirstSequenceNumber()),
...@@ -58,7 +60,8 @@ void Translator::translateFcn(std::unique_ptr<Cfg> Func) { ...@@ -58,7 +60,8 @@ void Translator::translateFcn(std::unique_ptr<Cfg> Func) {
void Translator::lowerGlobals( void Translator::lowerGlobals(
std::unique_ptr<VariableDeclarationList> VariableDeclarations) { std::unique_ptr<VariableDeclarationList> VariableDeclarations) {
EmitterWorkItem *Item = new EmitterWorkItem(getNextSequenceNumber(), Ctx->emitQueueBlockingPush(makeUnique<EmitterWorkItem>(
VariableDeclarations.release()); getNextSequenceNumber(), std::move(VariableDeclarations)));
Ctx->emitQueueBlockingPush(Item);
} }
} // end of namespace Ice
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
#include "IceDefs.h" #include "IceDefs.h"
#include "IceGlobalContext.h" #include "IceGlobalContext.h"
#include <memory>
namespace llvm { namespace llvm {
class Module; class Module;
} // end of namespace llvm } // end of namespace llvm
...@@ -48,8 +50,7 @@ public: ...@@ -48,8 +50,7 @@ public:
const ClFlags &getFlags() const { return Ctx->getFlags(); } const ClFlags &getFlags() const { return Ctx->getFlags(); }
/// Translates the constructed ICE function Fcn to machine code. Takes /// Translates the constructed ICE function Func to machine code.
/// ownership of Func.
void translateFcn(std::unique_ptr<Cfg> Func); void translateFcn(std::unique_ptr<Cfg> Func);
/// Lowers the given list of global addresses to target. Generates list of /// Lowers the given list of global addresses to target. Generates list of
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment