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// This contains code to emit Constant Expr nodes as LLVM code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Address.h"
14#include "CIRGenCXXABI.h"
15#include "CIRGenConstantEmitter.h"
16#include "CIRGenModule.h"
17#include "CIRGenRecordLayout.h"
18#include "mlir/IR/Attributes.h"
19#include "mlir/IR/BuiltinAttributeInterfaces.h"
20#include "mlir/IR/BuiltinAttributes.h"
21#include "clang/AST/APValue.h"
22#include "clang/AST/ASTContext.h"
23#include "clang/AST/Attr.h"
24#include "clang/AST/CharUnits.h"
25#include "clang/AST/OperationKinds.h"
26#include "clang/AST/RecordLayout.h"
27#include "clang/AST/StmtVisitor.h"
28#include "clang/Basic/Builtins.h"
29#include "clang/CIR/Dialect/IR/CIRAttrs.h"
30#include "clang/CIR/Dialect/IR/CIRTypes.h"
31#include "clang/CIR/MissingFeatures.h"
32#include "clang/CodeGenUtils/RecordLayoutUtils.h"
33#include "llvm/ADT/ArrayRef.h"
34#include "llvm/ADT/STLExtras.h"
35#include "llvm/Support/ErrorHandling.h"
36#include <functional>
37#include <iterator>
38#include <optional>
39
40using namespace clang;
41using namespace clang::CIRGen;
42
43/// Collects the initializer elements for the members of \p recordTy that are
44/// stored, in order, taking a zero initializer for any member left unset.
45/// \p elements is indexed by member, which is not the same as being indexed by
46/// stored element. A zero-width bit-field owns no bytes and so takes no
47/// element at all. Fails if a member has no zero initializer.
48static bool
49collectStoredInitializers(CIRGenBuilderTy &builder, cir::RecordType recordTy,
50 llvm::ArrayRef<mlir::Attribute> elements,
51 llvm::SmallVectorImpl<mlir::Attribute> &stored) {
52 for (auto [idx, memberTy] : llvm::enumerate(First: recordTy.getMembers())) {
53 if (!cir::memberOwnsBytes(memberTy))
54 continue;
55 mlir::Attribute elt = elements[idx];
56 if (!elt) {
57 elt = builder.getZeroInitAttr(ty: memberTy);
58 if (!elt)
59 return false;
60 }
61 stored.push_back(Elt: elt);
62 }
63 return true;
64}
65
66//===----------------------------------------------------------------------===//
67// ConstantAggregateBuilder
68//===----------------------------------------------------------------------===//
69
70namespace {
71namespace ConstRecordBuilder {
72// A class to manage the list of 'initializers' for building the record
73// initialization. This abstracts out the APValue and the InitListExpr.
74class RecordBuilderInitList {
75 unsigned initIdx = 0;
76 bool isUnion = false;
77 std::variant<APValue, const InitListExpr *> value;
78
79 bool holdsExpr() const {
80 return std::holds_alternative<const InitListExpr *>(v: value);
81 }
82
83 bool holdsAPValue() const { return std::holds_alternative<APValue>(v: value); }
84
85 const Expr *getExpr() {
86 assert(holdsExpr());
87 return std::get<const InitListExpr *>(v&: value)->getInit(Init: initIdx);
88 }
89
90 mlir::Location getExprLoc(CIRGenModule &cgm) {
91 assert(holdsExpr());
92 return cgm.getLoc(cLoc: std::get<const InitListExpr *>(v&: value)->getBeginLoc());
93 }
94
95 const APValue getAPVal() {
96 assert(holdsAPValue());
97 if (isUnion)
98 return std::get<APValue>(v&: value).getUnionValue();
99 return std::get<APValue>(v&: value).getStructField(i: initIdx);
100 }
101
102public:
103 RecordBuilderInitList(const RecordDecl *rd, APValue val)
104 : isUnion(rd->isUnion()), value(val) {}
105 RecordBuilderInitList(const RecordDecl *rd, const InitListExpr *ile)
106 : isUnion(rd->isUnion()), value(ile) {
107 assert(ile);
108 }
109
110 bool empty() const {
111 if (auto *const *ile = std::get_if<const InitListExpr *>(ptr: &value))
112 return initIdx >= (*ile)->getNumInits();
113
114 // This branch is likely always true, but we guard against it being 'none'
115 // anyway.
116 if (isUnion)
117 return !std::get<APValue>(v: value).isUnion();
118
119 return initIdx >= std::get<APValue>(v: value).getStructNumFields();
120 }
121
122 const FieldDecl *getActiveUnionField() const {
123 if (holdsExpr())
124 return std::get<const InitListExpr *>(v: value)
125 ->getInitializedFieldInUnion();
126 return std::get<APValue>(v: value).getUnionField();
127 }
128
129 // Return whether this is a field that should be skipped for one reason or
130 // another.
131 bool shouldSkip(const FieldDecl *fd) {
132 if (fd->isUnnamedBitField())
133 return true;
134
135 if (holdsExpr() && isa_and_nonnull<NoInitExpr>(Val: getExpr()))
136 return true;
137
138 return false;
139 }
140
141 // Advance the iterator on a 'skipped' field. Note in the case of an
142 // init-list this doesn't advance if its an unnamed bitfield, as those aren't
143 // represented in the AST.
144 void advanceSkip(const FieldDecl *fd) {
145 assert(!isUnion);
146 if (holdsExpr() && fd->isUnnamedBitField())
147 return;
148 ++initIdx;
149 }
150 // Advance the iterator on a 'normal' field, which always just increments the
151 // index.
152 void advance() {
153 assert(!isUnion);
154 ++initIdx;
155 }
156
157 APValue getBase(unsigned idx) {
158 // We could potentially handle this with init-list, but we just skip it
159 // because classic codegen does. If we decide to, we'll probably have to do
160 // something where we get through the init-list elements to make this work
161 // right (sub-init-list?).
162 assert(holdsAPValue());
163
164 return std::get<APValue>(v&: value).getStructBase(i: idx);
165 }
166
167 bool hasSideEffects(const ASTContext &ctx) {
168 if (holdsExpr() && getExpr()->HasSideEffects(Ctx: ctx))
169 return true;
170 // APValue never has side effects.
171 return false;
172 }
173
174 mlir::Attribute emit(ConstantEmitter &emitter, QualType fieldTy) {
175 if (holdsExpr()) {
176 const Expr *e = getExpr();
177 return e ? emitter.tryEmitPrivateForMemory(e, destTy: fieldTy)
178 : emitter.emitNullForMemory(loc: getExprLoc(cgm&: emitter.cgm), t: fieldTy);
179 }
180 return emitter.tryEmitPrivateForMemory(value: getAPVal(), destTy: fieldTy);
181 }
182};
183
184llvm::APInt bitfieldStorageToAPInt(mlir::Attribute attr, unsigned storageSize,
185 bool isBigEndian) {
186 // An empty attribute is just zero of the correct size.
187 if (!attr)
188 return llvm::APInt(storageSize, 0);
189 // An int type is just the value held in the attribute.
190 if (auto intAttr = mlir::dyn_cast<cir::IntAttr>(Val&: attr))
191 return intAttr.getValue();
192
193 // Else we are in the array case, we have to create the big APInt, and fill it
194 // up.
195 llvm::APInt result(storageSize, 0);
196 auto elts = mlir::cast<mlir::ArrayAttr>(
197 Val: mlir::cast<cir::ConstArrayAttr>(Val&: attr).getElts());
198
199 unsigned numBytes = elts.size();
200 for (unsigned i = 0; i != numBytes; ++i) {
201 unsigned byteIdx = isBigEndian ? numBytes - 1 - i : i;
202 llvm::APInt byte =
203 mlir::cast<cir::IntAttr>(Val: elts[i]).getValue().zextOrTrunc(width: 8);
204 result.insertBits(SubBits: byte, bitPosition: byteIdx * 8);
205 }
206 return result;
207}
208
209mlir::Attribute apIntToBitfieldStorage(CIRGenModule &cgm,
210 mlir::Type storageType,
211 const llvm::APInt &value,
212 bool isBigEndian) {
213 // If we'ere an int, just return the new value.
214 if (mlir::isa<cir::IntTypeInterface>(Val: storageType))
215 return cir::IntAttr::get(type: storageType, value);
216
217 // Array of bytes case, fill up an array.
218 CIRGenBuilderTy &builder = cgm.getBuilder();
219 auto arrayTy = mlir::cast<cir::ArrayType>(Val&: storageType);
220
221 unsigned numBytes = arrayTy.getSize();
222 cir::IntType byteTy = builder.getUInt8Ty();
223 llvm::SmallVector<mlir::Attribute, 8> bytes(numBytes);
224
225 for (unsigned i = 0; i != numBytes; ++i) {
226 unsigned byteIdx = isBigEndian ? numBytes - 1 - i : i;
227 bytes[i] = cir::IntAttr::get(type: byteTy, value: value.extractBits(numBits: 8, bitPosition: byteIdx * 8));
228 }
229 return cir::ConstArrayAttr::get(
230 type: arrayTy, elts: mlir::ArrayAttr::get(context: builder.getContext(), value: bytes));
231}
232
233// Bitfields are lowered to either an integer type, or a series of bytes, see
234// getBitfieldStorageType. Because of this, we have to figure out how to store
235// this init value in those. Do this by converting the current value in the
236// 'storage' type to an APInt so we can do our masking correctly, then convert
237// back. The int path is trivial (getValue/create a new one with the new
238// value). The array type requires breaking it up into its constituent values.
239mlir::Attribute updateBitfieldInit(CIRGenModule &cgm,
240 mlir::Attribute existingVal,
241 cir::IntAttr newVal, bool isSigned,
242 const CIRGenBitFieldInfo &bfInfo) {
243 bool isBigEndian = cgm.getDataLayout().isBigEndian();
244 llvm::APInt result =
245 bitfieldStorageToAPInt(attr: existingVal, storageSize: bfInfo.storageSize, isBigEndian);
246
247 llvm::APInt curValue = newVal.getValue();
248 // Make sure we truncate (or properly extend) the existing value for the
249 // number of bits in the bitfield. The AST/Sema doesn't do a good job of
250 // making sure this is done.
251 if (isSigned)
252 curValue = curValue.sextOrTrunc(width: bfInfo.size);
253 else
254 curValue = curValue.zextOrTrunc(width: bfInfo.size);
255
256 // Extend to the full storage size so we can shift/mask.
257 curValue = curValue.zext(width: bfInfo.storageSize);
258
259 // bfInfo.offset is already adjusted for endianness, so no endian-changes need
260 // to happen here.
261 curValue = curValue.shl(shiftAmt: bfInfo.offset);
262 llvm::APInt mask(bfInfo.storageSize, 0);
263 mask.setBits(loBit: bfInfo.offset, hiBit: bfInfo.offset + bfInfo.size);
264
265 result &= ~mask;
266 result |= curValue;
267
268 return apIntToBitfieldStorage(cgm, storageType: bfInfo.storageType, value: result, isBigEndian);
269}
270
271mlir::Attribute
272setBitfieldInit(CIRGenModule &cgm, const CIRGenRecordLayout &cirLayout,
273 CIRGenBuilderTy &builder, const FieldDecl *field,
274 mlir::Attribute existingVal, mlir::Attribute newVal) {
275 const CIRGenBitFieldInfo &info = cirLayout.getBitFieldInfo(fd: field);
276 auto intAttr = mlir::dyn_cast<cir::IntAttr>(Val&: newVal);
277 // This could alternatively be a 'bool' attr here, so do a quick fixup to
278 // get the value correctly initialized.
279 if (!intAttr) {
280 auto boolAttr = mlir::cast<cir::BoolAttr>(Val&: newVal);
281 intAttr = cir::IntAttr::get(
282 type: builder.getUIntNTy(n: 1), value: llvm::APInt(/*numBits=*/1, boolAttr.getValue()));
283 }
284
285 return updateBitfieldInit(
286 cgm, existingVal, newVal: intAttr,
287 isSigned: field->getType()->isSignedIntegerOrEnumerationType(), bfInfo: info);
288}
289
290// Handle potentially-overlapping field rewrite for the const subobject records.
291mlir::Attribute asBaseSubobject(CIRGenBuilderTy &builder, mlir::Attribute attr,
292 cir::RecordType baseSubobjTy) {
293 auto typedAttr = mlir::cast<mlir::TypedAttr>(Val&: attr);
294 if (typedAttr.getType() == baseSubobjTy)
295 return attr;
296
297 if (mlir::isa<cir::ZeroAttr>(Val: attr))
298 return builder.getZeroInitAttr(ty: baseSubobjTy);
299
300 auto recordAttr = mlir::cast<cir::ConstRecordAttr>(Val&: attr);
301 mlir::ArrayAttr members = recordAttr.getMembers();
302 llvm::SmallVector<mlir::Attribute> baseMembers(
303 members.begin(), members.begin() + baseSubobjTy.getNumElements());
304
305 // The base-subobject type is assumed to be a prefix of the complete-object
306 // type (i.e. the complete type minus tail padding). Verify that assumption
307 // holds so a layout change doesn't silently produce a wrong initializer.
308 //
309 // ConstRecordAttr's members skip zero-width bit-fields and store a
310 // bit-field's storage type rather than the bit-field type itself (see
311 // ConstRecordAttr::verify), so baseSubobjTy's raw member list has to be
312 // filtered the same way before comparing against baseMembers index-for-
313 // index.
314 assert(llvm::all_of(
315 llvm::zip_equal(baseMembers,
316 llvm::map_range(llvm::make_filter_range(
317 baseSubobjTy.getMembers(),
318 cir::memberOwnsBytes),
319 cir::memberStorageType)),
320 [](const auto &pair) {
321 auto &[member, baseTy] = pair;
322 return mlir::cast<mlir::TypedAttr>(member).getType() == baseTy;
323 }) &&
324 "base-subobject member type does not match complete-object member "
325 "type at the same index");
326
327 return cir::ConstRecordAttr::get(type: baseSubobjTy,
328 members: builder.getArrayAttr(value: baseMembers));
329}
330
331mlir::Attribute buildRecordHelper(ConstantEmitter &emitter,
332 const RecordDecl *rd,
333 const RecordDecl *vtableBaseTy,
334 RecordBuilderInitList inits, bool handleBases,
335 CharUnits offsetInDerived,
336 bool asBaseSubObj) {
337 CIRGenModule &cgm = emitter.cgm;
338 CIRGenBuilderTy &builder = cgm.getBuilder();
339 const CIRGenRecordLayout &cirLayout =
340 cgm.getTypes().getCIRGenRecordLayout(rd);
341 cir::RecordType recordTy = asBaseSubObj ? cirLayout.getBaseSubobjectCIRType()
342 : cirLayout.getCIRType();
343 // Unions in CIR are represented by all of their types, so we should be able
344 // to just initialize it with whatever the active field is.
345 if (rd->isUnion()) {
346 if (inits.empty())
347 return builder.getZeroInitAttr(ty: recordTy);
348
349 const FieldDecl *activeField = inits.getActiveUnionField();
350 if (!activeField || activeField->isZeroSize(Ctx: cgm.getASTContext()))
351 return builder.getZeroInitAttr(ty: recordTy);
352
353 mlir::Attribute eltAttr = inits.emit(emitter, fieldTy: activeField->getType());
354 if (!eltAttr)
355 return {};
356
357 if (activeField->isBitField())
358 eltAttr = setBitfieldInit(cgm, cirLayout, builder, field: activeField,
359 /*existingVal=*/{}, newVal: eltAttr);
360
361 return cir::ConstRecordAttr::get(type: recordTy, members: builder.getArrayAttr(value: {eltAttr}));
362 }
363
364 llvm::SmallVector<mlir::Attribute> elements(recordTy.getNumElements());
365
366 if (auto *cxxrd = dyn_cast<CXXRecordDecl>(Val: rd)) {
367 const ASTRecordLayout &astLayout =
368 emitter.cgm.getASTContext().getASTRecordLayout(D: cxxrd);
369 if (astLayout.hasOwnVFPtr()) {
370 assert(!cir::MissingFeatures::addressPointerAuthInfo());
371 cir::GlobalViewAttr vptr =
372 emitter.cgm.getCXXABI().getVTableAddressPointAttr(
373 base: BaseSubobject(cxxrd, offsetInDerived),
374 vtableClass: cast<CXXRecordDecl>(Val: vtableBaseTy));
375 assert(vptr && "failed to get vtable address point");
376 elements[0] = vptr;
377 }
378
379 unsigned baseNo = 0;
380 for (const CXXBaseSpecifier &base : cxxrd->bases()) {
381 // Our init-list implementation here just skips bases because classic
382 // compiler does (see the comment in buildRecord). We perhaps COULD do
383 // this, but for now we'll skip them.
384 if (!handleBases)
385 return {};
386
387 if (base.isVirtual())
388 continue;
389
390 const auto *baseDecl = base.getType()->castAsCXXRecordDecl();
391
392 unsigned curBaseNo = baseNo++;
393
394 if (!cirLayout.hasNonVirtualBaseCIRField(rd: baseDecl))
395 continue;
396
397 APValue baseValue = inits.getBase(idx: curBaseNo);
398
399 const ASTRecordLayout &derivedLayout =
400 cgm.getASTContext().getASTRecordLayout(D: cxxrd);
401 CharUnits baseOff =
402 offsetInDerived + derivedLayout.getBaseClassOffset(Base: baseDecl);
403
404 unsigned baseFieldIdx = cirLayout.getNonVirtualBaseCIRFieldNo(rd: baseDecl);
405 elements[baseFieldIdx] = buildRecordHelper(
406 emitter, rd: baseDecl, vtableBaseTy, inits: RecordBuilderInitList(rd, baseValue),
407 handleBases, offsetInDerived: baseOff, /*asBaseSubObj=*/true);
408 }
409
410 if (cxxrd->getNumVBases()) {
411 cgm.errorNYI(cxxrd->getSourceRange(),
412 "buildRecordHelper: virtual base classes");
413 return {};
414 }
415 }
416
417 for (const FieldDecl *field : rd->fields()) {
418 // If we don't have any initializers left, we'll just zero-init below. This
419 // isn't perfectly accurate to classic compiler, since we are potentially
420 // zero-initing padding (instead of leaving it undef), but that is a
421 // complexity we can deal with later if we find it necessary.
422 if (inits.empty())
423 break;
424
425 if (inits.shouldSkip(fd: field)) {
426 inits.advanceSkip(fd: field);
427 continue;
428 }
429
430 // If we didn't lay it out, there is nothing to initialize. This is
431 // either zero size or nothing at all. IF our init has side effects, we
432 // cannot const init this.
433 if (!cirLayout.hasCIRField(fd: field)) {
434 if (inits.hasSideEffects(ctx: cgm.getASTContext()))
435 return {};
436 inits.advance();
437 continue;
438 }
439
440 // A run of bit-fields shares one access unit, and every field of the run
441 // is numbered as that unit, so their bits pack into a single element.
442 unsigned fieldIdx = cirLayout.getCIRFieldNo(fd: field);
443
444 mlir::Attribute eltAttr = inits.emit(emitter, fieldTy: field->getType());
445 inits.advance();
446
447 if (!eltAttr)
448 return {};
449
450 if (field->isBitField()) {
451 elements[fieldIdx] = setBitfieldInit(cgm, cirLayout, builder, field,
452 existingVal: elements[fieldIdx], newVal: eltAttr);
453 } else {
454 if (field->isPotentiallyOverlapping()) {
455 if (auto expectedTy = mlir::dyn_cast<cir::RecordType>(
456 Val: recordTy.getMembers()[fieldIdx]))
457 eltAttr = asBaseSubobject(builder, attr: eltAttr, baseSubobjTy: expectedTy);
458 }
459 elements[fieldIdx] = eltAttr;
460 }
461 }
462
463 // Anything we haven't initialized, we try to zero init. We could/should
464 // probably leave the padding as undef if !CGM.ZeroInitPadding, but that ends
465 // up being quite an additional bit of complexity (but could be implemented in
466 // the field searching above).
467 llvm::SmallVector<mlir::Attribute> storedElements;
468 if (!collectStoredInitializers(builder, recordTy, elements, stored&: storedElements))
469 return {};
470
471 return builder.getConstRecordOrZeroAttr(arrayAttr: builder.getArrayAttr(value: storedElements),
472 recordTy);
473}
474
475mlir::Attribute buildRecord(ConstantEmitter &emitter, InitListExpr *ile,
476 QualType valTy) {
477 // Bail out if we have base classes. We could support these, but they only
478 // arise in C++1z where we will have already constant folded most
479 // interesting cases. FIXME: There are still a few more cases we can handle
480 // this way.
481 const bool handleBases = false;
482 const RecordDecl *rd = ile->getType()->castAsRecordDecl();
483 return buildRecordHelper(emitter, rd, vtableBaseTy: rd, inits: RecordBuilderInitList(rd, ile),
484 handleBases, offsetInDerived: CharUnits::Zero(),
485 /*asBaseSubObj=*/false);
486}
487
488mlir::Attribute buildRecord(ConstantEmitter &emitter, const APValue &val,
489 QualType valTy) {
490 const RecordDecl *rd =
491 valTy->castAs<clang::RecordType>()->getDecl()->getDefinitionOrSelf();
492 return buildRecordHelper(emitter, rd, vtableBaseTy: rd, inits: RecordBuilderInitList(rd, val),
493 /*handleBases=*/true, offsetInDerived: CharUnits::Zero(),
494 /*asBaseSubObj=*/false);
495}
496} // namespace ConstRecordBuilder
497
498//===----------------------------------------------------------------------===//
499// ConstExprEmitter
500//===----------------------------------------------------------------------===//
501
502// This class only needs to handle arrays, structs and unions.
503//
504// In LLVM codegen, when outside C++11 mode, those types are not constant
505// folded, while all other types are handled by constant folding.
506//
507// In CIR codegen, instead of folding things here, we should defer that work
508// to MLIR: do not attempt to do much here.
509class ConstExprEmitter
510 : public StmtVisitor<ConstExprEmitter, mlir::Attribute, QualType> {
511 CIRGenModule &cgm;
512 [[maybe_unused]] ConstantEmitter &emitter;
513
514public:
515 ConstExprEmitter(ConstantEmitter &emitter)
516 : cgm(emitter.cgm), emitter(emitter) {}
517
518 //===--------------------------------------------------------------------===//
519 // Visitor Methods
520 //===--------------------------------------------------------------------===//
521
522 mlir::Attribute VisitStmt(Stmt *s, QualType t) { return {}; }
523
524 mlir::Attribute VisitConstantExpr(ConstantExpr *ce, QualType t) {
525 if (mlir::Attribute result = emitter.tryEmitConstantExpr(ce))
526 return result;
527 return Visit(S: ce->getSubExpr(), P: t);
528 }
529
530 mlir::Attribute VisitParenExpr(ParenExpr *pe, QualType t) {
531 return Visit(S: pe->getSubExpr(), P: t);
532 }
533
534 mlir::Attribute
535 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *pe,
536 QualType t) {
537 return Visit(S: pe->getReplacement(), P: t);
538 }
539
540 mlir::Attribute VisitGenericSelectionExpr(GenericSelectionExpr *ge,
541 QualType t) {
542 return Visit(S: ge->getResultExpr(), P: t);
543 }
544
545 mlir::Attribute VisitChooseExpr(ChooseExpr *ce, QualType t) {
546 return Visit(S: ce->getChosenSubExpr(), P: t);
547 }
548
549 mlir::Attribute VisitCompoundLiteralExpr(CompoundLiteralExpr *e, QualType t) {
550 return Visit(S: e->getInitializer(), P: t);
551 }
552
553 mlir::Attribute VisitCastExpr(CastExpr *e, QualType destType) {
554 if (const auto *ece = dyn_cast<ExplicitCastExpr>(Val: e))
555 cgm.emitExplicitCastExprType(e: ece,
556 cgf: const_cast<CIRGenFunction *>(emitter.cgf));
557
558 Expr *subExpr = e->getSubExpr();
559
560 switch (e->getCastKind()) {
561 case CK_ToUnion:
562 case CK_AddressSpaceConversion:
563 case CK_ReinterpretMemberPointer:
564 cgm.errorNYI(e->getBeginLoc(), "ConstExprEmitter::VisitCastExpr");
565 return {};
566
567 case CK_DerivedToBaseMemberPointer:
568 case CK_BaseToDerivedMemberPointer:
569 // Return {} to let the APValue evaluator handle member pointer type
570 // conversions. The APValue::MemberPointer case in tryEmitPrivate
571 // already builds the correct GEP path for cross-class member pointers.
572 return {};
573
574 case CK_LValueToRValue:
575 case CK_AtomicToNonAtomic:
576 case CK_NonAtomicToAtomic:
577 case CK_NoOp:
578 case CK_ConstructorConversion:
579 return Visit(S: subExpr, P: destType);
580
581 case CK_IntToOCLSampler:
582 llvm_unreachable("global sampler variables are not generated");
583
584 case CK_Dependent:
585 llvm_unreachable("saw dependent cast!");
586
587 case CK_BuiltinFnToFnPtr:
588 llvm_unreachable("builtin functions are handled elsewhere");
589
590 // These will never be supported.
591 case CK_ObjCObjectLValueCast:
592 case CK_ARCProduceObject:
593 case CK_ARCConsumeObject:
594 case CK_ARCReclaimReturnedObject:
595 case CK_ARCExtendBlockObject:
596 case CK_CopyAndAutoreleaseBlockObject:
597 return {};
598
599 // These don't need to be handled here because Evaluate knows how to
600 // evaluate them in the cases where they can be folded.
601 case CK_BitCast:
602 case CK_ToVoid:
603 case CK_Dynamic:
604 case CK_LValueBitCast:
605 case CK_LValueToRValueBitCast:
606 case CK_NullToMemberPointer:
607 case CK_UserDefinedConversion:
608 case CK_CPointerToObjCPointerCast:
609 case CK_BlockPointerToObjCPointerCast:
610 case CK_AnyPointerToBlockPointerCast:
611 case CK_ArrayToPointerDecay:
612 case CK_FunctionToPointerDecay:
613 case CK_BaseToDerived:
614 case CK_DerivedToBase:
615 case CK_UncheckedDerivedToBase:
616 case CK_MemberPointerToBoolean:
617 case CK_VectorSplat:
618 case CK_FloatingRealToComplex:
619 case CK_FloatingComplexToReal:
620 case CK_FloatingComplexToBoolean:
621 case CK_FloatingComplexCast:
622 case CK_FloatingComplexToIntegralComplex:
623 case CK_IntegralRealToComplex:
624 case CK_IntegralComplexToReal:
625 case CK_IntegralComplexToBoolean:
626 case CK_IntegralComplexCast:
627 case CK_IntegralComplexToFloatingComplex:
628 case CK_PointerToIntegral:
629 case CK_PointerToBoolean:
630 case CK_NullToPointer:
631 case CK_IntegralCast:
632 case CK_BooleanToSignedIntegral:
633 case CK_IntegralToPointer:
634 case CK_IntegralToBoolean:
635 case CK_IntegralToFloating:
636 case CK_FloatingToIntegral:
637 case CK_FloatingToBoolean:
638 case CK_FloatingCast:
639 case CK_FloatingToFixedPoint:
640 case CK_FixedPointToFloating:
641 case CK_FixedPointCast:
642 case CK_FixedPointToBoolean:
643 case CK_FixedPointToIntegral:
644 case CK_IntegralToFixedPoint:
645 case CK_ZeroToOCLOpaqueType:
646 case CK_MatrixCast:
647 case CK_HLSLArrayRValue:
648 case CK_HLSLVectorTruncation:
649 case CK_HLSLMatrixTruncation:
650 case CK_HLSLElementwiseCast:
651 case CK_HLSLAggregateSplatCast:
652 return {};
653 }
654 llvm_unreachable("Invalid CastKind");
655 }
656
657 mlir::Attribute VisitCXXDefaultInitExpr(CXXDefaultInitExpr *die, QualType t) {
658 // No need for a DefaultInitExprScope: we don't handle 'this' in a
659 // constant expression.
660 return Visit(S: die->getExpr(), P: t);
661 }
662
663 mlir::Attribute VisitExprWithCleanups(ExprWithCleanups *e, QualType t) {
664 // Since this about constant emission no need to wrap this under a scope.
665 return Visit(S: e->getSubExpr(), P: t);
666 }
667
668 mlir::Attribute VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *e,
669 QualType t) {
670 return Visit(S: e->getSubExpr(), P: t);
671 }
672
673 mlir::Attribute VisitImplicitValueInitExpr(ImplicitValueInitExpr *e,
674 QualType t) {
675 return cgm.emitNullConstantAttr(t);
676 }
677
678 mlir::Attribute VisitInitListExpr(InitListExpr *ile, QualType t) {
679 if (ile->isTransparent())
680 return Visit(S: ile->getInit(Init: 0), P: t);
681
682 if (ile->getType()->isArrayType()) {
683 // If we return null here, the non-constant initializer will take care of
684 // it, but we would prefer to handle it here.
685 assert(!cir::MissingFeatures::constEmitterArrayILE());
686 return {};
687 }
688
689 if (ile->getType()->isRecordType()) {
690 return ConstRecordBuilder::buildRecord(emitter, ile, valTy: t);
691 }
692
693 if (ile->getType()->isVectorType()) {
694 // If we return null here, the non-constant initializer will take care of
695 // it, but we would prefer to handle it here.
696 assert(!cir::MissingFeatures::constEmitterVectorILE());
697 return {};
698 }
699
700 return {};
701 }
702
703 mlir::Attribute VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *e,
704 QualType destType) {
705 mlir::Attribute c = Visit(S: e->getBase(), P: destType);
706 if (!c)
707 return {};
708
709 cgm.errorNYI(e->getBeginLoc(),
710 "ConstExprEmitter::VisitDesignatedInitUpdateExpr");
711 return {};
712 }
713
714 mlir::Attribute VisitCXXConstructExpr(CXXConstructExpr *e, QualType ty) {
715 if (!e->getConstructor()->isTrivial())
716 return {};
717
718 // Only default and copy/move constructors can be trivial.
719 if (e->getNumArgs()) {
720 assert(e->getNumArgs() == 1 && "trivial ctor with > 1 argument");
721 assert(e->getConstructor()->isCopyOrMoveConstructor() &&
722 "trivial ctor has argument but isn't a copy/move ctor");
723
724 Expr *arg = e->getArg(Arg: 0);
725 assert(cgm.getASTContext().hasSameUnqualifiedType(ty, arg->getType()) &&
726 "argument to copy ctor is of wrong type");
727
728 // Look through the temporary; it's just converting the value to an lvalue
729 // to pass it to the constructor.
730 if (auto const *mte = dyn_cast<MaterializeTemporaryExpr>(Val: arg))
731 return Visit(S: mte->getSubExpr(), P: ty);
732
733 // TODO: Investigate whether there are cases that can fall through to here
734 // that need to be handled. This is missing in classic codegen also.
735 assert(!cir::MissingFeatures::ctorConstLvalueToRvalueConversion());
736
737 // Don't try to support arbitrary lvalue-to-rvalue conversions for now.
738 return {};
739 }
740
741 return cgm.getBuilder().getZeroInitAttr(ty: cgm.convertType(type: ty));
742 }
743
744 mlir::Attribute VisitStringLiteral(StringLiteral *e, QualType t) {
745 // This is a string literal initializing an array in an initializer.
746 return cgm.getConstantArrayFromStringLiteral(e);
747 }
748
749 mlir::Attribute VisitObjCEncodeExpr(ObjCEncodeExpr *e, QualType t) {
750 cgm.errorNYI(e->getBeginLoc(), "ConstExprEmitter::VisitObjCEncodeExpr");
751 return {};
752 }
753
754 mlir::Attribute VisitUnaryExtension(const UnaryOperator *e, QualType t) {
755 return Visit(S: e->getSubExpr(), P: t);
756 }
757
758 // Utility methods
759 mlir::Type convertType(QualType t) { return cgm.convertType(type: t); }
760};
761
762// TODO(cir): this can be shared with LLVM's codegen
763static QualType getNonMemoryType(CIRGenModule &cgm, QualType type) {
764 if (const auto *at = type->getAs<AtomicType>()) {
765 return cgm.getASTContext().getQualifiedType(T: at->getValueType(),
766 Qs: type.getQualifiers());
767 }
768 return type;
769}
770} // namespace
771
772//===----------------------------------------------------------------------===//
773// ConstantLValueEmitter
774//===----------------------------------------------------------------------===//
775
776namespace {
777/// A struct which can be used to peephole certain kinds of finalization
778/// that normally happen during l-value emission.
779struct ConstantLValue {
780 llvm::PointerUnion<mlir::Value, mlir::Attribute> value;
781 bool hasOffsetApplied;
782
783 /*implicit*/ ConstantLValue(std::nullptr_t)
784 : value(nullptr), hasOffsetApplied(false) {}
785 /*implicit*/ ConstantLValue(cir::GlobalViewAttr address)
786 : value(address), hasOffsetApplied(false) {}
787 /*implicit*/ ConstantLValue(cir::GlobalOffsetAttr address)
788 : value(address), hasOffsetApplied(true) {}
789 /*implicit*/ ConstantLValue(cir::BlockAddrInfoAttr address)
790 : value(address), hasOffsetApplied(true) {}
791
792 ConstantLValue() : value(nullptr), hasOffsetApplied(false) {}
793};
794
795/// A helper class for emitting constant l-values.
796class ConstantLValueEmitter
797 : public ConstStmtVisitor<ConstantLValueEmitter, ConstantLValue> {
798 CIRGenModule &cgm;
799 ConstantEmitter &emitter;
800 const APValue &value;
801 QualType destType;
802
803 // Befriend StmtVisitorBase so that we don't have to expose Visit*.
804 friend StmtVisitorBase;
805
806public:
807 ConstantLValueEmitter(ConstantEmitter &emitter, const APValue &value,
808 QualType destType)
809 : cgm(emitter.cgm), emitter(emitter), value(value), destType(destType) {}
810
811 mlir::Attribute tryEmit();
812
813private:
814 mlir::Attribute tryEmitAbsolute(mlir::Type destTy);
815 ConstantLValue tryEmitBase(const APValue::LValueBase &base);
816
817 ConstantLValue VisitStmt(const Stmt *s) { return nullptr; }
818 ConstantLValue VisitConstantExpr(const ConstantExpr *e);
819 ConstantLValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *e);
820 ConstantLValue VisitStringLiteral(const StringLiteral *e);
821 ConstantLValue VisitObjCBoxedExpr(const ObjCBoxedExpr *e);
822 ConstantLValue VisitObjCEncodeExpr(const ObjCEncodeExpr *e);
823 ConstantLValue VisitObjCStringLiteral(const ObjCStringLiteral *e);
824 ConstantLValue VisitPredefinedExpr(const PredefinedExpr *e);
825 ConstantLValue VisitAddrLabelExpr(const AddrLabelExpr *e);
826 ConstantLValue VisitCallExpr(const CallExpr *e);
827 ConstantLValue VisitBlockExpr(const BlockExpr *e);
828 ConstantLValue VisitCXXTypeidExpr(const CXXTypeidExpr *e);
829 ConstantLValue
830 VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *e);
831
832 /// Return GEP-like value offset, or std::nullopt if the offset doesn't
833 /// designate a subelement of \p ty and must be described as a byte offset.
834 /// A null ArrayAttr means the offset is zero, so no indexing is needed.
835 std::optional<mlir::ArrayAttr> getOffsetIndices(mlir::Type ty) {
836 int64_t offset = value.getLValueOffset().getQuantity();
837 cir::CIRDataLayout layout(cgm.getModule());
838 SmallVector<int64_t, 3> idxVec;
839 if (!cgm.getBuilder().computeGlobalViewIndicesFromFlatOffset(
840 offset, ty, layout, indices&: idxVec))
841 return std::nullopt;
842
843 llvm::SmallVector<mlir::Attribute, 3> indices;
844 for (int64_t i : idxVec) {
845 mlir::IntegerAttr intAttr = cgm.getBuilder().getI32IntegerAttr(value: i);
846 indices.push_back(Elt: intAttr);
847 }
848
849 if (indices.empty())
850 return mlir::ArrayAttr{};
851 return cgm.getBuilder().getArrayAttr(value: indices);
852 }
853
854 /// Apply the value offset to the given constant.
855 ConstantLValue applyOffset(ConstantLValue &c) {
856 // Handle attribute constant LValues.
857 if (auto attr = mlir::dyn_cast<mlir::Attribute>(Val&: c.value)) {
858 if (auto gv = mlir::dyn_cast<cir::GlobalViewAttr>(Val&: attr)) {
859 auto baseTy = mlir::cast<cir::PointerType>(Val: gv.getType()).getPointee();
860 mlir::Type destTy = cgm.getTypes().convertTypeForMem(destType);
861 assert(!gv.getIndices() && "Global view is already indexed");
862 std::optional<mlir::ArrayAttr> indices = getOffsetIndices(ty: baseTy);
863 if (!indices)
864 return cir::GlobalOffsetAttr::get(
865 type: destTy, symbol: gv.getSymbol(), offset: value.getLValueOffset().getQuantity());
866 return cir::GlobalViewAttr::get(type: destTy, symbol: gv.getSymbol(), indices: *indices);
867 }
868 llvm_unreachable("Unsupported attribute type to offset");
869 }
870
871 cgm.errorNYI(feature: "ConstantLValue: non-attribute offset");
872 return {};
873 }
874};
875
876} // namespace
877
878mlir::Attribute ConstantLValueEmitter::tryEmit() {
879 const APValue::LValueBase &base = value.getLValueBase();
880
881 // The destination type should be a pointer or reference
882 // type, but it might also be a cast thereof.
883 //
884 // FIXME: the chain of casts required should be reflected in the APValue.
885 // We need this in order to correctly handle things like a ptrtoint of a
886 // non-zero null pointer and addrspace casts that aren't trivially
887 // represented in LLVM IR.
888 mlir::Type destTy = cgm.getTypes().convertTypeForMem(destType);
889 assert((mlir::isa<cir::PointerType>(destTy) ||
890 mlir::isa<cir::IntType>(destTy)) &&
891 "constant lvalue destination must be pointer or integer");
892
893 // If there's no base at all, this is a null or absolute pointer,
894 // possibly cast back to an integer type.
895 if (!base)
896 return tryEmitAbsolute(destTy);
897
898 // Otherwise, try to emit the base.
899 ConstantLValue result = tryEmitBase(base);
900
901 // If that failed, we're done.
902 llvm::PointerUnion<mlir::Value, mlir::Attribute> &value = result.value;
903 if (!value)
904 return {};
905
906 // Apply the offset if necessary and not already done.
907 if (!result.hasOffsetApplied)
908 value = applyOffset(c&: result).value;
909
910 // CIR does not yet support signing constant lvalue initializers with pointer
911 // authentication. Classic CodeGen signs the offset-adjusted pointer here
912 // before the final pointer cast or ptrtoint conversion.
913 if (PointerAuthQualifier pointerAuth = destType.getPointerAuth()) {
914 cgm.errorNYI(feature: "ConstantLValueEmitter: pointer authentication");
915 return {};
916 }
917
918 // Convert to the appropriate type; this could be an lvalue for
919 // an integer. FIXME: performAddrSpaceCast
920 if (auto attr = mlir::dyn_cast<mlir::Attribute>(Val&: value)) {
921 if (auto gv = mlir::dyn_cast<cir::GlobalViewAttr>(Val&: attr))
922 return cir::GlobalViewAttr::get(type: destTy, symbol: gv.getSymbol(), indices: gv.getIndices());
923
924 if (mlir::isa<cir::PointerType>(Val: destTy))
925 return attr;
926 }
927
928 cgm.errorNYI(feature: "ConstantLValueEmitter: non-attribute pointer or integer");
929 return {};
930}
931
932/// Try to emit an absolute l-value, such as a null pointer or an integer
933/// bitcast to pointer type.
934mlir::Attribute ConstantLValueEmitter::tryEmitAbsolute(mlir::Type destTy) {
935 // If we're producing a pointer, this is easy.
936 auto destPtrTy = mlir::cast<cir::PointerType>(Val&: destTy);
937 return cgm.getBuilder().getConstPtrAttr(
938 type: destPtrTy, value: value.getLValueOffset().getQuantity());
939}
940
941ConstantLValue
942ConstantLValueEmitter::tryEmitBase(const APValue::LValueBase &base) {
943 // Handle values.
944 if (const ValueDecl *d = base.dyn_cast<const ValueDecl *>()) {
945 // The constant always points to the canonical declaration. We want to look
946 // at properties of the most recent declaration at the point of emission.
947 d = cast<ValueDecl>(Val: d->getMostRecentDecl());
948
949 if (auto *fd = dyn_cast<FunctionDecl>(Val: d)) {
950 cir::FuncOp fop = cgm.getRawFunctionPointer(GD: GlobalDecl{fd});
951 if (!fop)
952 return {};
953 CIRGenBuilderTy &builder = cgm.getBuilder();
954 mlir::MLIRContext *mlirContext = builder.getContext();
955 // Use the destination pointer type (e.g. struct field type), not
956 // fop.getFunctionType(), so initializers stay valid when a no-prototype
957 // FuncOp is later replaced by a prototyped definition with the same
958 // symbol. CIR allows the view type to differ from the symbol's type.
959 mlir::Type destTy = cgm.getTypes().convertTypeForMem(destType);
960 cir::PointerType ptrTy =
961 mlir::isa<cir::PointerType>(Val: destTy)
962 ? mlir::cast<cir::PointerType>(Val&: destTy)
963 : cir::PointerType::get(pointee: fop.getFunctionType());
964 mlir::StringAttr symName = fop.getSymNameAttr();
965 // On the HIP host, the address of a kernel is the address of its kernel
966 // handle, not of its device stub. CUDA uses the device stub itself as
967 // the kernel handle.
968 if (cgm.getLangOpts().HIP && !cgm.getLangOpts().CUDAIsDevice &&
969 fd->hasAttr<CUDAGlobalAttr>())
970 symName = mlir::cast<cir::GlobalOp>(
971 Val: cgm.getCUDARuntime().getKernelHandle(fn: fop, gd: fd))
972 .getSymNameAttr();
973 return cir::GlobalViewAttr::get(
974 type: ptrTy, symbol: mlir::FlatSymbolRefAttr::get(ctx: mlirContext, value: symName));
975 }
976
977 // A weakref refers to its alias target. Handle non-function versions here.
978 if (d->hasAttr<WeakRefAttr>()) {
979 auto aliasee = cast_or_null<cir::GlobalOp>(Val: cgm.getWeakRefReference(vd: d));
980 if (!aliasee)
981 return {};
982 return cgm.getBuilder().getGlobalViewAttr(globalOp: aliasee);
983 }
984
985 if (auto *vd = dyn_cast<VarDecl>(Val: d)) {
986 // We can never refer to a variable with local storage.
987 if (!vd->hasLocalStorage()) {
988 if (vd->isFileVarDecl() || vd->hasExternalStorage())
989 return cgm.getAddrOfGlobalVarAttr(d: vd);
990
991 if (vd->isLocalVarDecl()) {
992 cir::GlobalLinkageKind linkage = cgm.getCIRLinkageVarDefinition(vd);
993 return cgm.getBuilder().getGlobalViewAttr(
994 globalOp: cgm.getOrCreateStaticVarDecl(d: *vd, linkage));
995 }
996 }
997 }
998
999 if (isa<MSGuidDecl>(Val: d))
1000 cgm.errorNYI(d->getSourceRange(), "ConstantLValueEmitter: MSGuidDecl");
1001
1002 if (const auto *gcd = dyn_cast<UnnamedGlobalConstantDecl>(Val: d))
1003 return cgm.getBuilder().getGlobalViewAttr(
1004 globalOp: cgm.getAddrOfUnnamedGlobalConstantDecl(gcd));
1005
1006 if (const auto *tpo = dyn_cast<TemplateParamObjectDecl>(Val: d))
1007 return cgm.getBuilder().getGlobalViewAttr(
1008 globalOp: cgm.getAddrOfTemplateParamObject(tpo));
1009
1010 return {};
1011 }
1012
1013 // Handle typeid(T).
1014 if (TypeInfoLValue typeInfo = base.dyn_cast<TypeInfoLValue>())
1015 return cast<cir::GlobalViewAttr>(Val: cgm.getAddrOfRTTIDescriptor(
1016 loc: cgm.getBuilder().getUnknownLoc(), ty: QualType(typeInfo.getType(), 0)));
1017
1018 // Otherwise, it must be an expression.
1019 return Visit(S: base.get<const Expr *>());
1020}
1021
1022ConstantLValue ConstantLValueEmitter::VisitConstantExpr(const ConstantExpr *e) {
1023 cgm.errorNYI(e->getSourceRange(), "ConstantLValueEmitter: constant expr");
1024 return {};
1025}
1026
1027static cir::GlobalViewAttr
1028tryEmitGlobalCompoundLiteral(ConstantEmitter &emitter,
1029 const CompoundLiteralExpr *e) {
1030 CIRGenModule &cgm = emitter.cgm;
1031 CIRGenBuilderTy &builder = cgm.getBuilder();
1032 CharUnits align = cgm.getASTContext().getTypeAlignInChars(T: e->getType());
1033
1034 if (cir::GlobalOp addr = cgm.getAddrOfConstantCompoundLiteralIfEmitted(e))
1035 return builder.getGlobalViewAttr(globalOp: addr);
1036
1037 assert(!cir::MissingFeatures::addressSpace());
1038 mlir::Attribute c =
1039 emitter.tryEmitForInitializer(e: e->getInitializer(), destType: e->getType());
1040 if (!c) {
1041 assert(!e->isFileScope() &&
1042 "file-scope compound literal did not have constant initializer!");
1043 return {};
1044 }
1045
1046 auto typedInit = mlir::cast<mlir::TypedAttr>(Val&: c);
1047 bool isConstant = e->getType().isConstantStorage(Ctx: cgm.getASTContext(),
1048 /*ExcludeCtor=*/true,
1049 /*ExcludeDtor=*/false);
1050
1051 std::string name = cgm.getUniqueGlobalName(baseName: ".compoundliteral");
1052 mlir::Location loc = cgm.getLoc(cRange: e->getSourceRange());
1053 cir::GlobalOp gv =
1054 cgm.createGlobalOp(loc, name, t: typedInit.getType(), isConstant);
1055 gv.setLinkage(cir::GlobalLinkageKind::InternalLinkage);
1056 gv.setAlignment(align.getAsAlign().value());
1057 CIRGenModule::setInitializer(op&: gv, value: c);
1058
1059 emitter.finalize(gv);
1060 cgm.setAddrOfConstantCompoundLiteral(e, gv);
1061 return builder.getGlobalViewAttr(globalOp: gv);
1062}
1063
1064ConstantLValue
1065ConstantLValueEmitter::VisitCompoundLiteralExpr(const CompoundLiteralExpr *e) {
1066 ConstantEmitter compoundLiteralEmitter(cgm, emitter.cgf);
1067 compoundLiteralEmitter.setInConstantContext(emitter.isInConstantContext());
1068 return tryEmitGlobalCompoundLiteral(emitter&: compoundLiteralEmitter, e);
1069}
1070
1071ConstantLValue
1072ConstantLValueEmitter::VisitStringLiteral(const StringLiteral *e) {
1073 return cgm.getAddrOfConstantStringFromLiteral(s: e);
1074}
1075
1076ConstantLValue
1077ConstantLValueEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *e) {
1078 cgm.errorNYI(e->getSourceRange(), "ConstantLValueEmitter: objc encode expr");
1079 return {};
1080}
1081
1082ConstantLValue
1083ConstantLValueEmitter::VisitObjCStringLiteral(const ObjCStringLiteral *e) {
1084 cgm.errorNYI(e->getSourceRange(),
1085 "ConstantLValueEmitter: objc string literal");
1086 return {};
1087}
1088
1089ConstantLValue
1090ConstantLValueEmitter::VisitObjCBoxedExpr(const ObjCBoxedExpr *e) {
1091 cgm.errorNYI(e->getSourceRange(), "ConstantLValueEmitter: objc boxed expr");
1092 return {};
1093}
1094
1095ConstantLValue
1096ConstantLValueEmitter::VisitPredefinedExpr(const PredefinedExpr *e) {
1097 return cgm.getAddrOfConstantStringFromLiteral(s: e->getFunctionName());
1098}
1099
1100ConstantLValue
1101ConstantLValueEmitter::VisitAddrLabelExpr(const AddrLabelExpr *e) {
1102 // A label address taken in a constant context, e.g. a static computed-goto
1103 // dispatch table `static const void *tbl[] = {&&L1, &&L2}`. GotoSolver later
1104 // collects this block-address attribute (here, from a global initializer) so
1105 // the label survives and joins the indirect branch's successors. A label is
1106 // always function-local, so cgf is set here.
1107 assert(emitter.cgf && "label address in a constant requires a function");
1108 CIRGenFunction &cgf = *const_cast<CIRGenFunction *>(emitter.cgf);
1109 auto func = cast<cir::FuncOp>(Val: cgf.curFn);
1110 return cir::BlockAddrInfoAttr::get(context: &cgf.getMLIRContext(), func_name: func.getSymName(),
1111 label_name: e->getLabel()->getName());
1112}
1113
1114ConstantLValue ConstantLValueEmitter::VisitCallExpr(const CallExpr *e) {
1115 cgm.errorNYI(e->getSourceRange(), "ConstantLValueEmitter: call expr");
1116 return {};
1117}
1118
1119ConstantLValue ConstantLValueEmitter::VisitBlockExpr(const BlockExpr *e) {
1120 cgm.errorNYI(e->getSourceRange(), "ConstantLValueEmitter: block expr");
1121 return {};
1122}
1123
1124ConstantLValue
1125ConstantLValueEmitter::VisitCXXTypeidExpr(const CXXTypeidExpr *e) {
1126 if (e->isTypeOperand())
1127 return cast<cir::GlobalViewAttr>(
1128 Val: cgm.getAddrOfRTTIDescriptor(loc: cgm.getLoc(cRange: e->getSourceRange()),
1129 ty: e->getTypeOperand(Context: cgm.getASTContext())));
1130 return cast<cir::GlobalViewAttr>(Val: cgm.getAddrOfRTTIDescriptor(
1131 loc: cgm.getLoc(cRange: e->getSourceRange()), ty: e->getExprOperand()->getType()));
1132}
1133
1134ConstantLValue ConstantLValueEmitter::VisitMaterializeTemporaryExpr(
1135 const MaterializeTemporaryExpr *e) {
1136 assert(e->getStorageDuration() == SD_Static);
1137 const Expr *inner = e->getSubExpr()->skipRValueSubobjectAdjustments();
1138 mlir::Operation *global = cgm.getAddrOfGlobalTemporary(mte: e, init: inner);
1139 return ConstantLValue(
1140 cgm.getBuilder().getGlobalViewAttr(globalOp: mlir::cast<cir::GlobalOp>(Val: global)));
1141}
1142
1143//===----------------------------------------------------------------------===//
1144// ConstantEmitter
1145//===----------------------------------------------------------------------===//
1146
1147mlir::Attribute ConstantEmitter::tryEmitForInitializer(const VarDecl &d) {
1148 initializeNonAbstract();
1149 return markIfFailed(init: tryEmitPrivateForVarInit(d));
1150}
1151
1152mlir::Attribute ConstantEmitter::tryEmitForInitializer(const Expr *e,
1153 QualType destType) {
1154 initializeNonAbstract();
1155 return markIfFailed(init: tryEmitPrivateForMemory(e, destTy: destType));
1156}
1157
1158mlir::Attribute ConstantEmitter::emitForInitializer(const APValue &value,
1159 QualType destType) {
1160 initializeNonAbstract();
1161 auto c = tryEmitPrivateForMemory(value, destTy: destType);
1162 assert(c && "couldn't emit constant value non-abstractly?");
1163 return c;
1164}
1165
1166void ConstantEmitter::finalize(cir::GlobalOp gv) {
1167 assert(initializedNonAbstract &&
1168 "finalizing emitter that was used for abstract emission?");
1169 assert(!finalized && "finalizing emitter multiple times");
1170 assert(!gv.isDeclaration());
1171#ifndef NDEBUG
1172 // Note that we might also be Failed.
1173 finalized = true;
1174#endif // NDEBUG
1175}
1176
1177mlir::Attribute
1178ConstantEmitter::tryEmitAbstractForInitializer(const VarDecl &d) {
1179 AbstractStateRAII state(*this, true);
1180 return tryEmitPrivateForVarInit(d);
1181}
1182
1183ConstantEmitter::~ConstantEmitter() {
1184 assert((!initializedNonAbstract || finalized || failed) &&
1185 "not finalized after being initialized for non-abstract emission");
1186}
1187
1188static mlir::TypedAttr emitNullConstantForBase(CIRGenModule &cgm,
1189 mlir::Type baseType,
1190 const CXXRecordDecl *baseDecl);
1191
1192static mlir::TypedAttr emitNullConstant(CIRGenModule &cgm, const RecordDecl *rd,
1193 bool asCompleteObject) {
1194 const CIRGenRecordLayout &layout = cgm.getTypes().getCIRGenRecordLayout(rd);
1195 mlir::Type ty = (asCompleteObject ? layout.getCIRType()
1196 : layout.getBaseSubobjectCIRType());
1197 auto recordTy = mlir::cast<cir::RecordType>(Val&: ty);
1198
1199 unsigned numElements = rd->isUnion() ? 1 : recordTy.getNumElements();
1200 SmallVector<mlir::Attribute> elements(numElements);
1201
1202 auto *cxxrd = dyn_cast<CXXRecordDecl>(Val: rd);
1203 // Fill in all the bases.
1204 if (cxxrd) {
1205 for (const CXXBaseSpecifier &base : cxxrd->bases()) {
1206 if (base.isVirtual()) {
1207 // Ignore virtual bases; if we're laying out for a complete
1208 // object, we'll lay these out later.
1209 continue;
1210 }
1211
1212 const auto *baseDecl = base.getType()->castAsCXXRecordDecl();
1213 // Ignore empty bases.
1214 if (CodeGenUtils::isEmptyRecordForLayout(Ctx: cgm.getASTContext(),
1215 T: base.getType()) ||
1216 cgm.getASTContext()
1217 .getASTRecordLayout(D: baseDecl)
1218 .getNonVirtualSize()
1219 .isZero())
1220 continue;
1221
1222 unsigned fieldIndex = layout.getNonVirtualBaseCIRFieldNo(rd: baseDecl);
1223 mlir::Type baseType = recordTy.getElementType(idx: fieldIndex);
1224 elements[fieldIndex] = emitNullConstantForBase(cgm, baseType, baseDecl);
1225 }
1226 }
1227
1228 // Fill in all the fields.
1229 for (const FieldDecl *field : rd->fields()) {
1230 // Fill in non-bitfields. (Bitfields always use a zero pattern, which we
1231 // will fill in later.)
1232 if (!field->isBitField() &&
1233 !CodeGenUtils::isEmptyFieldForLayout(Ctx: cgm.getASTContext(), FD: field)) {
1234 unsigned fieldIndex = layout.getCIRFieldNo(fd: field);
1235 elements[fieldIndex] = cgm.emitNullConstantAttr(t: field->getType());
1236 }
1237
1238 // For unions, stop after the first named field.
1239 if (rd->isUnion()) {
1240 if (field->getIdentifier())
1241 break;
1242 if (const auto *fieldRD = field->getType()->getAsRecordDecl())
1243 if (fieldRD->findFirstNamedDataMember())
1244 break;
1245 }
1246 }
1247
1248 // Fill in the virtual bases, if we're working with the complete object.
1249 if (cxxrd && asCompleteObject) {
1250 for ([[maybe_unused]] const CXXBaseSpecifier &vbase : cxxrd->vbases()) {
1251 cgm.errorNYI(vbase.getSourceRange(), "emitNullConstant: virtual base");
1252 return {};
1253 }
1254 }
1255
1256 mlir::MLIRContext *mlirContext = recordTy.getContext();
1257
1258 // A union takes a single element, for whichever member stands in for the
1259 // active one.
1260 if (rd->isUnion()) {
1261 if (!elements[0])
1262 elements[0] =
1263 cgm.getBuilder().getZeroInitAttr(ty: recordTy.getElementType(idx: 0));
1264 return cir::ConstRecordAttr::get(
1265 type: recordTy, members: mlir::ArrayAttr::get(context: mlirContext, value: elements));
1266 }
1267
1268 // Now go through all other fields and zero them out.
1269 llvm::SmallVector<mlir::Attribute> storedElements;
1270 if (!collectStoredInitializers(builder&: cgm.getBuilder(), recordTy, elements,
1271 stored&: storedElements))
1272 return {};
1273
1274 return cir::ConstRecordAttr::get(
1275 type: recordTy, members: mlir::ArrayAttr::get(context: mlirContext, value: storedElements));
1276}
1277
1278/// Emit the null constant for a base subobject.
1279static mlir::TypedAttr emitNullConstantForBase(CIRGenModule &cgm,
1280 mlir::Type baseType,
1281 const CXXRecordDecl *baseDecl) {
1282 const CIRGenRecordLayout &baseLayout =
1283 cgm.getTypes().getCIRGenRecordLayout(rd: baseDecl);
1284
1285 // Just zero out bases that don't have any pointer to data members.
1286 if (baseLayout.isZeroInitializableAsBase())
1287 return cgm.getBuilder().getZeroInitAttr(ty: baseType);
1288
1289 // Otherwise, we can just use its null constant.
1290 return emitNullConstant(cgm, rd: baseDecl, /*asCompleteObject=*/false);
1291}
1292
1293mlir::Attribute ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &d) {
1294 // Make a quick check if variable can be default NULL initialized
1295 // and avoid going through rest of code which may do, for c++11,
1296 // initialization of memory to all NULLs.
1297 if (!d.hasLocalStorage()) {
1298 QualType ty = cgm.getASTContext().getBaseElementType(QT: d.getType());
1299 if (ty->isRecordType()) {
1300 if (const auto *e = dyn_cast_or_null<CXXConstructExpr>(Val: d.getInit())) {
1301 const CXXConstructorDecl *cd = e->getConstructor();
1302 if (cd->isTrivial() && cd->isDefaultConstructor())
1303 return cgm.emitNullConstantAttr(t: d.getType());
1304 }
1305 }
1306 }
1307 inConstantContext = d.hasConstantInitialization();
1308
1309 const Expr *e = d.getInit();
1310 assert(e && "No initializer to emit");
1311
1312 QualType destType = d.getType();
1313
1314 if (!destType->isReferenceType()) {
1315 QualType nonMemoryDestType = getNonMemoryType(cgm, type: destType);
1316 if (mlir::Attribute c = ConstExprEmitter(*this).Visit(S: const_cast<Expr *>(e),
1317 P: nonMemoryDestType))
1318 return emitForMemory(c, destType);
1319 }
1320
1321 // Try to emit the initializer. Note that this can allow some things that
1322 // are not allowed by tryEmitPrivateForMemory alone.
1323 if (const APValue *value = d.evaluateValue())
1324 return tryEmitPrivateForMemory(value: *value, destTy: destType);
1325
1326 return {};
1327}
1328
1329mlir::Attribute ConstantEmitter::tryEmitAbstract(const Expr *e,
1330 QualType destType) {
1331 AbstractStateRAII state{*this, true};
1332 return tryEmitPrivate(e, destType);
1333}
1334
1335mlir::Attribute ConstantEmitter::tryEmitConstantExpr(const ConstantExpr *ce) {
1336 if (!ce->hasAPValueResult())
1337 return {};
1338
1339 QualType retType = ce->getType();
1340 if (ce->isGLValue())
1341 retType = cgm.getASTContext().getLValueReferenceType(T: retType);
1342
1343 return emitAbstract(loc: ce->getBeginLoc(), value: ce->getAPValueResult(), destType: retType);
1344}
1345
1346mlir::Attribute ConstantEmitter::tryEmitPrivateForMemory(const Expr *e,
1347 QualType destType) {
1348 QualType nonMemoryDestType = getNonMemoryType(cgm, type: destType);
1349 mlir::TypedAttr c = tryEmitPrivate(e, destType: nonMemoryDestType);
1350 if (c) {
1351 mlir::Attribute attr = emitForMemory(c, destType);
1352 return mlir::cast<mlir::TypedAttr>(Val&: attr);
1353 }
1354 return nullptr;
1355}
1356
1357mlir::Attribute ConstantEmitter::tryEmitPrivateForMemory(const APValue &value,
1358 QualType destType) {
1359 QualType nonMemoryDestType = getNonMemoryType(cgm, type: destType);
1360 mlir::Attribute c = tryEmitPrivate(value, destType: nonMemoryDestType);
1361 return (c ? emitForMemory(c, destType) : nullptr);
1362}
1363
1364mlir::Attribute ConstantEmitter::emitAbstract(const Expr *e,
1365 QualType destType) {
1366 AbstractStateRAII state{*this, true};
1367 mlir::Attribute c = mlir::cast<mlir::Attribute>(Val: tryEmitPrivate(e, destType));
1368 if (!c)
1369 cgm.errorNYI(e->getSourceRange(),
1370 "emitAbstract failed, emit null constaant");
1371 return c;
1372}
1373
1374mlir::Attribute ConstantEmitter::emitAbstract(SourceLocation loc,
1375 const APValue &value,
1376 QualType destType) {
1377 AbstractStateRAII state(*this, true);
1378 mlir::Attribute c = tryEmitPrivate(value, destType);
1379 if (!c)
1380 cgm.errorNYI(loc, "emitAbstract failed, emit null constaant");
1381 return c;
1382}
1383
1384mlir::Attribute ConstantEmitter::emitNullForMemory(mlir::Location loc,
1385 CIRGenModule &cgm,
1386 QualType t) {
1387 cir::ConstantOp cstOp =
1388 cgm.emitNullConstant(t, loc).getDefiningOp<cir::ConstantOp>();
1389 assert(cstOp && "expected cir.const op");
1390 return emitForMemory(cgm, c: cstOp.getValue(), destTy: t);
1391}
1392
1393mlir::Attribute ConstantEmitter::emitForMemory(mlir::Attribute c,
1394 QualType destType) {
1395 return emitForMemory(cgm, c, destTy: destType);
1396}
1397
1398mlir::Attribute ConstantEmitter::emitForMemory(CIRGenModule &cgm,
1399 mlir::Attribute c,
1400 QualType destType) {
1401 // For an _Atomic-qualified constant, we may need to add tail padding.
1402 if (const auto *at = destType->getAs<AtomicType>()) {
1403 QualType destValueType = at->getValueType();
1404 c = emitForMemory(cgm, c, destType: destValueType);
1405
1406 uint64_t innerSize = cgm.getASTContext().getTypeSize(T: destValueType);
1407 uint64_t outerSize = cgm.getASTContext().getTypeSize(T: destType);
1408 if (innerSize == outerSize)
1409 return c;
1410
1411 assert(innerSize < outerSize && "emitted over-large constant for atomic");
1412 cgm.errorNYI(feature: "emitForMemory: tail padding in atomic initializer");
1413 }
1414
1415 // In HLSL bool vectors are stored in memory as a vector of i32
1416 if (destType->isExtVectorBoolType() &&
1417 !destType->isPackedVectorBoolType(ctx: cgm.getASTContext())) {
1418 cgm.errorNYI(feature: "emitForMemory: zero-extend HLSL bool vectors");
1419 }
1420
1421 // CIR represents source types as literally as possible. Some types, such as
1422 // bool and _BitInt(N), are kept at their literal width here and expanded to
1423 // their wider "in memory" types during lowering to the LLVM dialect, so the
1424 // constant is already in the right form and needs no adjustment.
1425
1426 return c;
1427}
1428
1429mlir::TypedAttr ConstantEmitter::tryEmitPrivate(const Expr *e,
1430 QualType destType) {
1431 assert(!destType->isVoidType() && "can't emit a void constant");
1432
1433 if (mlir::Attribute c =
1434 ConstExprEmitter(*this).Visit(S: const_cast<Expr *>(e), P: destType))
1435 return llvm::dyn_cast<mlir::TypedAttr>(Val&: c);
1436
1437 Expr::EvalResult result;
1438
1439 bool success = false;
1440
1441 if (destType->isReferenceType())
1442 success = e->EvaluateAsLValue(Result&: result, Ctx: cgm.getASTContext());
1443 else
1444 success =
1445 e->EvaluateAsRValue(Result&: result, Ctx: cgm.getASTContext(), InConstantContext: inConstantContext);
1446
1447 if (success && !result.hasSideEffects()) {
1448 mlir::Attribute c = tryEmitPrivate(value: result.Val, destType);
1449 return llvm::dyn_cast<mlir::TypedAttr>(Val&: c);
1450 }
1451
1452 return nullptr;
1453}
1454
1455mlir::Attribute ConstantEmitter::tryEmitPrivate(const APValue &value,
1456 QualType destType) {
1457 auto &builder = cgm.getBuilder();
1458 switch (value.getKind()) {
1459 case APValue::None:
1460 case APValue::Indeterminate:
1461 cgm.errorNYI(feature: "ConstExprEmitter::tryEmitPrivate none or indeterminate");
1462 return {};
1463 case APValue::Int: {
1464 mlir::Type ty = cgm.convertType(type: destType);
1465 if (mlir::isa<cir::BoolType>(Val: ty))
1466 return builder.getCIRBoolAttr(state: value.getInt().getZExtValue());
1467 assert(mlir::isa<cir::IntType>(ty) && "expected integral type");
1468 return cir::IntAttr::get(type: ty, value: value.getInt());
1469 }
1470 case APValue::Float: {
1471 mlir::Type ty = cgm.convertType(type: destType);
1472 assert(mlir::isa<cir::FPTypeInterface>(ty) &&
1473 "expected floating-point type");
1474 return cir::FPAttr::get(type: ty, value: value.getFloat());
1475 }
1476 case APValue::Array: {
1477 const ArrayType *arrayTy = cgm.getASTContext().getAsArrayType(T: destType);
1478 const QualType arrayElementTy = arrayTy->getElementType();
1479 const unsigned numElements = value.getArraySize();
1480 const unsigned numInitElts = value.getArrayInitializedElts();
1481
1482 mlir::TypedAttr filler;
1483 if (value.hasArrayFiller()) {
1484 mlir::Attribute fillerTemp =
1485 tryEmitPrivate(value: value.getArrayFiller(), destType: arrayElementTy);
1486 if (!fillerTemp)
1487 return {};
1488 filler = dyn_cast<mlir::TypedAttr>(Val&: fillerTemp);
1489 if (!filler) {
1490 cgm.errorNYI(feature: "ConstExprEmitter::tryEmitPrivate array filler should "
1491 "always be typed");
1492 return {};
1493 }
1494 }
1495
1496 CIRGenBuilderTy &builder = cgm.getBuilder();
1497 cir::ArrayType desiredType =
1498 cast<cir::ArrayType>(Val: cgm.convertType(type: destType));
1499
1500 llvm::SmallVector<mlir::Attribute> elts;
1501 if (!filler || builder.isNullValue(attr: filler))
1502 elts.reserve(N: numInitElts);
1503 else
1504 elts.reserve(N: numElements);
1505
1506 // Fill in the known values.
1507 for (unsigned i = 0; i < numInitElts; ++i) {
1508 const APValue &arrayElement = value.getArrayInitializedElt(I: i);
1509 const mlir::Attribute element =
1510 tryEmitPrivateForMemory(value: arrayElement, destType: arrayElementTy);
1511 if (!element)
1512 return {};
1513
1514 elts.push_back(Elt: element);
1515 }
1516
1517 // If we have an actual value we have to insert for the filler, do so now.
1518 if (filler && !builder.isNullValue(attr: filler))
1519 elts.insert(I: elts.end(), NumToInsert: numElements - elts.size(), Elt: filler);
1520
1521 // Remove all null values at the end, so they become 'trailing zeroes'.
1522 while (!elts.empty() && builder.isNullValue(attr: elts.back()))
1523 elts.pop_back();
1524
1525 // For flexible array members, we need to adjust the size of our result to
1526 // match this.
1527 if (desiredType.getSize() == 0 && numElements > 0) {
1528 desiredType =
1529 cir::ArrayType::get(elementType: desiredType.getElementType(), size: numElements);
1530 }
1531
1532 if (elts.empty())
1533 return cir::ZeroAttr::get(type: desiredType);
1534
1535 return cir::ConstArrayAttr::get(
1536 type: desiredType, elts: mlir::ArrayAttr::get(context: builder.getContext(), value: elts));
1537 }
1538 case APValue::Vector: {
1539 const QualType elementType =
1540 destType->castAs<VectorType>()->getElementType();
1541 const unsigned numElements = value.getVectorLength();
1542
1543 SmallVector<mlir::Attribute, 16> elements;
1544 elements.reserve(N: numElements);
1545
1546 for (unsigned i = 0; i < numElements; ++i) {
1547 const mlir::Attribute element =
1548 tryEmitPrivateForMemory(value: value.getVectorElt(I: i), destType: elementType);
1549 if (!element)
1550 return {};
1551 elements.push_back(Elt: element);
1552 }
1553
1554 const auto desiredVecTy =
1555 mlir::cast<cir::VectorType>(Val: cgm.convertType(type: destType));
1556
1557 return cir::ConstVectorAttr::get(
1558 type: desiredVecTy,
1559 elts: mlir::ArrayAttr::get(context: cgm.getBuilder().getContext(), value: elements));
1560 }
1561 case APValue::MemberPointer: {
1562 assert(!cir::MissingFeatures::cxxABI());
1563
1564 const ValueDecl *memberDecl = value.getMemberPointerDecl();
1565 if (!memberDecl)
1566 return builder.getZeroInitAttr(ty: cgm.convertType(type: destType));
1567
1568 if (auto const *cxxDecl = dyn_cast<CXXMethodDecl>(Val: memberDecl)) {
1569 auto ty = mlir::cast<cir::MethodType>(Val: cgm.convertType(type: destType));
1570 if (cxxDecl->isVirtual())
1571 return cgm.getCXXABI().buildVirtualMethodAttr(methodTy: ty, md: cxxDecl);
1572
1573 cir::FuncOp methodFuncOp =
1574 cgm.getAddrOfFunction(gd: cxxDecl, funcType: ty.getMemberFuncTy());
1575 return cgm.getBuilder().getMethodAttr(ty, methodFuncOp);
1576 }
1577
1578 auto cirTy = mlir::cast<cir::DataMemberType>(Val: cgm.convertType(type: destType));
1579 const auto *mpt = destType->castAs<MemberPointerType>();
1580 const auto *destClass = mpt->getMostRecentCXXRecordDecl();
1581
1582 // Empty [[no_unique_address]] fields have no CIR field index; represent the
1583 // pointer-to-data-member by its concrete byte offset.
1584 if (const auto *fieldDecl = dyn_cast<FieldDecl>(Val: memberDecl);
1585 fieldDecl && cgm.isEmptyFieldForMemberPointer(field: fieldDecl)) {
1586 const ASTContext &astContext = cgm.getASTContext();
1587 CharUnits offset =
1588 astContext.getMemberPointerPathAdjustment(MP: value) +
1589 astContext.toCharUnitsFromBits(BitSize: astContext.getFieldOffset(FD: fieldDecl));
1590 return cir::DataMemberOffsetAttr::get(type: cirTy, offset: offset.getQuantity());
1591 }
1592
1593 std::optional<llvm::SmallVector<int32_t>> path =
1594 cgm.buildMemberPath(destClass, decl: memberDecl);
1595 if (!path)
1596 return {};
1597 return builder.getDataMemberAttr(ty: cirTy, path: *path);
1598 }
1599 case APValue::LValue:
1600 return ConstantLValueEmitter(*this, value, destType).tryEmit();
1601 case APValue::Struct:
1602 case APValue::Union:
1603 return ConstRecordBuilder::buildRecord(emitter&: *this, val: value, valTy: destType);
1604 case APValue::ComplexInt:
1605 case APValue::ComplexFloat: {
1606 mlir::Type desiredType = cgm.convertType(type: destType);
1607 auto complexType = mlir::dyn_cast<cir::ComplexType>(Val&: desiredType);
1608
1609 mlir::Type complexElemTy = complexType.getElementType();
1610 if (isa<cir::IntType>(Val: complexElemTy)) {
1611 const llvm::APSInt &real = value.getComplexIntReal();
1612 const llvm::APSInt &imag = value.getComplexIntImag();
1613 return cir::ConstComplexAttr::get(context: builder.getContext(), type: complexType,
1614 real: cir::IntAttr::get(type: complexElemTy, value: real),
1615 imag: cir::IntAttr::get(type: complexElemTy, value: imag));
1616 }
1617
1618 assert(isa<cir::FPTypeInterface>(complexElemTy) &&
1619 "expected floating-point type");
1620 const llvm::APFloat &real = value.getComplexFloatReal();
1621 const llvm::APFloat &imag = value.getComplexFloatImag();
1622 return cir::ConstComplexAttr::get(context: builder.getContext(), type: complexType,
1623 real: cir::FPAttr::get(type: complexElemTy, value: real),
1624 imag: cir::FPAttr::get(type: complexElemTy, value: imag));
1625 }
1626 case APValue::FixedPoint: {
1627 mlir::Type ty = cgm.convertType(type: destType);
1628 return cir::IntAttr::get(type: ty, value: value.getFixedPoint().getValue());
1629 }
1630 case APValue::AddrLabelDiff: {
1631 const AddrLabelExpr *lhsExpr = value.getAddrLabelDiffLHS();
1632 const AddrLabelExpr *rhsExpr = value.getAddrLabelDiffRHS();
1633
1634 // Both labels belong to the function currently being emitted. The actual
1635 // subtraction (ptrtoint of each block address, subtract, then truncate to
1636 // the result type) is deferred to the LowerToLLVM pass, which is where
1637 // block addresses are resolved to concrete basic blocks.
1638 mlir::Type resultType = cgm.getTypes().convertType(type: destType);
1639 auto intResultType = mlir::cast<cir::IntType>(Val&: resultType);
1640 auto func = cast<cir::FuncOp>(Val: cgf->curFn);
1641 return cir::BlockAddrDiffAttr::get(
1642 context: builder.getContext(), type: intResultType, func_name: func.getSymName(),
1643 lhs_label_name: lhsExpr->getLabel()->getName(), rhs_label_name: rhsExpr->getLabel()->getName());
1644 }
1645
1646 case APValue::Matrix:
1647 cgm.errorNYI(feature: "ConstExprEmitter::tryEmitPrivate matrix");
1648 return {};
1649
1650 case APValue::Reflection:
1651 llvm_unreachable("std::meta::info is consteval-only type");
1652 }
1653 llvm_unreachable("Unknown APValue kind");
1654}
1655
1656mlir::Value CIRGenModule::emitNullConstant(QualType t, mlir::Location loc) {
1657 return builder.getConstant(loc, attr: emitNullConstantAttr(t));
1658}
1659
1660mlir::Value CIRGenModule::getNullPointer(cir::PointerType ptrTy, QualType qt,
1661 mlir::Location loc) {
1662 return getTargetCIRGenInfo().getNullPointer(cgm&: *this, ptrTy, qt, loc);
1663}
1664
1665mlir::TypedAttr CIRGenModule::emitNullConstantAttr(QualType t) {
1666 if (t->getAs<PointerType>())
1667 return builder.getConstNullPtrAttr(t: getTypes().convertTypeForMem(t));
1668
1669 if (getTypes().isZeroInitializable(ty: t))
1670 return builder.getZeroInitAttr(ty: getTypes().convertTypeForMem(t));
1671
1672 if (const ConstantArrayType *cat =
1673 getASTContext().getAsConstantArrayType(T: t)) {
1674 QualType elementTy = cat->getElementType();
1675 mlir::TypedAttr elementAttr = emitNullConstantAttr(t: elementTy);
1676 if (!elementAttr)
1677 return {};
1678
1679 auto arrayTy = mlir::cast<cir::ArrayType>(Val: getTypes().convertTypeForMem(t));
1680
1681 if (builder.isNullValue(attr: elementAttr))
1682 return cir::ZeroAttr::get(type: arrayTy);
1683
1684 llvm::SmallVector<mlir::Attribute> elements(cat->getZExtSize(),
1685 elementAttr);
1686 return cir::ConstArrayAttr::get(
1687 type: arrayTy, elts: mlir::ArrayAttr::get(context: builder.getContext(), value: elements));
1688 }
1689
1690 if (const RecordType *rt = t->getAs<RecordType>())
1691 return ::emitNullConstant(cgm&: *this, rd: rt->getDecl(), /*asCompleteObject=*/true);
1692
1693 assert(t->isMemberDataPointerType() &&
1694 "Should only see pointers to data members here!");
1695
1696 return emitNullMemberAttr(t, mpt: t->castAs<MemberPointerType>());
1697}
1698
1699mlir::TypedAttr
1700CIRGenModule::emitNullConstantForBase(const CXXRecordDecl *record) {
1701 return ::emitNullConstant(cgm&: *this, rd: record, asCompleteObject: false);
1702}
1703