[llvm] [llubi] Extract reusable methods of InstExecutor into ExecutorAPI (PR #186976)

Zhige Chen via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 26 08:02:12 PDT 2026


================
@@ -0,0 +1,98 @@
+//===--- ExecutorAPI.h - Non-visitor methods of InstExecutor --------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements non-visitor methods of InstExecutor for code reuse.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_LLUBI_EXECUTORAPI_H
+#define LLVM_TOOLS_LLUBI_EXECUTORAPI_H
+
+#include "Context.h"
+#include "Value.h"
+
+namespace llvm::ubi {
+
+enum class FrameState {
+  // It is about to enter the function.
+  // Valid transition:
+  //   -> Running
+  Entry,
+  // It is executing instructions inside the function.
+  // Valid transitions:
+  //   -> Pending (on call)
+  //   -> Exit (on return)
+  Running,
+  // It is about to enter a callee or handle return value from the callee.
+  // Valid transitions:
+  //   -> Running (after returning from callee)
+  Pending,
+  // It is about to return the control to the caller.
+  Exit,
+};
+
+/// Context for a function call.
+/// This struct maintains the state during the execution of a function,
+/// including the control flow, values of executed instructions, and stack
+/// objects.
+struct Frame {
+  Function &Func;
+  Frame *LastFrame;
+  CallBase *CallSite;
+  ArrayRef<AnyValue> Args;
+  AnyValue &RetVal;
+
+  TargetLibraryInfo TLI;
+  BasicBlock *BB;
+  BasicBlock::iterator PC;
+  FrameState State = FrameState::Entry;
+  // Stack objects allocated in this frame. They will be automatically freed
+  // when the function returns.
+  SmallVector<IntrusiveRefCntPtr<MemoryObject>> Allocas;
+  // Values of arguments and executed instructions in this function.
+  DenseMap<Value *, AnyValue> ValueMap;
+
+  // Reserved for in-flight subroutines.
+  Function *ResolvedCallee = nullptr;
+  SmallVector<AnyValue> CalleeArgs;
+  AnyValue CalleeRetVal;
+
+  Frame(Function &F, CallBase *CallSite, Frame *LastFrame,
+        ArrayRef<AnyValue> Args, AnyValue &RetVal,
+        const TargetLibraryInfoImpl &TLIImpl);
+};
+
+class ExecutorAPI {
+protected:
+  Context &Ctx;
+  EventHandler &Handler;
+  // Used to indicate whether the interpreter should continue execution.
+  bool Status;
----------------
nofe1248 wrote:

Actually I plan to add a new method requestProgramExit along with some relevant changes to support libcall implementation. Since currently we don't distinguish between termination caused by a call to exit/abort/terminate, and termination resulting from immediate UBs. If this change is feasible, I can implement it in this PR.

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


More information about the llvm-commits mailing list