Skip to content

[CodeGen][WebAssembly] Reserve a personality for the D language - #224883

Merged
QuantumSegfault merged 2 commits into
llvm:mainfrom
QuantumSegfault:wasm-dlang-personality
Sep 30, 2026
Merged

QuantumSegfault merged 2 commits into
llvm:mainfrom
QuantumSegfault:wasm-dlang-personality

Conversation

@QuantumSegfault

Copy link
Copy Markdown
Member

Now that #209282 is merged, languages/code may choose to create and use personality functions other than __gxx_wasm_personality_v0 on WebAssembly. However said personality must still be isScopedEHPersonality and recognized as a Wasm personality for everything to function correctly.

This reserves a EHPersonality::Wasm_D (symbol _d_eh_personality_wasm) for future use by the D programming language.

@llvmorg-github-actions llvmorg-github-actions Bot added llvm:globalisel llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:SelectionDAG SelectionDAGISel as well llvm:ir llvm:transforms labels Sep 20, 2026
@llvmorg-github-actions

llvmorg-github-actions Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-llvm-transforms
@llvm/pr-subscribers-llvm-globalisel

@llvm/pr-subscribers-llvm-ir

Author: Demetrius Kanios (QuantumSegfault)

Changes

Now that #209282 is merged, languages/code may choose to create and use personality functions other than __gxx_wasm_personality_v0 on WebAssembly. However said personality must still be isScopedEHPersonality and recognized as a Wasm personality for everything to function correctly.

This reserves a EHPersonality::Wasm_D (symbol _d_eh_personality_wasm) for future use by the D programming language.


Full diff: https://github.com/llvm/llvm-project/pull/224883.diff

7 Files Affected:

  • (modified) llvm/include/llvm/IR/EHPersonalities.h (+2)
  • (modified) llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp (+2-1)
  • (modified) llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp (+3-1)
  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (+3-2)
  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp (+1-1)
  • (modified) llvm/lib/IR/EHPersonalities.cpp (+3)
  • (modified) llvm/lib/Transforms/InstCombine/InstructionCombining.cpp (+1)
diff --git a/llvm/include/llvm/IR/EHPersonalities.h b/llvm/include/llvm/IR/EHPersonalities.h
index 39f6817b3dc38..9715f63eb6491 100644
--- a/llvm/include/llvm/IR/EHPersonalities.h
+++ b/llvm/include/llvm/IR/EHPersonalities.h
@@ -35,6 +35,7 @@ enum class EHPersonality {
   Wasm_CXX,
   XL_CXX,
   ZOS_CXX,
+  Wasm_D
 };
 
 /// See if the given exception handling personality function is one
@@ -85,6 +86,7 @@ inline bool isScopedEHPersonality(EHPersonality Pers) {
   case EHPersonality::MSVC_TableSEH:
   case EHPersonality::CoreCLR:
   case EHPersonality::Wasm_CXX:
+  case EHPersonality::Wasm_D:
     return true;
   default:
     return false;
diff --git a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
index f4d46e3cad48d..e6c0e9fe6fb7f 100644
--- a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
@@ -3748,9 +3748,10 @@ bool IRTranslatorImpl::findUnwindDestinations(
   bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
   bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
   bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
+  bool IsWasmD = Personality == EHPersonality::Wasm_D;
   bool IsSEH = isAsynchronousEHPersonality(Personality);
 
-  if (IsWasmCXX) {
+  if (IsWasmCXX || IsWasmD) {
     // Ignore this for now.
     return false;
   }
diff --git a/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp b/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp
index 6ca0592644a08..30e84d321a2b3 100644
--- a/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp
@@ -28,6 +28,7 @@
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/EHPersonalities.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/IntrinsicInst.h"
@@ -264,7 +265,8 @@ void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
         continue;
       }
       if (isa<FuncletPadInst>(PadInst) &&
-          Personality != EHPersonality::Wasm_CXX)
+          Personality != EHPersonality::Wasm_CXX &&
+          Personality != EHPersonality::Wasm_D)
         assert(BB.begin() == PadInst && "WinEHPrepare failed to demote PHIs");
     }
 
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index 794a4e6e1b537..71bfcb2549fd6 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -2118,7 +2118,7 @@ void SelectionDAGBuilder::visitCleanupPad(const CleanupPadInst &CPI) {
   // the start of an EH scope/funclet.
   FuncInfo.MBB->setIsEHScopeEntry();
   auto Pers = classifyEHPersonality(FuncInfo.Fn->getPersonalityFn());
-  if (Pers != EHPersonality::Wasm_CXX) {
+  if (Pers != EHPersonality::Wasm_CXX && Pers != EHPersonality::Wasm_D) {
     FuncInfo.MBB->setIsEHFuncletEntry();
     FuncInfo.MBB->setIsCleanupFuncletEntry();
   }
@@ -2142,6 +2142,7 @@ static void findUnwindDestinations(
   bool IsMSVCCXX = Personality == EHPersonality::MSVC_CXX;
   bool IsCoreCLR = Personality == EHPersonality::CoreCLR;
   bool IsWasmCXX = Personality == EHPersonality::Wasm_CXX;
+  bool IsWasmD = Personality == EHPersonality::Wasm_D;
   bool IsSEH = isAsynchronousEHPersonality(Personality);
 
   while (EHPadBB) {
@@ -2158,7 +2159,7 @@ static void findUnwindDestinations(
       UnwindDests.emplace_back(FuncInfo.getMBB(EHPadBB), Prob);
       UnwindDests.back().first->setIsEHScopeEntry();
       // In Wasm, EH scopes are not funclets
-      if (!IsWasmCXX)
+      if (!IsWasmCXX && !IsWasmD)
         UnwindDests.back().first->setIsEHFuncletEntry();
       break;
     } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(Pad)) {
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
index 6233cf8d0d668..9ac061cbefc9c 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -1475,7 +1475,7 @@ bool SelectionDAGISel::PrepareEHLandingPad() {
   if (auto *RegMask = TRI.getCustomEHPadPreservedMask(*MF))
     MF->getRegInfo().addPhysRegsUsedFromRegMask(RegMask);
 
-  if (Pers == EHPersonality::Wasm_CXX) {
+  if (Pers == EHPersonality::Wasm_CXX || Pers == EHPersonality::Wasm_D) {
     if (const auto *CPI = dyn_cast<CatchPadInst>(LLVMBB->getFirstNonPHIIt()))
       mapWasmLandingPadIndex(MBB, CPI);
   } else {
diff --git a/llvm/lib/IR/EHPersonalities.cpp b/llvm/lib/IR/EHPersonalities.cpp
index 12ae4748e1f4a..a7d0740dc8533 100644
--- a/llvm/lib/IR/EHPersonalities.cpp
+++ b/llvm/lib/IR/EHPersonalities.cpp
@@ -52,6 +52,7 @@ EHPersonality llvm::classifyEHPersonality(const Value *Pers) {
       .Case("__gxx_wasm_personality_v0", EHPersonality::Wasm_CXX)
       .Case("__xlcxx_personality_v1", EHPersonality::XL_CXX)
       .Case("__zos_cxx_personality_v2", EHPersonality::ZOS_CXX)
+      .Case("_d_eh_personality_wasm", EHPersonality::Wasm_D)
       .Default(EHPersonality::Unknown);
 }
 
@@ -86,6 +87,8 @@ StringRef llvm::getEHPersonalityName(EHPersonality Pers) {
     return "__xlcxx_personality_v1";
   case EHPersonality::ZOS_CXX:
     return "__zos_cxx_personality_v2";
+  case EHPersonality::Wasm_D:
+    return "_d_eh_personality_wasm";
   case EHPersonality::Unknown:
     llvm_unreachable("Unknown EHPersonality!");
   }
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 4b09510933d0c..180b10fe08270 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -4868,6 +4868,7 @@ static bool isCatchAll(EHPersonality Personality, Constant *TypeInfo) {
   case EHPersonality::Wasm_CXX:
   case EHPersonality::XL_CXX:
   case EHPersonality::ZOS_CXX:
+  case EHPersonality::Wasm_D:
     return isa<ConstantPointerNull>(TypeInfo);
   }
   llvm_unreachable("invalid enum");

@QuantumSegfault

Copy link
Copy Markdown
Member Author

@kinke

If you have any opinions on the symbol name. I chose _d_eh_personality_wasm.

@nikic
nikic requested a review from efriedma-quic September 20, 2026 07:10
@nikic

nikic commented Sep 20, 2026

Copy link
Copy Markdown
Contributor

I guess this is fine given how things are currently implemented. Though we should really avoid the need for this hardcoded list of personalities and encode the properties of the personality in IR, so that frontends can use custom personality functions without having to teach LLVM about them. Some options here would be either a first class way to declare personalities, or to have an attribute on the personality function declaration.

(See e.g. how we switched allocator optimizations to be encoded via attributes instead of adding Rust's allocator functions to the hardcoded list in LLVM.)

@aheejin aheejin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's nice to have something like isWasmFunctionality in EHPersonalities.h given that the pattern is repeated many times, but not sure what others would feel about adding a target-specific function in there.. Anyway, this LGTM, but if we ever get to add more Wasm personalities, maybe it'd be good to think that or what @nikic said

@QuantumSegfault

Copy link
Copy Markdown
Member Author

Yeah, I agree. Eventually we should probably find a way to decentralize the entire personality system.

However that's out of scope for this immediate change. And if we get anybody else wanting more personalities before this proposed rework, we can refactor as isWasmPersonality at that point.

@QuantumSegfault

QuantumSegfault commented Sep 29, 2026 •

Copy link
Copy Markdown
Member Author

@kinke

Ping. Any opinions on this? (name particularly)

@kinke

kinke commented Sep 30, 2026

Copy link
Copy Markdown

All good from my side.

@QuantumSegfault
QuantumSegfault enabled auto-merge (squash) September 30, 2026 17:40
@QuantumSegfault
QuantumSegfault enabled auto-merge (squash) September 30, 2026 17:42
@QuantumSegfault
QuantumSegfault merged commit a9a9c8a into llvm:main Sep 30, 2026
8 of 10 checks passed
Expertcoderz pushed a commit to Expertcoderz/llvm-project that referenced this pull request Oct 3, 2026
…#224883)

Now that llvm#209282 is merged, languages/code may choose to create and use
personality functions other than `__gxx_wasm_personality_v0` on
WebAssembly. However said personality must still be
`isScopedEHPersonality` and recognized as a Wasm personality for
everything to function correctly.

This reserves a `EHPersonality::Wasm_D` (symbol
`_d_eh_personality_wasm`) for future use by the D programming language.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:globalisel llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:ir llvm:SelectionDAG SelectionDAGISel as well llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants