[llvm] llvm-exegesis: Set the module triple and ABI-aware DataLayout (PR #228447)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 2 07:08:10 PDT 2026


https://github.com/arsenm created https://github.com/llvm/llvm-project/pull/228447

The module used to assemble snippets only had a DataLayout set, which
was computed without the target ABI. Set the target triple on the
module, synthesize the "target-abi" module flag from the
TargetMachine's ABI option, and compute the DataLayout from it.
Without the ABI, a TargetMachine with a non-default ABI would hit the
incompatible DataLayout error when creating the MachineFunction.

Co-Authored-By: Claude <noreply at anthropic.com>

>From 045d6f51629ace329db006dc3bf146856b0ff6c8 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Fri, 2 Oct 2026 15:54:53 +0200
Subject: [PATCH] llvm-exegesis: Set the module triple and ABI-aware DataLayout

The module used to assemble snippets only had a DataLayout set, which
was computed without the target ABI. Set the target triple on the
module, synthesize the "target-abi" module flag from the
TargetMachine's ABI option, and compute the DataLayout from it.
Without the ABI, a TargetMachine with a non-default ABI would hit the
incompatible DataLayout error when creating the MachineFunction.

Co-Authored-By: Claude <noreply at anthropic.com>
---
 llvm/tools/llvm-exegesis/lib/Assembler.cpp    | 22 ++++++++++------
 .../llvm-exegesis/Common/AssemblerUtils.h     | 25 +++++++++++++++++++
 .../tools/llvm-exegesis/Mips/TargetTest.cpp   |  5 ++++
 .../tools/llvm-exegesis/Mips/TestBase.h       |  1 +
 .../tools/llvm-exegesis/RISCV/TargetTest.cpp  | 11 ++++++++
 .../tools/llvm-exegesis/RISCV/TestBase.h      |  1 +
 6 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/llvm/tools/llvm-exegesis/lib/Assembler.cpp b/llvm/tools/llvm-exegesis/lib/Assembler.cpp
index 240ca8ba435b94..8b77b555e2163e 100644
--- a/llvm/tools/llvm-exegesis/lib/Assembler.cpp
+++ b/llvm/tools/llvm-exegesis/lib/Assembler.cpp
@@ -225,16 +225,24 @@ ArrayRef<MCRegister> FunctionFiller::getRegistersSetUp() const {
 }
 
 static std::unique_ptr<Module>
-createModule(const std::unique_ptr<LLVMContext> &Context, const DataLayout &DL) {
+createModule(const std::unique_ptr<LLVMContext> &Context,
+             const TargetMachine &TM) {
   auto Mod = std::make_unique<Module>(ModuleID, *Context);
-  Mod->setDataLayout(DL);
+  const Triple &TT = TM.getTargetTriple();
+  Mod->setTargetTriple(TT);
+  StringRef ABIName = TM.Options.MCOptions.getABIName();
+  if (!ABIName.empty()) {
+    Mod->addModuleFlag(Module::Error, "target-abi",
+                       MDString::get(*Context, ABIName));
+  }
+
+  Mod->setDataLayout(DataLayout(TT.computeDataLayout(ABIName)));
   return Mod;
 }
 
 BitVector getFunctionReservedRegs(const TargetMachine &TM) {
   std::unique_ptr<LLVMContext> Context = std::make_unique<LLVMContext>();
-  std::unique_ptr<Module> Module = createModule(
-      Context, DataLayout(TM.getTargetTriple().computeDataLayout()));
+  std::unique_ptr<Module> Module = createModule(Context, TM);
   auto MMIWP = std::make_unique<MachineModuleInfoWrapperPass>(&TM);
   MachineFunction &MF = createVoidVoidPtrMachineFunction(
       FunctionID, Module.get(), &MMIWP->getMMI());
@@ -248,8 +256,7 @@ Error assembleToStream(const ExegesisTarget &ET,
                        raw_pwrite_stream &AsmStream, const BenchmarkKey &Key,
                        bool GenerateMemoryInstructions) {
   auto Context = std::make_unique<LLVMContext>();
-  std::unique_ptr<Module> Module = createModule(
-      Context, DataLayout(TM->getTargetTriple().computeDataLayout()));
+  std::unique_ptr<Module> Module = createModule(Context, *TM);
   auto MMIWP = std::make_unique<MachineModuleInfoWrapperPass>(TM.get());
   MachineFunction &MF = createVoidVoidPtrMachineFunction(
       FunctionID, Module.get(), &MMIWP.get()->getMMI());
@@ -384,7 +391,8 @@ Expected<ExecutableFunction> ExecutableFunction::create(
 
   auto EJITOrErr =
       orc::LLJITBuilder()
-          .setDataLayout(DataLayout(TM->getTargetTriple().computeDataLayout()))
+          .setDataLayout(DataLayout(TM->getTargetTriple().computeDataLayout(
+              TM->Options.MCOptions.getABIName())))
           .create();
   if (!EJITOrErr)
     return EJITOrErr.takeError();
diff --git a/llvm/unittests/tools/llvm-exegesis/Common/AssemblerUtils.h b/llvm/unittests/tools/llvm-exegesis/Common/AssemblerUtils.h
index 0b489c53432461..aed0230182ac0f 100644
--- a/llvm/unittests/tools/llvm-exegesis/Common/AssemblerUtils.h
+++ b/llvm/unittests/tools/llvm-exegesis/Common/AssemblerUtils.h
@@ -95,6 +95,31 @@ class MachineFunctionGeneratorBaseTest : public ::testing::Test {
   const ExegesisTarget *const ET;
 };
 
+inline void checkAssembleWithABI(const Triple &TT, StringRef CpuName,
+                                 StringRef ABIName) {
+  std::string Error;
+  const Target *TheTarget = TargetRegistry::lookupTarget(TT, Error);
+  ASSERT_TRUE(TheTarget) << Error;
+  const ExegesisTarget *ET = ExegesisTarget::lookup(TT);
+  ASSERT_TRUE(ET);
+
+  TargetOptions Options;
+  Options.MCOptions.ABIName = ABIName;
+  std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
+      TT, CpuName, "", Options, Reloc::Model::Static));
+  ASSERT_TRUE(TM);
+  ASSERT_NE(TM->createDataLayout(), DataLayout(TT.computeDataLayout()));
+  EXPECT_TRUE(getFunctionReservedRegs(*TM).any());
+
+  SmallString<256> Buffer;
+  raw_svector_ostream AsmStream(Buffer);
+  EXPECT_EQ(toString(assembleToStream(
+                *ET, std::move(TM), /*LiveIns=*/{}, [](FunctionFiller &) {},
+                AsmStream, BenchmarkKey(), false)),
+            "");
+  EXPECT_FALSE(Buffer.empty());
+}
+
 } // namespace exegesis
 } // namespace llvm
 
diff --git a/llvm/unittests/tools/llvm-exegesis/Mips/TargetTest.cpp b/llvm/unittests/tools/llvm-exegesis/Mips/TargetTest.cpp
index d18ece99d4a6bb..e3e5082139fe95 100644
--- a/llvm/unittests/tools/llvm-exegesis/Mips/TargetTest.cpp
+++ b/llvm/unittests/tools/llvm-exegesis/Mips/TargetTest.cpp
@@ -10,6 +10,7 @@
 
 #include <cassert>
 
+#include "../Common/AssemblerUtils.h"
 #include "MCTargetDesc/MipsMCTargetDesc.h"
 #include "TestBase.h"
 #include "llvm/MC/TargetRegistry.h"
@@ -125,6 +126,10 @@ TEST_F(MipsTargetTest, DefaultPfmCounters) {
       Expected);
 }
 
+TEST_F(MipsTargetTest, AssembleWithN32) {
+  checkAssembleWithABI(Triple("mips64-unknown-linux"), "mips64", "n32");
+}
+
 } // namespace
 } // namespace exegesis
 } // namespace llvm
diff --git a/llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h b/llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h
index d92b8494b551a6..ef8445f15f808d 100644
--- a/llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h
+++ b/llvm/unittests/tools/llvm-exegesis/Mips/TestBase.h
@@ -31,6 +31,7 @@ class MipsTestBase : public ::testing::Test {
     LLVMInitializeMipsTargetInfo();
     LLVMInitializeMipsTargetMC();
     LLVMInitializeMipsTarget();
+    LLVMInitializeMipsAsmPrinter();
     InitializeMipsExegesisTarget();
   }
 
diff --git a/llvm/unittests/tools/llvm-exegesis/RISCV/TargetTest.cpp b/llvm/unittests/tools/llvm-exegesis/RISCV/TargetTest.cpp
index b45adc6a85a9d9..be06d9efc2a5b3 100644
--- a/llvm/unittests/tools/llvm-exegesis/RISCV/TargetTest.cpp
+++ b/llvm/unittests/tools/llvm-exegesis/RISCV/TargetTest.cpp
@@ -10,6 +10,7 @@
 
 #include <cassert>
 
+#include "../Common/AssemblerUtils.h"
 #include "MCTargetDesc/RISCVMCTargetDesc.h"
 #include "TestBase.h"
 #include "llvm/MC/TargetRegistry.h"
@@ -46,6 +47,16 @@ TEST_F(RISCVTargetTest, DefaultPfmCounters) {
       Expected);
 }
 
+TEST_F(RISCVTargetTest, AssembleWithILP32E) {
+  checkAssembleWithABI(Triple("riscv32-unknown-linux"), "generic-rv32",
+                       "ilp32e");
+}
+
+TEST_F(RISCVTargetTest, AssembleWithLP64E) {
+  checkAssembleWithABI(Triple("riscv64-unknown-linux"), "generic-rv64",
+                       "lp64e");
+}
+
 } // namespace
 } // namespace exegesis
 } // namespace llvm
diff --git a/llvm/unittests/tools/llvm-exegesis/RISCV/TestBase.h b/llvm/unittests/tools/llvm-exegesis/RISCV/TestBase.h
index 66748fb9a2ce1b..f7b50eca814535 100644
--- a/llvm/unittests/tools/llvm-exegesis/RISCV/TestBase.h
+++ b/llvm/unittests/tools/llvm-exegesis/RISCV/TestBase.h
@@ -32,6 +32,7 @@ class RISCVTestBase : public ::testing::Test {
     LLVMInitializeRISCVTargetInfo();
     LLVMInitializeRISCVTargetMC();
     LLVMInitializeRISCVTarget();
+    LLVMInitializeRISCVAsmPrinter();
     InitializeRISCVExegesisTarget();
   }
 



More information about the llvm-commits mailing list