12#include "mlir/Dialect/OpenACC/OpenACCOpsDialect.h.inc"
13#include "mlir/Dialect/OpenMP/OpenMPOpsDialect.h.inc"
14#include "mlir/IR/PatternMatch.h"
15#include "mlir/Interfaces/DataLayoutInterfaces.h"
16#include "mlir/Pass/Pass.h"
17#include "mlir/Transforms/DialectConversion.h"
27#include "llvm/ADT/ScopeExit.h"
28#include "llvm/ADT/TypeSwitch.h"
34#define GEN_PASS_DEF_CXXABILOWERING
35#include "clang/CIR/Dialect/Passes.h.inc"
42bool isCXXABIAttributeLegal(
const mlir::TypeConverter &tc,
43 mlir::Attribute
attr) {
50 if (isa<mlir::acc::OpenACCDialect, mlir::omp::OpenMPDialect>(
56 if (
isa<mlir::DenseArrayAttr, mlir::FloatAttr, mlir::UnitAttr,
57 mlir::StringAttr, mlir::IntegerAttr, mlir::SymbolRefAttr,
58 cir::AnnotationAttr>(
attr))
64#include
"clang/CIR/Dialect/IR/CIRLowering.inc"
70 if (isa<cir::DataMemberAttr, cir::DataMemberOffsetAttr, cir::MethodAttr>(
74 return llvm::TypeSwitch<mlir::Attribute, bool>(
attr)
77 [&tc](cir::ZeroAttr za) {
return tc.isLegal(za.getType()); })
78 .Case<cir::PoisonAttr>(
79 [&tc](cir::PoisonAttr pa) {
return tc.isLegal(pa.getType()); })
80 .Case<cir::UndefAttr>(
81 [&tc](cir::UndefAttr uda) {
return tc.isLegal(uda.getType()); })
82 .Case<mlir::TypeAttr>(
83 [&tc](mlir::TypeAttr ta) {
return tc.isLegal(ta.getValue()); })
84 .Case<cir::ConstPtrAttr>(
85 [&tc](cir::ConstPtrAttr cpa) {
return tc.isLegal(cpa.getType()); })
86 .Case<cir::CXXCtorAttr>(
87 [&tc](cir::CXXCtorAttr ca) {
return tc.isLegal(ca.getType()); })
88 .Case<cir::CXXDtorAttr>(
89 [&tc](cir::CXXDtorAttr da) {
return tc.isLegal(da.getType()); })
90 .Case<cir::CXXAssignAttr>(
91 [&tc](cir::CXXAssignAttr aa) {
return tc.isLegal(aa.getType()); })
95 .Case<mlir::ArrayAttr>([&tc](mlir::ArrayAttr array) {
96 return llvm::all_of(array.getValue(), [&tc](mlir::Attribute
attr) {
97 return isCXXABIAttributeLegal(tc, attr);
100 .Case<mlir::DictionaryAttr>([&tc](mlir::DictionaryAttr dict) {
101 return llvm::all_of(dict.getValue(), [&tc](mlir::NamedAttribute na) {
102 return isCXXABIAttributeLegal(tc, na.getValue());
106 .Case<cir::ConstArrayAttr>([&tc](cir::ConstArrayAttr array) {
107 return tc.isLegal(array.getType()) &&
108 isCXXABIAttributeLegal(tc, array.getElts());
110 .Case<cir::GlobalViewAttr>([&tc](cir::GlobalViewAttr gva) {
111 return tc.isLegal(gva.getType()) &&
112 isCXXABIAttributeLegal(tc, gva.getIndices());
114 .Case<cir::GlobalOffsetAttr>([&tc](cir::GlobalOffsetAttr goa) {
115 return tc.isLegal(goa.getType());
117 .Case<cir::VTableAttr>([&tc](cir::VTableAttr vta) {
118 return tc.isLegal(vta.getType()) &&
119 isCXXABIAttributeLegal(tc, vta.getData());
121 .Case<cir::TypeInfoAttr>([&tc](cir::TypeInfoAttr tia) {
122 return tc.isLegal(tia.getType()) &&
123 isCXXABIAttributeLegal(tc, tia.getData());
125 .Case<cir::DynamicCastInfoAttr>([&tc](cir::DynamicCastInfoAttr dcia) {
126 return isCXXABIAttributeLegal(tc, dcia.getSrcRtti()) &&
127 isCXXABIAttributeLegal(tc, dcia.getDestRtti()) &&
128 isCXXABIAttributeLegal(tc, dcia.getRuntimeFunc()) &&
129 isCXXABIAttributeLegal(tc, dcia.getBadCastFunc());
131 .Case<cir::ConstRecordAttr>([&tc](cir::ConstRecordAttr cra) {
132 return tc.isLegal(cra.getType()) &&
133 isCXXABIAttributeLegal(tc, cra.getMembers());
144mlir::Attribute rewriteAttribute(
const mlir::TypeConverter &tc,
145 mlir::MLIRContext *ctx, mlir::Attribute attr) {
148 if (isCXXABIAttributeLegal(tc, attr))
156 return llvm::TypeSwitch<mlir::Attribute, mlir::Attribute>(attr)
158 .Case<cir::ZeroAttr>([&tc](cir::ZeroAttr za) {
159 return cir::ZeroAttr::get(tc.convertType(za.getType()));
161 .Case<cir::PoisonAttr>([&tc](cir::PoisonAttr pa) {
162 return cir::PoisonAttr::get(tc.convertType(pa.getType()));
164 .Case<cir::UndefAttr>([&tc](cir::UndefAttr uda) {
165 return cir::UndefAttr::get(tc.convertType(uda.getType()));
167 .Case<mlir::TypeAttr>([&tc](mlir::TypeAttr ta) {
168 return mlir::TypeAttr::get(tc.convertType(ta.getValue()));
170 .Case<cir::ConstPtrAttr>([&tc](cir::ConstPtrAttr cpa) {
171 return cir::ConstPtrAttr::get(tc.convertType(cpa.getType()),
174 .Case<cir::CXXCtorAttr>([&tc](cir::CXXCtorAttr ca) {
175 return cir::CXXCtorAttr::get(tc.convertType(ca.getType()),
176 ca.getCtorKind(), ca.getIsTrivial());
178 .Case<cir::CXXDtorAttr>([&tc](cir::CXXDtorAttr da) {
179 return cir::CXXDtorAttr::get(tc.convertType(da.getType()),
182 .Case<cir::CXXAssignAttr>([&tc](cir::CXXAssignAttr aa) {
183 return cir::CXXAssignAttr::get(tc.convertType(aa.getType()),
184 aa.getAssignKind(), aa.getIsTrivial());
188 .Case<mlir::ArrayAttr>([&tc, ctx](mlir::ArrayAttr array) {
190 for (mlir::Attribute a : array.getValue())
191 elts.push_back(rewriteAttribute(tc, ctx, a));
192 return mlir::ArrayAttr::get(ctx, elts);
194 .Case<mlir::DictionaryAttr>([&tc, ctx](mlir::DictionaryAttr dict) {
196 for (mlir::NamedAttribute na : dict.getValue())
197 elts.emplace_back(na.getName(),
198 rewriteAttribute(tc, ctx, na.getValue()));
200 return mlir::DictionaryAttr::get(ctx, elts);
203 .Case<cir::ConstArrayAttr>([&tc, ctx](cir::ConstArrayAttr array) {
204 return cir::ConstArrayAttr::get(
205 ctx, tc.convertType(array.getType()),
206 rewriteAttribute(tc, ctx, array.getElts()),
207 array.getTrailingZerosNum());
209 .Case<cir::GlobalViewAttr>([&tc, ctx](cir::GlobalViewAttr gva) {
210 return cir::GlobalViewAttr::get(
211 tc.convertType(gva.getType()), gva.getSymbol(),
212 mlir::cast<mlir::ArrayAttr>(
213 rewriteAttribute(tc, ctx, gva.getIndices())));
215 .Case<cir::GlobalOffsetAttr>([&tc](cir::GlobalOffsetAttr goa) {
216 return cir::GlobalOffsetAttr::get(tc.convertType(goa.getType()),
217 goa.getSymbol(), goa.getOffset());
219 .Case<cir::VTableAttr>([&tc, ctx](cir::VTableAttr vta) {
220 return cir::VTableAttr::get(
221 tc.convertType(vta.getType()),
222 mlir::cast<mlir::ArrayAttr>(
223 rewriteAttribute(tc, ctx, vta.getData())));
225 .Case<cir::TypeInfoAttr>([&tc, ctx](cir::TypeInfoAttr tia) {
226 return cir::TypeInfoAttr::get(
227 tc.convertType(tia.getType()),
228 mlir::cast<mlir::ArrayAttr>(
229 rewriteAttribute(tc, ctx, tia.getData())));
231 .Case<cir::DynamicCastInfoAttr>([&tc,
232 ctx](cir::DynamicCastInfoAttr dcia) {
233 return cir::DynamicCastInfoAttr::get(
234 mlir::cast<cir::GlobalViewAttr>(
235 rewriteAttribute(tc, ctx, dcia.getSrcRtti())),
236 mlir::cast<cir::GlobalViewAttr>(
237 rewriteAttribute(tc, ctx, dcia.getDestRtti())),
238 dcia.getRuntimeFunc(), dcia.getBadCastFunc(), dcia.getOffsetHint());
240 .Case<cir::ConstRecordAttr>([&tc, ctx](cir::ConstRecordAttr cra) {
241 return cir::ConstRecordAttr::get(
242 ctx, tc.convertType(cra.getType()),
243 mlir::cast<mlir::ArrayAttr>(
244 rewriteAttribute(tc, ctx, cra.getMembers())));
246 .DefaultUnreachable(
"unrewritten illegal attribute kind");
249bool areCXXABIInherentAttrsLegal(mlir::Operation *op,
250 const mlir::TypeConverter &typeConverter) {
252 op->getName().walkInherentAttrs(
253 op, [&](llvm::StringRef, mlir::Attribute &attr) {
254 legal &= isCXXABIAttributeLegal(typeConverter, attr);
259#define GET_ABI_LOWERING_PATTERNS
260#include "clang/CIR/Dialect/IR/CIRLowering.inc"
261#undef GET_ABI_LOWERING_PATTERNS
263struct CXXABILoweringPass
264 :
public impl::CXXABILoweringBase<CXXABILoweringPass> {
265 CXXABILoweringPass() =
default;
266 void runOnOperation()
override;
274class CIRGenericCXXABILoweringPattern :
public mlir::ConversionPattern {
276 CIRGenericCXXABILoweringPattern(mlir::MLIRContext *context,
277 const mlir::TypeConverter &typeConverter)
278 : mlir::ConversionPattern(typeConverter, MatchAnyOpTypeTag(),
282 matchAndRewrite(mlir::Operation *op, llvm::ArrayRef<mlir::Value> operands,
283 mlir::ConversionPatternRewriter &rewriter)
const override {
285 if (llvm::isa<cir::AllocaOp, cir::BaseDataMemberOp, cir::BaseMethodOp,
286 cir::CastOp, cir::CmpOp, cir::ConstantOp, cir::DeleteArrayOp,
287 cir::DerivedDataMemberOp, cir::DerivedMethodOp, cir::FuncOp,
288 cir::GetMethodOp, cir::GetRuntimeMemberOp, cir::GlobalOp>(op))
289 return mlir::failure();
291 const mlir::TypeConverter *typeConverter = getTypeConverter();
292 assert(typeConverter &&
293 "CIRGenericCXXABILoweringPattern requires a type converter");
294 bool operandsAndResultsLegal = typeConverter->isLegal(op);
296 std::all_of(op->getRegions().begin(), op->getRegions().end(),
297 [typeConverter](mlir::Region ®ion) {
298 return typeConverter->isLegal(®ion);
300 bool attrsLegal = areCXXABIInherentAttrsLegal(op, *typeConverter);
302 if (operandsAndResultsLegal && regionsLegal && attrsLegal) {
305 return mlir::failure();
308 mlir::OperationState loweredOpState(op->getLoc(), op->getName());
309 loweredOpState.addOperands(operands);
310 loweredOpState.addSuccessors(op->getSuccessors());
313 loweredOpState.propertiesAttr = op->getPropertiesAsAttribute();
314 loweredOpState.addAttributes(op->getDiscardableAttrDictionary().getValue());
317 llvm::SmallVector<mlir::Type> loweredResultTypes;
318 loweredResultTypes.reserve(op->getNumResults());
319 for (mlir::Type result : op->getResultTypes())
320 loweredResultTypes.push_back(typeConverter->convertType(result));
321 loweredOpState.addTypes(loweredResultTypes);
324 for (mlir::Region ®ion : op->getRegions()) {
325 mlir::Region *loweredRegion = loweredOpState.addRegion();
326 rewriter.inlineRegionBefore(region, *loweredRegion, loweredRegion->end());
328 rewriter.convertRegionTypes(loweredRegion, *getTypeConverter())))
329 return mlir::failure();
333 mlir::Operation *loweredOp = rewriter.create(loweredOpState);
334 loweredOp->getName().walkInherentAttrs(
335 loweredOp, [&](llvm::StringRef, mlir::Attribute &attr) {
336 attr = rewriteAttribute(*typeConverter, op->getContext(), attr);
339 rewriter.replaceOp(op, loweredOp);
340 return mlir::success();
346mlir::LogicalResult CIRAllocaOpABILowering::matchAndRewrite(
347 cir::AllocaOp op, OpAdaptor adaptor,
348 mlir::ConversionPatternRewriter &rewriter)
const {
349 mlir::Type allocaPtrTy = op.getType();
350 mlir::Type loweredAllocaPtrTy = getTypeConverter()->convertType(allocaPtrTy);
352 cir::AllocaOp loweredOp = cir::AllocaOp::create(
353 rewriter, op.getLoc(), loweredAllocaPtrTy, op.getName(),
354 op.getAlignmentAttr(), adaptor.getDynAllocSize());
355 loweredOp.setInit(op.getInit());
356 loweredOp.setConstant(op.getConstant());
357 loweredOp.setAnnotationsAttr(op.getAnnotationsAttr());
359 rewriter.replaceOp(op, loweredOp);
360 return mlir::success();
363mlir::LogicalResult CIRCastOpABILowering::matchAndRewrite(
364 cir::CastOp op, OpAdaptor adaptor,
365 mlir::ConversionPatternRewriter &rewriter)
const {
366 mlir::Type srcTy = op.getSrc().getType();
368 if (mlir::isa<cir::DataMemberType, cir::MethodType>(srcTy)) {
369 switch (op.getKind()) {
370 case cir::CastKind::bitcast: {
371 mlir::Type destTy = getTypeConverter()->convertType(op.getType());
372 mlir::Value loweredResult;
373 if (mlir::isa<cir::DataMemberType>(srcTy))
374 loweredResult = lowerModule->getCXXABI().lowerDataMemberBitcast(
375 op, destTy, adaptor.getSrc(), rewriter);
377 loweredResult = lowerModule->getCXXABI().lowerMethodBitcast(
378 op, destTy, adaptor.getSrc(), rewriter);
379 rewriter.replaceOp(op, loweredResult);
380 return mlir::success();
382 case cir::CastKind::member_ptr_to_bool: {
383 mlir::Value loweredResult;
384 if (mlir::isa<cir::DataMemberType>(srcTy))
385 loweredResult = lowerModule->getCXXABI().lowerDataMemberToBoolCast(
386 op, adaptor.getSrc(), rewriter);
388 loweredResult = lowerModule->getCXXABI().lowerMethodToBoolCast(
389 op, adaptor.getSrc(), rewriter);
390 rewriter.replaceOp(op, loweredResult);
391 return mlir::success();
398 mlir::Value loweredResult = cir::CastOp::create(
399 rewriter, op.getLoc(), getTypeConverter()->convertType(op.getType()),
400 adaptor.getKind(), adaptor.getSrc());
401 rewriter.replaceOp(op, loweredResult);
402 return mlir::success();
407 const mlir::DataLayout &layout,
408 const mlir::TypeConverter &tc,
410 mlir::Attribute initVal) {
411 if (mlir::isa<cir::DataMemberType>(ty)) {
415 mlir::dyn_cast_if_present<cir::DataMemberOffsetAttr>(initVal))
418 auto dataMemberVal = mlir::cast_if_present<cir::DataMemberAttr>(initVal);
422 if (mlir::isa<cir::MethodType>(ty)) {
423 auto methodVal = mlir::cast_if_present<cir::MethodAttr>(initVal);
427 if (
auto arrTy = mlir::dyn_cast<cir::ArrayType>(ty)) {
428 auto loweredArrTy = mlir::cast<cir::ArrayType>(tc.convertType(arrTy));
433 if (
auto zeroVal = mlir::dyn_cast_if_present<cir::ZeroAttr>(initVal))
434 return cir::ZeroAttr::get(loweredArrTy);
436 auto arrayVal = mlir::cast<cir::ConstArrayAttr>(initVal);
442 if (mlir::isa<mlir::StringAttr>(arrayVal.getElts())) {
443 assert(loweredArrTy == arrTy &&
444 "string-literal array type should not change under CXX ABI");
448 auto arrayElts = mlir::cast<ArrayAttr>(arrayVal.getElts());
450 loweredElements.reserve(arrTy.getSize());
451 for (
const mlir::Attribute &attr : arrayElts) {
452 auto typedAttr = cast<mlir::TypedAttr>(attr);
454 lowerModule, layout, tc, typedAttr.getType(), typedAttr));
457 return cir::ConstArrayAttr::get(
458 loweredArrTy, mlir::ArrayAttr::get(ty.getContext(), loweredElements),
459 arrayVal.getTrailingZerosNum());
462 if (
auto recordTy = mlir::dyn_cast<cir::RecordType>(ty)) {
464 mlir::dyn_cast<cir::RecordType>(tc.convertType(recordTy));
468 if (
auto recVal = mlir::dyn_cast_if_present<cir::ZeroAttr>(initVal))
469 return cir::ZeroAttr::get(convertedTy);
471 if (
auto undefVal = mlir::dyn_cast_if_present<cir::UndefAttr>(initVal))
472 return cir::UndefAttr::get(convertedTy);
476 if (
auto poisonVal = mlir::dyn_cast_if_present<cir::PoisonAttr>(initVal))
477 return cir::PoisonAttr::get(convertedTy);
480 mlir::dyn_cast_if_present<cir::ConstRecordAttr>(initVal)) {
481 auto recordMembers = mlir::cast<ArrayAttr>(recVal.getMembers());
484 loweredMembers.reserve(recordMembers.size());
486 for (
const mlir::Attribute &attr : recordMembers) {
487 auto typedAttr = cast<mlir::TypedAttr>(attr);
489 lowerModule, layout, tc, typedAttr.getType(), typedAttr));
492 return cir::ConstRecordAttr::get(
493 convertedTy, mlir::ArrayAttr::get(ty.getContext(), loweredMembers));
496 assert(!initVal &&
"Record init val type not handled");
501 if (
auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty)) {
502 auto convertedTy = mlir::cast<cir::PointerType>(tc.convertType(ptrTy));
505 if (
auto gva = mlir::dyn_cast_if_present<cir::GlobalViewAttr>(initVal))
506 return cir::GlobalViewAttr::get(convertedTy, gva.getSymbol(),
509 if (
auto goa = mlir::dyn_cast_if_present<cir::GlobalOffsetAttr>(initVal))
510 return cir::GlobalOffsetAttr::get(convertedTy, goa.getSymbol(),
514 mlir::dyn_cast_if_present<cir::BlockAddrInfoAttr>(initVal)) {
515 assert(convertedTy == ptrTy &&
"BlockAddrInfo type should not change");
519 auto constPtr = mlir::cast_if_present<cir::ConstPtrAttr>(initVal);
522 return cir::ConstPtrAttr::get(convertedTy, constPtr.getValue());
525 assert(ty == tc.convertType(ty) &&
526 "cir.global or constant operand is not an CXXABI-dependent type");
529 return cast<mlir::TypedAttr>(initVal);
532mlir::LogicalResult CIRConstantOpABILowering::matchAndRewrite(
533 cir::ConstantOp op, OpAdaptor adaptor,
534 mlir::ConversionPatternRewriter &rewriter)
const {
536 mlir::DataLayout layout(op->getParentOfType<mlir::ModuleOp>());
538 lowerModule, layout, *getTypeConverter(), op.getType(), op.getValue());
539 rewriter.replaceOpWithNewOp<ConstantOp>(op, newValue);
540 return mlir::success();
543mlir::LogicalResult CIRCmpOpABILowering::matchAndRewrite(
544 cir::CmpOp op, OpAdaptor adaptor,
545 mlir::ConversionPatternRewriter &rewriter)
const {
546 mlir::Type type = op.getLhs().getType();
548 mlir::Value loweredResult;
549 if (mlir::isa<cir::DataMemberType>(type))
550 loweredResult = lowerModule->getCXXABI().lowerDataMemberCmp(
551 op, adaptor.getLhs(), adaptor.getRhs(), rewriter);
552 else if (mlir::isa<cir::MethodType>(type))
553 loweredResult = lowerModule->getCXXABI().lowerMethodCmp(
554 op, adaptor.getLhs(), adaptor.getRhs(), rewriter);
556 loweredResult = cir::CmpOp::create(
557 rewriter, op.getLoc(), getTypeConverter()->convertType(op.getType()),
558 adaptor.getKind(), adaptor.getLhs(), adaptor.getRhs());
560 rewriter.replaceOp(op, loweredResult);
561 return mlir::success();
564mlir::LogicalResult CIRFuncOpABILowering::matchAndRewrite(
565 cir::FuncOp op, OpAdaptor adaptor,
566 mlir::ConversionPatternRewriter &rewriter)
const {
567 cir::FuncType opFuncType = op.getFunctionType();
568 mlir::TypeConverter::SignatureConversion signatureConversion(
569 opFuncType.getNumInputs());
571 for (
const auto &[i, argType] : llvm::enumerate(opFuncType.getInputs())) {
572 mlir::Type loweredArgType = getTypeConverter()->convertType(argType);
574 return mlir::failure();
575 signatureConversion.addInputs(i, loweredArgType);
578 mlir::Type loweredResultType =
579 getTypeConverter()->convertType(opFuncType.getReturnType());
580 if (!loweredResultType)
581 return mlir::failure();
583 auto loweredFuncType =
584 cir::FuncType::get(signatureConversion.getConvertedTypes(),
585 loweredResultType, opFuncType.isVarArg());
588 cir::FuncOp loweredFuncOp = rewriter.cloneWithoutRegions(op);
589 loweredFuncOp.setFunctionType(loweredFuncType);
591 loweredFuncOp->getName().walkInherentAttrs(
592 loweredFuncOp, [&](llvm::StringRef, mlir::Attribute &attr) {
593 attr = rewriteAttribute(*getTypeConverter(), op->getContext(), attr);
595 rewriter.inlineRegionBefore(op.getBody(), loweredFuncOp.getBody(),
596 loweredFuncOp.end());
597 if (mlir::failed(rewriter.convertRegionTypes(
598 &loweredFuncOp.getBody(), *getTypeConverter(), &signatureConversion)))
599 return mlir::failure();
601 rewriter.eraseOp(op);
602 return mlir::success();
605mlir::LogicalResult CIRGlobalOpABILowering::matchAndRewrite(
606 cir::GlobalOp op, OpAdaptor adaptor,
607 mlir::ConversionPatternRewriter &rewriter)
const {
608 mlir::Type ty = op.getSymType();
609 mlir::Type loweredTy = getTypeConverter()->convertType(ty);
611 return mlir::failure();
613 mlir::DataLayout layout(op->getParentOfType<mlir::ModuleOp>());
616 lowerModule, layout, *getTypeConverter(), ty, op.getInitialValueAttr());
618 auto newOp = mlir::cast<cir::GlobalOp>(rewriter.clone(*op.getOperation()));
619 newOp.setInitialValueAttr(loweredInit);
620 newOp.setSymType(loweredTy);
621 rewriter.replaceOp(op, newOp);
622 return mlir::success();
625mlir::LogicalResult CIRBaseDataMemberOpABILowering::matchAndRewrite(
626 cir::BaseDataMemberOp op, OpAdaptor adaptor,
627 mlir::ConversionPatternRewriter &rewriter)
const {
628 mlir::Value loweredResult = lowerModule->getCXXABI().lowerBaseDataMember(
629 op, adaptor.getSrc(), rewriter);
630 rewriter.replaceOp(op, loweredResult);
631 return mlir::success();
634mlir::LogicalResult CIRBaseMethodOpABILowering::matchAndRewrite(
635 cir::BaseMethodOp op, OpAdaptor adaptor,
636 mlir::ConversionPatternRewriter &rewriter)
const {
637 mlir::Value loweredResult =
638 lowerModule->getCXXABI().lowerBaseMethod(op, adaptor.getSrc(), rewriter);
639 rewriter.replaceOp(op, loweredResult);
640 return mlir::success();
643mlir::LogicalResult CIRDeleteArrayOpABILowering::matchAndRewrite(
644 cir::DeleteArrayOp op, OpAdaptor adaptor,
645 mlir::ConversionPatternRewriter &rewriter)
const {
646 mlir::FlatSymbolRefAttr deleteFn = op.getDeleteFnAttr();
647 mlir::Location loc = op->getLoc();
648 mlir::Value loweredAddress = adaptor.getAddress();
650 cir::UsualDeleteParamsAttr deleteParams = op.getDeleteParamsAttr();
652 deleteParams = cir::UsualDeleteParamsAttr::get(op.getContext(),
false,
653 std::nullopt,
false,
false);
654 bool cookieRequired = deleteParams.getSize() || op.getElementDtorAttr();
656 assert(!deleteParams.getDestroyingDelete() &&
657 "destroying delete not legal on arrays");
658 assert(!deleteParams.getTypeAwareDelete() &&
659 "type-aware delete not legal on arrays");
661 const CIRCXXABI &cxxABI = lowerModule->getCXXABI();
670 mlir::Value deletePtr;
671 mlir::Value numElements;
672 cir::PointerType ptrTy;
674 mlir::DataLayout dl(op->getParentOfType<mlir::ModuleOp>());
677 cir::IntType sizeTy = cirBuilder.getUIntNTy(ptrWidth);
679 if (cookieRequired) {
680 ptrTy = mlir::cast<cir::PointerType>(loweredAddress.getType());
683 cxxABI.
readArrayCookie(loc, loweredAddress, elementAlign, dl, cirBuilder,
684 numElements, deletePtr, cookieSize);
686 deletePtr = cir::CastOp::create(rewriter, loc, cirBuilder.getVoidPtrTy(),
687 cir::CastKind::bitcast, loweredAddress);
697 mlir::FlatSymbolRefAttr dtorFn = op.getElementDtorAttr();
698 cir::CleanupKind cleanupKind =
699 op.getDtorMayThrow() ? cir::CleanupKind::All : cir::CleanupKind::Normal;
700 cir::CleanupScopeOp::create(
701 rewriter, loc, cleanupKind,
703 [&](mlir::OpBuilder &b, mlir::Location l) {
705 auto eltPtrTy = cir::PointerType::get(ptrTy.getPointee());
706 auto arrayDtor = cir::ArrayDtor::create(
707 b, l, loweredAddress, numElements,
708 [&](mlir::OpBuilder &bb, mlir::Location ll) {
710 bb.getInsertionBlock()->addArgument(eltPtrTy, ll);
711 auto dtorCall = cir::CallOp::create(
712 bb, ll, dtorFn, cir::VoidType(), mlir::ValueRange{
arg});
713 if (!op.getDtorMayThrow())
714 dtorCall.setNothrowAttr(bb.getUnitAttr());
715 cir::YieldOp::create(bb, ll);
717 if (op.getDtorMayThrow())
718 arrayDtor.setDtorMayThrow(
true);
720 cir::YieldOp::create(b, l);
723 [&](mlir::OpBuilder &b, mlir::Location l) {
726 if (deleteParams.getSize()) {
727 uint64_t eltSizeBytes = dl.getTypeSizeInBits(ptrTy.getPointee()) / 8;
728 auto eltSizeVal = cir::ConstantOp::create(
729 b, l, cir::IntAttr::get(sizeTy, eltSizeBytes));
730 mlir::Value allocSize =
731 cir::MulOp::create(b, l, sizeTy, eltSizeVal, numElements);
732 auto cookieSizeVal = cir::ConstantOp::create(
733 b, l, cir::IntAttr::get(sizeTy, cookieSize.
getQuantity()));
735 cir::AddOp::create(b, l, sizeTy, allocSize, cookieSizeVal);
738 if (deleteParams.getAlignment()) {
739 auto alignVal = cir::ConstantOp::create(
740 b, l, cir::IntAttr::get(sizeTy, *deleteParams.getAlignment()));
745 cir::CallOp::create(b, l, deleteFn, cir::VoidType(), callArgs);
748 deleteCall.setNothrowAttr(b.getUnitAttr());
749 cir::YieldOp::create(b, l);
752 rewriter.eraseOp(op);
753 return mlir::success();
756mlir::LogicalResult CIRDerivedDataMemberOpABILowering::matchAndRewrite(
757 cir::DerivedDataMemberOp op, OpAdaptor adaptor,
758 mlir::ConversionPatternRewriter &rewriter)
const {
759 mlir::Value loweredResult = lowerModule->getCXXABI().lowerDerivedDataMember(
760 op, adaptor.getSrc(), rewriter);
761 rewriter.replaceOp(op, loweredResult);
762 return mlir::success();
765mlir::LogicalResult CIRDerivedMethodOpABILowering::matchAndRewrite(
766 cir::DerivedMethodOp op, OpAdaptor adaptor,
767 mlir::ConversionPatternRewriter &rewriter)
const {
768 mlir::Value loweredResult = lowerModule->getCXXABI().lowerDerivedMethod(
769 op, adaptor.getSrc(), rewriter);
770 rewriter.replaceOp(op, loweredResult);
771 return mlir::success();
774mlir::LogicalResult CIRDynamicCastOpABILowering::matchAndRewrite(
775 cir::DynamicCastOp op, OpAdaptor adaptor,
776 mlir::ConversionPatternRewriter &rewriter)
const {
777 mlir::Value loweredResult =
778 lowerModule->getCXXABI().lowerDynamicCast(op, rewriter);
779 rewriter.replaceOp(op, loweredResult);
780 return mlir::success();
783mlir::LogicalResult CIRGetMethodOpABILowering::matchAndRewrite(
784 cir::GetMethodOp op, OpAdaptor adaptor,
785 mlir::ConversionPatternRewriter &rewriter)
const {
788 lowerModule->getCXXABI().lowerGetMethod(
789 op, callee, thisArg, adaptor.getMethod(), adaptor.getObject(), rewriter);
790 rewriter.replaceOp(op, {callee, thisArg});
791 return mlir::success();
794mlir::LogicalResult CIRGetRuntimeMemberOpABILowering::matchAndRewrite(
795 cir::GetRuntimeMemberOp op, OpAdaptor adaptor,
796 mlir::ConversionPatternRewriter &rewriter)
const {
797 mlir::Type resTy = getTypeConverter()->convertType(op.getType());
798 mlir::Operation *newOp = lowerModule->getCXXABI().lowerGetRuntimeMember(
799 op, resTy, adaptor.getAddr(), adaptor.getMember(), rewriter);
800 rewriter.replaceOp(op, newOp);
801 return mlir::success();
804mlir::LogicalResult CIRVTableGetTypeInfoOpABILowering::matchAndRewrite(
805 cir::VTableGetTypeInfoOp op, OpAdaptor adaptor,
806 mlir::ConversionPatternRewriter &rewriter)
const {
807 mlir::Value loweredResult =
808 lowerModule->getCXXABI().lowerVTableGetTypeInfo(op, rewriter);
809 rewriter.replaceOp(op, loweredResult);
810 return mlir::success();
818class CIRABITypeConverter :
public mlir::TypeConverter {
820 mlir::MLIRContext &context;
825 DenseMap<uint64_t, std::unique_ptr<SmallVector<cir::RecordType>>>
827 llvm::sys::SmartRWMutex<true> callStackMutex;
832 llvm::SmallVector<cir::RecordType> convertedRecordTypes;
833 llvm::sys::SmartRWMutex<true> recordTypeMutex;
839 SmallVector<cir::RecordType> &getCurrentThreadRecursiveStack() {
842 std::shared_lock<
decltype(callStackMutex)> lock(callStackMutex,
844 if (context.isMultithreadingEnabled())
846 auto recursiveStack = conversionCallStack.find(llvm::get_threadid());
847 if (recursiveStack != conversionCallStack.end())
848 return *recursiveStack->second;
853 std::unique_lock<
decltype(callStackMutex)> lock(callStackMutex);
854 auto recursiveStackInserted = conversionCallStack.insert(
855 std::make_pair(llvm::get_threadid(),
856 std::make_unique<SmallVector<cir::RecordType>>()));
857 return *recursiveStackInserted.first->second;
860 void addConvertedRecordType(cir::RecordType rt) {
861 std::unique_lock<
decltype(recordTypeMutex)> lock(recordTypeMutex);
862 convertedRecordTypes.push_back(rt);
865 llvm::SmallVector<mlir::Type> convertRecordMemberTypes(cir::RecordType type) {
866 llvm::SmallVector<mlir::Type> loweredMemberTypes;
867 loweredMemberTypes.reserve(
type.getNumElements());
869 if (mlir::failed(convertTypes(
type.getMembers(), loweredMemberTypes)))
872 return loweredMemberTypes;
875 cir::RecordType convertRecordType(cir::RecordType type) {
879 if (!
type.getName()) {
880 llvm::SmallVector<mlir::Type> converted = convertRecordMemberTypes(type);
881 assert(converted.size() ==
type.getNumElements() &&
882 "member conversion must be one type in, one type out for the "
883 "kinds to carry over by index");
884 if (
auto u = mlir::dyn_cast<cir::UnionType>(type)) {
885 mlir::Type loweredPadding;
886 if (mlir::Type pad = u.getPadding())
887 loweredPadding = convertType(pad);
888 return cir::UnionType::get(
type.getContext(), converted,
889 type.getPacked(), loweredPadding,
892 auto s = mlir::cast<cir::StructType>(type);
893 return cir::StructType::get(
type.getContext(), converted,
894 type.getPacked(), s.getIsClass(),
898 assert(!
type.isIncomplete() ||
type.getMembers().empty());
903 if (
type.isIncomplete() ||
type.isABIConvertedRecord())
906 SmallVectorImpl<cir::RecordType> &recursiveStack =
907 getCurrentThreadRecursiveStack();
909 cir::RecordType convertedType;
910 if (mlir::isa<cir::UnionType>(type))
912 cir::UnionType::get(
type.getContext(),
type.getABIConvertedName());
915 cir::StructType::get(
type.getContext(),
type.getABIConvertedName(),
916 mlir::cast<cir::StructType>(type).getIsClass());
920 return convertedType;
926 if (llvm::is_contained(recursiveStack, type))
927 return convertedType;
929 recursiveStack.push_back(type);
930 llvm::scope_exit popConvertingType(
931 [&recursiveStack]() { recursiveStack.pop_back(); });
933 SmallVector<mlir::Type> convertedMembers = convertRecordMemberTypes(type);
934 assert(convertedMembers.size() ==
type.getNumElements() &&
935 "member conversion must be one type in, one type out for the kinds "
936 "to carry over by index");
938 mlir::Type loweredPadding;
939 if (
auto u = mlir::dyn_cast<cir::UnionType>(type))
940 if (mlir::Type pad = u.getPadding())
941 loweredPadding = convertType(pad);
942 convertedType.
complete(convertedMembers,
type.getPacked(), loweredPadding,
943 type.getMemberKinds());
944 addConvertedRecordType(convertedType);
945 return convertedType;
949 CIRABITypeConverter(mlir::MLIRContext &ctx, mlir::DataLayout &dataLayout,
950 cir::LowerModule &lowerModule)
952 addConversion([&](mlir::Type type) -> mlir::Type {
return type; });
955 addConversion([&](cir::PointerType type) -> mlir::Type {
956 mlir::Type loweredPointeeType = convertType(
type.getPointee());
957 if (!loweredPointeeType)
959 return cir::PointerType::get(
type.getContext(), loweredPointeeType,
960 type.getAddrSpace());
962 addConversion([&](cir::ArrayType type) -> mlir::Type {
963 mlir::Type loweredElementType = convertType(
type.getElementType());
964 if (!loweredElementType)
966 return cir::ArrayType::get(loweredElementType,
type.getSize());
969 addConversion([&](cir::DataMemberType type) -> mlir::Type {
972 return convertType(abiType);
974 addConversion([&](cir::MethodType type) -> mlir::Type {
976 return convertType(abiType);
981 addConversion([&](cir::FuncType type) -> mlir::Type {
982 llvm::SmallVector<mlir::Type> loweredInputTypes;
983 loweredInputTypes.reserve(
type.getNumInputs());
984 if (mlir::failed(convertTypes(
type.getInputs(), loweredInputTypes)))
987 mlir::Type loweredReturnType = convertType(
type.getReturnType());
988 if (!loweredReturnType)
991 return cir::FuncType::get(loweredInputTypes, loweredReturnType,
994 addConversion([&](cir::StructType type) -> mlir::Type {
995 return convertRecordType(type);
997 addConversion([&](cir::UnionType type) -> mlir::Type {
998 return convertRecordType(type);
1002 void restoreRecordTypeNames() {
1003 std::unique_lock<
decltype(recordTypeMutex)> lock(recordTypeMutex);
1005 for (
auto rt : convertedRecordTypes)
1013 const mlir::TypeConverter &typeConverter) {
1014 target.addLegalOp<mlir::ModuleOp>();
1019 target.addDynamicallyLegalDialect<cir::CIRDialect>(
1020 [&typeConverter](mlir::Operation *op) {
1021 if (!typeConverter.isLegal(op))
1024 bool attrs = areCXXABIInherentAttrsLegal(op, typeConverter);
1027 std::all_of(op->getRegions().begin(), op->getRegions().end(),
1028 [&typeConverter](mlir::Region ®ion) {
1029 return typeConverter.isLegal(®ion);
1033 target.addDynamicallyLegalDialect<mlir::acc::OpenACCDialect>(
1034 [&typeConverter](mlir::Operation *op) {
1035 if (!typeConverter.isLegal(op))
1038 bool attrs = areCXXABIInherentAttrsLegal(op, typeConverter);
1041 std::all_of(op->getRegions().begin(), op->getRegions().end(),
1042 [&typeConverter](mlir::Region ®ion) {
1043 return typeConverter.isLegal(®ion);
1048 target.addDynamicallyLegalOp<cir::FuncOp>([&typeConverter](cir::FuncOp op) {
1049 bool attrs = areCXXABIInherentAttrsLegal(op, typeConverter);
1051 return attrs && typeConverter.isLegal(op.getFunctionType());
1053 target.addDynamicallyLegalOp<cir::GlobalOp>(
1054 [&typeConverter](cir::GlobalOp op) {
1055 return typeConverter.isLegal(op.getSymType());
1059 target.addIllegalOp<cir::DeleteArrayOp>();
1060 target.addIllegalOp<cir::DynamicCastOp>();
1061 target.addIllegalOp<cir::VTableGetTypeInfoOp>();
1068void CXXABILoweringPass::runOnOperation() {
1069 auto mod = mlir::cast<mlir::ModuleOp>(getOperation());
1070 mlir::MLIRContext *ctx = mod.getContext();
1075 mod.emitWarning(
"Cannot create a CIR lower module, skipping the ")
1080 mlir::DataLayout dataLayout(mod);
1081 CIRABITypeConverter typeConverter(*ctx, dataLayout, *lowerModule);
1083 mlir::RewritePatternSet patterns(ctx);
1084 patterns.add<CIRGenericCXXABILoweringPattern>(patterns.getContext(),
1087#define GET_ABI_LOWERING_PATTERNS_LIST
1088#include "clang/CIR/Dialect/IR/CIRLowering.inc"
1089#undef GET_ABI_LOWERING_PATTERNS_LIST
1090 >(patterns.getContext(), typeConverter, dataLayout, *lowerModule);
1092 mlir::ConversionTarget target(*ctx);
1095 llvm::SmallVector<mlir::Operation *> ops;
1099 if (failed(mlir::applyPartialConversion(ops, target, std::move(patterns))))
1100 signalPassFailure();
1102 typeConverter.restoreRecordTypeNames();
1106 return std::make_unique<CXXABILoweringPass>();
#define CXX_ABI_ALWAYS_LEGAL_ATTRS
static void populateCXXABIConversionTarget(mlir::ConversionTarget &target, const mlir::TypeConverter &typeConverter)
static mlir::TypedAttr lowerInitialValue(const LowerModule *lowerModule, const mlir::DataLayout &layout, const mlir::TypeConverter &tc, mlir::Type ty, mlir::Attribute initVal)
virtual mlir::Type lowerMethodType(cir::MethodType type, const mlir::TypeConverter &typeConverter) const =0
Lower the given member function pointer type to its ABI type.
virtual mlir::TypedAttr lowerDataMemberConstant(cir::DataMemberAttr attr, const mlir::DataLayout &layout, const mlir::TypeConverter &typeConverter) const =0
Lower the given data member pointer constant to a constant of the ABI type.
virtual mlir::TypedAttr lowerDataMemberOffsetConstant(cir::DataMemberOffsetAttr attr, const mlir::DataLayout &layout, const mlir::TypeConverter &typeConverter) const =0
Lower the given by-offset data member pointer constant (used for members with no CIR field index,...
virtual mlir::TypedAttr lowerMethodConstant(cir::MethodAttr attr, const mlir::DataLayout &layout, const mlir::TypeConverter &typeConverter) const =0
Lower the given member function pointer constant to a constant of the ABI type.
virtual mlir::Type lowerDataMemberType(cir::DataMemberType type, const mlir::TypeConverter &typeConverter) const =0
Lower the given data member pointer type to its ABI type.
void readArrayCookie(mlir::Location loc, mlir::Value elementPtr, clang::CharUnits elementAlign, const mlir::DataLayout &dataLayout, CIRBaseBuilderTy &builder, mlir::Value &numElements, mlir::Value &allocPtr, clang::CharUnits &cookieSize) const
Read the array cookie for a dynamically-allocated array whose first element is at elementPtr.
CIRCXXABI & getCXXABI() const
void removeABIConversionNamePrefix()
void complete(llvm::ArrayRef< mlir::Type > members, bool packed, mlir::Type padding, llvm::ArrayRef< RecordMemberKind > memberKinds)
padding is union-only.
This is an opaque type for sizes expressed in character units.
QuantityType getQuantity() const
Get the raw integer representation of this quantity.
static CharUnits fromQuantity(QuantityType Quantity)
Construct a CharUnits quantity from a raw integer type.
void collectUnreachable(mlir::Operation *parent, llvm::SmallVectorImpl< mlir::Operation * > &ops)
Collect ops in blocks that are unreachable from their region's entry, appending them to ops.
std::unique_ptr< LowerModule > createLowerModule(mlir::ModuleOp module)
const internal::VariadicAllOfMatcher< Attr > attr
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
StringRef getName(const HeaderType T)
bool isa(CodeGen::Address addr)
@ Default
Set to the current date and time.
std::unique_ptr< Pass > createCXXABILoweringPass()
__DEVICE__ _Tp arg(const std::complex< _Tp > &__c)