Skip to content

[SandboxIR][NFC] GenericSetter tracker class #102260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 31 additions & 76 deletions llvm/include/llvm/SandboxIR/Tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,71 +239,45 @@ class RemoveFromParent : public IRChangeBase {
#endif // NDEBUG
};

class CallBrInstSetDefaultDest : public IRChangeBase {
CallBrInst *CallBr;
BasicBlock *OrigDefaultDest;

public:
CallBrInstSetDefaultDest(CallBrInst *CallBr, Tracker &Tracker);
void revert() final;
void accept() final {}
#ifndef NDEBUG
void dump(raw_ostream &OS) const final {
dumpCommon(OS);
OS << "CallBrInstSetDefaultDest";
}
LLVM_DUMP_METHOD void dump() const final;
#endif
};

class AllocaSetAllocatedType final : public IRChangeBase {
AllocaInst *Alloca;
Type *OrigType;

public:
AllocaSetAllocatedType(AllocaInst *Alloca, Tracker &Tracker);
void revert() final;
void accept() final {}
#ifndef NDEBUG
void dump(raw_ostream &OS) const final {
dumpCommon(OS);
OS << "AllocaSetAllocatedType";
}
LLVM_DUMP_METHOD void dump() const final;
#endif
};

class AllocaSetAlignment final : public IRChangeBase {
AllocaInst *Alloca;
Align OrigAlign;
/// This class can be used for tracking most instruction setters.
/// The two template arguments are:
/// - GetterFn: The getter member function pointer (e.g., `&Foo::get`)
/// - SetterFn: The setter member function pointer (e.g., `&Foo::set`)
/// Upon construction, it saves a copy of the original value by calling the
/// getter function. Revert sets the value back to the one saved, using the
/// setter function provided.
///
/// Example:
/// Tracker.track(std::make_unique<
/// GenericSetter<&FooInst::get, &FooInst::set>>(I, Tracker));
///
template <auto GetterFn, auto SetterFn>
class GenericSetter final : public IRChangeBase {
/// Helper for getting the class type from the getter
template <typename ClassT, typename RetT>
static ClassT getClassTypeFromGetter(RetT (ClassT::*Fn)() const);
template <typename ClassT, typename RetT>
static ClassT getClassTypeFromGetter(RetT (ClassT::*Fn)());

using InstrT = decltype(getClassTypeFromGetter(GetterFn));
using SavedValT = std::invoke_result_t<decltype(GetterFn), InstrT>;
InstrT *I;
SavedValT OrigVal;

public:
AllocaSetAlignment(AllocaInst *Alloca, Tracker &Tracker);
void revert() final;
GenericSetter(InstrT *I, Tracker &Tracker)
: IRChangeBase(Tracker), I(I), OrigVal((I->*GetterFn)()) {}
void revert() final { (I->*SetterFn)(OrigVal); }
void accept() final {}
#ifndef NDEBUG
void dump(raw_ostream &OS) const final {
dumpCommon(OS);
OS << "AllocaSetAlignment";
OS << "GenericSetter";
}
LLVM_DUMP_METHOD void dump() const final;
#endif
};

class AllocaSetUsedWithInAlloca final : public IRChangeBase {
AllocaInst *Alloca;
bool Orig;

public:
AllocaSetUsedWithInAlloca(AllocaInst *Alloca, Tracker &Tracker);
void revert() final;
void accept() final {}
#ifndef NDEBUG
void dump(raw_ostream &OS) const final {
dumpCommon(OS);
OS << "AllocaSetUsedWithInAlloca";
LLVM_DUMP_METHOD void dump() const final {
dump(dbgs());
dbgs() << "\n";
}
LLVM_DUMP_METHOD void dump() const final;
#endif
};

Expand All @@ -325,25 +299,6 @@ class CallBrInstSetIndirectDest : public IRChangeBase {
#endif
};

class SetVolatile : public IRChangeBase {
/// This holds the properties of whether LoadInst or StoreInst was volatile
bool WasVolatile;
/// This could either be StoreInst or LoadInst
PointerUnion<StoreInst *, LoadInst *> StoreOrLoad;

public:
SetVolatile(Instruction *I, Tracker &Tracker);
void revert() final;
void accept() final {}
#ifndef NDEBUG
void dump(raw_ostream &OS) const final {
dumpCommon(OS);
OS << "SetVolatile";
}
LLVM_DUMP_METHOD void dump() const final;
#endif
};

class MoveInstr : public IRChangeBase {
/// The instruction that moved.
Instruction *MovedI;
Expand Down
46 changes: 34 additions & 12 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,11 @@ void BranchInst::dump() const {

void LoadInst::setVolatile(bool V) {
auto &Tracker = Ctx.getTracker();
if (Tracker.isTracking())
Tracker.track(std::make_unique<SetVolatile>(this, Tracker));
if (Tracker.isTracking()) {
Tracker.track(std::make_unique<
GenericSetter<&LoadInst::isVolatile, &LoadInst::setVolatile>>(
this, Tracker));
}
cast<llvm::LoadInst>(Val)->setVolatile(V);
}

Expand Down Expand Up @@ -686,8 +689,12 @@ void LoadInst::dump() const {

void StoreInst::setVolatile(bool V) {
auto &Tracker = Ctx.getTracker();
if (Tracker.isTracking())
Tracker.track(std::make_unique<SetVolatile>(this, Tracker));
if (Tracker.isTracking()) {
Tracker.track(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multi-line ifs should have braces

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the ones related to this patch, but there are plenty more. I will fix them separately.

std::make_unique<
GenericSetter<&StoreInst::isVolatile, &StoreInst::setVolatile>>(
this, Tracker));
}
cast<llvm::StoreInst>(Val)->setVolatile(V);
}

Expand Down Expand Up @@ -1007,8 +1014,11 @@ llvm::SmallVector<BasicBlock *, 16> CallBrInst::getIndirectDests() const {
}
void CallBrInst::setDefaultDest(BasicBlock *BB) {
auto &Tracker = Ctx.getTracker();
if (Tracker.isTracking())
Tracker.track(std::make_unique<CallBrInstSetDefaultDest>(this, Tracker));
if (Tracker.isTracking()) {
Tracker.track(std::make_unique<GenericSetter<&CallBrInst::getDefaultDest,
&CallBrInst::setDefaultDest>>(
this, Tracker));
}
cast<llvm::CallBrInst>(Val)->setDefaultDest(cast<llvm::BasicBlock>(BB->Val));
}
void CallBrInst::setIndirectDest(unsigned Idx, BasicBlock *BB) {
Expand Down Expand Up @@ -1260,22 +1270,34 @@ AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace,

void AllocaInst::setAllocatedType(Type *Ty) {
auto &Tracker = Ctx.getTracker();
if (Tracker.isTracking())
Tracker.track(std::make_unique<AllocaSetAllocatedType>(this, Tracker));
if (Tracker.isTracking()) {
Tracker.track(
std::make_unique<GenericSetter<&AllocaInst::getAllocatedType,
&AllocaInst::setAllocatedType>>(
this, Tracker));
}
cast<llvm::AllocaInst>(Val)->setAllocatedType(Ty);
}

void AllocaInst::setAlignment(Align Align) {
auto &Tracker = Ctx.getTracker();
if (Tracker.isTracking())
Tracker.track(std::make_unique<AllocaSetAlignment>(this, Tracker));
if (Tracker.isTracking()) {
Tracker.track(
std::make_unique<
GenericSetter<&AllocaInst::getAlign, &AllocaInst::setAlignment>>(
this, Tracker));
}
cast<llvm::AllocaInst>(Val)->setAlignment(Align);
}

void AllocaInst::setUsedWithInAlloca(bool V) {
auto &Tracker = Ctx.getTracker();
if (Tracker.isTracking())
Tracker.track(std::make_unique<AllocaSetUsedWithInAlloca>(this, Tracker));
if (Tracker.isTracking()) {
Tracker.track(
std::make_unique<GenericSetter<&AllocaInst::isUsedWithInAlloca,
&AllocaInst::setUsedWithInAlloca>>(
this, Tracker));
}
cast<llvm::AllocaInst>(Val)->setUsedWithInAlloca(V);
}

Expand Down
84 changes: 0 additions & 84 deletions llvm/lib/SandboxIR/Tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,61 +204,6 @@ void RemoveFromParent::dump() const {
}
#endif

AllocaSetAllocatedType::AllocaSetAllocatedType(AllocaInst *Alloca,
Tracker &Tracker)
: IRChangeBase(Tracker), Alloca(Alloca),
OrigType(Alloca->getAllocatedType()) {}

void AllocaSetAllocatedType::revert() { Alloca->setAllocatedType(OrigType); }

#ifndef NDEBUG
void AllocaSetAllocatedType::dump() const {
dump(dbgs());
dbgs() << "\n";
}
#endif // NDEBUG

AllocaSetAlignment::AllocaSetAlignment(AllocaInst *Alloca, Tracker &Tracker)
: IRChangeBase(Tracker), Alloca(Alloca), OrigAlign(Alloca->getAlign()) {}

void AllocaSetAlignment::revert() { Alloca->setAlignment(OrigAlign); }

#ifndef NDEBUG
void AllocaSetAlignment::dump() const {
dump(dbgs());
dbgs() << "\n";
}
#endif // NDEBUG

AllocaSetUsedWithInAlloca::AllocaSetUsedWithInAlloca(AllocaInst *Alloca,
Tracker &Tracker)
: IRChangeBase(Tracker), Alloca(Alloca),
Orig(Alloca->isUsedWithInAlloca()) {}

void AllocaSetUsedWithInAlloca::revert() { Alloca->setUsedWithInAlloca(Orig); }

#ifndef NDEBUG
void AllocaSetUsedWithInAlloca::dump() const {
dump(dbgs());
dbgs() << "\n";
}
#endif // NDEBUG

CallBrInstSetDefaultDest::CallBrInstSetDefaultDest(CallBrInst *CallBr,
Tracker &Tracker)
: IRChangeBase(Tracker), CallBr(CallBr) {
OrigDefaultDest = CallBr->getDefaultDest();
}
void CallBrInstSetDefaultDest::revert() {
CallBr->setDefaultDest(OrigDefaultDest);
}
#ifndef NDEBUG
void CallBrInstSetDefaultDest::dump() const {
dump(dbgs());
dbgs() << "\n";
}
#endif

CallBrInstSetIndirectDest::CallBrInstSetIndirectDest(CallBrInst *CallBr,
unsigned Idx,
Tracker &Tracker)
Expand All @@ -275,35 +220,6 @@ void CallBrInstSetIndirectDest::dump() const {
}
#endif

SetVolatile::SetVolatile(Instruction *I, Tracker &Tracker)
: IRChangeBase(Tracker) {
if (auto *Load = dyn_cast<LoadInst>(I)) {
WasVolatile = Load->isVolatile();
StoreOrLoad = Load;
} else if (auto *Store = dyn_cast<StoreInst>(I)) {
WasVolatile = Store->isVolatile();
StoreOrLoad = Store;
} else {
llvm_unreachable("Expected LoadInst or StoreInst");
}
}

void SetVolatile::revert() {
if (auto *Load = StoreOrLoad.dyn_cast<LoadInst *>()) {
Load->setVolatile(WasVolatile);
} else if (auto *Store = StoreOrLoad.dyn_cast<StoreInst *>()) {
Store->setVolatile(WasVolatile);
} else {
llvm_unreachable("Expected LoadInst or StoreInst");
}
}
#ifndef NDEBUG
void SetVolatile::dump() const {
dump(dbgs());
dbgs() << "\n";
}
#endif

MoveInstr::MoveInstr(Instruction *MovedI, Tracker &Tracker)
: IRChangeBase(Tracker), MovedI(MovedI) {
if (auto *NextI = MovedI->getNextNode())
Expand Down
Loading