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:
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:
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:
473 case Stmt::ReturnStmtClass:
475 case Stmt::AttributedStmtClass:
477 }
478
479 return mlir::success();
480}
481
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())
544 emitDecl(*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() &&
604 s.getNRVOCandidate()->isNRVOVariable()) {
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
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)
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 // From LLVM: if there are any cleanups between here and the loop-exit
918 // scope, create a block to stage a loop exit along.
919 // We probably already do the right thing because of ScopeOp, but make
920 // sure we handle all cases.
922
923 forOp = builder.createFor(
924 getLoc(s.getSourceRange()),
925 /*condBuilder=*/
926 [&](mlir::OpBuilder &b, mlir::Location loc) {
927 assert(!cir::MissingFeatures::createProfileWeightsForLoop());
928 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
929 mlir::Value condVal = evaluateExprAsBool(s.getCond());
930 builder.createCondition(condVal);
931 },
932 /*bodyBuilder=*/
933 [&](mlir::OpBuilder &b, mlir::Location loc) {
934 // https://en.cppreference.com/w/cpp/language/for
935 // In C++ the scope of the init-statement and the scope of
936 // statement are one and the same.
937 bool useCurrentScope = true;
938 if (emitStmt(s.getLoopVarStmt(), useCurrentScope).failed())
939 loopRes = mlir::failure();
940 if (emitStmt(s.getBody(), useCurrentScope).failed())
941 loopRes = mlir::failure();
942 emitStopPoint(&s);
943 },
944 /*stepBuilder=*/
945 [&](mlir::OpBuilder &b, mlir::Location loc) {
946 if (s.getInc())
947 if (emitStmt(s.getInc(), /*useCurrentScope=*/true).failed())
948 loopRes = mlir::failure();
949 builder.createYield(loc);
950 });
951 return loopRes;
952 };
953
954 mlir::LogicalResult res = mlir::success();
955 mlir::Location scopeLoc = getLoc(s.getSourceRange());
956 cir::ScopeOp::create(builder, scopeLoc, /*scopeBuilder=*/
957 [&](mlir::OpBuilder &b, mlir::Location loc) {
958 // Create a cleanup scope for the condition
959 // variable cleanups. Logical equivalent from
960 // LLVM codegn for LexicalScope
961 // ConditionScope(*this, S.getSourceRange())...
962 LexicalScope lexScope{*this, loc,
963 builder.getInsertionBlock()};
964 res = forStmtBuilder();
965 });
966
967 if (res.failed())
968 return res;
969
970 terminateStructuredRegionBody(forOp.getBody(), getLoc(s.getEndLoc()));
971 return mlir::success();
972}
973
974mlir::LogicalResult CIRGenFunction::emitForStmt(const ForStmt &s) {
975 cir::ForOp forOp;
976
977 // TODO: pass in an array of attributes.
978 auto forStmtBuilder = [&]() -> mlir::LogicalResult {
979 mlir::LogicalResult loopRes = mlir::success();
980 // Evaluate the first part before the loop.
981 if (s.getInit())
982 if (emitStmt(s.getInit(), /*useCurrentScope=*/true).failed())
983 return mlir::failure();
985 // In the classic codegen, if there are any cleanups between here and the
986 // loop-exit scope, a block is created to stage the loop exit. We probably
987 // already do the right thing because of ScopeOp, but we need more testing
988 // to be sure we handle all cases.
990
991 forOp = builder.createFor(
992 getLoc(s.getSourceRange()),
993 /*condBuilder=*/
994 [&](mlir::OpBuilder &b, mlir::Location loc) {
995 assert(!cir::MissingFeatures::createProfileWeightsForLoop());
996 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
997 mlir::Value condVal;
998 if (s.getCond()) {
999 // If the for statement has a condition scope,
1000 // emit the local variable declaration.
1001 if (s.getConditionVariable())
1002 emitDecl(*s.getConditionVariable());
1003 // C99 6.8.5p2/p4: The first substatement is executed if the
1004 // expression compares unequal to 0. The condition must be a
1005 // scalar type.
1006 condVal = evaluateExprAsBool(s.getCond());
1007 } else {
1008 condVal = cir::ConstantOp::create(b, loc, builder.getTrueAttr());
1009 }
1010 builder.createCondition(condVal);
1011 },
1012 /*bodyBuilder=*/
1013 [&](mlir::OpBuilder &b, mlir::Location loc) {
1014 // The scope of the for loop body is nested within the scope of the
1015 // for loop's init-statement and condition.
1016 if (emitStmt(s.getBody(), /*useCurrentScope=*/false).failed())
1017 loopRes = mlir::failure();
1018 emitStopPoint(&s);
1019 },
1020 /*stepBuilder=*/
1021 [&](mlir::OpBuilder &b, mlir::Location loc) {
1022 if (s.getInc())
1023 if (emitStmt(s.getInc(), /*useCurrentScope=*/true).failed())
1024 loopRes = mlir::failure();
1025 builder.createYield(loc);
1026 });
1027 return loopRes;
1028 };
1029
1030 auto res = mlir::success();
1031 auto scopeLoc = getLoc(s.getSourceRange());
1032 cir::ScopeOp::create(builder, scopeLoc, /*scopeBuilder=*/
1033 [&](mlir::OpBuilder &b, mlir::Location loc) {
1034 LexicalScope lexScope{*this, loc,
1035 builder.getInsertionBlock()};
1036 res = forStmtBuilder();
1037 });
1038
1039 if (res.failed())
1040 return res;
1041
1042 terminateStructuredRegionBody(forOp.getBody(), getLoc(s.getEndLoc()));
1043 return mlir::success();
1044}
1045
1046mlir::LogicalResult CIRGenFunction::emitDoStmt(const DoStmt &s) {
1047 cir::DoWhileOp doWhileOp;
1048
1049 // TODO: pass in array of attributes.
1050 auto doStmtBuilder = [&]() -> mlir::LogicalResult {
1051 mlir::LogicalResult loopRes = mlir::success();
1053 // From LLVM: if there are any cleanups between here and the loop-exit
1054 // scope, create a block to stage a loop exit along.
1055 // We probably already do the right thing because of ScopeOp, but make
1056 // sure we handle all cases.
1058
1059 doWhileOp = builder.createDoWhile(
1060 getLoc(s.getSourceRange()),
1061 /*condBuilder=*/
1062 [&](mlir::OpBuilder &b, mlir::Location loc) {
1063 assert(!cir::MissingFeatures::createProfileWeightsForLoop());
1064 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
1065 // C99 6.8.5p2/p4: The first substatement is executed if the
1066 // expression compares unequal to 0. The condition must be a
1067 // scalar type.
1068 mlir::Value condVal = evaluateExprAsBool(s.getCond());
1069 builder.createCondition(condVal);
1070 },
1071 /*bodyBuilder=*/
1072 [&](mlir::OpBuilder &b, mlir::Location loc) {
1073 // The scope of the do-while loop body is a nested scope.
1074 if (emitStmt(s.getBody(), /*useCurrentScope=*/false).failed())
1075 loopRes = mlir::failure();
1076 emitStopPoint(&s);
1077 });
1078 return loopRes;
1079 };
1080
1081 mlir::LogicalResult res = mlir::success();
1082 mlir::Location scopeLoc = getLoc(s.getSourceRange());
1083 cir::ScopeOp::create(builder, scopeLoc, /*scopeBuilder=*/
1084 [&](mlir::OpBuilder &b, mlir::Location loc) {
1085 LexicalScope lexScope{*this, loc,
1086 builder.getInsertionBlock()};
1087 res = doStmtBuilder();
1088 });
1089
1090 if (res.failed())
1091 return res;
1092
1093 terminateStructuredRegionBody(doWhileOp.getBody(), getLoc(s.getEndLoc()));
1094 return mlir::success();
1095}
1096
1097mlir::LogicalResult CIRGenFunction::emitWhileStmt(const WhileStmt &s) {
1098 cir::WhileOp whileOp;
1099
1100 // TODO: pass in array of attributes.
1101 auto whileStmtBuilder = [&]() -> mlir::LogicalResult {
1102 mlir::LogicalResult loopRes = mlir::success();
1104 // From LLVM: if there are any cleanups between here and the loop-exit
1105 // scope, create a block to stage a loop exit along.
1106 // We probably already do the right thing because of ScopeOp, but make
1107 // sure we handle all cases.
1109
1110 whileOp = builder.createWhile(
1111 getLoc(s.getSourceRange()),
1112 /*condBuilder=*/
1113 [&](mlir::OpBuilder &b, mlir::Location loc) {
1114 assert(!cir::MissingFeatures::createProfileWeightsForLoop());
1115 assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
1116 mlir::Value condVal;
1117 // If the for statement has a condition scope,
1118 // emit the local variable declaration.
1119 if (s.getConditionVariable())
1120 emitDecl(*s.getConditionVariable());
1121 // C99 6.8.5p2/p4: The first substatement is executed if the
1122 // expression compares unequal to 0. The condition must be a
1123 // scalar type.
1124 condVal = evaluateExprAsBool(s.getCond());
1125 builder.createCondition(condVal);
1126 },
1127 /*bodyBuilder=*/
1128 [&](mlir::OpBuilder &b, mlir::Location loc) {
1129 // The scope of the while loop body is a nested scope.
1130 if (emitStmt(s.getBody(), /*useCurrentScope=*/false).failed())
1131 loopRes = mlir::failure();
1132 emitStopPoint(&s);
1133 });
1134 return loopRes;
1135 };
1136
1137 mlir::LogicalResult res = mlir::success();
1138 mlir::Location scopeLoc = getLoc(s.getSourceRange());
1139 cir::ScopeOp::create(builder, scopeLoc, /*scopeBuilder=*/
1140 [&](mlir::OpBuilder &b, mlir::Location loc) {
1141 LexicalScope lexScope{*this, loc,
1142 builder.getInsertionBlock()};
1143 res = whileStmtBuilder();
1144 });
1145
1146 if (res.failed())
1147 return res;
1148
1149 terminateStructuredRegionBody(whileOp.getBody(), getLoc(s.getEndLoc()));
1150 return mlir::success();
1151}
1152
1153mlir::LogicalResult CIRGenFunction::emitSwitchBody(const Stmt *s) {
1154 // It is rare but legal if the switch body is not a compound stmt. e.g.,
1155 //
1156 // switch(a)
1157 // while(...) {
1158 // case1
1159 // ...
1160 // case2
1161 // ...
1162 // }
1163 if (!isa<CompoundStmt>(s))
1164 return emitStmt(s, /*useCurrentScope=*/true);
1165
1167
1168 ArrayRef<Stmt *> body{compoundStmt->body_begin(), compoundStmt->body_end()};
1169
1170 mlir::Block *switchBlock = builder.getBlock();
1171
1172 // Any statements appearing before the first case statement are 'unassociated'
1173 // with anything. So we have to create them FIRST in their own block. After
1174 // that, the 'case' regions will take care of future ones.
1175 if (!body.empty() && !isa<SwitchCase>(body.front())) {
1176 builder.setInsertionPointToEnd(switchBlock);
1177 while (!body.empty() && !isa<SwitchCase>(body.front())) {
1178
1179 auto *c = body.front();
1180 if (mlir::failed(emitStmt(c, /*useCurrentScope=*/!isa<CompoundStmt>(c))))
1181 return mlir::failure();
1182
1183 body = body.drop_front();
1184 }
1185
1186 // Now that we've emitted ALL of the statements, we can create a new block
1187 // for the actual case statements/etc to appear.
1188 mlir::Block *lastBlock = builder.getBlock();
1189 switchBlock = builder.createBlock(switchBlock->getParent());
1190 builder.setInsertionPointToEnd(lastBlock);
1191 cir::BrOp::create(builder, getLoc(s->getSourceRange()), switchBlock);
1192 }
1193
1194 for (auto *c : body) {
1195 if (auto *switchCase = dyn_cast<SwitchCase>(c)) {
1196 builder.setInsertionPointToEnd(switchBlock);
1197 // Reset insert point automatically, so that we can attach following
1198 // random stmt to the region of previous built case op to try to make
1199 // the being generated `cir.switch` to be in simple form.
1200 if (mlir::failed(
1201 emitSwitchCase(*switchCase, /*buildingTopLevelCase=*/true)))
1202 return mlir::failure();
1203
1204 continue;
1205 }
1206
1207 // Otherwise, just build the statements in the nearest case region.
1208 if (mlir::failed(emitStmt(c, /*useCurrentScope=*/!isa<CompoundStmt>(c))))
1209 return mlir::failure();
1210 }
1211
1212 return mlir::success();
1213}
1214
1216 // TODO: LLVM codegen does some early optimization to fold the condition and
1217 // only emit live cases. CIR should use MLIR to achieve similar things,
1218 // nothing to be done here.
1219 // if (ConstantFoldsToSimpleInteger(S.getCond(), ConstantCondValue))...
1221
1222 SwitchOp swop;
1223 auto switchStmtBuilder = [&]() -> mlir::LogicalResult {
1224 if (s.getInit())
1225 if (emitStmt(s.getInit(), /*useCurrentScope=*/true).failed())
1226 return mlir::failure();
1227
1228 if (s.getConditionVariable())
1229 emitDecl(*s.getConditionVariable(), /*evaluateConditionDecl=*/true);
1230
1231 mlir::Value condV = emitScalarExpr(s.getCond());
1232
1233 // TODO: PGO and likelihood (e.g. PGO.haveRegionCounts())
1236 // TODO: if the switch has a condition wrapped by __builtin_unpredictable?
1238
1239 mlir::LogicalResult res = mlir::success();
1240 swop = SwitchOp::create(
1241 builder, getLoc(s.getBeginLoc()), condV,
1242 /*switchBuilder=*/
1243 [&](mlir::OpBuilder &b, mlir::Location loc, mlir::OperationState &os) {
1244 curLexScope->setAsSwitch();
1245
1246 condTypeStack.push_back(condV.getType());
1247
1248 res = emitSwitchBody(s.getBody());
1249
1250 condTypeStack.pop_back();
1251 });
1252
1253 return res;
1254 };
1255
1256 // The switch scope contains the full source range for SwitchStmt.
1257 mlir::Location scopeLoc = getLoc(s.getSourceRange());
1258 mlir::LogicalResult res = mlir::success();
1259 cir::ScopeOp::create(builder, scopeLoc, /*scopeBuilder=*/
1260 [&](mlir::OpBuilder &b, mlir::Location loc) {
1261 LexicalScope lexScope{*this, loc,
1262 builder.getInsertionBlock()};
1263 res = switchStmtBuilder();
1264 });
1265
1267 swop.collectCases(cases);
1268 for (auto caseOp : cases)
1269 terminateStructuredRegionBody(caseOp.getCaseRegion(), caseOp.getLoc());
1270 terminateStructuredRegionBody(swop.getBody(), swop.getLoc());
1271
1272 swop.setAllEnumCasesCovered(s.isAllEnumCasesCovered());
1273
1274 return res;
1275}
1276
1277void CIRGenFunction::emitReturnOfRValue(mlir::Location loc, RValue rv,
1278 QualType ty) {
1279 if (rv.isScalar()) {
1280 builder.createStore(loc, rv.getValue(), returnValue);
1281 } else if (rv.isAggregate()) {
1282 Address rvAddr = rv.getAggregateAddress();
1283 // If the aggregate is already in the return slot (e.g. a callee was
1284 // invoked through a ReturnValueSlot bound to returnValue), the copy is
1285 // a no-op. Calling emitAggregateCopy here would also incorrectly
1286 // require the type to have a trivial copy/move.
1287 if (rvAddr.getPointer() != returnValue.getPointer()) {
1288 LValue dest = makeAddrLValue(returnValue, ty);
1289 LValue src = makeAddrLValue(rvAddr, ty);
1291 }
1292 } else {
1293 cgm.errorNYI(loc, "emitReturnOfRValue: complex return type");
1294 }
1295
1296 // Classic codegen emits a branch through any cleanups before continuing to
1297 // a shared return block. Because CIR handles branching through cleanups
1298 // during the CFG flattening phase, we can just emit the return statement
1299 // directly.
1300 // TODO(cir): Eliminate this redundant load and the store above when we can.
1301 // Load the value from `__retval` and return it via the `cir.return` op.
1302 cir::AllocaOp retAlloca =
1303 mlir::cast<cir::AllocaOp>(fnRetAlloca->getDefiningOp());
1304 auto value = cir::LoadOp::create(builder, loc, retAlloca.getAllocaType(),
1305 *fnRetAlloca);
1306
1307 cir::ReturnOp::create(builder, loc, {value});
1308}
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.
__device__ __2f16 b
__device__ __2f16 float __ockl_bool s
__device__ __2f16 float c
Attr - This represents one attribute.
Definition Attr.h:46
Represents an attribute applied to a statement.
Definition Stmt.h:2213
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
CaseStmt - Represent a case statement.
Definition Stmt.h:1930
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition Stmt.h:1750
ContinueStmt - This represents a continue.
Definition Stmt.h:3129
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1641
DoStmt - This represents a 'do/while' stmt.
Definition Stmt.h:2842
This represents one expression.
Definition Expr.h:112
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:3695
QualType getType() const
Definition Expr.h:144
ForStmt - This represents a 'for (init;cond;inc)' stmt.
Definition Stmt.h:2898
GotoStmt - This represents a direct goto.
Definition Stmt.h:2979
IfStmt - This represents an if/then/else.
Definition Stmt.h:2269
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
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
Stmt - This represents one statement.
Definition Stmt.h:86
@ NoStmtClass
Definition Stmt.h:89
SwitchStmt - This represents a 'switch' stmt.
Definition Stmt.h:2519
bool isVoidType() const
Definition TypeBase.h:9048
WhileStmt - This represents a 'while' stmt.
Definition Stmt.h:2707
@ 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
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 loopSpecificCleanupHandling()
static bool incrementProfileCounter()
Represents a scope, including function bodies, compound statements, and the substatements of if/while...