[llvm] Triple: Move getSjLjDataSize off TargetMachine (PR #225360)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 03:32:45 PDT 2026


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

This eliminates the TargetMachine dependence of SjLjEHPrepare.
The pass had the questionable behavior of just proceeding with the
default size without a TargetMachine. VE is the only user and changes
the integer bitwidth used, and this doesn't seem worthwhile of a
virtual function. Move to the triple in keeping with migrating
ABI parameters out of codegen.

Co-authored-by: Claude (Claude-Opus-4.8) <noreply at anthropic.com>

>From b72b9f2f8e1e70322acf8f9d090e4ccdd3b66541 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Tue, 22 Sep 2026 11:06:42 +0200
Subject: [PATCH] Triple: Move getSjLjDataSize off TargetMachine

This eliminates the TargetMachine dependence of SjLjEHPrepare.
The pass had the questionable behavior of just proceeding with the
default size without a TargetMachine. VE is the only user and changes
the integer bitwidth used, and this doesn't seem worthwhile of a
virtual function. Move to the triple in keeping with migrating
ABI parameters out of codegen.

Co-authored-by: Claude (Claude-Opus-4.8) <noreply at anthropic.com>
---
 llvm/include/llvm/CodeGen/Passes.h        |  2 +-
 llvm/include/llvm/CodeGen/SjLjEHPrepare.h |  5 -----
 llvm/include/llvm/Target/TargetMachine.h  |  4 ----
 llvm/include/llvm/TargetParser/Triple.h   |  4 ++++
 llvm/lib/CodeGen/SjLjEHPrepare.cpp        | 16 +++++-----------
 llvm/lib/CodeGen/TargetPassConfig.cpp     |  2 +-
 llvm/lib/Passes/CodeGenPassBuilder.cpp    |  2 +-
 llvm/lib/Passes/PassRegistry.def          |  2 +-
 llvm/lib/Target/VE/VETargetMachine.h      |  2 --
 llvm/lib/TargetParser/Triple.cpp          |  6 ++++++
 10 files changed, 19 insertions(+), 26 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/Passes.h b/llvm/include/llvm/CodeGen/Passes.h
index 861ca5aeeb852..ae84a215fffcb 100644
--- a/llvm/include/llvm/CodeGen/Passes.h
+++ b/llvm/include/llvm/CodeGen/Passes.h
@@ -427,7 +427,7 @@ LLVM_ABI FunctionPass *createWinEHPass(bool DemoteCatchSwitchPHIOnly = false);
 /// createSjLjEHPreparePass - This pass adapts exception handling code to use
 /// the GCC-style builtin setjmp/longjmp (sjlj) to handling EH control flow.
 ///
-LLVM_ABI FunctionPass *createSjLjEHPreparePass(const TargetMachine *TM);
+LLVM_ABI FunctionPass *createSjLjEHPreparePass();
 
 /// createWasmEHPass - This pass adapts exception handling code to use
 /// WebAssembly's exception handling scheme.
diff --git a/llvm/include/llvm/CodeGen/SjLjEHPrepare.h b/llvm/include/llvm/CodeGen/SjLjEHPrepare.h
index d91d87bb29ecd..62a421107c109 100644
--- a/llvm/include/llvm/CodeGen/SjLjEHPrepare.h
+++ b/llvm/include/llvm/CodeGen/SjLjEHPrepare.h
@@ -13,13 +13,8 @@
 
 namespace llvm {
 
-class TargetMachine;
-
 class SjLjEHPreparePass : public RequiredPassInfoMixin<SjLjEHPreparePass> {
-  const TargetMachine *TM;
-
 public:
-  explicit SjLjEHPreparePass(const TargetMachine *TM) : TM(TM) {}
   LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
 };
 
diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h
index 5b1dede9d7893..dae6c76aaf64f 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -466,10 +466,6 @@ class LLVM_ABI TargetMachine {
                          Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
   MCSymbol *getSymbol(const GlobalValue *GV) const;
 
-  /// The integer bit size to use for SjLj based exception handling.
-  static constexpr unsigned DefaultSjLjDataSize = 32;
-  virtual unsigned getSjLjDataSize() const { return DefaultSjLjDataSize; }
-
   /// getAddressSpaceForPseudoSourceKind - Given the kind of memory
   /// (e.g. stack) the target returns the corresponding address space.
   virtual unsigned getAddressSpaceForPseudoSourceKind(unsigned Kind) const {
diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index b369b66960b1a..55913240f0be7 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -1476,6 +1476,10 @@ class Triple {
 
   LLVM_ABI ExceptionHandling getDefaultExceptionHandling() const;
 
+  /// Returns the bit width of each word in the SjLj exception handling jump
+  /// buffer's data array for this target.
+  LLVM_ABI unsigned getSjLjDataSize() const;
+
   /// Compute the LLVM IR data layout string based on the triple. Some targets
   /// customize the layout based on the ABIName string.
   LLVM_ABI std::string computeDataLayout(StringRef ABIName = "") const;
diff --git a/llvm/lib/CodeGen/SjLjEHPrepare.cpp b/llvm/lib/CodeGen/SjLjEHPrepare.cpp
index 7ba743c206a21..f3b13d7fe02cb 100644
--- a/llvm/lib/CodeGen/SjLjEHPrepare.cpp
+++ b/llvm/lib/CodeGen/SjLjEHPrepare.cpp
@@ -28,7 +28,7 @@
 #include "llvm/Pass.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
-#include "llvm/Target/TargetMachine.h"
+#include "llvm/TargetParser/Triple.h"
 #include "llvm/Transforms/Utils/Local.h"
 using namespace llvm;
 
@@ -53,10 +53,8 @@ class SjLjEHPrepareImpl {
   Function *CallSiteFn = nullptr;
   Function *FuncCtxFn = nullptr;
   AllocaInst *FuncCtx = nullptr;
-  const TargetMachine *TM = nullptr;
 
 public:
-  explicit SjLjEHPrepareImpl(const TargetMachine *TM = nullptr) : TM(TM) {}
   bool doInitialization(Module &M);
   bool runOnFunction(Function &F);
 
@@ -74,8 +72,7 @@ class SjLjEHPrepare : public FunctionPass {
 
 public:
   static char ID; // Pass identification, replacement for typeid
-  explicit SjLjEHPrepare(const TargetMachine *TM = nullptr)
-      : FunctionPass(ID), Impl(TM) {}
+  SjLjEHPrepare() : FunctionPass(ID) {}
   bool doInitialization(Module &M) override { return Impl.doInitialization(M); }
   bool runOnFunction(Function &F) override { return Impl.runOnFunction(F); };
 
@@ -88,7 +85,7 @@ class SjLjEHPrepare : public FunctionPass {
 
 PreservedAnalyses SjLjEHPreparePass::run(Function &F,
                                          FunctionAnalysisManager &FAM) {
-  SjLjEHPrepareImpl Impl(TM);
+  SjLjEHPrepareImpl Impl;
   Impl.doInitialization(*F.getParent());
   bool Changed = Impl.runOnFunction(F);
   return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
@@ -99,9 +96,7 @@ INITIALIZE_PASS(SjLjEHPrepare, DEBUG_TYPE, "Prepare SjLj exceptions",
                 false, false)
 
 // Public Interface To the SjLjEHPrepare pass.
-FunctionPass *llvm::createSjLjEHPreparePass(const TargetMachine *TM) {
-  return new SjLjEHPrepare(TM);
-}
+FunctionPass *llvm::createSjLjEHPreparePass() { return new SjLjEHPrepare(); }
 
 // doInitialization - Set up decalarations and types needed to process
 // exceptions.
@@ -109,8 +104,7 @@ bool SjLjEHPrepareImpl::doInitialization(Module &M) {
   // Build the function context structure.
   // builtin_setjmp uses a five word jbuf
   Type *VoidPtrTy = PointerType::getUnqual(M.getContext());
-  unsigned DataBits =
-      TM ? TM->getSjLjDataSize() : TargetMachine::DefaultSjLjDataSize;
+  unsigned DataBits = M.getTargetTriple().getSjLjDataSize();
   DataTy = Type::getIntNTy(M.getContext(), DataBits);
   doubleUnderDataTy = ArrayType::get(DataTy, 4);
   doubleUnderJBufTy = ArrayType::get(VoidPtrTy, 5);
diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp
index 9e73048264b0a..45456a56ec8ce 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -937,7 +937,7 @@ void TargetPassConfig::addPassesToHandleExceptions() {
     // removed from the parent invoke(s). This could happen when a landing
     // pad is shared by multiple invokes and is also a target of a normal
     // edge from elsewhere.
-    addPass(createSjLjEHPreparePass(TM));
+    addPass(createSjLjEHPreparePass());
     [[fallthrough]];
   case ExceptionHandling::DwarfCFI:
   case ExceptionHandling::ARM:
diff --git a/llvm/lib/Passes/CodeGenPassBuilder.cpp b/llvm/lib/Passes/CodeGenPassBuilder.cpp
index a24323d816caa..d763cba7934f0 100644
--- a/llvm/lib/Passes/CodeGenPassBuilder.cpp
+++ b/llvm/lib/Passes/CodeGenPassBuilder.cpp
@@ -449,7 +449,7 @@ void CodeGenPassBuilder::addPassesToHandleExceptions(PassManagerWrapper &PMW) {
     // removed from the parent invoke(s). This could happen when a landing
     // pad is shared by multiple invokes and is also a target of a normal
     // edge from elsewhere.
-    addFunctionPass(SjLjEHPreparePass(&TM), PMW);
+    addFunctionPass(SjLjEHPreparePass(), PMW);
     [[fallthrough]];
   case ExceptionHandling::DwarfCFI:
   case ExceptionHandling::ARM:
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 33c9e19988d7a..e085bf6422738 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -545,7 +545,7 @@ FUNCTION_PASS("select-optimize", SelectOptimizePass(*TM))
 FUNCTION_PASS("separate-const-offset-from-gep",
               SeparateConstOffsetFromGEPPass())
 FUNCTION_PASS("sink", SinkingPass())
-FUNCTION_PASS("sjlj-eh-prepare", SjLjEHPreparePass(TM))
+FUNCTION_PASS("sjlj-eh-prepare", SjLjEHPreparePass())
 FUNCTION_PASS("slp-vectorizer", SLPVectorizerPass())
 FUNCTION_PASS("slsr", StraightLineStrengthReducePass())
 FUNCTION_PASS("stack-protector", StackProtectorPass(*TM))
diff --git a/llvm/lib/Target/VE/VETargetMachine.h b/llvm/lib/Target/VE/VETargetMachine.h
index 540d5d8e712a1..6f9cad7b767a0 100644
--- a/llvm/lib/Target/VE/VETargetMachine.h
+++ b/llvm/lib/Target/VE/VETargetMachine.h
@@ -56,8 +56,6 @@ class VETargetMachine : public CodeGenTargetMachineImpl {
   bool isMachineVerifierClean() const override { return false; }
 
   TargetTransformInfo getTargetTransformInfo(const Function &F) const override;
-
-  unsigned getSjLjDataSize() const override { return 64; }
 };
 
 } // namespace llvm
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index fb54408c0729f..6460345c6bec8 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -2518,6 +2518,12 @@ ExceptionHandling Triple::getDefaultExceptionHandling() const {
   return ExceptionHandling::None;
 }
 
+unsigned Triple::getSjLjDataSize() const {
+  // VE uses 64-bit words in the SjLj jump buffer's data array; everything else
+  // uses 32-bit words.
+  return getArch() == Triple::ve ? 64 : 32;
+}
+
 static FloatABI::ABIType getARMDefaultFloatABI(const Triple &T) {
   Triple::EnvironmentType Env = T.getEnvironment();
   bool IsHard =



More information about the llvm-commits mailing list