[llvm] [Kaleidoscope] Update tutorial ch4 to match example code document (PR #172395)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 15 16:49:51 PST 2025
https://github.com/Crivens6 created https://github.com/llvm/llvm-project/pull/172395
The assert line has been removed as it does not compile and is not present in the code document. References to the assert in the tutorial have also been removed.
ExprSymbol.getAddress().toPtr<double (*)()>(); has been changed to
ExprSymbol.toPtr<double (*)()>(); to match the code document's use of newer API. Reference to the getAdress() function has also been removed.
>From 2409e3681ef80a1c250b1cf4b38be683a0554c88 Mon Sep 17 00:00:00 2001
From: Christyllin <DiskworldWizzard at Gmail.com>
Date: Fri, 12 Dec 2025 17:44:24 -0800
Subject: [PATCH] [Kaleidoscope] Update tutorial ch4 to match example code
document
The line
assert(ExprSymbol && "Function not found");
has been removed as it does not compile and is not present in the code
document. References to the assert in the tutorial have also been removed.
double (*FP)() = ExprSymbol.getAddress().toPtr<double (*)()>();
has been changed to
double (*FP)() = ExprSymbol.toPtr<double (*)()>();
to match the code document's use of newer API. Reference to the
getAdress() function has also been removed.
---
.../MyFirstLanguageFrontend/LangImpl04.rst | 29 +++++++++----------
1 file changed, 13 insertions(+), 16 deletions(-)
diff --git a/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl04.rst b/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl04.rst
index 5ebff3b0474b1..9f6f8539b04de 100644
--- a/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl04.rst
+++ b/llvm/docs/tutorial/MyFirstLanguageFrontend/LangImpl04.rst
@@ -341,11 +341,10 @@ look like this:
// Search the JIT for the __anon_expr symbol.
auto ExprSymbol = ExitOnErr(TheJIT->lookup("__anon_expr"));
- assert(ExprSymbol && "Function not found");
// Get the symbol's address and cast it to the right type (takes no
// arguments, returns a double) so we can call it as a native function.
- double (*FP)() = ExprSymbol.getAddress().toPtr<double (*)()>();
+ double (*FP)() = ExprSymbol.toPtr<double (*)()>();
fprintf(stderr, "Evaluated to %f\n", FP());
// Delete the anonymous expression module from the JIT.
@@ -355,23 +354,21 @@ look like this:
If parsing and codegen succeed, the next step is to add the module containing
the top-level expression to the JIT. We do this by calling addModule, which
triggers code generation for all the functions in the module, and accepts a
-``ResourceTracker`` which can be used to remove the module from the JIT later. Once the module
-has been added to the JIT it can no longer be modified, so we also open a new
-module to hold subsequent code by calling ``InitializeModuleAndPassManager()``.
+``ResourceTracker`` which can be used to remove the module from the JIT later. Once
+the module has been added to the JIT it can no longer be modified, so we also open a
+new module to hold subsequent code by calling ``InitializeModuleAndPassManager()``.
Once we've added the module to the JIT we need to get a pointer to the final
generated code. We do this by calling the JIT's ``lookup`` method, and passing
-the name of the top-level expression function: ``__anon_expr``. Since we just
-added this function, we assert that ``lookup`` returned a result.
-
-Next, we get the in-memory address of the ``__anon_expr`` function by calling
-``getAddress()`` on the symbol. Recall that we compile top-level expressions
-into a self-contained LLVM function that takes no arguments and returns the
-computed double. Because the LLVM JIT compiler matches the native platform ABI,
-this means that you can just cast the result pointer to a function pointer of
-that type and call it directly. This means, there is no difference between JIT
-compiled code and native machine code that is statically linked into your
-application.
+the name of the top-level expression function: ``__anon_expr``.
+
+Next, we get the in-memory address of the ``__anon_expr`` function. Recall
+that we compile top-level expressions into a self-contained LLVM function that
+takes no arguments and returns the computed double. Because the LLVM JIT compiler
+matches the native platform ABI, this means that you can just cast the result pointer
+to a function pointer of that type and call it directly. This means, there is no
+difference between JIT compiled code and native machine code that is statically
+linked into your application.
Finally, since we don't support re-evaluation of top-level expressions, we
remove the module from the JIT when we're done to free the associated memory.
More information about the llvm-commits
mailing list