Skip to content

Commit 89c4579

Browse files
committed
[CIR][CIRGen][Exceptions][NFC] Reuse lexical scope instead of custom RAII
1 parent db95ea7 commit 89c4579

File tree

4 files changed

+20
-29
lines changed

4 files changed

+20
-29
lines changed

clang/lib/CIR/CodeGen/CIRGenCall.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,12 @@ buildCallLikeOp(CIRGenFunction &CGF, mlir::Location callLoc,
366366
auto &builder = CGF.getBuilder();
367367

368368
if (InvokeDest) {
369+
auto addr = CGF.currLexScope->getExceptionInfo().addr;
369370
if (indirectFuncTy)
370371
return builder.create<mlir::cir::TryCallOp>(
371-
callLoc, CGF.currExceptionInfo.exceptionAddr, indirectFuncVal,
372-
indirectFuncTy, CIRCallArgs);
373-
return builder.create<mlir::cir::TryCallOp>(
374-
callLoc, directFuncOp, CGF.currExceptionInfo.exceptionAddr,
375-
CIRCallArgs);
372+
callLoc, addr, indirectFuncVal, indirectFuncTy, CIRCallArgs);
373+
return builder.create<mlir::cir::TryCallOp>(callLoc, directFuncOp, addr,
374+
CIRCallArgs);
376375
}
377376

378377
if (indirectFuncTy)

clang/lib/CIR/CodeGen/CIRGenException.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ mlir::Block *CIRGenFunction::getEHResumeBlock(bool isCleanup) {
256256
// Just like some other try/catch related logic: return the basic block
257257
// pointer but only use it to denote we're tracking things, but there
258258
// shouldn't be any changes to that block after work done in this function.
259-
auto catchOp = currExceptionInfo.catchOp;
259+
auto catchOp = currLexScope->getExceptionInfo().catchOp;
260260
assert(catchOp.getNumRegions() && "expected at least one region");
261261
auto &fallbackRegion = catchOp.getRegion(catchOp.getNumRegions() - 1);
262262

@@ -379,7 +379,7 @@ CIRGenFunction::buildCXXTryStmtUnderScope(const CXXTryStmt &S) {
379379
getBuilder().getInsertionBlock()};
380380

381381
{
382-
ExceptionInfoRAIIObject ehx{*this, {exceptionInfoInsideTry, catchOp}};
382+
lexScope.setExceptionInfo({exceptionInfoInsideTry, catchOp});
383383
// Attach the basic blocks for the catchOp regions into ScopeCatch
384384
// info.
385385
enterCXXTryStmt(S, catchOp);
@@ -393,7 +393,7 @@ CIRGenFunction::buildCXXTryStmtUnderScope(const CXXTryStmt &S) {
393393
}
394394

395395
{
396-
ExceptionInfoRAIIObject ehx{*this, {tryScope->getResult(0), catchOp}};
396+
lexScope.setExceptionInfo({tryScope->getResult(0), catchOp});
397397
// Emit catch clauses.
398398
exitCXXTryStmt(S);
399399
}
@@ -633,7 +633,7 @@ mlir::Operation *CIRGenFunction::buildLandingPad() {
633633
// If there's an existing CatchOp, it means we got a `cir.try` scope
634634
// that leads to this "landing pad" creation site. Otherwise, exceptions
635635
// are enabled but a throwing function is called anyways.
636-
auto catchOp = currExceptionInfo.catchOp;
636+
auto catchOp = currLexScope->getExceptionInfo().catchOp;
637637
if (!catchOp) {
638638
llvm_unreachable("NYI");
639639
}

clang/lib/CIR/CodeGen/CIRGenFunction.h

+10-18
Original file line numberDiff line numberDiff line change
@@ -312,26 +312,9 @@ class CIRGenFunction : public CIRGenTypeCache {
312312
/// Try/Catch: calls within try statements need to refer to local
313313
/// allocas for the exception info
314314
struct CIRExceptionInfo {
315-
mlir::Value exceptionAddr{};
315+
mlir::Value addr{};
316316
mlir::cir::CatchOp catchOp{};
317317
};
318-
CIRExceptionInfo currExceptionInfo{};
319-
class ExceptionInfoRAIIObject {
320-
CIRGenFunction &P;
321-
CIRExceptionInfo OldVal{};
322-
323-
public:
324-
ExceptionInfoRAIIObject(CIRGenFunction &p, CIRExceptionInfo info) : P(p) {
325-
if (P.currExceptionInfo.exceptionAddr)
326-
OldVal = P.currExceptionInfo;
327-
P.currExceptionInfo = info;
328-
}
329-
330-
/// Can be used to restore the state early, before the dtor
331-
/// is run.
332-
void restore() { P.currExceptionInfo = OldVal; }
333-
~ExceptionInfoRAIIObject() { restore(); }
334-
};
335318

336319
enum class EvaluationOrder {
337320
///! No langauge constraints on evaluation order.
@@ -1773,6 +1756,9 @@ class CIRGenFunction : public CIRGenTypeCache {
17731756

17741757
LexicalScope *ParentScope = nullptr;
17751758

1759+
// If there's exception information for this scope, store it.
1760+
CIRExceptionInfo exInfo{};
1761+
17761762
// FIXME: perhaps we can use some info encoded in operations.
17771763
enum Kind {
17781764
Regular, // cir.if, cir.scope, if_regions
@@ -1873,6 +1859,12 @@ class CIRGenFunction : public CIRGenTypeCache {
18731859
// Labels solved inside this scope.
18741860
llvm::SmallPtrSet<const clang::LabelDecl *, 4> SolvedLabels;
18751861

1862+
// ---
1863+
// Exception handling
1864+
// ---
1865+
CIRExceptionInfo &getExceptionInfo() { return exInfo; }
1866+
void setExceptionInfo(const CIRExceptionInfo &info) { exInfo = info; }
1867+
18761868
// ---
18771869
// Return handling
18781870
// ---

clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ static mlir::Value CallBeginCatch(CIRGenFunction &CGF, mlir::Value Exn,
667667
static void InitCatchParam(CIRGenFunction &CGF, const VarDecl &CatchParam,
668668
Address ParamAddr, SourceLocation Loc) {
669669
// Load the exception from where the landing pad saved it.
670-
auto Exn = CGF.currExceptionInfo.exceptionAddr;
670+
auto Exn = CGF.currLexScope->getExceptionInfo().addr;
671671

672672
CanQualType CatchType =
673673
CGF.CGM.getASTContext().getCanonicalType(CatchParam.getType());
@@ -771,7 +771,7 @@ void CIRGenItaniumCXXABI::emitBeginCatch(CIRGenFunction &CGF,
771771

772772
VarDecl *CatchParam = S->getExceptionDecl();
773773
if (!CatchParam) {
774-
auto Exn = CGF.currExceptionInfo.exceptionAddr;
774+
auto Exn = CGF.currLexScope->getExceptionInfo().addr;
775775
CallBeginCatch(CGF, Exn, CGF.getBuilder().getVoidPtrTy(), true);
776776
return;
777777
}

0 commit comments

Comments
 (0)