From 9c5e648bf8a1ce1a0d301f63764add192db95d33 Mon Sep 17 00:00:00 2001 From: Vasileios Porpodas Date: Thu, 8 Aug 2024 15:08:43 -0700 Subject: [PATCH] fixup! [SandboxIR] Clean up tracking code with the help of tryTrack() --- llvm/include/llvm/SandboxIR/Tracker.h | 7 +++-- llvm/lib/SandboxIR/SandboxIR.cpp | 44 +++++++++++++-------------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/llvm/include/llvm/SandboxIR/Tracker.h b/llvm/include/llvm/SandboxIR/Tracker.h index 930b78eb236e04c..c95af75cfa5a2bf 100644 --- a/llvm/include/llvm/SandboxIR/Tracker.h +++ b/llvm/include/llvm/SandboxIR/Tracker.h @@ -392,12 +392,13 @@ class Tracker { #endif } /// A convenience wrapper for `track()` that constructs and tracks the Change - /// object if tracking is enabled. + /// object if tracking is enabled. \Returns true if tracking is enabled. template - void tryTrack(ArgsT... ChangeArgs) { + bool emplaceIfTracking(ArgsT... ChangeArgs) { if (!isTracking()) - return; + return false; track(std::make_unique(ChangeArgs..., *this)); + return true; } /// \Returns true if the tracker is recording changes. bool isTracking() const { return State == TrackerState::Record; } diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp index 2e337f7ba162966..6fddd132fe99326 100644 --- a/llvm/lib/SandboxIR/SandboxIR.cpp +++ b/llvm/lib/SandboxIR/SandboxIR.cpp @@ -18,14 +18,14 @@ using namespace llvm::sandboxir; Value *Use::get() const { return Ctx->getValue(LLVMUse->get()); } void Use::set(Value *V) { - Ctx->getTracker().tryTrack(*this); + Ctx->getTracker().emplaceIfTracking(*this); LLVMUse->set(V->Val); } unsigned Use::getOperandNo() const { return Usr->getUseOperandNo(*this); } void Use::swap(Use &OtherUse) { - Ctx->getTracker().tryTrack(*this, OtherUse); + Ctx->getTracker().emplaceIfTracking(*this, OtherUse); LLVMUse->swap(*OtherUse.LLVMUse); } @@ -148,7 +148,7 @@ void Value::replaceUsesWithIf( Use UseToReplace(&LLVMUse, DstU, Ctx); if (!ShouldReplace(UseToReplace)) return false; - Ctx.getTracker().tryTrack(UseToReplace); + Ctx.getTracker().emplaceIfTracking(UseToReplace); return true; }); } @@ -251,7 +251,7 @@ bool User::classof(const Value *From) { void User::setOperand(unsigned OperandIdx, Value *Operand) { assert(isa(Val) && "No operands!"); - Ctx.getTracker().tryTrack(getOperandUse(OperandIdx)); + Ctx.getTracker().emplaceIfTracking(getOperandUse(OperandIdx)); // We are delegating to llvm::User::setOperand(). cast(Val)->setOperand(OperandIdx, Operand->Val); } @@ -262,7 +262,7 @@ bool User::replaceUsesOfWith(Value *FromV, Value *ToV) { for (auto OpIdx : seq(0, getNumOperands())) { auto Use = getOperandUse(OpIdx); if (Use.get() == FromV) - Tracker.tryTrack(Use); + Tracker.emplaceIfTracking(Use); } } // We are delegating RUOW to LLVM IR's RUOW. @@ -356,7 +356,7 @@ Instruction *Instruction::getPrevNode() const { } void Instruction::removeFromParent() { - Ctx.getTracker().tryTrack(this); + Ctx.getTracker().emplaceIfTracking(this); // Detach all the LLVM IR instructions from their parent BB. for (llvm::Instruction *I : getLLVMInstrs()) @@ -393,7 +393,7 @@ void Instruction::moveBefore(BasicBlock &BB, const BBIterator &WhereIt) { // Destination is same as origin, nothing to do. return; - Ctx.getTracker().tryTrack(this); + Ctx.getTracker().emplaceIfTracking(this); auto *LLVMBB = cast(BB.Val); llvm::BasicBlock::iterator It; @@ -419,7 +419,7 @@ void Instruction::insertBefore(Instruction *BeforeI) { [](auto *I1, auto *I2) { return I1->comesBefore(I2); }) && "Expected program order!"); - Ctx.getTracker().tryTrack(this); + Ctx.getTracker().emplaceIfTracking(this); // Insert the LLVM IR Instructions in program order. for (llvm::Instruction *I : getLLVMInstrs()) @@ -445,7 +445,7 @@ void Instruction::insertInto(BasicBlock *BB, const BBIterator &WhereIt) { LLVMBeforeIt = LLVMBB->end(); } - Ctx.getTracker().tryTrack(this); + Ctx.getTracker().emplaceIfTracking(this); // Insert the LLVM IR Instructions in program order. for (llvm::Instruction *I : getLLVMInstrs()) @@ -610,8 +610,8 @@ void BranchInst::dump() const { void LoadInst::setVolatile(bool V) { Ctx.getTracker() - .tryTrack>( - this); + .emplaceIfTracking< + GenericSetter<&LoadInst::isVolatile, &LoadInst::setVolatile>>(this); cast(Val)->setVolatile(V); } @@ -672,8 +672,8 @@ void LoadInst::dump() const { void StoreInst::setVolatile(bool V) { Ctx.getTracker() - .tryTrack>( - this); + .emplaceIfTracking< + GenericSetter<&StoreInst::isVolatile, &StoreInst::setVolatile>>(this); cast(Val)->setVolatile(V); } @@ -1026,12 +1026,12 @@ llvm::SmallVector CallBrInst::getIndirectDests() const { } void CallBrInst::setDefaultDest(BasicBlock *BB) { Ctx.getTracker() - .tryTrack>(this); + .emplaceIfTracking>(this); cast(Val)->setDefaultDest(cast(BB->Val)); } void CallBrInst::setIndirectDest(unsigned Idx, BasicBlock *BB) { - Ctx.getTracker().tryTrack(this, Idx); + Ctx.getTracker().emplaceIfTracking(this, Idx); cast(Val)->setIndirectDest(Idx, cast(BB->Val)); } @@ -1276,14 +1276,14 @@ AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace, void AllocaInst::setAllocatedType(Type *Ty) { Ctx.getTracker() - .tryTrack>(this); + .emplaceIfTracking>(this); cast(Val)->setAllocatedType(Ty); } void AllocaInst::setAlignment(Align Align) { Ctx.getTracker() - .tryTrack< + .emplaceIfTracking< GenericSetter<&AllocaInst::getAlign, &AllocaInst::setAlignment>>( this); cast(Val)->setAlignment(Align); @@ -1291,8 +1291,8 @@ void AllocaInst::setAlignment(Align Align) { void AllocaInst::setUsedWithInAlloca(bool V) { Ctx.getTracker() - .tryTrack>(this); + .emplaceIfTracking>(this); cast(Val)->setUsedWithInAlloca(V); } @@ -1465,7 +1465,7 @@ Value *Context::registerValue(std::unique_ptr &&VPtr) { // meaning that the instructions need to be inserted into a block upon // creation. This is why the tracker class combines creation and insertion. if (auto *I = dyn_cast(VPtr.get())) - getTracker().tryTrack(I); + getTracker().emplaceIfTracking(I); Value *V = VPtr.get(); [[maybe_unused]] auto Pair =