clang 24.0.0git
CIRGenStmt.cpp
Go to the documentation of this file.
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"
23#include "clang/AST/StmtSYCL.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.
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>(exprResult)) {
75 if (const auto *ls = dyn_cast<LabelStmt>(exprResult)) {
76 if (cgf.emitLabel(*ls->getDecl()).failed())
77 return mlir::failure();
78 exprResult = ls->getSubStmt();
79 } else if (const auto *as = dyn_cast<AttributedStmt>(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>(exprResult);
89 QualType exprTy = e->getType();
90 if (cgf.hasAggregateEvaluationKind(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, *lastValue, Qualifiers(),
97 /*IsInit*/ false);
98 }
99
100 return mlir::success();
101}
102
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(*this, exprResult, slot, lastValue).failed())
115 result = mlir::failure();
116 } else {
117 if (emitStmt(curStmt, /*useCurrentScope=*/false).failed())
118 result = mlir::failure();
119 }
120 }
121 return result;
122}
123
124mlir::LogicalResult
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>(attr);
138 break;
139 case attr::NoMerge:
140 case attr::NoConvergent:
141 case attr::AMDGPUAvailableVisible:
142 case attr::HLSLControlFlowHint:
143 cgm.errorNYI(s.getSourceRange(),
144 "Unimplemented statement attribute: ", 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>(sub);
157 musttail = cast<CallExpr>(ret->getRetValue()->IgnoreParens());
158 break;
159 }
160 case attr::CXXAssume: {
161 const Expr *assumptionExpr = cast<CXXAssumeAttr>(attr)->getAssumption();
162 if (getLangOpts().CXXAssumptions && builder.getInsertionBlock() &&
163 !assumptionExpr->HasSideEffects(getContext())) {
164 mlir::Value assumptionValue = emitCheckedArgForAssume(assumptionExpr);
165 cir::AssumeOp::create(builder, getLoc(s.getSourceRange()),
166 assumptionValue, cir::AssumeBundleKind::None,
167 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(savedAtomicOpts, atomicAttr));
185
186 mlir::LogicalResult result =
187 emitStmt(s.getSubStmt(), /*useCurrentScope=*/false, s.getAttrs());
188
189 cgm.setAtomicOpts(savedAtomicOpts);
190 return result;
191}
192
194 Address *lastValue,
195 AggValueSlot slot) {
196 // Add local scope to track new declared variables.
198 mlir::Location scopeLoc = getLoc(s.getSourceRange());
199 mlir::OpBuilder::InsertPoint scopeInsPt;
200 cir::ScopeOp::create(
201 builder, scopeLoc,
202 [&](mlir::OpBuilder &b, mlir::Type &type, mlir::Location loc) {
203 scopeInsPt = b.saveInsertionPoint();
204 });
205 mlir::OpBuilder::InsertionGuard guard(builder);
206 builder.restoreInsertionPoint(scopeInsPt);
207 LexicalScope lexScope(*this, scopeLoc, builder.getInsertionBlock());
208 return emitCompoundStmtWithoutScope(s, lastValue, slot);
209}
210
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,
220 if (mlir::succeeded(emitSimpleStmt(s, useCurrentScope)))
221 return mlir::success();
222
223 switch (s->getStmtClass()) {
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
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(cast<IfStmt>(*s));
257 case Stmt::SwitchStmtClass:
259 case Stmt::ForStmtClass:
260 return emitForStmt(cast<ForStmt>(*s), attr);
261 case Stmt::WhileStmtClass:
263 case Stmt::DoStmtClass:
264 return emitDoStmt(cast<DoStmt>(*s), attr);
265 case Stmt::CXXTryStmtClass:
267 case Stmt::CXXForRangeStmtClass:
269 case Stmt::CoroutineBodyStmtClass:
271 case Stmt::IndirectGotoStmtClass:
273 case Stmt::CoreturnStmtClass:
275 case Stmt::SYCLKernelCallStmtClass:
277 case Stmt::OpenACCComputeConstructClass:
279 case Stmt::OpenACCLoopConstructClass:
281 case Stmt::OpenACCCombinedConstructClass:
283 case Stmt::OpenACCDataConstructClass:
285 case Stmt::OpenACCEnterDataConstructClass:
287 case Stmt::OpenACCExitDataConstructClass:
289 case Stmt::OpenACCHostDataConstructClass:
291 case Stmt::OpenACCWaitConstructClass:
293 case Stmt::OpenACCInitConstructClass:
295 case Stmt::OpenACCShutdownConstructClass:
297 case Stmt::OpenACCSetConstructClass:
299 case Stmt::OpenACCUpdateConstructClass:
301 case Stmt::OpenACCCacheConstructClass:
303 case Stmt::OpenACCAtomicConstructClass:
305 case Stmt::GCCAsmStmtClass:
306 case Stmt::MSAsmStmtClass:
307 return emitAsmStmt(cast<AsmStmt>(*s));
308 case Stmt::OMPScopeDirectiveClass:
310 case Stmt::OMPErrorDirectiveClass:
312 case Stmt::OMPParallelDirectiveClass:
314 case Stmt::OMPTaskwaitDirectiveClass:
316 case Stmt::OMPTaskyieldDirectiveClass:
318 case Stmt::OMPBarrierDirectiveClass:
320 case Stmt::OMPMetaDirectiveClass:
322 case Stmt::OMPCanonicalLoopClass:
324 case Stmt::OMPSimdDirectiveClass:
326 case Stmt::OMPTileDirectiveClass:
328 case Stmt::OMPUnrollDirectiveClass:
330 case Stmt::OMPFuseDirectiveClass:
332 case Stmt::OMPForDirectiveClass:
334 case Stmt::OMPForSimdDirectiveClass:
336 case Stmt::OMPSectionsDirectiveClass:
338 case Stmt::OMPSectionDirectiveClass:
340 case Stmt::OMPSingleDirectiveClass:
342 case Stmt::OMPMasterDirectiveClass:
344 case Stmt::OMPCriticalDirectiveClass:
346 case Stmt::OMPParallelForDirectiveClass:
348 case Stmt::OMPParallelForSimdDirectiveClass:
351 case Stmt::OMPParallelMasterDirectiveClass:
353 case Stmt::OMPParallelSectionsDirectiveClass:
356 case Stmt::OMPTaskDirectiveClass:
358 case Stmt::OMPTaskgroupDirectiveClass:
360 case Stmt::OMPFlushDirectiveClass:
362 case Stmt::OMPDepobjDirectiveClass:
364 case Stmt::OMPScanDirectiveClass:
366 case Stmt::OMPOrderedStandaloneDirectiveClass:
369 case Stmt::OMPOrderedBlockAssocDirectiveClass:
372 case Stmt::OMPAtomicDirectiveClass:
374 case Stmt::OMPTargetDirectiveClass:
376 case Stmt::OMPTeamsDirectiveClass:
378 case Stmt::OMPCancellationPointDirectiveClass:
381 case Stmt::OMPCancelDirectiveClass:
383 case Stmt::OMPTargetDataDirectiveClass:
385 case Stmt::OMPTargetEnterDataDirectiveClass:
388 case Stmt::OMPTargetExitDataDirectiveClass:
390 case Stmt::OMPTargetParallelDirectiveClass:
392 case Stmt::OMPTargetParallelForDirectiveClass:
395 case Stmt::OMPTaskLoopDirectiveClass:
397 case Stmt::OMPTaskLoopSimdDirectiveClass:
399 case Stmt::OMPMaskedTaskLoopDirectiveClass:
401 case Stmt::OMPMaskedTaskLoopSimdDirectiveClass:
404 case Stmt::OMPMasterTaskLoopDirectiveClass:
406 case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
409 case Stmt::OMPParallelGenericLoopDirectiveClass:
412 case Stmt::OMPParallelMaskedDirectiveClass:
414 case Stmt::OMPParallelMaskedTaskLoopDirectiveClass:
417 case Stmt::OMPParallelMaskedTaskLoopSimdDirectiveClass:
420 case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
423 case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
426 case Stmt::OMPDistributeDirectiveClass:
428 case Stmt::OMPDistributeParallelForDirectiveClass:
431 case Stmt::OMPDistributeParallelForSimdDirectiveClass:
434 case Stmt::OMPDistributeSimdDirectiveClass:
436 case Stmt::OMPTargetParallelGenericLoopDirectiveClass:
439 case Stmt::OMPTargetGenericLoopDirectiveClass:
442 case Stmt::OMPTargetParallelForSimdDirectiveClass:
445 case Stmt::OMPTargetSimdDirectiveClass:
447 case Stmt::OMPTargetTeamsGenericLoopDirectiveClass:
450 case Stmt::OMPTargetUpdateDirectiveClass:
452 case Stmt::OMPTeamsDistributeDirectiveClass:
455 case Stmt::OMPTeamsDistributeSimdDirectiveClass:
458 case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
461 case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
464 case Stmt::OMPTeamsGenericLoopDirectiveClass:
467 case Stmt::OMPTargetTeamsDirectiveClass:
469 case Stmt::OMPTargetTeamsDistributeDirectiveClass:
472 case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
475 case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
478 case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
481 case Stmt::OMPInteropDirectiveClass:
483 case Stmt::OMPDispatchDirectiveClass:
485 case Stmt::OMPGenericLoopDirectiveClass:
487 case Stmt::OMPReverseDirectiveClass:
489 case Stmt::OMPSplitDirectiveClass:
491 case Stmt::OMPInterchangeDirectiveClass:
493 case Stmt::OMPFlattenDirectiveClass:
495 case Stmt::OMPAssumeDirectiveClass:
497 case Stmt::OMPMaskedDirectiveClass:
499 case Stmt::OMPStripeDirectiveClass:
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(cast<DeclStmt>(*s));
535 case Stmt::CompoundStmtClass:
536 if (useCurrentScope)
539 case Stmt::GotoStmtClass:
540 return emitGotoStmt(cast<GotoStmt>(*s));
541 case Stmt::ContinueStmtClass:
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(cast<LabelStmt>(*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.
554 /*buildingTopLevelCase=*/false);
555 break;
556
557 case Stmt::BreakStmtClass:
558 return emitBreakStmt(cast<BreakStmt>(*s));
559 case Stmt::ReturnStmtClass:
561 case Stmt::AttributedStmtClass:
563 }
564
565 return mlir::success();
566}
567
568mlir::LogicalResult CIRGenFunction::emitLabelStmt(const clang::LabelStmt &s) {
569
570 if (emitLabel(*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.getSubStmt(), /*useCurrentScope=*/false);
577}
578
579// Add a terminating yield on a body region if no other terminators are used.
581 mlir::Location loc) {
582 if (r.empty())
583 return;
584
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(&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(constevalExecuted, /*useCurrentScope=*/true);
624
625 if (s.getInit())
626 if (emitStmt(s.getInit(), /*useCurrentScope=*/true).failed())
627 return mlir::failure();
628
629 if (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(s.getCond(), condConstant, 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(executed, 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(skipped)) {
645 if (executed)
646 return emitStmt(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
655 return emitIfOnBoolExpr(s.getCond(), s.getThen(), 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(s.getSourceRange());
662 cir::ScopeOp::create(builder, 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(*i, /*evaluateConditionDecl=*/true);
677
678 return mlir::success();
679}
680
681mlir::LogicalResult CIRGenFunction::emitReturnStmt(const ReturnStmt &s) {
682 mlir::Location loc = getLoc(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>(rv)) {
688 rv = ewc->getSubExpr();
689 createNewScope = true;
690 }
691
692 auto handleReturnVal = [&]() {
693 if (getContext().getLangOpts().ElideConstructors && s.getNRVOCandidate() &&
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, true, 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(rv);
711 }
712 } else if (cast<FunctionDecl>(curGD.getDecl())
713 ->getReturnType()
714 ->isReferenceType()) {
715 // If this function returns a reference, take the address of the
716 // expression rather than the value.
718 builder.CIRBaseBuilderTy::createStore(loc, result.getValue(),
719 *fnRetAlloca);
720 } else {
721 mlir::Value value = nullptr;
723 case cir::TEK_Scalar:
724 value = emitScalarExpr(rv);
725 if (value) { // Change this to an assert once emitScalarExpr is complete
726 builder.CIRBaseBuilderTy::createStore(loc, value, *fnRetAlloca);
727 }
728 break;
729 case cir::TEK_Complex:
732 /*isInit=*/true);
733 break;
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 the return slot and return it via `cir.return`.
761 cir::AllocaOp retAlloca =
762 mlir::cast<cir::AllocaOp>(fnRetAlloca->getDefiningOp());
763 auto value = cir::LoadOp::create(builder, loc, retAlloca.getAllocaType(),
764 *fnRetAlloca);
765
766 cir::ReturnOp::create(builder, loc, {value});
767 } else {
768 cir::ReturnOp::create(builder, 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(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.
784
785 cir::GotoOp::create(builder, getLoc(s.getSourceRange()),
786 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(builder.getBlock()->getParent());
792
793 return mlir::success();
794}
795
796mlir::LogicalResult
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(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, getLoc(s.getSourceRange()), val);
810
811 // The indirect goto ends the block; open a fresh one so codegen can resume.
812 builder.createBlock(builder.getBlock()->getParent());
813 return mlir::success();
814}
815
816mlir::LogicalResult
818 builder.createContinue(getLoc(s.getKwLoc()));
819
820 // Insert the new block to continue codegen after the continue statement.
821 builder.createBlock(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(builder.getBlock()->getParent());
837 }
838 cir::BrOp::create(builder, getLoc(d.getSourceRange()), labelBlock);
839 }
840
841 builder.setInsertionPointToEnd(labelBlock);
842 cir::LabelOp::create(builder, getLoc(d.getSourceRange()), 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
850 return mlir::success();
851}
852
853mlir::LogicalResult CIRGenFunction::emitBreakStmt(const clang::BreakStmt &s) {
854 builder.createBreak(getLoc(s.getKwLoc()));
855
856 // Insert the new block to continue codegen after the break statement.
857 builder.createBlock(builder.getBlock()->getParent());
858
859 return mlir::success();
860}
861
862template <typename T>
863mlir::LogicalResult
865 mlir::ArrayAttr value, CaseOpKind kind,
866 bool buildingTopLevelCase) {
867
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, loc, value, kind, insertPoint);
881
882 {
883 mlir::OpBuilder::InsertionGuard guardSwitch(builder);
884 builder.restoreInsertionPoint(insertPoint);
885
886 if (isa<DefaultStmt>(sub) && isa<CaseStmt>(stmt)) {
887 subStmtKind = SubStmtKind::Default;
888 builder.createYield(loc);
889 } else if (isa<CaseStmt>(sub) && isa<DefaultStmt, CaseStmt>(stmt)) {
890 subStmtKind = SubStmtKind::Case;
891 builder.createYield(loc);
892 } else {
893 result = emitStmt(sub, /*useCurrentScope=*/!isa<CompoundStmt>(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(*cast<CaseStmt>(sub), condType, buildingTopLevelCase);
933 } else if (subStmtKind == SubStmtKind::Default) {
934 result = emitDefaultStmt(*cast<DefaultStmt>(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(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(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>(condType))
956 condType = builder.getUIntNTy(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(getContext());
963 value = builder.getArrayAttr({cir::IntAttr::get(condType, intVal),
964 cir::IntAttr::get(condType, endVal)});
965 kind = cir::CaseOpKind::Range;
966 } else {
967 value = builder.getArrayAttr({cir::IntAttr::get(condType, intVal)});
968 kind = cir::CaseOpKind::Equal;
969 }
970
971 return emitCaseDefaultCascade(&s, condType, value, kind,
972 buildingTopLevelCase);
973}
974
976 mlir::Type condType,
977 bool buildingTopLevelCase) {
978 return emitCaseDefaultCascade(&s, condType, builder.getArrayAttr({}),
979 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(cast<CaseStmt>(s), condTypeStack.back(),
989 buildingTopLevelCase);
990
991 if (s.getStmtClass() == Stmt::DefaultStmtClass)
993 buildingTopLevelCase);
994
995 llvm_unreachable("expect case or default stmt");
996}
997
999 bool mustProgress) {
1000 // Determine the state of the hints, the same way classic codegen does.
1003 cgm.getCodeGenOpts(), attrs, mustProgress);
1004
1005 // Memory accesses in a `parallel` loop are tagged as parallel accesses when
1006 // CIR is lowered.
1008 mlir::MLIRContext *ctx = &getMLIRContext();
1009
1010 auto convertFlag = [&](bool set) {
1011 return set ? mlir::UnitAttr::get(ctx) : mlir::UnitAttr();
1012 };
1013 auto convertStateToBool =
1015 switch (state) {
1017 return mlir::BoolAttr();
1019 return mlir::BoolAttr::get(ctx, true);
1021 return mlir::BoolAttr::get(ctx, false);
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) {
1032 return std::nullopt;
1034 return cir::LoopUnrollMode::Enable;
1036 return cir::LoopUnrollMode::Disable;
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 ==
1049 loopAttrs.VectorizeEnable ==
1051 cgm.errorNYI(SourceLocation{}, "unsupported vector-enable loop state");
1052
1053 auto hints = cir::LoopHintAttr::get(
1054 ctx, convertFlag(loopAttrs.MustProgress),
1055 convertStateToBool(loopAttrs.VectorizeEnable), loopAttrs.VectorizeWidth,
1056 convertStateToBool(loopAttrs.VectorizeScalable),
1057 convertStateToBool(loopAttrs.VectorizePredicateEnable),
1058 loopAttrs.InterleaveCount, convertUnrollMode(loopAttrs.UnrollEnable),
1059 loopAttrs.UnrollCount, convertUnrollMode(loopAttrs.UnrollAndJamEnable),
1060 loopAttrs.UnrollAndJamCount,
1061 convertStateToBool(loopAttrs.DistributeEnable),
1062 convertFlag(loopAttrs.PipelineDisabled),
1063 loopAttrs.PipelineInitiationInterval, convertFlag(loopAttrs.LICMDisabled),
1064 convertFlag(loopAttrs.IsParallel), 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(mlir::UnitAttr::get(loopOp->getContext())));
1078}
1079
1080mlir::LogicalResult
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.getInit(), /*useCurrentScope=*/true).failed())
1090 return mlir::failure();
1091 if (emitStmt(s.getRangeStmt(), /*useCurrentScope=*/true).failed())
1092 return mlir::failure();
1093 if (emitStmt(s.getBeginStmt(), /*useCurrentScope=*/true).failed())
1094 return mlir::failure();
1095 if (emitStmt(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(forAttrs, /*mustProgress=*/false);
1102
1103 forOp = builder.createFor(
1105 /*condBuilder=*/
1106 [&](mlir::OpBuilder &b, mlir::Location loc) {
1107 assert(!cir::MissingFeatures::createProfileWeightsForLoop());
1108 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
1109 mlir::Value condVal = evaluateExprAsBool(s.getCond());
1110 builder.createCondition(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.getLoopVarStmt(), useCurrentScope).failed())
1120 loopRes = mlir::failure();
1121 if (emitStmt(s.getBody(), useCurrentScope).failed())
1122 loopRes = mlir::failure();
1123 emitStopPoint(&s);
1124 },
1125 /*stepBuilder=*/
1126 [&](mlir::OpBuilder &b, mlir::Location loc) {
1127 if (s.getInc())
1128 if (emitStmt(s.getInc(), /*useCurrentScope=*/true).failed())
1129 loopRes = mlir::failure();
1130 builder.createYield(loc);
1131 });
1132 setLoopHints(forOp, loopHints);
1133 return loopRes;
1134 };
1135
1136 mlir::LogicalResult res = mlir::success();
1137 mlir::Location scopeLoc = getLoc(s.getSourceRange());
1138 cir::ScopeOp::create(builder, 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(forOp.getBody(), getLoc(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.getInit(), /*useCurrentScope=*/true).failed())
1165 return mlir::failure();
1166 bool mustProgress =
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 =
1180 bool needsCondCleanup =
1181 condVar &&
1183 shouldEmitLifetimeMarkersForAutoVar() ||
1184 !condVarExtendedTemporaries.empty());
1185 CapturedCleanups loopCondScope(*this, needsCondCleanup);
1186
1187 auto condBuilder = [&](mlir::OpBuilder &b, mlir::Location loc) {
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(*condVar, loopCondScope,
1195 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(s.getCond());
1200 } else {
1201 condVal = cir::ConstantOp::create(b, loc, builder.getTrueAttr());
1202 }
1203 builder.createCondition(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.getBody(), /*useCurrentScope=*/false).failed())
1210 loopRes = mlir::failure();
1211 emitStopPoint(&s);
1212 };
1213 auto stepBuilder = [&](mlir::OpBuilder &b, mlir::Location loc) {
1214 if (s.getInc())
1215 if (emitStmt(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 getLoc(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(getLoc(s.getSourceRange()), condBuilder,
1234 bodyBuilder, stepBuilder);
1235 }
1236
1237 setLoopHints(forOp, loopHints);
1238 return loopRes;
1239 };
1240
1241 auto res = mlir::success();
1242 auto scopeLoc = getLoc(s.getSourceRange());
1243 cir::ScopeOp::create(builder, 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(forOp.getBody(), getLoc(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 =
1265 cir::LoopHintAttr loopHints = getLoopHints(attrs, mustProgress);
1266
1267 doWhileOp = builder.createDoWhile(
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(s.getCond());
1277 builder.createCondition(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.getBody(), /*useCurrentScope=*/false).failed())
1284 loopRes = mlir::failure();
1285 emitStopPoint(&s);
1286 });
1287 setLoopHints(doWhileOp, loopHints);
1288 return loopRes;
1289 };
1290
1291 mlir::LogicalResult res = mlir::success();
1292 mlir::Location scopeLoc = getLoc(s.getSourceRange());
1293 cir::ScopeOp::create(builder, 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(doWhileOp.getBody(), getLoc(s.getEndLoc()));
1304 return mlir::success();
1305}
1306
1307mlir::LogicalResult
1309 ArrayRef<const Attr *> attrs) {
1310 cir::WhileOp whileOp;
1311
1312 auto whileStmtBuilder = [&]() -> mlir::LogicalResult {
1313 mlir::LogicalResult loopRes = mlir::success();
1314 bool mustProgress =
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 =
1327 bool needsCondCleanup =
1328 condVar &&
1330 shouldEmitLifetimeMarkersForAutoVar() ||
1331 !condVarExtendedTemporaries.empty());
1332 CapturedCleanups loopCondScope(*this, needsCondCleanup);
1333
1334 auto condBuilder = [&](mlir::OpBuilder &b, mlir::Location loc) {
1337 // If the while statement declares a condition variable, emit that here.
1338 if (condVar)
1339 emitLoopConditionVariable(*condVar, loopCondScope,
1340 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(s.getCond());
1345 builder.createCondition(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.getBody(), /*useCurrentScope=*/false).failed())
1351 loopRes = mlir::failure();
1352 emitStopPoint(&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 getLoc(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(getLoc(s.getSourceRange()), condBuilder,
1369 bodyBuilder);
1370 }
1371 setLoopHints(whileOp, loopHints);
1372 return loopRes;
1373 };
1374
1375 mlir::LogicalResult res = mlir::success();
1376 mlir::Location scopeLoc = getLoc(s.getSourceRange());
1377 cir::ScopeOp::create(builder, 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(whileOp.getBody(), getLoc(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>(s))
1402 return emitStmt(s, /*useCurrentScope=*/true);
1403
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>(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>(body.front())) {
1420
1421 auto *c = body.front();
1422 if (mlir::failed(
1423 emitStmt(c, /*useCurrentScope=*/!isa<CompoundStmt>(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(switchBlock->getParent());
1434 builder.setInsertionPointToEnd(lastBlock);
1435 cir::BrOp::create(builder, getLoc(s->getSourceRange()), switchBlock);
1436 }
1437
1438 for (auto *c : body) {
1439 if (auto *switchCase = dyn_cast<SwitchCase>(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 emitSwitchCase(*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(emitStmt(c, /*useCurrentScope=*/!isa<CompoundStmt>(c))))
1453 return mlir::failure();
1454 }
1455
1456 return mlir::success();
1457}
1458
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))...
1465
1466 SwitchOp swop;
1467 auto switchStmtBuilder = [&]() -> mlir::LogicalResult {
1468 if (s.getInit())
1469 if (emitStmt(s.getInit(), /*useCurrentScope=*/true).failed())
1470 return mlir::failure();
1471
1472 if (s.getConditionVariable())
1473 emitDecl(*s.getConditionVariable(), /*evaluateConditionDecl=*/true);
1474
1475 mlir::Value condV = emitScalarExpr(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>(condV.getType()))
1482 condV = builder.createBoolToInt(condV, builder.getUIntNTy(1));
1483
1484 // TODO: PGO and likelihood (e.g. PGO.haveRegionCounts())
1487 // TODO: if the switch has a condition wrapped by __builtin_unpredictable?
1489
1490 mlir::LogicalResult res = mlir::success();
1491 swop = SwitchOp::create(
1492 builder, getLoc(s.getBeginLoc()), condV,
1493 /*switchBuilder=*/
1494 [&](mlir::OpBuilder &b, mlir::Location loc, mlir::OperationState &os) {
1495 curLexScope->setAsSwitch();
1496
1497 condTypeStack.push_back(condV.getType());
1498
1499 res = emitSwitchBody(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(s.getSourceRange());
1509 mlir::LogicalResult res = mlir::success();
1510 cir::ScopeOp::create(builder, scopeLoc, /*scopeBuilder=*/
1511 [&](mlir::OpBuilder &b, mlir::Location loc) {
1512 LexicalScope lexScope{*this, loc,
1513 builder.getInsertionBlock()};
1514 res = switchStmtBuilder();
1515 });
1516
1518 swop.collectCases(cases);
1519 for (auto caseOp : cases)
1520 terminateStructuredRegionBody(caseOp.getCaseRegion(), caseOp.getLoc());
1521 terminateStructuredRegionBody(swop.getBody(), 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, rv.getValue(), 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(returnValue, ty);
1540 LValue src = makeAddrLValue(rvAddr, ty);
1542 }
1543 } else {
1544 assert(rv.isComplex() && "Unknown rvalue kind?");
1545 builder.createStore(loc, rv.getComplexValue(), 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 the return slot and return it via `cir.return`.
1554 cir::AllocaOp retAlloca =
1555 mlir::cast<cir::AllocaOp>(fnRetAlloca->getDefiningOp());
1556 auto value = cir::LoadOp::create(builder, loc, retAlloca.getAllocaType(),
1557 *fnRetAlloca);
1558
1559 cir::ReturnOp::create(builder, loc, {value});
1560}
static mlir::LogicalResult emitStmtWithResult(CIRGenFunction &cgf, const Stmt *exprResult, AggValueSlot slot, Address *lastValue)
static void setLoopHints(cir::LoopOpInterface loopOp, cir::LoopHintAttr loopHints)
static clang::AtomicOptions getAdjustedAtomicOptions(clang::AtomicOptions ao, const AtomicAttr *aa)
Compute the atomic options that apply for the extent of a clang::atomic attributed statement,...
Defines the clang::Expr interface and subclasses for C++ expressions.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
This file defines OpenACC AST classes for statement-level contructs.
This file defines OpenMP AST classes for executable directives and clauses.
This file defines SYCL AST classes used to represent calls to SYCL kernels.
Attr - This represents one attribute.
Definition Attr.h:47
Represents an attribute applied to a statement.
Definition Stmt.h:2214
Stmt * getSubStmt()
Definition Stmt.h:2250
ArrayRef< const Attr * > getAttrs() const
Definition Stmt.h:2246
BreakStmt - This represents a break.
Definition Stmt.h:3148
mlir::Value getPointer() const
Definition Address.h:98
An aggregate value slot.
static AggValueSlot forAddr(Address addr, clang::Qualifiers quals, IsDestructed_t isDestructed, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed)
Captures cleanups for variables whose lifetime ends in a region owned by an enclosing op rather than ...
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited.
void forceCleanup(ArrayRef< mlir::Value * > valuesToReload={})
Force the emission of cleanups now, instead of waiting until this object is destroyed.
mlir::LogicalResult emitOMPTargetParallelForDirective(const OMPTargetParallelForDirective &s)
mlir::LogicalResult emitOMPParallelMasterTaskLoopSimdDirective(const OMPParallelMasterTaskLoopSimdDirective &s)
mlir::LogicalResult emitOMPSimdDirective(const OMPSimdDirective &s)
mlir::Value emitCheckedArgForAssume(const Expr *e)
Emits an argument for a call to a __builtin_assume.
mlir::LogicalResult emitOMPCriticalDirective(const OMPCriticalDirective &s)
static cir::TypeEvaluationKind getEvaluationKind(clang::QualType type)
Return the cir::TypeEvaluationKind of QualType type.
clang::GlobalDecl curGD
The GlobalDecl for the current function being compiled or the global variable currently being initial...
mlir::LogicalResult emitCoreturnStmt(const CoreturnStmt &s)
mlir::LogicalResult emitOpenACCDataConstruct(const OpenACCDataConstruct &s)
EHScopeStack::stable_iterator prologueCleanupDepth
The cleanup depth enclosing all the cleanups associated with the parameters.
mlir::LogicalResult emitOpenACCCombinedConstruct(const OpenACCCombinedConstruct &s)
mlir::LogicalResult emitOMPParallelMasterDirective(const OMPParallelMasterDirective &s)
mlir::LogicalResult emitOpenACCWaitConstruct(const OpenACCWaitConstruct &s)
mlir::LogicalResult emitOMPCancellationPointDirective(const OMPCancellationPointDirective &s)
mlir::LogicalResult emitOMPParallelMaskedTaskLoopDirective(const OMPParallelMaskedTaskLoopDirective &s)
mlir::LogicalResult emitOMPReverseDirective(const OMPReverseDirective &s)
const clang::LangOptions & getLangOpts() const
mlir::LogicalResult emitOpenACCUpdateConstruct(const OpenACCUpdateConstruct &s)
mlir::LogicalResult emitOMPTileDirective(const OMPTileDirective &s)
mlir::LogicalResult emitIfOnBoolExpr(const clang::Expr *cond, const clang::Stmt *thenS, const clang::Stmt *elseS)
Emit an if on a boolean condition to the specified blocks.
mlir::LogicalResult emitOMPTargetTeamsDirective(const OMPTargetTeamsDirective &s)
mlir::LogicalResult emitOMPTeamsDistributeParallelForDirective(const OMPTeamsDistributeParallelForDirective &s)
mlir::LogicalResult emitOMPBarrierDirective(const OMPBarrierDirective &s)
mlir::LogicalResult emitOMPTargetParallelDirective(const OMPTargetParallelDirective &s)
mlir::LogicalResult emitOpenACCCacheConstruct(const OpenACCCacheConstruct &s)
mlir::LogicalResult emitOMPTargetDirective(const OMPTargetDirective &s)
mlir::LogicalResult emitCXXForRangeStmt(const CXXForRangeStmt &s, llvm::ArrayRef< const Attr * > attrs)
mlir::Value evaluateExprAsBool(const clang::Expr *e)
Perform the usual unary conversions on the specified expression and compare the result against zero,...
void emitAggregateCopy(LValue dest, LValue src, QualType eltTy, AggValueSlot::Overlap_t mayOverlap, bool isVolatile=false)
Emit an aggregate copy.
mlir::LogicalResult emitForStmt(const clang::ForStmt &s, llvm::ArrayRef< const Attr * > attrs={})
mlir::LogicalResult emitOMPScopeDirective(const OMPScopeDirective &s)
mlir::Location getLoc(clang::SourceLocation srcLoc)
Helpers to convert Clang's SourceLocation to a MLIR Location.
mlir::LogicalResult emitOMPDepobjDirective(const OMPDepobjDirective &s)
bool constantFoldsToBool(const clang::Expr *cond, bool &resultBool, bool allowLabels=false)
If the specified expression does not fold to a constant, or if it does but contains a label,...
mlir::LogicalResult emitDoStmt(const clang::DoStmt &s, llvm::ArrayRef< const Attr * > attrs={})
mlir::LogicalResult emitReturnStmt(const clang::ReturnStmt &s)
mlir::LogicalResult emitOpenACCInitConstruct(const OpenACCInitConstruct &s)
void emitAnyExprToMem(const Expr *e, Address location, Qualifiers quals, bool isInitializer)
Emits the code necessary to evaluate an arbitrary expression into the given memory location.
mlir::LogicalResult emitOMPDistributeParallelForSimdDirective(const OMPDistributeParallelForSimdDirective &s)
void emitLoopConditionVariable(const clang::VarDecl &d, CapturedCleanups &condCleanup, llvm::ArrayRef< QualType > extendedTemporaries)
Emit a loop's condition-variable declaration.
mlir::LogicalResult emitOMPUnrollDirective(const OMPUnrollDirective &s)
mlir::LogicalResult emitOMPTaskDirective(const OMPTaskDirective &s)
mlir::LogicalResult emitOpenACCSetConstruct(const OpenACCSetConstruct &s)
RValue emitReferenceBindingToExpr(const Expr *e)
Emits a reference binding to the passed in expression.
mlir::LogicalResult emitOMPTeamsGenericLoopDirective(const OMPTeamsGenericLoopDirective &s)
mlir::LogicalResult emitOMPCanonicalLoop(const OMPCanonicalLoop &s)
mlir::LogicalResult emitSwitchStmt(const clang::SwitchStmt &s)
mlir::LogicalResult emitOMPTeamsDirective(const OMPTeamsDirective &s)
mlir::LogicalResult emitWhileStmt(const clang::WhileStmt &s, llvm::ArrayRef< const Attr * > attrs={})
mlir::LogicalResult emitCaseStmt(const clang::CaseStmt &s, mlir::Type condType, bool buildingTopLevelCase)
llvm::ScopedHashTableScope< const clang::Decl *, mlir::Value > SymTableScopeTy
mlir::LogicalResult emitOMPMaskedTaskLoopDirective(const OMPMaskedTaskLoopDirective &s)
mlir::LogicalResult emitOMPFuseDirective(const OMPFuseDirective &s)
mlir::LogicalResult emitSimpleStmt(const clang::Stmt *s, bool useCurrentScope)
mlir::LogicalResult emitOMPSectionDirective(const OMPSectionDirective &s)
mlir::LogicalResult emitAsmStmt(const clang::AsmStmt &s)
mlir::LogicalResult emitOMPFlattenDirective(const OMPFlattenDirective &s)
mlir::LogicalResult emitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &s)
mlir::LogicalResult emitOMPDistributeParallelForDirective(const OMPDistributeParallelForDirective &s)
mlir::LogicalResult emitOMPOrderedStandaloneDirective(const OMPOrderedStandaloneDirective &s)
mlir::LogicalResult emitOpenACCComputeConstruct(const OpenACCComputeConstruct &s)
mlir::LogicalResult emitOMPMasterTaskLoopSimdDirective(const OMPMasterTaskLoopSimdDirective &s)
EHScopeStack ehStack
Tracks function scope overall cleanup handling.
mlir::LogicalResult emitSwitchBody(const clang::Stmt *s)
mlir::LogicalResult emitOMPTaskwaitDirective(const OMPTaskwaitDirective &s)
mlir::LogicalResult emitOMPFlushDirective(const OMPFlushDirective &s)
mlir::LogicalResult emitOMPGenericLoopDirective(const OMPGenericLoopDirective &s)
mlir::LogicalResult emitOMPTargetUpdateDirective(const OMPTargetUpdateDirective &s)
std::optional< mlir::Value > fnRetAlloca
The compiler-generated variable that holds the return value.
mlir::LogicalResult emitOMPTargetGenericLoopDirective(const OMPTargetGenericLoopDirective &s)
mlir::LogicalResult emitOMPTargetParallelForSimdDirective(const OMPTargetParallelForSimdDirective &s)
mlir::LogicalResult emitOMPInterchangeDirective(const OMPInterchangeDirective &s)
mlir::LogicalResult emitOMPDispatchDirective(const OMPDispatchDirective &s)
mlir::LogicalResult emitOMPParallelDirective(const OMPParallelDirective &s)
mlir::LogicalResult emitCXXTryStmt(const clang::CXXTryStmt &s, cxxTryBodyEmitter &bodyCallback, bool isFnTryBlock=false)
mlir::LogicalResult emitAttributedStmt(const AttributedStmt &s)
mlir::LogicalResult emitOMPForSimdDirective(const OMPForSimdDirective &s)
mlir::LogicalResult emitOMPTaskLoopDirective(const OMPTaskLoopDirective &s)
Address returnValue
The temporary alloca to hold the return value.
mlir::LogicalResult emitOMPTargetDataDirective(const OMPTargetDataDirective &s)
mlir::LogicalResult emitLabel(const clang::LabelDecl &d)
mlir::LogicalResult emitOMPTargetParallelGenericLoopDirective(const OMPTargetParallelGenericLoopDirective &s)
static bool hasAggregateEvaluationKind(clang::QualType type)
mlir::LogicalResult emitOMPParallelMaskedDirective(const OMPParallelMaskedDirective &s)
mlir::LogicalResult emitOMPMaskedTaskLoopSimdDirective(const OMPMaskedTaskLoopSimdDirective &s)
mlir::LogicalResult emitOMPAtomicDirective(const OMPAtomicDirective &s)
mlir::LogicalResult emitOpenACCShutdownConstruct(const OpenACCShutdownConstruct &s)
mlir::LogicalResult emitBreakStmt(const clang::BreakStmt &s)
mlir::LogicalResult emitIndirectGotoStmt(const IndirectGotoStmt &s)
mlir::LogicalResult emitOMPTeamsDistributeParallelForSimdDirective(const OMPTeamsDistributeParallelForSimdDirective &s)
mlir::LogicalResult emitOMPTaskgroupDirective(const OMPTaskgroupDirective &s)
mlir::LogicalResult emitOMPParallelMaskedTaskLoopSimdDirective(const OMPParallelMaskedTaskLoopSimdDirective &s)
mlir::LogicalResult emitOMPTeamsDistributeDirective(const OMPTeamsDistributeDirective &s)
void emitReturnOfRValue(mlir::Location loc, RValue rv, QualType ty)
mlir::LogicalResult emitOMPInteropDirective(const OMPInteropDirective &s)
mlir::LogicalResult emitOMPErrorDirective(const OMPErrorDirective &s)
mlir::LogicalResult emitOMPSingleDirective(const OMPSingleDirective &s)
bool checkIfLoopMustProgress(const clang::Expr *controllingExpression, bool hasEmptyBody)
cir::LoopHintAttr getLoopHints(llvm::ArrayRef< const Attr * > attrs, bool mustProgress)
Build the hints that get attached to a loop, or a null attribute if there is nothing to say about it.
mlir::LogicalResult emitContinueStmt(const clang::ContinueStmt &s)
mlir::LogicalResult emitOMPTaskyieldDirective(const OMPTaskyieldDirective &s)
mlir::LogicalResult emitOMPTargetTeamsDistributeSimdDirective(const OMPTargetTeamsDistributeSimdDirective &s)
mlir::LogicalResult emitOMPScanDirective(const OMPScanDirective &s)
llvm::SmallVector< mlir::Type, 2 > condTypeStack
The type of the condition for the emitting switch statement.
mlir::LogicalResult emitOMPTargetEnterDataDirective(const OMPTargetEnterDataDirective &s)
void emitStopPoint(const Stmt *s)
Build a debug stoppoint if we are emitting debug info.
mlir::LogicalResult emitOMPMasterTaskLoopDirective(const OMPMasterTaskLoopDirective &s)
mlir::LogicalResult emitOpenACCHostDataConstruct(const OpenACCHostDataConstruct &s)
mlir::Value emitScalarExpr(const clang::Expr *e, bool ignoreResultAssign=false)
Emit the computation of the specified expression of scalar type.
mlir::LogicalResult emitIfStmt(const clang::IfStmt &s)
mlir::LogicalResult emitOMPForDirective(const OMPForDirective &s)
mlir::LogicalResult emitOMPMasterDirective(const OMPMasterDirective &s)
AggValueSlot::Overlap_t getOverlapForReturnValue()
Determine whether a return value slot may overlap some other object.
mlir::LogicalResult emitSwitchCase(const clang::SwitchCase &s, bool buildingTopLevelCase)
mlir::LogicalResult emitOMPMetaDirective(const OMPMetaDirective &s)
mlir::LogicalResult emitOMPDistributeSimdDirective(const OMPDistributeSimdDirective &s)
void emitDecl(const clang::Decl &d, bool evaluateConditionDecl=false)
mlir::LogicalResult emitOMPParallelGenericLoopDirective(const OMPParallelGenericLoopDirective &s)
mlir::LogicalResult emitOMPMaskedDirective(const OMPMaskedDirective &s)
mlir::LogicalResult emitOMPSplitDirective(const OMPSplitDirective &s)
llvm::DenseMap< const VarDecl *, mlir::Value > nrvoFlags
A mapping from NRVO variables to the flags used to indicate when the NRVO has been applied to this va...
mlir::MLIRContext & getMLIRContext()
mlir::LogicalResult emitOpenACCEnterDataConstruct(const OpenACCEnterDataConstruct &s)
mlir::LogicalResult emitOMPTargetExitDataDirective(const OMPTargetExitDataDirective &s)
bool containsLabel(const clang::Stmt *s, bool ignoreCaseStmts=false)
Return true if the statement contains a label in it.
mlir::LogicalResult emitOMPOrderedBlockAssocDirective(const OMPOrderedBlockAssocDirective &s)
mlir::LogicalResult emitOMPTargetTeamsDistributeParallelForDirective(const OMPTargetTeamsDistributeParallelForDirective &s)
void emitComplexExprIntoLValue(const Expr *e, LValue dest, bool isInit)
mlir::LogicalResult emitOMPParallelForDirective(const OMPParallelForDirective &s)
mlir::LogicalResult emitCaseDefaultCascade(const T *stmt, mlir::Type condType, mlir::ArrayAttr value, cir::CaseOpKind kind, bool buildingTopLevelCase)
mlir::LogicalResult emitOMPSectionsDirective(const OMPSectionsDirective &s)
LValue makeAddrLValue(Address addr, QualType ty, AlignmentSource source=AlignmentSource::Type)
mlir::LogicalResult emitOMPDistributeDirective(const OMPDistributeDirective &s)
RValue emitAnyExpr(const clang::Expr *e, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
Emit code to compute the specified expression which can have any type.
mlir::LogicalResult emitOMPTargetTeamsDistributeParallelForSimdDirective(const OMPTargetTeamsDistributeParallelForSimdDirective &s)
mlir::LogicalResult emitOMPTargetTeamsGenericLoopDirective(const OMPTargetTeamsGenericLoopDirective &s)
mlir::LogicalResult emitDeclStmt(const clang::DeclStmt &s)
mlir::LogicalResult emitOMPTeamsDistributeSimdDirective(const OMPTeamsDistributeSimdDirective &s)
mlir::LogicalResult emitDefaultStmt(const clang::DefaultStmt &s, mlir::Type condType, bool buildingTopLevelCase)
mlir::LogicalResult emitLabelStmt(const clang::LabelStmt &s)
mlir::LogicalResult emitOMPTaskLoopSimdDirective(const OMPTaskLoopSimdDirective &s)
void terminateStructuredRegionBody(mlir::Region &r, mlir::Location loc)
clang::ASTContext & getContext() const
mlir::LogicalResult emitCoroutineBody(const CoroutineBodyStmt &s)
mlir::LogicalResult emitCompoundStmt(const clang::CompoundStmt &s, Address *lastValue=nullptr, AggValueSlot slot=AggValueSlot::ignored())
mlir::LogicalResult emitGotoStmt(const clang::GotoStmt &s)
bool inAlwaysInlineAttributedStmt
True if the current statement has always_inline attribute.
bool inNoInlineAttributedStmt
True if the current statement has noinline attribute.
mlir::LogicalResult emitOMPParallelMasterTaskLoopDirective(const OMPParallelMasterTaskLoopDirective &s)
static llvm::SmallVector< QualType, 2 > getLifetimeExtendedTemporariesWithDestructor(const clang::VarDecl &d)
The types of the temporaries with a destructor that the initializer of the variable d,...
mlir::LogicalResult emitStmt(const clang::Stmt *s, bool useCurrentScope, llvm::ArrayRef< const Attr * > attrs={})
mlir::LogicalResult emitOMPCancelDirective(const OMPCancelDirective &s)
mlir::LogicalResult emitOMPStripeDirective(const OMPStripeDirective &s)
mlir::LogicalResult emitOMPTargetTeamsDistributeDirective(const OMPTargetTeamsDistributeDirective &s)
mlir::LogicalResult emitCompoundStmtWithoutScope(const clang::CompoundStmt &s, Address *lastValue=nullptr, AggValueSlot slot=AggValueSlot::ignored())
mlir::LogicalResult emitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &s)
mlir::LogicalResult emitOpenACCExitDataConstruct(const OpenACCExitDataConstruct &s)
mlir::LogicalResult emitSYCLKernelCallStmt(const SYCLKernelCallStmt &s)
void emitIgnoredExpr(const clang::Expr *e)
Emit code to compute the specified expression, ignoring the result.
void emitAggExpr(const clang::Expr *e, AggValueSlot slot)
mlir::LogicalResult emitOpenACCAtomicConstruct(const OpenACCAtomicConstruct &s)
mlir::LogicalResult emitOMPTargetSimdDirective(const OMPTargetSimdDirective &s)
mlir::LogicalResult emitOMPAssumeDirective(const OMPAssumeDirective &s)
mlir::LogicalResult emitOpenACCLoopConstruct(const OpenACCLoopConstruct &s)
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
This trivial value class is used to represent the result of an expression that is evaluated.
Definition CIRGenValue.h:33
Address getAggregateAddress() const
Return the value of the address of the aggregate.
Definition CIRGenValue.h:69
bool isAggregate() const
Definition CIRGenValue.h:51
bool isComplex() const
Definition CIRGenValue.h:50
mlir::Value getValue() const
Return the value of this scalar value.
Definition CIRGenValue.h:57
bool isScalar() const
Definition CIRGenValue.h:49
mlir::Value getComplexValue() const
Return the value of this complex value.
Definition CIRGenValue.h:63
CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for statement, represented as 'for (ra...
Definition StmtCXX.h:136
DeclStmt * getBeginStmt()
Definition StmtCXX.h:164
DeclStmt * getEndStmt()
Definition StmtCXX.h:167
DeclStmt * getRangeStmt()
Definition StmtCXX.h:163
SourceLocation getEndLoc() const LLVM_READONLY
Definition StmtCXX.h:209
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:3012
CaseStmt - Represent a case statement.
Definition Stmt.h:1931
Expr * getLHS()
Definition Stmt.h:2014
Expr * getRHS()
Definition Stmt.h:2026
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition Stmt.h:1751
body_range body()
Definition Stmt.h:1814
Stmt * body_back()
Definition Stmt.h:1819
ContinueStmt - This represents a continue.
Definition Stmt.h:3132
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1642
decl_range decls()
Definition Stmt.h:1690
DoStmt - This represents a 'do/while' stmt.
Definition Stmt.h:2843
Expr * getCond()
Definition Stmt.h:2861
SourceLocation getEndLoc() const
Definition Stmt.h:2880
This represents one expression.
Definition Expr.h:114
llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx) const
EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded integer.
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3125
bool HasSideEffects(const ASTContext &Ctx, bool IncludePossibleEffects=true) const
HasSideEffects - This routine returns true for all those expressions which have any effect other than...
Definition Expr.cpp:3728
QualType getType() const
Definition Expr.h:146
ForStmt - This represents a 'for (init;cond;inc)' stmt.
Definition Stmt.h:2899
Stmt * getInit()
Definition Stmt.h:2914
VarDecl * getConditionVariable() const
Retrieve the variable declared in this "for" statement, if any.
Definition Stmt.cpp:1120
SourceLocation getEndLoc() const
Definition Stmt.h:2963
Stmt * getBody()
Definition Stmt.h:2943
Expr * getInc()
Definition Stmt.h:2942
Expr * getCond()
Definition Stmt.h:2941
GotoStmt - This represents a direct goto.
Definition Stmt.h:2980
LabelDecl * getLabel() const
Definition Stmt.h:2993
IfStmt - This represents an if/then/else.
Definition Stmt.h:2270
Stmt * getThen()
Definition Stmt.h:2359
Stmt * getInit()
Definition Stmt.h:2420
Expr * getCond()
Definition Stmt.h:2347
bool isConstexpr() const
Definition Stmt.h:2463
bool isNegatedConsteval() const
Definition Stmt.h:2459
Stmt * getElse()
Definition Stmt.h:2368
bool isConsteval() const
Definition Stmt.h:2450
VarDecl * getConditionVariable()
Retrieve the variable declared in this "if" statement, if any.
Definition Stmt.cpp:1068
IndirectGotoStmt - This represents an indirect goto.
Definition Stmt.h:3019
Represents the declaration of a label.
Definition Decl.h:526
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.h:556
LabelStmt - Represents a label, which has a substatement.
Definition Stmt.h:2157
LabelDecl * getDecl() const
Definition Stmt.h:2175
bool isSideEntry() const
Definition Stmt.h:2204
Stmt * getSubStmt()
Definition Stmt.h:2179
SourceLocation getKwLoc() const
Definition Stmt.h:3093
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:303
A (possibly-)qualified type.
Definition TypeBase.h:938
The collection of all-type qualifiers we support.
Definition TypeBase.h:332
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition Stmt.h:3173
const VarDecl * getNRVOCandidate() const
Retrieve the variable that might be used for the named return value optimization.
Definition Stmt.h:3209
Expr * getRetValue()
Definition Stmt.h:3200
Encodes a location in the source.
Stmt - This represents one statement.
Definition Stmt.h:85
@ NoStmtClass
Definition Stmt.h:88
StmtClass getStmtClass() const
Definition Stmt.h:1504
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
const char * getStmtClassName() const
Definition Stmt.cpp:86
SwitchStmt - This represents a 'switch' stmt.
Definition Stmt.h:2520
bool isAllEnumCasesCovered() const
Returns true if the SwitchStmt is a switch of an enum value and all cases have been explicitly covere...
Definition Stmt.h:2680
Expr * getCond()
Definition Stmt.h:2583
VarDecl * getConditionVariable()
Retrieve the variable declared in this "switch" statement, if any.
Definition Stmt.cpp:1186
Stmt * getInit()
Definition Stmt.h:2600
SourceLocation getBeginLoc() const
Definition Stmt.h:2684
bool isVoidType() const
Definition TypeBase.h:9200
Represents a variable declaration or definition.
Definition Decl.h:934
bool isNRVOVariable() const
Determine whether this local variable can be used with the named return value optimization (NRVO).
Definition Decl.h:1549
QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const
Would the destruction of this variable have any effect, and if so, what kind?
Definition Decl.cpp:2845
WhileStmt - This represents a 'while' stmt.
Definition Stmt.h:2708
Expr * getCond()
Definition Stmt.h:2760
VarDecl * getConditionVariable()
Retrieve the variable declared in this "while" statement, if any.
Definition Stmt.cpp:1247
SourceLocation getEndLoc() const LLVM_READONLY
Definition Stmt.h:2822
Stmt * getBody()
Definition Stmt.h:2772
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
void updateLoopAttributes(LoopAttributes &Attrs, ASTContext &Ctx, const CodeGenOptions &CGOpts, llvm::ArrayRef< const Attr * > StmtAttrs, bool MustProgress)
Update Attrs with the loop hints in StmtAttrs (#pragma clang loop, #pragma unroll,...
bool hasEmptyLoopBody(const LoopStmt &S)
Definition StmtUtils.h:39
const internal::VariadicDynCastAllOfMatcher< Stmt, CompoundStmt > compoundStmt
Matches compound statements.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Stmt, SwitchCase > switchCase
Matches case and default statements inside switch statements.
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ Default
Set to the current date and time.
const FunctionProtoType * T
U cast(CodeGen::Address addr)
Definition Address.h:327
static bool aggValueSlotGC()
static bool createProfileWeightsForLoop()
static bool loopInfoStack()
static bool emitCondLikelihoodViaExpectIntrinsic()
static bool constantFoldSwitchStatement()
static bool insertBuiltinUnpredictable()
static bool generateDebugInfo()
static bool incrementProfileCounter()
unsigned ignore_denormal_mode
Represents a scope, including function bodies, compound statements, and the substatements of if/while...
Attributes that may be specified on loops.
bool LICMDisabled
Value for llvm.licm.disable metadata.
LVEnableState VectorizeEnable
Value for llvm.loop.vectorize.enable metadata.
LVEnableState UnrollEnable
Value for llvm.loop.unroll.* metadata (enable, disable, or full).
unsigned PipelineInitiationInterval
Value for llvm.loop.pipeline.iicount metadata.
unsigned UnrollAndJamCount
llvm.unroll.
unsigned CodeAlign
Value for 'llvm.loop.align' metadata.
unsigned VectorizeWidth
Value for llvm.loop.vectorize.width metadata.
LVEnableState UnrollAndJamEnable
Value for llvm.loop.unroll_and_jam.* metadata (enable, disable, or full).
LVEnableState VectorizePredicateEnable
Value for llvm.loop.vectorize.predicate metadata.
bool PipelineDisabled
Value for llvm.loop.pipeline.disable metadata.
bool IsParallel
Generate llvm.loop.parallel metadata for loads and stores.
LVEnableState
State of loop vectorization or unrolling.
bool MustProgress
Value for whether the loop is required to make progress.
LVEnableState DistributeEnable
Value for llvm.loop.distribute.enable metadata.
unsigned InterleaveCount
Value for llvm.loop.interleave.count metadata.