18#include "mlir/IR/Attributes.h"
19#include "mlir/IR/BuiltinAttributeInterfaces.h"
20#include "mlir/IR/BuiltinAttributes.h"
32#include "llvm/ADT/ArrayRef.h"
33#include "llvm/ADT/STLExtras.h"
34#include "llvm/Support/ErrorHandling.h"
46class ConstExprEmitter;
54 return bld.
getConstArray(mlir::ArrayAttr::get(bld.getContext(), elts),
55 cir::ArrayType::get(eltTy, arSize));
58 return cir::ZeroAttr::get(eltTy);
62emitArrayConstant(
CIRGenModule &cgm, mlir::Type desiredType,
63 mlir::Type commonElementType,
unsigned arrayBound,
65 mlir::TypedAttr filler);
67struct ConstantAggregateBuilderUtils {
69 cir::CIRDataLayout dataLayout;
71 ConstantAggregateBuilderUtils(CIRGenModule &cgm)
72 : cgm(cgm), dataLayout{cgm.getModule()} {}
74 CharUnits getAlignment(
const mlir::TypedAttr
c)
const {
76 dataLayout.getAlignment(
c.getType(),
true));
79 CharUnits getSize(mlir::Type ty)
const {
83 CharUnits getSize(
const mlir::TypedAttr
c)
const {
84 return getSize(
c.getType());
87 mlir::TypedAttr getPadding(CharUnits size)
const {
88 return computePadding(cgm, size);
94class ConstantAggregateBuilder :
private ConstantAggregateBuilderUtils {
96 Element(mlir::TypedAttr element, CharUnits offset)
97 : element(element), offset(offset) {}
99 mlir::TypedAttr element;
110 llvm::SmallVector<Element, 32> elements;
119 bool naturalLayout =
true;
121 static mlir::Attribute buildFrom(CIRGenModule &cgm, ArrayRef<Element> elems,
122 CharUnits startOffset, CharUnits size,
123 bool naturalLayout, mlir::Type desiredTy,
124 bool allowOversized);
127 ConstantAggregateBuilder(CIRGenModule &cgm)
128 : ConstantAggregateBuilderUtils(cgm) {}
135 bool add(mlir::TypedAttr typedAttr, CharUnits offset,
bool allowOverwrite);
138 bool addBits(llvm::APInt bits, uint64_t offsetInBits,
bool allowOverwrite);
145 mlir::Attribute build(mlir::Type desiredTy,
bool allowOversized)
const {
146 return buildFrom(cgm, elements,
CharUnits::Zero(), size, naturalLayout,
147 desiredTy, allowOversized);
151template <
typename Container,
typename Range = std::initializer_list<
152 typename Container::value_type>>
153static void replace(Container &
c,
size_t beginOff,
size_t endOff, Range vals) {
154 assert(beginOff <= endOff &&
"invalid replacement range");
155 llvm::replace(
c,
c.begin() + beginOff,
c.begin() + endOff, vals);
158bool ConstantAggregateBuilder::add(mlir::TypedAttr typedAttr,
CharUnits offset,
159 bool allowOverwrite) {
161 if (offset >= size) {
162 CharUnits
align = getAlignment(typedAttr);
163 CharUnits alignedSize = size.
alignTo(align);
164 if (alignedSize > offset || offset.
alignTo(align) != offset) {
165 naturalLayout =
false;
166 }
else if (alignedSize < offset) {
167 elements.emplace_back(getPadding(offset - size), size);
169 elements.emplace_back(typedAttr, offset);
170 size = offset + getSize(typedAttr);
175 cgm.
errorNYI(
"overlapping constants");
180ConstantAggregateBuilder::buildFrom(CIRGenModule &cgm, ArrayRef<Element> elems,
181 CharUnits startOffset, CharUnits size,
182 bool naturalLayout, mlir::Type desiredTy,
183 bool allowOversized) {
184 ConstantAggregateBuilderUtils utils(cgm);
187 return cir::UndefAttr::get(desiredTy);
191 if (mlir::isa<cir::ArrayType>(desiredTy)) {
192 cgm.
errorNYI(
"array aggregate constants");
199 CharUnits desiredSize = utils.getSize(desiredTy);
200 if (size > desiredSize) {
201 assert(allowOversized &&
"elems are oversized");
207 for (
auto [e, offset] : elems)
208 align = std::max(align, utils.getAlignment(e));
211 CharUnits alignedSize = size.
alignTo(align);
216 llvm::SmallVector<mlir::Attribute, 32> unpackedElems;
217 if (desiredSize < alignedSize || desiredSize.
alignTo(align) != desiredSize) {
218 naturalLayout =
false;
223 unpackedElems.reserve(elems.size() + 1);
224 llvm::transform(elems, std::back_inserter(unpackedElems),
225 std::mem_fn(&Element::element));
226 if (desiredSize > alignedSize)
227 unpackedElems.push_back(utils.getPadding(desiredSize - size));
233 llvm::SmallVector<mlir::Attribute, 32> packedElems;
234 packedElems.reserve(elems.size());
235 if (!naturalLayout) {
237 for (
auto [element, offset] : elems) {
238 CharUnits
align = utils.getAlignment(element);
239 CharUnits naturalOffset = sizeSoFar.
alignTo(align);
240 CharUnits desiredOffset = offset - startOffset;
241 assert(desiredOffset >= sizeSoFar &&
"elements out of order");
243 if (desiredOffset != naturalOffset)
245 if (desiredOffset != sizeSoFar)
246 packedElems.push_back(utils.getPadding(desiredOffset - sizeSoFar));
247 packedElems.push_back(element);
248 sizeSoFar = desiredOffset + utils.getSize(element);
253 assert(sizeSoFar <= desiredSize &&
254 "requested size is too small for contents");
256 if (sizeSoFar < desiredSize)
257 packedElems.push_back(utils.getPadding(desiredSize - sizeSoFar));
262 auto arrAttr = mlir::ArrayAttr::get(builder.getContext(),
263 packed ? packedElems : unpackedElems);
266 if (
auto desired = mlir::dyn_cast<cir::RecordType>(desiredTy))
277class ConstRecordBuilder {
279 ConstantEmitter &emitter;
280 ConstantAggregateBuilder &builder;
281 CharUnits startOffset;
284 static mlir::Attribute buildRecord(ConstantEmitter &emitter,
285 InitListExpr *ile, QualType valTy);
286 static mlir::Attribute buildRecord(ConstantEmitter &emitter,
287 const APValue &value, QualType valTy);
288 static bool updateRecord(ConstantEmitter &emitter,
289 ConstantAggregateBuilder &constant, CharUnits offset,
290 InitListExpr *updater);
293 ConstRecordBuilder(ConstantEmitter &emitter,
294 ConstantAggregateBuilder &builder, CharUnits startOffset)
295 : cgm(emitter.cgm), emitter(emitter), builder(builder),
296 startOffset(startOffset) {}
298 bool appendField(
const FieldDecl *field, uint64_t fieldOffset,
299 mlir::TypedAttr initCst,
bool allowOverwrite =
false);
301 bool appendBytes(CharUnits fieldOffsetInChars, mlir::TypedAttr initCst,
302 bool allowOverwrite =
false);
304 bool build(InitListExpr *ile,
bool allowOverwrite);
305 bool build(
const APValue &val,
const RecordDecl *rd,
bool isPrimaryBase,
306 const CXXRecordDecl *vTableClass, CharUnits baseOffset);
308 mlir::Attribute
finalize(QualType ty);
311bool ConstRecordBuilder::appendField(
const FieldDecl *field,
312 uint64_t fieldOffset,
313 mlir::TypedAttr initCst,
314 bool allowOverwrite) {
319 return appendBytes(fieldOffsetInChars, initCst, allowOverwrite);
322bool ConstRecordBuilder::appendBytes(CharUnits fieldOffsetInChars,
323 mlir::TypedAttr initCst,
324 bool allowOverwrite) {
325 return builder.add(initCst, startOffset + fieldOffsetInChars, allowOverwrite);
328bool ConstRecordBuilder::build(InitListExpr *ile,
bool allowOverwrite) {
329 RecordDecl *rd = ile->
getType()
330 ->
castAs<clang::RecordType>()
332 ->getDefinitionOrSelf();
338 if (
auto *cxxrd = dyn_cast<CXXRecordDecl>(rd))
339 if (cxxrd->getNumBases())
348 unsigned elementNo = 0;
349 for (
auto [index, field] : llvm::enumerate(rd->
fields())) {
362 Expr *init =
nullptr;
363 if (elementNo < ile->getNumInits())
364 init = ile->
getInit(elementNo++);
365 if (isa_and_nonnull<NoInitExpr>(init))
381 if (allowOverwrite &&
387 mlir::TypedAttr eltInit;
389 eltInit = mlir::cast<mlir::TypedAttr>(
405 if (field->
hasAttr<NoUniqueAddressAttr>())
406 allowOverwrite =
true;
409 if (
auto constInt = dyn_cast<cir::IntAttr>(eltInit)) {
425 BaseInfo(
const CXXRecordDecl *decl, CharUnits offset,
unsigned index)
426 : decl(decl), offset(offset), index(index) {}
428 const CXXRecordDecl *decl;
432 bool operator<(
const BaseInfo &o)
const {
return offset < o.offset; }
436bool ConstRecordBuilder::build(
const APValue &val,
const RecordDecl *rd,
438 const CXXRecordDecl *vTableClass,
441 if (
const CXXRecordDecl *cd = dyn_cast<CXXRecordDecl>(rd)) {
445 cir::GlobalOp vtable =
447 clang::VTableLayout::AddressPointLocation addressPoint =
452 mlir::ArrayAttr indices = builder.getArrayAttr({
453 builder.getI32IntegerAttr(addressPoint.
VTableIndex),
456 cir::GlobalViewAttr vtableInit =
458 if (!appendBytes(offset, vtableInit))
464 SmallVector<BaseInfo> bases;
465 bases.reserve(cd->getNumBases());
466 for (
auto [index, base] : llvm::enumerate(cd->bases())) {
467 assert(!base.isVirtual() &&
"should not have virtual bases here");
468 const CXXRecordDecl *bd = base.getType()->getAsCXXRecordDecl();
470 bases.push_back(BaseInfo(bd, baseOffset, index));
472#ifdef EXPENSIVE_CHECKS
473 assert(llvm::is_sorted(bases) &&
"bases not sorted by offset");
476 for (BaseInfo &base : bases) {
478 build(val.
getStructBase(base.index), base.decl, isPrimaryBase,
479 vTableClass, offset + base.offset);
485 bool allowOverwrite =
false;
486 for (
auto [index, field] : llvm::enumerate(rd->
fields())) {
498 mlir::TypedAttr eltInit = mlir::cast<mlir::TypedAttr>(
505 if (!appendField(field, layout.
getFieldOffset(index) + offsetBits,
506 eltInit, allowOverwrite))
510 if (field->
hasAttr<NoUniqueAddressAttr>())
511 allowOverwrite =
true;
521mlir::Attribute ConstRecordBuilder::finalize(QualType
type) {
523 RecordDecl *rd =
type->castAs<clang::RecordType>()
530mlir::Attribute ConstRecordBuilder::buildRecord(ConstantEmitter &emitter,
533 ConstantAggregateBuilder constant(emitter.
cgm);
536 if (!builder.build(ile,
false))
539 return builder.finalize(valTy);
542mlir::Attribute ConstRecordBuilder::buildRecord(ConstantEmitter &emitter,
545 ConstantAggregateBuilder constant(emitter.
cgm);
548 const RecordDecl *rd = valTy->
castAs<clang::RecordType>()
550 ->getDefinitionOrSelf();
551 const CXXRecordDecl *cd = dyn_cast<CXXRecordDecl>(rd);
555 return builder.finalize(valTy);
558bool ConstRecordBuilder::updateRecord(ConstantEmitter &emitter,
559 ConstantAggregateBuilder &constant,
560 CharUnits offset, InitListExpr *updater) {
561 return ConstRecordBuilder(emitter, constant, offset)
562 .build(updater,
true);
576class ConstExprEmitter
577 :
public StmtVisitor<ConstExprEmitter, mlir::Attribute, QualType> {
579 LLVM_ATTRIBUTE_UNUSED ConstantEmitter &emitter;
582 ConstExprEmitter(ConstantEmitter &emitter)
583 : cgm(emitter.cgm), emitter(emitter) {}
589 mlir::Attribute VisitStmt(Stmt *
s, QualType t) {
return {}; }
591 mlir::Attribute VisitConstantExpr(ConstantExpr *ce, QualType t) {
592 if (mlir::Attribute result = emitter.tryEmitConstantExpr(ce))
597 mlir::Attribute VisitParenExpr(ParenExpr *pe, QualType t) {
602 VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *pe,
607 mlir::Attribute VisitGenericSelectionExpr(GenericSelectionExpr *ge,
612 mlir::Attribute VisitChooseExpr(ChooseExpr *ce, QualType t) {
616 mlir::Attribute VisitCompoundLiteralExpr(CompoundLiteralExpr *e, QualType t) {
620 mlir::Attribute VisitCastExpr(
CastExpr *e, QualType destType) {
623 "ConstExprEmitter::VisitCastExpr explicit cast");
629 case CK_AddressSpaceConversion:
630 case CK_ReinterpretMemberPointer:
631 case CK_DerivedToBaseMemberPointer:
632 case CK_BaseToDerivedMemberPointer:
636 case CK_LValueToRValue:
637 case CK_AtomicToNonAtomic:
638 case CK_NonAtomicToAtomic:
640 case CK_ConstructorConversion:
641 return Visit(subExpr, destType);
643 case CK_IntToOCLSampler:
644 llvm_unreachable(
"global sampler variables are not generated");
647 llvm_unreachable(
"saw dependent cast!");
649 case CK_BuiltinFnToFnPtr:
650 llvm_unreachable(
"builtin functions are handled elsewhere");
653 case CK_ObjCObjectLValueCast:
654 case CK_ARCProduceObject:
655 case CK_ARCConsumeObject:
656 case CK_ARCReclaimReturnedObject:
657 case CK_ARCExtendBlockObject:
658 case CK_CopyAndAutoreleaseBlockObject:
666 case CK_LValueBitCast:
667 case CK_LValueToRValueBitCast:
668 case CK_NullToMemberPointer:
669 case CK_UserDefinedConversion:
670 case CK_CPointerToObjCPointerCast:
671 case CK_BlockPointerToObjCPointerCast:
672 case CK_AnyPointerToBlockPointerCast:
673 case CK_ArrayToPointerDecay:
674 case CK_FunctionToPointerDecay:
675 case CK_BaseToDerived:
676 case CK_DerivedToBase:
677 case CK_UncheckedDerivedToBase:
678 case CK_MemberPointerToBoolean:
680 case CK_FloatingRealToComplex:
681 case CK_FloatingComplexToReal:
682 case CK_FloatingComplexToBoolean:
683 case CK_FloatingComplexCast:
684 case CK_FloatingComplexToIntegralComplex:
685 case CK_IntegralRealToComplex:
686 case CK_IntegralComplexToReal:
687 case CK_IntegralComplexToBoolean:
688 case CK_IntegralComplexCast:
689 case CK_IntegralComplexToFloatingComplex:
690 case CK_PointerToIntegral:
691 case CK_PointerToBoolean:
692 case CK_NullToPointer:
693 case CK_IntegralCast:
694 case CK_BooleanToSignedIntegral:
695 case CK_IntegralToPointer:
696 case CK_IntegralToBoolean:
697 case CK_IntegralToFloating:
698 case CK_FloatingToIntegral:
699 case CK_FloatingToBoolean:
700 case CK_FloatingCast:
701 case CK_FloatingToFixedPoint:
702 case CK_FixedPointToFloating:
703 case CK_FixedPointCast:
704 case CK_FixedPointToBoolean:
705 case CK_FixedPointToIntegral:
706 case CK_IntegralToFixedPoint:
707 case CK_ZeroToOCLOpaqueType:
709 case CK_HLSLArrayRValue:
710 case CK_HLSLVectorTruncation:
711 case CK_HLSLElementwiseCast:
712 case CK_HLSLAggregateSplatCast:
715 llvm_unreachable(
"Invalid CastKind");
718 mlir::Attribute VisitCXXDefaultInitExpr(CXXDefaultInitExpr *die, QualType t) {
720 "ConstExprEmitter::VisitCXXDefaultInitExpr");
724 mlir::Attribute VisitExprWithCleanups(ExprWithCleanups *e, QualType t) {
729 mlir::Attribute VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *e,
734 mlir::Attribute VisitImplicitValueInitExpr(ImplicitValueInitExpr *e,
737 "ConstExprEmitter::VisitImplicitValueInitExpr");
741 mlir::Attribute VisitInitListExpr(InitListExpr *ile, QualType t) {
743 return Visit(ile->
getInit(0), t);
753 return ConstRecordBuilder::buildRecord(emitter, ile, t);
766 mlir::Attribute VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *e,
768 mlir::Attribute
c = Visit(e->
getBase(), destType);
773 "ConstExprEmitter::VisitDesignatedInitUpdateExpr");
777 mlir::Attribute VisitCXXConstructExpr(CXXConstructExpr *e, QualType ty) {
782 mlir::Attribute VisitStringLiteral(StringLiteral *e, QualType t) {
787 mlir::Attribute VisitObjCEncodeExpr(ObjCEncodeExpr *e, QualType t) {
792 mlir::Attribute VisitUnaryExtension(
const UnaryOperator *e, QualType t) {
797 mlir::Type convertType(QualType t) {
return cgm.
convertType(t); }
802 if (
const auto *at =
type->getAs<AtomicType>()) {
804 type.getQualifiers());
809static mlir::Attribute
810emitArrayConstant(CIRGenModule &cgm, mlir::Type desiredType,
811 mlir::Type commonElementType,
unsigned arrayBound,
812 SmallVectorImpl<mlir::TypedAttr> &elements,
813 mlir::TypedAttr filler) {
816 unsigned nonzeroLength = arrayBound;
817 if (elements.size() < nonzeroLength && builder.
isNullValue(filler))
818 nonzeroLength = elements.size();
820 if (nonzeroLength == elements.size()) {
821 while (nonzeroLength > 0 &&
826 if (nonzeroLength == 0)
827 return cir::ZeroAttr::get(desiredType);
829 const unsigned trailingZeroes = arrayBound - nonzeroLength;
832 if (trailingZeroes >= 8) {
833 assert(elements.size() >= nonzeroLength &&
834 "missing initializer for non-zero element");
836 if (commonElementType && nonzeroLength >= 8) {
840 SmallVector<mlir::Attribute> eles;
841 eles.reserve(nonzeroLength);
842 for (
const auto &element : elements)
843 eles.push_back(element);
844 auto initial = cir::ConstArrayAttr::get(
845 cir::ArrayType::get(commonElementType, nonzeroLength),
846 mlir::ArrayAttr::get(builder.getContext(), eles));
848 elements[0] = initial;
852 elements.resize(nonzeroLength + 1);
855 mlir::Type fillerType =
858 : mlir::cast<cir::ArrayType>(desiredType).getElementType();
859 fillerType = cir::ArrayType::get(fillerType, trailingZeroes);
860 elements.back() = cir::ZeroAttr::get(fillerType);
861 commonElementType =
nullptr;
862 }
else if (elements.size() != arrayBound) {
863 elements.resize(arrayBound, filler);
865 if (filler.getType() != commonElementType)
866 commonElementType = {};
869 if (commonElementType) {
870 SmallVector<mlir::Attribute> eles;
871 eles.reserve(elements.size());
873 for (
const auto &element : elements)
874 eles.push_back(element);
876 return cir::ConstArrayAttr::get(
877 cir::ArrayType::get(commonElementType, arrayBound),
878 mlir::ArrayAttr::get(builder.getContext(), eles));
881 SmallVector<mlir::Attribute> eles;
882 eles.reserve(elements.size());
883 for (
auto const &element : elements)
884 eles.push_back(element);
886 auto arrAttr = mlir::ArrayAttr::get(builder.getContext(), eles);
899struct ConstantLValue {
900 llvm::PointerUnion<mlir::Value, mlir::Attribute> value;
901 bool hasOffsetApplied;
903 ConstantLValue(std::nullptr_t)
905 ConstantLValue(cir::GlobalViewAttr address)
906 : value(address), hasOffsetApplied(
false) {}
908 ConstantLValue() : value(
nullptr), hasOffsetApplied(
false) {}
912class ConstantLValueEmitter
913 :
public ConstStmtVisitor<ConstantLValueEmitter, ConstantLValue> {
915 ConstantEmitter &emitter;
920 friend StmtVisitorBase;
923 ConstantLValueEmitter(ConstantEmitter &emitter,
const APValue &value,
925 : cgm(emitter.cgm), emitter(emitter), value(value), destType(destType) {}
927 mlir::Attribute tryEmit();
930 mlir::Attribute tryEmitAbsolute(mlir::Type destTy);
931 ConstantLValue tryEmitBase(
const APValue::LValueBase &base);
933 ConstantLValue VisitStmt(
const Stmt *
s) {
return nullptr; }
934 ConstantLValue VisitConstantExpr(
const ConstantExpr *e);
935 ConstantLValue VisitCompoundLiteralExpr(
const CompoundLiteralExpr *e);
936 ConstantLValue VisitStringLiteral(
const StringLiteral *e);
937 ConstantLValue VisitObjCBoxedExpr(
const ObjCBoxedExpr *e);
938 ConstantLValue VisitObjCEncodeExpr(
const ObjCEncodeExpr *e);
939 ConstantLValue VisitObjCStringLiteral(
const ObjCStringLiteral *e);
940 ConstantLValue VisitPredefinedExpr(
const PredefinedExpr *e);
941 ConstantLValue VisitAddrLabelExpr(
const AddrLabelExpr *e);
942 ConstantLValue VisitCallExpr(
const CallExpr *e);
943 ConstantLValue VisitBlockExpr(
const BlockExpr *e);
944 ConstantLValue VisitCXXTypeidExpr(
const CXXTypeidExpr *e);
946 VisitMaterializeTemporaryExpr(
const MaterializeTemporaryExpr *e);
949 mlir::ArrayAttr getOffset(mlir::Type ty) {
951 cir::CIRDataLayout layout(cgm.
getModule());
952 SmallVector<int64_t, 3> idxVec;
956 llvm::SmallVector<mlir::Attribute, 3> indices;
957 for (int64_t i : idxVec) {
958 mlir::IntegerAttr intAttr = cgm.
getBuilder().getI32IntegerAttr(i);
959 indices.push_back(intAttr);
964 return cgm.
getBuilder().getArrayAttr(indices);
968 ConstantLValue applyOffset(ConstantLValue &
c) {
970 if (
auto attr = mlir::dyn_cast<mlir::Attribute>(
c.value)) {
971 if (
auto gv = mlir::dyn_cast<cir::GlobalViewAttr>(attr)) {
972 auto baseTy = mlir::cast<cir::PointerType>(gv.getType()).getPointee();
974 assert(!gv.getIndices() &&
"Global view is already indexed");
975 return cir::GlobalViewAttr::get(destTy, gv.getSymbol(),
978 llvm_unreachable(
"Unsupported attribute type to offset");
981 cgm.
errorNYI(
"ConstantLValue: non-attribute offset");
988mlir::Attribute ConstantLValueEmitter::tryEmit() {
999 assert(mlir::isa<cir::PointerType>(destTy));
1004 return tryEmitAbsolute(destTy);
1007 ConstantLValue result = tryEmitBase(base);
1010 llvm::PointerUnion<mlir::Value, mlir::Attribute> &value = result.value;
1015 if (!result.hasOffsetApplied)
1016 value = applyOffset(result).value;
1020 if (mlir::isa<cir::PointerType>(destTy)) {
1021 if (
auto attr = mlir::dyn_cast<mlir::Attribute>(value))
1023 cgm.
errorNYI(
"ConstantLValueEmitter: non-attribute pointer");
1027 cgm.
errorNYI(
"ConstantLValueEmitter: other?");
1033mlir::Attribute ConstantLValueEmitter::tryEmitAbsolute(mlir::Type destTy) {
1035 auto destPtrTy = mlir::cast<cir::PointerType>(destTy);
1037 destPtrTy, value.getLValueOffset().getQuantity());
1041ConstantLValueEmitter::tryEmitBase(
const APValue::LValueBase &base) {
1043 if (
const ValueDecl *d = base.
dyn_cast<
const ValueDecl *>()) {
1048 if (d->hasAttr<WeakRefAttr>()) {
1050 "ConstantLValueEmitter: emit pointer base for weakref");
1054 if (
auto *fd = dyn_cast<FunctionDecl>(d)) {
1057 mlir::MLIRContext *mlirContext = builder.getContext();
1058 return cir::GlobalViewAttr::get(
1060 mlir::FlatSymbolRefAttr::get(mlirContext, fop.getSymNameAttr()));
1063 if (
auto *vd = dyn_cast<VarDecl>(d)) {
1065 if (!vd->hasLocalStorage()) {
1066 if (vd->isFileVarDecl() || vd->hasExternalStorage())
1069 if (vd->isLocalVarDecl()) {
1071 "ConstantLValueEmitter: local var decl");
1082 "ConstantLValueEmitter: unhandled value decl");
1087 if (base.
dyn_cast<TypeInfoLValue>()) {
1088 cgm.
errorNYI(
"ConstantLValueEmitter: typeid");
1093 return Visit(base.
get<
const Expr *>());
1096ConstantLValue ConstantLValueEmitter::VisitConstantExpr(
const ConstantExpr *e) {
1102ConstantLValueEmitter::VisitCompoundLiteralExpr(
const CompoundLiteralExpr *e) {
1108ConstantLValueEmitter::VisitStringLiteral(
const StringLiteral *e) {
1113ConstantLValueEmitter::VisitObjCEncodeExpr(
const ObjCEncodeExpr *e) {
1119ConstantLValueEmitter::VisitObjCStringLiteral(
const ObjCStringLiteral *e) {
1121 "ConstantLValueEmitter: objc string literal");
1126ConstantLValueEmitter::VisitObjCBoxedExpr(
const ObjCBoxedExpr *e) {
1132ConstantLValueEmitter::VisitPredefinedExpr(
const PredefinedExpr *e) {
1138ConstantLValueEmitter::VisitAddrLabelExpr(
const AddrLabelExpr *e) {
1143ConstantLValue ConstantLValueEmitter::VisitCallExpr(
const CallExpr *e) {
1148ConstantLValue ConstantLValueEmitter::VisitBlockExpr(
const BlockExpr *e) {
1154ConstantLValueEmitter::VisitCXXTypeidExpr(
const CXXTypeidExpr *e) {
1159ConstantLValue ConstantLValueEmitter::VisitMaterializeTemporaryExpr(
1160 const MaterializeTemporaryExpr *e) {
1162 "ConstantLValueEmitter: materialize temporary expr");
1171 initializeNonAbstract();
1176 assert(initializedNonAbstract &&
1177 "finalizing emitter that was used for abstract emission?");
1178 assert(!finalized &&
"finalizing emitter multiple times");
1179 assert(!gv.isDeclaration());
1188 AbstractStateRAII state(*
this,
true);
1193 assert((!initializedNonAbstract || finalized || failed) &&
1194 "not finalized after being initialized for non-abstract emission");
1204 if (
const auto *e = dyn_cast_or_null<CXXConstructExpr>(d.
getInit())) {
1212 if (cxxrd->getNumBases() != 0) {
1215 cgm.errorNYI(
"tryEmitPrivateForVarInit: cxx record with bases");
1218 if (!
cgm.getTypes().isZeroInitializable(cxxrd)) {
1222 "tryEmitPrivateForVarInit: non-zero-initializable cxx record");
1225 return cir::ZeroAttr::get(
cgm.convertType(d.
getType()));
1233 assert(e &&
"No initializer to emit");
1239 if (mlir::Attribute
c = ConstExprEmitter(*this).Visit(
const_cast<Expr *
>(e),
1258 retType =
cgm.getASTContext().getLValueReferenceType(retType);
1269 return mlir::cast<mlir::TypedAttr>(
attr);
1283 AbstractStateRAII state{*
this,
true};
1284 mlir::Attribute
c = mlir::cast<mlir::Attribute>(
tryEmitPrivate(e, destType));
1287 "emitAbstract failed, emit null constaant");
1294 AbstractStateRAII state(*
this,
true);
1297 cgm.errorNYI(loc,
"emitAbstract failed, emit null constaant");
1304 cir::ConstantOp cstOp =
1305 cgm.emitNullConstant(t, loc).getDefiningOp<cir::ConstantOp>();
1306 assert(cstOp &&
"expected cir.const op");
1314 cgm.errorNYI(
"emitForMemory: atomic type");
1326 cgm.errorNYI(
"atomic constants");
1334 assert(!destType->
isVoidType() &&
"can't emit a void constant");
1336 if (mlir::Attribute
c =
1337 ConstExprEmitter(*this).Visit(
const_cast<Expr *
>(e), destType))
1338 return llvm::dyn_cast<mlir::TypedAttr>(
c);
1342 bool success =
false;
1352 return llvm::dyn_cast<mlir::TypedAttr>(
c);
1360 auto &builder =
cgm.getBuilder();
1364 cgm.errorNYI(
"ConstExprEmitter::tryEmitPrivate none or indeterminate");
1367 mlir::Type ty =
cgm.convertType(destType);
1368 if (mlir::isa<cir::BoolType>(ty))
1370 assert(mlir::isa<cir::IntType>(ty) &&
"expected integral type");
1371 return cir::IntAttr::get(ty, value.
getInt());
1374 const llvm::APFloat &init = value.
getFloat();
1375 if (&init.getSemantics() == &llvm::APFloat::IEEEhalf() &&
1376 !
cgm.getASTContext().getLangOpts().NativeHalfType &&
1377 cgm.getASTContext().getTargetInfo().useFP16ConversionIntrinsics()) {
1378 cgm.errorNYI(
"ConstExprEmitter::tryEmitPrivate half");
1382 mlir::Type ty =
cgm.convertType(destType);
1383 assert(mlir::isa<cir::FPTypeInterface>(ty) &&
1384 "expected floating-point type");
1385 return cir::FPAttr::get(ty, init);
1388 const ArrayType *arrayTy =
cgm.getASTContext().getAsArrayType(destType);
1393 mlir::Attribute filler;
1402 elements.reserve(numInitElts + 1);
1404 elements.reserve(numInitElts);
1406 mlir::Type commonElementType;
1407 for (
unsigned i = 0; i < numInitElts; ++i) {
1409 const mlir::Attribute element =
1414 const mlir::TypedAttr elementTyped = mlir::cast<mlir::TypedAttr>(element);
1416 commonElementType = elementTyped.getType();
1417 else if (elementTyped.getType() != commonElementType) {
1418 commonElementType = {};
1421 elements.push_back(elementTyped);
1424 mlir::TypedAttr typedFiller = llvm::cast_or_null<mlir::TypedAttr>(filler);
1425 if (filler && !typedFiller)
1426 cgm.errorNYI(
"array filler should always be typed");
1428 mlir::Type desiredType =
cgm.convertType(destType);
1429 return emitArrayConstant(
cgm, desiredType, commonElementType, numElements,
1430 elements, typedFiller);
1438 elements.reserve(numElements);
1440 for (
unsigned i = 0; i < numElements; ++i) {
1441 const mlir::Attribute element =
1445 elements.push_back(element);
1448 const auto desiredVecTy =
1449 mlir::cast<cir::VectorType>(
cgm.convertType(destType));
1451 return cir::ConstVectorAttr::get(
1453 mlir::ArrayAttr::get(
cgm.getBuilder().getContext(), elements));
1456 cgm.errorNYI(
"ConstExprEmitter::tryEmitPrivate member pointer");
1460 return ConstantLValueEmitter(*
this, value, destType).tryEmit();
1463 return ConstRecordBuilder::buildRecord(*
this, value, destType);
1466 mlir::Type desiredType =
cgm.convertType(destType);
1467 auto complexType = mlir::dyn_cast<cir::ComplexType>(desiredType);
1469 mlir::Type complexElemTy =
complexType.getElementType();
1473 return cir::ConstComplexAttr::get(builder.getContext(),
complexType,
1474 cir::IntAttr::get(complexElemTy, real),
1475 cir::IntAttr::get(complexElemTy, imag));
1479 "expected floating-point type");
1482 return cir::ConstComplexAttr::get(builder.getContext(),
complexType,
1483 cir::FPAttr::get(complexElemTy, real),
1484 cir::FPAttr::get(complexElemTy, imag));
1489 "ConstExprEmitter::tryEmitPrivate fixed point, addr label diff");
1492 llvm_unreachable(
"Unknown APValue kind");
1497 return builder.getNullPtr(
getTypes().convertTypeForMem(t), loc);
1500 if (
getTypes().isZeroInitializable(t))
1501 return builder.getNullValue(
getTypes().convertTypeForMem(t), loc);
1504 errorNYI(
"CIRGenModule::emitNullConstant ConstantArrayType");
1508 errorNYI(
"CIRGenModule::emitNullConstant RecordType");
1511 "Should only see pointers to data members here!");
1513 errorNYI(
"CIRGenModule::emitNullConstant unsupported type");
Defines the clang::ASTContext interface.
Defines enum values for all the target-independent builtin functions.
static QualType getNonMemoryType(CodeGenModule &CGM, QualType type)
__device__ __2f16 float __ockl_bool s
__device__ __2f16 float c
cir::GlobalViewAttr getGlobalViewAttr(cir::GlobalOp globalOp, mlir::ArrayAttr indices={})
Get constant address of a global variable as an MLIR attribute.
cir::BoolAttr getCIRBoolAttr(bool state)
cir::PointerType getPointerTo(mlir::Type ty)
mlir::TypedAttr getConstPtrAttr(mlir::Type type, int64_t value)
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
bool hasArrayFiller() const
const LValueBase getLValueBase() const
APValue & getArrayInitializedElt(unsigned I)
APValue & getStructField(unsigned i)
const FieldDecl * getUnionField() const
APSInt & getComplexIntImag()
ValueKind getKind() const
unsigned getArrayInitializedElts() const
APValue & getUnionValue()
CharUnits & getLValueOffset()
APValue & getVectorElt(unsigned I)
APValue & getArrayFiller()
unsigned getVectorLength() const
unsigned getArraySize() const
@ Indeterminate
This object has an indeterminate value (C++ [basic.indet]).
@ None
There is no such object (it's outside its lifetime).
APSInt & getComplexIntReal()
APFloat & getComplexFloatImag()
APFloat & getComplexFloatReal()
APValue & getStructBase(unsigned i)
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
QualType getQualifiedType(SplitQualType split) const
Un-split a SplitQualType.
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
bool hasOwnVFPtr() const
hasOwnVFPtr - Does this class provide its own virtual-function table pointer, rather than inheriting ...
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
const CXXRecordDecl * getPrimaryBase() const
getPrimaryBase - Get the primary base for this record.
Represents an array type, per C99 6.7.5.2 - Array Declarators.
QualType getElementType() const
cir::ConstRecordAttr getAnonConstRecord(mlir::ArrayAttr arrayAttr, bool packed=false, bool padded=false, mlir::Type ty={})
mlir::Attribute getConstRecordOrZeroAttr(mlir::ArrayAttr arrayAttr, bool packed=false, bool padded=false, mlir::Type type={})
bool isNullValue(mlir::Attribute attr) const
cir::RecordType getCompleteRecordType(mlir::ArrayAttr fields, bool packed=false, bool padded=false, llvm::StringRef name="")
void computeGlobalViewIndicesFromFlatOffset(int64_t offset, mlir::Type ty, cir::CIRDataLayout layout, llvm::SmallVectorImpl< int64_t > &indices)
cir::ConstArrayAttr getConstArray(mlir::Attribute attrs, cir::ArrayType arrayTy) const
virtual cir::GlobalOp getAddrOfVTable(const CXXRecordDecl *rd, CharUnits vptrOffset)=0
Get the address of the vtable for the given record decl which should be used for the vptr at the give...
This class organizes the cross-function state that is used while generating CIR code.
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
clang::ASTContext & getASTContext() const
mlir::Type convertType(clang::QualType type)
CIRGenBuilderTy & getBuilder()
ItaniumVTableContext & getItaniumVTableContext()
cir::FuncOp getAddrOfFunction(clang::GlobalDecl gd, mlir::Type funcType=nullptr, bool forVTable=false, bool dontDefer=false, ForDefinition_t isForDefinition=NotForDefinition)
Return the address of the given function.
cir::GlobalViewAttr getAddrOfGlobalVarAttr(const VarDecl *d)
Return the mlir::GlobalViewAttr for the address of the given global.
mlir::Location getLoc(clang::SourceLocation cLoc)
Helpers to convert the presumed location of Clang's SourceLocation to an MLIR Location.
mlir::Value emitNullConstant(QualType t, mlir::Location loc)
Return the result of value-initializing the given type, i.e.
mlir::ModuleOp getModule() const
CIRGenCXXABI & getCXXABI() const
cir::GlobalViewAttr getAddrOfConstantStringFromLiteral(const StringLiteral *s, llvm::StringRef name=".str")
Return a global symbol reference to a constant array for the given string literal.
bool shouldZeroInitPadding() const
mlir::Attribute getConstantArrayFromStringLiteral(const StringLiteral *e)
Return a constant array for the given string.
mlir::Type convertTypeForMem(clang::QualType, bool forBitField=false)
Convert type T into an mlir::Type.
void finalize(cir::GlobalOp gv)
mlir::Attribute emitForMemory(mlir::Attribute c, QualType destType)
mlir::Attribute emitNullForMemory(mlir::Location loc, QualType t)
mlir::TypedAttr tryEmitPrivate(const Expr *e, QualType destType)
mlir::Attribute tryEmitPrivateForVarInit(const VarDecl &d)
mlir::Attribute tryEmitPrivateForMemory(const Expr *e, QualType destTy)
mlir::Attribute emitAbstract(const Expr *e, QualType destType)
Emit the result of the given expression as an abstract constant, asserting that it succeeded.
mlir::Attribute tryEmitForInitializer(const VarDecl &d)
Try to emit the initializer of the given declaration as an abstract constant.
mlir::Attribute tryEmitAbstractForInitializer(const VarDecl &d)
Try to emit the initializer of the given declaration as an abstract constant.
mlir::Attribute tryEmitConstantExpr(const ConstantExpr *ce)
SourceLocation getBeginLoc() const LLVM_READONLY
Represents a C++ constructor within a class.
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
SourceLocation getBeginLoc() const
SourceRange getSourceRange() const LLVM_READONLY
CastKind getCastKind() const
CharUnits - This is an opaque type for sizes expressed in character units.
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
static CharUnits One()
One - Construct a CharUnits quantity of one.
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
CharUnits alignTo(const CharUnits &Align) const
alignTo - Returns the next integer (mod 2**64) that is greater than or equal to this quantity and is ...
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
const Expr * getInitializer() const
ConstantExpr - An expression that occurs in a constant context and optionally the result of evaluatin...
APValue getAPValueResult() const
SourceLocation getBeginLoc() const LLVM_READONLY
bool hasAPValueResult() const
SourceLocation getBeginLoc() const LLVM_READONLY
This represents one expression.
bool EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsLValue - Evaluate an expression to see if we can fold it to an lvalue with link time known ...
bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsRValue - Return true if this is a constant which we can fold to an rvalue using any crazy t...
bool HasSideEffects(const ASTContext &Ctx, bool IncludePossibleEffects=true) const
HasSideEffects - This routine returns true for all those expressions which have any effect other than...
bool isBitField() const
Determines whether this field is a bitfield.
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
bool isZeroSize(const ASTContext &Ctx) const
Determine if this field is a subobject of zero size, that is, either a zero-length bit-field or a fie...
bool isUnnamedBitField() const
Determines whether this is an unnamed bitfield.
const Expr * getSubExpr() const
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Expr * getResultExpr()
Return the result expression of this controlling expression.
SourceLocation getBeginLoc() const LLVM_READONLY
bool isTransparent() const
Is this a transparent initializer list (that is, an InitListExpr that is purely syntactic,...
FieldDecl * getInitializedFieldInUnion()
If this initializes a union, specifies which field in the union to initialize.
const Expr * getInit(unsigned Init) const
const VTableLayout & getVTableLayout(const CXXRecordDecl *RD)
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation getBeginLoc() const LLVM_READONLY
const Expr * getSubExpr() const
PointerType - C99 6.7.5.1 - Pointer Declarators.
A (possibly-)qualified type.
bool hasFlexibleArrayMember() const
field_range fields() const
RecordDecl * getDefinitionOrSelf() const
Encodes a location in the source.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
SourceLocation getBeginLoc() const LLVM_READONLY
Expr * getReplacement() const
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
CXXRecordDecl * castAsCXXRecordDecl() const
const T * castAs() const
Member-template castAs<specific type>.
bool isReferenceType() const
bool isMemberDataPointerType() const
bool isVectorType() const
const T * getAs() const
Member-template getAs<specific type>'.
bool isRecordType() const
Expr * getSubExpr() const
AddressPointLocation getAddressPoint(BaseSubobject Base) const
Represents a variable declaration or definition.
APValue * evaluateValue() const
Attempt to evaluate the value of the initializer attached to this declaration, and produce notes expl...
bool hasConstantInitialization() const
Determine whether this variable has constant initialization.
const Expr * getInit() const
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Represents a GCC generic vector type.
const internal::VariadicAllOfMatcher< Attr > attr
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const AstTypeMatcher< ComplexType > complexType
const AstTypeMatcher< RecordType > recordType
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
bool declaresSameEntity(const Decl *D1, const Decl *D2)
Determine whether two declarations declare the same entity.
void finalize(TemplateInstantiationCallbackPtrs &Callbacks, const Sema &TheSema)
U cast(CodeGen::Address addr)
static bool recordZeroInitPadding()
static bool addressPointerAuthInfo()
static bool constEmitterArrayILE()
static bool constEmitterVectorILE()
mlir::Type UCharTy
ClangIR char.
EvalResult is a struct with detailed info about an evaluated expression.
APValue Val
Val - This is the value the expression can be folded to.
bool hasSideEffects() const
Return true if the evaluated expression has side effects.
unsigned AddressPointIndex