9#ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENBUILDER_H
10#define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENBUILDER_H
15#include "mlir/IR/Attributes.h"
16#include "mlir/IR/BuiltinAttributes.h"
17#include "mlir/Support/LLVM.h"
23#include "llvm/ADT/APFloat.h"
24#include "llvm/ADT/STLExtras.h"
30 llvm::StringMap<unsigned> recordNames;
31 llvm::StringMap<unsigned> globalsVersioning;
40 mlir::Attribute
getString(llvm::StringRef str, mlir::Type eltTy,
41 std::optional<size_t> size) {
42 size_t finalSize = size.value_or(str.size());
44 size_t lastNonZeroPos = str.find_last_not_of(
'\0');
47 if (lastNonZeroPos == llvm::StringRef::npos) {
48 auto arrayTy = cir::ArrayType::get(eltTy, finalSize);
49 return cir::ZeroAttr::get(arrayTy);
52 size_t trailingZerosNum = 0;
53 if (finalSize > lastNonZeroPos + 2)
54 trailingZerosNum = finalSize - lastNonZeroPos - 1;
55 auto truncatedArrayTy =
56 cir::ArrayType::get(eltTy, finalSize - trailingZerosNum);
57 auto fullArrayTy = cir::ArrayType::get(eltTy, finalSize);
58 return cir::ConstArrayAttr::get(
60 mlir::StringAttr::get(str.drop_back(trailingZerosNum),
66 cir::ArrayType arrayTy)
const {
67 return cir::ConstArrayAttr::get(arrayTy, attrs);
73 mlir::Type
type = {});
80 for (
auto &f : arrayAttr) {
81 auto ta = mlir::cast<mlir::TypedAttr>(f);
82 members.push_back(ta.getType());
88 auto sTy = mlir::cast<cir::RecordType>(ty);
89 return cir::ConstRecordAttr::get(sTy, arrayAttr);
94 return cir::TypeInfoAttr::get(anonRecord.getType(), fieldsAttr);
100 auto it = recordNames.find(baseName);
101 if (it == recordNames.end()) {
102 recordNames[baseName] = 0;
106 return baseName +
"." + std::to_string(recordNames[baseName]++);
110 if (&
format == &llvm::APFloat::IEEEdouble())
111 return cir::LongDoubleType::get(getContext(), typeCache.doubleTy);
112 if (&
format == &llvm::APFloat::x87DoubleExtended())
113 return cir::LongDoubleType::get(getContext(), typeCache.fP80Ty);
114 if (&
format == &llvm::APFloat::IEEEquad())
115 return cir::LongDoubleType::get(getContext(), typeCache.fP128Ty);
116 if (&
format == &llvm::APFloat::PPCDoubleDouble())
117 llvm_unreachable(
"NYI: PPC double-double format for long double");
118 llvm_unreachable(
"Unsupported format for long double");
126 bool isVarArg =
false) {
127 return cir::FuncType::get(params, retTy, isVarArg);
134 return cir::RecordType::Class;
136 return cir::RecordType::Struct;
138 return cir::RecordType::Union;
140 llvm_unreachable(
"interface records are NYI");
142 llvm_unreachable(
"enums are not records");
144 llvm_unreachable(
"Unsupported record kind");
152 bool packed,
bool padded,
153 llvm::StringRef name) {
154 const auto nameAttr = getStringAttr(name);
155 auto kind = cir::RecordType::RecordKind::Struct;
164 assert(!
type.isIncomplete() ||
165 (
type.getMembers() == members &&
type.getPacked() == packed &&
166 type.getPadded() == padded));
170 type.complete(members, packed, padded);
178 llvm::StringRef name =
"");
185 const mlir::StringAttr nameAttr = getStringAttr(name);
186 cir::RecordType::RecordKind kind = cir::RecordType::RecordKind::Struct;
193 unsigned memberIndex) {
194 return cir::DataMemberAttr::get(ty, memberIndex);
198 return cir::DataMemberAttr::get(ty);
204 if (mlir::isa<cir::ZeroAttr>(
attr))
207 if (
const auto ptrVal = mlir::dyn_cast<cir::ConstPtrAttr>(
attr))
208 return ptrVal.isNullValue();
210 if (
const auto intVal = mlir::dyn_cast<cir::IntAttr>(
attr))
211 return intVal.isNullValue();
213 if (
const auto boolVal = mlir::dyn_cast<cir::BoolAttr>(
attr))
214 return !boolVal.getValue();
216 if (
auto fpAttr = mlir::dyn_cast<cir::FPAttr>(
attr)) {
217 auto fpVal = fpAttr.getValue();
219 llvm::APFloat fv(+0.0);
220 fv.convert(fpVal.getSemantics(), llvm::APFloat::rmNearestTiesToEven,
222 return fv.bitwiseIsEqual(fpVal);
224 if (
const auto recordVal = mlir::dyn_cast<cir::ConstRecordAttr>(
attr)) {
225 for (
const auto elt : recordVal.getMembers()) {
227 if (mlir::isa<mlir::StringAttr>(elt))
235 if (
const auto arrayVal = mlir::dyn_cast<cir::ConstArrayAttr>(
attr)) {
236 if (mlir::isa<mlir::StringAttr>(arrayVal.getElts()))
240 mlir::cast<mlir::ArrayAttr>(arrayVal.getElts()),
241 [&](
const mlir::Attribute &elt) { return isNullValue(elt); });
261 return cir::IntType::get(getContext(), n,
false);
276 return cir::IntType::get(getContext(), n,
true);
297 cir::ConstantOp
getConstInt(mlir::Location loc, llvm::APSInt intVal);
299 cir::ConstantOp
getConstInt(mlir::Location loc, llvm::APInt intVal,
302 cir::ConstantOp
getConstInt(mlir::Location loc, mlir::Type t, uint64_t
c);
304 cir::ConstantOp
getConstFP(mlir::Location loc, mlir::Type t,
305 llvm::APFloat fpVal);
308 return i == typeCache.uInt8Ty || i == typeCache.sInt8Ty;
311 return i == typeCache.uInt16Ty || i == typeCache.sInt16Ty;
314 return i == typeCache.uInt32Ty || i == typeCache.sInt32Ty;
317 return i == typeCache.uInt64Ty || i == typeCache.sInt64Ty;
319 bool isInt(mlir::Type i) {
return mlir::isa<cir::IntType>(i); }
326 bool packed =
false,
bool padded =
false) {
328 auto kind = cir::RecordType::RecordKind::Struct;
351 if (
auto intTy = mlir::dyn_cast<cir::IntType>(value.getType())) {
353 if (intTy.isUnsigned())
355 return cir::UnaryOp::create(*
this, value.getLoc(), value.getType(),
356 cir::UnaryOpKind::Minus, value);
359 llvm_unreachable(
"negation for the given type is NYI");
363 cir::FPClassTest flags) {
364 return cir::IsFPClassOp::create(*
this, loc, src, flags);
369 mlir::Location loc) {
378 return cir::CastOp::create(*
this, v.getLoc(), destType,
379 cir::CastKind::floating, v);
382 mlir::Value
createFSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
387 return cir::BinOp::create(*
this, loc, cir::BinOpKind::Sub, lhs, rhs);
390 mlir::Value
createFAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
395 return cir::BinOp::create(*
this, loc, cir::BinOpKind::Add, lhs, rhs);
397 mlir::Value
createFMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
402 return cir::BinOp::create(*
this, loc, cir::BinOpKind::Mul, lhs, rhs);
404 mlir::Value
createFDiv(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
409 return cir::BinOp::create(*
this, loc, cir::BinOpKind::Div, lhs, rhs);
413 cir::PointerType destType,
bool isRefCast,
414 cir::DynamicCastInfoAttr info) {
416 isRefCast ? cir::DynamicCastKind::Ref : cir::DynamicCastKind::Ptr;
417 return cir::DynamicCastOp::create(*
this, loc, destType, castKind, src, info,
422 bool vtableUseRelativeLayout) {
426 return cir::DynamicCastOp::create(
427 *
this, loc, destTy, cir::DynamicCastKind::Ptr, src,
428 cir::DynamicCastInfoAttr{}, vtableUseRelativeLayout);
432 mlir::Type destType,
unsigned offset,
433 bool assumeNotNull) {
439 cir::BaseClassAddrOp::create(*
this, loc, ptrTy, addr.
getPointer(),
440 mlir::APInt(64, offset), assumeNotNull);
445 mlir::Type destType,
unsigned offset,
446 bool assumeNotNull) {
452 cir::DerivedClassAddrOp::create(*
this, loc, ptrTy, addr.
getPointer(),
453 mlir::APInt(64, offset), assumeNotNull);
458 mlir::Value addr, uint64_t offset) {
459 return cir::VTTAddrPointOp::create(*
this, loc, retTy,
460 mlir::FlatSymbolRefAttr{}, addr, offset);
464 mlir::FlatSymbolRefAttr sym, uint64_t offset) {
465 return cir::VTTAddrPointOp::create(*
this, loc, retTy, sym, mlir::Value{},
472 mlir::Type destType) {
482 bool isVolatile =
false) {
484 return cir::LoadOp::create(*
this, loc, addr.
getPointer(),
false,
486 cir::SyncScopeKindAttr{},
487 cir::MemOrderAttr{});
491 mlir::Value ptr, llvm::MaybeAlign align) {
492 if (ty != mlir::cast<cir::PointerType>(ptr.getType()).getPointee())
494 uint64_t alignment = align ? align->value() : 0;
496 return cir::LoadOp::create(*
this, loc, ptr,
false,
498 cir::SyncScopeKindAttr{},
499 cir::MemOrderAttr{});
509 bool isVolatile =
false,
510 mlir::IntegerAttr align = {},
511 cir::SyncScopeKindAttr scope = {},
512 cir::MemOrderAttr order = {}) {
515 return CIRBaseBuilderTy::createStore(loc, val, dst.getPointer(), isVolatile,
516 align, scope, order);
522 auto srcPtrTy = mlir::cast<cir::PointerType>(value.getType());
523 auto srcComplexTy = mlir::cast<cir::ComplexType>(srcPtrTy.getPointee());
524 return cir::ComplexRealPtrOp::create(
525 *
this, loc,
getPointerTo(srcComplexTy.getElementType()), value);
537 auto srcPtrTy = mlir::cast<cir::PointerType>(value.getType());
538 auto srcComplexTy = mlir::cast<cir::ComplexType>(srcPtrTy.getPointee());
539 return cir::ComplexImagPtrOp::create(
540 *
this, loc,
getPointerTo(srcComplexTy.getElementType()), value);
549 mlir::Value objectPtr,
550 mlir::Value memberPtr) {
551 auto memberPtrTy = mlir::cast<cir::DataMemberType>(memberPtr.getType());
555 cir::PointerType resultTy =
getPointerTo(memberPtrTy.getMemberTy());
557 return cir::GetRuntimeMemberOp::create(*
this, loc, resultTy, objectPtr,
565 mlir::Location arrayLocEnd, mlir::Value arrayPtr,
566 mlir::Type eltTy, mlir::Value idx,
585 [[nodiscard]] cir::GlobalOp
587 mlir::StringRef name, mlir::Type
type,
bool isConstant,
588 cir::GlobalLinkageKind linkage) {
590 std::string uniqueName;
591 if (
unsigned version = globalsVersioning[name.str()]++)
592 uniqueName = name.str() +
"." + std::to_string(version);
594 uniqueName = name.str();
600 return cir::StackSaveOp::create(*
this, loc, ty);
604 return cir::StackRestoreOp::create(*
this, loc, v);
608 Address dstAddr, mlir::Type storageType,
610 bool isLvalueVolatile,
bool useVolatile) {
616 useVolatile ? cir::IntType::get(storageType.getContext(),
619 return cir::SetBitfieldOp::create(
620 *
this, loc, resultType, dstAddr.
getPointer(), storageType, src,
626 Address addr, mlir::Type storageType,
628 bool isLvalueVolatile,
bool useVolatile) {
634 useVolatile ? cir::IntType::get(storageType.getContext(),
637 return cir::GetBitfieldOp::create(*
this, loc, resultType, addr.
getPointer(),
638 storageType, info.
name, info.
size, offset,
646 auto vecType = mlir::cast<cir::VectorType>(vec1.getType());
648 cir::VectorType::get(vecType.getElementType(), maskAttrs.size());
649 return cir::VecShuffleOp::create(*
this, loc, resultTy, vec1, vec2,
650 getArrayAttr(maskAttrs));
656 auto maskAttrs = llvm::to_vector_of<mlir::Attribute>(
657 llvm::map_range(mask, [&](int32_t idx) {
667 cir::ConstantOp poison =
668 getConstant(loc, cir::PoisonAttr::get(vec1.getType()));
static bool isUnsigned(SValBuilder &SVB, NonLoc Value)
__device__ __2f16 float c
cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr)
cir::PointerType getPointerTo(mlir::Type ty)
mlir::Value createPtrBitcast(mlir::Value src, mlir::Type newPointeeTy)
mlir::Value createIntCast(mlir::Value src, mlir::Type newTy)
mlir::Value createBitcast(mlir::Value src, mlir::Type newTy)
CIRBaseBuilderTy(mlir::MLIRContext &mlirContext)
cir::GlobalOp createGlobal(mlir::ModuleOp mlirModule, mlir::Location loc, mlir::StringRef name, mlir::Type type, bool isConstant, cir::GlobalLinkageKind linkage)
mlir::IntegerAttr getAlignmentAttr(clang::CharUnits alignment)
cir::ConstantOp getConstantInt(mlir::Location loc, mlir::Type ty, int64_t value)
cir::PointerType getVoidPtrTy(clang::LangAS langAS=clang::LangAS::Default)
mlir::Value getPointer() const
mlir::Type getElementType() const
clang::CharUnits getAlignment() const
cir::RecordType getCompleteNamedRecordType(llvm::ArrayRef< mlir::Type > members, bool packed, bool padded, llvm::StringRef name)
Get a CIR named record type.
cir::StackSaveOp createStackSave(mlir::Location loc, mlir::Type ty)
cir::IntType getUInt64Ty()
cir::IntType getUInt8Ty()
cir::TypeInfoAttr getTypeInfo(mlir::ArrayAttr fieldsAttr)
mlir::Value createComplexRealPtr(mlir::Location loc, mlir::Value value)
Create a cir.complex.real_ptr operation that derives a pointer to the real part of the complex value ...
cir::VecShuffleOp createVecShuffle(mlir::Location loc, mlir::Value vec1, mlir::Value vec2, llvm::ArrayRef< int64_t > mask)
cir::ConstantOp getUInt64(uint64_t c, mlir::Location loc)
cir::RecordType::RecordKind getRecordKind(const clang::TagTypeKind kind)
Get a CIR record kind from a AST declaration tag.
cir::IntType getSIntNTy(int n)
cir::ConstRecordAttr getAnonConstRecord(mlir::ArrayAttr arrayAttr, bool packed=false, bool padded=false, mlir::Type ty={})
cir::ConstantOp getSInt64(uint64_t c, mlir::Location loc)
cir::RecordType getIncompleteRecordTy(llvm::StringRef name, const clang::RecordDecl *rd)
Get an incomplete CIR struct type.
cir::ConstantOp getUInt32(uint32_t c, mlir::Location loc)
cir::VecShuffleOp createVecShuffle(mlir::Location loc, mlir::Value vec1, mlir::Value vec2, llvm::ArrayRef< mlir::Attribute > maskAttrs)
cir::GlobalOp createVersionedGlobal(mlir::ModuleOp module, mlir::Location loc, mlir::StringRef name, mlir::Type type, bool isConstant, cir::GlobalLinkageKind linkage)
Creates a versioned global variable.
cir::PointerType getUInt8PtrTy()
std::string getUniqueRecordName(const std::string &baseName)
mlir::Attribute getConstRecordOrZeroAttr(mlir::ArrayAttr arrayAttr, bool packed=false, bool padded=false, mlir::Type type={})
cir::RecordType getAnonRecordTy(llvm::ArrayRef< mlir::Type > members, bool packed=false, bool padded=false)
Get a CIR anonymous record type.
mlir::Value createVTTAddrPoint(mlir::Location loc, mlir::Type retTy, mlir::FlatSymbolRefAttr sym, uint64_t offset)
Address createBaseClassAddr(mlir::Location loc, Address addr, mlir::Type destType, unsigned offset, bool assumeNotNull)
mlir::Value createComplexImagPtr(mlir::Location loc, mlir::Value value)
Create a cir.complex.imag_ptr operation that derives a pointer to the imaginary part of the complex v...
mlir::Value maybeBuildArrayDecay(mlir::Location loc, mlir::Value arrayPtr, mlir::Type eltTy)
Returns a decayed pointer to the first element of the array pointed to by arrayPtr.
mlir::Attribute getString(llvm::StringRef str, mlir::Type eltTy, std::optional< size_t > size)
Get a cir::ConstArrayAttr for a string literal.
cir::LoadOp createAlignedLoad(mlir::Location loc, mlir::Type ty, mlir::Value ptr, llvm::MaybeAlign align)
cir::ConstantOp getConstFP(mlir::Location loc, mlir::Type t, llvm::APFloat fpVal)
mlir::Value createFloatingCast(mlir::Value v, mlir::Type destType)
cir::FuncType getFuncType(llvm::ArrayRef< mlir::Type > params, mlir::Type retTy, bool isVarArg=false)
cir::FP16Type getFp16Ty()
mlir::Value createFDiv(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::SingleType getSingleTy()
Address createDerivedClassAddr(mlir::Location loc, Address addr, mlir::Type destType, unsigned offset, bool assumeNotNull)
std::string getUniqueAnonRecordName()
cir::GetRuntimeMemberOp createGetIndirectMember(mlir::Location loc, mlir::Value objectPtr, mlir::Value memberPtr)
Address createElementBitCast(mlir::Location loc, Address addr, mlir::Type destType)
Cast the element type of the given address to a different type, preserving information like the align...
cir::IntType getSInt8Ty()
cir::ConstantOp getNullDataMemberPtr(cir::DataMemberType ty, mlir::Location loc)
Create constant nullptr for pointer-to-data-member type ty.
bool isInt16Ty(mlir::Type i)
bool isInt32Ty(mlir::Type i)
mlir::Value createDynCastToVoid(mlir::Location loc, mlir::Value src, bool vtableUseRelativeLayout)
cir::VecShuffleOp createVecShuffle(mlir::Location loc, mlir::Value vec1, llvm::ArrayRef< int64_t > mask)
cir::BF16Type getBfloat6Ty()
cir::IntType getUInt16Ty()
mlir::Value createDynCast(mlir::Location loc, mlir::Value src, cir::PointerType destType, bool isRefCast, cir::DynamicCastInfoAttr info)
mlir::Value createGetBitfield(mlir::Location loc, mlir::Type resultType, Address addr, mlir::Type storageType, const CIRGenBitFieldInfo &info, bool isLvalueVolatile, bool useVolatile)
bool isNullValue(mlir::Attribute attr) const
bool isInt64Ty(mlir::Type i)
cir::StackRestoreOp createStackRestore(mlir::Location loc, mlir::Value v)
mlir::Value createSetBitfield(mlir::Location loc, mlir::Type resultType, Address dstAddr, mlir::Type storageType, mlir::Value src, const CIRGenBitFieldInfo &info, bool isLvalueVolatile, bool useVolatile)
Address createComplexRealPtr(mlir::Location loc, Address addr)
mlir::Value createFAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::StoreOp createStore(mlir::Location loc, mlir::Value val, Address dst, bool isVolatile=false, mlir::IntegerAttr align={}, cir::SyncScopeKindAttr scope={}, cir::MemOrderAttr order={})
CIRGenBuilderTy(mlir::MLIRContext &mlirContext, const CIRGenTypeCache &tc)
cir::IsFPClassOp createIsFPClass(mlir::Location loc, mlir::Value src, cir::FPClassTest flags)
cir::RecordType getCompleteRecordType(mlir::ArrayAttr fields, bool packed=false, bool padded=false, llvm::StringRef name="")
cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal)
cir::VoidType getVoidTy()
void computeGlobalViewIndicesFromFlatOffset(int64_t offset, mlir::Type ty, cir::CIRDataLayout layout, llvm::SmallVectorImpl< int64_t > &indices)
cir::IntType getUInt32Ty()
mlir::Type getPtrToVPtrType()
cir::IntType getSInt32Ty()
Address createComplexImagPtr(mlir::Location loc, Address addr)
mlir::Value createFMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::IntType getSInt64Ty()
cir::DataMemberAttr getNullDataMemberAttr(cir::DataMemberType ty)
cir::ConstantOp getSInt32(int32_t c, mlir::Location loc)
mlir::Value createVTTAddrPoint(mlir::Location loc, mlir::Type retTy, mlir::Value addr, uint64_t offset)
cir::LoadOp createLoad(mlir::Location loc, Address addr, bool isVolatile=false)
cir::LongDoubleType getLongDoubleTy(const llvm::fltSemantics &format) const
cir::IntType getSInt16Ty()
cir::ConstArrayAttr getConstArray(mlir::Attribute attrs, cir::ArrayType arrayTy) const
mlir::Value createFSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::IntType getUIntNTy(int n)
mlir::Value getArrayElement(mlir::Location arrayLocBegin, mlir::Location arrayLocEnd, mlir::Value arrayPtr, mlir::Type eltTy, mlir::Value idx, bool shouldDecay)
Create a cir.ptr_stride operation to get access to an array element.
bool isInt8Ty(mlir::Type i)
mlir::Value createNeg(mlir::Value value)
cir::LoadOp createAlignedLoad(mlir::Location loc, mlir::Type ty, mlir::Value ptr, clang::CharUnits align=clang::CharUnits::One())
cir::DataMemberAttr getDataMemberAttr(cir::DataMemberType ty, unsigned memberIndex)
cir::DoubleType getDoubleTy()
CharUnits - This is an opaque type for sizes expressed in character units.
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
static CharUnits One()
One - Construct a CharUnits quantity of one.
Represents a struct/union/class.
TagKind getTagKind() const
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
TagTypeKind
The kind of a tag type.
@ Interface
The "__interface" keyword.
@ Struct
The "struct" keyword.
@ Class
The "class" keyword.
@ Union
The "union" keyword.
@ Enum
The "enum" keyword.
static bool metaDataNode()
static bool addressSpace()
static bool fpConstraints()
static bool astRecordDeclAttr()
static bool fastMathFlags()
Record with information about how a bitfield should be accessed.
unsigned offset
The offset within a contiguous run of bitfields that are represented as a single "field" within the c...
unsigned volatileStorageSize
The storage size in bits which should be used when accessing this bitfield.
unsigned size
The total size of the bit-field, in bits.
unsigned isSigned
Whether the bit-field is signed.
unsigned volatileOffset
The offset within a contiguous run of bitfields that are represented as a single "field" within the c...
llvm::StringRef name
The name of a bitfield.
This structure provides a set of types that are commonly used during IR emission.