[llvm] fe47933 - IR: Introduce "exception-model" module flag (#220052)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 15 03:57:10 PDT 2026
Author: Matt Arsenault
Date: 2026-09-15T12:57:05+02:00
New Revision: fe479338a7abbe93d9c4bc32d44a53a17a7e2f6e
URL: https://github.com/llvm/llvm-project/commit/fe479338a7abbe93d9c4bc32d44a53a17a7e2f6e
DIFF: https://github.com/llvm/llvm-project/commit/fe479338a7abbe93d9c4bc32d44a53a17a7e2f6e.diff
LOG: IR: Introduce "exception-model" module flag (#220052)
Add an "exception-model" IR module flag intended to replace
TargetOptions::ExceptionModel, such that the ABI is fully
computable from the IR alone. Add the basic documentation and
verifier/linker tests, but doesn't wire up the consumers.
Co-authored-by: Claude (Claude-Opus-4.8) <noreply at anthropic.com>
Added:
llvm/test/Assembler/module-flags-exception-model.ll
llvm/test/Linker/module-flags-exception-model.ll
llvm/test/Verifier/module-flags-exception-model.ll
Modified:
llvm/docs/LangRef.md
llvm/include/llvm/IR/Module.h
llvm/include/llvm/Support/CodeGen.h
llvm/include/llvm/Target/TargetMachine.h
llvm/include/llvm/Target/TargetOptions.h
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
llvm/lib/CodeGen/CommandFlags.cpp
llvm/lib/CodeGen/TargetPassConfig.cpp
llvm/lib/IR/Module.cpp
llvm/lib/IR/RuntimeLibcalls.cpp
llvm/lib/IR/Verifier.cpp
llvm/lib/Passes/CodeGenPassBuilder.cpp
llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
Removed:
################################################################################
diff --git a/llvm/docs/LangRef.md b/llvm/docs/LangRef.md
index a6a7fa8926f2f..a29e8f7508a7f 100644
--- a/llvm/docs/LangRef.md
+++ b/llvm/docs/LangRef.md
@@ -9549,6 +9549,50 @@ while ARM uses names such as `"aapcs"` and `"apcs-gnu"`:
```
!llvm.module.flags = !{!0}
!0 = !{i32 1, !"target-abi", !"aapcs"}
+...
+
+### Exception Model Module Flags Metadata
+
+This module flag describes the exception-handling model that the module was
+compiled for. The value is an `MDString` and must be one of:
+
+```{list-table}
+:header-rows: 1
+:widths: 30 70
+* - Value
+ - Meaning
+
+* - `"none"`
+ - Exceptions are explicitly disabled.
+
+* - `"dwarf"`
+ - DWARF-like table-based (CFI) exception handling.
+
+* - `"sjlj"`
+ - setjmp/longjmp based exception handling.
+
+* - `"arm"`
+ - ARM EHABI exception handling.
+
+* - `"wineh"`
+ - Windows exception handling.
+
+* - `"wasm"`
+ - WebAssembly exception handling.
+
+* - `"emscripten"`
+ - Emscripten JavaScript-based exception handling.
+
+```
+
+When the flag is absent, the exception model is unspecified and the target's
+default is used. This is distinct from an explicit `"none"`, which disables
+exceptions even on targets that support them by default. The flag must use the
+`error` merge behavior, so that linking modules with conflicting exception
+models is rejected. For example:
+```
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"sjlj"}
```
### Long Double Type Module Flags Metadata
diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h
index 017394b0018b3..11f75256f6948 100644
--- a/llvm/include/llvm/IR/Module.h
+++ b/llvm/include/llvm/IR/Module.h
@@ -1095,6 +1095,15 @@ class LLVM_ABI Module {
/// Set the thread model.
void setThreadModel(ThreadModel Model);
+
+ /// @name Utility function for querying the exception model
+ /// @{
+
+ /// Returns the exception model recorded by the "exception-model" module flag,
+ /// or ExceptionHandling::Default when the flag is absent (meaning
+ /// unspecified; callers with a TargetMachine should fall back to its
+ /// exception model).
+ ExceptionHandling getExceptionModel() const;
/// @}
/// @}
diff --git a/llvm/include/llvm/Support/CodeGen.h b/llvm/include/llvm/Support/CodeGen.h
index 5edf66b6c2156..210c2a023b30c 100644
--- a/llvm/include/llvm/Support/CodeGen.h
+++ b/llvm/include/llvm/Support/CodeGen.h
@@ -52,6 +52,7 @@ namespace llvm {
}
enum class ExceptionHandling : int {
+ Default, ///< Not specified; resolve to the target's default model
None, ///< No exception support
DwarfCFI, ///< DWARF-like instruction based exceptions
SjLj, ///< setjmp/longjmp based exceptions
@@ -64,6 +65,54 @@ namespace llvm {
///< PPA1 is used instead of an .eh_frame section.
};
+ /// Returns the "exception-model" module flag spelling for an
+ /// ExceptionHandling value. Default, AIX, and ZOS have no spelling and return
+ /// "".
+ inline StringRef getExceptionModelName(ExceptionHandling EH) {
+ switch (EH) {
+ case ExceptionHandling::None:
+ return "none";
+ case ExceptionHandling::DwarfCFI:
+ return "dwarf";
+ case ExceptionHandling::SjLj:
+ return "sjlj";
+ case ExceptionHandling::ARM:
+ return "arm";
+ case ExceptionHandling::WinEH:
+ return "wineh";
+ case ExceptionHandling::Wasm:
+ return "wasm";
+ case ExceptionHandling::Emscripten:
+ return "emscripten";
+ case ExceptionHandling::Default:
+ case ExceptionHandling::AIX:
+ case ExceptionHandling::ZOS:
+ break;
+ }
+ return "";
+ }
+
+ /// Parses the string spelling used by the "exception-model" IR module flag
+ /// into an ExceptionHandling value, returning std::nullopt if it does not
+ /// name a supported exception model.
+ inline std::optional<ExceptionHandling> parseExceptionModel(StringRef Name) {
+ if (Name == "none")
+ return ExceptionHandling::None;
+ if (Name == "dwarf")
+ return ExceptionHandling::DwarfCFI;
+ if (Name == "sjlj")
+ return ExceptionHandling::SjLj;
+ if (Name == "arm")
+ return ExceptionHandling::ARM;
+ if (Name == "wineh")
+ return ExceptionHandling::WinEH;
+ if (Name == "wasm")
+ return ExceptionHandling::Wasm;
+ if (Name == "emscripten")
+ return ExceptionHandling::Emscripten;
+ return std::nullopt;
+ }
+
/// The floating-point format used for the target's "long double" type.
enum class LongDoubleFormat {
IEEEsingle,
diff --git a/llvm/include/llvm/Target/TargetMachine.h b/llvm/include/llvm/Target/TargetMachine.h
index ba017910c263d..5b1dede9d7893 100644
--- a/llvm/include/llvm/Target/TargetMachine.h
+++ b/llvm/include/llvm/Target/TargetMachine.h
@@ -258,11 +258,10 @@ class LLVM_ABI TargetMachine {
/// assembly.
const MCSubtargetInfo &getMCSubtargetInfo(StringRef CPU, StringRef FS);
- /// Return the ExceptionHandling to use, considering TargetOptions and the
- /// Triple's default.
+ /// Return the ExceptionHandling to use. A Default model resolves to the
+ /// triple's default; None means exceptions are disabled.
ExceptionHandling getExceptionModel() const {
- // FIXME: This interface fails to distinguish default from not supported.
- return Options.ExceptionModel == ExceptionHandling::None
+ return Options.ExceptionModel == ExceptionHandling::Default
? TargetTriple.getDefaultExceptionHandling()
: Options.ExceptionModel;
}
diff --git a/llvm/include/llvm/Target/TargetOptions.h b/llvm/include/llvm/Target/TargetOptions.h
index 78ad27a00e4f9..e2ed4fd7dd853 100644
--- a/llvm/include/llvm/Target/TargetOptions.h
+++ b/llvm/include/llvm/Target/TargetOptions.h
@@ -311,7 +311,7 @@ class TargetOptions {
public:
/// What exception model to use
- ExceptionHandling ExceptionModel = ExceptionHandling::None;
+ ExceptionHandling ExceptionModel = ExceptionHandling::Default;
/// Machine level options.
MCTargetOptions MCOptions;
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 5ca684a2c016c..c0348a2dad159 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -668,6 +668,8 @@ bool AsmPrinter::doInitialization(Module &M) {
EHStreamer *ES = nullptr;
switch (MAI.getExceptionHandlingType()) {
+ case ExceptionHandling::Default:
+ llvm_unreachable("should have resolved exception model kind");
case ExceptionHandling::None:
case ExceptionHandling::Emscripten:
// Emscripten EH is handled in JS glue code and emits no EH tables here.
diff --git a/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp b/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
index 94e0dbeac6f35..2bc383f8ff703 100644
--- a/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
+++ b/llvm/lib/CodeGen/CodeGenTargetMachineImpl.cpp
@@ -84,7 +84,7 @@ void CodeGenTargetMachineImpl::initAsmInfo() {
getTargetTriple().getDefaultExceptionHandling() &&
"MCAsmInfo and Triple disagree on default exception handling type");
- if (Options.ExceptionModel != ExceptionHandling::None)
+ if (Options.ExceptionModel != ExceptionHandling::Default)
TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
AsmInfo.reset(TmpAsmInfo);
diff --git a/llvm/lib/CodeGen/CommandFlags.cpp b/llvm/lib/CodeGen/CommandFlags.cpp
index bf53b28c13a82..24ddcf0fbc6cc 100644
--- a/llvm/lib/CodeGen/CommandFlags.cpp
+++ b/llvm/lib/CodeGen/CommandFlags.cpp
@@ -172,10 +172,11 @@ codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() {
static cl::opt<ExceptionHandling> ExceptionModel(
"exception-model", cl::desc("exception model"),
- cl::init(ExceptionHandling::None),
+ cl::init(ExceptionHandling::Default),
cl::values(
- clEnumValN(ExceptionHandling::None, "default",
+ clEnumValN(ExceptionHandling::Default, "default",
"default exception handling model"),
+ clEnumValN(ExceptionHandling::None, "none", "no exception handling"),
clEnumValN(ExceptionHandling::DwarfCFI, "dwarf",
"DWARF-like CFI based exception handling"),
clEnumValN(ExceptionHandling::SjLj, "sjlj",
diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp
index 75fba01042c8d..e6c6cf542da35 100644
--- a/llvm/lib/CodeGen/TargetPassConfig.cpp
+++ b/llvm/lib/CodeGen/TargetPassConfig.cpp
@@ -960,6 +960,7 @@ void TargetPassConfig::addPassesToHandleExceptions() {
addPass(createWinEHPass(/*DemoteCatchSwitchPHIOnly=*/true));
addPass(createWasmEHPass());
break;
+ case ExceptionHandling::Default:
case ExceptionHandling::None:
case ExceptionHandling::Emscripten:
// Emscripten EH is lowered earlier by WebAssemblyLowerEmscriptenEHSjLj, so
diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp
index a1b2c2fac1aed..5544663a510df 100644
--- a/llvm/lib/IR/Module.cpp
+++ b/llvm/lib/IR/Module.cpp
@@ -717,6 +717,15 @@ void Module::setThreadModel(ThreadModel Model) {
MDString::get(getContext(), getThreadModelName(Model)));
}
+ExceptionHandling Module::getExceptionModel() const {
+ if (auto *Val = dyn_cast_or_null<MDString>(getModuleFlag("exception-model")))
+ return *parseExceptionModel(Val->getString());
+
+ // TODO: Return getDefaultExceptionHandling when TargetOptions field is
+ // deleted.
+ return ExceptionHandling::Default;
+}
+
std::optional<uint64_t> Module::getLargeDataThreshold() const {
auto *Val =
cast_or_null<ConstantAsMetadata>(getModuleFlag("Large Data Threshold"));
diff --git a/llvm/lib/IR/RuntimeLibcalls.cpp b/llvm/lib/IR/RuntimeLibcalls.cpp
index 89a5a929bec01..f69400d9140fc 100644
--- a/llvm/lib/IR/RuntimeLibcalls.cpp
+++ b/llvm/lib/IR/RuntimeLibcalls.cpp
@@ -31,11 +31,9 @@ RuntimeLibcallsInfo::RuntimeLibcallsInfo(const Triple &TT,
FloatABI::ABIType FloatABI,
EABI EABIVersion, StringRef ABIName,
VectorLibrary VecLib) {
- // FIXME: The ExceptionModel parameter is to handle the field in
- // TargetOptions. This interface fails to distinguish the forced disable
- // case for targets which support exceptions by default. This should
- // probably be a module flag and removed from TargetOptions.
- if (ExceptionModel == ExceptionHandling::None)
+ // Only an unspecified model resolves to the triple default; None is left as
+ // an explicit disable.
+ if (ExceptionModel == ExceptionHandling::Default)
ExceptionModel = TT.getDefaultExceptionHandling();
initLibcalls(TT, ExceptionModel, FloatABI, EABIVersion, ABIName,
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index c60c1b6ac11b6..bb5466219f461 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2157,6 +2157,17 @@ Verifier::visitModuleFlag(const MDNode *Op,
return;
}
+ if (ID->getString() == "exception-model") {
+ Check(MFB == Module::Error,
+ "exception-model module flag must use 'error' merge behavior", Op);
+ const MDString *Value = dyn_cast_or_null<MDString>(Op->getOperand(2));
+ Check(Value, "exception-model metadata requires a string argument");
+ if (Value)
+ Check(parseExceptionModel(Value->getString()).has_value(),
+ "invalid exception-model metadata value", Op);
+ return;
+ }
+
if (Name == "Linker Options") {
// If the llvm.linker.options named metadata exists, we assume that the
// bitcode reader has upgraded the module flag. Otherwise the flag might
diff --git a/llvm/lib/Passes/CodeGenPassBuilder.cpp b/llvm/lib/Passes/CodeGenPassBuilder.cpp
index e67377d7c6b3d..eb550cc276087 100644
--- a/llvm/lib/Passes/CodeGenPassBuilder.cpp
+++ b/llvm/lib/Passes/CodeGenPassBuilder.cpp
@@ -472,6 +472,7 @@ void CodeGenPassBuilder::addPassesToHandleExceptions(PassManagerWrapper &PMW) {
addFunctionPass(WinEHPreparePass(/*DemoteCatchSwitchPHIOnly=*/false), PMW);
addFunctionPass(WasmEHPreparePass(), PMW);
break;
+ case ExceptionHandling::Default:
case ExceptionHandling::None:
case ExceptionHandling::Emscripten:
// Emscripten EH is lowered earlier by WebAssemblyLowerEmscriptenEHSjLj, so
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
index 61a14c852ca4b..7cfc7a3c9c983 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
@@ -160,7 +160,7 @@ static void basicCheckForEHAndSjLj(TargetMachine *TM) {
report_fatal_error(
"-exception-model=emscripten not allowed with -wasm-enable-sjlj");
- if (TM->Options.ExceptionModel == ExceptionHandling::None) {
+ if (TM->Options.ExceptionModel == ExceptionHandling::Default) {
// FIXME: These flags should be removed in favor of directly using the
// generically configured ExceptionsType
if (WebAssembly::WasmEnableEH || WebAssembly::WasmEnableSjLj)
@@ -168,7 +168,8 @@ static void basicCheckForEHAndSjLj(TargetMachine *TM) {
}
// Basic Correctness checking related to -exception-model
- if (TM->Options.ExceptionModel != ExceptionHandling::None &&
+ if (TM->Options.ExceptionModel != ExceptionHandling::Default &&
+ TM->Options.ExceptionModel != ExceptionHandling::None &&
TM->Options.ExceptionModel != ExceptionHandling::Wasm &&
TM->Options.ExceptionModel != ExceptionHandling::Emscripten)
report_fatal_error(
diff --git a/llvm/test/Assembler/module-flags-exception-model.ll b/llvm/test/Assembler/module-flags-exception-model.ll
new file mode 100644
index 0000000000000..870e36be1dc1f
--- /dev/null
+++ b/llvm/test/Assembler/module-flags-exception-model.ll
@@ -0,0 +1,43 @@
+; RUN: split-file %s %t
+; RUN: llvm-as < %t/dwarf.ll | llvm-dis | FileCheck %s --check-prefix=DWARF
+; RUN: llvm-as < %t/sjlj.ll | llvm-dis | FileCheck %s --check-prefix=SJLJ
+; RUN: llvm-as < %t/arm.ll | llvm-dis | FileCheck %s --check-prefix=ARM
+; RUN: llvm-as < %t/wineh.ll | llvm-dis | FileCheck %s --check-prefix=WINEH
+; RUN: llvm-as < %t/wasm.ll | llvm-dis | FileCheck %s --check-prefix=WASM
+; RUN: llvm-as < %t/emscripten.ll | llvm-dis | FileCheck %s --check-prefix=EMSCRIPTEN
+; RUN: llvm-as < %t/none.ll | llvm-dis | FileCheck %s --check-prefix=NONE
+
+;--- dwarf.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"dwarf"}
+; DWARF: !0 = !{i32 1, !"exception-model", !"dwarf"}
+
+;--- sjlj.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"sjlj"}
+; SJLJ: !0 = !{i32 1, !"exception-model", !"sjlj"}
+
+;--- arm.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"arm"}
+; ARM: !0 = !{i32 1, !"exception-model", !"arm"}
+
+;--- wineh.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"wineh"}
+; WINEH: !0 = !{i32 1, !"exception-model", !"wineh"}
+
+;--- wasm.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"wasm"}
+; WASM: !0 = !{i32 1, !"exception-model", !"wasm"}
+
+;--- emscripten.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"emscripten"}
+; EMSCRIPTEN: !0 = !{i32 1, !"exception-model", !"emscripten"}
+
+;--- none.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"none"}
+; NONE: !0 = !{i32 1, !"exception-model", !"none"}
diff --git a/llvm/test/Linker/module-flags-exception-model.ll b/llvm/test/Linker/module-flags-exception-model.ll
new file mode 100644
index 0000000000000..67ffcfbe58f19
--- /dev/null
+++ b/llvm/test/Linker/module-flags-exception-model.ll
@@ -0,0 +1,28 @@
+; RUN: split-file %s %t
+
+;; A module without the flag (target default) links cleanly with one that sets
+;; it, and the explicit value is preserved.
+; RUN: llvm-link %t/none.ll %t/sjlj.ll -S -o - | FileCheck %s --check-prefix=SJLJ
+; RUN: llvm-link %t/sjlj.ll %t/none.ll -S -o - | FileCheck %s --check-prefix=SJLJ
+
+;; Two modules that agree link cleanly.
+; RUN: llvm-link %t/sjlj.ll %t/sjlj.ll -S -o - | FileCheck %s --check-prefix=SJLJ
+
+;; Two modules that disagree are rejected by the 'error' merge behavior.
+; RUN: not llvm-link %t/sjlj.ll %t/dwarf.ll -S -o /dev/null 2>&1 | FileCheck %s --check-prefix=CONFLICT
+
+; SJLJ: !{i32 1, !"exception-model", !"sjlj"}
+; CONFLICT: linking module flags 'exception-model': IDs have conflicting values
+
+;--- none.ll
+define void @f() {
+ ret void
+}
+
+;--- sjlj.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"sjlj"}
+
+;--- dwarf.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"dwarf"}
diff --git a/llvm/test/Verifier/module-flags-exception-model.ll b/llvm/test/Verifier/module-flags-exception-model.ll
new file mode 100644
index 0000000000000..0e2090dcc2021
--- /dev/null
+++ b/llvm/test/Verifier/module-flags-exception-model.ll
@@ -0,0 +1,37 @@
+; RUN: split-file %s %t
+; RUN: not llvm-as < %t/not-string.ll -disable-output 2>&1 | FileCheck %s --check-prefix=NOTSTRING
+; RUN: not llvm-as < %t/bad-value.ll -disable-output 2>&1 | FileCheck %s --check-prefix=BADVALUE
+; RUN: not llvm-as < %t/empty-value.ll -disable-output 2>&1 | FileCheck %s --check-prefix=EMPTY
+; RUN: not llvm-as < %t/too-few.ll -disable-output 2>&1 | FileCheck %s --check-prefix=TOOFEW
+; RUN: not llvm-as < %t/too-many.ll -disable-output 2>&1 | FileCheck %s --check-prefix=TOOMANY
+; RUN: not llvm-as < %t/bad-behavior.ll -disable-output 2>&1 | FileCheck %s --check-prefix=BEHAVIOR
+
+;--- not-string.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", i32 1}
+; NOTSTRING: exception-model metadata requires a string argument
+
+;--- bad-value.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"seh"}
+; BADVALUE: invalid exception-model metadata value
+
+;--- empty-value.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !""}
+; EMPTY: invalid exception-model metadata value
+
+;--- too-few.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model"}
+; TOOFEW: incorrect number of operands in module flag
+
+;--- too-many.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"exception-model", !"sjlj", !"extra"}
+; TOOMANY: incorrect number of operands in module flag
+
+;--- bad-behavior.ll
+!llvm.module.flags = !{!0}
+!0 = !{i32 2, !"exception-model", !"sjlj"}
+; BEHAVIOR: exception-model module flag must use 'error' merge behavior
More information about the llvm-commits
mailing list