[llvm] [SandboxIR][Doc] Add Quick start notes (PR #123992)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 22 11:30:11 PST 2025


https://github.com/vporpo created https://github.com/llvm/llvm-project/pull/123992

None

>From 5c2d4ff94ab6bff5255bc1ed91f03db0db531ce5 Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vporpodas at google.com>
Date: Tue, 21 Jan 2025 14:12:55 -0800
Subject: [PATCH] [SandboxIR][Doc] Add Quick start notes

---
 llvm/docs/SandboxIR.md | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/llvm/docs/SandboxIR.md b/llvm/docs/SandboxIR.md
index 3b792659bb59ba..61bae4e36ef435 100644
--- a/llvm/docs/SandboxIR.md
+++ b/llvm/docs/SandboxIR.md
@@ -2,6 +2,41 @@
 
 Sandbox IR is an IR layer on top of LLVM IR that allows you to save/restore its state.
 
+# Quick Start Notes
+
+Within your LLVM pass:
+
+```
+// 1. Include the necessary Sandbox IR header files.
+#include "llvm/SandboxIR/Context.h
+#include "llvm/SandboxIR/Function.h
+
+// 2. Create a sandboxir::Context using LLVMContext `LLVMCtx`.
+sandboxir::Context Ctx(LLVMCtx);
+
+// 3. Create a sandboxir::Function using LLVM IR Function `LLVMF`.
+auto *F = Ctx.createFunction(LLVMF);
+
+// ... Use Sandbox IR in `F` as usual, e.g., iterating, modifying it etc. ...
+
+// 4. Save state when needed.
+Ctx.save();
+
+// ... Modify Sandbox IR ...
+
+// 5. Revert to the saved state.
+Ctx.revert();
+```
+
+Make sure you link against `SandboxIR` in `CMakeLists.txt`:
+
+```
+LINK_COMPONENTS
+...
+SandboxIR
+...
+```
+
 # API
 The Sandbox IR API is designed to feel like LLVM, replicating many common API classes and functions to mirror the LLVM API.
 The class hierarchy is similar (but in the `llvm::sandboxir` namespace).



More information about the llvm-commits mailing list