clang 22.0.0git
CIRGenBuilder.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENBUILDER_H
10#define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENBUILDER_H
11
12#include "Address.h"
13#include "CIRGenRecordLayout.h"
14#include "CIRGenTypeCache.h"
15#include "mlir/IR/Attributes.h"
16#include "mlir/IR/BuiltinAttributes.h"
17#include "mlir/Support/LLVM.h"
20
23#include "llvm/ADT/APFloat.h"
24#include "llvm/ADT/STLExtras.h"
25
26namespace clang::CIRGen {
27
29 const CIRGenTypeCache &typeCache;
30 llvm::StringMap<unsigned> recordNames;
31 llvm::StringMap<unsigned> globalsVersioning;
32
33public:
34 CIRGenBuilderTy(mlir::MLIRContext &mlirContext, const CIRGenTypeCache &tc)
35 : CIRBaseBuilderTy(mlirContext), typeCache(tc) {}
36
37 /// Get a cir::ConstArrayAttr for a string literal.
38 /// Note: This is different from what is returned by
39 /// mlir::Builder::getStringAttr() which is an mlir::StringAttr.
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());
43
44 size_t lastNonZeroPos = str.find_last_not_of('\0');
45 // If the string is full of null bytes, emit a #cir.zero rather than
46 // a #cir.const_array.
47 if (lastNonZeroPos == llvm::StringRef::npos) {
48 auto arrayTy = cir::ArrayType::get(eltTy, finalSize);
49 return cir::ZeroAttr::get(arrayTy);
50 }
51 // We emit trailing zeros only if there are multiple trailing zeros.
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(
59 fullArrayTy,
60 mlir::StringAttr::get(str.drop_back(trailingZerosNum),
61 truncatedArrayTy),
62 trailingZerosNum);
63 }
64
65 cir::ConstArrayAttr getConstArray(mlir::Attribute attrs,
66 cir::ArrayType arrayTy) const {
67 return cir::ConstArrayAttr::get(arrayTy, attrs);
68 }
69
70 mlir::Attribute getConstRecordOrZeroAttr(mlir::ArrayAttr arrayAttr,
71 bool packed = false,
72 bool padded = false,
73 mlir::Type type = {});
74
75 cir::ConstRecordAttr getAnonConstRecord(mlir::ArrayAttr arrayAttr,
76 bool packed = false,
77 bool padded = false,
78 mlir::Type ty = {}) {
80 for (auto &f : arrayAttr) {
81 auto ta = mlir::cast<mlir::TypedAttr>(f);
82 members.push_back(ta.getType());
83 }
84
85 if (!ty)
86 ty = getAnonRecordTy(members, packed, padded);
87
88 auto sTy = mlir::cast<cir::RecordType>(ty);
89 return cir::ConstRecordAttr::get(sTy, arrayAttr);
90 }
91
92 cir::TypeInfoAttr getTypeInfo(mlir::ArrayAttr fieldsAttr) {
93 cir::ConstRecordAttr anonRecord = getAnonConstRecord(fieldsAttr);
94 return cir::TypeInfoAttr::get(anonRecord.getType(), fieldsAttr);
95 }
96
97 std::string getUniqueAnonRecordName() { return getUniqueRecordName("anon"); }
98
99 std::string getUniqueRecordName(const std::string &baseName) {
100 auto it = recordNames.find(baseName);
101 if (it == recordNames.end()) {
102 recordNames[baseName] = 0;
103 return baseName;
104 }
105
106 return baseName + "." + std::to_string(recordNames[baseName]++);
107 }
108
109 cir::LongDoubleType getLongDoubleTy(const llvm::fltSemantics &format) const {
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");
119 }
120
121 mlir::Type getPtrToVPtrType() {
122 return getPointerTo(cir::VPtrType::get(getContext()));
123 }
124
125 cir::FuncType getFuncType(llvm::ArrayRef<mlir::Type> params, mlir::Type retTy,
126 bool isVarArg = false) {
127 return cir::FuncType::get(params, retTy, isVarArg);
128 }
129
130 /// Get a CIR record kind from a AST declaration tag.
131 cir::RecordType::RecordKind getRecordKind(const clang::TagTypeKind kind) {
132 switch (kind) {
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");
143 }
144 llvm_unreachable("Unsupported record kind");
145 }
146
147 /// Get a CIR named record type.
148 ///
149 /// If a record already exists and is complete, but the client tries to fetch
150 /// it with a different set of attributes, this method will crash.
152 bool packed, bool padded,
153 llvm::StringRef name) {
154 const auto nameAttr = getStringAttr(name);
155 auto kind = cir::RecordType::RecordKind::Struct;
157
158 // Create or get the record.
159 auto type =
160 getType<cir::RecordType>(members, nameAttr, packed, padded, kind);
161
162 // If we found an existing type, verify that either it is incomplete or
163 // it matches the requested attributes.
164 assert(!type.isIncomplete() ||
165 (type.getMembers() == members && type.getPacked() == packed &&
166 type.getPadded() == padded));
167
168 // Complete an incomplete record or ensure the existing complete record
169 // matches the requested attributes.
170 type.complete(members, packed, padded);
171
172 return type;
173 }
174
175 cir::RecordType getCompleteRecordType(mlir::ArrayAttr fields,
176 bool packed = false,
177 bool padded = false,
178 llvm::StringRef name = "");
179
180 /// Get an incomplete CIR struct type. If we have a complete record
181 /// declaration, we may create an incomplete type and then add the
182 /// members, so \p rd here may be complete.
183 cir::RecordType getIncompleteRecordTy(llvm::StringRef name,
184 const clang::RecordDecl *rd) {
185 const mlir::StringAttr nameAttr = getStringAttr(name);
186 cir::RecordType::RecordKind kind = cir::RecordType::RecordKind::Struct;
187 if (rd)
188 kind = getRecordKind(rd->getTagKind());
189 return getType<cir::RecordType>(nameAttr, kind);
190 }
191
192 cir::DataMemberAttr getDataMemberAttr(cir::DataMemberType ty,
193 unsigned memberIndex) {
194 return cir::DataMemberAttr::get(ty, memberIndex);
195 }
196
197 // Return true if the value is a null constant such as null pointer, (+0.0)
198 // for floating-point or zero initializer
199 bool isNullValue(mlir::Attribute attr) const {
200 if (mlir::isa<cir::ZeroAttr>(attr))
201 return true;
202
203 if (const auto ptrVal = mlir::dyn_cast<cir::ConstPtrAttr>(attr))
204 return ptrVal.isNullValue();
205
206 if (const auto intVal = mlir::dyn_cast<cir::IntAttr>(attr))
207 return intVal.isNullValue();
208
209 if (const auto boolVal = mlir::dyn_cast<cir::BoolAttr>(attr))
210 return !boolVal.getValue();
211
212 if (auto fpAttr = mlir::dyn_cast<cir::FPAttr>(attr)) {
213 auto fpVal = fpAttr.getValue();
214 bool ignored;
215 llvm::APFloat fv(+0.0);
216 fv.convert(fpVal.getSemantics(), llvm::APFloat::rmNearestTiesToEven,
217 &ignored);
218 return fv.bitwiseIsEqual(fpVal);
219 }
220 if (const auto recordVal = mlir::dyn_cast<cir::ConstRecordAttr>(attr)) {
221 for (const auto elt : recordVal.getMembers()) {
222 // FIXME(cir): the record's ID should not be considered a member.
223 if (mlir::isa<mlir::StringAttr>(elt))
224 continue;
225 if (!isNullValue(elt))
226 return false;
227 }
228 return true;
229 }
230
231 if (const auto arrayVal = mlir::dyn_cast<cir::ConstArrayAttr>(attr)) {
232 if (mlir::isa<mlir::StringAttr>(arrayVal.getElts()))
233 return false;
234
235 return llvm::all_of(
236 mlir::cast<mlir::ArrayAttr>(arrayVal.getElts()),
237 [&](const mlir::Attribute &elt) { return isNullValue(elt); });
238 }
239 return false;
240 }
241
242 //
243 // Type helpers
244 // ------------
245 //
246 cir::IntType getUIntNTy(int n) {
247 switch (n) {
248 case 8:
249 return getUInt8Ty();
250 case 16:
251 return getUInt16Ty();
252 case 32:
253 return getUInt32Ty();
254 case 64:
255 return getUInt64Ty();
256 default:
257 return cir::IntType::get(getContext(), n, false);
258 }
259 }
260
261 cir::IntType getSIntNTy(int n) {
262 switch (n) {
263 case 8:
264 return getSInt8Ty();
265 case 16:
266 return getSInt16Ty();
267 case 32:
268 return getSInt32Ty();
269 case 64:
270 return getSInt64Ty();
271 default:
272 return cir::IntType::get(getContext(), n, true);
273 }
274 }
275
276 cir::VoidType getVoidTy() { return typeCache.voidTy; }
277
278 cir::IntType getSInt8Ty() { return typeCache.sInt8Ty; }
279 cir::IntType getSInt16Ty() { return typeCache.sInt16Ty; }
280 cir::IntType getSInt32Ty() { return typeCache.sInt32Ty; }
281 cir::IntType getSInt64Ty() { return typeCache.sInt64Ty; }
282
283 cir::IntType getUInt8Ty() { return typeCache.uInt8Ty; }
284 cir::IntType getUInt16Ty() { return typeCache.uInt16Ty; }
285 cir::IntType getUInt32Ty() { return typeCache.uInt32Ty; }
286 cir::IntType getUInt64Ty() { return typeCache.uInt64Ty; }
287
288 cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal);
289
290 cir::ConstantOp getConstInt(mlir::Location loc, llvm::APInt intVal);
291
292 cir::ConstantOp getConstInt(mlir::Location loc, mlir::Type t, uint64_t c);
293
294 cir::ConstantOp getConstFP(mlir::Location loc, mlir::Type t,
295 llvm::APFloat fpVal);
296
297 bool isInt8Ty(mlir::Type i) {
298 return i == typeCache.uInt8Ty || i == typeCache.sInt8Ty;
299 }
300 bool isInt16Ty(mlir::Type i) {
301 return i == typeCache.uInt16Ty || i == typeCache.sInt16Ty;
302 }
303 bool isInt32Ty(mlir::Type i) {
304 return i == typeCache.uInt32Ty || i == typeCache.sInt32Ty;
305 }
306 bool isInt64Ty(mlir::Type i) {
307 return i == typeCache.uInt64Ty || i == typeCache.sInt64Ty;
308 }
309 bool isInt(mlir::Type i) { return mlir::isa<cir::IntType>(i); }
310
311 // Fetch the type representing a pointer to unsigned int8 values.
312 cir::PointerType getUInt8PtrTy() { return typeCache.uInt8PtrTy; }
313
314 /// Get a CIR anonymous record type.
316 bool packed = false, bool padded = false) {
318 auto kind = cir::RecordType::RecordKind::Struct;
319 return getType<cir::RecordType>(members, packed, padded, kind);
320 }
321
322 //
323 // Constant creation helpers
324 // -------------------------
325 //
326 cir::ConstantOp getSInt32(int32_t c, mlir::Location loc) {
327 return getConstantInt(loc, getSInt32Ty(), c);
328 }
329 cir::ConstantOp getUInt32(uint32_t c, mlir::Location loc) {
330 return getConstantInt(loc, getUInt32Ty(), c);
331 }
332 cir::ConstantOp getSInt64(uint64_t c, mlir::Location loc) {
333 return getConstantInt(loc, getSInt64Ty(), c);
334 }
335 cir::ConstantOp getUInt64(uint64_t c, mlir::Location loc) {
336 return getConstantInt(loc, getUInt64Ty(), c);
337 }
338
339 mlir::Value createNeg(mlir::Value value) {
340
341 if (auto intTy = mlir::dyn_cast<cir::IntType>(value.getType())) {
342 // Source is a unsigned integer: first cast it to signed.
343 if (intTy.isUnsigned())
344 value = createIntCast(value, getSIntNTy(intTy.getWidth()));
345 return cir::UnaryOp::create(*this, value.getLoc(), value.getType(),
346 cir::UnaryOpKind::Minus, value);
347 }
348
349 llvm_unreachable("negation for the given type is NYI");
350 }
351
352 cir::IsFPClassOp createIsFPClass(mlir::Location loc, mlir::Value src,
353 cir::FPClassTest flags) {
354 return cir::IsFPClassOp::create(*this, loc, src, flags);
355 }
356
357 // TODO: split this to createFPExt/createFPTrunc when we have dedicated cast
358 // operations.
359 mlir::Value createFloatingCast(mlir::Value v, mlir::Type destType) {
361
362 return cir::CastOp::create(*this, v.getLoc(), destType,
363 cir::CastKind::floating, v);
364 }
365
366 mlir::Value createFSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
370
371 return cir::BinOp::create(*this, loc, cir::BinOpKind::Sub, lhs, rhs);
372 }
373
374 mlir::Value createFAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
378
379 return cir::BinOp::create(*this, loc, cir::BinOpKind::Add, lhs, rhs);
380 }
381 mlir::Value createFMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
385
386 return cir::BinOp::create(*this, loc, cir::BinOpKind::Mul, lhs, rhs);
387 }
388 mlir::Value createFDiv(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
392
393 return cir::BinOp::create(*this, loc, cir::BinOpKind::Div, lhs, rhs);
394 }
395
396 mlir::Value createDynCast(mlir::Location loc, mlir::Value src,
397 cir::PointerType destType, bool isRefCast,
398 cir::DynamicCastInfoAttr info) {
399 auto castKind =
400 isRefCast ? cir::DynamicCastKind::Ref : cir::DynamicCastKind::Ptr;
401 return cir::DynamicCastOp::create(*this, loc, destType, castKind, src, info,
402 /*relative_layout=*/false);
403 }
404
405 mlir::Value createDynCastToVoid(mlir::Location loc, mlir::Value src,
406 bool vtableUseRelativeLayout) {
407 // TODO(cir): consider address space here.
409 cir::PointerType destTy = getVoidPtrTy();
410 return cir::DynamicCastOp::create(
411 *this, loc, destTy, cir::DynamicCastKind::Ptr, src,
412 cir::DynamicCastInfoAttr{}, vtableUseRelativeLayout);
413 }
414
415 Address createBaseClassAddr(mlir::Location loc, Address addr,
416 mlir::Type destType, unsigned offset,
417 bool assumeNotNull) {
418 if (destType == addr.getElementType())
419 return addr;
420
421 auto ptrTy = getPointerTo(destType);
422 auto baseAddr =
423 cir::BaseClassAddrOp::create(*this, loc, ptrTy, addr.getPointer(),
424 mlir::APInt(64, offset), assumeNotNull);
425 return Address(baseAddr, destType, addr.getAlignment());
426 }
427
428 Address createDerivedClassAddr(mlir::Location loc, Address addr,
429 mlir::Type destType, unsigned offset,
430 bool assumeNotNull) {
431 if (destType == addr.getElementType())
432 return addr;
433
434 cir::PointerType ptrTy = getPointerTo(destType);
435 auto derivedAddr =
436 cir::DerivedClassAddrOp::create(*this, loc, ptrTy, addr.getPointer(),
437 mlir::APInt(64, offset), assumeNotNull);
438 return Address(derivedAddr, destType, addr.getAlignment());
439 }
440
441 mlir::Value createVTTAddrPoint(mlir::Location loc, mlir::Type retTy,
442 mlir::Value addr, uint64_t offset) {
443 return cir::VTTAddrPointOp::create(*this, loc, retTy,
444 mlir::FlatSymbolRefAttr{}, addr, offset);
445 }
446
447 mlir::Value createVTTAddrPoint(mlir::Location loc, mlir::Type retTy,
448 mlir::FlatSymbolRefAttr sym, uint64_t offset) {
449 return cir::VTTAddrPointOp::create(*this, loc, retTy, sym, mlir::Value{},
450 offset);
451 }
452
453 /// Cast the element type of the given address to a different type,
454 /// preserving information like the alignment.
455 Address createElementBitCast(mlir::Location loc, Address addr,
456 mlir::Type destType) {
457 if (destType == addr.getElementType())
458 return addr;
459
460 auto ptrTy = getPointerTo(destType);
461 return Address(createBitcast(loc, addr.getPointer(), ptrTy), destType,
462 addr.getAlignment());
463 }
464
465 cir::LoadOp createLoad(mlir::Location loc, Address addr,
466 bool isVolatile = false) {
467 mlir::IntegerAttr align = getAlignmentAttr(addr.getAlignment());
468 return cir::LoadOp::create(*this, loc, addr.getPointer(), /*isDeref=*/false,
469 isVolatile, /*alignment=*/align,
470 /*sync_scope=*/cir::SyncScopeKindAttr{},
471 /*mem_order=*/cir::MemOrderAttr{});
472 }
473
474 cir::LoadOp createAlignedLoad(mlir::Location loc, mlir::Type ty,
475 mlir::Value ptr, llvm::MaybeAlign align) {
476 if (ty != mlir::cast<cir::PointerType>(ptr.getType()).getPointee())
477 ptr = createPtrBitcast(ptr, ty);
478 uint64_t alignment = align ? align->value() : 0;
479 mlir::IntegerAttr alignAttr = getAlignmentAttr(alignment);
480 return cir::LoadOp::create(*this, loc, ptr, /*isDeref=*/false,
481 /*isVolatile=*/false, alignAttr,
482 /*sync_scope=*/cir::SyncScopeKindAttr{},
483 /*mem_order=*/cir::MemOrderAttr{});
484 }
485
486 cir::LoadOp
487 createAlignedLoad(mlir::Location loc, mlir::Type ty, mlir::Value ptr,
489 return createAlignedLoad(loc, ty, ptr, align.getAsAlign());
490 }
491
492 cir::StoreOp createStore(mlir::Location loc, mlir::Value val, Address dst,
493 bool isVolatile = false,
494 mlir::IntegerAttr align = {},
495 cir::MemOrderAttr order = {}) {
496 if (!align)
497 align = getAlignmentAttr(dst.getAlignment());
498 return CIRBaseBuilderTy::createStore(loc, val, dst.getPointer(), isVolatile,
499 align, order);
500 }
501
502 /// Create a cir.complex.real_ptr operation that derives a pointer to the real
503 /// part of the complex value pointed to by the specified pointer value.
504 mlir::Value createComplexRealPtr(mlir::Location loc, mlir::Value value) {
505 auto srcPtrTy = mlir::cast<cir::PointerType>(value.getType());
506 auto srcComplexTy = mlir::cast<cir::ComplexType>(srcPtrTy.getPointee());
507 return cir::ComplexRealPtrOp::create(
508 *this, loc, getPointerTo(srcComplexTy.getElementType()), value);
509 }
510
511 Address createComplexRealPtr(mlir::Location loc, Address addr) {
512 return Address{createComplexRealPtr(loc, addr.getPointer()),
513 addr.getAlignment()};
514 }
515
516 /// Create a cir.complex.imag_ptr operation that derives a pointer to the
517 /// imaginary part of the complex value pointed to by the specified pointer
518 /// value.
519 mlir::Value createComplexImagPtr(mlir::Location loc, mlir::Value value) {
520 auto srcPtrTy = mlir::cast<cir::PointerType>(value.getType());
521 auto srcComplexTy = mlir::cast<cir::ComplexType>(srcPtrTy.getPointee());
522 return cir::ComplexImagPtrOp::create(
523 *this, loc, getPointerTo(srcComplexTy.getElementType()), value);
524 }
525
526 Address createComplexImagPtr(mlir::Location loc, Address addr) {
527 return Address{createComplexImagPtr(loc, addr.getPointer()),
528 addr.getAlignment()};
529 }
530
531 /// Create a cir.ptr_stride operation to get access to an array element.
532 /// \p idx is the index of the element to access, \p shouldDecay is true if
533 /// the result should decay to a pointer to the element type.
534 mlir::Value getArrayElement(mlir::Location arrayLocBegin,
535 mlir::Location arrayLocEnd, mlir::Value arrayPtr,
536 mlir::Type eltTy, mlir::Value idx,
537 bool shouldDecay);
538
539 /// Returns a decayed pointer to the first element of the array
540 /// pointed to by \p arrayPtr.
541 mlir::Value maybeBuildArrayDecay(mlir::Location loc, mlir::Value arrayPtr,
542 mlir::Type eltTy);
543
544 // Convert byte offset to sequence of high-level indices suitable for
545 // GlobalViewAttr. Ideally we shouldn't deal with low-level offsets at all
546 // but currently some parts of Clang AST, which we don't want to touch just
547 // yet, return them.
549 int64_t offset, mlir::Type ty, cir::CIRDataLayout layout,
551
552 /// Creates a versioned global variable. If the symbol is already taken, an ID
553 /// will be appended to the symbol. The returned global must always be queried
554 /// for its name so it can be referenced correctly.
555 [[nodiscard]] cir::GlobalOp
556 createVersionedGlobal(mlir::ModuleOp module, mlir::Location loc,
557 mlir::StringRef name, mlir::Type type, bool isConstant,
558 cir::GlobalLinkageKind linkage) {
559 // Create a unique name if the given name is already taken.
560 std::string uniqueName;
561 if (unsigned version = globalsVersioning[name.str()]++)
562 uniqueName = name.str() + "." + std::to_string(version);
563 else
564 uniqueName = name.str();
565
566 return createGlobal(module, loc, uniqueName, type, isConstant, linkage);
567 }
568
569 cir::StackSaveOp createStackSave(mlir::Location loc, mlir::Type ty) {
570 return cir::StackSaveOp::create(*this, loc, ty);
571 }
572
573 cir::StackRestoreOp createStackRestore(mlir::Location loc, mlir::Value v) {
574 return cir::StackRestoreOp::create(*this, loc, v);
575 }
576
577 mlir::Value createSetBitfield(mlir::Location loc, mlir::Type resultType,
578 Address dstAddr, mlir::Type storageType,
579 mlir::Value src, const CIRGenBitFieldInfo &info,
580 bool isLvalueVolatile, bool useVolatile) {
581 unsigned offset = useVolatile ? info.volatileOffset : info.offset;
582
583 // If using AAPCS and the field is volatile, load with the size of the
584 // declared field
585 storageType =
586 useVolatile ? cir::IntType::get(storageType.getContext(),
587 info.volatileStorageSize, info.isSigned)
588 : storageType;
589 return cir::SetBitfieldOp::create(
590 *this, loc, resultType, dstAddr.getPointer(), storageType, src,
591 info.name, info.size, offset, info.isSigned, isLvalueVolatile,
592 dstAddr.getAlignment().getAsAlign().value());
593 }
594
595 mlir::Value createGetBitfield(mlir::Location loc, mlir::Type resultType,
596 Address addr, mlir::Type storageType,
597 const CIRGenBitFieldInfo &info,
598 bool isLvalueVolatile, bool useVolatile) {
599 unsigned offset = useVolatile ? info.volatileOffset : info.offset;
600
601 // If using AAPCS and the field is volatile, load with the size of the
602 // declared field
603 storageType =
604 useVolatile ? cir::IntType::get(storageType.getContext(),
605 info.volatileStorageSize, info.isSigned)
606 : storageType;
607 return cir::GetBitfieldOp::create(*this, loc, resultType, addr.getPointer(),
608 storageType, info.name, info.size, offset,
609 info.isSigned, isLvalueVolatile,
610 addr.getAlignment().getAsAlign().value());
611 }
612
613 cir::VecShuffleOp
614 createVecShuffle(mlir::Location loc, mlir::Value vec1, mlir::Value vec2,
616 auto vecType = mlir::cast<cir::VectorType>(vec1.getType());
617 auto resultTy = cir::VectorType::get(getContext(), vecType.getElementType(),
618 maskAttrs.size());
619 return cir::VecShuffleOp::create(*this, loc, resultTy, vec1, vec2,
620 getArrayAttr(maskAttrs));
621 }
622
623 cir::VecShuffleOp createVecShuffle(mlir::Location loc, mlir::Value vec1,
624 mlir::Value vec2,
626 auto maskAttrs = llvm::to_vector_of<mlir::Attribute>(
627 llvm::map_range(mask, [&](int32_t idx) {
628 return cir::IntAttr::get(getSInt32Ty(), idx);
629 }));
630 return createVecShuffle(loc, vec1, vec2, maskAttrs);
631 }
632
633 cir::VecShuffleOp createVecShuffle(mlir::Location loc, mlir::Value vec1,
635 /// Create a unary shuffle. The second vector operand of the IR instruction
636 /// is poison.
637 cir::ConstantOp poison =
638 getConstant(loc, cir::PoisonAttr::get(vec1.getType()));
639 return createVecShuffle(loc, vec1, poison, mask);
640 }
641};
642
643} // namespace clang::CIRGen
644
645#endif
TokenType getType() const
Returns the token's type, e.g.
__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
Definition Address.h:90
mlir::Type getElementType() const
Definition Address.h:117
clang::CharUnits getAlignment() const
Definition Address.h:130
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::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)
mlir::Value createFDiv(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
Address createDerivedClassAddr(mlir::Location loc, Address addr, mlir::Type destType, unsigned offset, bool assumeNotNull)
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...
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)
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
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)
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)
void computeGlobalViewIndicesFromFlatOffset(int64_t offset, mlir::Type ty, cir::CIRDataLayout layout, llvm::SmallVectorImpl< int64_t > &indices)
Address createComplexImagPtr(mlir::Location loc, Address addr)
mlir::Value createFMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
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::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.
cir::StoreOp createStore(mlir::Location loc, mlir::Value val, Address dst, bool isVolatile=false, mlir::IntegerAttr align={}, cir::MemOrderAttr order={})
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)
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
Definition CharUnits.h:189
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition CharUnits.h:58
Represents a struct/union/class.
Definition Decl.h:4321
TagKind getTagKind() const
Definition Decl.h:3911
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
TagTypeKind
The kind of a tag type.
Definition TypeBase.h:5878
@ Interface
The "__interface" keyword.
Definition TypeBase.h:5883
@ Struct
The "struct" keyword.
Definition TypeBase.h:5880
@ Class
The "class" keyword.
Definition TypeBase.h:5889
@ Union
The "union" keyword.
Definition TypeBase.h:5886
@ Enum
The "enum" keyword.
Definition TypeBase.h:5892
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.