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

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 8 12:40:32 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)
----------------
vporpo wrote:

Yeah I don't think this hack is needed now. To give you some context, in the original code the lookup into `LLVMValueToValueMap` was done within the `getOrCreate*()` functions, not at the top of `getOrCreateValueInternal()`. So this hack was used to avoid a second lookup into the map for all types except Constants.

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


More information about the llvm-commits mailing list