[llvm] [SandboxIR] Add callbacks for instruction insert/remove/move ops. (PR #112965)

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 18 14:00:26 PDT 2024


================
@@ -660,4 +663,54 @@ Module *Context::createModule(llvm::Module *LLVMM) {
   return M;
 }
 
+void Context::runRemoveInstrCallbacks(Instruction *I) {
+  for (const auto &CBEntry : RemoveInstrCallbacks)
+    CBEntry.second(I);
+}
+
+void Context::runInsertInstrCallbacks(Instruction *I) {
+  for (auto &CBEntry : InsertInstrCallbacks)
+    CBEntry.second(I);
+}
+
+void Context::runMoveInstrCallbacks(Instruction *I, const BBIterator &WhereIt) {
+  for (auto &CBEntry : MoveInstrCallbacks)
+    CBEntry.second(I, WhereIt);
+}
+
+Context::CallbackID Context::NextCallbackID = 0;
+
+int Context::registerRemoveInstrCallback(RemoveInstrCallback CB) {
+  CallbackID ID = NextCallbackID++;
+  RemoveInstrCallbacks[ID] = CB;
+  return ID;
+}
+void Context::unregisterRemoveInstrCallback(CallbackID ID) {
+  [[maybe_unused]] bool erased = RemoveInstrCallbacks.erase(ID);
+  assert(erased &&
+         "Callback ID not found in RemoveInstrCallbacks during deregistration");
+}
+
+int Context::registerInsertInstrCallback(InsertInstrCallback CB) {
+  CallbackID ID = NextCallbackID++;
+  InsertInstrCallbacks[ID] = CB;
+  return ID;
+}
+void Context::unregisterInsertInstrCallback(CallbackID ID) {
+  [[maybe_unused]] bool erased = InsertInstrCallbacks.erase(ID);
----------------
aeubanks wrote:

```suggestion
  [[maybe_unused]] bool Erased = InsertInstrCallbacks.erase(ID);
```

https://github.com/llvm/llvm-project/pull/112965


More information about the llvm-commits mailing list