1//===----------------------------------------------------------------------===//
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// Emit Stmt nodes as CIR code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CIRGenBuilder.h"
14#include "CIRGenFunction.h"
15
16#include "mlir/IR/Builders.h"
17#include "mlir/IR/Location.h"
18#include "mlir/Support/LLVM.h"
19#include "clang/AST/ExprCXX.h"
20#include "clang/AST/Stmt.h"
21#include "clang/AST/StmtOpenACC.h"
22#include "clang/AST/StmtOpenMP.h"
23#include "clang/AST/StmtSYCL.h"
24#include "clang/CIR/MissingFeatures.h"
25#include "clang/CodeGenUtils/LoopAttributes.h"
26#include "clang/CodeGenUtils/StmtUtils.h"
27#include "llvm/Support/SaveAndRestore.h"
28
29using namespace clang;
30using namespace clang::CIRGen;
31using namespace cir;
32
33/// Compute the atomic options that apply for the extent of a clang::atomic
34/// attributed statement, starting from the enclosing options adjustments on top
35/// of them.
36static clang::AtomicOptions getAdjustedAtomicOptions(clang::AtomicOptions ao,
37 const AtomicAttr *aa) {
38 if (!aa)
39 return ao;
40 for (auto option : aa->atomicOptions()) {
41 switch (option) {
42 case AtomicAttr::remote_memory:
43 ao.remote_memory = true;
44 break;
45 case AtomicAttr::no_remote_memory:
46 ao.remote_memory = false;
47 break;
48 case AtomicAttr::fine_grained_memory:
49 ao.fine_grained_memory = true;
50 break;
51 case AtomicAttr::no_fine_grained_memory:
52 ao.fine_grained_memory = false;
53 break;
54 case AtomicAttr::ignore_denormal_mode:
55 ao.ignore_denormal_mode = true;
56 break;
57 case AtomicAttr::no_ignore_denormal_mode:
58 ao.ignore_denormal_mode = false;
59 break;
60 }
61 }
62 return ao;
63}
64
65static mlir::LogicalResult emitStmtWithResult(CIRGenFunction &cgf,
66 const Stmt *exprResult,
67 AggValueSlot slot,
68 Address *lastValue) {
69 // We have to special case labels here. They are statements, but when put
70 // at the end of a statement expression, they yield the value of their
71 // subexpression. Handle this by walking through all labels we encounter,
72 // emitting them before we evaluate the subexpr.
73 // Similar issues arise for attributed statements.
74 while (!isa<Expr>(Val: exprResult)) {
75 if (const auto *ls = dyn_cast<LabelStmt>(Val: exprResult)) {
76 if (cgf.emitLabel(d: *ls->getDecl()).failed())
77 return mlir::failure();
78 exprResult = ls->getSubStmt();
79 } else if (const auto *as = dyn_cast<AttributedStmt>(Val: exprResult)) {
80 // FIXME: Update this if we ever have attributes that affect the
81 // semantics of an expression.
82 exprResult = as->getSubStmt();
83 } else {
84 llvm_unreachable("Unknown value statement");
85 }
86 }
87
88 const Expr *e = cast<Expr>(Val: exprResult);
89 QualType exprTy = e->getType();
90 if (cgf.hasAggregateEvaluationKind(type: exprTy)) {
91 cgf.emitAggExpr(e, slot);
92 } else {
93 // We can't return an RValue here because there might be cleanups at
94 // the end of the StmtExpr. Because of that, we have to emit the result
95 // here into a temporary alloca.
96 cgf.emitAnyExprToMem(e, location: *lastValue, quals: Qualifiers(),
97 /*IsInit*/ isInitializer: false);
98 }
99
100 return mlir::success();
101}
102
103mlir::LogicalResult CIRGenFunction::emitCompoundStmtWithoutScope(
104 const CompoundStmt &s, Address *lastValue, AggValueSlot slot) {
105 mlir::LogicalResult result = mlir::success();
106 const Stmt *exprResult = s.body_back();
107 assert((!lastValue || (lastValue && exprResult)) &&
108 "If lastValue is not null then the CompoundStmt must have a "
109 "StmtExprResult");
110
111 for (const Stmt *curStmt : s.body()) {
112 const bool saveResult = lastValue && exprResult == curStmt;
113 if (saveResult) {
114 if (emitStmtWithResult(cgf&: *this, exprResult, slot, lastValue).failed())
115 result = mlir::failure();
116 } else {
117 if (emitStmt(s: curStmt, /*useCurrentScope=*/false).failed())
118 result = mlir::failure();
119 }
120 }
121 return result;
122}
123
124mlir::LogicalResult
125CIRGenFunction::emitAttributedStmt(const AttributedStmt &s) {
126
127 bool noinline = inNoInlineAttributedStmt;
128 bool alwaysinline = inAlwaysInlineAttributedStmt;
129 const CallExpr *musttail = mustTailCall;
130 const AtomicAttr *atomicAttr = nullptr;
131
132 for (const Attr *attr : s.getAttrs()) {
133 switch (attr->getKind()) {
134 default:
135 break;
136 case attr::Atomic:
137 atomicAttr = cast<AtomicAttr>(Val: attr);
138 break;
139 case attr::NoMerge:
140 case attr::NoConvergent:
141 case attr::AMDGPUAvailableVisible:
142 case attr::HLSLControlFlowHint:
143 cgm.errorNYI(loc: s.getSourceRange(),
144 feature: "Unimplemented statement attribute: ", name: attr->getKind());
145 break;
146 case attr::NoInline:
147 noinline = true;
148 alwaysinline = false;
149 break;
150 case attr::AlwaysInline:
151 alwaysinline = true;
152 noinline = false;
153 break;
154 case attr::MustTail: {
155 const Stmt *sub = s.getSubStmt();
156 const ReturnStmt *ret = cast<ReturnStmt>(Val: sub);
157 musttail = cast<CallExpr>(Val: ret->getRetValue()->IgnoreParens());
158 break;
159 }
160 case attr::CXXAssume: {
161 const Expr *assumptionExpr = cast<CXXAssumeAttr>(Val: attr)->getAssumption();
162 if (getLangOpts().CXXAssumptions && builder.getInsertionBlock() &&
163 !assumptionExpr->HasSideEffects(Ctx: getContext())) {
164 mlir::Value assumptionValue = emitCheckedArgForAssume(e: assumptionExpr);
165 cir::AssumeOp::create(builder, location: getLoc(srcLoc: s.getSourceRange()),
166 predicate: assumptionValue, bundle_kind: cir::AssumeBundleKind::None,
167 bundle_args: mlir::ValueRange{});
168 }
169 } break;
170 }
171 }
172
173 assert(!(alwaysinline && noinline) &&
174 "alwaysinline and noinline are mutually exclusive");
175
176 SaveAndRestore save_noinline(inNoInlineAttributedStmt, noinline);
177 SaveAndRestore save_alwaysinline(inAlwaysInlineAttributedStmt, alwaysinline);
178
179 SaveAndRestore save_musttail(mustTailCall, musttail);
180
181 // clang::atomic adjusts the atomic options for the extent of the statement it
182 // is attached to, so they are saved and restored around it.
183 clang::AtomicOptions savedAtomicOpts = cgm.getAtomicOpts();
184 cgm.setAtomicOpts(getAdjustedAtomicOptions(ao: savedAtomicOpts, aa: atomicAttr));
185
186 mlir::LogicalResult result =
187 emitStmt(s: s.getSubStmt(), /*useCurrentScope=*/false, attrs: s.getAttrs());
188
189 cgm.setAtomicOpts(savedAtomicOpts);
190 return result;
191}
192
193mlir::LogicalResult CIRGenFunction::emitCompoundStmt(const CompoundStmt &s,
194 Address *lastValue,
195 AggValueSlot slot) {
196 // Add local scope to track new declared variables.
197 SymTableScopeTy varScope(symbolTable);
198 mlir::Location scopeLoc = getLoc(srcLoc: s.getSourceRange());
199 mlir::OpBuilder::InsertPoint scopeInsPt;
200 cir::ScopeOp::create(
201 builder, location: scopeLoc,
202 scopeBuilder: [&](mlir::OpBuilder &b, mlir::Type &type, mlir::Location loc) {
203 scopeInsPt = b.saveInsertionPoint();
204 });
205 mlir::OpBuilder::InsertionGuard guard(builder);
206 builder.restoreInsertionPoint(ip: scopeInsPt);
207 LexicalScope lexScope(*this, scopeLoc, builder.getInsertionBlock());
208 return emitCompoundStmtWithoutScope(s, lastValue, slot);
209}
210
211void CIRGenFunction::emitStopPoint(const Stmt *s) {
212 assert(!cir::MissingFeatures::generateDebugInfo());
213}
214
215// Build CIR for a statement. useCurrentScope should be true if no new scopes
216// need to be created when finding a compound statement.
217mlir::LogicalResult CIRGenFunction::emitStmt(const Stmt *s,
218 bool useCurrentScope,
219 ArrayRef<const Attr *> attr) {
220 if (mlir::succeeded(Result: emitSimpleStmt(s, useCurrentScope)))
221 return mlir::success();
222
223 switch (s->getStmtClass()) {
224 case Stmt::NoStmtClass:
225 case Stmt::CXXCatchStmtClass:
226 case Stmt::SEHExceptStmtClass:
227 case Stmt::SEHFinallyStmtClass:
228 case Stmt::MSDependentExistsStmtClass:
229 case Stmt::UnresolvedSYCLKernelCallStmtClass:
230 llvm_unreachable("invalid statement class to emit generically");
231 case Stmt::BreakStmtClass:
232 case Stmt::NullStmtClass:
233 case Stmt::CompoundStmtClass:
234 case Stmt::ContinueStmtClass:
235 case Stmt::DeclStmtClass:
236 case Stmt::ReturnStmtClass:
237 llvm_unreachable("should have emitted these statements as simple");
238
239#define STMT(Type, Base)
240#define ABSTRACT_STMT(Op)
241#define EXPR(Type, Base) case Stmt::Type##Class:
242#include "clang/AST/StmtNodes.inc"
243 {
244 assert(builder.getInsertionBlock() &&
245 "expression emission must have an insertion point");
246
247 emitIgnoredExpr(e: cast<Expr>(Val: s));
248
249 // Classic codegen has a check here to see if the emitter created a new
250 // block that isn't used (comparing the incoming and outgoing insertion
251 // points) and deletes the outgoing block if it's not used. In CIR, we
252 // will handle that during the cir.canonicalize pass.
253 return mlir::success();
254 }
255 case Stmt::IfStmtClass:
256 return emitIfStmt(s: cast<IfStmt>(Val: *s));
257 case Stmt::SwitchStmtClass:
258 return emitSwitchStmt(s: cast<SwitchStmt>(Val: *s));
259 case Stmt::ForStmtClass:
260 return emitForStmt(s: cast<ForStmt>(Val: *s), attrs: attr);
261 case Stmt::WhileStmtClass:
262 return emitWhileStmt(s: cast<WhileStmt>(Val: *s), attrs: attr);
263 case Stmt::DoStmtClass:
264 return emitDoStmt(s: cast<DoStmt>(Val: *s), attrs: attr);
265 case Stmt::CXXTryStmtClass:
266 return emitCXXTryStmt(s: cast<CXXTryStmt>(Val: *s));
267 case Stmt::CXXForRangeStmtClass:
268 return emitCXXForRangeStmt(s: cast<CXXForRangeStmt>(Val: *s), attrs: attr);
269 case Stmt::CoroutineBodyStmtClass:
270 return emitCoroutineBody(s: cast<CoroutineBodyStmt>(Val: *s));
271 case Stmt::IndirectGotoStmtClass:
272 return emitIndirectGotoStmt(s: cast<IndirectGotoStmt>(Val: *s));
273 case Stmt::CoreturnStmtClass:
274 return emitCoreturnStmt(s: cast<CoreturnStmt>(Val: *s));
275 case Stmt::SYCLKernelCallStmtClass:
276 return emitSYCLKernelCallStmt(s: cast<SYCLKernelCallStmt>(Val: *s));
277 case Stmt::OpenACCComputeConstructClass:
278 return emitOpenACCComputeConstruct(s: cast<OpenACCComputeConstruct>(Val: *s));
279 case Stmt::OpenACCLoopConstructClass:
280 return emitOpenACCLoopConstruct(s: cast<OpenACCLoopConstruct>(Val: *s));
281 case Stmt::OpenACCCombinedConstructClass:
282 return emitOpenACCCombinedConstruct(s: cast<OpenACCCombinedConstruct>(Val: *s));
283 case Stmt::OpenACCDataConstructClass:
284 return emitOpenACCDataConstruct(s: cast<OpenACCDataConstruct>(Val: *s));
285 case Stmt::OpenACCEnterDataConstructClass:
286 return emitOpenACCEnterDataConstruct(s: cast<OpenACCEnterDataConstruct>(Val: *s));
287 case Stmt::OpenACCExitDataConstructClass:
288 return emitOpenACCExitDataConstruct(s: cast<OpenACCExitDataConstruct>(Val: *s));
289 case Stmt::OpenACCHostDataConstructClass:
290 return emitOpenACCHostDataConstruct(s: cast<OpenACCHostDataConstruct>(Val: *s));
291 case Stmt::OpenACCWaitConstructClass:
292 return emitOpenACCWaitConstruct(s: cast<OpenACCWaitConstruct>(Val: *s));
293 case Stmt::OpenACCInitConstructClass:
294 return emitOpenACCInitConstruct(s: cast<OpenACCInitConstruct>(Val: *s));
295 case Stmt::OpenACCShutdownConstructClass:
296 return emitOpenACCShutdownConstruct(s: cast<OpenACCShutdownConstruct>(Val: *s));
297 case Stmt::OpenACCSetConstructClass:
298 return emitOpenACCSetConstruct(s: cast<OpenACCSetConstruct>(Val: *s));
299 case Stmt::OpenACCUpdateConstructClass:
300 return emitOpenACCUpdateConstruct(s: cast<OpenACCUpdateConstruct>(Val: *s));
301 case Stmt::OpenACCCacheConstructClass:
302 return emitOpenACCCacheConstruct(s: cast<OpenACCCacheConstruct>(Val: *s));
303 case Stmt::OpenACCAtomicConstructClass:
304 return emitOpenACCAtomicConstruct(s: cast<OpenACCAtomicConstruct>(Val: *s));
305 case Stmt::GCCAsmStmtClass:
306 case Stmt::MSAsmStmtClass:
307 return emitAsmStmt(s: cast<AsmStmt>(Val: *s));
308 case Stmt::OMPScopeDirectiveClass:
309 return emitOMPScopeDirective(s: cast<OMPScopeDirective>(Val: *s));
310 case Stmt::OMPErrorDirectiveClass:
311 return emitOMPErrorDirective(s: cast<OMPErrorDirective>(Val: *s));
312 case Stmt::OMPParallelDirectiveClass:
313 return emitOMPParallelDirective(s: cast<OMPParallelDirective>(Val: *s));
314 case Stmt::OMPTaskwaitDirectiveClass:
315 return emitOMPTaskwaitDirective(s: cast<OMPTaskwaitDirective>(Val: *s));
316 case Stmt::OMPTaskyieldDirectiveClass:
317 return emitOMPTaskyieldDirective(s: cast<OMPTaskyieldDirective>(Val: *s));
318 case Stmt::OMPBarrierDirectiveClass:
319 return emitOMPBarrierDirective(s: cast<OMPBarrierDirective>(Val: *s));
320 case Stmt::OMPMetaDirectiveClass:
321 return emitOMPMetaDirective(s: cast<OMPMetaDirective>(Val: *s));
322 case Stmt::OMPCanonicalLoopClass:
323 return emitOMPCanonicalLoop(s: cast<OMPCanonicalLoop>(Val: *s));
324 case Stmt::OMPSimdDirectiveClass:
325 return emitOMPSimdDirective(s: cast<OMPSimdDirective>(Val: *s));
326 case Stmt::OMPTileDirectiveClass:
327 return emitOMPTileDirective(s: cast<OMPTileDirective>(Val: *s));
328 case Stmt::OMPUnrollDirectiveClass:
329 return emitOMPUnrollDirective(s: cast<OMPUnrollDirective>(Val: *s));
330 case Stmt::OMPFuseDirectiveClass:
331 return emitOMPFuseDirective(s: cast<OMPFuseDirective>(Val: *s));
332 case Stmt::OMPForDirectiveClass:
333 return emitOMPForDirective(s: cast<OMPForDirective>(Val: *s));
334 case Stmt::OMPForSimdDirectiveClass:
335 return emitOMPForSimdDirective(s: cast<OMPForSimdDirective>(Val: *s));
336 case Stmt::OMPSectionsDirectiveClass:
337 return emitOMPSectionsDirective(s: cast<OMPSectionsDirective>(Val: *s));
338 case Stmt::OMPSectionDirectiveClass:
339 return emitOMPSectionDirective(s: cast<OMPSectionDirective>(Val: *s));
340 case Stmt::OMPSingleDirectiveClass:
341 return emitOMPSingleDirective(s: cast<OMPSingleDirective>(Val: *s));
342 case Stmt::OMPMasterDirectiveClass:
343 return emitOMPMasterDirective(s: cast<OMPMasterDirective>(Val: *s));
344 case Stmt::OMPCriticalDirectiveClass:
345 return emitOMPCriticalDirective(s: cast<OMPCriticalDirective>(Val: *s));
346 case Stmt::OMPParallelForDirectiveClass:
347 return emitOMPParallelForDirective(s: cast<OMPParallelForDirective>(Val: *s));
348 case Stmt::OMPParallelForSimdDirectiveClass:
349 return emitOMPParallelForSimdDirective(
350 s: cast<OMPParallelForSimdDirective>(Val: *s));
351 case Stmt::OMPParallelMasterDirectiveClass:
352 return emitOMPParallelMasterDirective(s: cast<OMPParallelMasterDirective>(Val: *s));
353 case Stmt::OMPParallelSectionsDirectiveClass:
354 return emitOMPParallelSectionsDirective(
355 s: cast<OMPParallelSectionsDirective>(Val: *s));
356 case Stmt::OMPTaskDirectiveClass:
357 return emitOMPTaskDirective(s: cast<OMPTaskDirective>(Val: *s));
358 case Stmt::OMPTaskgroupDirectiveClass:
359 return emitOMPTaskgroupDirective(s: cast<OMPTaskgroupDirective>(Val: *s));
360 case Stmt::OMPFlushDirectiveClass:
361 return emitOMPFlushDirective(s: cast<OMPFlushDirective>(Val: *s));
362 case Stmt::OMPDepobjDirectiveClass:
363 return emitOMPDepobjDirective(s: cast<OMPDepobjDirective>(Val: *s));
364 case Stmt::OMPScanDirectiveClass:
365 return emitOMPScanDirective(s: cast<OMPScanDirective>(Val: *s));
366 case Stmt::OMPOrderedStandaloneDirectiveClass:
367 return emitOMPOrderedStandaloneDirective(
368 s: cast<OMPOrderedStandaloneDirective>(Val: *s));
369 case Stmt::OMPOrderedBlockAssocDirectiveClass:
370 return emitOMPOrderedBlockAssocDirective(
371 s: cast<OMPOrderedBlockAssocDirective>(Val: *s));
372 case Stmt::OMPAtomicDirectiveClass:
373 return emitOMPAtomicDirective(s: cast<OMPAtomicDirective>(Val: *s));
374 case Stmt::OMPTargetDirectiveClass:
375 return emitOMPTargetDirective(s: cast<OMPTargetDirective>(Val: *s));
376 case Stmt::OMPTeamsDirectiveClass:
377 return emitOMPTeamsDirective(s: cast<OMPTeamsDirective>(Val: *s));
378 case Stmt::OMPCancellationPointDirectiveClass:
379 return emitOMPCancellationPointDirective(
380 s: cast<OMPCancellationPointDirective>(Val: *s));
381 case Stmt::OMPCancelDirectiveClass:
382 return emitOMPCancelDirective(s: cast<OMPCancelDirective>(Val: *s));
383 case Stmt::OMPTargetDataDirectiveClass:
384 return emitOMPTargetDataDirective(s: cast<OMPTargetDataDirective>(Val: *s));
385 case Stmt::OMPTargetEnterDataDirectiveClass:
386 return emitOMPTargetEnterDataDirective(
387 s: cast<OMPTargetEnterDataDirective>(Val: *s));
388 case Stmt::OMPTargetExitDataDirectiveClass:
389 return emitOMPTargetExitDataDirective(s: cast<OMPTargetExitDataDirective>(Val: *s));
390 case Stmt::OMPTargetParallelDirectiveClass:
391 return emitOMPTargetParallelDirective(s: cast<OMPTargetParallelDirective>(Val: *s));
392 case Stmt::OMPTargetParallelForDirectiveClass:
393 return emitOMPTargetParallelForDirective(
394 s: cast<OMPTargetParallelForDirective>(Val: *s));
395 case Stmt::OMPTaskLoopDirectiveClass:
396 return emitOMPTaskLoopDirective(s: cast<OMPTaskLoopDirective>(Val: *s));
397 case Stmt::OMPTaskLoopSimdDirectiveClass:
398 return emitOMPTaskLoopSimdDirective(s: cast<OMPTaskLoopSimdDirective>(Val: *s));
399 case Stmt::OMPMaskedTaskLoopDirectiveClass:
400 return emitOMPMaskedTaskLoopDirective(s: cast<OMPMaskedTaskLoopDirective>(Val: *s));
401 case Stmt::OMPMaskedTaskLoopSimdDirectiveClass:
402 return emitOMPMaskedTaskLoopSimdDirective(
403 s: cast<OMPMaskedTaskLoopSimdDirective>(Val: *s));
404 case Stmt::OMPMasterTaskLoopDirectiveClass:
405 return emitOMPMasterTaskLoopDirective(s: cast<OMPMasterTaskLoopDirective>(Val: *s));
406 case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
407 return emitOMPMasterTaskLoopSimdDirective(
408 s: cast<OMPMasterTaskLoopSimdDirective>(Val: *s));
409 case Stmt::OMPParallelGenericLoopDirectiveClass:
410 return emitOMPParallelGenericLoopDirective(
411 s: cast<OMPParallelGenericLoopDirective>(Val: *s));
412 case Stmt::OMPParallelMaskedDirectiveClass:
413 return emitOMPParallelMaskedDirective(s: cast<OMPParallelMaskedDirective>(Val: *s));
414 case Stmt::OMPParallelMaskedTaskLoopDirectiveClass:
415 return emitOMPParallelMaskedTaskLoopDirective(
416 s: cast<OMPParallelMaskedTaskLoopDirective>(Val: *s));
417 case Stmt::OMPParallelMaskedTaskLoopSimdDirectiveClass:
418 return emitOMPParallelMaskedTaskLoopSimdDirective(
419 s: cast<OMPParallelMaskedTaskLoopSimdDirective>(Val: *s));
420 case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
421 return emitOMPParallelMasterTaskLoopDirective(
422 s: cast<OMPParallelMasterTaskLoopDirective>(Val: *s));
423 case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
424 return emitOMPParallelMasterTaskLoopSimdDirective(
425 s: cast<OMPParallelMasterTaskLoopSimdDirective>(Val: *s));
426 case Stmt::OMPDistributeDirectiveClass:
427 return emitOMPDistributeDirective(s: cast<OMPDistributeDirective>(Val: *s));
428 case Stmt::OMPDistributeParallelForDirectiveClass:
429 return emitOMPDistributeParallelForDirective(
430 s: cast<OMPDistributeParallelForDirective>(Val: *s));
431 case Stmt::OMPDistributeParallelForSimdDirectiveClass:
432 return emitOMPDistributeParallelForSimdDirective(
433 s: cast<OMPDistributeParallelForSimdDirective>(Val: *s));
434 case Stmt::OMPDistributeSimdDirectiveClass:
435 return emitOMPDistributeSimdDirective(s: cast<OMPDistributeSimdDirective>(Val: *s));
436 case Stmt::OMPTargetParallelGenericLoopDirectiveClass:
437 return emitOMPTargetParallelGenericLoopDirective(
438 s: cast<OMPTargetParallelGenericLoopDirective>(Val: *s));
439 case Stmt::OMPTargetGenericLoopDirectiveClass:
440 return emitOMPTargetGenericLoopDirective(
441 s: cast<OMPTargetGenericLoopDirective>(Val: *s));
442 case Stmt::OMPTargetParallelForSimdDirectiveClass:
443 return emitOMPTargetParallelForSimdDirective(
444 s: cast<OMPTargetParallelForSimdDirective>(Val: *s));
445 case Stmt::OMPTargetSimdDirectiveClass:
446 return emitOMPTargetSimdDirective(s: cast<OMPTargetSimdDirective>(Val: *s));
447 case Stmt::OMPTargetTeamsGenericLoopDirectiveClass:
448 return emitOMPTargetTeamsGenericLoopDirective(
449 s: cast<OMPTargetTeamsGenericLoopDirective>(Val: *s));
450 case Stmt::OMPTargetUpdateDirectiveClass:
451 return emitOMPTargetUpdateDirective(s: cast<OMPTargetUpdateDirective>(Val: *s));
452 case Stmt::OMPTeamsDistributeDirectiveClass:
453 return emitOMPTeamsDistributeDirective(
454 s: cast<OMPTeamsDistributeDirective>(Val: *s));
455 case Stmt::OMPTeamsDistributeSimdDirectiveClass:
456 return emitOMPTeamsDistributeSimdDirective(
457 s: cast<OMPTeamsDistributeSimdDirective>(Val: *s));
458 case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
459 return emitOMPTeamsDistributeParallelForSimdDirective(
460 s: cast<OMPTeamsDistributeParallelForSimdDirective>(Val: *s));
461 case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
462 return emitOMPTeamsDistributeParallelForDirective(
463 s: cast<OMPTeamsDistributeParallelForDirective>(Val: *s));
464 case Stmt::OMPTeamsGenericLoopDirectiveClass:
465 return emitOMPTeamsGenericLoopDirective(
466 s: cast<OMPTeamsGenericLoopDirective>(Val: *s));
467 case Stmt::OMPTargetTeamsDirectiveClass:
468 return emitOMPTargetTeamsDirective(s: cast<OMPTargetTeamsDirective>(Val: *s));
469 case Stmt::OMPTargetTeamsDistributeDirectiveClass:
470 return emitOMPTargetTeamsDistributeDirective(
471 s: cast<OMPTargetTeamsDistributeDirective>(Val: *s));
472 case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
473 return emitOMPTargetTeamsDistributeParallelForDirective(
474 s: cast<OMPTargetTeamsDistributeParallelForDirective>(Val: *s));
475 case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
476 return emitOMPTargetTeamsDistributeParallelForSimdDirective(
477 s: cast<OMPTargetTeamsDistributeParallelForSimdDirective>(Val: *s));
478 case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
479 return emitOMPTargetTeamsDistributeSimdDirective(
480 s: cast<OMPTargetTeamsDistributeSimdDirective>(Val: *s));
481 case Stmt::OMPInteropDirectiveClass:
482 return emitOMPInteropDirective(s: cast<OMPInteropDirective>(Val: *s));
483 case Stmt::OMPDispatchDirectiveClass:
484 return emitOMPDispatchDirective(s: cast<OMPDispatchDirective>(Val: *s));
485 case Stmt::OMPGenericLoopDirectiveClass:
486 return emitOMPGenericLoopDirective(s: cast<OMPGenericLoopDirective>(Val: *s));
487 case Stmt::OMPReverseDirectiveClass:
488 return emitOMPReverseDirective(s: cast<OMPReverseDirective>(Val: *s));
489 case Stmt::OMPSplitDirectiveClass:
490 return emitOMPSplitDirective(s: cast<OMPSplitDirective>(Val: *s));
491 case Stmt::OMPInterchangeDirectiveClass:
492 return emitOMPInterchangeDirective(s: cast<OMPInterchangeDirective>(Val: *s));
493 case Stmt::OMPFlattenDirectiveClass:
494 return emitOMPFlattenDirective(s: cast<OMPFlattenDirective>(Val: *s));
495 case Stmt::OMPAssumeDirectiveClass:
496 return emitOMPAssumeDirective(s: cast<OMPAssumeDirective>(Val: *s));
497 case Stmt::OMPMaskedDirectiveClass:
498 return emitOMPMaskedDirective(s: cast<OMPMaskedDirective>(Val: *s));
499 case Stmt::OMPStripeDirectiveClass:
500 return emitOMPStripeDirective(s: cast<OMPStripeDirective>(Val: *s));
501 case Stmt::LabelStmtClass:
502 case Stmt::AttributedStmtClass:
503 case Stmt::GotoStmtClass:
504 case Stmt::DefaultStmtClass:
505 case Stmt::CaseStmtClass:
506 case Stmt::SEHLeaveStmtClass:
507 case Stmt::ObjCAtTryStmtClass:
508 case Stmt::ObjCAtThrowStmtClass:
509 case Stmt::ObjCAtSynchronizedStmtClass:
510 case Stmt::ObjCForCollectionStmtClass:
511 case Stmt::ObjCAutoreleasePoolStmtClass:
512 case Stmt::SEHTryStmtClass:
513 case Stmt::ObjCAtCatchStmtClass:
514 case Stmt::ObjCAtFinallyStmtClass:
515 case Stmt::DeferStmtClass:
516 case Stmt::CXXExpansionStmtPatternClass:
517 case Stmt::CXXExpansionStmtInstantiationClass:
518 cgm.errorNYI(s->getSourceRange(),
519 std::string("emitStmt: ") + s->getStmtClassName());
520 return mlir::failure();
521 case Stmt::CapturedStmtClass:
522 llvm_unreachable("CapturedStmt must be handled by the parent directive");
523 }
524
525 llvm_unreachable("Unexpected statement class");
526}
527
528mlir::LogicalResult CIRGenFunction::emitSimpleStmt(const Stmt *s,
529 bool useCurrentScope) {
530 switch (s->getStmtClass()) {
531 default:
532 return mlir::failure();
533 case Stmt::DeclStmtClass:
534 return emitDeclStmt(s: cast<DeclStmt>(Val: *s));
535 case Stmt::CompoundStmtClass:
536 if (useCurrentScope)
537 return emitCompoundStmtWithoutScope(s: cast<CompoundStmt>(Val: *s));
538 return emitCompoundStmt(s: cast<CompoundStmt>(Val: *s));
539 case Stmt::GotoStmtClass:
540 return emitGotoStmt(s: cast<GotoStmt>(Val: *s));
541 case Stmt::ContinueStmtClass:
542 return emitContinueStmt(s: cast<ContinueStmt>(Val: *s));
543
544 // NullStmt doesn't need any handling, but we need to say we handled it.
545 case Stmt::NullStmtClass:
546 break;
547
548 case Stmt::LabelStmtClass:
549 return emitLabelStmt(s: cast<LabelStmt>(Val: *s));
550 case Stmt::CaseStmtClass:
551 case Stmt::DefaultStmtClass:
552 // If we reached here, we must not handling a switch case in the top level.
553 return emitSwitchCase(s: cast<SwitchCase>(Val: *s),
554 /*buildingTopLevelCase=*/false);
555 break;
556
557 case Stmt::BreakStmtClass:
558 return emitBreakStmt(s: cast<BreakStmt>(Val: *s));
559 case Stmt::ReturnStmtClass:
560 return emitReturnStmt(s: cast<ReturnStmt>(Val: *s));
561 case Stmt::AttributedStmtClass:
562 return emitAttributedStmt(s: cast<AttributedStmt>(Val: *s));
563 }
564
565 return mlir::success();
566}
567
568mlir::LogicalResult CIRGenFunction::emitLabelStmt(const clang::LabelStmt &s) {
569
570 if (emitLabel(d: *s.getDecl()).failed())
571 return mlir::failure();
572
573 if (getContext().getLangOpts().EHAsynch && s.isSideEntry())
574 getCIRGenModule().errorNYI(s.getSourceRange(), "IsEHa: not implemented.");
575
576 return emitStmt(s: s.getSubStmt(), /*useCurrentScope=*/false);
577}
578
579// Add a terminating yield on a body region if no other terminators are used.
580void CIRGenFunction::terminateStructuredRegionBody(mlir::Region &r,
581 mlir::Location loc) {
582 if (r.empty())
583 return;
584
585 SmallVector<mlir::Block *, 4> eraseBlocks;
586 unsigned numBlocks = r.getBlocks().size();
587 for (auto &block : r.getBlocks()) {
588 // Already cleanup after return operations, which might create
589 // empty blocks if emitted as last stmt.
590 if (numBlocks != 1 && block.empty() && block.hasNoPredecessors() &&
591 block.hasNoSuccessors())
592 eraseBlocks.push_back(Elt: &block);
593
594 if (block.empty() ||
595 !block.back().hasTrait<mlir::OpTrait::IsTerminator>()) {
596 mlir::OpBuilder::InsertionGuard guardCase(builder);
597 builder.setInsertionPointToEnd(&block);
598 builder.createYield(loc);
599 }
600 }
601
602 for (auto *b : eraseBlocks)
603 b->erase();
604}
605
606mlir::LogicalResult CIRGenFunction::emitIfStmt(const IfStmt &s) {
607 mlir::LogicalResult res = mlir::success();
608 // The else branch of a consteval if statement is always the only branch
609 // that can be runtime evaluated.
610 const Stmt *constevalExecuted;
611 if (s.isConsteval()) {
612 constevalExecuted = s.isNegatedConsteval() ? s.getThen() : s.getElse();
613 if (!constevalExecuted) {
614 // No runtime code execution required
615 return res;
616 }
617 }
618
619 // C99 6.8.4.1: The first substatement is executed if the expression
620 // compares unequal to 0. The condition must be a scalar type.
621 auto ifStmtBuilder = [&]() -> mlir::LogicalResult {
622 if (s.isConsteval())
623 return emitStmt(s: constevalExecuted, /*useCurrentScope=*/true);
624
625 if (s.getInit())
626 if (emitStmt(s: s.getInit(), /*useCurrentScope=*/true).failed())
627 return mlir::failure();
628
629 if (s.getConditionVariable())
630 emitDecl(d: *s.getConditionVariable());
631
632 // If the condition constant folds and can be elided, try to avoid
633 // emitting the condition and the dead arm of the if/else.
634 bool condConstant;
635 if (constantFoldsToBool(cond: s.getCond(), resultBool&: condConstant, allowLabels: s.isConstexpr())) {
636 // Figure out which block (then or else) is executed.
637 const Stmt *executed = s.getThen();
638 const Stmt *skipped = s.getElse();
639 if (!condConstant) // Condition false?
640 std::swap(a&: executed, b&: skipped);
641
642 // If the skipped block has no labels in it, just emit the executed block.
643 // This avoids emitting dead code and simplifies the CFG substantially.
644 if (s.isConstexpr() || !containsLabel(s: skipped)) {
645 if (executed)
646 return emitStmt(s: executed, /*useCurrentScope=*/true);
647 // There is nothing to execute at runtime.
648 // TODO(cir): there is still an empty cir.scope generated by the caller.
649 return mlir::success();
650 }
651 }
652
653 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
654 assert(!cir::MissingFeatures::incrementProfileCounter());
655 return emitIfOnBoolExpr(cond: s.getCond(), thenS: s.getThen(), elseS: s.getElse());
656 };
657
658 // TODO: Add a new scoped symbol table.
659 // LexicalScope ConditionScope(*this, S.getCond()->getSourceRange());
660 // The if scope contains the full source range for IfStmt.
661 mlir::Location scopeLoc = getLoc(srcLoc: s.getSourceRange());
662 cir::ScopeOp::create(builder, location: scopeLoc, /*scopeBuilder=*/
663 [&](mlir::OpBuilder &b, mlir::Location loc) {
664 LexicalScope lexScope{*this, scopeLoc,
665 builder.getInsertionBlock()};
666 res = ifStmtBuilder();
667 });
668
669 return res;
670}
671
672mlir::LogicalResult CIRGenFunction::emitDeclStmt(const DeclStmt &s) {
673 assert(builder.getInsertionBlock() && "expected valid insertion point");
674
675 for (const Decl *i : s.decls())
676 emitDecl(d: *i, /*evaluateConditionDecl=*/true);
677
678 return mlir::success();
679}
680
681mlir::LogicalResult CIRGenFunction::emitReturnStmt(const ReturnStmt &s) {
682 mlir::Location loc = getLoc(srcLoc: s.getSourceRange());
683 const Expr *rv = s.getRetValue();
684
685 RunCleanupsScope cleanupScope(*this);
686 bool createNewScope = false;
687 if (const auto *ewc = dyn_cast_or_null<ExprWithCleanups>(Val: rv)) {
688 rv = ewc->getSubExpr();
689 createNewScope = true;
690 }
691
692 auto handleReturnVal = [&]() {
693 if (getContext().getLangOpts().ElideConstructors && s.getNRVOCandidate() &&
694 s.getNRVOCandidate()->isNRVOVariable()) {
695 assert(!cir::MissingFeatures::openMP());
696 // Apply the named return value optimization for this return statement,
697 // which means doing nothing: the appropriate result has already been
698 // constructed into the NRVO variable.
699
700 // If there is an NRVO flag for this variable, set it to 1 into indicate
701 // that the cleanup code should not destroy the variable.
702 if (auto nrvoFlag = nrvoFlags[s.getNRVOCandidate()])
703 builder.createFlagStore(loc, val: true, dst: nrvoFlag);
704 } else if (!rv) {
705 // No return expression. Do nothing.
706 } else if (rv->getType()->isVoidType()) {
707 // Make sure not to return anything, but evaluate the expression
708 // for side effects.
709 if (rv) {
710 emitAnyExpr(e: rv);
711 }
712 } else if (cast<FunctionDecl>(Val: curGD.getDecl())
713 ->getReturnType()
714 ->isReferenceType()) {
715 // If this function returns a reference, take the address of the
716 // expression rather than the value.
717 RValue result = emitReferenceBindingToExpr(e: rv);
718 builder.CIRBaseBuilderTy::createStore(loc, val: result.getValue(),
719 dst: *fnRetAlloca);
720 } else {
721 mlir::Value value = nullptr;
722 switch (CIRGenFunction::getEvaluationKind(type: rv->getType())) {
723 case cir::TEK_Scalar:
724 value = emitScalarExpr(e: rv);
725 if (value) { // Change this to an assert once emitScalarExpr is complete
726 builder.CIRBaseBuilderTy::createStore(loc, val: value, dst: *fnRetAlloca);
727 }
728 break;
729 case cir::TEK_Complex:
730 emitComplexExprIntoLValue(e: rv,
731 dest: makeAddrLValue(addr: returnValue, ty: rv->getType()),
732 /*isInit=*/true);
733 break;
734 case cir::TEK_Aggregate:
735 assert(!cir::MissingFeatures::aggValueSlotGC());
736 emitAggExpr(e: rv, slot: AggValueSlot::forAddr(addr: returnValue, quals: Qualifiers(),
737 isDestructed: AggValueSlot::IsDestructed,
738 isAliased: AggValueSlot::IsNotAliased,
739 mayOverlap: getOverlapForReturnValue()));
740 break;
741 }
742 }
743 };
744
745 if (!createNewScope) {
746 handleReturnVal();
747 } else {
748 FullExprCleanupScope fullExprScope(*this, rv);
749 handleReturnVal();
750 }
751
752 cleanupScope.forceCleanup();
753
754 // Classic codegen emits a branch through any cleanups before continuing to
755 // a shared return block. Because CIR handles branching through cleanups
756 // during the CFG flattening phase, we can just emit the return statement
757 // directly.
758 // TODO(cir): Eliminate this redundant load and the store above when we can.
759 if (fnRetAlloca) {
760 // Load the value from `__retval` and return it via the `cir.return` op.
761 cir::AllocaOp retAlloca =
762 mlir::cast<cir::AllocaOp>(Val: fnRetAlloca->getDefiningOp());
763 auto value = cir::LoadOp::create(builder, location: loc, result: retAlloca.getAllocaType(),
764 addr: *fnRetAlloca);
765
766 cir::ReturnOp::create(builder, location: loc, input: {value});
767 } else {
768 cir::ReturnOp::create(builder, location: loc);
769 }
770
771 // Insert the new block to continue codegen after the return statement.
772 // This will get deleted if we don't populate it. This handles the case of
773 // unreachable statements below a return.
774 builder.createBlock(parent: builder.getBlock()->getParent());
775 return mlir::success();
776}
777
778mlir::LogicalResult CIRGenFunction::emitGotoStmt(const clang::GotoStmt &s) {
779 // FIXME: LLVM codegen inserts emit a stop point here for debug info
780 // sake when the insertion point is available, but doesn't do
781 // anything special when there isn't. We haven't implemented debug
782 // info support just yet, look at this again once we have it.
783 assert(!cir::MissingFeatures::generateDebugInfo());
784
785 cir::GotoOp::create(builder, location: getLoc(srcLoc: s.getSourceRange()),
786 label: s.getLabel()->getName());
787
788 // A goto marks the end of a block, create a new one for codegen after
789 // emitGotoStmt can resume building in that block.
790 // Insert the new block to continue codegen after goto.
791 builder.createBlock(parent: builder.getBlock()->getParent());
792
793 return mlir::success();
794}
795
796mlir::LogicalResult
797CIRGenFunction::emitIndirectGotoStmt(const IndirectGotoStmt &s) {
798 // An indirect goto with an active cleanup may leave its scope. Determining
799 // whether its dynamic destination requires cleanup is not implemented.
800 if (ehStack.stable_begin() != prologueCleanupDepth) {
801 cgm.errorNYI(s.getSourceRange(), "indirect goto with active cleanup");
802 return mlir::success();
803 }
804
805 mlir::Value val = emitScalarExpr(e: s.getTarget());
806 // Emit a symbolic indirect goto. GotoSolver resolves it into the shared
807 // indirect-branch block after FlattenCFG merges regions, so this stays valid
808 // even when the goto sits inside a nested scope.
809 cir::IndirectGotoOp::create(builder, location: getLoc(srcLoc: s.getSourceRange()), addr: val);
810
811 // The indirect goto ends the block; open a fresh one so codegen can resume.
812 builder.createBlock(parent: builder.getBlock()->getParent());
813 return mlir::success();
814}
815
816mlir::LogicalResult
817CIRGenFunction::emitContinueStmt(const clang::ContinueStmt &s) {
818 builder.createContinue(loc: getLoc(srcLoc: s.getKwLoc()));
819
820 // Insert the new block to continue codegen after the continue statement.
821 builder.createBlock(parent: builder.getBlock()->getParent());
822
823 return mlir::success();
824}
825
826mlir::LogicalResult CIRGenFunction::emitLabel(const clang::LabelDecl &d) {
827 // Create a new block to tag with a label and add a branch from
828 // the current one to it. If the block is empty just call attach it
829 // to this label.
830 mlir::Block *currBlock = builder.getBlock();
831 mlir::Block *labelBlock = currBlock;
832
833 if (!currBlock->empty() || currBlock->isEntryBlock()) {
834 {
835 mlir::OpBuilder::InsertionGuard guard(builder);
836 labelBlock = builder.createBlock(parent: builder.getBlock()->getParent());
837 }
838 cir::BrOp::create(builder, location: getLoc(srcLoc: d.getSourceRange()), dest: labelBlock);
839 }
840
841 builder.setInsertionPointToEnd(labelBlock);
842 cir::LabelOp::create(builder, location: getLoc(srcLoc: d.getSourceRange()), label: d.getName());
843 // In a coroutine, the label makes the end of the body reachable again even
844 // after a co_return, so the implicit `co_return;` must not be left out.
845 if (curLexScope)
846 curLexScope->clearCoreturnInEnclosingScopes();
847 // FIXME: emit debug info for labels, incrementProfileCounter
848 assert(!cir::MissingFeatures::incrementProfileCounter());
849 assert(!cir::MissingFeatures::generateDebugInfo());
850 return mlir::success();
851}
852
853mlir::LogicalResult CIRGenFunction::emitBreakStmt(const clang::BreakStmt &s) {
854 builder.createBreak(loc: getLoc(srcLoc: s.getKwLoc()));
855
856 // Insert the new block to continue codegen after the break statement.
857 builder.createBlock(parent: builder.getBlock()->getParent());
858
859 return mlir::success();
860}
861
862template <typename T>
863mlir::LogicalResult
864CIRGenFunction::emitCaseDefaultCascade(const T *stmt, mlir::Type condType,
865 mlir::ArrayAttr value, CaseOpKind kind,
866 bool buildingTopLevelCase) {
867
868 assert((isa<CaseStmt, DefaultStmt>(stmt)) &&
869 "only case or default stmt go here");
870
871 mlir::LogicalResult result = mlir::success();
872
873 mlir::Location loc = getLoc(stmt->getBeginLoc());
874
875 enum class SubStmtKind { Case, Default, Other };
876 SubStmtKind subStmtKind = SubStmtKind::Other;
877 const Stmt *sub = stmt->getSubStmt();
878
879 mlir::OpBuilder::InsertPoint insertPoint;
880 CaseOp::create(builder, location: loc, value, kind, insertPoint);
881
882 {
883 mlir::OpBuilder::InsertionGuard guardSwitch(builder);
884 builder.restoreInsertionPoint(ip: insertPoint);
885
886 if (isa<DefaultStmt>(Val: sub) && isa<CaseStmt>(stmt)) {
887 subStmtKind = SubStmtKind::Default;
888 builder.createYield(loc);
889 } else if (isa<CaseStmt>(Val: sub) && isa<DefaultStmt, CaseStmt>(stmt)) {
890 subStmtKind = SubStmtKind::Case;
891 builder.createYield(loc);
892 } else {
893 result = emitStmt(s: sub, /*useCurrentScope=*/!isa<CompoundStmt>(Val: sub));
894 }
895
896 insertPoint = builder.saveInsertionPoint();
897 }
898
899 // If the substmt is default stmt or case stmt, try to handle the special case
900 // to make it into the simple form. e.g.
901 //
902 // switch () {
903 // case 1:
904 // default:
905 // ...
906 // }
907 //
908 // we prefer generating
909 //
910 // cir.switch() {
911 // cir.case(equal, 1) {
912 // cir.yield
913 // }
914 // cir.case(default) {
915 // ...
916 // }
917 // }
918 //
919 // than
920 //
921 // cir.switch() {
922 // cir.case(equal, 1) {
923 // cir.case(default) {
924 // ...
925 // }
926 // }
927 // }
928 //
929 // We don't need to revert this if we find the current switch can't be in
930 // simple form later since the conversion itself should be harmless.
931 if (subStmtKind == SubStmtKind::Case) {
932 result = emitCaseStmt(s: *cast<CaseStmt>(Val: sub), condType, buildingTopLevelCase);
933 } else if (subStmtKind == SubStmtKind::Default) {
934 result = emitDefaultStmt(s: *cast<DefaultStmt>(Val: sub), condType,
935 buildingTopLevelCase);
936 } else if (buildingTopLevelCase) {
937 // If we're building a top level case, try to restore the insert point to
938 // the case we're building, then we can attach more random stmts to the
939 // case to make generating `cir.switch` operation to be a simple form.
940 builder.restoreInsertionPoint(ip: insertPoint);
941 }
942
943 return result;
944}
945
946mlir::LogicalResult CIRGenFunction::emitCaseStmt(const CaseStmt &s,
947 mlir::Type condType,
948 bool buildingTopLevelCase) {
949 cir::CaseOpKind kind;
950 mlir::ArrayAttr value;
951 llvm::APSInt intVal = s.getLHS()->EvaluateKnownConstInt(Ctx: getContext());
952
953 // Coerce a bool to an i1 for a switch, so we can just treat all its elements
954 // as an int later on.
955 if (isa<cir::BoolType>(Val: condType))
956 condType = builder.getUIntNTy(n: 1);
957
958 // If the case statement has an RHS value, it is representing a GNU
959 // case range statement, where LHS is the beginning of the range
960 // and RHS is the end of the range.
961 if (const Expr *rhs = s.getRHS()) {
962 llvm::APSInt endVal = rhs->EvaluateKnownConstInt(Ctx: getContext());
963 value = builder.getArrayAttr(value: {cir::IntAttr::get(type: condType, value: intVal),
964 cir::IntAttr::get(type: condType, value: endVal)});
965 kind = cir::CaseOpKind::Range;
966 } else {
967 value = builder.getArrayAttr(value: {cir::IntAttr::get(type: condType, value: intVal)});
968 kind = cir::CaseOpKind::Equal;
969 }
970
971 return emitCaseDefaultCascade(stmt: &s, condType, value, kind,
972 buildingTopLevelCase);
973}
974
975mlir::LogicalResult CIRGenFunction::emitDefaultStmt(const clang::DefaultStmt &s,
976 mlir::Type condType,
977 bool buildingTopLevelCase) {
978 return emitCaseDefaultCascade(stmt: &s, condType, value: builder.getArrayAttr(value: {}),
979 kind: cir::CaseOpKind::Default, buildingTopLevelCase);
980}
981
982mlir::LogicalResult CIRGenFunction::emitSwitchCase(const SwitchCase &s,
983 bool buildingTopLevelCase) {
984 assert(!condTypeStack.empty() &&
985 "build switch case without specifying the type of the condition");
986
987 if (s.getStmtClass() == Stmt::CaseStmtClass)
988 return emitCaseStmt(s: cast<CaseStmt>(Val: s), condType: condTypeStack.back(),
989 buildingTopLevelCase);
990
991 if (s.getStmtClass() == Stmt::DefaultStmtClass)
992 return emitDefaultStmt(s: cast<DefaultStmt>(Val: s), condType: condTypeStack.back(),
993 buildingTopLevelCase);
994
995 llvm_unreachable("expect case or default stmt");
996}
997
998cir::LoopHintAttr CIRGenFunction::getLoopHints(ArrayRef<const Attr *> attrs,
999 bool mustProgress) {
1000 // Determine the state of the hints, the same way classic codegen does.
1001 CodeGenUtils::LoopAttributes loopAttrs;
1002 CodeGenUtils::updateLoopAttributes(Attrs&: loopAttrs, Ctx&: getContext(),
1003 CGOpts: cgm.getCodeGenOpts(), StmtAttrs: attrs, MustProgress: mustProgress);
1004
1005 // Memory accesses in a `parallel` loop are tagged as parallel accesses when
1006 // CIR is lowered.
1007 assert(!cir::MissingFeatures::loopInfoStack());
1008 mlir::MLIRContext *ctx = &getMLIRContext();
1009
1010 auto convertFlag = [&](bool set) {
1011 return set ? mlir::UnitAttr::get(context: ctx) : mlir::UnitAttr();
1012 };
1013 auto convertStateToBool =
1014 [&](CodeGenUtils::LoopAttributes::LVEnableState state) {
1015 switch (state) {
1016 case CodeGenUtils::LoopAttributes::LVEnableState::Unspecified:
1017 return mlir::BoolAttr();
1018 case CodeGenUtils::LoopAttributes::LVEnableState::Enable:
1019 return mlir::BoolAttr::get(context: ctx, value: true);
1020 case CodeGenUtils::LoopAttributes::LVEnableState::Disable:
1021 return mlir::BoolAttr::get(context: ctx, value: false);
1022 case CodeGenUtils::LoopAttributes::LVEnableState::Full:
1023 llvm_unreachable("'full' only applies to unrolling");
1024 }
1025 llvm_unreachable("unknown bool state");
1026 };
1027
1028 auto convertUnrollMode = [](CodeGenUtils::LoopAttributes::LVEnableState state)
1029 -> std::optional<cir::LoopUnrollMode> {
1030 switch (state) {
1031 case CodeGenUtils::LoopAttributes::LVEnableState::Unspecified:
1032 return std::nullopt;
1033 case CodeGenUtils::LoopAttributes::LVEnableState::Enable:
1034 return cir::LoopUnrollMode::Enable;
1035 case CodeGenUtils::LoopAttributes::LVEnableState::Disable:
1036 return cir::LoopUnrollMode::Disable;
1037 case CodeGenUtils::LoopAttributes::LVEnableState::Full:
1038 return cir::LoopUnrollMode::Full;
1039 }
1040
1041 llvm_unreachable("unknown unroll state");
1042 };
1043
1044 // Only OMP seems to generate this, so leave as an NYI for now. The dialect
1045 // can comprehend this/it will work correctly, but no tests exist. Once we get
1046 // good OMP tests for this, we should be able to remove this NYI.
1047 if (loopAttrs.VectorizeEnable ==
1048 CodeGenUtils::LoopAttributes::LVEnableState::Disable ||
1049 loopAttrs.VectorizeEnable ==
1050 CodeGenUtils::LoopAttributes::LVEnableState::Full)
1051 cgm.errorNYI(SourceLocation{}, "unsupported vector-enable loop state");
1052
1053 auto hints = cir::LoopHintAttr::get(
1054 context: ctx, must_progress: convertFlag(loopAttrs.MustProgress),
1055 vectorize_enable: convertStateToBool(loopAttrs.VectorizeEnable), vectorize_width: loopAttrs.VectorizeWidth,
1056 vectorize_scalable: convertStateToBool(loopAttrs.VectorizeScalable),
1057 vectorize_predicate: convertStateToBool(loopAttrs.VectorizePredicateEnable),
1058 interleave_count: loopAttrs.InterleaveCount, unroll: convertUnrollMode(loopAttrs.UnrollEnable),
1059 unroll_count: loopAttrs.UnrollCount, unroll_and_jam: convertUnrollMode(loopAttrs.UnrollAndJamEnable),
1060 unroll_and_jam_count: loopAttrs.UnrollAndJamCount,
1061 distribute: convertStateToBool(loopAttrs.DistributeEnable),
1062 pipeline_disabled: convertFlag(loopAttrs.PipelineDisabled),
1063 pipeline_initiation_interval: loopAttrs.PipelineInitiationInterval, licm_disabled: convertFlag(loopAttrs.LICMDisabled),
1064 parallel: convertFlag(loopAttrs.IsParallel), code_align: loopAttrs.CodeAlign);
1065
1066 if (hints.isEmpty())
1067 return {};
1068 return hints;
1069}
1070
1071static void setLoopHints(cir::LoopOpInterface loopOp,
1072 cir::LoopHintAttr loopHints) {
1073 if (!loopHints)
1074 return;
1075 loopOp.setLoopHintsAttr(loopHints);
1076 loopOp.setLoopIdAttr(
1077 mlir::DistinctAttr::create(referencedAttr: mlir::UnitAttr::get(context: loopOp->getContext())));
1078}
1079
1080mlir::LogicalResult
1081CIRGenFunction::emitCXXForRangeStmt(const CXXForRangeStmt &s,
1082 ArrayRef<const Attr *> forAttrs) {
1083 cir::ForOp forOp;
1084
1085 auto forStmtBuilder = [&]() -> mlir::LogicalResult {
1086 mlir::LogicalResult loopRes = mlir::success();
1087 // Evaluate the first pieces before the loop.
1088 if (s.getInit())
1089 if (emitStmt(s: s.getInit(), /*useCurrentScope=*/true).failed())
1090 return mlir::failure();
1091 if (emitStmt(s: s.getRangeStmt(), /*useCurrentScope=*/true).failed())
1092 return mlir::failure();
1093 if (emitStmt(s: s.getBeginStmt(), /*useCurrentScope=*/true).failed())
1094 return mlir::failure();
1095 if (emitStmt(s: s.getEndStmt(), /*useCurrentScope=*/true).failed())
1096 return mlir::failure();
1097
1098 // FIXME: Classic codegen never sets 'must progress' on these loops, but
1099 // probably could?
1100 cir::LoopHintAttr loopHints =
1101 getLoopHints(attrs: forAttrs, /*mustProgress=*/false);
1102
1103 forOp = builder.createFor(
1104 loc: getLoc(srcLoc: s.getSourceRange()),
1105 /*condBuilder=*/
1106 [&](mlir::OpBuilder &b, mlir::Location loc) {
1107 assert(!cir::MissingFeatures::createProfileWeightsForLoop());
1108 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
1109 mlir::Value condVal = evaluateExprAsBool(e: s.getCond());
1110 builder.createCondition(condition: condVal);
1111 },
1112 /*bodyBuilder=*/
1113 [&](mlir::OpBuilder &b, mlir::Location loc) {
1114 // https://en.cppreference.com/w/cpp/language/for
1115 // In C++ the scope of the init-statement and the scope of
1116 // statement are one and the same.
1117 RunCleanupsScope bodyScope(*this);
1118 bool useCurrentScope = true;
1119 if (emitStmt(s: s.getLoopVarStmt(), useCurrentScope).failed())
1120 loopRes = mlir::failure();
1121 if (emitStmt(s: s.getBody(), useCurrentScope).failed())
1122 loopRes = mlir::failure();
1123 emitStopPoint(s: &s);
1124 },
1125 /*stepBuilder=*/
1126 [&](mlir::OpBuilder &b, mlir::Location loc) {
1127 if (s.getInc())
1128 if (emitStmt(s: s.getInc(), /*useCurrentScope=*/true).failed())
1129 loopRes = mlir::failure();
1130 builder.createYield(loc);
1131 });
1132 setLoopHints(loopOp: forOp, loopHints);
1133 return loopRes;
1134 };
1135
1136 mlir::LogicalResult res = mlir::success();
1137 mlir::Location scopeLoc = getLoc(srcLoc: s.getSourceRange());
1138 cir::ScopeOp::create(builder, location: scopeLoc, /*scopeBuilder=*/
1139 [&](mlir::OpBuilder &b, mlir::Location loc) {
1140 // Create a cleanup scope for the condition
1141 // variable cleanups. Logical equivalent from
1142 // LLVM codegn for LexicalScope
1143 // ConditionScope(*this, S.getSourceRange())...
1144 LexicalScope lexScope{*this, loc,
1145 builder.getInsertionBlock()};
1146 res = forStmtBuilder();
1147 });
1148
1149 if (res.failed())
1150 return res;
1151
1152 terminateStructuredRegionBody(r&: forOp.getBody(), loc: getLoc(srcLoc: s.getEndLoc()));
1153 return mlir::success();
1154}
1155
1156mlir::LogicalResult CIRGenFunction::emitForStmt(const ForStmt &s,
1157 ArrayRef<const Attr *> attrs) {
1158 cir::ForOp forOp;
1159
1160 auto forStmtBuilder = [&]() -> mlir::LogicalResult {
1161 mlir::LogicalResult loopRes = mlir::success();
1162 // Evaluate the first part before the loop.
1163 if (s.getInit())
1164 if (emitStmt(s: s.getInit(), /*useCurrentScope=*/true).failed())
1165 return mlir::failure();
1166 bool mustProgress =
1167 checkIfLoopMustProgress(controllingExpression: s.getCond(), hasEmptyBody: CodeGenUtils::hasEmptyLoopBody(S: s));
1168 cir::LoopHintAttr loopHints = getLoopHints(attrs, mustProgress);
1169
1170 // A condition variable's lifetime is a single iteration, so capture its
1171 // destructor and lifetime-end cleanups, and those of the temporaries that
1172 // its initializer lifetime-extends, and emit them into the loop's
1173 // per-iteration cleanup region. This scope is constructed after the
1174 // init-statement so the init-statement's cleanups are not captured.
1175 const VarDecl *condVar = s.getConditionVariable();
1176 SmallVector<QualType, 2> condVarExtendedTemporaries;
1177 if (condVar)
1178 condVarExtendedTemporaries =
1179 getLifetimeExtendedTemporariesWithDestructor(d: *condVar);
1180 bool needsCondCleanup =
1181 condVar &&
1182 (condVar->needsDestruction(Ctx: getContext()) != QualType::DK_none ||
1183 shouldEmitLifetimeMarkersForAutoVar() ||
1184 !condVarExtendedTemporaries.empty());
1185 CapturedCleanups loopCondScope(*this, needsCondCleanup);
1186
1187 auto condBuilder = [&](mlir::OpBuilder &b, mlir::Location loc) {
1188 assert(!cir::MissingFeatures::createProfileWeightsForLoop());
1189 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
1190 mlir::Value condVal;
1191 if (s.getCond()) {
1192 // If the for statement declares a condition variable, emit that here.
1193 if (condVar)
1194 emitLoopConditionVariable(d: *condVar, condCleanup&: loopCondScope,
1195 extendedTemporaries: condVarExtendedTemporaries);
1196 // C99 6.8.5p2/p4: The first substatement is executed if the
1197 // expression compares unequal to 0. The condition must be a
1198 // scalar type.
1199 condVal = evaluateExprAsBool(e: s.getCond());
1200 } else {
1201 condVal = cir::ConstantOp::create(builder&: b, location: loc, value: builder.getTrueAttr());
1202 }
1203 builder.createCondition(condition: condVal);
1204 };
1205 auto bodyBuilder = [&](mlir::OpBuilder &b, mlir::Location loc) {
1206 // The scope of the for loop body is nested within the scope of the
1207 // for loop's init-statement and condition.
1208 RunCleanupsScope bodyScope(*this);
1209 if (emitStmt(s: s.getBody(), /*useCurrentScope=*/false).failed())
1210 loopRes = mlir::failure();
1211 emitStopPoint(s: &s);
1212 };
1213 auto stepBuilder = [&](mlir::OpBuilder &b, mlir::Location loc) {
1214 if (s.getInc())
1215 if (emitStmt(s: s.getInc(), /*useCurrentScope=*/true).failed())
1216 loopRes = mlir::failure();
1217 builder.createYield(loc);
1218 };
1219
1220 if (needsCondCleanup) {
1221 cir::CleanupKind cleanupKind = getLangOpts().Exceptions
1222 ? cir::CleanupKind::All
1223 : cir::CleanupKind::Normal;
1224 forOp = builder.createFor(
1225 loc: getLoc(srcLoc: s.getSourceRange()), condBuilder, bodyBuilder, stepBuilder,
1226 /*cleanupBuilder=*/
1227 [&](mlir::OpBuilder &b, mlir::Location loc) {
1228 loopCondScope.emitIntoCleanupRegion(loc);
1229 builder.createYield(loc);
1230 },
1231 cleanupKind);
1232 } else {
1233 forOp = builder.createFor(loc: getLoc(srcLoc: s.getSourceRange()), condBuilder,
1234 bodyBuilder, stepBuilder);
1235 }
1236
1237 setLoopHints(loopOp: forOp, loopHints);
1238 return loopRes;
1239 };
1240
1241 auto res = mlir::success();
1242 auto scopeLoc = getLoc(srcLoc: s.getSourceRange());
1243 cir::ScopeOp::create(builder, location: scopeLoc, /*scopeBuilder=*/
1244 [&](mlir::OpBuilder &b, mlir::Location loc) {
1245 LexicalScope lexScope{*this, loc,
1246 builder.getInsertionBlock()};
1247 res = forStmtBuilder();
1248 });
1249
1250 if (res.failed())
1251 return res;
1252
1253 terminateStructuredRegionBody(r&: forOp.getBody(), loc: getLoc(srcLoc: s.getEndLoc()));
1254 return mlir::success();
1255}
1256
1257mlir::LogicalResult CIRGenFunction::emitDoStmt(const DoStmt &s,
1258 ArrayRef<const Attr *> attrs) {
1259 cir::DoWhileOp doWhileOp;
1260
1261 auto doStmtBuilder = [&]() -> mlir::LogicalResult {
1262 mlir::LogicalResult loopRes = mlir::success();
1263 bool mustProgress =
1264 checkIfLoopMustProgress(controllingExpression: s.getCond(), hasEmptyBody: CodeGenUtils::hasEmptyLoopBody(S: s));
1265 cir::LoopHintAttr loopHints = getLoopHints(attrs, mustProgress);
1266
1267 doWhileOp = builder.createDoWhile(
1268 loc: getLoc(srcLoc: s.getSourceRange()),
1269 /*condBuilder=*/
1270 [&](mlir::OpBuilder &b, mlir::Location loc) {
1271 assert(!cir::MissingFeatures::createProfileWeightsForLoop());
1272 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
1273 // C99 6.8.5p2/p4: The first substatement is executed if the
1274 // expression compares unequal to 0. The condition must be a
1275 // scalar type.
1276 mlir::Value condVal = evaluateExprAsBool(e: s.getCond());
1277 builder.createCondition(condition: condVal);
1278 },
1279 /*bodyBuilder=*/
1280 [&](mlir::OpBuilder &b, mlir::Location loc) {
1281 // The scope of the do-while loop body is a nested scope.
1282 RunCleanupsScope bodyScope(*this);
1283 if (emitStmt(s: s.getBody(), /*useCurrentScope=*/false).failed())
1284 loopRes = mlir::failure();
1285 emitStopPoint(s: &s);
1286 });
1287 setLoopHints(loopOp: doWhileOp, loopHints);
1288 return loopRes;
1289 };
1290
1291 mlir::LogicalResult res = mlir::success();
1292 mlir::Location scopeLoc = getLoc(srcLoc: s.getSourceRange());
1293 cir::ScopeOp::create(builder, location: scopeLoc, /*scopeBuilder=*/
1294 [&](mlir::OpBuilder &b, mlir::Location loc) {
1295 LexicalScope lexScope{*this, loc,
1296 builder.getInsertionBlock()};
1297 res = doStmtBuilder();
1298 });
1299
1300 if (res.failed())
1301 return res;
1302
1303 terminateStructuredRegionBody(r&: doWhileOp.getBody(), loc: getLoc(srcLoc: s.getEndLoc()));
1304 return mlir::success();
1305}
1306
1307mlir::LogicalResult
1308CIRGenFunction::emitWhileStmt(const WhileStmt &s,
1309 ArrayRef<const Attr *> attrs) {
1310 cir::WhileOp whileOp;
1311
1312 auto whileStmtBuilder = [&]() -> mlir::LogicalResult {
1313 mlir::LogicalResult loopRes = mlir::success();
1314 bool mustProgress =
1315 checkIfLoopMustProgress(controllingExpression: s.getCond(), hasEmptyBody: CodeGenUtils::hasEmptyLoopBody(S: s));
1316 cir::LoopHintAttr loopHints = getLoopHints(attrs, mustProgress);
1317
1318 // A condition variable's lifetime is a single iteration, so capture its
1319 // destructor and lifetime-end cleanups, and those of the temporaries that
1320 // its initializer lifetime-extends, and emit them into the loop's
1321 // per-iteration cleanup region.
1322 const VarDecl *condVar = s.getConditionVariable();
1323 SmallVector<QualType, 2> condVarExtendedTemporaries;
1324 if (condVar)
1325 condVarExtendedTemporaries =
1326 getLifetimeExtendedTemporariesWithDestructor(d: *condVar);
1327 bool needsCondCleanup =
1328 condVar &&
1329 (condVar->needsDestruction(Ctx: getContext()) != QualType::DK_none ||
1330 shouldEmitLifetimeMarkersForAutoVar() ||
1331 !condVarExtendedTemporaries.empty());
1332 CapturedCleanups loopCondScope(*this, needsCondCleanup);
1333
1334 auto condBuilder = [&](mlir::OpBuilder &b, mlir::Location loc) {
1335 assert(!cir::MissingFeatures::createProfileWeightsForLoop());
1336 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
1337 // If the while statement declares a condition variable, emit that here.
1338 if (condVar)
1339 emitLoopConditionVariable(d: *condVar, condCleanup&: loopCondScope,
1340 extendedTemporaries: condVarExtendedTemporaries);
1341 // C99 6.8.5p2/p4: The first substatement is executed if the
1342 // expression compares unequal to 0. The condition must be a
1343 // scalar type.
1344 mlir::Value condVal = evaluateExprAsBool(e: s.getCond());
1345 builder.createCondition(condition: condVal);
1346 };
1347 auto bodyBuilder = [&](mlir::OpBuilder &b, mlir::Location loc) {
1348 // The scope of the while loop body is a nested scope.
1349 RunCleanupsScope bodyScope(*this);
1350 if (emitStmt(s: s.getBody(), /*useCurrentScope=*/false).failed())
1351 loopRes = mlir::failure();
1352 emitStopPoint(s: &s);
1353 };
1354
1355 if (needsCondCleanup) {
1356 cir::CleanupKind cleanupKind = getLangOpts().Exceptions
1357 ? cir::CleanupKind::All
1358 : cir::CleanupKind::Normal;
1359 whileOp = builder.createWhile(
1360 loc: getLoc(srcLoc: s.getSourceRange()), condBuilder, bodyBuilder,
1361 /*cleanupBuilder=*/
1362 [&](mlir::OpBuilder &b, mlir::Location loc) {
1363 loopCondScope.emitIntoCleanupRegion(loc);
1364 builder.createYield(loc);
1365 },
1366 cleanupKind);
1367 } else {
1368 whileOp = builder.createWhile(loc: getLoc(srcLoc: s.getSourceRange()), condBuilder,
1369 bodyBuilder);
1370 }
1371 setLoopHints(loopOp: whileOp, loopHints);
1372 return loopRes;
1373 };
1374
1375 mlir::LogicalResult res = mlir::success();
1376 mlir::Location scopeLoc = getLoc(srcLoc: s.getSourceRange());
1377 cir::ScopeOp::create(builder, location: scopeLoc, /*scopeBuilder=*/
1378 [&](mlir::OpBuilder &b, mlir::Location loc) {
1379 LexicalScope lexScope{*this, loc,
1380 builder.getInsertionBlock()};
1381 res = whileStmtBuilder();
1382 });
1383
1384 if (res.failed())
1385 return res;
1386
1387 terminateStructuredRegionBody(r&: whileOp.getBody(), loc: getLoc(srcLoc: s.getEndLoc()));
1388 return mlir::success();
1389}
1390
1391mlir::LogicalResult CIRGenFunction::emitSwitchBody(const Stmt *s) {
1392 // It is rare but legal if the switch body is not a compound stmt. e.g.,
1393 //
1394 // switch(a)
1395 // while(...) {
1396 // case1
1397 // ...
1398 // case2
1399 // ...
1400 // }
1401 if (!isa<CompoundStmt>(Val: s))
1402 return emitStmt(s, /*useCurrentScope=*/true);
1403
1404 auto *compoundStmt = cast<CompoundStmt>(Val: s);
1405
1406 ArrayRef<Stmt *> body{compoundStmt->body_begin(), compoundStmt->body_end()};
1407
1408 mlir::Block *switchBlock = builder.getBlock();
1409
1410 // Any statements appearing before the first case statement are 'unassociated'
1411 // with anything. So we have to create them FIRST in their own block. After
1412 // that, the 'case' regions will take care of future ones.
1413 if (!body.empty() && !isa<SwitchCase>(Val: body.front())) {
1414 builder.setInsertionPointToEnd(switchBlock);
1415 {
1416 // This is needed to handle cleanups in a compound statement before the
1417 // first case statement.
1418 RunCleanupsScope preCaseScope(*this);
1419 while (!body.empty() && !isa<SwitchCase>(Val: body.front())) {
1420
1421 auto *c = body.front();
1422 if (mlir::failed(
1423 Result: emitStmt(s: c, /*useCurrentScope=*/!isa<CompoundStmt>(Val: c))))
1424 return mlir::failure();
1425
1426 body = body.drop_front();
1427 }
1428 }
1429
1430 // Now that we've emitted ALL of the statements, we can create a new block
1431 // for the actual case statements/etc to appear.
1432 mlir::Block *lastBlock = builder.getBlock();
1433 switchBlock = builder.createBlock(parent: switchBlock->getParent());
1434 builder.setInsertionPointToEnd(lastBlock);
1435 cir::BrOp::create(builder, location: getLoc(srcLoc: s->getSourceRange()), dest: switchBlock);
1436 }
1437
1438 for (auto *c : body) {
1439 if (auto *switchCase = dyn_cast<SwitchCase>(Val: c)) {
1440 builder.setInsertionPointToEnd(switchBlock);
1441 // Reset insert point automatically, so that we can attach following
1442 // random stmt to the region of previous built case op to try to make
1443 // the being generated `cir.switch` to be in simple form.
1444 if (mlir::failed(
1445 Result: emitSwitchCase(s: *switchCase, /*buildingTopLevelCase=*/true)))
1446 return mlir::failure();
1447
1448 continue;
1449 }
1450
1451 // Otherwise, just build the statements in the nearest case region.
1452 if (mlir::failed(Result: emitStmt(s: c, /*useCurrentScope=*/!isa<CompoundStmt>(Val: c))))
1453 return mlir::failure();
1454 }
1455
1456 return mlir::success();
1457}
1458
1459mlir::LogicalResult CIRGenFunction::emitSwitchStmt(const clang::SwitchStmt &s) {
1460 // TODO: LLVM codegen does some early optimization to fold the condition and
1461 // only emit live cases. CIR should use MLIR to achieve similar things,
1462 // nothing to be done here.
1463 // if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue))...
1464 assert(!cir::MissingFeatures::constantFoldSwitchStatement());
1465
1466 SwitchOp swop;
1467 auto switchStmtBuilder = [&]() -> mlir::LogicalResult {
1468 if (s.getInit())
1469 if (emitStmt(s: s.getInit(), /*useCurrentScope=*/true).failed())
1470 return mlir::failure();
1471
1472 if (s.getConditionVariable())
1473 emitDecl(d: *s.getConditionVariable(), /*evaluateConditionDecl=*/true);
1474
1475 mlir::Value condV = emitScalarExpr(e: s.getCond());
1476
1477 // Coerce bool values to an i1. There is no real sensible reason we need to
1478 // represent a 'switch' of scoped-enum-with-bool-backing-type specially
1479 // here. It is a rarely used thing, and would result in a lot of work to
1480 // properly handle this everywhere.
1481 if (isa<cir::BoolType>(Val: condV.getType()))
1482 condV = builder.createBoolToInt(src: condV, newTy: builder.getUIntNTy(n: 1));
1483
1484 // TODO: PGO and likelihood (e.g. PGO.haveRegionCounts())
1485 assert(!cir::MissingFeatures::pgoUse());
1486 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
1487 // TODO: if the switch has a condition wrapped by __builtin_unpredictable?
1488 assert(!cir::MissingFeatures::insertBuiltinUnpredictable());
1489
1490 mlir::LogicalResult res = mlir::success();
1491 swop = SwitchOp::create(
1492 builder, location: getLoc(srcLoc: s.getBeginLoc()), condition: condV,
1493 /*switchBuilder=*/
1494 [&](mlir::OpBuilder &b, mlir::Location loc, mlir::OperationState &os) {
1495 curLexScope->setAsSwitch();
1496
1497 condTypeStack.push_back(Elt: condV.getType());
1498
1499 res = emitSwitchBody(s: s.getBody());
1500
1501 condTypeStack.pop_back();
1502 });
1503
1504 return res;
1505 };
1506
1507 // The switch scope contains the full source range for SwitchStmt.
1508 mlir::Location scopeLoc = getLoc(srcLoc: s.getSourceRange());
1509 mlir::LogicalResult res = mlir::success();
1510 cir::ScopeOp::create(builder, location: scopeLoc, /*scopeBuilder=*/
1511 [&](mlir::OpBuilder &b, mlir::Location loc) {
1512 LexicalScope lexScope{*this, loc,
1513 builder.getInsertionBlock()};
1514 res = switchStmtBuilder();
1515 });
1516
1517 llvm::SmallVector<CaseOp> cases;
1518 swop.collectCases(cases);
1519 for (auto caseOp : cases)
1520 terminateStructuredRegionBody(r&: caseOp.getCaseRegion(), loc: caseOp.getLoc());
1521 terminateStructuredRegionBody(r&: swop.getBody(), loc: swop.getLoc());
1522
1523 swop.setAllEnumCasesCovered(s.isAllEnumCasesCovered());
1524
1525 return res;
1526}
1527
1528void CIRGenFunction::emitReturnOfRValue(mlir::Location loc, RValue rv,
1529 QualType ty) {
1530 if (rv.isScalar()) {
1531 builder.createStore(loc, val: rv.getValue(), dst: returnValue);
1532 } else if (rv.isAggregate()) {
1533 Address rvAddr = rv.getAggregateAddress();
1534 // If the aggregate is already in the return slot (e.g. a callee was
1535 // invoked through a ReturnValueSlot bound to returnValue), the copy is
1536 // a no-op. Calling emitAggregateCopy here would also incorrectly
1537 // require the type to have a trivial copy/move.
1538 if (rvAddr.getPointer() != returnValue.getPointer()) {
1539 LValue dest = makeAddrLValue(addr: returnValue, ty);
1540 LValue src = makeAddrLValue(addr: rvAddr, ty);
1541 emitAggregateCopy(dest, src, eltTy: ty, mayOverlap: getOverlapForReturnValue());
1542 }
1543 } else {
1544 assert(rv.isComplex() && "Unknown rvalue kind?");
1545 builder.createStore(loc, val: rv.getComplexValue(), dst: returnValue);
1546 }
1547
1548 // Classic codegen emits a branch through any cleanups before continuing to
1549 // a shared return block. Because CIR handles branching through cleanups
1550 // during the CFG flattening phase, we can just emit the return statement
1551 // directly.
1552 // TODO(cir): Eliminate this redundant load and the store above when we can.
1553 // Load the value from `__retval` and return it via the `cir.return` op.
1554 cir::AllocaOp retAlloca =
1555 mlir::cast<cir::AllocaOp>(Val: fnRetAlloca->getDefiningOp());
1556 auto value = cir::LoadOp::create(builder, location: loc, result: retAlloca.getAllocaType(),
1557 addr: *fnRetAlloca);
1558
1559 cir::ReturnOp::create(builder, location: loc, input: {value});
1560}
1561