30#include "mlir/IR/Builders.h"
31#include "mlir/IR/IRMapping.h"
32#include "mlir/IR/PatternMatch.h"
39#include "llvm/ADT/DenseMap.h"
40#include "llvm/ADT/SmallVector.h"
41#include "llvm/TargetParser/Triple.h"
47#define GEN_PASS_DEF_CIREHABILOWERING
48#include "clang/CIR/Dialect/Passes.h.inc"
59static cir::FuncOp getOrCreateRuntimeFuncDecl(mlir::ModuleOp mod,
62 cir::FuncType funcTy) {
63 if (
auto existing = mod.lookupSymbol<cir::FuncOp>(
name))
66 mlir::OpBuilder builder(mod.getContext());
67 builder.setInsertionPointToEnd(mod.getBody());
68 auto funcOp = cir::FuncOp::create(builder, loc,
name, funcTy);
69 funcOp.setLinkage(cir::GlobalLinkageKind::ExternalLinkage);
82 explicit EHABILowering(mlir::ModuleOp mod)
83 : mod(mod), ctx(mod.getContext()), builder(ctx) {}
84 virtual ~EHABILowering() =
default;
87 virtual mlir::LogicalResult
run() = 0;
91 mlir::MLIRContext *ctx;
92 mlir::OpBuilder builder;
106class ItaniumEHLowering :
public EHABILowering {
108 using EHABILowering::EHABILowering;
109 mlir::LogicalResult
run()
override;
114 using EhTokenMap = DenseMap<mlir::Value, std::pair<mlir::Value, mlir::Value>>;
116 cir::VoidType voidType;
117 cir::PointerType voidPtrType;
118 cir::PointerType u8PtrType;
119 cir::IntType u32Type;
123 cir::FuncOp personalityFunc;
124 cir::FuncOp beginCatchFunc;
125 cir::FuncOp endCatchFunc;
126 cir::FuncOp getExceptionPtrFunc;
127 cir::FuncOp clangCallTerminateFunc;
128 cir::FuncOp cxaThrowFunc;
129 cir::FuncOp cxaRethrowFunc;
131 DenseMap<mlir::StringAttr, cir::FuncOp> catchCopyThunks;
133 constexpr const static ::llvm::StringLiteral kGxxPersonality =
134 "__gxx_personality_v0";
136 void ensureRuntimeDecls(mlir::Location loc);
137 void ensureClangCallTerminate(mlir::Location loc);
138 void ensureCxaThrowDecl(mlir::Location loc);
139 void ensureCxaRethrowDecl(mlir::Location loc);
140 mlir::Block *buildTerminateBlock(cir::FuncOp funcOp, mlir::Location loc);
141 mlir::FailureOr<cir::FuncOp>
142 resolveCatchCopyThunk(cir::ConstructCatchParamOp op);
143 mlir::LogicalResult lowerFunc(cir::FuncOp funcOp);
145 lowerEhInitiate(cir::EhInitiateOp initiateOp, EhTokenMap &ehTokenMap,
146 SmallVectorImpl<mlir::Operation *> &deadOps);
147 void lowerDispatch(cir::EhDispatchOp dispatch, mlir::Value exnPtr,
149 SmallVectorImpl<mlir::Operation *> &deadOps);
150 mlir::LogicalResult lowerConstructCatchParam(cir::ConstructCatchParamOp op,
152 void lowerInitCatchParam(cir::InitCatchParamOp op);
153 mlir::LogicalResult lowerTryThrow(cir::TryThrowOp op);
157mlir::LogicalResult ItaniumEHLowering::run() {
160 voidType = cir::VoidType::get(ctx);
161 voidPtrType = cir::PointerType::get(voidType);
162 auto u8Type = cir::IntType::get(ctx, 8,
false);
163 u8PtrType = cir::PointerType::get(u8Type);
164 u32Type = cir::IntType::get(ctx, 32,
false);
166 for (cir::FuncOp funcOp : mod.getOps<cir::FuncOp>()) {
167 if (mlir::failed(lowerFunc(funcOp)))
168 return mlir::failure();
170 return mlir::success();
175void ItaniumEHLowering::ensureRuntimeDecls(mlir::Location loc) {
178 if (!personalityFunc) {
179 auto s32Type = cir::IntType::get(ctx, 32,
true);
180 auto personalityFuncTy = cir::FuncType::get({}, s32Type,
true);
181 personalityFunc = getOrCreateRuntimeFuncDecl(mod, loc, kGxxPersonality,
185 if (!beginCatchFunc) {
186 auto beginCatchFuncTy =
187 cir::FuncType::get({voidPtrType}, u8PtrType,
false);
188 beginCatchFunc = getOrCreateRuntimeFuncDecl(mod, loc,
"__cxa_begin_catch",
193 auto endCatchFuncTy = cir::FuncType::get({}, voidType,
false);
195 getOrCreateRuntimeFuncDecl(mod, loc,
"__cxa_end_catch", endCatchFuncTy);
198 if (!getExceptionPtrFunc) {
199 auto getExceptionPtrFuncTy =
200 cir::FuncType::get({voidPtrType}, u8PtrType,
false);
201 getExceptionPtrFunc = getOrCreateRuntimeFuncDecl(
202 mod, loc,
"__cxa_get_exception_ptr", getExceptionPtrFuncTy);
215void ItaniumEHLowering::ensureClangCallTerminate(mlir::Location loc) {
216 if (clangCallTerminateFunc)
219 ensureRuntimeDecls(loc);
221 if (
auto existing = mod.lookupSymbol<cir::FuncOp>(
"__clang_call_terminate")) {
222 clangCallTerminateFunc = existing;
226 auto funcTy = cir::FuncType::get({voidPtrType}, voidType,
false);
227 builder.setInsertionPointToEnd(mod.getBody());
229 cir::FuncOp::create(builder, loc,
"__clang_call_terminate", funcTy);
230 funcOp.setLinkage(cir::GlobalLinkageKind::LinkOnceODRLinkage);
231 funcOp.setGlobalVisibility(cir::VisibilityKind::Hidden);
233 mlir::Block *entryBlock = funcOp.addEntryBlock();
234 builder.setInsertionPointToStart(entryBlock);
235 mlir::Value exnArg = entryBlock->getArgument(0);
237 auto catchCall = cir::CallOp::create(
238 builder, loc, mlir::FlatSymbolRefAttr::get(beginCatchFunc), u8PtrType,
239 mlir::ValueRange{exnArg});
240 catchCall.setNothrowAttr(builder.getUnitAttr());
242 auto terminateFuncDecl = getOrCreateRuntimeFuncDecl(
243 mod, loc,
"_ZSt9terminatev",
244 cir::FuncType::get({}, voidType,
false));
245 terminateFuncDecl->setAttr(cir::CIRDialect::getNoReturnAttrName(),
246 builder.getUnitAttr());
247 auto terminateCall = cir::CallOp::create(
248 builder, loc, mlir::FlatSymbolRefAttr::get(terminateFuncDecl), voidType,
250 terminateCall.setNothrowAttr(builder.getUnitAttr());
251 terminateCall->setAttr(cir::CIRDialect::getNoReturnAttrName(),
252 builder.getUnitAttr());
254 cir::UnreachableOp::create(builder, loc);
256 funcOp->setAttr(cir::CIRDialect::getNoReturnAttrName(),
257 builder.getUnitAttr());
258 clangCallTerminateFunc = funcOp;
264void ItaniumEHLowering::ensureCxaThrowDecl(mlir::Location loc) {
267 auto throwFuncTy = cir::FuncType::get({voidPtrType, voidPtrType, voidPtrType},
270 getOrCreateRuntimeFuncDecl(mod, loc,
"__cxa_throw", throwFuncTy);
276void ItaniumEHLowering::ensureCxaRethrowDecl(mlir::Location loc) {
279 auto rethrowFuncTy = cir::FuncType::get({}, voidType,
false);
281 getOrCreateRuntimeFuncDecl(mod, loc,
"__cxa_rethrow", rethrowFuncTy);
285mlir::Block *ItaniumEHLowering::buildTerminateBlock(cir::FuncOp funcOp,
286 mlir::Location loc) {
287 assert(clangCallTerminateFunc &&
288 "ensureClangCallTerminate must run before buildTerminateBlock");
289 mlir::Region &body = funcOp.getRegion();
290 mlir::Block *terminateBlock = builder.createBlock(&body, body.end());
291 auto inflight = cir::EhInflightOp::create(
292 builder, loc,
false,
true,
294 auto terminateCall = cir::CallOp::create(
295 builder, loc, mlir::FlatSymbolRefAttr::get(clangCallTerminateFunc),
296 voidType, mlir::ValueRange{inflight.getExceptionPtr()});
297 terminateCall.setNothrowAttr(builder.getUnitAttr());
298 terminateCall->setAttr(cir::CIRDialect::getNoReturnAttrName(),
299 builder.getUnitAttr());
300 cir::UnreachableOp::create(builder, loc);
301 return terminateBlock;
305mlir::LogicalResult ItaniumEHLowering::lowerFunc(cir::FuncOp funcOp) {
306 if (funcOp.isDeclaration())
307 return mlir::success();
313 SmallVector<cir::EhInitiateOp> initiateOps;
314 funcOp.walk([&](cir::EhInitiateOp op) { initiateOps.push_back(op); });
315 if (initiateOps.empty())
316 return mlir::success();
318 ensureRuntimeDecls(funcOp.getLoc());
325 if (!funcOp.getPersonality())
326 funcOp.setPersonality(kGxxPersonality);
333 EhTokenMap ehTokenMap;
334 SmallVector<mlir::Operation *> deadOps;
335 for (cir::EhInitiateOp initiateOp : initiateOps)
336 if (mlir::failed(lowerEhInitiate(initiateOp, ehTokenMap, deadOps)))
337 return mlir::failure();
341 for (mlir::Operation *op : deadOps)
346 for (mlir::Block &block : funcOp.getBody()) {
347 for (
int i = block.getNumArguments() - 1; i >= 0; --i) {
348 if (mlir::isa<cir::EhTokenType>(block.getArgument(i).getType()))
349 block.eraseArgument(i);
356 SmallVector<cir::InitCatchParamOp> initCatchOps;
357 funcOp.walk([&](cir::InitCatchParamOp op) { initCatchOps.push_back(op); });
358 for (cir::InitCatchParamOp op : initCatchOps)
359 lowerInitCatchParam(op);
364 SmallVector<cir::TryThrowOp> tryThrowOps;
365 funcOp.walk([&](cir::TryThrowOp op) { tryThrowOps.push_back(op); });
366 for (cir::TryThrowOp op : tryThrowOps)
367 if (mlir::failed(lowerTryThrow(op)))
368 return mlir::failure();
370 return mlir::success();
399mlir::LogicalResult ItaniumEHLowering::lowerEhInitiate(
400 cir::EhInitiateOp initiateOp, EhTokenMap &ehTokenMap,
401 SmallVectorImpl<mlir::Operation *> &deadOps) {
402 mlir::Value rootToken = initiateOp.getEhToken();
406 builder.setInsertionPoint(initiateOp);
407 auto inflightOp = cir::EhInflightOp::create(
408 builder, initiateOp.getLoc(), initiateOp.getCleanup(),
412 ehTokenMap[rootToken] = {inflightOp.getExceptionPtr(),
413 inflightOp.getTypeId()};
419 SmallVector<mlir::Value> worklist;
420 SmallPtrSet<mlir::Value, 8> visited;
421 worklist.push_back(rootToken);
423 while (!worklist.empty()) {
424 mlir::Value current = worklist.pop_back_val();
425 if (!visited.insert(current).second)
430 SmallVector<mlir::Operation *> users;
431 for (mlir::OpOperand &use : current.getUses())
432 users.push_back(use.getOwner());
436 for (mlir::Operation *user : users) {
442 for (
unsigned s = 0;
s < user->getNumSuccessors(); ++
s) {
443 mlir::Block *succ = user->getSuccessor(
s);
444 for (mlir::BlockArgument arg : succ->getArguments()) {
445 if (!mlir::isa<cir::EhTokenType>(
arg.getType()))
447 if (!ehTokenMap.count(arg)) {
448 mlir::Value ptrArg = succ->addArgument(voidPtrType,
arg.getLoc());
449 mlir::Value u32Arg = succ->addArgument(u32Type,
arg.getLoc());
450 ehTokenMap[
arg] = {ptrArg, u32Arg};
452 worklist.push_back(arg);
456 if (
auto op = mlir::dyn_cast<cir::BeginCleanupOp>(user)) {
459 for (
auto &tokenUsers :
460 llvm::make_early_inc_range(op.getCleanupToken().getUses())) {
462 mlir::dyn_cast<cir::EndCleanupOp>(tokenUsers.getOwner()))
466 }
else if (
auto op = mlir::dyn_cast<cir::BeginCatchOp>(user)) {
469 for (
auto &tokenUsers :
470 llvm::make_early_inc_range(op.getCatchToken().getUses())) {
472 mlir::dyn_cast<cir::EndCatchOp>(tokenUsers.getOwner())) {
473 builder.setInsertionPoint(endOp);
474 cir::CallOp::create(builder, endOp.getLoc(),
475 mlir::FlatSymbolRefAttr::get(endCatchFunc),
476 voidType, mlir::ValueRange{});
481 auto [exnPtr, typeId] = ehTokenMap.lookup(op.getEhToken());
482 builder.setInsertionPoint(op);
483 auto callOp = cir::CallOp::create(
484 builder, op.getLoc(), mlir::FlatSymbolRefAttr::get(beginCatchFunc),
485 u8PtrType, mlir::ValueRange{exnPtr});
486 mlir::Value castResult = callOp.getResult();
487 mlir::Type expectedPtrType = op.getExnPtr().getType();
488 if (castResult.getType() != expectedPtrType)
490 cir::CastOp::create(builder, op.getLoc(), expectedPtrType,
491 cir::CastKind::bitcast, callOp.getResult());
492 op.getExnPtr().replaceAllUsesWith(castResult);
494 }
else if (
auto op = mlir::dyn_cast<cir::ConstructCatchParamOp>(user)) {
495 auto [exnPtr, typeId] = ehTokenMap.lookup(op.getEhToken());
496 if (mlir::failed(lowerConstructCatchParam(op, exnPtr)))
497 return mlir::failure();
498 }
else if (
auto op = mlir::dyn_cast<cir::EhDispatchOp>(user)) {
500 mlir::ArrayAttr catchTypes = op.getCatchTypesAttr();
501 if (catchTypes && catchTypes.size() > 0) {
502 SmallVector<mlir::Attribute> typeSymbols;
503 for (mlir::Attribute attr : catchTypes)
504 typeSymbols.push_back(
505 mlir::cast<cir::GlobalViewAttr>(attr).getSymbol());
506 inflightOp.setCatchTypeListAttr(builder.getArrayAttr(typeSymbols));
508 if (op.getDefaultIsCatchAll())
509 inflightOp.setCatchAllAttr(builder.getUnitAttr());
513 if (!llvm::is_contained(deadOps, op.getOperation())) {
514 auto [exnPtr, typeId] = ehTokenMap.lookup(op.getEhToken());
515 lowerDispatch(op, exnPtr, typeId, deadOps);
517 }
else if (
auto op = mlir::dyn_cast<cir::EhTerminateOp>(user)) {
518 auto [exnPtr, typeId] = ehTokenMap.lookup(op.getEhToken());
519 ensureClangCallTerminate(op.getLoc());
520 builder.setInsertionPoint(op);
521 auto call = cir::CallOp::create(
522 builder, op.getLoc(),
523 mlir::FlatSymbolRefAttr::get(clangCallTerminateFunc), voidType,
524 mlir::ValueRange{exnPtr});
525 call.setNothrowAttr(builder.getUnitAttr());
526 call->setAttr(cir::CIRDialect::getNoReturnAttrName(),
527 builder.getUnitAttr());
528 cir::UnreachableOp::create(builder, op.getLoc());
530 }
else if (
auto op = mlir::dyn_cast<cir::ResumeOp>(user)) {
531 auto [exnPtr, typeId] = ehTokenMap.lookup(op.getEhToken());
532 builder.setInsertionPoint(op);
533 cir::ResumeFlatOp::create(builder, op.getLoc(), exnPtr, typeId);
535 }
else if (
auto op = mlir::dyn_cast<cir::BrOp>(user)) {
537 SmallVector<mlir::Value> newOperands;
538 bool changed =
false;
539 for (mlir::Value operand : op.getDestOperands()) {
540 auto it = ehTokenMap.find(operand);
541 if (it != ehTokenMap.end()) {
542 newOperands.push_back(it->second.first);
543 newOperands.push_back(it->second.second);
546 newOperands.push_back(operand);
550 builder.setInsertionPoint(op);
551 cir::BrOp::create(builder, op.getLoc(), op.getDest(), newOperands);
559 return mlir::success();
565void ItaniumEHLowering::lowerDispatch(
566 cir::EhDispatchOp dispatch, mlir::Value exnPtr, mlir::Value typeId,
567 SmallVectorImpl<mlir::Operation *> &deadOps) {
568 mlir::Location dispLoc = dispatch.getLoc();
569 mlir::Block *defaultDest = dispatch.getDefaultDestination();
570 mlir::ArrayAttr catchTypes = dispatch.getCatchTypesAttr();
571 mlir::SuccessorRange catchDests = dispatch.getCatchDestinations();
572 mlir::Block *dispatchBlock = dispatch->getBlock();
577 if (!catchTypes || catchTypes.empty()) {
579 builder.setInsertionPoint(dispatch);
580 cir::BrOp::create(builder, dispLoc, defaultDest,
581 mlir::ValueRange{exnPtr, typeId});
583 unsigned numCatches = catchTypes.size();
589 mlir::Block *
insertBefore = dispatchBlock->getNextNode();
590 mlir::Block *falseDest = defaultDest;
591 mlir::Block *firstCmpBlock =
nullptr;
592 for (
int i = numCatches - 1; i >= 0; --i) {
593 auto *cmpBlock = builder.createBlock(insertBefore, {voidPtrType, u32Type},
596 mlir::Value cmpExnPtr = cmpBlock->getArgument(0);
597 mlir::Value cmpTypeId = cmpBlock->getArgument(1);
599 auto globalView = mlir::cast<cir::GlobalViewAttr>(catchTypes[i]);
601 cir::EhTypeIdOp::create(builder, dispLoc, globalView.getSymbol());
602 auto cmpOp = cir::CmpOp::create(builder, dispLoc, cir::CmpOpKind::eq,
603 cmpTypeId, ehTypeIdOp.getTypeId());
605 cir::BrCondOp::create(builder, dispLoc, cmpOp, catchDests[i], falseDest,
606 mlir::ValueRange{cmpExnPtr, cmpTypeId},
607 mlir::ValueRange{cmpExnPtr, cmpTypeId});
610 falseDest = cmpBlock;
611 firstCmpBlock = cmpBlock;
615 builder.setInsertionPoint(dispatch);
616 cir::BrOp::create(builder, dispLoc, firstCmpBlock,
617 mlir::ValueRange{exnPtr, typeId});
623 deadOps.push_back(dispatch);
626mlir::FailureOr<cir::FuncOp>
627ItaniumEHLowering::resolveCatchCopyThunk(cir::ConstructCatchParamOp op) {
628 mlir::FlatSymbolRefAttr thunkRef = op.getCopyFnAttr();
629 mlir::StringAttr thunkName = thunkRef.getAttr();
630 auto cached = catchCopyThunks.find(thunkName);
631 if (cached != catchCopyThunks.end())
632 return cached->second;
634 cir::FuncOp thunk = mod.lookupSymbol<cir::FuncOp>(thunkRef);
636 return op.emitError(
"could not resolve catch-copy thunk symbol");
637 assert(thunk->hasAttr(cir::CIRDialect::getCatchCopyThunkAttrName()) &&
638 "verifier should have rejected non-thunk catch-copy reference");
639 if (thunk.isDeclaration())
640 return op.emitError(
"catch-copy thunk has no body to inline");
642 mlir::Region &thunkRegion = thunk.getRegion();
643 if (!llvm::hasSingleElement(thunkRegion))
644 return op.emitError(
"multi-block catch-copy thunks are NYI");
646 mlir::Block &thunkEntry = thunkRegion.front();
647 assert(thunkEntry.getNumArguments() == 2 &&
648 "catch-copy thunk must have exactly two parameters");
649 if (!mlir::isa<cir::ReturnOp>(thunkEntry.getTerminator()))
650 return op.emitError(
"catch-copy thunk must end in cir.return");
652 catchCopyThunks[thunkName] = thunk;
660ItaniumEHLowering::lowerConstructCatchParam(cir::ConstructCatchParamOp op,
661 mlir::Value exnPtr) {
662 mlir::Location loc = op.getLoc();
663 mlir::Value paramAddr = op.getParamAddr();
664 cir::PointerType paramAddrType =
665 mlir::cast<cir::PointerType>(paramAddr.getType());
667 if (op.getKind() == cir::InitCatchKind::Reference) {
669 constexpr unsigned headerSize = 32;
671 builder.setInsertionPoint(op);
672 auto index = cir::ConstantOp::create(
673 builder, loc, cir::IntAttr::get(u32Type, headerSize));
674 assert((exnPtr.getType() == voidPtrType || exnPtr.getType() == u8PtrType) &&
675 "lowerConstructCatchParam exn ptr not void* or i8*");
677 cir::PtrStrideOp::create(builder, loc, exnPtr.getType(), exnPtr, index);
679 cir::CastOp::create(builder, loc, paramAddrType.getPointee(),
680 cir::CastKind::bitcast, exnObj);
681 cir::StoreOp::create(builder, loc, casted, paramAddr, {}, {}, {}, {});
686 if (op.getKind() != cir::InitCatchKind::NonTrivialCopy)
688 "ConstructCatchParam: only non_trivial_copy is supported");
690 ensureRuntimeDecls(loc);
691 ensureClangCallTerminate(loc);
694 builder.setInsertionPoint(op);
695 cir::CallOp getExnCall = cir::CallOp::create(
696 builder, loc, mlir::FlatSymbolRefAttr::get(getExceptionPtrFunc),
697 u8PtrType, mlir::ValueRange{exnPtr});
698 getExnCall.setNothrowAttr(builder.getUnitAttr());
699 mlir::Value adjusted =
700 cir::CastOp::create(builder, loc, paramAddrType, cir::CastKind::bitcast,
701 getExnCall.getResult());
704 mlir::FailureOr<cir::FuncOp> thunkOr = resolveCatchCopyThunk(op);
705 if (mlir::failed(thunkOr))
706 return mlir::failure();
707 cir::FuncOp thunk = *thunkOr;
711 assert(llvm::hasSingleElement(thunk.getRegion()) &&
712 "multi-block catch-copy thunks are NYI");
715 mlir::Block &thunkEntry = thunk.getRegion().front();
716 mlir::IRMapping mapping;
717 mapping.map(thunkEntry.getArgument(0), paramAddr);
718 mapping.map(thunkEntry.getArgument(1), adjusted);
719 llvm::SmallVector<cir::CallOp> throwingCalls;
720 for (mlir::Operation &thunkOp : thunkEntry.without_terminator()) {
721 mlir::Operation *cloned = builder.clone(thunkOp, mapping);
722 if (cir::CallOp callOp = mlir::dyn_cast<cir::CallOp>(cloned))
723 if (!callOp.getNothrow())
724 throwingCalls.push_back(callOp);
728 if (throwingCalls.empty())
729 return mlir::success();
733 mlir::IRRewriter rewriter(builder);
734 mlir::Block *terminateBlock =
nullptr;
735 for (cir::CallOp call : throwingCalls) {
737 terminateBlock = buildTerminateBlock(call->getParentOfType<cir::FuncOp>(),
741 return mlir::success();
748mlir::LogicalResult ItaniumEHLowering::lowerTryThrow(cir::TryThrowOp op) {
749 mlir::Location loc = op.getLoc();
750 mlir::Block *normalDest = op.getNormalDest();
751 mlir::Block *unwindDest = op.getUnwindDest();
752 builder.setInsertionPoint(op);
755 ensureCxaRethrowDecl(loc);
756 cir::TryCallOp::create(
757 builder, loc, mlir::FlatSymbolRefAttr::get(cxaRethrowFunc), voidType,
758 normalDest, unwindDest, mlir::ValueRange{});
760 return mlir::success();
763 ensureCxaThrowDecl(loc);
766 mlir::Value exnPtr = op.getExceptionPtr();
767 if (exnPtr.getType() != voidPtrType)
768 exnPtr = cir::CastOp::create(builder, loc, voidPtrType,
769 cir::CastKind::bitcast, exnPtr);
774 mlir::FlatSymbolRefAttr typeInfoAttr = op.getTypeInfoAttr();
775 auto typeInfoGlobal = mod.lookupSymbol<cir::GlobalOp>(typeInfoAttr);
777 return op.emitError(
"type_info symbol not found in module");
778 auto typeInfoPtrTy = cir::PointerType::get(typeInfoGlobal.getSymType());
779 mlir::Value typeInfo = cir::GetGlobalOp::create(builder, loc, typeInfoPtrTy,
780 typeInfoAttr.getValue());
781 if (typeInfo.getType() != voidPtrType)
782 typeInfo = cir::CastOp::create(builder, loc, voidPtrType,
783 cir::CastKind::bitcast, typeInfo);
787 if (mlir::FlatSymbolRefAttr dtorAttr = op.getDtorAttr()) {
788 auto dtorFunc = mod.lookupSymbol<cir::FuncOp>(dtorAttr);
790 return op.emitError(
"dtor symbol not found in module");
791 auto dtorPtrTy = cir::PointerType::get(dtorFunc.getFunctionType());
793 cir::GetGlobalOp::create(builder, loc, dtorPtrTy, dtorAttr.getValue());
794 if (dtor.getType() != voidPtrType)
795 dtor = cir::CastOp::create(builder, loc, voidPtrType,
796 cir::CastKind::bitcast, dtor);
798 dtor = cir::ConstantOp::create(
800 cir::ConstPtrAttr::get(voidPtrType, builder.getI64IntegerAttr(0)));
803 cir::TryCallOp::create(
804 builder, loc, mlir::FlatSymbolRefAttr::get(cxaThrowFunc), voidType,
805 normalDest, unwindDest, mlir::ValueRange{exnPtr, typeInfo, dtor});
807 return mlir::success();
830void ItaniumEHLowering::lowerInitCatchParam(cir::InitCatchParamOp op) {
831 builder.setInsertionPoint(op);
832 mlir::Location loc = op.getLoc();
833 mlir::Value exnPtr = op.getExnPtr();
834 mlir::Value paramAddr = op.getParamAddr();
835 auto paramAddrType = mlir::cast<cir::PointerType>(paramAddr.getType());
836 mlir::Type elementType = paramAddrType.getPointee();
837 cir::InitCatchKind
kind = op.getKind();
840 case InitCatchKind::Reference: {
844 if (
const auto ref = mlir::dyn_cast<cir::PointerType>(elementType)) {
847 if (
auto ptr = mlir::dyn_cast<cir::PointerType>(ref.getPointee()))
848 if (!mlir::isa<cir::RecordType>(ptr.getPointee()))
854 mlir::Value casted = cir::CastOp::create(builder, loc, elementType,
855 cir::CastKind::bitcast, exnPtr);
856 cir::StoreOp::create(builder, loc, casted, paramAddr, {}, {}, {}, {});
859 case InitCatchKind::TrivialCopy: {
860 mlir::Value srcPtr = cir::CastOp::create(builder, loc, paramAddrType,
861 cir::CastKind::bitcast, exnPtr);
862 cir::CopyOp::create(builder, loc, paramAddr, srcPtr, {}, {});
865 case InitCatchKind::NonTrivialCopy:
869 case InitCatchKind::Scalar: {
873 mlir::Value srcPtr = cir::CastOp::create(builder, loc, paramAddrType,
874 cir::CastKind::bitcast, exnPtr);
875 auto loadOp = cir::LoadOp::create(builder, loc, elementType, srcPtr);
876 cir::StoreOp::create(builder, loc, loadOp.getResult(), paramAddr, {}, {},
880 case InitCatchKind::Pointer: {
881 mlir::Value casted = cir::CastOp::create(builder, loc, elementType,
882 cir::CastKind::bitcast, exnPtr);
883 cir::StoreOp::create(builder, loc, casted, paramAddr, {}, {}, {}, {});
886 case InitCatchKind::Objc:
887 llvm_unreachable(
"InitCatchParam: ObjCLifetime is NYI");
898struct CIREHABILoweringPass
899 :
public impl::CIREHABILoweringBase<CIREHABILoweringPass> {
900 CIREHABILoweringPass() =
default;
901 void runOnOperation()
override;
907static void eraseCatchCopyThunks(mlir::ModuleOp mod) {
908 llvm::StringRef catchHelperAttr =
909 cir::CIRDialect::getCatchCopyThunkAttrName();
910 for (cir::FuncOp f : llvm::make_early_inc_range(mod.getOps<cir::FuncOp>())) {
911 if (!f->hasAttr(catchHelperAttr))
915 assert(mlir::SymbolTable::symbolKnownUseEmpty(f, mod) &&
916 "catch-init helper has remaining users");
921void CIREHABILoweringPass::runOnOperation() {
922 auto mod = mlir::cast<mlir::ModuleOp>(getOperation());
927 auto tripleAttr = mlir::dyn_cast_if_present<mlir::StringAttr>(
928 mod->getAttr(cir::CIRDialect::getTripleAttrName()));
930 mod.emitError(
"Module has no target triple");
937 llvm::Triple triple(tripleAttr.getValue());
938 std::unique_ptr<EHABILowering> lowering;
939 if (triple.isWindowsMSVCEnvironment()) {
941 "EH ABI lowering is not yet implemented for the Microsoft ABI");
942 return signalPassFailure();
944 lowering = std::make_unique<ItaniumEHLowering>(mod);
947 if (mlir::failed(lowering->run()))
948 return signalPassFailure();
951 eraseCatchCopyThunks(mod);
957 return std::make_unique<CIREHABILoweringPass>();
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
__device__ __2f16 float __ockl_bool s
mlir::Block * replaceCallWithTryCall(cir::CallOp callOp, mlir::Block *unwindDest, mlir::Location loc, mlir::RewriterBase &rewriter)
Replace a cir::CallOp with a cir::TryCallOp whose unwind destination is unwindDest.
std::unique_ptr< Pass > createCIREHABILoweringPass()
__DEVICE__ _Tp arg(const std::complex< _Tp > &__c)
static bool sizeOfUnwindException()