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// Internal per-function state used for AST-to-ClangIR code gen
10//
11//===----------------------------------------------------------------------===//
12
13#include "CIRGenFunction.h"
14
15#include "CIRGenCXXABI.h"
16#include "CIRGenCall.h"
17#include "CIRGenConstantEmitter.h"
18#include "CIRGenValue.h"
19#include "mlir/IR/Location.h"
20#include "clang/AST/Attr.h"
21#include "clang/AST/ExprCXX.h"
22#include "clang/AST/GlobalDecl.h"
23#include "clang/CIR/Dialect/IR/CIRDialect.h"
24#include "clang/CIR/MissingFeatures.h"
25#include "clang/CodeGenUtils/ClassUtils.h"
26#include "clang/CodeGenUtils/FunctionUtils.h"
27#include "llvm/ADT/ScopeExit.h"
28#include "llvm/IR/FPEnv.h"
29
30#include <cassert>
31
32namespace clang::CIRGen {
33
34/// Does the statement tree rooted at \p s contain a label, switch, or indirect
35/// goto that could bypass a local's initialization? A coarse stand-in for
36/// classic CodeGen's per-decl bypass analysis (PR28267).
37static bool functionMightHaveBypass(const Stmt *s) {
38 if (!s)
39 return false;
40 if (isa<LabelStmt, SwitchStmt, IndirectGotoStmt>(Val: s))
41 return true;
42 for (const Stmt *child : s->children())
43 if (functionMightHaveBypass(s: child))
44 return true;
45 return false;
46}
47
48CIRGenFunction::CIRGenFunction(CIRGenModule &cgm, CIRGenBuilderTy &builder,
49 bool suppressNewContext)
50 : CIRGenTypeCache(cgm), cgm{cgm}, builder(builder),
51 curFPFeatures(cgm.getLangOpts()) {
52 ehStack.setCGF(this);
53 shouldEmitLifetimeMarkers = CodeGenUtils::shouldEmitLifetimeMarkers(
54 CGOpts: cgm.getCodeGenOpts(), LangOpts: getContext().getLangOpts());
55 setFastMathFlags(curFPFeatures);
56}
57
58CIRGenFunction::~CIRGenFunction() {}
59
60void CIRGenFunction::setFastMathFlags(FPOptions fpFeatures) {
61 cir::FastMathFlags flags = cir::FastMathFlags::none;
62 if (fpFeatures.getAllowFPReassociate())
63 flags = flags | cir::FastMathFlags::reassoc;
64 if (fpFeatures.getNoHonorNaNs())
65 flags = flags | cir::FastMathFlags::nnan;
66 if (fpFeatures.getNoHonorInfs())
67 flags = flags | cir::FastMathFlags::ninf;
68 if (fpFeatures.getNoSignedZero())
69 flags = flags | cir::FastMathFlags::nsz;
70 if (fpFeatures.getAllowReciprocal())
71 flags = flags | cir::FastMathFlags::arcp;
72 if (fpFeatures.getAllowApproxFunc())
73 flags = flags | cir::FastMathFlags::afn;
74 if (fpFeatures.allowFPContractAcrossStatement())
75 flags = flags | cir::FastMathFlags::contract;
76 builder.setFastMathFlags(flags);
77}
78
79// This is copied from clang/lib/CodeGen/CodeGenFunction.cpp
80cir::TypeEvaluationKind CIRGenFunction::getEvaluationKind(QualType type) {
81 type = type.getCanonicalType();
82 while (true) {
83 switch (type->getTypeClass()) {
84#define TYPE(name, parent)
85#define ABSTRACT_TYPE(name, parent)
86#define NON_CANONICAL_TYPE(name, parent) case Type::name:
87#define DEPENDENT_TYPE(name, parent) case Type::name:
88#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name:
89#include "clang/AST/TypeNodes.inc"
90 llvm_unreachable("non-canonical or dependent type in IR-generation");
91
92 case Type::Auto:
93 case Type::DeducedTemplateSpecialization:
94 llvm_unreachable("undeduced type in IR-generation");
95
96 // Various scalar types.
97 case Type::Builtin:
98 case Type::Pointer:
99 case Type::BlockPointer:
100 case Type::LValueReference:
101 case Type::RValueReference:
102 case Type::MemberPointer:
103 case Type::Vector:
104 case Type::ExtVector:
105 case Type::ConstantMatrix:
106 case Type::FunctionProto:
107 case Type::FunctionNoProto:
108 case Type::Enum:
109 case Type::ObjCObjectPointer:
110 case Type::Pipe:
111 case Type::BitInt:
112 case Type::OverflowBehavior:
113 case Type::HLSLAttributedResource:
114 case Type::HLSLInlineSpirv:
115 return cir::TEK_Scalar;
116
117 // Complexes.
118 case Type::Complex:
119 return cir::TEK_Complex;
120
121 // Arrays, records, and Objective-C objects.
122 case Type::ConstantArray:
123 case Type::IncompleteArray:
124 case Type::VariableArray:
125 case Type::Record:
126 case Type::ObjCObject:
127 case Type::ObjCInterface:
128 case Type::ArrayParameter:
129 return cir::TEK_Aggregate;
130
131 // We operate on atomic values according to their underlying type.
132 case Type::Atomic:
133 type = cast<AtomicType>(Val&: type)->getValueType();
134 continue;
135 }
136 llvm_unreachable("unknown type kind!");
137 }
138}
139
140mlir::Type CIRGenFunction::convertTypeForMem(QualType t) {
141 return cgm.getTypes().convertTypeForMem(t);
142}
143
144mlir::Type CIRGenFunction::convertType(QualType t) {
145 return cgm.getTypes().convertType(type: t);
146}
147
148mlir::Location CIRGenFunction::getLoc(SourceLocation srcLoc) {
149 // Some AST nodes might contain invalid source locations (e.g.
150 // CXXDefaultArgExpr), workaround that to still get something out.
151 if (srcLoc.isValid()) {
152 const SourceManager &sm = getContext().getSourceManager();
153 PresumedLoc pLoc = sm.getPresumedLoc(Loc: srcLoc);
154 StringRef filename = pLoc.getFilename();
155 return mlir::FileLineColLoc::get(filename: builder.getStringAttr(bytes: filename),
156 line: pLoc.getLine(), column: pLoc.getColumn());
157 }
158 // We expect to have a currSrcLoc set, but it isn't critical for the
159 // correctness of compilation, so in non-assert builds we fallback on using an
160 // unknown location.
161 if (currSrcLoc && currSrcLoc->isValid())
162 return getLoc(srcLoc: *currSrcLoc);
163 // We're brave, but time to give up.
164 return builder.getUnknownLoc();
165}
166
167mlir::Location CIRGenFunction::getLoc(SourceRange srcLoc) {
168 // Some AST nodes might contain invalid source locations (e.g.
169 // CXXDefaultArgExpr), workaround that to still get something out.
170
171 // SourceRange is only valid if BOTH are valid, so get the fused location from
172 // the 2-mlir::Location version of this.
173 if (srcLoc.isValid())
174 return getLoc(lhs: getLoc(srcLoc: srcLoc.getBegin()), rhs: getLoc(srcLoc: srcLoc.getEnd()));
175
176 // If only ONE of the two is valid, try our hardest to get this right.
177 if (srcLoc.getBegin().isValid())
178 return getLoc(srcLoc: srcLoc.getBegin());
179 if (srcLoc.getEnd().isValid())
180 return getLoc(srcLoc: srcLoc.getEnd());
181
182 // We expect to have a currSrcLoc set, but it isn't critical for the
183 // correctness of compilation, so in non-assert builds we fallback on using an
184 // unknown location.
185 if (currSrcLoc &&
186 (currSrcLoc->getBegin().isValid() || currSrcLoc->getEnd().isValid()))
187 return getLoc(srcLoc: *currSrcLoc);
188 // We're brave, but time to give up.
189 return builder.getUnknownLoc();
190}
191
192mlir::Location CIRGenFunction::getLoc(mlir::Location lhs, mlir::Location rhs) {
193 SmallVector<mlir::Location, 2> locs = {lhs, rhs};
194 mlir::Attribute metadata;
195 return mlir::FusedLoc::get(locs, metadata, context: &getMLIRContext());
196}
197
198bool CIRGenFunction::containsLabel(const Stmt *s, bool ignoreCaseStmts) {
199 // Null statement, not a label!
200 if (!s)
201 return false;
202
203 // If this is a label, we have to emit the code, consider something like:
204 // if (0) { ... foo: bar(); } goto foo;
205 //
206 // TODO: If anyone cared, we could track __label__'s, since we know that you
207 // can't jump to one from outside their declared region.
208 if (isa<LabelStmt>(Val: s))
209 return true;
210
211 // If this is a case/default statement, and we haven't seen a switch, we
212 // have to emit the code.
213 if (isa<SwitchCase>(Val: s) && !ignoreCaseStmts)
214 return true;
215
216 // If this is a switch statement, we want to ignore case statements when we
217 // recursively process the sub-statements of the switch. If we haven't
218 // encountered a switch statement, we treat case statements like labels, but
219 // if we are processing a switch statement, case statements are expected.
220 if (isa<SwitchStmt>(Val: s))
221 ignoreCaseStmts = true;
222
223 // Scan subexpressions for verboten labels.
224 return std::any_of(first: s->child_begin(), last: s->child_end(),
225 pred: [=](const Stmt *subStmt) {
226 return containsLabel(s: subStmt, ignoreCaseStmts);
227 });
228}
229
230/// If the specified expression does not fold to a constant, or if it does but
231/// contains a label, return false. If it constant folds return true and set
232/// the boolean result in Result.
233bool CIRGenFunction::constantFoldsToBool(const Expr *cond, bool &resultBool,
234 bool allowLabels) {
235 llvm::APSInt resultInt;
236 if (!constantFoldsToSimpleInteger(cond, resultInt, allowLabels))
237 return false;
238
239 resultBool = resultInt.getBoolValue();
240 return true;
241}
242
243/// If the specified expression does not fold to a constant, or if it does
244/// fold but contains a label, return false. If it constant folds, return
245/// true and set the folded value.
246bool CIRGenFunction::constantFoldsToSimpleInteger(const Expr *cond,
247 llvm::APSInt &resultInt,
248 bool allowLabels) {
249 // FIXME: Rename and handle conversion of other evaluatable things
250 // to bool.
251 Expr::EvalResult result;
252 if (!cond->EvaluateAsInt(Result&: result, Ctx: getContext()))
253 return false; // Not foldable, not integer or not fully evaluatable.
254
255 llvm::APSInt intValue = result.Val.getInt();
256 if (!allowLabels && containsLabel(s: cond))
257 return false; // Contains a label.
258
259 resultInt = intValue;
260 return true;
261}
262
263void CIRGenFunction::emitAndUpdateRetAlloca(QualType type, mlir::Location loc,
264 CharUnits alignment) {
265 if (!type->isVoidType()) {
266 Address allocaAddr = Address::invalid();
267 returnValue = createMemTemp(t: type, align: alignment, loc, name: "__retval", alloca: &allocaAddr);
268 fnRetAlloca = allocaAddr.getPointer();
269 }
270}
271
272void CIRGenFunction::declare(mlir::Value addrVal, const Decl *var, QualType ty,
273 mlir::Location loc, CharUnits alignment,
274 bool isParam) {
275 assert(isa<NamedDecl>(var) && "Needs a named decl");
276 assert(!symbolTable.count(var) && "not supposed to be available just yet");
277
278 Address addr(addrVal, alignment);
279 cir::AllocaOp allocaOp = addr.getUnderlyingAllocaOp();
280 assert(allocaOp && "expected cir::AllocaOp");
281
282 if (isParam)
283 allocaOp.setInitAttr(mlir::UnitAttr::get(context: &getMLIRContext()));
284 if (ty->isReferenceType() || ty.isConstQualified())
285 allocaOp.setConstantAttr(mlir::UnitAttr::get(context: &getMLIRContext()));
286
287 symbolTable.insert(Key: var, Val: addrVal);
288}
289
290void CIRGenFunction::emitVarAnnotations(const VarDecl *d, mlir::Value addr) {
291 assert(d->hasAttr<AnnotateAttr>() && "no annotate attribute");
292 for (const auto *attr : d->specific_attrs<AnnotateAttr>())
293 cir::VarAnnotationOp::create(builder, location: getLoc(srcLoc: d->getLocation()), addr,
294 annotation: cgm.emitAnnotateAttr(aa: attr));
295}
296
297Address CIRGenFunction::emitFieldAnnotations(const FieldDecl *field,
298 Address addr) {
299 assert(field->hasAttr<AnnotateAttr>() && "no annotate attribute");
300 mlir::Value ptr = addr.getPointer();
301 for (const auto *attr : field->specific_attrs<AnnotateAttr>())
302 ptr = cir::PtrAnnotationOp::create(builder, location: getLoc(srcLoc: field->getLocation()),
303 addr: ptr, annotation: cgm.emitAnnotateAttr(aa: attr));
304 return addr.withPointer(newPtr: ptr);
305}
306
307void CIRGenFunction::LexicalScope::cleanup() {
308 CIRGenBuilderTy &builder = cgf.builder;
309 LexicalScope *localScope = cgf.curLexScope;
310
311 // Process all return blocks — emit cir.return ops.
312 // TODO(cir): Handle returning from a switch statement through a cleanup
313 // block. We can't simply jump to the cleanup block, because the cleanup block
314 // is not part of the case region. Either reemit all cleanups in the return
315 // block or wait for MLIR structured control flow to support early exits.
316 llvm::SmallVector<mlir::Block *> retBlocks;
317 for (mlir::Block *retBlock : localScope->getRetBlocks()) {
318 mlir::OpBuilder::InsertionGuard guard(builder);
319 builder.setInsertionPointToEnd(retBlock);
320 retBlocks.push_back(Elt: retBlock);
321 mlir::Location retLoc = localScope->getRetLoc(b: retBlock);
322 emitReturn(loc: retLoc);
323 }
324
325 // Pop cleanup scopes from the EH stack. In CIR, this emits cleanup code
326 // into the cleanup regions of cir.cleanup.scope ops — no CFG-level cleanup
327 // blocks or branches are needed.
328 if (performCleanup) {
329 assert(!cir::MissingFeatures::generateDebugInfo());
330 forceCleanup();
331 }
332
333 mlir::Block *curBlock = builder.getBlock();
334 if (isGlobalInit() && !curBlock)
335 return;
336 if (curBlock->mightHaveTerminator() && curBlock->getTerminator())
337 return;
338
339 // Get rid of any empty block at the end of the scope. An empty non-entry
340 // block is created when a terminator (return/break/continue) is followed
341 // by unreachable code.
342 bool isEntryBlock = builder.getInsertionBlock()->isEntryBlock();
343 if (!isEntryBlock && curBlock->empty()) {
344 curBlock->erase();
345 for (mlir::Block *retBlock : retBlocks) {
346 if (retBlock->getUses().empty())
347 retBlock->erase();
348 }
349 return;
350 }
351
352 if (localScope->depth == 0) {
353 // Reached the end of the function.
354 if (localScope->getRetBlocks().size() == 1) {
355 mlir::Block *retBlock = localScope->getRetBlocks()[0];
356 mlir::Location retLoc = localScope->getRetLoc(b: retBlock);
357 if (retBlock->getUses().empty()) {
358 retBlock->erase();
359 } else {
360 cir::BrOp::create(builder, location: retLoc, dest: retBlock);
361 return;
362 }
363 }
364 emitImplicitReturn();
365 return;
366 }
367
368 // End of any local scope != function.
369 // Ternary ops have to deal with matching arms for yielding types
370 // and do return a value, it must do its own cir.yield insertion.
371 if (!localScope->isTernary() && !curBlock->mightHaveTerminator()) {
372 !retVal ? cir::YieldOp::create(builder, location: localScope->endLoc)
373 : cir::YieldOp::create(builder, location: localScope->endLoc, args: retVal);
374 }
375}
376
377cir::ReturnOp CIRGenFunction::LexicalScope::emitReturn(mlir::Location loc) {
378 CIRGenBuilderTy &builder = cgf.getBuilder();
379
380 auto fn = dyn_cast<cir::FuncOp>(Val: cgf.curFn);
381 assert(fn && "emitReturn from non-function");
382
383 if (!fn.getFunctionType().hasVoidReturn()) {
384 // Load the value from `__retval` and return it via the `cir.return` op.
385 auto value = cir::LoadOp::create(
386 builder, location: loc, result: fn.getFunctionType().getReturnType(), addr: *cgf.fnRetAlloca);
387 return cir::ReturnOp::create(builder, location: loc,
388 input: llvm::ArrayRef(value.getResult()));
389 }
390 return cir::ReturnOp::create(builder, location: loc);
391}
392
393// This is copied from CodeGenModule::MayDropFunctionReturn. This is a
394// candidate for sharing between CIRGen and CodeGen.
395static bool mayDropFunctionReturn(const ASTContext &astContext,
396 QualType returnType) {
397 // We can't just discard the return value for a record type with a complex
398 // destructor or a non-trivially copyable type.
399 if (const auto *classDecl = returnType->getAsCXXRecordDecl())
400 return classDecl->hasTrivialDestructor();
401 return returnType.isTriviallyCopyableType(Context: astContext);
402}
403
404static bool previousOpIsNonYieldingCleanup(mlir::Block *block) {
405 if (block->empty())
406 return false;
407 mlir::Operation *op = &block->back();
408 auto cleanupScopeOp = mlir::dyn_cast<cir::CleanupScopeOp>(Val: op);
409 if (!cleanupScopeOp)
410 return false;
411
412 // Check whether the body region of the cleanup scope exits via cir.yield.
413 // Exits via cir.return or cir.goto do not fall through to the operation
414 // following the cleanup scope, and exits via break, continue, and resume
415 // are not expected here.
416 for (mlir::Block &bodyBlock : cleanupScopeOp.getBodyRegion()) {
417 if (bodyBlock.mightHaveTerminator()) {
418 if (mlir::isa<cir::YieldOp>(Val: bodyBlock.getTerminator()))
419 return false;
420 assert(!mlir::isa<cir::BreakOp>(bodyBlock.getTerminator()) &&
421 !mlir::isa<cir::ContinueOp>(bodyBlock.getTerminator()) &&
422 !mlir::isa<cir::ResumeOp>(bodyBlock.getTerminator()));
423 }
424 }
425 return true;
426}
427
428void CIRGenFunction::LexicalScope::emitImplicitReturn() {
429 CIRGenBuilderTy &builder = cgf.getBuilder();
430 LexicalScope *localScope = cgf.curLexScope;
431
432 // Synthesized functions (e.g. SYCL kernel caller entry points) have no
433 // FunctionDecl; the non-void flow-off-the-end handling below is guarded on
434 // fd.
435 const auto *fd = dyn_cast_or_null<clang::FunctionDecl>(Val: cgf.curGD.getDecl());
436
437 // In C++, flowing off the end of a non-void function is always undefined
438 // behavior. In C, flowing off the end of a non-void function is undefined
439 // behavior only if the non-existent return value is used by the caller.
440 // That influences whether the terminating op is trap, unreachable, or
441 // return.
442 if (fd && cgf.getLangOpts().CPlusPlus && !fd->hasImplicitReturnZero() &&
443 !cgf.sawAsmBlock && !fd->getReturnType()->isVoidType() &&
444 builder.getInsertionBlock() &&
445 !previousOpIsNonYieldingCleanup(block: builder.getInsertionBlock())) {
446 bool shouldEmitUnreachable =
447 cgf.cgm.getCodeGenOpts().StrictReturn ||
448 !mayDropFunctionReturn(astContext: fd->getASTContext(), returnType: fd->getReturnType());
449
450 if (shouldEmitUnreachable) {
451 assert(!cir::MissingFeatures::sanitizers());
452 if (cgf.cgm.getCodeGenOpts().OptimizationLevel == 0)
453 cir::TrapOp::create(builder, location: localScope->endLoc, /*implicit=*/true);
454 else
455 cir::UnreachableOp::create(builder, location: localScope->endLoc,
456 /*implicit=*/true);
457 builder.clearInsertionPoint();
458 return;
459 }
460 }
461
462 emitReturn(loc: localScope->endLoc).setImplicit(true);
463}
464
465cir::TryOp CIRGenFunction::LexicalScope::getClosestTryParent() {
466 LexicalScope *scope = this;
467 while (scope) {
468 if (scope->isTry())
469 return scope->getTry();
470 scope = scope->parentScope;
471 }
472 return nullptr;
473}
474
475/// An argument came in as a promoted argument; demote it back to its
476/// declared type.
477static mlir::Value emitArgumentDemotion(CIRGenFunction &cgf, const VarDecl *var,
478 mlir::Value value) {
479 mlir::Type ty = cgf.convertType(t: var->getType());
480
481 // This can happen with promotions that actually don't change the
482 // underlying type, like the enum promotions.
483 if (value.getType() == ty)
484 return value;
485
486 assert((mlir::isa<cir::IntType>(ty) || cir::isAnyFloatingPointType(ty)) &&
487 "unexpected promotion type");
488
489 if (mlir::isa<cir::IntType>(Val: ty))
490 return cgf.getBuilder().CIRBaseBuilderTy::createIntCast(src: value, newTy: ty);
491
492 return cgf.getBuilder().createFloatingCast(v: value, destType: ty);
493}
494
495void CIRGenFunction::emitFunctionProlog(const FunctionArgList &args,
496 mlir::Block *entryBB,
497 const FunctionDecl *fd,
498 SourceLocation bodyBeginLoc) {
499 // Naked functions don't have prologues.
500 if (fd && fd->hasAttr<NakedAttr>()) {
501 cgm.errorNYI(bodyBeginLoc, "naked function decl");
502 }
503
504 // If this is an implicit-return-zero function, initialize the return value.
505 if (fd && fd->hasImplicitReturnZero()) {
506 mlir::Type retTy = convertType(t: fd->getReturnType().getUnqualifiedType());
507 mlir::Location loc = getLoc(srcLoc: bodyBeginLoc);
508 mlir::Value zero = builder.getNullValue(ty: retTy, loc);
509 builder.CIRBaseBuilderTy::createStore(loc, val: zero, dst: returnValue.getPointer());
510 }
511
512 // Declare all the function arguments in the symbol table.
513 for (const auto nameValue : llvm::zip(t: args, u: entryBB->getArguments())) {
514 const VarDecl *paramVar = std::get<0>(t: nameValue);
515 mlir::Value paramVal = std::get<1>(t: nameValue);
516 CharUnits alignment = getContext().getDeclAlign(D: paramVar);
517 mlir::Location paramLoc = getLoc(srcLoc: paramVar->getSourceRange());
518 paramVal.setLoc(paramLoc);
519
520 mlir::Value addrVal =
521 emitAlloca(name: cast<NamedDecl>(Val: paramVar)->getName(),
522 ty: convertType(t: paramVar->getType()), loc: paramLoc, alignment,
523 /*insertIntoFnEntryBlock=*/true);
524
525 mlir::ptr::MemorySpaceAttrInterface destAddrSpace =
526 cir::toCIRAddressSpaceAttr(ctx&: getMLIRContext(),
527 langAS: paramVar->getType().getAddressSpace());
528 Address addr = Address(addrVal, alignment);
529 addr = maybeCastStackAddressSpace(alloca: addr, destAddrSpace);
530
531 declare(addrVal: addr.getPointer(), var: paramVar, ty: paramVar->getType(), loc: paramLoc,
532 alignment,
533 /*isParam=*/true);
534
535 setAddrOfLocalVar(vd: paramVar, addr);
536
537 bool isPromoted = isa<ParmVarDecl>(Val: paramVar) &&
538 cast<ParmVarDecl>(Val: paramVar)->isKNRPromoted();
539 assert(!cir::MissingFeatures::constructABIArgDirectExtend());
540 if (isPromoted)
541 paramVal = emitArgumentDemotion(cgf&: *this, var: paramVar, value: paramVal);
542
543 // Location of the store to the param storage tracked as beginning of
544 // the function body.
545 mlir::Location fnBodyBegin = getLoc(srcLoc: bodyBeginLoc);
546 builder.CIRBaseBuilderTy::createStore(loc: fnBodyBegin, val: paramVal,
547 dst: addr.getPointer());
548
549 if (paramVar->hasAttr<AnnotateAttr>())
550 emitVarAnnotations(d: paramVar, addr: addr.getPointer());
551 }
552 assert(builder.getInsertionBlock() && "Should be valid");
553}
554
555void CIRGenFunction::startFunction(GlobalDecl gd, QualType returnType,
556 cir::FuncOp fn, cir::FuncType funcType,
557 FunctionArgList args, SourceLocation loc,
558 SourceLocation startLoc) {
559 assert(!curFn &&
560 "CIRGenFunction can only be used for one function at a time");
561
562 curFn = fn;
563
564 const Decl *d = gd.getDecl();
565
566 didCallStackSave = false;
567 curCodeDecl = d;
568 const auto *fd = dyn_cast_or_null<FunctionDecl>(Val: d);
569 curFuncDecl = (d ? d->getNonClosureContext() : nullptr);
570
571 // Recursion is disallowed for C++ main, OpenCL, HLSL, SYCL device code and
572 // CUDA/HIP kernels.
573 if (fd &&
574 ((getLangOpts().CPlusPlus && fd->isMain()) || getLangOpts().OpenCL ||
575 getLangOpts().HLSL || getLangOpts().SYCLIsDevice ||
576 (getLangOpts().CUDA && fd->hasAttr<CUDAGlobalAttr>())))
577 fn->setAttr(name: cir::CIRDialect::getNoRecurseAttrName(),
578 value: mlir::UnitAttr::get(context: fn.getContext()));
579
580 // This is an artifact of the legacy handling of constrained floating-point
581 // modes. The rounding mode and exception behavior tracked in
582 // clang::LangOptions don't correspond directly to the representation we
583 // use in CIR, but the CIR settings can be derived from them. We track the
584 // default state using the legacy settings because it keeps the tracking in
585 // sync with classic codegen.
586 llvm::RoundingMode rm = getLangOpts().getDefaultRoundingMode();
587 LangOptions::FPExceptionModeKind eb = getLangOpts().getDefaultExceptionMode();
588 builder.setDefaultConstrainedRounding(rm);
589 builder.setDefaultConstrainedExcept(eb);
590 builder.setIsFPConstrained(false);
591 if ((fd && (fd->UsesFPIntrin() || fd->hasAttr<StrictFPAttr>())) ||
592 (!fd && (eb != LangOptions::FPExceptionModeKind::FPE_Ignore ||
593 rm != llvm::RoundingMode::NearestTiesToEven))) {
594 builder.setIsFPConstrained(true);
595 fn->setAttr(name: cir::CIRDialect::getStrictFPAttrName(),
596 value: mlir::UnitAttr::get(context: fn.getContext()));
597 }
598 mlir::Block *entryBB = &fn.getBlocks().front();
599 builder.setInsertionPointToStart(entryBB);
600
601 // When the current function is not void, create an address to store the
602 // result value.
603 if (!returnType->isVoidType()) {
604 // Determine the function body end location. A function without a body
605 // uses its declaration's location, and a null fd uses loc.
606 SourceLocation bodyEndLoc = loc;
607 if (fd) {
608 if (Stmt *body = fd->getBody())
609 bodyEndLoc = body->getEndLoc();
610 else
611 bodyEndLoc = fd->getLocation();
612 }
613 emitAndUpdateRetAlloca(type: returnType, loc: getLoc(srcLoc: bodyEndLoc),
614 alignment: getContext().getTypeAlignInChars(T: returnType));
615 }
616
617 // Wrap the rest of the function in a filter try when this declaration has
618 // a dynamic exception specification. Parameter cleanups are pushed after
619 // this so they nest inside the specification, matching classic codegen.
620 emitStartEHSpec(d);
621 prologueCleanupDepth = ehStack.stable_begin();
622
623 // Determine the function body begin location for the prolog. A function
624 // without a body uses its declaration's location, and a null fd uses
625 // startLoc.
626 SourceLocation bodyBeginLoc = startLoc;
627 if (fd) {
628 if (Stmt *body = fd->getBody())
629 bodyBeginLoc = body->getBeginLoc();
630 else
631 bodyBeginLoc = fd->getLocation();
632 }
633
634 emitFunctionProlog(args, entryBB, fd, bodyBeginLoc);
635
636 if (const auto *md = dyn_cast_if_present<CXXMethodDecl>(Val: d);
637 md && !md->isStatic()) {
638 bool isInLambda =
639 md->getParent()->isLambda() && md->getOverloadedOperator() == OO_Call;
640
641 if (md->isImplicitObjectMemberFunction())
642 cgm.getCXXABI().emitInstanceFunctionProlog(loc, cgf&: *this);
643
644 if (isInLambda) {
645 // We're in a lambda; figure out the captures.
646 auto fn = cast<cir::FuncOp>(Val: curFn);
647 fn.setLambda(true);
648
649 md->getParent()->getCaptureFields(Captures&: lambdaCaptureFields,
650 ThisCapture&: lambdaThisCaptureField);
651 if (lambdaThisCaptureField) {
652 // If the lambda captures the object referred to by '*this' - either by
653 // value or by reference, make sure CXXThisValue points to the correct
654 // object.
655
656 // Get the lvalue for the field (which is a copy of the enclosing object
657 // or contains the address of the enclosing object).
658 LValue thisFieldLValue =
659 emitLValueForLambdaField(field: lambdaThisCaptureField);
660 if (!lambdaThisCaptureField->getType()->isPointerType()) {
661 // If the enclosing object was captured by value, just use its
662 // address. Sign this pointer.
663 cxxThisValue = thisFieldLValue.getPointer();
664 } else {
665 // Load the lvalue pointed to by the field, since '*this' was captured
666 // by reference.
667 cxxThisValue =
668 emitLoadOfLValue(lv: thisFieldLValue, loc: SourceLocation()).getValue();
669 }
670 }
671 for (auto *fd : md->getParent()->fields()) {
672 if (fd->hasCapturedVLAType()) {
673 mlir::Value exprArg =
674 emitLoadOfLValue(lv: emitLValueForLambdaField(field: fd), loc: SourceLocation())
675 .getValue();
676 const VariableArrayType *vat = fd->getCapturedVLAType();
677 vlaSizeMap[vat->getSizeExpr()] = exprArg;
678 }
679 }
680 } else if (md->isImplicitObjectMemberFunction()) {
681 // Not in a lambda; just use 'this' from the method.
682 // FIXME: Should we generate a new load for each use of 'this'? The fast
683 // register allocator would be happier...
684 cxxThisValue = cxxabiThisValue;
685 }
686
687 assert(!cir::MissingFeatures::sanitizers());
688 assert(!cir::MissingFeatures::emitTypeCheck());
689 }
690
691 // If any of the arguments have a variably modified type, make sure to
692 // emit the type size, but only if the function is not naked. Naked functions
693 // have no prolog to run this evaluation.
694 if (!fd || !fd->hasAttr<NakedAttr>()) {
695 for (const VarDecl *vd : args) {
696 // Dig out the type as written from ParmVarDecls; it's unclear whether
697 // the standard (C99 6.9.1p10) requires this, but we're following the
698 // precedent set by gcc.
699 QualType ty;
700 if (const auto *pvd = dyn_cast<ParmVarDecl>(Val: vd))
701 ty = pvd->getOriginalType();
702 else
703 ty = vd->getType();
704 if (ty->isVariablyModifiedType())
705 emitVariablyModifiedType(ty);
706 }
707 }
708}
709
710void CIRGenFunction::finishFunction(SourceLocation endLoc) {
711 // Pop any cleanups that might have been associated with the
712 // parameters. Do this in whatever block we're currently in; it's
713 // important to do this before we enter the return block or return
714 // edges will be *really* confused.
715 // TODO(cir): Use prologueCleanupDepth here.
716 bool hasCleanups = ehStack.stable_begin() != prologueCleanupDepth;
717 if (hasCleanups) {
718 assert(!cir::MissingFeatures::generateDebugInfo());
719 // FIXME(cir): should we clearInsertionPoint? breaks many testcases
720 popCleanupBlocks(oldCleanupStackDepth: prologueCleanupDepth);
721 }
722
723 assert(deferredConditionalCleanupStack.empty() &&
724 "deferred conditional cleanups were not consumed by a "
725 "FullExprCleanupScope");
726
727 emitEndEHSpec(d: curCodeDecl);
728}
729
730mlir::LogicalResult CIRGenFunction::emitFunctionBody(const clang::Stmt *body) {
731 // We start with function level scope for variables.
732 SymTableScopeTy varScope(symbolTable);
733
734 if (const CompoundStmt *block = dyn_cast<CompoundStmt>(Val: body))
735 return emitCompoundStmtWithoutScope(s: *block);
736
737 return emitStmt(s: body, /*useCurrentScope=*/true);
738}
739
740void CIRGenFunction::eraseEmptyAndUnusedBlocks(cir::FuncOp func) {
741 // Remove any leftover blocks that are unreachable and empty, since they do
742 // not represent unreachable code useful for warnings nor anything deemed
743 // useful in general.
744 SmallVector<mlir::Block *> blocksToDelete;
745 for (mlir::Block &block : func.getBlocks()) {
746 if (block.empty() && block.getUses().empty())
747 blocksToDelete.push_back(Elt: &block);
748 }
749 for (mlir::Block *block : blocksToDelete)
750 block->erase();
751}
752
753cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl gd, cir::FuncOp fn,
754 cir::FuncType funcType) {
755 const auto *funcDecl = cast<FunctionDecl>(Val: gd.getDecl());
756 curGD = gd;
757
758 if (funcDecl->isInlineBuiltinDeclaration()) {
759 // When generating code for a builtin with an inline declaration, use a
760 // mangled name to hold the actual body, while keeping an external
761 // declaration in case the function pointer is referenced somewhere.
762 std::string fdInlineName = (cgm.getMangledName(gd: funcDecl) + ".inline").str();
763 cir::FuncOp clone =
764 mlir::cast_or_null<cir::FuncOp>(Val: cgm.getGlobalValue(ref: fdInlineName));
765 if (!clone) {
766 mlir::OpBuilder::InsertionGuard guard(builder);
767 builder.setInsertionPoint(fn);
768 clone = cir::FuncOp::create(builder, location: fn.getLoc(), sym_name: fdInlineName,
769 type: fn.getFunctionType());
770 cgm.insertGlobalSymbol(op: clone);
771 clone.setLinkage(cir::GlobalLinkageKind::InternalLinkage);
772 clone.setSymVisibility("private");
773 clone.setInlineKind(cir::InlineKind::AlwaysInline);
774 }
775 fn.setLinkage(cir::GlobalLinkageKind::ExternalLinkage);
776 fn.setSymVisibility("private");
777 fn = clone;
778 } else {
779 // Detect the unusual situation where an inline version is shadowed by a
780 // non-inline version. In that case we should pick the external one
781 // everywhere. That's GCC behavior too.
782 for (const FunctionDecl *pd = funcDecl->getPreviousDecl(); pd;
783 pd = pd->getPreviousDecl()) {
784 if (LLVM_UNLIKELY(pd->isInlineBuiltinDeclaration())) {
785 std::string inlineName = funcDecl->getName().str() + ".inline";
786 if (auto inlineFn = mlir::cast_or_null<cir::FuncOp>(
787 Val: cgm.getGlobalValue(ref: inlineName))) {
788 // Replace all uses of the .inline function with the regular function
789 // FIXME: This performs a linear walk over the module. Introduce some
790 // caching here.
791 if (inlineFn
792 .replaceAllSymbolUses(newSymbol: fn.getSymNameAttr(), from: cgm.getModule())
793 .failed())
794 llvm_unreachable("Failed to replace inline builtin symbol uses");
795 cgm.eraseGlobalSymbol(op: inlineFn);
796 inlineFn.erase();
797 }
798 break;
799 }
800 }
801 }
802
803 SourceLocation loc = funcDecl->getLocation();
804 Stmt *body = funcDecl->getBody();
805 SourceRange bodyRange =
806 body ? body->getSourceRange() : funcDecl->getLocation();
807
808 SourceLocRAIIObject fnLoc{*this, funcDecl->getSourceRange()};
809
810 auto validMLIRLoc = [&](clang::SourceLocation clangLoc) {
811 return clangLoc.isValid() ? getLoc(srcLoc: clangLoc) : builder.getUnknownLoc();
812 };
813 const mlir::Location fusedLoc = mlir::FusedLoc::get(
814 context: &getMLIRContext(),
815 locs: {validMLIRLoc(bodyRange.getBegin()), validMLIRLoc(bodyRange.getEnd())});
816 mlir::Block *entryBB = fn.addEntryBlock();
817
818 FunctionArgList args;
819 QualType retTy = buildFunctionArgList(gd, args);
820
821 // Create a scope in the symbol table to hold variable declarations.
822 SymTableScopeTy varScope(symbolTable);
823 {
824 LexicalScope lexScope(*this, fusedLoc, entryBB);
825
826 // Emit the standard function prologue.
827 startFunction(gd, returnType: retTy, fn, funcType, args, loc, startLoc: bodyRange.getBegin());
828
829 // Save parameters for coroutine function.
830 if (body && isa_and_nonnull<CoroutineBodyStmt>(Val: body))
831 llvm::append_range(C&: fnArgs, R: funcDecl->parameters());
832
833 if (checkIfFunctionMustProgress())
834 fn->setAttr(name: cir::CIRDialect::getMustProgressAttrName(),
835 value: mlir::UnitAttr::get(context: &getMLIRContext()));
836
837 if (shouldEmitLifetimeMarkers)
838 fnHasBypassStmt = functionMightHaveBypass(s: body);
839
840 if (isa<CXXDestructorDecl>(Val: funcDecl)) {
841 emitDestructorBody(args);
842 } else if (isa<CXXConstructorDecl>(Val: funcDecl)) {
843 emitConstructorBody(args);
844 } else if (getLangOpts().CUDA && !getLangOpts().CUDAIsDevice &&
845 funcDecl->hasAttr<CUDAGlobalAttr>()) {
846 cgm.getCUDARuntime().emitDeviceStub(cgf&: *this, fn, args);
847 } else if (isa<CXXMethodDecl>(Val: funcDecl) &&
848 cast<CXXMethodDecl>(Val: funcDecl)->isLambdaStaticInvoker()) {
849 // The lambda static invoker function is special, because it forwards or
850 // clones the body of the function call operator (but is actually
851 // static).
852 emitLambdaStaticInvokeBody(md: cast<CXXMethodDecl>(Val: funcDecl));
853 } else if (funcDecl->isDefaulted() && isa<CXXMethodDecl>(Val: funcDecl) &&
854 (cast<CXXMethodDecl>(Val: funcDecl)->isCopyAssignmentOperator() ||
855 cast<CXXMethodDecl>(Val: funcDecl)->isMoveAssignmentOperator())) {
856 // Implicit copy-assignment gets the same special treatment as implicit
857 // copy-constructors.
858 emitImplicitAssignmentOperatorBody(args);
859 } else if (body) {
860 // Emit standard function body.
861 if (mlir::failed(Result: emitFunctionBody(body))) {
862 return nullptr;
863 }
864 } else {
865 // Anything without a body should have been handled above.
866 llvm_unreachable("no definition for normal function");
867 }
868
869 // Finish the function (including closing a dynamic exception
870 // specification try) before verifying so the try body is terminated.
871 finishFunction(endLoc: bodyRange.getEnd());
872
873 if (mlir::failed(Result: fn.verifyBody()))
874 return nullptr;
875 }
876
877 if (getLangOpts().OpenCL && funcDecl->hasAttr<DeviceKernelAttr>())
878 cgm.emitOpenCLKernelArgMetadata(func: fn, fd: funcDecl);
879
880 eraseEmptyAndUnusedBlocks(func: fn);
881 return fn;
882}
883
884void CIRGenFunction::emitConstructorBody(FunctionArgList &args) {
885 assert(!cir::MissingFeatures::sanitizers());
886 const auto *ctor = cast<CXXConstructorDecl>(Val: curGD.getDecl());
887 CXXCtorType ctorType = curGD.getCtorType();
888
889 assert((cgm.getTarget().getCXXABI().hasConstructorVariants() ||
890 ctorType == Ctor_Complete) &&
891 "can only generate complete ctor for this ABI");
892
893 cgm.setFuncInfoAttr(funcOp: cast<cir::FuncOp>(Val: curFn), funcDecl: ctor);
894
895 if (ctorType == Ctor_Complete && isConstructorDelegationValid(ctor) &&
896 cgm.getTarget().getCXXABI().hasConstructorVariants()) {
897 emitDelegateCXXConstructorCall(ctor, ctorType: Ctor_Base, args, loc: ctor->getEndLoc());
898 return;
899 }
900
901 const FunctionDecl *definition = nullptr;
902 Stmt *body = ctor->getBody(Definition&: definition);
903 assert(definition == ctor && "emitting wrong constructor body");
904
905 bool isTryBody = isa_and_nonnull<CXXTryStmt>(Val: body);
906
907 // A type that handles the emission of the constructor body, that can be
908 // called directly for cases where we don't have a try-body, or passed to
909 // emitCXXTryStmt.
910 struct ctorTryBodyEmitter final : cxxTryBodyEmitter {
911 const CXXConstructorDecl *ctor = nullptr;
912 CXXCtorType ctorType;
913 FunctionArgList &args;
914 Stmt *emitterBody = nullptr;
915 ctorTryBodyEmitter(const CXXConstructorDecl *ctor, CXXCtorType ctorType,
916 FunctionArgList &args, bool isTryBody, Stmt *b)
917 : ctor(ctor), ctorType(ctorType), args(args),
918 emitterBody(isTryBody ? cast<CXXTryStmt>(Val: b)->getTryBlock() : b) {}
919 ~ctorTryBodyEmitter() override = default;
920
921 mlir::LogicalResult operator()(CIRGenFunction &cgf) override {
922 assert(!cir::MissingFeatures::incrementProfileCounter());
923 assert(!cir::MissingFeatures::runCleanupsScope());
924
925 //// TODO: in restricted cases, we can emit the vbase initializers of a
926 //// complete ctor and then delegate to the base ctor.
927
928 cgf.emitCtorPrologue(ctor, ctorType, args);
929 return cgf.emitStmt(s: emitterBody, /*useCurrentScope=*/true);
930 }
931 };
932
933 ctorTryBodyEmitter emitter{ctor, ctorType, args, isTryBody, body};
934 mlir::LogicalResult bodyRes =
935 isTryBody ? emitCXXTryStmt(s: *cast<CXXTryStmt>(Val: body), bodyCallback&: emitter,
936 /*isFnTryBlock=*/true)
937 : emitter(*this);
938
939 // TODO(cir): propagate this result via mlir::logical result. Just
940 // unreachable now just to have it handled.
941 if (bodyRes.failed())
942 cgm.errorNYI(ctor->getSourceRange(),
943 "emitConstructorBody: emit body statement failed.");
944}
945
946/// Emits the body of the current destructor.
947void CIRGenFunction::emitDestructorBody(FunctionArgList &args) {
948 const CXXDestructorDecl *dtor = cast<CXXDestructorDecl>(Val: curGD.getDecl());
949 CXXDtorType dtorType = curGD.getDtorType();
950
951 cgm.setFuncInfoAttr(funcOp: cast<cir::FuncOp>(Val: curFn), funcDecl: dtor);
952
953 // For an abstract class, non-base destructors are never used (and can't
954 // be emitted in general, because vbase dtors may not have been validated
955 // by Sema), but the Itanium ABI doesn't make them optional and Clang may
956 // in fact emit references to them from other compilations, so emit them
957 // as functions containing a trap instruction.
958 Stmt *body = dtor->getBody();
959 if (dtorType != Dtor_Base && dtor->getParent()->isAbstract()) {
960 SourceLocation loc = body ? body->getBeginLoc() : dtor->getLocation();
961 emitTrap(loc: getLoc(srcLoc: loc), createNewBlock: true);
962 return;
963 }
964
965 assert(body && !cir::MissingFeatures::incrementProfileCounter());
966
967 // The call to operator delete in a deleting destructor happens
968 // outside of the function-try-block, which means it's always
969 // possible to delegate the destructor body to the complete
970 // destructor. Do so.
971 if (dtorType == Dtor_Deleting || dtorType == Dtor_VectorDeleting) {
972 if (cxxStructorImplicitParamValue && dtorType == Dtor_VectorDeleting)
973 cgm.errorNYI(dtor->getSourceRange(), "emitConditionalArrayDtorCall");
974
975 // A destroying operator delete destroys the object and deallocates its
976 // storage, so the deleting destructor only calls the operator delete.
977 const FunctionDecl *operatorDelete = dtor->getOperatorDelete();
978 if (operatorDelete->isDestroyingOperatorDelete()) {
979 if (cxxStructorImplicitParamValue) {
980 // The implicit parameter of a deleting destructor is the Microsoft ABI.
981 cgm.errorNYI(dtor->getSourceRange(), "emitConditionalArrayDtorCall");
982 }
983 emitDeleteCall(deleteFD: operatorDelete, ptr: loadThisForDtorDelete(dd: dtor),
984 deleteTy: getContext().getCanonicalTagType(TD: dtor->getParent()));
985 return;
986 }
987
988 RunCleanupsScope dtorEpilogue(*this);
989 enterDtorCleanups(dtor, type: Dtor_Deleting);
990 QualType thisTy = dtor->getFunctionObjectParameterType();
991 emitCXXDestructorCall(dd: dtor, type: Dtor_Complete, /*forVirtualBase=*/false,
992 /*delegating=*/false, thisAddr: loadCXXThisAddress(), thisTy);
993 return;
994 }
995
996 // If the body is a function-try-block, enter the try before
997 // anything else.
998 const bool isTryBody = isa_and_nonnull<CXXTryStmt>(Val: body);
999 if (isTryBody)
1000 cgm.errorNYI(dtor->getSourceRange(), "function-try-block destructor");
1001
1002 assert(!cir::MissingFeatures::sanitizers());
1003
1004 // Enter the epilogue cleanups.
1005 RunCleanupsScope dtorEpilogue(*this);
1006
1007 // If this is the complete variant, just invoke the base variant;
1008 // the epilogue will destruct the virtual bases. But we can't do
1009 // this optimization if the body is a function-try-block, because
1010 // we'd introduce *two* handler blocks. In the Microsoft ABI, we
1011 // always delegate because we might not have a definition in this TU.
1012 switch (dtorType) {
1013 case Dtor_Unified:
1014 llvm_unreachable("not expecting a unified dtor");
1015 case Dtor_Comdat:
1016 llvm_unreachable("not expecting a COMDAT");
1017 case Dtor_Deleting:
1018 case Dtor_VectorDeleting:
1019 llvm_unreachable("already handled deleting case");
1020
1021 case Dtor_Complete:
1022 assert((body || getTarget().getCXXABI().isMicrosoft()) &&
1023 "can't emit a dtor without a body for non-Microsoft ABIs");
1024
1025 // Enter the cleanup scopes for virtual bases.
1026 enterDtorCleanups(dtor, type: Dtor_Complete);
1027
1028 if (!isTryBody) {
1029 QualType thisTy = dtor->getFunctionObjectParameterType();
1030 emitCXXDestructorCall(dd: dtor, type: Dtor_Base, /*forVirtualBase=*/false,
1031 /*delegating=*/false, thisAddr: loadCXXThisAddress(), thisTy);
1032 break;
1033 }
1034
1035 // Fallthrough: act like we're in the base variant.
1036 [[fallthrough]];
1037
1038 case Dtor_Base:
1039 assert(body);
1040
1041 bool needsVTableInit =
1042 !CodeGenUtils::canSkipVTablePointerInitialization(Ctx&: getContext(), Dtor: dtor);
1043 // Launder 'this' if necessary.
1044 if (needsVTableInit && cgm.getCodeGenOpts().StrictVTablePointers &&
1045 cgm.getCodeGenOpts().OptimizationLevel > 0) {
1046 cxxThisValue = cir::LaunderOp::create(
1047 builder, location: getLoc(srcLoc: dtor->getBeginLoc()), arg: loadCXXThis());
1048 }
1049
1050 // Enter the cleanup scopes for fields and non-virtual bases.
1051 enterDtorCleanups(dtor, type: Dtor_Base);
1052
1053 // Initialize the vtable pointers before entering the body.
1054 if (needsVTableInit)
1055 initializeVTablePointers(loc: getLoc(srcLoc: dtor->getBeginLoc()), rd: dtor->getParent());
1056
1057 if (isTryBody) {
1058 cgm.errorNYI(dtor->getSourceRange(), "function-try-block destructor");
1059 } else if (body) {
1060 (void)emitStmt(s: body, /*useCurrentScope=*/true);
1061 } else {
1062 assert(dtor->isImplicit() && "bodyless dtor not implicit");
1063 // nothing to do besides what's in the epilogue
1064 }
1065 // -fapple-kext must inline any call to this dtor into
1066 // the caller's body.
1067 assert(!cir::MissingFeatures::appleKext());
1068
1069 break;
1070 }
1071
1072 // Jump out through the epilogue cleanups.
1073 dtorEpilogue.forceCleanup();
1074
1075 // Exit the try if applicable.
1076 if (isTryBody)
1077 cgm.errorNYI(dtor->getSourceRange(), "function-try-block destructor");
1078}
1079
1080/// Given a value of type T* that may not be to a complete object, construct
1081/// an l-vlaue withi the natural pointee alignment of T.
1082LValue CIRGenFunction::makeNaturalAlignPointeeAddrLValue(mlir::Value val,
1083 QualType ty) {
1084 // FIXME(cir): is it safe to assume Op->getResult(0) is valid? Perhaps
1085 // assert on the result type first.
1086 LValueBaseInfo baseInfo;
1087 assert(!cir::MissingFeatures::opTBAA());
1088 CharUnits align = cgm.getNaturalTypeAlignment(t: ty, baseInfo: &baseInfo);
1089 return makeAddrLValue(addr: Address(val, align), ty, baseInfo);
1090}
1091
1092LValue CIRGenFunction::makeNaturalAlignAddrLValue(mlir::Value val,
1093 QualType ty) {
1094 LValueBaseInfo baseInfo;
1095 CharUnits alignment = cgm.getNaturalTypeAlignment(t: ty, baseInfo: &baseInfo);
1096 Address addr(val, convertTypeForMem(t: ty), alignment);
1097 assert(!cir::MissingFeatures::opTBAA());
1098 return makeAddrLValue(addr, ty, baseInfo);
1099}
1100
1101clang::QualType CIRGenFunction::buildFunctionArgList(clang::GlobalDecl gd,
1102 FunctionArgList &args) {
1103 const auto *fd = cast<FunctionDecl>(Val: gd.getDecl());
1104 QualType retTy = fd->getReturnType();
1105
1106 // Only implicit-object member functions need the CXXABI-supplied `this`
1107 // parameter prepended to the arg list. Explicit-object members carry the
1108 // object as a regular parameter that fd->parameters() already enumerates.
1109 const auto *md = dyn_cast<CXXMethodDecl>(Val: fd);
1110 if (md && md->isImplicitObjectMemberFunction()) {
1111 if (cgm.getCXXABI().hasThisReturn(gd))
1112 retTy = md->getThisType();
1113 else if (cgm.getCXXABI().hasMostDerivedReturn(gd))
1114 cgm.errorNYI(fd->getSourceRange(), "most derived return");
1115 cgm.getCXXABI().buildThisParam(cgf&: *this, params&: args);
1116 }
1117
1118 bool passedParams = true;
1119 if (const auto *cd = dyn_cast<CXXConstructorDecl>(Val: fd))
1120 if (auto inherited = cd->getInheritedConstructor())
1121 passedParams =
1122 getTypes().inheritingCtorHasParams(inherited, type: gd.getCtorType());
1123
1124 if (passedParams) {
1125 for (auto *param : fd->parameters()) {
1126 args.push_back(Elt: param);
1127 if (!param->hasAttr<PassObjectSizeAttr>())
1128 continue;
1129
1130 auto *implicit = ImplicitParamDecl::Create(
1131 C&: getContext(), DC: param->getDeclContext(), IdLoc: param->getLocation(),
1132 /*Id=*/nullptr, T: getContext().getSizeType(), ParamKind: ImplicitParamKind::Other);
1133 sizeArguments[param] = implicit;
1134 args.push_back(Elt: implicit);
1135 }
1136 }
1137
1138 if (md && (isa<CXXConstructorDecl>(Val: md) || isa<CXXDestructorDecl>(Val: md)))
1139 cgm.getCXXABI().addImplicitStructorParams(cgf&: *this, resTy&: retTy, params&: args);
1140
1141 return retTy;
1142}
1143
1144LValue CIRGenFunction::emitInitListLValue(const InitListExpr *e) {
1145 // Initializing an aggregate temporary in C++11: T{...}.
1146 if (!e->isGLValue())
1147 return emitAggExprToLValue(e);
1148
1149 // An lvalue initializer list must be initializing a reference.
1150 assert(e->isTransparent() && "non-transparent glvalue init list");
1151 return emitLValue(e: e->getInit(Init: 0));
1152}
1153
1154static std::variant<LValue, RValue>
1155emitPseudoObjectExpr(CIRGenFunction &cgf, const PseudoObjectExpr *e,
1156 bool forLValue, AggValueSlot slot) {
1157 using OVMD = CIRGenFunction::OpaqueValueMappingData;
1158 SmallVector<OVMD> opaques;
1159 llvm::scope_exit opaque_cleanup{
1160 [&]() { llvm::for_each(Range&: opaques, F: [&](OVMD &o) { o.unbind(cgf); }); }};
1161
1162 // Find the result expression, if any.
1163 const Expr *resultExpr = e->getResultExpr();
1164 std::variant<LValue, RValue> result;
1165
1166 for (const Expr *semantic : e->semantics()) {
1167 // If this semantic expression is an opaque value, bind it
1168 // to the result of its source expression.
1169 if (const auto *ov = dyn_cast<OpaqueValueExpr>(Val: semantic)) {
1170
1171 // Skip unique OVEs.
1172 if (ov->isUnique()) {
1173 assert(ov != resultExpr &&
1174 "A unique OVE cannot be used as the result expression");
1175 continue;
1176 }
1177
1178 // If this is the result expression, we may need to evaluate
1179 // directly into the slot.
1180 OVMD opaqueData;
1181 if (ov == resultExpr && ov->isPRValue() && !forLValue &&
1182 CIRGenFunction::hasAggregateEvaluationKind(type: ov->getType())) {
1183 cgf.cgm.errorNYI(e->getSourceRange(),
1184 "emitPseudoObjectExpr for RValue & aggregate kind");
1185 } else {
1186 opaqueData = OVMD::bind(cgf, ov, e: ov->getSourceExpr());
1187
1188 // If this is the result, also evaluate the result now.
1189 if (ov == resultExpr) {
1190 // FIXME: This doesn't really affect anything, but I cannot find a
1191 // test for this, so leave an ErrorNYI here until we can find one.
1192 cgf.cgm.errorNYI(e->getSourceRange(),
1193 "emitPseudoObjectExpr as result");
1194 if (forLValue)
1195 result = cgf.emitLValue(e: ov);
1196 else
1197 cgf.cgm.errorNYI(e->getSourceRange(),
1198 "emitPseudoObjectExpr as an RValue");
1199 }
1200 }
1201 opaques.push_back(Elt: opaqueData);
1202 } else if (semantic == resultExpr) {
1203 // Otherwise, if the expression is the result, evaluate it
1204 // and remember the result.
1205 if (forLValue)
1206 result = cgf.emitLValue(e: semantic);
1207 else
1208 result = cgf.emitAnyExpr(e: semantic, aggSlot: slot);
1209 } else {
1210 // FIXME: best I can tell, this is only reachable as an r-value, so this
1211 // isn't properly tested.
1212 cgf.cgm.errorNYI(e->getSourceRange(),
1213 "emitPseudoObjectExpr as an ignored value");
1214 // Otherwise, evaluate the expression in an ignored context.
1215 cgf.emitIgnoredExpr(e: semantic);
1216 }
1217 }
1218
1219 return result;
1220}
1221
1222RValue CIRGenFunction::emitPseudoObjectRValue(const PseudoObjectExpr *e,
1223 AggValueSlot slot) {
1224 return std::get<RValue>(
1225 v: emitPseudoObjectExpr(cgf&: *this, e, /*forLValue=*/false, slot));
1226}
1227
1228LValue CIRGenFunction::emitPseudoObjectLValue(const PseudoObjectExpr *e) {
1229 return std::get<LValue>(v: emitPseudoObjectExpr(cgf&: *this, e, /*forLValue=*/true,
1230 slot: AggValueSlot::ignored()));
1231}
1232
1233/// Emit code to compute a designator that specifies the location
1234/// of the expression.
1235/// FIXME: document this function better.
1236LValue CIRGenFunction::emitLValue(const Expr *e) {
1237 assert(!cir::MissingFeatures::generateDebugInfo());
1238 switch (e->getStmtClass()) {
1239 default:
1240 getCIRGenModule().errorNYI(e->getSourceRange(),
1241 "emitLValue: unsupported l-value class");
1242 return LValue();
1243
1244 case Expr::ObjCPropertyRefExprClass:
1245 llvm_unreachable("cannot emit a property reference directly");
1246
1247 case Expr::ObjCSelectorExprClass:
1248 getCIRGenModule().errorNYI(e->getSourceRange(),
1249 "emitLValue: ObjCSelectorExpr");
1250 return LValue();
1251 case Expr::ObjCIsaExprClass:
1252 getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: ObjCIsaExpr");
1253 return LValue();
1254 case Expr::BinaryOperatorClass:
1255 return emitBinaryOperatorLValue(e: cast<BinaryOperator>(Val: e));
1256 case Expr::CompoundAssignOperatorClass: {
1257 QualType ty = e->getType();
1258 if (const AtomicType *at = ty->getAs<AtomicType>())
1259 ty = at->getValueType();
1260 if (!ty->isAnyComplexType())
1261 return emitCompoundAssignmentLValue(e: cast<CompoundAssignOperator>(Val: e));
1262
1263 return emitComplexCompoundAssignmentLValue(e: cast<CompoundAssignOperator>(Val: e));
1264 }
1265 case Expr::CallExprClass:
1266 case Expr::CXXMemberCallExprClass:
1267 case Expr::CXXOperatorCallExprClass:
1268 case Expr::UserDefinedLiteralClass:
1269 return emitCallExprLValue(e: cast<CallExpr>(Val: e));
1270 case Expr::CXXRewrittenBinaryOperatorClass:
1271 assert(!cir::MissingFeatures::addressIsKnownNonNull());
1272 return emitLValue(e: cast<CXXRewrittenBinaryOperator>(Val: e)->getSemanticForm());
1273 case Expr::VAArgExprClass:
1274 getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: VAArgExpr");
1275 return LValue();
1276 case Expr::DeclRefExprClass:
1277 return emitDeclRefLValue(e: cast<DeclRefExpr>(Val: e));
1278 case Expr::ConstantExprClass: {
1279 const ConstantExpr *ce = cast<ConstantExpr>(Val: e);
1280 if (mlir::Attribute result =
1281 ConstantEmitter(*this).tryEmitConstantExpr(ce)) {
1282 mlir::Value val = builder.getConstant(
1283 loc: getLoc(srcLoc: e->getSourceRange()), attr: mlir::cast<mlir::TypedAttr>(Val&: result));
1284 return makeNaturalAlignPointeeAddrLValue(val, ty: ce->getType());
1285 }
1286 return emitLValue(e: ce->getSubExpr());
1287 }
1288 case Expr::ParenExprClass:
1289 return emitLValue(e: cast<ParenExpr>(Val: e)->getSubExpr());
1290 case Expr::GenericSelectionExprClass:
1291 return emitLValue(e: cast<GenericSelectionExpr>(Val: e)->getResultExpr());
1292 case Expr::PredefinedExprClass:
1293 return emitPredefinedLValue(e: cast<PredefinedExpr>(Val: e));
1294 case Expr::StringLiteralClass:
1295 return emitStringLiteralLValue(e: cast<StringLiteral>(Val: e));
1296 case Expr::ObjCEncodeExprClass:
1297 getCIRGenModule().errorNYI(e->getSourceRange(),
1298 "emitLValue: ObjCEncodeExpr");
1299 return LValue();
1300 case Expr::PseudoObjectExprClass:
1301 return emitPseudoObjectLValue(e: cast<PseudoObjectExpr>(Val: e));
1302 case Expr::InitListExprClass:
1303 return emitInitListLValue(e: cast<InitListExpr>(Val: e));
1304 case Expr::CXXTemporaryObjectExprClass:
1305 case Expr::CXXConstructExprClass:
1306 return emitCXXConstructLValue(e: cast<CXXConstructExpr>(Val: e));
1307 case Expr::CXXBindTemporaryExprClass:
1308 return emitCXXBindTemporaryLValue(e: cast<CXXBindTemporaryExpr>(Val: e));
1309 case Expr::CXXUuidofExprClass:
1310 getCIRGenModule().errorNYI(e->getSourceRange(),
1311 "emitLValue: CXXUuidofExpr");
1312 return LValue();
1313 case Expr::LambdaExprClass:
1314 getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: LambdaExpr");
1315 return LValue();
1316 case Expr::ExprWithCleanupsClass: {
1317 const auto *cleanups = cast<ExprWithCleanups>(Val: e);
1318 FullExprCleanupScope scope(*this, cleanups->getSubExpr());
1319 LValue lv = emitLValue(e: cleanups->getSubExpr());
1320 if (lv.isSimple()) {
1321 // Defend against branches out of gnu statement expressions surrounded by
1322 // cleanups.
1323 Address addr = lv.getAddress();
1324 mlir::Value v = addr.getPointer();
1325 scope.exit(valuesToReload: {&v});
1326 return LValue::makeAddr(address: addr.withPointer(newPtr: v), t: lv.getType(),
1327 baseInfo: lv.getBaseInfo());
1328 }
1329 // FIXME: Is it possible to create an ExprWithCleanups that produces a
1330 // bitfield lvalue or some other non-simple lvalue?
1331 return lv;
1332 }
1333 case Expr::CXXDefaultArgExprClass: {
1334 auto *dae = cast<CXXDefaultArgExpr>(Val: e);
1335 CXXDefaultArgExprScope scope(*this, dae);
1336 return emitLValue(e: dae->getExpr());
1337 }
1338 case Expr::CXXDefaultInitExprClass: {
1339 auto *die = cast<CXXDefaultInitExpr>(Val: e);
1340 CXXDefaultInitExprScope scope(*this, die);
1341 return emitLValue(e: die->getExpr());
1342 }
1343 case Expr::CXXTypeidExprClass:
1344 return emitCXXTypeidLValue(e: cast<CXXTypeidExpr>(Val: e));
1345 case Expr::ObjCMessageExprClass:
1346 getCIRGenModule().errorNYI(e->getSourceRange(),
1347 "emitLValue: ObjCMessageExpr");
1348 return LValue();
1349 case Expr::ObjCIvarRefExprClass:
1350 getCIRGenModule().errorNYI(e->getSourceRange(),
1351 "emitLValue: ObjCIvarRefExpr");
1352 return LValue();
1353 case Expr::StmtExprClass:
1354 getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: StmtExpr");
1355 return LValue();
1356 case Expr::UnaryOperatorClass:
1357 return emitUnaryOpLValue(e: cast<UnaryOperator>(Val: e));
1358 case Expr::ArraySubscriptExprClass:
1359 return emitArraySubscriptExpr(e: cast<ArraySubscriptExpr>(Val: e));
1360 case Expr::MatrixSingleSubscriptExprClass:
1361 getCIRGenModule().errorNYI(e->getSourceRange(),
1362 "emitLValue: MatrixSingleSubscriptExpr");
1363 return LValue();
1364 case Expr::MatrixSubscriptExprClass:
1365 return emitMatrixSubscriptExpr(e: cast<MatrixSubscriptExpr>(Val: e));
1366 case Expr::ArraySectionExprClass:
1367 getCIRGenModule().errorNYI(e->getSourceRange(),
1368 "emitLValue: ArraySectionExpr");
1369 return LValue();
1370 case Expr::ExtVectorElementExprClass:
1371 return emitExtVectorElementExpr(e: cast<ExtVectorElementExpr>(Val: e));
1372 case Expr::MatrixElementExprClass:
1373 return emitMatrixElementExpr(e: cast<MatrixElementExpr>(Val: e));
1374 case Expr::CXXThisExprClass:
1375 return makeAddrLValue(addr: loadCXXThisAddress(), ty: e->getType());
1376 case Expr::MemberExprClass:
1377 return emitMemberExpr(e: cast<MemberExpr>(Val: e));
1378 case Expr::CompoundLiteralExprClass:
1379 return emitCompoundLiteralLValue(e: cast<CompoundLiteralExpr>(Val: e));
1380 case Expr::ConditionalOperatorClass:
1381 return emitConditionalOperatorLValue(expr: cast<ConditionalOperator>(Val: e));
1382 case Expr::BinaryConditionalOperatorClass:
1383 return emitConditionalOperatorLValue(expr: cast<BinaryConditionalOperator>(Val: e));
1384 case Expr::ChooseExprClass:
1385 return emitLValue(e: cast<ChooseExpr>(Val: e)->getChosenSubExpr());
1386 case Expr::OpaqueValueExprClass:
1387 return emitOpaqueValueLValue(e: cast<OpaqueValueExpr>(Val: e));
1388 case Expr::SubstNonTypeTemplateParmExprClass:
1389 return emitLValue(e: cast<SubstNonTypeTemplateParmExpr>(Val: e)->getReplacement());
1390 case Expr::ImplicitCastExprClass:
1391 case Expr::CStyleCastExprClass:
1392 case Expr::CXXFunctionalCastExprClass:
1393 case Expr::CXXStaticCastExprClass:
1394 case Expr::CXXDynamicCastExprClass:
1395 case Expr::CXXReinterpretCastExprClass:
1396 case Expr::CXXConstCastExprClass:
1397 return emitCastLValue(e: cast<CastExpr>(Val: e));
1398 case Expr::CXXAddrspaceCastExprClass:
1399 case Expr::ObjCBridgedCastExprClass:
1400 // TODO(cir): These can just be moved into the cast handling above, but
1401 // they need test cases.
1402 getCIRGenModule().errorNYI(e->getSourceRange(),
1403 "emitLValue: addrspace or ObjC bridged cast");
1404 return LValue();
1405 case Expr::MaterializeTemporaryExprClass:
1406 return emitMaterializeTemporaryExpr(e: cast<MaterializeTemporaryExpr>(Val: e));
1407 case Expr::CoawaitExprClass:
1408 return emitCoawaitLValue(e: cast<CoawaitExpr>(Val: e));
1409 case Expr::CoyieldExprClass:
1410 return emitCoyieldLValue(e: cast<CoyieldExpr>(Val: e));
1411 case Expr::PackIndexingExprClass:
1412 return emitLValue(e: cast<PackIndexingExpr>(Val: e)->getSelectedExpr());
1413 case Expr::HLSLOutArgExprClass:
1414 llvm_unreachable("cannot emit a HLSL out argument directly");
1415 }
1416}
1417
1418static std::string getVersionedTmpName(llvm::StringRef name, unsigned cnt) {
1419 SmallString<256> buffer;
1420 llvm::raw_svector_ostream out(buffer);
1421 out << name << cnt;
1422 return std::string(out.str());
1423}
1424
1425std::string CIRGenFunction::getCounterRefTmpAsString() {
1426 return getVersionedTmpName(name: "ref.tmp", cnt: counterRefTmp++);
1427}
1428
1429std::string CIRGenFunction::getCounterAggTmpAsString() {
1430 return getVersionedTmpName(name: "agg.tmp", cnt: counterAggTmp++);
1431}
1432
1433void CIRGenFunction::emitNullInitialization(mlir::Location loc, Address destPtr,
1434 QualType ty) {
1435 // Ignore empty classes in C++.
1436 if (getLangOpts().CPlusPlus)
1437 if (const auto *rd = ty->getAsCXXRecordDecl(); rd && rd->isEmpty())
1438 return;
1439
1440 // Cast the dest ptr to the appropriate i8 pointer type.
1441 if (!builder.isInt8Ty(i: destPtr.getElementType()))
1442 destPtr = destPtr.withElementType(builder, ElemTy: uInt8Ty);
1443
1444 // Get size and alignment info for this aggregate.
1445 mlir::IntegerAttr sizeVal;
1446 const CharUnits size = getContext().getTypeSizeInChars(T: ty);
1447 if (size.isZero()) {
1448 // But note that getTypeInfo returns 0 for a VLA.
1449 if (isa_and_nonnull<VariableArrayType>(Val: getContext().getAsArrayType(T: ty))) {
1450 cgm.errorNYI(loc,
1451 feature: "emitNullInitialization for zero size VariableArrayType");
1452 } else {
1453 return;
1454 }
1455 } else {
1456 sizeVal = cgm.getSize(size);
1457 }
1458
1459 // If the type contains a pointer to data member we can't memset it to zero.
1460 // Instead, create a null constant and copy it to the destination.
1461 // TODO: there are other patterns besides zero that we can usefully memset,
1462 // like -1, which happens to be the pattern used by member-pointers.
1463 if (!cgm.getTypes().isZeroInitializable(ty)) {
1464 // Only the pointer-to-data-member case is tested here; emitNullConstant
1465 // owns the NYIs for shapes it cannot build (virtual bases, non-zero-init
1466 // arrays).
1467 assert((ty->isMemberDataPointerType() || ty->isRecordType()) &&
1468 "emitNullInitialization: only pointer-to-data-member (directly or "
1469 "within a record) null initialization is implemented");
1470 mlir::Value nullVal = cgm.emitNullConstant(t: ty, loc);
1471 builder.createStore(loc, val: nullVal, dst: destPtr);
1472 return;
1473 }
1474
1475 // Otherwise, just memset the whole thing to zero. This is legal
1476 // because in LLVM, all default initializers (other than the ones we just
1477 // handled above, and the case handled below) are guaranteed to have a bit
1478 // pattern of all zeros.
1479 mlir::Value zero = builder.getNullValue(ty: builder.getUInt8Ty(), loc);
1480 mlir::Value sizeValue =
1481 builder.getConstAPInt(loc, typ: cgm.uInt64Ty, val: sizeVal.getValue());
1482 destPtr = destPtr.withElementType(builder, ElemTy: cgm.voidTy);
1483 builder.createMemSet(loc, dst: destPtr, val: zero, len: sizeValue);
1484}
1485
1486CIRGenFunction::CIRGenFPOptionsRAII::CIRGenFPOptionsRAII(CIRGenFunction &cgf,
1487 const clang::Expr *e)
1488 : cgf(cgf) {
1489 ConstructorHelper(FPFeatures: e->getFPFeaturesInEffect(LO: cgf.getLangOpts()));
1490}
1491
1492CIRGenFunction::CIRGenFPOptionsRAII::CIRGenFPOptionsRAII(CIRGenFunction &cgf,
1493 FPOptions fpFeatures)
1494 : cgf(cgf) {
1495 ConstructorHelper(FPFeatures: fpFeatures);
1496}
1497
1498void CIRGenFunction::CIRGenFPOptionsRAII::ConstructorHelper(
1499 FPOptions fpFeatures) {
1500 oldFPFeatures = cgf.curFPFeatures;
1501 cgf.curFPFeatures = fpFeatures;
1502
1503 oldExcept = cgf.builder.getDefaultConstrainedExcept();
1504 oldRounding = cgf.builder.getDefaultConstrainedRounding();
1505
1506 if (oldFPFeatures == fpFeatures)
1507 return;
1508
1509 oldFastMathFlags = cgf.builder.getFastMathFlags();
1510
1511 llvm::RoundingMode newRoundingMode = fpFeatures.getRoundingMode();
1512 LangOptions::FPExceptionModeKind newExceptionBehavior =
1513 fpFeatures.getExceptionMode();
1514
1515 cgf.builder.setDefaultConstrainedRounding(newRoundingMode);
1516 cgf.builder.setDefaultConstrainedExcept(newExceptionBehavior);
1517
1518 cgf.setFastMathFlags(fpFeatures);
1519
1520 assert((cgf.curFuncDecl == nullptr || cgf.builder.getIsFPConstrained() ||
1521 isa<CXXConstructorDecl>(cgf.curFuncDecl) ||
1522 isa<CXXDestructorDecl>(cgf.curFuncDecl) ||
1523 (newExceptionBehavior == LangOptions::FPE_Ignore &&
1524 newRoundingMode == llvm::RoundingMode::NearestTiesToEven)) &&
1525 "FPConstrained should be enabled on entire function");
1526
1527 // TODO(cir): mark CIR function with fast math attributes.
1528 assert(!cir::MissingFeatures::fastMathFuncAttributes());
1529}
1530
1531CIRGenFunction::CIRGenFPOptionsRAII::~CIRGenFPOptionsRAII() {
1532 cgf.curFPFeatures = oldFPFeatures;
1533 cgf.builder.setDefaultConstrainedExcept(oldExcept);
1534 cgf.builder.setDefaultConstrainedRounding(oldRounding);
1535 if (oldFastMathFlags)
1536 cgf.builder.setFastMathFlags(*oldFastMathFlags);
1537}
1538
1539cir::FastMathFlags CIRGenFunction::getCurrentFastMathFlags() const {
1540 cir::FastMathFlags flags = cir::FastMathFlags::none;
1541 if (curFPFeatures.getAllowFPReassociate())
1542 flags |= cir::FastMathFlags::reassoc;
1543 if (curFPFeatures.getNoHonorNaNs())
1544 flags |= cir::FastMathFlags::nnan;
1545 if (curFPFeatures.getNoHonorInfs())
1546 flags |= cir::FastMathFlags::ninf;
1547 if (curFPFeatures.getNoSignedZero())
1548 flags |= cir::FastMathFlags::nsz;
1549 if (curFPFeatures.getAllowReciprocal())
1550 flags |= cir::FastMathFlags::arcp;
1551 if (curFPFeatures.getAllowApproxFunc())
1552 flags |= cir::FastMathFlags::afn;
1553 if (curFPFeatures.allowFPContractAcrossStatement())
1554 flags |= cir::FastMathFlags::contract;
1555
1556 return flags;
1557}
1558
1559cir::FastMathFlagsAttr
1560CIRGenFunction::getFastMathFlagsAttr(cir::FastMathFlags fastMathFlags) {
1561 if (fastMathFlags == cir::FastMathFlags::none)
1562 return {};
1563 return cir::FastMathFlagsAttr::get(context: &getMLIRContext(), value: fastMathFlags);
1564}
1565
1566// TODO(cir): should be shared with LLVM codegen.
1567bool CIRGenFunction::shouldNullCheckClassCastValue(const CastExpr *ce) {
1568 const Expr *e = ce->getSubExpr();
1569
1570 if (ce->getCastKind() == CK_UncheckedDerivedToBase)
1571 return false;
1572
1573 if (isa<CXXThisExpr>(Val: e->IgnoreParens())) {
1574 // We always assume that 'this' is never null.
1575 return false;
1576 }
1577
1578 if (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(Val: ce)) {
1579 // And that glvalue casts are never null.
1580 if (ice->isGLValue())
1581 return false;
1582 }
1583
1584 return true;
1585}
1586
1587/// Computes the length of an array in elements, as well as the base
1588/// element type and a properly-typed first element pointer.
1589mlir::Value
1590CIRGenFunction::emitArrayLength(const clang::ArrayType *origArrayType,
1591 QualType &baseType, Address &addr) {
1592 const clang::ArrayType *arrayType = origArrayType;
1593
1594 // If it's a VLA, we have to load the stored size. Note that
1595 // this is the size of the VLA in bytes, not its size in elements.
1596 mlir::Value numVLAElements = nullptr;
1597 if (isa<VariableArrayType>(Val: arrayType)) {
1598 numVLAElements = getVLASize(type: cast<VariableArrayType>(Val: arrayType)).numElts;
1599
1600 // Walk into all VLAs. This doesn't require changes to addr,
1601 // which has type T* where T is the first non-VLA element type.
1602 do {
1603 QualType elementType = arrayType->getElementType();
1604 arrayType = getContext().getAsArrayType(T: elementType);
1605
1606 // If we only have VLA components, 'addr' requires no adjustment.
1607 if (!arrayType) {
1608 baseType = elementType;
1609 return numVLAElements;
1610 }
1611 } while (isa<VariableArrayType>(Val: arrayType));
1612
1613 // We get out here only if we find a constant array type
1614 // inside the VLA.
1615 }
1616
1617 // Classic codegen emits an all-zero inbounds GEP to convert addr from
1618 // [M x [N x T]]* to T*. CIR doesn't need this because callers handle
1619 // the array-to-element pointer conversion themselves (via array_to_ptrdecay
1620 // casts, ptr_bitcast, or manual array type peeling).
1621
1622 uint64_t countFromCLAs = 1;
1623 QualType eltType;
1624
1625 auto cirArrayType = mlir::dyn_cast<cir::ArrayType>(Val: addr.getElementType());
1626
1627 while (cirArrayType) {
1628 assert(isa<ConstantArrayType>(arrayType));
1629 countFromCLAs *= cirArrayType.getSize();
1630 eltType = arrayType->getElementType();
1631
1632 cirArrayType =
1633 mlir::dyn_cast<cir::ArrayType>(Val: cirArrayType.getElementType());
1634
1635 arrayType = getContext().getAsArrayType(T: arrayType->getElementType());
1636 assert((!cirArrayType || arrayType) &&
1637 "CIR and Clang types are out-of-sync");
1638 }
1639
1640 if (arrayType) {
1641 // From this point onwards, the Clang array type has been emitted
1642 // as some other type (probably a packed struct). Compute the array
1643 // size, and just emit the 'begin' expression as a bitcast.
1644 cgm.errorNYI(*currSrcLoc, "length for non-array underlying types");
1645 }
1646
1647 baseType = eltType;
1648
1649 mlir::Value numElements =
1650 builder.getConstInt(loc: getLoc(srcLoc: *currSrcLoc), t: sizeTy, c: countFromCLAs);
1651
1652 // If we had any VLA dimensions, factor them in.
1653 if (numVLAElements)
1654 numElements =
1655 builder.createMul(loc: numVLAElements.getLoc(), lhs: numVLAElements, rhs: numElements,
1656 flags: cir::OverflowFlags::NoUnsignedWrap);
1657
1658 return numElements;
1659}
1660
1661mlir::Value CIRGenFunction::emitAlignmentAssumption(
1662 mlir::Value ptrValue, QualType ty, SourceLocation loc,
1663 SourceLocation assumptionLoc, int64_t alignment, mlir::Value offsetValue) {
1664 assert(!cir::MissingFeatures::sanitizers());
1665 mlir::Location assumeLoc = getLoc(srcLoc: assumptionLoc);
1666 mlir::Value alignValue = builder.getUInt64(c: alignment, loc: assumeLoc);
1667 mlir::Value cond = builder.getBool(state: true, loc: assumeLoc);
1668 llvm::SmallVector<mlir::Value> bundleArgs{ptrValue, alignValue};
1669 if (offsetValue)
1670 bundleArgs.push_back(Elt: offsetValue);
1671 cir::AssumeOp::create(builder, location: assumeLoc, predicate: cond, bundle_kind: cir::AssumeBundleKind::Align,
1672 bundle_args: bundleArgs);
1673 return ptrValue;
1674}
1675
1676mlir::Value CIRGenFunction::emitAlignmentAssumption(
1677 mlir::Value ptrValue, const Expr *expr, SourceLocation assumptionLoc,
1678 int64_t alignment, mlir::Value offsetValue) {
1679 QualType ty = expr->getType();
1680 SourceLocation loc = expr->getExprLoc();
1681 return emitAlignmentAssumption(ptrValue, ty, loc, assumptionLoc, alignment,
1682 offsetValue);
1683}
1684
1685CIRGenFunction::VlaSizePair CIRGenFunction::getVLASize(QualType type) {
1686 const VariableArrayType *vla =
1687 cgm.getASTContext().getAsVariableArrayType(T: type);
1688 assert(vla && "type was not a variable array type!");
1689 return getVLASize(type: vla);
1690}
1691
1692CIRGenFunction::VlaSizePair
1693CIRGenFunction::getVLASize(const VariableArrayType *type) {
1694 // The number of elements so far; always size_t.
1695 mlir::Value numElements;
1696
1697 QualType elementType;
1698 do {
1699 elementType = type->getElementType();
1700 mlir::Value vlaSize = vlaSizeMap[type->getSizeExpr()];
1701 assert(vlaSize && "no size for VLA!");
1702 assert(vlaSize.getType() == sizeTy);
1703
1704 if (!numElements) {
1705 numElements = vlaSize;
1706 } else {
1707 // It's undefined behavior if this wraps around, so mark it that way.
1708 // FIXME: Teach -fsanitize=undefined to trap this.
1709
1710 numElements =
1711 builder.createMul(loc: numElements.getLoc(), lhs: numElements, rhs: vlaSize,
1712 flags: cir::OverflowFlags::NoUnsignedWrap);
1713 }
1714 } while ((type = getContext().getAsVariableArrayType(T: elementType)));
1715
1716 assert(numElements && "Undefined elements number");
1717 return {numElements, elementType};
1718}
1719
1720CIRGenFunction::VlaSizePair
1721CIRGenFunction::getVLAElements1D(const VariableArrayType *vla) {
1722 mlir::Value vlaSize = vlaSizeMap[vla->getSizeExpr()];
1723 assert(vlaSize && "no size for VLA!");
1724 assert(vlaSize.getType() == sizeTy);
1725 return {vlaSize, vla->getElementType()};
1726}
1727
1728// TODO(cir): Most of this function can be shared between CIRGen
1729// and traditional LLVM codegen
1730void CIRGenFunction::emitVariablyModifiedType(QualType type) {
1731 assert(type->isVariablyModifiedType() &&
1732 "Must pass variably modified type to EmitVLASizes!");
1733
1734 // We're going to walk down into the type and look for VLA
1735 // expressions.
1736 do {
1737 assert(type->isVariablyModifiedType());
1738
1739 const Type *ty = type.getTypePtr();
1740 switch (ty->getTypeClass()) {
1741 case Type::CountAttributed:
1742 case Type::PackIndexing:
1743 case Type::ArrayParameter:
1744 case Type::HLSLAttributedResource:
1745 case Type::HLSLInlineSpirv:
1746 case Type::PredefinedSugar:
1747 case Type::LateParsedAttr:
1748 cgm.errorNYI(feature: "CIRGenFunction::emitVariablyModifiedType");
1749 break;
1750
1751#define TYPE(Class, Base)
1752#define ABSTRACT_TYPE(Class, Base)
1753#define NON_CANONICAL_TYPE(Class, Base)
1754#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1755#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base)
1756#include "clang/AST/TypeNodes.inc"
1757 llvm_unreachable(
1758 "dependent type must be resolved before the CIR codegen");
1759
1760 // These types are never variably-modified.
1761 case Type::Builtin:
1762 case Type::Complex:
1763 case Type::Vector:
1764 case Type::ExtVector:
1765 case Type::ConstantMatrix:
1766 case Type::Record:
1767 case Type::Enum:
1768 case Type::Using:
1769 case Type::TemplateSpecialization:
1770 case Type::ObjCTypeParam:
1771 case Type::ObjCObject:
1772 case Type::ObjCInterface:
1773 case Type::ObjCObjectPointer:
1774 case Type::BitInt:
1775 case Type::OverflowBehavior:
1776 llvm_unreachable("type class is never variably-modified!");
1777
1778 case Type::Adjusted:
1779 type = cast<clang::AdjustedType>(Val: ty)->getAdjustedType();
1780 break;
1781
1782 case Type::Decayed:
1783 type = cast<clang::DecayedType>(Val: ty)->getPointeeType();
1784 break;
1785
1786 case Type::Pointer:
1787 type = cast<clang::PointerType>(Val: ty)->getPointeeType();
1788 break;
1789
1790 case Type::BlockPointer:
1791 type = cast<clang::BlockPointerType>(Val: ty)->getPointeeType();
1792 break;
1793
1794 case Type::LValueReference:
1795 case Type::RValueReference:
1796 type = cast<clang::ReferenceType>(Val: ty)->getPointeeType();
1797 break;
1798
1799 case Type::MemberPointer:
1800 type = cast<clang::MemberPointerType>(Val: ty)->getPointeeType();
1801 break;
1802
1803 case Type::ConstantArray:
1804 case Type::IncompleteArray:
1805 // Losing element qualification here is fine.
1806 type = cast<clang::ArrayType>(Val: ty)->getElementType();
1807 break;
1808
1809 case Type::VariableArray: {
1810 // Losing element qualification here is fine.
1811 const VariableArrayType *vat = cast<clang::VariableArrayType>(Val: ty);
1812
1813 // Unknown size indication requires no size computation.
1814 // Otherwise, evaluate and record it.
1815 if (const Expr *sizeExpr = vat->getSizeExpr()) {
1816 // It's possible that we might have emitted this already,
1817 // e.g. with a typedef and a pointer to it.
1818 mlir::Value &entry = vlaSizeMap[sizeExpr];
1819 if (!entry) {
1820 mlir::Value size = emitScalarExpr(e: sizeExpr);
1821 assert(!cir::MissingFeatures::sanitizers());
1822
1823 // Always zexting here would be wrong if it weren't
1824 // undefined behavior to have a negative bound.
1825 // FIXME: What about when size's type is larger than size_t?
1826 entry = builder.createBoolIntToIntCast(src: size, newTy: sizeTy);
1827 }
1828 }
1829 type = vat->getElementType();
1830 break;
1831 }
1832
1833 case Type::FunctionProto:
1834 case Type::FunctionNoProto:
1835 type = cast<clang::FunctionType>(Val: ty)->getReturnType();
1836 break;
1837
1838 case Type::Paren:
1839 case Type::TypeOf:
1840 case Type::UnaryTransform:
1841 case Type::Attributed:
1842 case Type::BTFTagAttributed:
1843 case Type::SubstTemplateTypeParm:
1844 case Type::MacroQualified:
1845 // Keep walking after single level desugaring.
1846 type = type.getSingleStepDesugaredType(Context: getContext());
1847 break;
1848
1849 case Type::Typedef:
1850 case Type::Decltype:
1851 case Type::Auto:
1852 case Type::DeducedTemplateSpecialization:
1853 // Stop walking: nothing to do.
1854 return;
1855
1856 case Type::TypeOfExpr:
1857 // Stop walking: emit typeof expression.
1858 emitIgnoredExpr(e: cast<clang::TypeOfExprType>(Val: ty)->getUnderlyingExpr());
1859 return;
1860
1861 case Type::Atomic:
1862 type = cast<clang::AtomicType>(Val: ty)->getValueType();
1863 break;
1864
1865 case Type::Pipe:
1866 type = cast<clang::PipeType>(Val: ty)->getElementType();
1867 break;
1868 }
1869 } while (type->isVariablyModifiedType());
1870}
1871
1872Address CIRGenFunction::emitVAListRef(const Expr *e) {
1873 if (getContext().getBuiltinVaListType()->isArrayType())
1874 return emitPointerWithAlignment(expr: e);
1875 return emitLValue(e).getAddress();
1876}
1877
1878} // namespace clang::CIRGen
1879