Skip to content

Commit f361d5c

Browse files
committed
Address some review feedback.
- Introduced `CallbackID` typedef for callback ids rather than a plain int. - Remove unnecessary braces.
1 parent 56ffef4 commit f361d5c

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

llvm/include/llvm/SandboxIR/Context.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class Context {
3434
using MoveInstrCallback =
3535
std::function<void(Instruction *, const BBIterator &)>;
3636

37+
/// An ID for a registered callback. Used for deregistration.
38+
using CallbackID = int;
39+
3740
protected:
3841
LLVMContext &LLVMCtx;
3942
friend class Type; // For LLVMCtx.
@@ -63,18 +66,18 @@ class Context {
6366

6467
/// Callbacks called when an IR instruction is about to get removed. Keys are
6568
/// used as IDs for deregistration.
66-
DenseMap<int, RemoveInstrCallback> RemoveInstrCallbacks;
69+
DenseMap<CallbackID, RemoveInstrCallback> RemoveInstrCallbacks;
6770
/// Callbacks called when an IR instruction is about to get inserted. Keys are
6871
/// used as IDs for deregistration.
69-
DenseMap<int, InsertInstrCallback> InsertInstrCallbacks;
72+
DenseMap<CallbackID, InsertInstrCallback> InsertInstrCallbacks;
7073
/// Callbacks called when an IR instruction is about to get moved. Keys are
7174
/// used as IDs for deregistration.
72-
DenseMap<int, MoveInstrCallback> MoveInstrCallbacks;
75+
DenseMap<CallbackID, MoveInstrCallback> MoveInstrCallbacks;
7376

7477
/// A counter used for assigning callback IDs during registration. The same
7578
/// counter is used for all kinds of callbacks so we can detect mismatched
7679
/// registration/deregistration.
77-
static int NextCallbackId;
80+
static CallbackID NextCallbackID;
7881

7982
/// Remove \p V from the maps and returns the unique_ptr.
8083
std::unique_ptr<Value> detachLLVMValue(llvm::Value *V);

llvm/lib/SandboxIR/Context.cpp

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -664,56 +664,53 @@ Module *Context::createModule(llvm::Module *LLVMM) {
664664
}
665665

666666
void Context::runRemoveInstrCallbacks(Instruction *I) {
667-
for (const auto &CBEntry : RemoveInstrCallbacks) {
667+
for (const auto &CBEntry : RemoveInstrCallbacks)
668668
CBEntry.second(I);
669-
}
670669
}
671670

672671
void Context::runInsertInstrCallbacks(Instruction *I) {
673-
for (auto &CBEntry : InsertInstrCallbacks) {
672+
for (auto &CBEntry : InsertInstrCallbacks)
674673
CBEntry.second(I);
675-
}
676674
}
677675

678676
void Context::runMoveInstrCallbacks(Instruction *I, const BBIterator &WhereIt) {
679-
for (auto &CBEntry : MoveInstrCallbacks) {
677+
for (auto &CBEntry : MoveInstrCallbacks)
680678
CBEntry.second(I, WhereIt);
681-
}
682679
}
683680

684-
int Context::NextCallbackId = 0;
681+
Context::CallbackID Context::NextCallbackID = 0;
685682

686683
int Context::registerRemoveInstrCallback(RemoveInstrCallback CB) {
687-
int Id = NextCallbackId++;
688-
RemoveInstrCallbacks[Id] = CB;
689-
return Id;
684+
CallbackID ID = NextCallbackID++;
685+
RemoveInstrCallbacks[ID] = CB;
686+
return ID;
690687
}
691-
void Context::unregisterRemoveInstrCallback(int CallbackId) {
692-
[[maybe_unused]] bool erased = RemoveInstrCallbacks.erase(CallbackId);
688+
void Context::unregisterRemoveInstrCallback(CallbackID ID) {
689+
[[maybe_unused]] bool erased = RemoveInstrCallbacks.erase(ID);
693690
assert(erased &&
694-
"Callback id not found in RemoveInstrCallbacks during deregistration");
691+
"Callback ID not found in RemoveInstrCallbacks during deregistration");
695692
}
696693

697694
int Context::registerInsertInstrCallback(InsertInstrCallback CB) {
698-
int Id = NextCallbackId++;
699-
InsertInstrCallbacks[Id] = CB;
700-
return Id;
695+
CallbackID ID = NextCallbackID++;
696+
InsertInstrCallbacks[ID] = CB;
697+
return ID;
701698
}
702-
void Context::unregisterInsertInstrCallback(int CallbackId) {
703-
[[maybe_unused]] bool erased = InsertInstrCallbacks.erase(CallbackId);
699+
void Context::unregisterInsertInstrCallback(CallbackID ID) {
700+
[[maybe_unused]] bool erased = InsertInstrCallbacks.erase(ID);
704701
assert(erased &&
705-
"Callback id not found in InsertInstrCallbacks during deregistration");
702+
"Callback ID not found in InsertInstrCallbacks during deregistration");
706703
}
707704

708705
int Context::registerMoveInstrCallback(MoveInstrCallback CB) {
709-
int Id = NextCallbackId++;
710-
MoveInstrCallbacks[Id] = CB;
711-
return Id;
706+
CallbackID ID = NextCallbackID++;
707+
MoveInstrCallbacks[ID] = CB;
708+
return ID;
712709
}
713-
void Context::unregisterMoveInstrCallback(int CallbackId) {
714-
[[maybe_unused]] bool erased = MoveInstrCallbacks.erase(CallbackId);
710+
void Context::unregisterMoveInstrCallback(CallbackID ID) {
711+
[[maybe_unused]] bool erased = MoveInstrCallbacks.erase(ID);
715712
assert(erased &&
716-
"Callback id not found in MoveInstrCallbacks during deregistration");
713+
"Callback ID not found in MoveInstrCallbacks during deregistration");
717714
}
718715

719716
} // namespace llvm::sandboxir

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5980,18 +5980,18 @@ TEST_F(SandboxIRTest, InstructionCallbacks) {
59805980
sandboxir::Instruction *Ret = &BB.front();
59815981

59825982
SmallVector<sandboxir::Instruction *> Inserted;
5983-
int InsertCbId = Ctx.registerInsertInstrCallback(
5983+
auto InsertCbId = Ctx.registerInsertInstrCallback(
59845984
[&Inserted](sandboxir::Instruction *I) { Inserted.push_back(I); });
59855985

59865986
SmallVector<sandboxir::Instruction *> Removed;
5987-
int RemoveCbId = Ctx.registerRemoveInstrCallback(
5987+
auto RemoveCbId = Ctx.registerRemoveInstrCallback(
59885988
[&Removed](sandboxir::Instruction *I) { Removed.push_back(I); });
59895989

59905990
// Keep the moved instruction and the instruction pointed by the Where
59915991
// iterator so we can check both callback arguments work as expected.
59925992
SmallVector<std::pair<sandboxir::Instruction *, sandboxir::Instruction *>>
59935993
Moved;
5994-
int MoveCbId = Ctx.registerMoveInstrCallback(
5994+
auto MoveCbId = Ctx.registerMoveInstrCallback(
59955995
[&Moved](sandboxir::Instruction *I, const sandboxir::BBIterator &Where) {
59965996
// Use a nullptr to signal "move to end" to keep it single. We only
59975997
// have a basic block in this test case anyway.
@@ -6040,7 +6040,7 @@ TEST_F(SandboxIRTest, InstructionCallbacks) {
60406040
Ctx.unregisterRemoveInstrCallback(RemoveCbId);
60416041
Ctx.unregisterMoveInstrCallback(MoveCbId);
60426042
auto *NewI2 = sandboxir::StoreInst::create(Val, Ptr, /*Align=*/std::nullopt,
6043-
Ret->getIterator(), Ctx);
6043+
Ret->getIterator(), Ctx);
60446044
Ret->moveBefore(NewI2);
60456045
Ret->eraseFromParent();
60466046
EXPECT_THAT(Inserted, testing::IsEmpty());

0 commit comments

Comments
 (0)