1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
//===--- CIRGenException.cpp - Emit CIR Code for C++ exceptions -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This contains code dealing with C++ exception related code generation.
//
//===----------------------------------------------------------------------===//
#include "CIRGenCXXABI.h"
#include "CIRGenFunction.h"
#include "clang/AST/StmtVisitor.h"
using namespace clang;
using namespace clang::CIRGen;
void CIRGenFunction::emitCXXThrowExpr(const CXXThrowExpr *e) {
const llvm::Triple &triple = getTarget().getTriple();
if (cgm.getLangOpts().OpenMPIsTargetDevice &&
(triple.isNVPTX() || triple.isAMDGCN())) {
cgm.errorNYI("emitCXXThrowExpr OpenMP with NVPTX or AMDGCN Triples");
return;
}
if (const Expr *subExpr = e->getSubExpr()) {
QualType throwType = subExpr->getType();
if (throwType->isObjCObjectPointerType()) {
cgm.errorNYI("emitCXXThrowExpr ObjCObjectPointerType");
return;
}
cgm.getCXXABI().emitThrow(*this, e);
return;
}
cgm.getCXXABI().emitRethrow(*this, /*isNoReturn=*/true);
}
void CIRGenFunction::emitAnyExprToExn(const Expr *e, Address addr) {
// Make sure the exception object is cleaned up if there's an
// exception during initialization.
assert(!cir::MissingFeatures::ehCleanupScope());
// __cxa_allocate_exception returns a void*; we need to cast this
// to the appropriate type for the object.
mlir::Type ty = convertTypeForMem(e->getType());
Address typedAddr = addr.withElementType(builder, ty);
// From LLVM's codegen:
// FIXME: this isn't quite right! If there's a final unelided call
// to a copy constructor, then according to [except.terminate]p1 we
// must call std::terminate() if that constructor throws, because
// technically that copy occurs after the exception expression is
// evaluated but before the exception is caught. But the best way
// to handle that is to teach EmitAggExpr to do the final copy
// differently if it can't be elided.
emitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(),
/*isInitializer=*/true);
// Deactivate the cleanup block.
assert(!cir::MissingFeatures::ehCleanupScope());
}
mlir::LogicalResult CIRGenFunction::emitCXXTryStmt(const CXXTryStmt &s) {
if (s.getTryBlock()->body_empty())
return mlir::LogicalResult::success();
mlir::Location loc = getLoc(s.getSourceRange());
// Create a scope to hold try local storage for catch params.
mlir::OpBuilder::InsertPoint scopeIP;
cir::ScopeOp::create(
builder, loc,
/*scopeBuilder=*/[&](mlir::OpBuilder &b, mlir::Location loc) {
scopeIP = builder.saveInsertionPoint();
});
mlir::OpBuilder::InsertionGuard guard(builder);
builder.restoreInsertionPoint(scopeIP);
mlir::LogicalResult result = emitCXXTryStmtUnderScope(s);
cir::YieldOp::create(builder, loc);
return result;
}
mlir::LogicalResult
CIRGenFunction::emitCXXTryStmtUnderScope(const CXXTryStmt &s) {
const llvm::Triple &t = getTarget().getTriple();
// If we encounter a try statement on in an OpenMP target region offloaded to
// a GPU, we treat it as a basic block.
const bool isTargetDevice =
(cgm.getLangOpts().OpenMPIsTargetDevice && (t.isNVPTX() || t.isAMDGCN()));
if (isTargetDevice) {
cgm.errorNYI(
"emitCXXTryStmtUnderScope: OpenMP target region offloaded to GPU");
return mlir::success();
}
unsigned numHandlers = s.getNumHandlers();
mlir::Location tryLoc = getLoc(s.getBeginLoc());
mlir::OpBuilder::InsertPoint beginInsertTryBody;
bool hasCatchAll = false;
for (unsigned i = 0; i != numHandlers; ++i) {
hasCatchAll |= s.getHandler(i)->getExceptionDecl() == nullptr;
if (hasCatchAll)
break;
}
// Create the scope to represent only the C/C++ `try {}` part. However,
// don't populate right away. Create regions for the catch handlers,
// but don't emit the handler bodies yet. For now, only make sure the
// scope returns the exception information.
auto tryOp = cir::TryOp::create(
builder, tryLoc,
/*tryBuilder=*/
[&](mlir::OpBuilder &b, mlir::Location loc) {
beginInsertTryBody = builder.saveInsertionPoint();
},
/*handlersBuilder=*/
[&](mlir::OpBuilder &b, mlir::Location loc,
mlir::OperationState &result) {
mlir::OpBuilder::InsertionGuard guard(b);
// We create an extra region for an unwind catch handler in case the
// catch-all handler doesn't exists
unsigned numRegionsToCreate =
hasCatchAll ? numHandlers : numHandlers + 1;
for (unsigned i = 0; i != numRegionsToCreate; ++i) {
mlir::Region *region = result.addRegion();
builder.createBlock(region);
}
});
// Finally emit the body for try/catch.
{
mlir::Location loc = tryOp.getLoc();
mlir::OpBuilder::InsertionGuard guard(builder);
builder.restoreInsertionPoint(beginInsertTryBody);
CIRGenFunction::LexicalScope tryScope{*this, loc,
builder.getInsertionBlock()};
tryScope.setAsTry(tryOp);
// Attach the basic blocks for the catch regions.
enterCXXTryStmt(s, tryOp);
// Emit the body for the `try {}` part.
{
mlir::OpBuilder::InsertionGuard guard(builder);
CIRGenFunction::LexicalScope tryBodyScope{*this, loc,
builder.getInsertionBlock()};
if (emitStmt(s.getTryBlock(), /*useCurrentScope=*/true).failed())
return mlir::failure();
}
// Emit catch clauses.
exitCXXTryStmt(s);
}
return mlir::success();
}
void CIRGenFunction::enterCXXTryStmt(const CXXTryStmt &s, cir::TryOp tryOp,
bool isFnTryBlock) {
unsigned numHandlers = s.getNumHandlers();
EHCatchScope *catchScope = ehStack.pushCatch(numHandlers);
for (unsigned i = 0; i != numHandlers; ++i) {
const CXXCatchStmt *catchStmt = s.getHandler(i);
if (catchStmt->getExceptionDecl()) {
cgm.errorNYI("enterCXXTryStmt: CatchStmt with ExceptionDecl");
return;
}
// No exception decl indicates '...', a catch-all.
mlir::Region *handler = &tryOp.getHandlerRegions()[i];
catchScope->setHandler(i, cgm.getCXXABI().getCatchAllTypeInfo(), handler);
// Under async exceptions, catch(...) needs to catch HW exception too
// Mark scope with SehTryBegin as a SEH __try scope
if (getLangOpts().EHAsynch) {
cgm.errorNYI("enterCXXTryStmt: EHAsynch");
return;
}
}
}
void CIRGenFunction::exitCXXTryStmt(const CXXTryStmt &s, bool isFnTryBlock) {
unsigned numHandlers = s.getNumHandlers();
EHCatchScope &catchScope = cast<EHCatchScope>(*ehStack.begin());
assert(catchScope.getNumHandlers() == numHandlers);
cir::TryOp tryOp = curLexScope->getTry();
// If the catch was not required, bail out now.
if (!catchScope.mayThrow()) {
catchScope.clearHandlerBlocks();
ehStack.popCatch();
// Drop all basic block from all catch regions.
SmallVector<mlir::Block *> eraseBlocks;
for (mlir::Region &handlerRegion : tryOp.getHandlerRegions()) {
if (handlerRegion.empty())
continue;
for (mlir::Block &b : handlerRegion.getBlocks())
eraseBlocks.push_back(&b);
}
for (mlir::Block *b : eraseBlocks)
b->erase();
tryOp.setHandlerTypesAttr({});
return;
}
cgm.errorNYI("exitCXXTryStmt: Required catch");
}
|