1//===- EHABILowering.cpp - Lower flattened CIR EH ops to ABI-specific form ===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a pass that lowers ABI-agnostic flattened CIR exception
10// handling operations into an ABI-specific form. Currently only the Itanium
11// C++ ABI is supported.
12//
13// The Itanium ABI lowering performs these transformations:
14// - cir.eh.initiate → cir.eh.inflight_exception (landing pad)
15// - cir.eh.dispatch → cir.eh.typeid + cir.cmp + cir.brcond chains
16// - cir.begin_cleanup → (removed)
17// - cir.end_cleanup → (removed)
18// - cir.begin_catch → call to __cxa_begin_catch
19// - cir.end_catch → call to __cxa_end_catch
20// - cir.eh.terminate → call to __clang_call_terminate + unreachable
21// - cir.eh.unexpected → call to __cxa_call_unexpected + unreachable
22// - cir.resume → cir.resume.flat
23// - !cir.eh_token values → (!cir.ptr<!void>, !u32i) value pairs
24// - cir.construct_catch_param → __cxa_get_exception_ptr + inlined
25// catch-copy thunk body, or a store of the
26// exception object address for
27// reference_to_pointer
28// - personality function set on functions requiring EH and removed from
29// functions without landing pads
30//
31//===----------------------------------------------------------------------===//
32
33#include "PassDetail.h"
34#include "mlir/IR/Builders.h"
35#include "mlir/IR/IRMapping.h"
36#include "mlir/IR/PatternMatch.h"
37#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
38#include "clang/CIR/Dialect/IR/CIRDialect.h"
39#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
40#include "clang/CIR/Dialect/IR/CIRTypes.h"
41#include "clang/CIR/Dialect/Passes.h"
42#include "clang/CIR/Dialect/Transforms/CIRTransformUtils.h"
43#include "clang/CIR/MissingFeatures.h"
44#include "llvm/ADT/DenseMap.h"
45#include "llvm/ADT/SetVector.h"
46#include "llvm/ADT/SmallVector.h"
47#include "llvm/TargetParser/Triple.h"
48
49using namespace mlir;
50using namespace cir;
51
52namespace mlir {
53#define GEN_PASS_DEF_CIREHABILOWERING
54#include "clang/CIR/Dialect/Passes.h.inc"
55} // namespace mlir
56
57namespace {
58
59//===----------------------------------------------------------------------===//
60// Shared utilities
61//===----------------------------------------------------------------------===//
62
63/// Ensure a function with the given name and type exists in the module. If it
64/// does not exist, create a private external declaration.
65static cir::FuncOp getOrCreateRuntimeFuncDecl(mlir::ModuleOp mod,
66 mlir::Location loc,
67 StringRef name,
68 cir::FuncType funcTy) {
69 if (auto existing = mod.lookupSymbol<cir::FuncOp>(name))
70 return existing;
71
72 mlir::OpBuilder builder(mod.getContext());
73 builder.setInsertionPointToEnd(mod.getBody());
74 auto funcOp = cir::FuncOp::create(builder, location: loc, sym_name: name, type: funcTy);
75 funcOp.setLinkage(cir::GlobalLinkageKind::ExternalLinkage);
76 funcOp.setPrivate();
77 funcOp->setAttr(name: cir::CIRDialect::getRuntimeFuncAttrName(),
78 value: builder.getUnitAttr());
79 // This pass runs after CallConvLowering and builds the declaration in its
80 // target ABI form already.
81 funcOp.setRequiresCallConvLowering(false);
82 return funcOp;
83}
84
85//===----------------------------------------------------------------------===//
86// EH ABI Lowering Base Class
87//===----------------------------------------------------------------------===//
88
89/// Abstract base class for exception-handling ABI lowering.
90/// Each supported ABI (Itanium, Microsoft, etc.) provides a concrete subclass.
91class EHABILowering {
92public:
93 explicit EHABILowering(mlir::ModuleOp mod)
94 : mod(mod), ctx(mod.getContext()), builder(ctx) {}
95 virtual ~EHABILowering() = default;
96
97 /// Lower all EH operations in the module to an ABI-specific form.
98 virtual mlir::LogicalResult run() = 0;
99
100protected:
101 mlir::ModuleOp mod;
102 mlir::MLIRContext *ctx;
103 mlir::OpBuilder builder;
104};
105
106//===----------------------------------------------------------------------===//
107// Itanium EH ABI Lowering
108//===----------------------------------------------------------------------===//
109
110/// Lowers flattened CIR EH operations to the Itanium C++ ABI form.
111///
112/// The entry point is run(), which iterates over all functions and
113/// calls lowerFunc() for each. lowerFunc() drives all lowering from
114/// cir.eh.initiate operations: every other EH op (begin/end_cleanup,
115/// eh.dispatch, begin/end_catch, resume) is reachable by tracing the
116/// eh_token produced by the initiate through its users.
117class ItaniumEHLowering : public EHABILowering {
118public:
119 using EHABILowering::EHABILowering;
120 mlir::LogicalResult run() override;
121
122private:
123 /// Maps a !cir.eh_token value to its Itanium ABI replacement pair:
124 /// an exception pointer (!cir.ptr<!void>) and a type id (!u32i).
125 using EhTokenMap = DenseMap<mlir::Value, std::pair<mlir::Value, mlir::Value>>;
126
127 cir::VoidType voidType;
128 cir::PointerType voidPtrType;
129 cir::PointerType u8PtrType;
130 cir::IntType u32Type;
131 cir::IntType s32Type;
132
133 // Cached runtime function declarations, initialized when needed by
134 // ensureRuntimeDecls().
135 cir::FuncOp personalityFunc;
136 cir::FuncOp beginCatchFunc;
137 cir::FuncOp endCatchFunc;
138 cir::FuncOp getExceptionPtrFunc;
139 cir::FuncOp clangCallTerminateFunc;
140 cir::FuncOp cxaThrowFunc;
141 cir::FuncOp cxaRethrowFunc;
142 cir::FuncOp cxaCallUnexpectedFunc;
143
144 DenseMap<mlir::StringAttr, cir::FuncOp> catchCopyThunks;
145
146 llvm::SmallSetVector<mlir::StringAttr, 1> droppedPersonalities;
147
148 constexpr const static ::llvm::StringLiteral kGxxPersonality =
149 "__gxx_personality_v0";
150
151 void ensureRuntimeDecls(mlir::Location loc);
152 void ensureClangCallTerminate(mlir::Location loc);
153 void ensureCxaThrowDecl(mlir::Location loc);
154 void ensureCxaRethrowDecl(mlir::Location loc);
155 void ensureCxaCallUnexpectedDecl(mlir::Location loc);
156 mlir::Block *buildTerminateBlock(cir::FuncOp funcOp, mlir::Location loc);
157 mlir::FailureOr<cir::FuncOp>
158 resolveCatchCopyThunk(cir::ConstructCatchParamOp op);
159 mlir::LogicalResult lowerFunc(cir::FuncOp funcOp);
160 mlir::LogicalResult
161 lowerEhInitiate(cir::EhInitiateOp initiateOp,
162 llvm::ArrayRef<cir::EhDispatchOp> reachedDispatches,
163 bool reachesCleanup, EhTokenMap &ehTokenMap);
164 void lowerDispatch(cir::EhDispatchOp dispatch, mlir::Value exnPtr,
165 mlir::Value typeId);
166 mlir::LogicalResult lowerConstructCatchParam(cir::ConstructCatchParamOp op,
167 mlir::Value exnPtr);
168 void lowerInitCatchParam(cir::InitCatchParamOp op);
169 mlir::LogicalResult lowerTryThrow(cir::TryThrowOp op);
170};
171
172/// Lower all EH operations in the module to the Itanium-specific form.
173mlir::LogicalResult ItaniumEHLowering::run() {
174 // Pre-compute the common types used throughout all function lowerings.
175 // TODO(cir): Move these to the base class if they are also needed for MSVC.
176 voidType = cir::VoidType::get(ctx);
177 voidPtrType = cir::PointerType::get(pointee: voidType);
178 auto u8Type = cir::IntType::get(context: ctx, width: 8, /*isSigned=*/is_signed: false);
179 u8PtrType = cir::PointerType::get(pointee: u8Type);
180 u32Type = cir::IntType::get(context: ctx, width: 32, /*isSigned=*/is_signed: false);
181 s32Type = cir::IntType::get(context: ctx, width: 32, /*isSigned=*/is_signed: true);
182
183 for (cir::FuncOp funcOp : mod.getOps<cir::FuncOp>()) {
184 if (mlir::failed(Result: lowerFunc(funcOp)))
185 return mlir::failure();
186 }
187
188 for (mlir::StringAttr name : droppedPersonalities) {
189 auto personalityDecl = mod.lookupSymbol<cir::FuncOp>(name);
190 if (personalityDecl && personalityDecl.isDeclaration() &&
191 mlir::SymbolTable::symbolKnownUseEmpty(symbol: personalityDecl, from: mod))
192 personalityDecl.erase();
193 }
194 return mlir::success();
195}
196
197/// Ensure the necessary Itanium runtime function declarations exist in the
198/// module.
199void ItaniumEHLowering::ensureRuntimeDecls(mlir::Location loc) {
200 // TODO(cir): Handle other personality functions. This probably isn't needed
201 // here if we fix codegen to always set the personality function.
202 if (!personalityFunc) {
203 auto personalityFuncTy = cir::FuncType::get(inputs: {}, returnType: s32Type, /*isVarArg=*/true);
204 personalityFunc = getOrCreateRuntimeFuncDecl(mod, loc, name: kGxxPersonality,
205 funcTy: personalityFuncTy);
206 }
207
208 if (!beginCatchFunc) {
209 auto beginCatchFuncTy =
210 cir::FuncType::get(inputs: {voidPtrType}, returnType: u8PtrType, /*isVarArg=*/false);
211 beginCatchFunc = getOrCreateRuntimeFuncDecl(mod, loc, name: "__cxa_begin_catch",
212 funcTy: beginCatchFuncTy);
213 }
214
215 if (!endCatchFunc) {
216 auto endCatchFuncTy = cir::FuncType::get(inputs: {}, returnType: voidType, /*isVarArg=*/false);
217 endCatchFunc =
218 getOrCreateRuntimeFuncDecl(mod, loc, name: "__cxa_end_catch", funcTy: endCatchFuncTy);
219 }
220
221 if (!getExceptionPtrFunc) {
222 auto getExceptionPtrFuncTy =
223 cir::FuncType::get(inputs: {voidPtrType}, returnType: u8PtrType, /*isVarArg=*/false);
224 getExceptionPtrFunc = getOrCreateRuntimeFuncDecl(
225 mod, loc, name: "__cxa_get_exception_ptr", funcTy: getExceptionPtrFuncTy);
226 }
227}
228
229/// Ensure the __clang_call_terminate function exists in the module. This
230/// function is defined with a body that calls __cxa_begin_catch followed by
231/// std::terminate, matching the behavior of Clang's LLVM IR codegen.
232///
233/// void __clang_call_terminate(void *exn) nounwind noreturn {
234/// __cxa_begin_catch(exn);
235/// std::terminate();
236/// unreachable;
237/// }
238void ItaniumEHLowering::ensureClangCallTerminate(mlir::Location loc) {
239 // TODO(cir): set the runtime calling convention on the runtime calls below.
240 assert(!cir::MissingFeatures::opFuncCallingConv());
241 if (clangCallTerminateFunc)
242 return;
243
244 ensureRuntimeDecls(loc);
245
246 if (auto existing = mod.lookupSymbol<cir::FuncOp>(name: "__clang_call_terminate")) {
247 clangCallTerminateFunc = existing;
248 return;
249 }
250
251 auto funcTy = cir::FuncType::get(inputs: {voidPtrType}, returnType: voidType, /*isVarArg=*/false);
252 builder.setInsertionPointToEnd(mod.getBody());
253 auto funcOp =
254 cir::FuncOp::create(builder, location: loc, sym_name: "__clang_call_terminate", type: funcTy);
255 funcOp.setLinkage(cir::GlobalLinkageKind::LinkOnceODRLinkage);
256 funcOp.setGlobalVisibility(cir::VisibilityKind::Hidden);
257 // This pass runs after CallConvLowering and builds the function in its
258 // target ABI form already.
259 funcOp.setRequiresCallConvLowering(false);
260
261 mlir::Block *entryBlock = funcOp.addEntryBlock();
262 builder.setInsertionPointToStart(entryBlock);
263 mlir::Value exnArg = entryBlock->getArgument(i: 0);
264
265 auto catchCall = cir::CallOp::create(
266 builder, location: loc, callee: mlir::FlatSymbolRefAttr::get(symbol: beginCatchFunc), resType: u8PtrType,
267 operands: mlir::ValueRange{exnArg});
268 catchCall.setNothrowAttr(builder.getUnitAttr());
269
270 auto terminateFuncDecl = getOrCreateRuntimeFuncDecl(
271 mod, loc, name: "_ZSt9terminatev",
272 funcTy: cir::FuncType::get(inputs: {}, returnType: voidType, /*isVarArg=*/false));
273 terminateFuncDecl->setAttr(name: cir::CIRDialect::getNoReturnAttrName(),
274 value: builder.getUnitAttr());
275 auto terminateCall = cir::CallOp::create(
276 builder, location: loc, callee: mlir::FlatSymbolRefAttr::get(symbol: terminateFuncDecl), resType: voidType,
277 operands: mlir::ValueRange{});
278 terminateCall.setNothrowAttr(builder.getUnitAttr());
279 terminateCall->setAttr(name: cir::CIRDialect::getNoReturnAttrName(),
280 value: builder.getUnitAttr());
281
282 cir::UnreachableOp::create(builder, location: loc);
283
284 funcOp->setAttr(name: cir::CIRDialect::getNoReturnAttrName(),
285 value: builder.getUnitAttr());
286 clangCallTerminateFunc = funcOp;
287}
288
289/// Ensure the __cxa_throw runtime function is declared in the module.
290///
291/// void __cxa_throw(void *exception, void *type_info, void *dtor);
292void ItaniumEHLowering::ensureCxaThrowDecl(mlir::Location loc) {
293 if (cxaThrowFunc)
294 return;
295 auto throwFuncTy = cir::FuncType::get(inputs: {voidPtrType, voidPtrType, voidPtrType},
296 returnType: voidType, /*isVarArg=*/false);
297 cxaThrowFunc =
298 getOrCreateRuntimeFuncDecl(mod, loc, name: "__cxa_throw", funcTy: throwFuncTy);
299}
300
301/// Ensure the __cxa_rethrow runtime function is declared in the module.
302///
303/// void __cxa_rethrow();
304void ItaniumEHLowering::ensureCxaRethrowDecl(mlir::Location loc) {
305 if (cxaRethrowFunc)
306 return;
307 auto rethrowFuncTy = cir::FuncType::get(inputs: {}, returnType: voidType, /*isVarArg=*/false);
308 cxaRethrowFunc =
309 getOrCreateRuntimeFuncDecl(mod, loc, name: "__cxa_rethrow", funcTy: rethrowFuncTy);
310}
311
312/// void __cxa_call_unexpected(void *exn);
313void ItaniumEHLowering::ensureCxaCallUnexpectedDecl(mlir::Location loc) {
314 if (cxaCallUnexpectedFunc)
315 return;
316 auto unexpectedFuncTy =
317 cir::FuncType::get(inputs: {voidPtrType}, returnType: voidType, /*isVarArg=*/false);
318 cxaCallUnexpectedFunc = getOrCreateRuntimeFuncDecl(
319 mod, loc, name: "__cxa_call_unexpected", funcTy: unexpectedFuncTy);
320}
321
322/// Create a terminate landing pad block at the end of the specified function.
323mlir::Block *ItaniumEHLowering::buildTerminateBlock(cir::FuncOp funcOp,
324 mlir::Location loc) {
325 // TODO(cir): set the runtime calling convention on the runtime calls below.
326 assert(!cir::MissingFeatures::opFuncCallingConv());
327 assert(clangCallTerminateFunc &&
328 "ensureClangCallTerminate must run before buildTerminateBlock");
329 mlir::Region &body = funcOp.getRegion();
330 mlir::Block *terminateBlock = builder.createBlock(parent: &body, insertPt: body.end());
331 auto inflight = cir::EhInflightOp::create(
332 builder, location: loc, /*cleanup=*/false, /*catch_all=*/true,
333 /*catch_type_list=*/mlir::ArrayAttr{},
334 /*filter_type_list=*/mlir::ArrayAttr{});
335 auto terminateCall = cir::CallOp::create(
336 builder, location: loc, callee: mlir::FlatSymbolRefAttr::get(symbol: clangCallTerminateFunc),
337 resType: voidType, operands: mlir::ValueRange{inflight.getExceptionPtr()});
338 terminateCall.setNothrowAttr(builder.getUnitAttr());
339 terminateCall->setAttr(name: cir::CIRDialect::getNoReturnAttrName(),
340 value: builder.getUnitAttr());
341 cir::UnreachableOp::create(builder, location: loc);
342 return terminateBlock;
343}
344
345/// Read-only walk of the eh_token graph from an initiate's root token,
346/// collecting every cir.eh.dispatch its exception can reach, innermost first.
347/// The token flows through cleanups to the innermost dispatch and then, via
348/// that dispatch's continue-unwind edge, on to each enclosing dispatch (nested
349/// try/catch). The walk follows the token into catch-handler blocks too, but
350/// it dead-ends there because cir.begin_catch consumes the eh_token (producing
351/// a catch_token), so only the unwind chain yields further dispatches.
352///
353/// This is computed before any destructive lowering so a landing pad's catch
354/// types -- which are a property of the EH graph -- do not depend on the order
355/// in which the destructive per-initiate traversal tears down shared
356/// token-graph edges. \p dispatches is left empty for a cleanup-only initiate
357/// that reaches no dispatch (e.g. a path that only resumes).
358static void collectReachableDispatches(
359 mlir::Value rootToken,
360 llvm::SmallSetVector<cir::EhDispatchOp, 4> &dispatches,
361 bool &reachesCleanup) {
362 llvm::SmallVector<mlir::Value> worklist;
363 llvm::SmallPtrSet<mlir::Value, 8> visited;
364 worklist.push_back(Elt: rootToken);
365 // Breadth-first (process in insertion order) so dispatches are discovered
366 // innermost first, matching the order catch clauses must appear in the
367 // landing pad.
368 for (unsigned i = 0; i < worklist.size(); ++i) {
369 mlir::Value current = worklist[i];
370 if (!visited.insert(Ptr: current).second)
371 continue;
372 for (mlir::OpOperand &use : current.getUses()) {
373 mlir::Operation *user = use.getOwner();
374 // A cleanup anywhere on the unwind path (this initiate's own cleanup or
375 // an enclosing scope's) means the landing pad must carry the cleanup
376 // clause so destructors still run when a foreign exception unwinds
377 // through this frame.
378 if (mlir::isa<cir::BeginCleanupOp>(Val: user))
379 reachesCleanup = true;
380 if (auto dispatch = mlir::dyn_cast<cir::EhDispatchOp>(Val: user))
381 dispatches.insert(X: dispatch);
382 // Follow the token into eh_token block arguments of successor blocks.
383 for (unsigned s = 0, e = user->getNumSuccessors(); s < e; ++s)
384 for (mlir::BlockArgument arg : user->getSuccessor(index: s)->getArguments())
385 if (mlir::isa<cir::EhTokenType>(Val: arg.getType()))
386 worklist.push_back(Elt: arg);
387 }
388 }
389}
390
391/// Lower all EH operations in a single function.
392mlir::LogicalResult ItaniumEHLowering::lowerFunc(cir::FuncOp funcOp) {
393 if (funcOp.isDeclaration())
394 return mlir::success();
395
396 // All EH lowering follows from cir.eh.initiate operations. The token each
397 // initiate produces connects it to every other EH op in the function
398 // (begin/end_cleanup, eh.dispatch, begin/end_catch, resume) through the
399 // token graph. A single walk to collect initiates is therefore sufficient.
400 SmallVector<cir::EhInitiateOp> initiateOps;
401 bool hasLandingPad = false;
402 funcOp.walk(callback: [&](mlir::Operation *op) {
403 if (auto initiateOp = mlir::dyn_cast<cir::EhInitiateOp>(Val: op))
404 initiateOps.push_back(Elt: initiateOp);
405 else if (mlir::isa<cir::EhInflightOp>(Val: op))
406 hasLandingPad = true;
407 });
408 if (initiateOps.empty()) {
409 // Without a landing pad the personality is dead, as in classic codegen.
410 mlir::FlatSymbolRefAttr personality = funcOp.getPersonalityAttr();
411 if (personality && !hasLandingPad) {
412 droppedPersonalities.insert(X: personality.getAttr());
413 funcOp.removePersonalityAttr();
414 }
415 return mlir::success();
416 }
417
418 ensureRuntimeDecls(loc: funcOp.getLoc());
419
420 // Set the personality function if it is not already set.
421 // TODO(cir): The personality function should already have been set by this
422 // point. If we've seen a try operation, it will have been set by
423 // emitCXXTryStmt. If we only have cleanups, it may not have been set. We
424 // need to fix that in CodeGen. This is a placeholder until that is done.
425 if (!funcOp.getPersonality())
426 funcOp.setPersonality(kGxxPersonality);
427
428 // Compute, read-only and before any destructive lowering, the dispatches each
429 // initiate's exception can reach (innermost first; more than one for nested
430 // try/catch). A landing pad's catch types are a property of the EH graph, so
431 // deriving them here keeps them independent of the order in which the
432 // destructive per-initiate traversal in lowerEhInitiate tears down shared
433 // token-graph edges. Otherwise a sibling or outer dispatch could be missed,
434 // leaving a landing pad without its catch clause (it would resume past the
435 // handler to std::terminate) or an un-lowered leftover dispatch.
436 // Per initiate: the dispatches its exception can reach (innermost first) and
437 // whether a cleanup lies on its unwind path. Both are properties of the EH
438 // graph and are always consumed together for the same initiate, so they live
439 // in one map keyed by the initiate op.
440 struct InitiateEHInfo {
441 llvm::SmallSetVector<cir::EhDispatchOp, 4> reachedDispatches;
442 bool reachesCleanup = false;
443 };
444 llvm::DenseMap<mlir::Operation *, InitiateEHInfo> initiateInfo;
445 llvm::SmallSetVector<cir::EhDispatchOp, 4> dispatchesToLower;
446 for (cir::EhInitiateOp initiateOp : initiateOps) {
447 InitiateEHInfo &info = initiateInfo[initiateOp.getOperation()];
448 collectReachableDispatches(rootToken: initiateOp.getEhToken(), dispatches&: info.reachedDispatches,
449 reachesCleanup&: info.reachesCleanup);
450 dispatchesToLower.insert(Start: info.reachedDispatches.begin(),
451 End: info.reachedDispatches.end());
452 }
453
454 EhTokenMap ehTokenMap;
455 for (cir::EhInitiateOp initiateOp : initiateOps) {
456 const InitiateEHInfo &info = initiateInfo[initiateOp.getOperation()];
457 if (mlir::failed(Result: lowerEhInitiate(initiateOp,
458 reachedDispatches: info.reachedDispatches.getArrayRef(),
459 reachesCleanup: info.reachesCleanup, ehTokenMap)))
460 return mlir::failure();
461 }
462
463 // Lower each dispatch exactly once. Every initiate's token block-argument
464 // (ptr, u32) replacements are registered in ehTokenMap by now, so each
465 // dispatch's own (exnPtr, typeId) pair is available. lowerDispatch erases
466 // the dispatch after building its comparison chain.
467 for (cir::EhDispatchOp dispatch : dispatchesToLower) {
468 auto [exnPtr, typeId] = ehTokenMap.lookup(Val: dispatch.getEhToken());
469 assert(exnPtr && typeId &&
470 "dispatch eh_token must be registered in ehTokenMap");
471 lowerDispatch(dispatch, exnPtr, typeId);
472 }
473
474 // Remove the !cir.eh_token block arguments that were replaced by (ptr, u32)
475 // pairs. Iterate in reverse to preserve argument indices during removal.
476 for (mlir::Block &block : funcOp.getBody()) {
477 for (int i = block.getNumArguments() - 1; i >= 0; --i) {
478 if (mlir::isa<cir::EhTokenType>(Val: block.getArgument(i).getType()))
479 block.eraseArgument(index: i);
480 }
481 }
482
483 // Lower any cir.init_catch_param ops in this function. These materialize
484 // the catch parameter local from the (already lowered) begin_catch result,
485 // and are independent of the eh_token graph traversal above.
486 SmallVector<cir::InitCatchParamOp> initCatchOps;
487 funcOp.walk(callback: [&](cir::InitCatchParamOp op) { initCatchOps.push_back(Elt: op); });
488 for (cir::InitCatchParamOp op : initCatchOps)
489 lowerInitCatchParam(op);
490
491 // Lower any cir.try_throw ops in this function to cir.try_call of
492 // __cxa_throw / __cxa_rethrow. These are produced by FlattenCFG when a
493 // cir.throw appears inside a cleanup scope or try region.
494 SmallVector<cir::TryThrowOp> tryThrowOps;
495 funcOp.walk(callback: [&](cir::TryThrowOp op) { tryThrowOps.push_back(Elt: op); });
496 for (cir::TryThrowOp op : tryThrowOps)
497 if (mlir::failed(Result: lowerTryThrow(op)))
498 return mlir::failure();
499
500 return mlir::success();
501}
502
503/// Lower all EH operations connected to a single cir.eh.initiate.
504///
505/// The cir.eh.initiate is the root of a token graph. The token it produces
506/// flows through branch edges to consuming operations:
507///
508/// cir.eh.initiate → (via cir.br) → cir.begin_cleanup
509/// → cir.end_cleanup (via cleanup_token)
510/// → (via cir.br) → cir.eh.dispatch
511/// → (successors) →
512/// cir.begin_catch
513/// → cir.end_catch
514/// (via catch_token)
515/// → cir.resume
516///
517/// A single traversal of the token graph discovers and processes every
518/// connected op inline. The inflight_exception is created up-front without
519/// a catch_type_list; when the dispatch is encountered during traversal,
520/// the catch types are read and set on the inflight op.
521///
522/// Dispatch ops are not lowered here; they are lowered once by the caller after
523/// every initiate has been processed (a dispatch can be shared by sibling
524/// initiates), so this traversal only registers the catch-handler block
525/// arguments reachable through them.
526///
527/// \p ehTokenMap is shared across all initiates in the function so that block
528/// arguments reachable from multiple sibling initiates are registered once.
529mlir::LogicalResult ItaniumEHLowering::lowerEhInitiate(
530 cir::EhInitiateOp initiateOp,
531 llvm::ArrayRef<cir::EhDispatchOp> reachedDispatches, bool reachesCleanup,
532 EhTokenMap &ehTokenMap) {
533 // TODO(cir): set the runtime calling convention on the runtime calls below.
534 assert(!cir::MissingFeatures::opFuncCallingConv());
535 mlir::Value rootToken = initiateOp.getEhToken();
536
537 // The catch clauses for this landing pad come from the dispatches its
538 // exception reaches (computed read-only by the caller before any destructive
539 // lowering). For nested try/catch the exception can reach several dispatches
540 // innermost first, so the landing pad lists their catch types in that order
541 // (matching classic CodeGen), stopping at a catch-all since nothing escapes
542 // it. Deriving this from the original EH graph -- rather than during the
543 // destructive token-graph traversal below -- keeps it correct regardless of
544 // the order in which sibling/nested initiates are lowered.
545 mlir::ArrayAttr catchTypeList;
546 mlir::ArrayAttr filterTypeList;
547 bool catchAll = false;
548 SmallVector<mlir::Attribute> typeSymbols;
549 for (cir::EhDispatchOp dispatch : reachedDispatches) {
550 if (mlir::ArrayAttr catchTypes = dispatch.getCatchTypesAttr()) {
551 for (mlir::Attribute attr : catchTypes) {
552 if (auto filter = mlir::dyn_cast<cir::EhFilterAttr>(Val&: attr)) {
553 SmallVector<mlir::Attribute> filterSymbols;
554 // A filter terminates the landing-pad clause list the same way a
555 // catch-all does. Nothing outside the specification is reachable.
556 for (mlir::Attribute typeAttr : filter.getPermittedTypes()) {
557 auto globalView = mlir::cast<cir::GlobalViewAttr>(Val&: typeAttr);
558 filterSymbols.push_back(Elt: globalView.getSymbol());
559 }
560 filterTypeList = builder.getArrayAttr(value: filterSymbols);
561 continue;
562 }
563 typeSymbols.push_back(
564 Elt: mlir::cast<cir::GlobalViewAttr>(Val&: attr).getSymbol());
565 }
566 }
567 if (dispatch.getDefaultIsCatchAll() || filterTypeList) {
568 catchAll = dispatch.getDefaultIsCatchAll();
569 // A catch-all or filter handles the remaining exceptions, so it
570 // stops the unwind. No enclosing dispatch is reachable past it.
571 assert(dispatch == reachedDispatches.back() &&
572 "catch-all or filter must be the last reachable dispatch");
573 break;
574 }
575 }
576 if (!typeSymbols.empty())
577 catchTypeList = builder.getArrayAttr(value: typeSymbols);
578
579 builder.setInsertionPoint(initiateOp);
580 auto inflightOp = cir::EhInflightOp::create(
581 builder, location: initiateOp.getLoc(), cleanup: initiateOp.getCleanup() || reachesCleanup,
582 catch_all: catchAll, catch_type_list: catchTypeList, filter_type_list: filterTypeList);
583
584 ehTokenMap[rootToken] = {inflightOp.getExceptionPtr(),
585 inflightOp.getTypeId()};
586
587 // Single traversal of the token graph. For each token value (the root token
588 // or a block argument that carries it), we snapshot its users, register
589 // (ptr, u32) replacement arguments on successor blocks, then process every
590 // user inline. This avoids collecting ops into separate vectors.
591 SmallVector<mlir::Value> worklist;
592 SmallPtrSet<mlir::Value, 8> visited;
593 worklist.push_back(Elt: rootToken);
594
595 while (!worklist.empty()) {
596 mlir::Value current = worklist.pop_back_val();
597 if (!visited.insert(Ptr: current).second)
598 continue;
599
600 // Snapshot users before modifying any of them (erasing ops during
601 // iteration would invalidate the use-list iterator).
602 SmallVector<mlir::Operation *> users;
603 for (mlir::OpOperand &use : current.getUses())
604 users.push_back(Elt: use.getOwner());
605
606 // Register replacement block arguments on successor blocks (extending the
607 // worklist), then lower the op itself.
608 for (mlir::Operation *user : users) {
609 // Trace into successor blocks to register (ptr, u32) replacement
610 // arguments for any !cir.eh_token block arguments found there. Even
611 // if a block arg was already registered by a sibling initiate, it is
612 // still added to the worklist so that the traversal can reach the
613 // shared dispatch to read catch types.
614 for (unsigned s = 0; s < user->getNumSuccessors(); ++s) {
615 mlir::Block *succ = user->getSuccessor(index: s);
616 for (mlir::BlockArgument arg : succ->getArguments()) {
617 if (!mlir::isa<cir::EhTokenType>(Val: arg.getType()))
618 continue;
619 if (!ehTokenMap.count(Val: arg)) {
620 mlir::Value ptrArg = succ->addArgument(type: voidPtrType, loc: arg.getLoc());
621 mlir::Value u32Arg = succ->addArgument(type: u32Type, loc: arg.getLoc());
622 ehTokenMap[arg] = {ptrArg, u32Arg};
623 }
624 worklist.push_back(Elt: arg);
625 }
626 }
627
628 if (auto op = mlir::dyn_cast<cir::BeginCleanupOp>(Val: user)) {
629 // begin_cleanup / end_cleanup are no-ops for Itanium. Erase the
630 // end_cleanup first (drops the cleanup_token use) then the begin.
631 for (auto &tokenUsers :
632 llvm::make_early_inc_range(Range: op.getCleanupToken().getUses())) {
633 if (auto endOp =
634 mlir::dyn_cast<cir::EndCleanupOp>(Val: tokenUsers.getOwner()))
635 endOp.erase();
636 }
637 op.erase();
638 } else if (auto op = mlir::dyn_cast<cir::BeginCatchOp>(Val: user)) {
639 // Replace end_catch → __cxa_end_catch (drops the catch_token use),
640 // then replace begin_catch → __cxa_begin_catch.
641 for (auto &tokenUsers :
642 llvm::make_early_inc_range(Range: op.getCatchToken().getUses())) {
643 if (auto endOp =
644 mlir::dyn_cast<cir::EndCatchOp>(Val: tokenUsers.getOwner())) {
645 builder.setInsertionPoint(endOp);
646 cir::CallOp::create(builder, location: endOp.getLoc(),
647 callee: mlir::FlatSymbolRefAttr::get(symbol: endCatchFunc),
648 resType: voidType, operands: mlir::ValueRange{});
649 endOp.erase();
650 }
651 }
652
653 auto [exnPtr, typeId] = ehTokenMap.lookup(Val: op.getEhToken());
654 builder.setInsertionPoint(op);
655 auto callOp = cir::CallOp::create(
656 builder, location: op.getLoc(), callee: mlir::FlatSymbolRefAttr::get(symbol: beginCatchFunc),
657 resType: u8PtrType, operands: mlir::ValueRange{exnPtr});
658 mlir::Value castResult = callOp.getResult();
659 mlir::Type expectedPtrType = op.getExnPtr().getType();
660 if (castResult.getType() != expectedPtrType)
661 castResult =
662 cir::CastOp::create(builder, location: op.getLoc(), result: expectedPtrType,
663 kind: cir::CastKind::bitcast, src: callOp.getResult());
664 op.getExnPtr().replaceAllUsesWith(newValue: castResult);
665 op.erase();
666 } else if (auto op = mlir::dyn_cast<cir::ConstructCatchParamOp>(Val: user)) {
667 auto [exnPtr, typeId] = ehTokenMap.lookup(Val: op.getEhToken());
668 if (mlir::failed(Result: lowerConstructCatchParam(op, exnPtr)))
669 return mlir::failure();
670 } else if (mlir::isa<cir::EhDispatchOp>(Val: user)) {
671 // The dispatch's catch types were already read into every reaching
672 // landing pad (read-only, before this traversal). The dispatch op
673 // itself is lowered once by the caller after all initiates are
674 // processed, so nothing is done here; the successor catch-handler
675 // blocks are still reached via the block-argument registration above.
676 } else if (auto op = mlir::dyn_cast<cir::EhTerminateOp>(Val: user)) {
677 auto [exnPtr, typeId] = ehTokenMap.lookup(Val: op.getEhToken());
678 ensureClangCallTerminate(loc: op.getLoc());
679 builder.setInsertionPoint(op);
680 auto call = cir::CallOp::create(
681 builder, location: op.getLoc(),
682 callee: mlir::FlatSymbolRefAttr::get(symbol: clangCallTerminateFunc), resType: voidType,
683 operands: mlir::ValueRange{exnPtr});
684 call.setNothrowAttr(builder.getUnitAttr());
685 call->setAttr(name: cir::CIRDialect::getNoReturnAttrName(),
686 value: builder.getUnitAttr());
687 cir::UnreachableOp::create(builder, location: op.getLoc());
688 op.erase();
689 } else if (auto op = mlir::dyn_cast<cir::EhUnexpectedOp>(Val: user)) {
690 auto [exnPtr, typeId] = ehTokenMap.lookup(Val: op.getEhToken());
691 ensureCxaCallUnexpectedDecl(loc: op.getLoc());
692 builder.setInsertionPoint(op);
693 auto call = cir::CallOp::create(
694 builder, location: op.getLoc(),
695 callee: mlir::FlatSymbolRefAttr::get(symbol: cxaCallUnexpectedFunc), resType: voidType,
696 operands: mlir::ValueRange{exnPtr});
697 call->setAttr(name: cir::CIRDialect::getNoReturnAttrName(),
698 value: builder.getUnitAttr());
699 cir::UnreachableOp::create(builder, location: op.getLoc());
700 op.erase();
701 } else if (auto op = mlir::dyn_cast<cir::ResumeOp>(Val: user)) {
702 auto [exnPtr, typeId] = ehTokenMap.lookup(Val: op.getEhToken());
703 builder.setInsertionPoint(op);
704 cir::ResumeFlatOp::create(builder, location: op.getLoc(), exception_ptr: exnPtr, type_id: typeId);
705 op.erase();
706 } else if (auto op = mlir::dyn_cast<cir::BrOp>(Val: user)) {
707 // Replace eh_token operands with the (ptr, u32) pair.
708 SmallVector<mlir::Value> newOperands;
709 bool changed = false;
710 for (mlir::Value operand : op.getDestOperands()) {
711 auto it = ehTokenMap.find(Val: operand);
712 if (it != ehTokenMap.end()) {
713 newOperands.push_back(Elt: it->second.first);
714 newOperands.push_back(Elt: it->second.second);
715 changed = true;
716 } else {
717 newOperands.push_back(Elt: operand);
718 }
719 }
720 if (changed) {
721 builder.setInsertionPoint(op);
722 // We shouldn't get any loop information here.
723 assert(!op.getLoopIdAttr() && !op.getLoopHintsAttr());
724 cir::BrOp::create(builder, location: op.getLoc(), destOperands: newOperands,
725 /*loop_id=*/nullptr, /*loop_hints=*/nullptr,
726 dest: op.getDest());
727 op.erase();
728 }
729 }
730 }
731 }
732
733 initiateOp.erase();
734 return mlir::success();
735}
736
737/// Lower a cir.eh.dispatch by creating a comparison chain in new blocks.
738/// The dispatch itself is replaced with a branch to the first comparison
739/// block and then erased.
740void ItaniumEHLowering::lowerDispatch(cir::EhDispatchOp dispatch,
741 mlir::Value exnPtr, mlir::Value typeId) {
742 mlir::Location dispLoc = dispatch.getLoc();
743 mlir::Block *defaultDest = dispatch.getDefaultDestination();
744 mlir::Block *dispatchBlock = dispatch->getBlock();
745
746 llvm::SmallVector<mlir::Attribute> catchAttrs;
747 llvm::SmallVector<mlir::Block *> catchDests;
748 cir::EhFilterAttr filterAttr;
749 mlir::Block *filterDest = nullptr;
750 if (mlir::ArrayAttr catchTypes = dispatch.getCatchTypesAttr()) {
751 for (auto [attr, dest] :
752 llvm::zip(t&: catchTypes, u: dispatch.getCatchDestinations())) {
753 if (auto filter = mlir::dyn_cast<cir::EhFilterAttr>(Val: attr)) {
754 assert(!filterAttr && "at most one filter handler");
755 filterAttr = filter;
756 filterDest = dest;
757 continue;
758 }
759 catchAttrs.push_back(Elt: attr);
760 catchDests.push_back(Elt: dest);
761 }
762 }
763
764 // Build the comparison chain in new blocks inserted after the dispatch's
765 // block. The dispatch itself is replaced with a branch to the first
766 // comparison block and erased below.
767 mlir::Block *insertBefore = dispatchBlock->getNextNode();
768 mlir::Block *falseDest = defaultDest;
769 if (filterAttr) {
770 assert(filterDest && "filter handler requires a destination");
771 // An empty permitted-type list means that every exception violates the
772 // specification, so the filter destination is taken unconditionally.
773 if (filterAttr.getPermittedTypes().empty()) {
774 falseDest = filterDest;
775 } else {
776 // The personality reports a filter failure with a negative selector.
777 // Type ids are still !u32i on the eh_token replacement pair, so recast
778 // to !s32i for the signed comparison. TODO: produce !s32i throughout.
779 auto *cmpBlock = builder.createBlock(insertBefore, argTypes: {voidPtrType, u32Type},
780 locs: {dispLoc, dispLoc});
781 mlir::Value cmpExnPtr = cmpBlock->getArgument(i: 0);
782 mlir::Value cmpTypeId = cmpBlock->getArgument(i: 1);
783 mlir::Value signedTypeId = cir::CastOp::create(
784 builder, location: dispLoc, result: s32Type, kind: cir::CastKind::integral, src: cmpTypeId);
785 mlir::Value zero = cir::ConstantOp::create(builder, location: dispLoc,
786 value: cir::IntAttr::get(type: s32Type, value: 0));
787 auto cmpOp = cir::CmpOp::create(builder, location: dispLoc, kind: cir::CmpOpKind::lt,
788 lhs: signedTypeId, rhs: zero);
789 cir::BrCondOp::create(builder, location: dispLoc, cond: cmpOp, destTrue: filterDest, destFalse: defaultDest,
790 destOperandsTrue: mlir::ValueRange{cmpExnPtr, cmpTypeId},
791 destOperandsFalse: mlir::ValueRange{cmpExnPtr, cmpTypeId});
792 insertBefore = cmpBlock;
793 falseDest = cmpBlock;
794 }
795 }
796
797 mlir::Block *firstCmpBlock = nullptr;
798 if (!catchAttrs.empty()) {
799 unsigned numCatches = catchAttrs.size();
800 for (int i = numCatches - 1; i >= 0; --i) {
801 auto *cmpBlock = builder.createBlock(insertBefore, argTypes: {voidPtrType, u32Type},
802 locs: {dispLoc, dispLoc});
803
804 mlir::Value cmpExnPtr = cmpBlock->getArgument(i: 0);
805 mlir::Value cmpTypeId = cmpBlock->getArgument(i: 1);
806
807 auto globalView = mlir::cast<cir::GlobalViewAttr>(Val&: catchAttrs[i]);
808 auto ehTypeIdOp =
809 cir::EhTypeIdOp::create(builder, location: dispLoc, type_sym: globalView.getSymbol());
810 auto cmpOp = cir::CmpOp::create(builder, location: dispLoc, kind: cir::CmpOpKind::eq,
811 lhs: cmpTypeId, rhs: ehTypeIdOp.getTypeId());
812
813 cir::BrCondOp::create(builder, location: dispLoc, cond: cmpOp, destTrue: catchDests[i], destFalse: falseDest,
814 destOperandsTrue: mlir::ValueRange{cmpExnPtr, cmpTypeId},
815 destOperandsFalse: mlir::ValueRange{cmpExnPtr, cmpTypeId});
816
817 insertBefore = cmpBlock;
818 falseDest = cmpBlock;
819 firstCmpBlock = cmpBlock;
820 }
821 } else {
822 firstCmpBlock = falseDest;
823 }
824
825 builder.setInsertionPoint(dispatch);
826 cir::BrOp::create(builder, location: dispLoc, dest: firstCmpBlock,
827 destOperands: mlir::ValueRange{exnPtr, typeId});
828
829 // The caller lowers each dispatch exactly once after every initiate has been
830 // processed, so no sibling still needs it; erase it now.
831 dispatch.erase();
832}
833
834mlir::FailureOr<cir::FuncOp>
835ItaniumEHLowering::resolveCatchCopyThunk(cir::ConstructCatchParamOp op) {
836 mlir::FlatSymbolRefAttr thunkRef = op.getCopyFnAttr();
837 mlir::StringAttr thunkName = thunkRef.getAttr();
838 auto cached = catchCopyThunks.find(Val: thunkName);
839 if (cached != catchCopyThunks.end())
840 return cached->second;
841
842 cir::FuncOp thunk = mod.lookupSymbol<cir::FuncOp>(symbol: thunkRef);
843 if (!thunk)
844 return op.emitError(message: "could not resolve catch-copy thunk symbol");
845 assert(thunk->hasAttr(cir::CIRDialect::getCatchCopyThunkAttrName()) &&
846 "verifier should have rejected non-thunk catch-copy reference");
847 if (thunk.isDeclaration())
848 return op.emitError(message: "catch-copy thunk has no body to inline");
849
850 mlir::Region &thunkRegion = thunk.getRegion();
851 if (!llvm::hasSingleElement(C&: thunkRegion))
852 return op.emitError(message: "multi-block catch-copy thunks are NYI");
853
854 mlir::Block &thunkEntry = thunkRegion.front();
855 assert(thunkEntry.getNumArguments() == 2 &&
856 "catch-copy thunk must have exactly two parameters");
857 if (!mlir::isa<cir::ReturnOp>(Val: thunkEntry.getTerminator()))
858 return op.emitError(message: "catch-copy thunk must end in cir.return");
859
860 catchCopyThunks[thunkName] = thunk;
861 return thunk;
862}
863
864/// Lower a cir.construct_catch_param into the Itanium-specific sequence
865/// that runs before `__cxa_begin_catch` to bind the catch parameter to the
866/// in-flight exception.
867mlir::LogicalResult
868ItaniumEHLowering::lowerConstructCatchParam(cir::ConstructCatchParamOp op,
869 mlir::Value exnPtr) {
870 // TODO(cir): set the runtime calling convention on the runtime calls below.
871 assert(!cir::MissingFeatures::opFuncCallingConv());
872 mlir::Location loc = op.getLoc();
873 mlir::Value paramAddr = op.getParamAddr();
874 cir::PointerType paramAddrType =
875 mlir::cast<cir::PointerType>(Val: paramAddr.getType());
876
877 if (op.getKind() == cir::InitCatchKind::ReferenceToPointer) {
878 assert(!MissingFeatures::sizeOfUnwindException());
879 constexpr unsigned headerSize = 32;
880
881 builder.setInsertionPoint(op);
882 auto index = cir::ConstantOp::create(
883 builder, location: loc, value: cir::IntAttr::get(type: u32Type, value: headerSize));
884 assert((exnPtr.getType() == voidPtrType || exnPtr.getType() == u8PtrType) &&
885 "lowerConstructCatchParam exn ptr not void* or i8*");
886 auto exnObj =
887 cir::PtrStrideOp::create(builder, location: loc, result: exnPtr.getType(), base: exnPtr, stride: index);
888 mlir::Value casted =
889 cir::CastOp::create(builder, location: loc, result: paramAddrType.getPointee(),
890 kind: cir::CastKind::bitcast, src: exnObj);
891 cir::StoreOp::create(builder, location: loc, value: casted, addr: paramAddr, is_volatile: {}, is_nontemporal: {}, alignment: {}, sync_scope: {}, mem_order: {});
892 op.erase();
893 return success();
894 }
895
896 if (op.getKind() != cir::InitCatchKind::NonTrivialCopy)
897 return op.emitOpError(message: "only reference_to_pointer and non_trivial_copy "
898 "kinds are supported");
899
900 ensureRuntimeDecls(loc);
901 ensureClangCallTerminate(loc);
902
903 // Call __cxa_get_exception_ptr to get the in-flight exception.
904 builder.setInsertionPoint(op);
905 cir::CallOp getExnCall = cir::CallOp::create(
906 builder, location: loc, callee: mlir::FlatSymbolRefAttr::get(symbol: getExceptionPtrFunc),
907 resType: u8PtrType, operands: mlir::ValueRange{exnPtr});
908 getExnCall.setNothrowAttr(builder.getUnitAttr());
909 mlir::Value adjusted =
910 cir::CastOp::create(builder, location: loc, result: paramAddrType, kind: cir::CastKind::bitcast,
911 src: getExnCall.getResult());
912
913 // Get the thunk function definition.
914 mlir::FailureOr<cir::FuncOp> thunkOr = resolveCatchCopyThunk(op);
915 if (mlir::failed(Result: thunkOr))
916 return mlir::failure();
917 cir::FuncOp thunk = *thunkOr;
918
919 // This is also verified by resolveCatchCopyThunk, but the loop below is
920 // where the constraint is required so let's assert it again here.
921 assert(llvm::hasSingleElement(thunk.getRegion()) &&
922 "multi-block catch-copy thunks are NYI");
923
924 // Clone the thunk function to perform the copy.
925 mlir::Block &thunkEntry = thunk.getRegion().front();
926 mlir::IRMapping mapping;
927 mapping.map(from: thunkEntry.getArgument(i: 0), to: paramAddr);
928 mapping.map(from: thunkEntry.getArgument(i: 1), to: adjusted);
929 llvm::SmallVector<cir::CallOp> throwingCalls;
930 for (mlir::Operation &thunkOp : thunkEntry.without_terminator()) {
931 mlir::Operation *cloned = builder.clone(op&: thunkOp, mapper&: mapping);
932 if (cir::CallOp callOp = mlir::dyn_cast<cir::CallOp>(Val: cloned))
933 if (!callOp.getNothrow())
934 throwingCalls.push_back(Elt: callOp);
935 }
936 op.erase();
937
938 if (throwingCalls.empty())
939 return mlir::success();
940
941 // All calls in the copy (which is usually just a single call) need to
942 // unwind to a terminate block if it throws an exception.
943 mlir::IRRewriter rewriter(builder);
944 mlir::Block *terminateBlock = nullptr;
945 for (cir::CallOp call : throwingCalls) {
946 if (!terminateBlock)
947 terminateBlock = buildTerminateBlock(funcOp: call->getParentOfType<cir::FuncOp>(),
948 loc: call.getLoc());
949 cir::replaceCallWithTryCall(callOp: call, unwindDest: terminateBlock, loc: call.getLoc(), rewriter);
950 }
951 return mlir::success();
952}
953
954/// Lower a cir.try_throw to a cir.try_call of __cxa_throw (or
955/// __cxa_rethrow for the no-operand rethrow form). Materializes the
956/// type_info and dtor pointers from their symbol attributes, bitcasting
957/// each to !cir.ptr<!void> as required by the runtime function signature.
958mlir::LogicalResult ItaniumEHLowering::lowerTryThrow(cir::TryThrowOp op) {
959 // TODO(cir): set the runtime calling convention on the runtime calls below.
960 assert(!cir::MissingFeatures::opFuncCallingConv());
961 mlir::Location loc = op.getLoc();
962 mlir::Block *normalDest = op.getNormalDest();
963 mlir::Block *unwindDest = op.getUnwindDest();
964 builder.setInsertionPoint(op);
965
966 if (op.rethrows()) {
967 ensureCxaRethrowDecl(loc);
968 cir::TryCallOp::create(
969 builder, location: loc, callee: mlir::FlatSymbolRefAttr::get(symbol: cxaRethrowFunc), resType: voidType,
970 normalDest, unwindDest, callOperands: mlir::ValueRange{});
971 op.erase();
972 return mlir::success();
973 }
974
975 ensureCxaThrowDecl(loc);
976
977 // Bitcast the exception pointer to void* if necessary.
978 mlir::Value exnPtr = op.getExceptionPtr();
979 if (exnPtr.getType() != voidPtrType)
980 exnPtr = cir::CastOp::create(builder, location: loc, result: voidPtrType,
981 kind: cir::CastKind::bitcast, src: exnPtr);
982
983 // Materialize the type_info pointer, looking up the typed symbol in the
984 // module so we get the correct pointer type for cir.get_global, then
985 // bitcasting to void* to match the runtime signature.
986 mlir::FlatSymbolRefAttr typeInfoAttr = op.getTypeInfoAttr();
987 auto typeInfoGlobal = mod.lookupSymbol<cir::GlobalOp>(symbol: typeInfoAttr);
988 if (!typeInfoGlobal)
989 return op.emitError(message: "type_info symbol not found in module");
990 auto typeInfoPtrTy = cir::PointerType::get(pointee: typeInfoGlobal.getSymType());
991 mlir::Value typeInfo = cir::GetGlobalOp::create(builder, location: loc, addr: typeInfoPtrTy,
992 name: typeInfoAttr.getValue());
993 if (typeInfo.getType() != voidPtrType)
994 typeInfo = cir::CastOp::create(builder, location: loc, result: voidPtrType,
995 kind: cir::CastKind::bitcast, src: typeInfo);
996
997 // Materialize the dtor pointer (or null if no dtor).
998 mlir::Value dtor;
999 if (mlir::FlatSymbolRefAttr dtorAttr = op.getDtorAttr()) {
1000 auto dtorFunc = mod.lookupSymbol<cir::FuncOp>(symbol: dtorAttr);
1001 if (!dtorFunc)
1002 return op.emitError(message: "dtor symbol not found in module");
1003 auto dtorPtrTy = cir::PointerType::get(pointee: dtorFunc.getFunctionType());
1004 dtor =
1005 cir::GetGlobalOp::create(builder, location: loc, addr: dtorPtrTy, name: dtorAttr.getValue());
1006 if (dtor.getType() != voidPtrType)
1007 dtor = cir::CastOp::create(builder, location: loc, result: voidPtrType,
1008 kind: cir::CastKind::bitcast, src: dtor);
1009 } else {
1010 dtor = cir::ConstantOp::create(
1011 builder, location: loc,
1012 value: cir::ConstPtrAttr::get(type: voidPtrType, value: builder.getI64IntegerAttr(value: 0)));
1013 }
1014
1015 cir::TryCallOp::create(
1016 builder, location: loc, callee: mlir::FlatSymbolRefAttr::get(symbol: cxaThrowFunc), resType: voidType,
1017 normalDest, unwindDest, callOperands: mlir::ValueRange{exnPtr, typeInfo, dtor});
1018 op.erase();
1019 return mlir::success();
1020}
1021
1022/// Lower a cir.init_catch_param into the Itanium-specific sequence that
1023/// materializes the catch parameter's local variable from the exception
1024/// pointer returned by __cxa_begin_catch. The shape of the lowering
1025/// depends on the init catch kind:
1026///
1027/// - Reference: the begin_catch result is the adjusted exception object
1028/// pointer, so just bitcast and store it into the alloca.
1029/// - ReferenceToPointer: the companion `cir.construct_catch_param`
1030/// already bound the reference to the exception object, so this lowering
1031/// is a no-op.
1032/// - ReferenceToRecordPointer: the begin_catch result is the adjusted
1033/// pointer value itself, so store it into a temporary and bind the
1034/// reference to the temporary.
1035/// - Pointer: the begin_catch result is
1036/// the pointer value itself, so just bitcast and store it into the
1037/// alloca.
1038/// - Scalar (any other by-value catch): treat the begin_catch result as a
1039/// pointer to the value, load it, and store it into the alloca.
1040/// - Objc: Handle pointer representation with ObjCLifetime.
1041/// - TrivialCopy: copy the exception
1042/// object's bytes into the alloca via cir.copy.
1043/// - NonTrivialCopy: the construction was already performed by the
1044/// companion `cir.construct_catch_param` before `cir.begin_catch`, so
1045/// this lowering is a no-op.
1046///
1047void ItaniumEHLowering::lowerInitCatchParam(cir::InitCatchParamOp op) {
1048 builder.setInsertionPoint(op);
1049 mlir::Location loc = op.getLoc();
1050 mlir::Value exnPtr = op.getExnPtr();
1051 mlir::Value paramAddr = op.getParamAddr();
1052 auto paramAddrType = mlir::cast<cir::PointerType>(Val: paramAddr.getType());
1053 mlir::Type elementType = paramAddrType.getPointee();
1054 cir::InitCatchKind kind = op.getKind();
1055
1056 switch (kind) {
1057 case InitCatchKind::Reference: {
1058 mlir::Value casted = cir::CastOp::create(builder, location: loc, result: elementType,
1059 kind: cir::CastKind::bitcast, src: exnPtr);
1060 cir::StoreOp::create(builder, location: loc, value: casted, addr: paramAddr, is_volatile: {}, is_nontemporal: {}, alignment: {}, sync_scope: {}, mem_order: {});
1061 break;
1062 }
1063 case InitCatchKind::ReferenceToPointer:
1064 // The reference was bound to the exception object by the matching
1065 // cir.construct_catch_param before cir.begin_catch.
1066 break;
1067 case InitCatchKind::ReferenceToRecordPointer: {
1068 // __cxa_begin_catch returns the caught pointer by value, and the
1069 // personality function may have adjusted it. Store the adjusted pointer
1070 // into a temporary and bind the reference to it. Assigning to the catch
1071 // parameter then changes the temporary, not the exception object, which
1072 // departs from the language semantics.
1073 auto caughtType = mlir::cast<cir::PointerType>(
1074 Val: mlir::cast<cir::PointerType>(Val&: elementType).getPointee());
1075 cir::AllocaOp byRefTmp;
1076 {
1077 mlir::OpBuilder::InsertionGuard guard(builder);
1078 builder.setInsertionPointToStart(
1079 &op->getParentOfType<cir::FuncOp>().getBody().front());
1080 cir::CIRDataLayout dataLayout(mod);
1081 uint64_t alignment =
1082 dataLayout.getAlignment(ty: caughtType, /*useABIAlign=*/true).value();
1083 byRefTmp =
1084 cir::AllocaOp::create(builder, location: loc, addr: elementType, name: "exn.byref.tmp",
1085 alignment: builder.getI64IntegerAttr(value: alignment));
1086 }
1087 mlir::Value casted = cir::CastOp::create(builder, location: loc, result: caughtType,
1088 kind: cir::CastKind::bitcast, src: exnPtr);
1089 cir::StoreOp::create(builder, location: loc, value: casted, addr: byRefTmp, is_volatile: {}, is_nontemporal: {}, alignment: {}, sync_scope: {}, mem_order: {});
1090 cir::StoreOp::create(builder, location: loc, value: byRefTmp, addr: paramAddr, is_volatile: {}, is_nontemporal: {}, alignment: {}, sync_scope: {}, mem_order: {});
1091 break;
1092 }
1093 case InitCatchKind::TrivialCopy: {
1094 mlir::Value srcPtr = cir::CastOp::create(builder, location: loc, result: paramAddrType,
1095 kind: cir::CastKind::bitcast, src: exnPtr);
1096 cir::CopyOp::create(builder, location: loc, dst: paramAddr, src: srcPtr, dst_alignment: {}, src_alignment: {});
1097 break;
1098 }
1099 case InitCatchKind::NonTrivialCopy:
1100 // The non-trivial copy was performed by the matching
1101 // cir.construct_catch_param before cir.begin_catch.
1102 break;
1103 case InitCatchKind::Scalar: {
1104 // Scalar by-value catch (integer, float, complex, etc.). The begin_catch
1105 // result points into the exception object; load the value through a
1106 // typed pointer and store it into the alloca.
1107 mlir::Value srcPtr = cir::CastOp::create(builder, location: loc, result: paramAddrType,
1108 kind: cir::CastKind::bitcast, src: exnPtr);
1109 auto loadOp = cir::LoadOp::create(builder, location: loc, result: elementType, addr: srcPtr);
1110 cir::StoreOp::create(builder, location: loc, value: loadOp.getResult(), addr: paramAddr, is_volatile: {}, is_nontemporal: {},
1111 alignment: {}, sync_scope: {}, mem_order: {});
1112 break;
1113 }
1114 case InitCatchKind::Pointer: {
1115 mlir::Value casted = cir::CastOp::create(builder, location: loc, result: elementType,
1116 kind: cir::CastKind::bitcast, src: exnPtr);
1117 cir::StoreOp::create(builder, location: loc, value: casted, addr: paramAddr, is_volatile: {}, is_nontemporal: {}, alignment: {}, sync_scope: {}, mem_order: {});
1118 break;
1119 }
1120 case InitCatchKind::Objc:
1121 llvm_unreachable("InitCatchParam: ObjCLifetime is NYI");
1122 break;
1123 }
1124
1125 op.erase();
1126}
1127
1128//===----------------------------------------------------------------------===//
1129// The Pass
1130//===----------------------------------------------------------------------===//
1131
1132struct CIREHABILoweringPass
1133 : public impl::CIREHABILoweringBase<CIREHABILoweringPass> {
1134 CIREHABILoweringPass() = default;
1135 void runOnOperation() override;
1136};
1137
1138/// Erase all catch-init thunks after the EHABI lowering. CIRGen emits a thunk
1139/// for every `cir.construct_catch_param` op, but those uses should all have
1140/// been replaced during the lowering.
1141static void eraseCatchCopyThunks(mlir::ModuleOp mod) {
1142 llvm::StringRef catchHelperAttr =
1143 cir::CIRDialect::getCatchCopyThunkAttrName();
1144 for (cir::FuncOp f : llvm::make_early_inc_range(Range: mod.getOps<cir::FuncOp>())) {
1145 if (!f->hasAttr(name: catchHelperAttr))
1146 continue;
1147 // This is an expensive check, so we need to rely on the implementation
1148 // to have done the right thing.
1149 assert(mlir::SymbolTable::symbolKnownUseEmpty(f, mod) &&
1150 "catch-init helper has remaining users");
1151 f.erase();
1152 }
1153}
1154
1155void CIREHABILoweringPass::runOnOperation() {
1156 auto mod = mlir::cast<mlir::ModuleOp>(Val: getOperation());
1157
1158 // The target triple is attached to the module as the "cir.triple"
1159 // attribute. If it is absent (e.g. a CIR module parsed from text without a
1160 // triple) we cannot determine the ABI and must skip the pass.
1161 auto tripleAttr = mlir::dyn_cast_if_present<mlir::StringAttr>(
1162 Val: mod->getAttr(name: cir::CIRDialect::getTripleAttrName()));
1163 if (!tripleAttr) {
1164 mod.emitError(message: "Module has no target triple");
1165 return;
1166 }
1167
1168 // Select the ABI-specific lowering handler from the triple. The Microsoft
1169 // C++ ABI targets a Windows MSVC environment; everything else uses Itanium.
1170 // Extend this when Microsoft ABI lowering is added.
1171 llvm::Triple triple(tripleAttr.getValue());
1172 std::unique_ptr<EHABILowering> lowering;
1173 if (triple.isWindowsMSVCEnvironment()) {
1174 mod.emitError(
1175 message: "EH ABI lowering is not yet implemented for the Microsoft ABI");
1176 return signalPassFailure();
1177 } else {
1178 lowering = std::make_unique<ItaniumEHLowering>(args&: mod);
1179 }
1180
1181 if (mlir::failed(Result: lowering->run()))
1182 return signalPassFailure();
1183
1184 // Sweep away any the thunk functions. They've been inlined to all users now.
1185 eraseCatchCopyThunks(mod);
1186}
1187
1188} // namespace
1189
1190std::unique_ptr<Pass> mlir::createCIREHABILoweringPass() {
1191 return std::make_unique<CIREHABILoweringPass>();
1192}
1193