[llvm] [examples] Set target triple on modules in Kaleidoscope. (PR #229666)

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 7 03:13:38 PDT 2026


https://github.com/lhames updated https://github.com/llvm/llvm-project/pull/229666

>From 9d4570f6e924fb213e640309eb6c10a09f653f5e Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Wed, 7 Oct 2026 15:56:51 +1100
Subject: [PATCH] [examples] Set target triple on modules in Kaleidoscope.

If no triple is set then the object format component of the Module's
triple will be set by getDefaultFormat in Triple.cpp, typically to ELF.
In AArch64AsmPrinter::emitStartOfAsmFile, the "ELF" default will cause
control to fall through the following check:

  if (!TT.isOSBinFormatELF())
    return;

and into a region that assumes an MCTargetStreamer. On Darwin/arm64,
which does not set an MCTargetStreamer, this will cause a nullptr
access.

Fix the issue by setting the Module's triple to the host triple as
reported by the KaleidoscopeJIT object.

rdar://189147321
---
 .../Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h        | 2 ++
 llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp        | 1 +
 .../Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h        | 2 ++
 llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp        | 1 +
 .../Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h        | 2 ++
 llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp        | 1 +
 .../Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h        | 2 ++
 llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp        | 1 +
 llvm/examples/Kaleidoscope/Chapter4/toy.cpp                     | 1 +
 llvm/examples/Kaleidoscope/Chapter5/toy.cpp                     | 1 +
 llvm/examples/Kaleidoscope/Chapter6/toy.cpp                     | 1 +
 llvm/examples/Kaleidoscope/Chapter7/toy.cpp                     | 1 +
 llvm/examples/Kaleidoscope/Chapter9/toy.cpp                     | 1 +
 llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h            | 2 ++
 14 files changed, 19 insertions(+)

diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h
index 7800dca5fd3756..59f131a7d542dc 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/KaleidoscopeJIT.h
@@ -82,6 +82,8 @@ class KaleidoscopeJIT {
                                              std::move(*DL));
   }
 
+  const Triple &getTargetTriple() const { return ES->getTargetTriple(); }
+
   const DataLayout &getDataLayout() const { return DL; }
 
   JITDylib &getMainJITDylib() { return MainJD; }
diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
index 0d6d03989f9282..e43dcb85e868b3 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
@@ -1106,6 +1106,7 @@ static void InitializeModule() {
   // Open a new context and module.
   TheContext = std::make_unique<LLVMContext>();
   TheModule = std::make_unique<Module>("my cool jit", *TheContext);
+  TheModule->setTargetTriple(TheJIT->getTargetTriple());
   TheModule->setDataLayout(TheJIT->getDataLayout());
 
   // Create a new builder for the module.
diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h
index 1789ab1e20fef6..0772f389eed54b 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/KaleidoscopeJIT.h
@@ -89,6 +89,8 @@ class KaleidoscopeJIT {
                                              std::move(*DL));
   }
 
+  const Triple &getTargetTriple() const { return ES->getTargetTriple(); }
+
   const DataLayout &getDataLayout() const { return DL; }
 
   JITDylib &getMainJITDylib() { return MainJD; }
diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp
index 0d6d03989f9282..e43dcb85e868b3 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp
@@ -1106,6 +1106,7 @@ static void InitializeModule() {
   // Open a new context and module.
   TheContext = std::make_unique<LLVMContext>();
   TheModule = std::make_unique<Module>("my cool jit", *TheContext);
+  TheModule->setTargetTriple(TheJIT->getTargetTriple());
   TheModule->setDataLayout(TheJIT->getDataLayout());
 
   // Create a new builder for the module.
diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h
index 028c3afe8e9c60..5c0a0359bfa907 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/KaleidoscopeJIT.h
@@ -134,6 +134,8 @@ class KaleidoscopeJIT {
         std::move(*MemAccess), std::move(JTMB), std::move(*DL));
   }
 
+  const Triple &getTargetTriple() const { return ES->getTargetTriple(); }
+
   const DataLayout &getDataLayout() const { return DL; }
 
   JITDylib &getMainJITDylib() { return MainJD; }
diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp
index 0d6d03989f9282..e43dcb85e868b3 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp
@@ -1106,6 +1106,7 @@ static void InitializeModule() {
   // Open a new context and module.
   TheContext = std::make_unique<LLVMContext>();
   TheModule = std::make_unique<Module>("my cool jit", *TheContext);
+  TheModule->setTargetTriple(TheJIT->getTargetTriple());
   TheModule->setDataLayout(TheJIT->getDataLayout());
 
   // Create a new builder for the module.
diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h
index e4933c0ef6870f..1db29dd0385c14 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/KaleidoscopeJIT.h
@@ -217,6 +217,8 @@ class KaleidoscopeJIT {
         std::move(*MemAccess), std::move(JTMB), std::move(*DL));
   }
 
+  const Triple &getTargetTriple() const { return ES->getTargetTriple(); }
+
   const DataLayout &getDataLayout() const { return DL; }
 
   JITDylib &getMainJITDylib() { return MainJD; }
diff --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp
index bbdb7907722fa9..97cf1dfbb8f262 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp
@@ -1097,6 +1097,7 @@ static void InitializeModule() {
   // Open a new context and module.
   TheContext = std::make_unique<LLVMContext>();
   TheModule = std::make_unique<Module>("my cool jit", *TheContext);
+  TheModule->setTargetTriple(TheJIT->getTargetTriple());
   TheModule->setDataLayout(TheJIT->getDataLayout());
 
   // Create a new builder for the module.
diff --git a/llvm/examples/Kaleidoscope/Chapter4/toy.cpp b/llvm/examples/Kaleidoscope/Chapter4/toy.cpp
index 0f58391c506679..4dd2cd047f71f6 100644
--- a/llvm/examples/Kaleidoscope/Chapter4/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter4/toy.cpp
@@ -563,6 +563,7 @@ static void InitializeModuleAndManagers() {
   // Open a new context and module.
   TheContext = std::make_unique<LLVMContext>();
   TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
+  TheModule->setTargetTriple(TheJIT->getTargetTriple());
   TheModule->setDataLayout(TheJIT->getDataLayout());
 
   // Create a new builder for the module.
diff --git a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
index 7117eaf4982b03..4d208de188dab3 100644
--- a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
@@ -837,6 +837,7 @@ static void InitializeModuleAndManagers() {
   // Open a new context and module.
   TheContext = std::make_unique<LLVMContext>();
   TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
+  TheModule->setTargetTriple(TheJIT->getTargetTriple());
   TheModule->setDataLayout(TheJIT->getDataLayout());
 
   // Create a new builder for the module.
diff --git a/llvm/examples/Kaleidoscope/Chapter6/toy.cpp b/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
index cb7b6cc8651c13..963750523285e4 100644
--- a/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
@@ -956,6 +956,7 @@ static void InitializeModuleAndManagers() {
   // Open a new context and module.
   TheContext = std::make_unique<LLVMContext>();
   TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
+  TheModule->setTargetTriple(TheJIT->getTargetTriple());
   TheModule->setDataLayout(TheJIT->getDataLayout());
 
   // Create a new builder for the module.
diff --git a/llvm/examples/Kaleidoscope/Chapter7/toy.cpp b/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
index 9db44640f1c520..e6efa4e1b43a8a 100644
--- a/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
@@ -1125,6 +1125,7 @@ static void InitializeModuleAndManagers() {
   // Open a new context and module.
   TheContext = std::make_unique<LLVMContext>();
   TheModule = std::make_unique<Module>("KaleidoscopeJIT", *TheContext);
+  TheModule->setTargetTriple(TheJIT->getTargetTriple());
   TheModule->setDataLayout(TheJIT->getDataLayout());
 
   // Create a new builder for the module.
diff --git a/llvm/examples/Kaleidoscope/Chapter9/toy.cpp b/llvm/examples/Kaleidoscope/Chapter9/toy.cpp
index 14081fb3c3b10a..7c0175f9e35ab1 100644
--- a/llvm/examples/Kaleidoscope/Chapter9/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter9/toy.cpp
@@ -1324,6 +1324,7 @@ static void InitializeModule() {
   // Open a new module.
   TheContext = std::make_unique<LLVMContext>();
   TheModule = std::make_unique<Module>("my cool jit", *TheContext);
+  TheModule->setTargetTriple(TheJIT->getTargetTriple());
   TheModule->setDataLayout(TheJIT->getDataLayout());
 
   Builder = std::make_unique<IRBuilder<>>(*TheContext);
diff --git a/llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h b/llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h
index 1fc89fca1e1da7..853144429518e7 100644
--- a/llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h
+++ b/llvm/examples/Kaleidoscope/include/KaleidoscopeJIT.h
@@ -87,6 +87,8 @@ class KaleidoscopeJIT {
                                              std::move(*DL));
   }
 
+  const Triple &getTargetTriple() const { return ES->getTargetTriple(); }
+
   const DataLayout &getDataLayout() const { return DL; }
 
   JITDylib &getMainJITDylib() { return MainJD; }



More information about the llvm-commits mailing list