[llvm] [SandboxIR] Adds BasicBlock and adds functionality to Function and Context (PR #97637)

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 8 10:46:05 PDT 2024


================
@@ -171,9 +208,194 @@ void Function::dump() const {
 }
 #endif // NDEBUG
 
+BasicBlock::iterator::pointer
+BasicBlock::iterator::getI(llvm::BasicBlock::iterator It) const {
+  return cast_or_null<Instruction>(Ctx->getValue(&*It));
+}
+
+Value *Context::registerValue(std::unique_ptr<Value> &&VPtr) {
+  assert(VPtr->getSubclassID() != Value::ClassID::User &&
+         "Can't register a user!");
+  Value *V = VPtr.get();
+  llvm::Value *Key = V->Val;
+  LLVMValueToValueMap[Key] = std::move(VPtr);
+  return V;
+}
+
+Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, int Depth,
+                                         llvm::User *U) {
+  assert(Depth < 1048576 && "Infinite recursion?");
+  auto Pair = LLVMValueToValueMap.insert({LLVMV, nullptr});
+  auto It = Pair.first;
+  if (!Pair.second)
+    return It->second.get();
+
+  if (auto *C = dyn_cast<llvm::Constant>(LLVMV)) {
+    // Globals may be self-referencing, like @bar = global [1 x ptr] [ptr @bar].
+    // Avoid infinite loops by early returning once we detect a loop.
+    if (isa<GlobalValue>(C)) {
+      if (Depth == 0)
----------------
aeubanks wrote:

if `getOrCreateValueInternal` is only called from `getOrCreateValue`, maybe construct `VisitedConstants` in `getOrCreateValue` and pass it around as a `getOrCreateValueInternal` param? then no need for `Depth` param. it would also be helpful to check this for all values and not just constants in the future if we start looking through phis

on second thought, why do we need this if we're already eagerly adding to `LLVMValueToValueMap` before recursing? won't we see the nullptr we added earlier when we revisit a self-referencing global?

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


More information about the llvm-commits mailing list