clang 23.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"
24
25using namespace clang;
26using namespace clang::CIRGen;
27using namespace cir;
28
29static mlir::LogicalResult emitStmtWithResult(CIRGenFunction &cgf,
30 const Stmt *exprResult,
31 AggValueSlot slot,
32 Address *lastValue) {
33 // We have to special case labels here. They are statements, but when put
34 // at the end of a statement expression, they yield the value of their
35 // subexpression. Handle this by walking through all labels we encounter,
36 // emitting them before we evaluate the subexpr.
37 // Similar issues arise for attributed statements.
38 while (!isa<Expr>(exprResult)) {
39 if (const auto *ls = dyn_cast<LabelStmt>(exprResult)) {
40 if (cgf.emitLabel(*ls->getDecl()).failed())
41 return mlir::failure();
42 exprResult = ls->getSubStmt();
43 } else if (const auto *as = dyn_cast<AttributedStmt>(exprResult)) {
44 // FIXME: Update this if we ever have attributes that affect the
45 // semantics of an expression.
46 exprResult = as->getSubStmt();
47 } else {
48 llvm_unreachable("Unknown value statement");
49 }
50 }
51
52 const Expr *e = cast<Expr>(exprResult);
53 QualType exprTy = e->getType();
54 if (cgf.hasAggregateEvaluationKind(exprTy)) {
55 cgf.emitAggExpr(e, slot);
56 } else {
57 // We can't return an RValue here because there might be cleanups at
58 // the end of the StmtExpr. Because of that, we have to emit the result
59 // here into a temporary alloca.
60 cgf.emitAnyExprToMem(e, *lastValue, Qualifiers(),
61 /*IsInit*/ false);
62 }
63
64 return mlir::success();
65}
66
68 const CompoundStmt &s, Address *lastValue, AggValueSlot slot) {
69 mlir::LogicalResult result = mlir::success();
70 const Stmt *exprResult = s.body_back();
71 assert((!lastValue || (lastValue && exprResult)) &&
72 "If lastValue is not null then the CompoundStmt must have a "
73 "StmtExprResult");
74
75 for (const Stmt *curStmt : s.body()) {
76 const bool saveResult = lastValue && exprResult == curStmt;
77 if (saveResult) {
78 if (emitStmtWithResult(*this, exprResult, slot, lastValue).failed())
79 result = mlir::failure();
80 } else {
81 if (emitStmt(curStmt, /*useCurrentScope=*/false).failed())
82 result = mlir::failure();
83 }
84 }
85 return result;
86}
87
88mlir::LogicalResult
90 for (const Attr *attr : s.getAttrs()) {
91 switch (attr->getKind()) {
92 default:
93 break;
94 case attr::NoMerge:
95 case attr::NoInline:
96 case attr::AlwaysInline:
97 case attr::NoConvergent:
98 case attr::MustTail:
99 case attr::Atomic:
100 case attr::HLSLControlFlowHint:
101 cgm.errorNYI(s.getSourceRange(),
102 "Unimplemented statement attribute: ", attr->getKind());
103 break;
104 case attr::CXXAssume: {
105 const Expr *assumptionExpr = cast<CXXAssumeAttr>(attr)->getAssumption();
106 if (getLangOpts().CXXAssumptions && builder.getInsertionBlock() &&
107 !assumptionExpr->HasSideEffects(getContext())) {
108 mlir::Value assumptionValue = emitCheckedArgForAssume(assumptionExpr);
109 cir::AssumeOp::create(builder, getLoc(s.getSourceRange()),
110 assumptionValue, cir::AssumeBundleKind::None,
111 mlir::ValueRange{});
112 }
113 } break;
114 }
115 }
116
117 return emitStmt(s.getSubStmt(), /*useCurrentScope=*/true, s.getAttrs());
118}
119
121 Address *lastValue,
122 AggValueSlot slot) {
123 // Add local scope to track new declared variables.
125 mlir::Location scopeLoc = getLoc(s.getSourceRange());
126 mlir::OpBuilder::InsertPoint scopeInsPt;
127 cir::ScopeOp::create(
128 builder, scopeLoc,
129 [&](mlir::OpBuilder &b, mlir::Type &type, mlir::Location loc) {
130 scopeInsPt = b.saveInsertionPoint();
131 });
132 mlir::OpBuilder::InsertionGuard guard(builder);
133 builder.restoreInsertionPoint(scopeInsPt);
134 LexicalScope lexScope(*this, scopeLoc, builder.getInsertionBlock());
135 return emitCompoundStmtWithoutScope(s, lastValue, slot);
136}
137
141
142// Build CIR for a statement. useCurrentScope should be true if no new scopes
143// need to be created when finding a compound statement.
144mlir::LogicalResult CIRGenFunction::emitStmt(const Stmt *s,
145 bool useCurrentScope,
147 if (mlir::succeeded(emitSimpleStmt(s, useCurrentScope)))
148 return mlir::success();
149
150 switch (s->getStmtClass()) {
152 case Stmt::CXXCatchStmtClass:
153 case Stmt::SEHExceptStmtClass:
154 case Stmt::SEHFinallyStmtClass:
155 case Stmt::MSDependentExistsStmtClass:
156 case Stmt::UnresolvedSYCLKernelCallStmtClass:
157 llvm_unreachable("invalid statement class to emit generically");
158 case Stmt::BreakStmtClass:
159 case Stmt::NullStmtClass:
160 case Stmt::CompoundStmtClass:
161 case Stmt::ContinueStmtClass:
162 case Stmt::DeclStmtClass:
163 case Stmt::ReturnStmtClass:
164 llvm_unreachable("should have emitted these statements as simple");
165
166#define STMT(Type, Base)
167#define ABSTRACT_STMT(Op)
168#define EXPR(Type, Base) case Stmt::Type##Class:
169#include "clang/AST/StmtNodes.inc"
170 {
171 assert(builder.getInsertionBlock() &&
172 "expression emission must have an insertion point");
173
175
176 // Classic codegen has a check here to see if the emitter created a new
177 // block that isn't used (comparing the incoming and outgoing insertion
178 // points) and deletes the outgoing block if it's not used. In CIR, we
179 // will handle that during the cir.canonicalize pass.
180 return mlir::success();
181 }
182 case Stmt::IfStmtClass:
183 return emitIfStmt(cast<IfStmt>(*s));
184 case Stmt::SwitchStmtClass:
186 case Stmt::ForStmtClass:
187 return emitForStmt(cast<ForStmt>(*s));
188 case Stmt::WhileStmtClass:
189 return emitWhileStmt(cast<WhileStmt>(*s));
190 case Stmt::DoStmtClass:
191 return emitDoStmt(cast<DoStmt>(*s));
192 case Stmt::CXXTryStmtClass:
194 case Stmt::CXXForRangeStmtClass:
196 case Stmt::CoroutineBodyStmtClass:
198 case Stmt::IndirectGotoStmtClass:
200 case Stmt::CoreturnStmtClass:
202 case Stmt::OpenACCComputeConstructClass:
204 case Stmt::OpenACCLoopConstructClass:
206 case Stmt::OpenACCCombinedConstructClass:
208 case Stmt::OpenACCDataConstructClass:
210 case Stmt::OpenACCEnterDataConstructClass:
212 case Stmt::OpenACCExitDataConstructClass:
214 case Stmt::OpenACCHostDataConstructClass:
216 case Stmt::OpenACCWaitConstructClass:
218 case Stmt::OpenACCInitConstructClass:
220 case Stmt::OpenACCShutdownConstructClass:
222 case Stmt::OpenACCSetConstructClass:
224 case Stmt::OpenACCUpdateConstructClass:
226 case Stmt::OpenACCCacheConstructClass:
228 case Stmt::OpenACCAtomicConstructClass:
230 case Stmt::GCCAsmStmtClass:
231 case Stmt::MSAsmStmtClass:
232 return emitAsmStmt(cast<AsmStmt>(*s));
233 case Stmt::OMPScopeDirectiveClass:
235 case Stmt::OMPErrorDirectiveClass:
237 case Stmt::OMPParallelDirectiveClass:
239 case Stmt::OMPTaskwaitDirectiveClass:
241 case Stmt::OMPTaskyieldDirectiveClass:
243 case Stmt::OMPBarrierDirectiveClass:
245 case Stmt::OMPMetaDirectiveClass:
247 case Stmt::OMPCanonicalLoopClass:
249 case Stmt::OMPSimdDirectiveClass:
251 case Stmt::OMPTileDirectiveClass:
253 case Stmt::OMPUnrollDirectiveClass:
255 case Stmt::OMPFuseDirectiveClass:
257 case Stmt::OMPForDirectiveClass:
259 case Stmt::OMPForSimdDirectiveClass:
261 case Stmt::OMPSectionsDirectiveClass:
263 case Stmt::OMPSectionDirectiveClass:
265 case Stmt::OMPSingleDirectiveClass:
267 case Stmt::OMPMasterDirectiveClass:
269 case Stmt::OMPCriticalDirectiveClass:
271 case Stmt::OMPParallelForDirectiveClass:
273 case Stmt::OMPParallelForSimdDirectiveClass:
276 case Stmt::OMPParallelMasterDirectiveClass:
278 case Stmt::OMPParallelSectionsDirectiveClass:
281 case Stmt::OMPTaskDirectiveClass:
283 case Stmt::OMPTaskgroupDirectiveClass:
285 case Stmt::OMPFlushDirectiveClass:
287 case Stmt::OMPDepobjDirectiveClass:
289 case Stmt::OMPScanDirectiveClass:
291 case Stmt::OMPOrderedDirectiveClass:
293 case Stmt::OMPAtomicDirectiveClass:
295 case Stmt::OMPTargetDirectiveClass:
297 case Stmt::OMPTeamsDirectiveClass:
299 case Stmt::OMPCancellationPointDirectiveClass:
302 case Stmt::OMPCancelDirectiveClass:
304 case Stmt::OMPTargetDataDirectiveClass:
306 case Stmt::OMPTargetEnterDataDirectiveClass:
309 case Stmt::OMPTargetExitDataDirectiveClass:
311 case Stmt::OMPTargetParallelDirectiveClass:
313 case Stmt::OMPTargetParallelForDirectiveClass:
316 case Stmt::OMPTaskLoopDirectiveClass:
318 case Stmt::OMPTaskLoopSimdDirectiveClass:
320 case Stmt::OMPMaskedTaskLoopDirectiveClass:
322 case Stmt::OMPMaskedTaskLoopSimdDirectiveClass:
325 case Stmt::OMPMasterTaskLoopDirectiveClass:
327 case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
330 case Stmt::OMPParallelGenericLoopDirectiveClass:
333 case Stmt::OMPParallelMaskedDirectiveClass:
335 case Stmt::OMPParallelMaskedTaskLoopDirectiveClass:
338 case Stmt::OMPParallelMaskedTaskLoopSimdDirectiveClass:
341 case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
344 case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
347 case Stmt::OMPDistributeDirectiveClass:
349 case Stmt::OMPDistributeParallelForDirectiveClass:
352 case Stmt::OMPDistributeParallelForSimdDirectiveClass:
355 case Stmt::OMPDistributeSimdDirectiveClass:
357 case Stmt::OMPTargetParallelGenericLoopDirectiveClass:
360 case Stmt::OMPTargetParallelForSimdDirectiveClass:
363 case Stmt::OMPTargetSimdDirectiveClass:
365 case Stmt::OMPTargetTeamsGenericLoopDirectiveClass:
368 case Stmt::OMPTargetUpdateDirectiveClass:
370 case Stmt::OMPTeamsDistributeDirectiveClass:
373 case Stmt::OMPTeamsDistributeSimdDirectiveClass:
376 case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
379 case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
382 case Stmt::OMPTeamsGenericLoopDirectiveClass:
385 case Stmt::OMPTargetTeamsDirectiveClass:
387 case Stmt::OMPTargetTeamsDistributeDirectiveClass:
390 case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
393 case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
396 case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
399 case Stmt::OMPInteropDirectiveClass:
401 case Stmt::OMPDispatchDirectiveClass:
403 case Stmt::OMPGenericLoopDirectiveClass:
405 case Stmt::OMPReverseDirectiveClass:
407 case Stmt::OMPSplitDirectiveClass:
409 case Stmt::OMPInterchangeDirectiveClass:
411 case Stmt::OMPAssumeDirectiveClass:
413 case Stmt::OMPMaskedDirectiveClass:
415 case Stmt::OMPStripeDirectiveClass:
417 case Stmt::LabelStmtClass:
418 case Stmt::AttributedStmtClass:
419 case Stmt::GotoStmtClass:
420 case Stmt::DefaultStmtClass:
421 case Stmt::CaseStmtClass:
422 case Stmt::SEHLeaveStmtClass:
423 case Stmt::SYCLKernelCallStmtClass:
424 case Stmt::CapturedStmtClass:
425 case Stmt::ObjCAtTryStmtClass:
426 case Stmt::ObjCAtThrowStmtClass:
427 case Stmt::ObjCAtSynchronizedStmtClass:
428 case Stmt::ObjCForCollectionStmtClass:
429 case Stmt::ObjCAutoreleasePoolStmtClass:
430 case Stmt::SEHTryStmtClass:
431 case Stmt::ObjCAtCatchStmtClass:
432 case Stmt::ObjCAtFinallyStmtClass:
433 case Stmt::DeferStmtClass:
434 cgm.errorNYI(s->getSourceRange(),
435 std::string("emitStmt: ") + s->getStmtClassName());
436 return mlir::failure();
437 }
438
439 llvm_unreachable("Unexpected statement class");
440}
441
442mlir::LogicalResult CIRGenFunction::emitSimpleStmt(const Stmt *s,
443 bool useCurrentScope) {
444 switch (s->getStmtClass()) {
445 default:
446 return mlir::failure();
447 case Stmt::DeclStmtClass:
448 return emitDeclStmt(cast<DeclStmt>(*s));
449 case Stmt::CompoundStmtClass:
450 if (useCurrentScope)
453 case Stmt::GotoStmtClass:
454 return emitGotoStmt(cast<GotoStmt>(*s));
455 case Stmt::ContinueStmtClass:
457
458 // NullStmt doesn't need any handling, but we need to say we handled it.
459 case Stmt::NullStmtClass:
460 break;
461
462 case Stmt::LabelStmtClass:
463 return emitLabelStmt(cast<LabelStmt>(*s));
464 case Stmt::CaseStmtClass:
465 case Stmt::DefaultStmtClass:
466 // If we reached here, we must not handling a switch case in the top level.
468 /*buildingTopLevelCase=*/false);
469 break;
470
471 case Stmt::BreakStmtClass:
472 return emitBreakStmt(cast<BreakStmt>(*s));
473 case Stmt::ReturnStmtClass:
475 case Stmt::AttributedStmtClass:
477 }
478
479 return mlir::success();
480}
481
482mlir::LogicalResult CIRGenFunction::emitLabelStmt(const clang::LabelStmt &s) {
483
484 if (emitLabel(*s.getDecl()).failed())
485 return mlir::failure();
486
487 if (getContext().getLangOpts().EHAsynch && s.isSideEntry())
488 getCIRGenModule().errorNYI(s.getSourceRange(), "IsEHa: not implemented.");
489
490 return emitStmt(s.getSubStmt(), /*useCurrentScope*/ true);
491}
492
493// Add a terminating yield on a body region if no other terminators are used.
495 mlir::Location loc) {
496 if (r.empty())
497 return;
498
500 unsigned numBlocks = r.getBlocks().size();
501 for (auto &block : r.getBlocks()) {
502 // Already cleanup after return operations, which might create
503 // empty blocks if emitted as last stmt.
504 if (numBlocks != 1 && block.empty() && block.hasNoPredecessors() &&
505 block.hasNoSuccessors())
506 eraseBlocks.push_back(&block);
507
508 if (block.empty() ||
509 !block.back().hasTrait<mlir::OpTrait::IsTerminator>()) {
510 mlir::OpBuilder::InsertionGuard guardCase(builder);
511 builder.setInsertionPointToEnd(&block);
512 builder.createYield(loc);
513 }
514 }
515
516 for (auto *b : eraseBlocks)
517 b->erase();
518}
519
520mlir::LogicalResult CIRGenFunction::emitIfStmt(const IfStmt &s) {
521 mlir::LogicalResult res = mlir::success();
522 // The else branch of a consteval if statement is always the only branch
523 // that can be runtime evaluated.
524 const Stmt *constevalExecuted;
525 if (s.isConsteval()) {
526 constevalExecuted = s.isNegatedConsteval() ? s.getThen() : s.getElse();
527 if (!constevalExecuted) {
528 // No runtime code execution required
529 return res;
530 }
531 }
532
533 // C99 6.8.4.1: The first substatement is executed if the expression
534 // compares unequal to 0. The condition must be a scalar type.
535 auto ifStmtBuilder = [&]() -> mlir::LogicalResult {
536 if (s.isConsteval())
537 return emitStmt(constevalExecuted, /*useCurrentScope=*/true);
538
539 if (s.getInit())
540 if (emitStmt(s.getInit(), /*useCurrentScope=*/true).failed())
541 return mlir::failure();
542
543 if (s.getConditionVariable())
545
546 // If the condition folds to a constant and this is an 'if constexpr',
547 // we simplify it early in CIRGen to avoid emitting the full 'if'.
548 bool condConstant;
549 if (constantFoldsToBool(s.getCond(), condConstant, s.isConstexpr())) {
550 if (s.isConstexpr()) {
551 // Handle "if constexpr" explicitly here to avoid generating some
552 // ill-formed code since in CIR the "if" is no longer simplified
553 // in this lambda like in Clang but postponed to other MLIR
554 // passes.
555 if (const Stmt *executed = condConstant ? s.getThen() : s.getElse())
556 return emitStmt(executed, /*useCurrentScope=*/true);
557 // There is nothing to execute at runtime.
558 // TODO(cir): there is still an empty cir.scope generated by the caller.
559 return mlir::success();
560 }
561 }
562
565 return emitIfOnBoolExpr(s.getCond(), s.getThen(), s.getElse());
566 };
567
568 // TODO: Add a new scoped symbol table.
569 // LexicalScope ConditionScope(*this, S.getCond()->getSourceRange());
570 // The if scope contains the full source range for IfStmt.
571 mlir::Location scopeLoc = getLoc(s.getSourceRange());
572 cir::ScopeOp::create(builder, scopeLoc, /*scopeBuilder=*/
573 [&](mlir::OpBuilder &b, mlir::Location loc) {
574 LexicalScope lexScope{*this, scopeLoc,
575 builder.getInsertionBlock()};
576 res = ifStmtBuilder();
577 });
578
579 return res;
580}
581
582mlir::LogicalResult CIRGenFunction::emitDeclStmt(const DeclStmt &s) {
583 assert(builder.getInsertionBlock() && "expected valid insertion point");
584
585 for (const Decl *i : s.decls())
586 emitDecl(*i, /*evaluateConditionDecl=*/true);
587
588 return mlir::success();
589}
590
591mlir::LogicalResult CIRGenFunction::emitReturnStmt(const ReturnStmt &s) {
592 mlir::Location loc = getLoc(s.getSourceRange());
593 const Expr *rv = s.getRetValue();
594
595 RunCleanupsScope cleanupScope(*this);
596 bool createNewScope = false;
597 if (const auto *ewc = dyn_cast_or_null<ExprWithCleanups>(rv)) {
598 rv = ewc->getSubExpr();
599 createNewScope = true;
600 }
601
602 auto handleReturnVal = [&]() {
603 if (getContext().getLangOpts().ElideConstructors && s.getNRVOCandidate() &&
606 // Apply the named return value optimization for this return statement,
607 // which means doing nothing: the appropriate result has already been
608 // constructed into the NRVO variable.
609
610 // If there is an NRVO flag for this variable, set it to 1 into indicate
611 // that the cleanup code should not destroy the variable.
612 if (auto nrvoFlag = nrvoFlags[s.getNRVOCandidate()])
613 builder.createFlagStore(loc, true, nrvoFlag);
614 } else if (!rv) {
615 // No return expression. Do nothing.
616 } else if (rv->getType()->isVoidType()) {
617 // Make sure not to return anything, but evaluate the expression
618 // for side effects.
619 if (rv) {
620 emitAnyExpr(rv);
621 }
622 } else if (cast<FunctionDecl>(curGD.getDecl())
623 ->getReturnType()
624 ->isReferenceType()) {
625 // If this function returns a reference, take the address of the
626 // expression rather than the value.
628 builder.CIRBaseBuilderTy::createStore(loc, result.getValue(),
629 *fnRetAlloca);
630 } else {
631 mlir::Value value = nullptr;
633 case cir::TEK_Scalar:
634 value = emitScalarExpr(rv);
635 if (value) { // Change this to an assert once emitScalarExpr is complete
636 builder.CIRBaseBuilderTy::createStore(loc, value, *fnRetAlloca);
637 }
638 break;
639 case cir::TEK_Complex:
642 /*isInit=*/true);
643 break;
650 break;
651 }
652 }
653 };
654
655 if (!createNewScope) {
656 handleReturnVal();
657 } else {
658 FullExprCleanupScope fullExprScope(*this, rv);
659 handleReturnVal();
660 }
661
662 cleanupScope.forceCleanup();
663
664 // Classic codegen emits a branch through any cleanups before continuing to
665 // a shared return block. Because CIR handles branching through cleanups
666 // during the CFG flattening phase, we can just emit the return statement
667 // directly.
668 // TODO(cir): Eliminate this redundant load and the store above when we can.
669 if (fnRetAlloca) {
670 // Load the value from `__retval` and return it via the `cir.return` op.
671 cir::AllocaOp retAlloca =
672 mlir::cast<cir::AllocaOp>(fnRetAlloca->getDefiningOp());
673 auto value = cir::LoadOp::create(builder, loc, retAlloca.getAllocaType(),
674 *fnRetAlloca);
675
676 cir::ReturnOp::create(builder, loc, {value});
677 } else {
678 cir::ReturnOp::create(builder, loc);
679 }
680
681 // Insert the new block to continue codegen after the return statement.
682 // This will get deleted if we don't populate it. This handles the case of
683 // unreachable statements below a return.
684 builder.createBlock(builder.getBlock()->getParent());
685 return mlir::success();
686}
687
688mlir::LogicalResult CIRGenFunction::emitGotoStmt(const clang::GotoStmt &s) {
689 // FIXME: LLVM codegen inserts emit a stop point here for debug info
690 // sake when the insertion point is available, but doesn't do
691 // anything special when there isn't. We haven't implemented debug
692 // info support just yet, look at this again once we have it.
694
695 cir::GotoOp::create(builder, getLoc(s.getSourceRange()),
696 s.getLabel()->getName());
697
698 // A goto marks the end of a block, create a new one for codegen after
699 // emitGotoStmt can resume building in that block.
700 // Insert the new block to continue codegen after goto.
701 builder.createBlock(builder.getBlock()->getParent());
702
703 return mlir::success();
704}
705
706mlir::LogicalResult
708 mlir::Value val = emitScalarExpr(s.getTarget());
709 assert(indirectGotoBlock &&
710 "If you jumping to a indirect branch should be alareadye emitted");
711 cir::BrOp::create(builder, getLoc(s.getSourceRange()), indirectGotoBlock,
712 val);
713 builder.createBlock(builder.getBlock()->getParent());
714 return mlir::success();
715}
716
717mlir::LogicalResult
719 builder.createContinue(getLoc(s.getKwLoc()));
720
721 // Insert the new block to continue codegen after the continue statement.
722 builder.createBlock(builder.getBlock()->getParent());
723
724 return mlir::success();
725}
726
727mlir::LogicalResult CIRGenFunction::emitLabel(const clang::LabelDecl &d) {
728 // Create a new block to tag with a label and add a branch from
729 // the current one to it. If the block is empty just call attach it
730 // to this label.
731 mlir::Block *currBlock = builder.getBlock();
732 mlir::Block *labelBlock = currBlock;
733
734 if (!currBlock->empty() || currBlock->isEntryBlock()) {
735 {
736 mlir::OpBuilder::InsertionGuard guard(builder);
737 labelBlock = builder.createBlock(builder.getBlock()->getParent());
738 }
739 cir::BrOp::create(builder, getLoc(d.getSourceRange()), labelBlock);
740 }
741
742 builder.setInsertionPointToEnd(labelBlock);
743 cir::LabelOp label =
744 cir::LabelOp::create(builder, getLoc(d.getSourceRange()), d.getName());
745 builder.setInsertionPointToEnd(labelBlock);
746 auto func = cast<cir::FuncOp>(curFn);
747 cgm.mapBlockAddress(cir::BlockAddrInfoAttr::get(builder.getContext(),
748 func.getSymNameAttr(),
749 label.getLabelAttr()),
750 label);
751 // FIXME: emit debug info for labels, incrementProfileCounter
754 return mlir::success();
755}
756
757mlir::LogicalResult CIRGenFunction::emitBreakStmt(const clang::BreakStmt &s) {
758 builder.createBreak(getLoc(s.getKwLoc()));
759
760 // Insert the new block to continue codegen after the break statement.
761 builder.createBlock(builder.getBlock()->getParent());
762
763 return mlir::success();
764}
765
766template <typename T>
767mlir::LogicalResult
768CIRGenFunction::emitCaseDefaultCascade(const T *stmt, mlir::Type condType,
769 mlir::ArrayAttr value, CaseOpKind kind,
770 bool buildingTopLevelCase) {
771
773 "only case or default stmt go here");
774
775 mlir::LogicalResult result = mlir::success();
776
777 mlir::Location loc = getLoc(stmt->getBeginLoc());
778
779 enum class SubStmtKind { Case, Default, Other };
780 SubStmtKind subStmtKind = SubStmtKind::Other;
781 const Stmt *sub = stmt->getSubStmt();
782
783 mlir::OpBuilder::InsertPoint insertPoint;
784 CaseOp::create(builder, loc, value, kind, insertPoint);
785
786 {
787 mlir::OpBuilder::InsertionGuard guardSwitch(builder);
788 builder.restoreInsertionPoint(insertPoint);
789
790 if (isa<DefaultStmt>(sub) && isa<CaseStmt>(stmt)) {
791 subStmtKind = SubStmtKind::Default;
792 builder.createYield(loc);
793 } else if (isa<CaseStmt>(sub) && isa<DefaultStmt, CaseStmt>(stmt)) {
794 subStmtKind = SubStmtKind::Case;
795 builder.createYield(loc);
796 } else {
797 result = emitStmt(sub, /*useCurrentScope=*/!isa<CompoundStmt>(sub));
798 }
799
800 insertPoint = builder.saveInsertionPoint();
801 }
802
803 // If the substmt is default stmt or case stmt, try to handle the special case
804 // to make it into the simple form. e.g.
805 //
806 // switch () {
807 // case 1:
808 // default:
809 // ...
810 // }
811 //
812 // we prefer generating
813 //
814 // cir.switch() {
815 // cir.case(equal, 1) {
816 // cir.yield
817 // }
818 // cir.case(default) {
819 // ...
820 // }
821 // }
822 //
823 // than
824 //
825 // cir.switch() {
826 // cir.case(equal, 1) {
827 // cir.case(default) {
828 // ...
829 // }
830 // }
831 // }
832 //
833 // We don't need to revert this if we find the current switch can't be in
834 // simple form later since the conversion itself should be harmless.
835 if (subStmtKind == SubStmtKind::Case) {
836 result = emitCaseStmt(*cast<CaseStmt>(sub), condType, buildingTopLevelCase);
837 } else if (subStmtKind == SubStmtKind::Default) {
838 result = emitDefaultStmt(*cast<DefaultStmt>(sub), condType,
839 buildingTopLevelCase);
840 } else if (buildingTopLevelCase) {
841 // If we're building a top level case, try to restore the insert point to
842 // the case we're building, then we can attach more random stmts to the
843 // case to make generating `cir.switch` operation to be a simple form.
844 builder.restoreInsertionPoint(insertPoint);
845 }
846
847 return result;
848}
849
850mlir::LogicalResult CIRGenFunction::emitCaseStmt(const CaseStmt &s,
851 mlir::Type condType,
852 bool buildingTopLevelCase) {
853 cir::CaseOpKind kind;
854 mlir::ArrayAttr value;
855 llvm::APSInt intVal = s.getLHS()->EvaluateKnownConstInt(getContext());
856
857 // If the case statement has an RHS value, it is representing a GNU
858 // case range statement, where LHS is the beginning of the range
859 // and RHS is the end of the range.
860 if (const Expr *rhs = s.getRHS()) {
861 llvm::APSInt endVal = rhs->EvaluateKnownConstInt(getContext());
862 value = builder.getArrayAttr({cir::IntAttr::get(condType, intVal),
863 cir::IntAttr::get(condType, endVal)});
864 kind = cir::CaseOpKind::Range;
865 } else {
866 value = builder.getArrayAttr({cir::IntAttr::get(condType, intVal)});
867 kind = cir::CaseOpKind::Equal;
868 }
869
870 return emitCaseDefaultCascade(&s, condType, value, kind,
871 buildingTopLevelCase);
872}
873
875 mlir::Type condType,
876 bool buildingTopLevelCase) {
877 return emitCaseDefaultCascade(&s, condType, builder.getArrayAttr({}),
878 cir::CaseOpKind::Default, buildingTopLevelCase);
879}
880
881mlir::LogicalResult CIRGenFunction::emitSwitchCase(const SwitchCase &s,
882 bool buildingTopLevelCase) {
883 assert(!condTypeStack.empty() &&
884 "build switch case without specifying the type of the condition");
885
886 if (s.getStmtClass() == Stmt::CaseStmtClass)
887 return emitCaseStmt(cast<CaseStmt>(s), condTypeStack.back(),
888 buildingTopLevelCase);
889
890 if (s.getStmtClass() == Stmt::DefaultStmtClass)
892 buildingTopLevelCase);
893
894 llvm_unreachable("expect case or default stmt");
895}
896
897mlir::LogicalResult
899 ArrayRef<const Attr *> forAttrs) {
900 cir::ForOp forOp;
901
902 // TODO(cir): pass in array of attributes.
903 auto forStmtBuilder = [&]() -> mlir::LogicalResult {
904 mlir::LogicalResult loopRes = mlir::success();
905 // Evaluate the first pieces before the loop.
906 if (s.getInit())
907 if (emitStmt(s.getInit(), /*useCurrentScope=*/true).failed())
908 return mlir::failure();
909 if (emitStmt(s.getRangeStmt(), /*useCurrentScope=*/true).failed())
910 return mlir::failure();
911 if (emitStmt(s.getBeginStmt(), /*useCurrentScope=*/true).failed())
912 return mlir::failure();
913 if (emitStmt(s.getEndStmt(), /*useCurrentScope=*/true).failed())
914 return mlir::failure();
915
917
918 forOp = builder.createFor(
920 /*condBuilder=*/
921 [&](mlir::OpBuilder &b, mlir::Location loc) {
922 assert(!cir::MissingFeatures::createProfileWeightsForLoop());
923 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
924 mlir::Value condVal = evaluateExprAsBool(s.getCond());
925 builder.createCondition(condVal);
926 },
927 /*bodyBuilder=*/
928 [&](mlir::OpBuilder &b, mlir::Location loc) {
929 // https://en.cppreference.com/w/cpp/language/for
930 // In C++ the scope of the init-statement and the scope of
931 // statement are one and the same.
932 RunCleanupsScope bodyScope(*this);
933 bool useCurrentScope = true;
934 if (emitStmt(s.getLoopVarStmt(), useCurrentScope).failed())
935 loopRes = mlir::failure();
936 if (emitStmt(s.getBody(), useCurrentScope).failed())
937 loopRes = mlir::failure();
938 emitStopPoint(&s);
939 },
940 /*stepBuilder=*/
941 [&](mlir::OpBuilder &b, mlir::Location loc) {
942 if (s.getInc())
943 if (emitStmt(s.getInc(), /*useCurrentScope=*/true).failed())
944 loopRes = mlir::failure();
945 builder.createYield(loc);
946 });
947 return loopRes;
948 };
949
950 mlir::LogicalResult res = mlir::success();
951 mlir::Location scopeLoc = getLoc(s.getSourceRange());
952 cir::ScopeOp::create(builder, scopeLoc, /*scopeBuilder=*/
953 [&](mlir::OpBuilder &b, mlir::Location loc) {
954 // Create a cleanup scope for the condition
955 // variable cleanups. Logical equivalent from
956 // LLVM codegn for LexicalScope
957 // ConditionScope(*this, S.getSourceRange())...
958 LexicalScope lexScope{*this, loc,
959 builder.getInsertionBlock()};
960 res = forStmtBuilder();
961 });
962
963 if (res.failed())
964 return res;
965
966 terminateStructuredRegionBody(forOp.getBody(), getLoc(s.getEndLoc()));
967 return mlir::success();
968}
969
970mlir::LogicalResult CIRGenFunction::emitForStmt(const ForStmt &s) {
971 cir::ForOp forOp;
972
973 // TODO: pass in an array of attributes.
974 auto forStmtBuilder = [&]() -> mlir::LogicalResult {
975 mlir::LogicalResult loopRes = mlir::success();
976 // Evaluate the first part before the loop.
977 if (s.getInit())
978 if (emitStmt(s.getInit(), /*useCurrentScope=*/true).failed())
979 return mlir::failure();
981
982 forOp = builder.createFor(
984 /*condBuilder=*/
985 [&](mlir::OpBuilder &b, mlir::Location loc) {
986 assert(!cir::MissingFeatures::createProfileWeightsForLoop());
987 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
988 mlir::Value condVal;
989 if (s.getCond()) {
990 // If the for statement has a condition scope,
991 // emit the local variable declaration.
992 if (s.getConditionVariable())
993 emitDecl(*s.getConditionVariable());
994 // C99 6.8.5p2/p4: The first substatement is executed if the
995 // expression compares unequal to 0. The condition must be a
996 // scalar type.
997 condVal = evaluateExprAsBool(s.getCond());
998 } else {
999 condVal = cir::ConstantOp::create(b, loc, builder.getTrueAttr());
1000 }
1001 builder.createCondition(condVal);
1002 },
1003 /*bodyBuilder=*/
1004 [&](mlir::OpBuilder &b, mlir::Location loc) {
1005 // The scope of the for loop body is nested within the scope of the
1006 // for loop's init-statement and condition.
1007 RunCleanupsScope bodyScope(*this);
1008 if (emitStmt(s.getBody(), /*useCurrentScope=*/false).failed())
1009 loopRes = mlir::failure();
1010 emitStopPoint(&s);
1011 },
1012 /*stepBuilder=*/
1013 [&](mlir::OpBuilder &b, mlir::Location loc) {
1014 if (s.getInc())
1015 if (emitStmt(s.getInc(), /*useCurrentScope=*/true).failed())
1016 loopRes = mlir::failure();
1017 builder.createYield(loc);
1018 });
1019 return loopRes;
1020 };
1021
1022 auto res = mlir::success();
1023 auto scopeLoc = getLoc(s.getSourceRange());
1024 cir::ScopeOp::create(builder, scopeLoc, /*scopeBuilder=*/
1025 [&](mlir::OpBuilder &b, mlir::Location loc) {
1026 LexicalScope lexScope{*this, loc,
1027 builder.getInsertionBlock()};
1028 res = forStmtBuilder();
1029 });
1030
1031 if (res.failed())
1032 return res;
1033
1034 terminateStructuredRegionBody(forOp.getBody(), getLoc(s.getEndLoc()));
1035 return mlir::success();
1036}
1037
1038mlir::LogicalResult CIRGenFunction::emitDoStmt(const DoStmt &s) {
1039 cir::DoWhileOp doWhileOp;
1040
1041 // TODO: pass in array of attributes.
1042 auto doStmtBuilder = [&]() -> mlir::LogicalResult {
1043 mlir::LogicalResult loopRes = mlir::success();
1045
1046 doWhileOp = builder.createDoWhile(
1048 /*condBuilder=*/
1049 [&](mlir::OpBuilder &b, mlir::Location loc) {
1050 assert(!cir::MissingFeatures::createProfileWeightsForLoop());
1051 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
1052 // C99 6.8.5p2/p4: The first substatement is executed if the
1053 // expression compares unequal to 0. The condition must be a
1054 // scalar type.
1055 mlir::Value condVal = evaluateExprAsBool(s.getCond());
1056 builder.createCondition(condVal);
1057 },
1058 /*bodyBuilder=*/
1059 [&](mlir::OpBuilder &b, mlir::Location loc) {
1060 // The scope of the do-while loop body is a nested scope.
1061 RunCleanupsScope bodyScope(*this);
1062 if (emitStmt(s.getBody(), /*useCurrentScope=*/false).failed())
1063 loopRes = mlir::failure();
1064 emitStopPoint(&s);
1065 });
1066 return loopRes;
1067 };
1068
1069 mlir::LogicalResult res = mlir::success();
1070 mlir::Location scopeLoc = getLoc(s.getSourceRange());
1071 cir::ScopeOp::create(builder, scopeLoc, /*scopeBuilder=*/
1072 [&](mlir::OpBuilder &b, mlir::Location loc) {
1073 LexicalScope lexScope{*this, loc,
1074 builder.getInsertionBlock()};
1075 res = doStmtBuilder();
1076 });
1077
1078 if (res.failed())
1079 return res;
1080
1081 terminateStructuredRegionBody(doWhileOp.getBody(), getLoc(s.getEndLoc()));
1082 return mlir::success();
1083}
1084
1085mlir::LogicalResult CIRGenFunction::emitWhileStmt(const WhileStmt &s) {
1086 cir::WhileOp whileOp;
1087
1088 // TODO: pass in array of attributes.
1089 auto whileStmtBuilder = [&]() -> mlir::LogicalResult {
1090 mlir::LogicalResult loopRes = mlir::success();
1092
1093 whileOp = builder.createWhile(
1095 /*condBuilder=*/
1096 [&](mlir::OpBuilder &b, mlir::Location loc) {
1097 assert(!cir::MissingFeatures::createProfileWeightsForLoop());
1098 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
1099 mlir::Value condVal;
1100 // If the for statement has a condition scope,
1101 // emit the local variable declaration.
1102 if (s.getConditionVariable())
1103 emitDecl(*s.getConditionVariable());
1104 // C99 6.8.5p2/p4: The first substatement is executed if the
1105 // expression compares unequal to 0. The condition must be a
1106 // scalar type.
1107 condVal = evaluateExprAsBool(s.getCond());
1108 builder.createCondition(condVal);
1109 },
1110 /*bodyBuilder=*/
1111 [&](mlir::OpBuilder &b, mlir::Location loc) {
1112 // The scope of the while loop body is a nested scope.
1113 RunCleanupsScope bodyScope(*this);
1114 if (emitStmt(s.getBody(), /*useCurrentScope=*/false).failed())
1115 loopRes = mlir::failure();
1116 emitStopPoint(&s);
1117 });
1118 return loopRes;
1119 };
1120
1121 mlir::LogicalResult res = mlir::success();
1122 mlir::Location scopeLoc = getLoc(s.getSourceRange());
1123 cir::ScopeOp::create(builder, scopeLoc, /*scopeBuilder=*/
1124 [&](mlir::OpBuilder &b, mlir::Location loc) {
1125 LexicalScope lexScope{*this, loc,
1126 builder.getInsertionBlock()};
1127 res = whileStmtBuilder();
1128 });
1129
1130 if (res.failed())
1131 return res;
1132
1133 terminateStructuredRegionBody(whileOp.getBody(), getLoc(s.getEndLoc()));
1134 return mlir::success();
1135}
1136
1137mlir::LogicalResult CIRGenFunction::emitSwitchBody(const Stmt *s) {
1138 // It is rare but legal if the switch body is not a compound stmt. e.g.,
1139 //
1140 // switch(a)
1141 // while(...) {
1142 // case1
1143 // ...
1144 // case2
1145 // ...
1146 // }
1147 if (!isa<CompoundStmt>(s))
1148 return emitStmt(s, /*useCurrentScope=*/true);
1149
1151
1152 ArrayRef<Stmt *> body{compoundStmt->body_begin(), compoundStmt->body_end()};
1153
1154 mlir::Block *switchBlock = builder.getBlock();
1155
1156 // Any statements appearing before the first case statement are 'unassociated'
1157 // with anything. So we have to create them FIRST in their own block. After
1158 // that, the 'case' regions will take care of future ones.
1159 if (!body.empty() && !isa<SwitchCase>(body.front())) {
1160 builder.setInsertionPointToEnd(switchBlock);
1161 {
1162 // This is needed to handle cleanups in a compound statement before the
1163 // first case statement.
1164 RunCleanupsScope preCaseScope(*this);
1165 while (!body.empty() && !isa<SwitchCase>(body.front())) {
1166
1167 auto *c = body.front();
1168 if (mlir::failed(
1169 emitStmt(c, /*useCurrentScope=*/!isa<CompoundStmt>(c))))
1170 return mlir::failure();
1171
1172 body = body.drop_front();
1173 }
1174 }
1175
1176 // Now that we've emitted ALL of the statements, we can create a new block
1177 // for the actual case statements/etc to appear.
1178 mlir::Block *lastBlock = builder.getBlock();
1179 switchBlock = builder.createBlock(switchBlock->getParent());
1180 builder.setInsertionPointToEnd(lastBlock);
1181 cir::BrOp::create(builder, getLoc(s->getSourceRange()), switchBlock);
1182 }
1183
1184 for (auto *c : body) {
1185 if (auto *switchCase = dyn_cast<SwitchCase>(c)) {
1186 builder.setInsertionPointToEnd(switchBlock);
1187 // Reset insert point automatically, so that we can attach following
1188 // random stmt to the region of previous built case op to try to make
1189 // the being generated `cir.switch` to be in simple form.
1190 if (mlir::failed(
1191 emitSwitchCase(*switchCase, /*buildingTopLevelCase=*/true)))
1192 return mlir::failure();
1193
1194 continue;
1195 }
1196
1197 // Otherwise, just build the statements in the nearest case region.
1198 if (mlir::failed(emitStmt(c, /*useCurrentScope=*/!isa<CompoundStmt>(c))))
1199 return mlir::failure();
1200 }
1201
1202 return mlir::success();
1203}
1204
1206 // TODO: LLVM codegen does some early optimization to fold the condition and
1207 // only emit live cases. CIR should use MLIR to achieve similar things,
1208 // nothing to be done here.
1209 // if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue))...
1211
1212 SwitchOp swop;
1213 auto switchStmtBuilder = [&]() -> mlir::LogicalResult {
1214 if (s.getInit())
1215 if (emitStmt(s.getInit(), /*useCurrentScope=*/true).failed())
1216 return mlir::failure();
1217
1218 if (s.getConditionVariable())
1219 emitDecl(*s.getConditionVariable(), /*evaluateConditionDecl=*/true);
1220
1221 mlir::Value condV = emitScalarExpr(s.getCond());
1222
1223 // TODO: PGO and likelihood (e.g. PGO.haveRegionCounts())
1226 // TODO: if the switch has a condition wrapped by __builtin_unpredictable?
1228
1229 mlir::LogicalResult res = mlir::success();
1230 swop = SwitchOp::create(
1231 builder, getLoc(s.getBeginLoc()), condV,
1232 /*switchBuilder=*/
1233 [&](mlir::OpBuilder &b, mlir::Location loc, mlir::OperationState &os) {
1234 curLexScope->setAsSwitch();
1235
1236 condTypeStack.push_back(condV.getType());
1237
1238 res = emitSwitchBody(s.getBody());
1239
1240 condTypeStack.pop_back();
1241 });
1242
1243 return res;
1244 };
1245
1246 // The switch scope contains the full source range for SwitchStmt.
1247 mlir::Location scopeLoc = getLoc(s.getSourceRange());
1248 mlir::LogicalResult res = mlir::success();
1249 cir::ScopeOp::create(builder, scopeLoc, /*scopeBuilder=*/
1250 [&](mlir::OpBuilder &b, mlir::Location loc) {
1251 LexicalScope lexScope{*this, loc,
1252 builder.getInsertionBlock()};
1253 res = switchStmtBuilder();
1254 });
1255
1257 swop.collectCases(cases);
1258 for (auto caseOp : cases)
1259 terminateStructuredRegionBody(caseOp.getCaseRegion(), caseOp.getLoc());
1260 terminateStructuredRegionBody(swop.getBody(), swop.getLoc());
1261
1262 swop.setAllEnumCasesCovered(s.isAllEnumCasesCovered());
1263
1264 return res;
1265}
1266
1267void CIRGenFunction::emitReturnOfRValue(mlir::Location loc, RValue rv,
1268 QualType ty) {
1269 if (rv.isScalar()) {
1270 builder.createStore(loc, rv.getValue(), returnValue);
1271 } else if (rv.isAggregate()) {
1272 Address rvAddr = rv.getAggregateAddress();
1273 // If the aggregate is already in the return slot (e.g. a callee was
1274 // invoked through a ReturnValueSlot bound to returnValue), the copy is
1275 // a no-op. Calling emitAggregateCopy here would also incorrectly
1276 // require the type to have a trivial copy/move.
1277 if (rvAddr.getPointer() != returnValue.getPointer()) {
1278 LValue dest = makeAddrLValue(returnValue, ty);
1279 LValue src = makeAddrLValue(rvAddr, ty);
1281 }
1282 } else {
1283 cgm.errorNYI(loc, "emitReturnOfRValue: complex return type");
1284 }
1285
1286 // Classic codegen emits a branch through any cleanups before continuing to
1287 // a shared return block. Because CIR handles branching through cleanups
1288 // during the CFG flattening phase, we can just emit the return statement
1289 // directly.
1290 // TODO(cir): Eliminate this redundant load and the store above when we can.
1291 // Load the value from `__retval` and return it via the `cir.return` op.
1292 cir::AllocaOp retAlloca =
1293 mlir::cast<cir::AllocaOp>(fnRetAlloca->getDefiningOp());
1294 auto value = cir::LoadOp::create(builder, loc, retAlloca.getAllocaType(),
1295 *fnRetAlloca);
1296
1297 cir::ReturnOp::create(builder, loc, {value});
1298}
static mlir::LogicalResult emitStmtWithResult(CIRGenFunction &cgf, const Stmt *exprResult, AggValueSlot slot, Address *lastValue)
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.
Attr - This represents one attribute.
Definition Attr.h:46
Represents an attribute applied to a statement.
Definition Stmt.h:2213
Stmt * getSubStmt()
Definition Stmt.h:2249
ArrayRef< const Attr * > getAttrs() const
Definition Stmt.h:2245
BreakStmt - This represents a break.
Definition Stmt.h:3145
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)
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 emitDoStmt(const clang::DoStmt &s)
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)
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)
void emitAggregateCopy(LValue dest, LValue src, QualType eltTy, AggValueSlot::Overlap_t mayOverlap, bool isVolatile=false)
Emit an aggregate copy.
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 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)
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 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::Block * indirectGotoBlock
IndirectBranch - The first time an indirect goto is seen we create a block reserved for the indirect ...
mlir::Operation * curFn
The current function or global initializer that is generated code for.
mlir::LogicalResult emitAsmStmt(const clang::AsmStmt &s)
mlir::LogicalResult emitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &s)
mlir::LogicalResult emitOMPDistributeParallelForDirective(const OMPDistributeParallelForDirective &s)
mlir::LogicalResult emitOpenACCComputeConstruct(const OpenACCComputeConstruct &s)
mlir::LogicalResult emitOMPMasterTaskLoopSimdDirective(const OMPMasterTaskLoopSimdDirective &s)
mlir::LogicalResult emitSwitchBody(const clang::Stmt *s)
mlir::LogicalResult emitForStmt(const clang::ForStmt &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 emitOMPOrderedDirective(const OMPOrderedDirective &s)
mlir::LogicalResult emitOMPTargetParallelForSimdDirective(const OMPTargetParallelForSimdDirective &s)
mlir::LogicalResult emitOMPInterchangeDirective(const OMPInterchangeDirective &s)
mlir::LogicalResult emitOMPDispatchDirective(const OMPDispatchDirective &s)
mlir::LogicalResult emitCXXTryStmt(const clang::CXXTryStmt &s, cxxTryBodyEmitter &bodyCallback)
mlir::LogicalResult emitOMPParallelDirective(const OMPParallelDirective &s)
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)
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::LogicalResult emitOpenACCEnterDataConstruct(const OpenACCEnterDataConstruct &s)
mlir::LogicalResult emitOMPTargetExitDataDirective(const OMPTargetExitDataDirective &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 emitWhileStmt(const clang::WhileStmt &s)
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)
mlir::LogicalResult emitOMPParallelMasterTaskLoopDirective(const OMPParallelMasterTaskLoopDirective &s)
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)
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
mlir::Value getValue() const
Return the value of this scalar value.
Definition CIRGenValue.h:57
bool isScalar() const
Definition CIRGenValue.h:49
CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for statement, represented as 'for (ra...
Definition StmtCXX.h:135
DeclStmt * getBeginStmt()
Definition StmtCXX.h:163
DeclStmt * getEndStmt()
Definition StmtCXX.h:166
DeclStmt * getRangeStmt()
Definition StmtCXX.h:162
SourceLocation getEndLoc() const LLVM_READONLY
Definition StmtCXX.h:208
CaseStmt - Represent a case statement.
Definition Stmt.h:1930
Expr * getLHS()
Definition Stmt.h:2013
Expr * getRHS()
Definition Stmt.h:2025
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition Stmt.h:1750
body_range body()
Definition Stmt.h:1813
Stmt * body_back()
Definition Stmt.h:1818
ContinueStmt - This represents a continue.
Definition Stmt.h:3129
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1641
decl_range decls()
Definition Stmt.h:1689
DoStmt - This represents a 'do/while' stmt.
Definition Stmt.h:2842
SourceLocation getEndLoc() const
Definition Stmt.h:2879
This represents one expression.
Definition Expr.h:112
llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx) const
EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded integer.
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:3697
QualType getType() const
Definition Expr.h:144
ForStmt - This represents a 'for (init;cond;inc)' stmt.
Definition Stmt.h:2898
Stmt * getInit()
Definition Stmt.h:2913
Stmt * getBody()
Definition Stmt.h:2942
Expr * getInc()
Definition Stmt.h:2941
GotoStmt - This represents a direct goto.
Definition Stmt.h:2979
LabelDecl * getLabel() const
Definition Stmt.h:2992
IfStmt - This represents an if/then/else.
Definition Stmt.h:2269
Stmt * getThen()
Definition Stmt.h:2358
Stmt * getInit()
Definition Stmt.h:2419
Expr * getCond()
Definition Stmt.h:2346
bool isConstexpr() const
Definition Stmt.h:2462
bool isNegatedConsteval() const
Definition Stmt.h:2458
Stmt * getElse()
Definition Stmt.h:2367
bool isConsteval() const
Definition Stmt.h:2449
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:3018
Represents the declaration of a label.
Definition Decl.h:524
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.h:554
LabelStmt - Represents a label, which has a substatement.
Definition Stmt.h:2156
LabelDecl * getDecl() const
Definition Stmt.h:2174
bool isSideEntry() const
Definition Stmt.h:2203
Stmt * getSubStmt()
Definition Stmt.h:2178
SourceLocation getKwLoc() const
Definition Stmt.h:3092
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
A (possibly-)qualified type.
Definition TypeBase.h:937
The collection of all-type qualifiers we support.
Definition TypeBase.h:331
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition Stmt.h:3170
const VarDecl * getNRVOCandidate() const
Retrieve the variable that might be used for the named return value optimization.
Definition Stmt.h:3206
Expr * getRetValue()
Definition Stmt.h:3197
Stmt - This represents one statement.
Definition Stmt.h:86
@ NoStmtClass
Definition Stmt.h:89
StmtClass getStmtClass() const
Definition Stmt.h:1503
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:2519
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:2679
Expr * getCond()
Definition Stmt.h:2582
VarDecl * getConditionVariable()
Retrieve the variable declared in this "switch" statement, if any.
Definition Stmt.cpp:1186
Stmt * getInit()
Definition Stmt.h:2599
SourceLocation getBeginLoc() const
Definition Stmt.h:2683
bool isVoidType() const
Definition TypeBase.h:9050
bool isNRVOVariable() const
Determine whether this local variable can be used with the named return value optimization (NRVO).
Definition Decl.h:1525
WhileStmt - This represents a 'while' stmt.
Definition Stmt.h:2707
SourceLocation getEndLoc() const LLVM_READONLY
Definition Stmt.h:2821
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
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.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ Default
Set to the current date and time.
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Other
Other implicit parameter.
Definition Decl.h:1763
static bool aggValueSlotGC()
static bool loopInfoStack()
static bool emitCondLikelihoodViaExpectIntrinsic()
static bool constantFoldSwitchStatement()
static bool insertBuiltinUnpredictable()
static bool generateDebugInfo()
static bool incrementProfileCounter()
Represents a scope, including function bodies, compound statements, and the substatements of if/while...