clang 23.0.0git
CXXABILowering.cpp
Go to the documentation of this file.
1//==- CXXABILowering.cpp - lower C++ operations to target-specific ABI form -=//
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#include "PassDetail.h"
11
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"
26
27#include "llvm/ADT/ScopeExit.h"
28#include "llvm/ADT/TypeSwitch.h"
29
30using namespace mlir;
31using namespace cir;
32
33namespace mlir {
34#define GEN_PASS_DEF_CXXABILOWERING
35#include "clang/CIR/Dialect/Passes.h.inc"
36} // namespace mlir
37
38namespace {
39// Check an attribute for legality. An attribute is only currently potentially
40// illegal if it contains a type, member pointers are our source of illegality
41// in regards to attributes.
42bool isCXXABIAttributeLegal(const mlir::TypeConverter &tc,
43 mlir::Attribute attr) {
44 // If we don't have an attribute, it can't have a type!
45 if (!attr)
46 return true;
47
48 // None of the OpenACC/OMP attributes contain a type of concern, so we can
49 // just treat them as legal.
50 if (isa<mlir::acc::OpenACCDialect, mlir::omp::OpenMPDialect>(
51 attr.getDialect()))
52 return true;
53
54 // These attributes either don't contain a type, or don't contain a type that
55 // can have a data member/method.
56 if (isa<mlir::DenseArrayAttr, mlir::FloatAttr, mlir::UnitAttr,
57 mlir::StringAttr, mlir::IntegerAttr, mlir::SymbolRefAttr,
58 cir::AnnotationAttr>(attr))
59 return true;
60
61 // Tablegen'ed always-legal attributes:
62 if (isa<
64#include "clang/CIR/Dialect/IR/CIRLowering.inc"
66 >(attr))
67 return true;
68
69 // Data Member and method are ALWAYS illegal.
70 if (isa<cir::DataMemberAttr, cir::MethodAttr>(attr))
71 return false;
72
73 return llvm::TypeSwitch<mlir::Attribute, bool>(attr)
74 // These attributes just have a type, so they are legal if their type is.
75 .Case<cir::ZeroAttr>(
76 [&tc](cir::ZeroAttr za) { return tc.isLegal(za.getType()); })
77 .Case<cir::PoisonAttr>(
78 [&tc](cir::PoisonAttr pa) { return tc.isLegal(pa.getType()); })
79 .Case<cir::UndefAttr>(
80 [&tc](cir::UndefAttr uda) { return tc.isLegal(uda.getType()); })
81 .Case<mlir::TypeAttr>(
82 [&tc](mlir::TypeAttr ta) { return tc.isLegal(ta.getValue()); })
83 .Case<cir::ConstPtrAttr>(
84 [&tc](cir::ConstPtrAttr cpa) { return tc.isLegal(cpa.getType()); })
85 .Case<cir::CXXCtorAttr>(
86 [&tc](cir::CXXCtorAttr ca) { return tc.isLegal(ca.getType()); })
87 .Case<cir::CXXDtorAttr>(
88 [&tc](cir::CXXDtorAttr da) { return tc.isLegal(da.getType()); })
89 .Case<cir::CXXAssignAttr>(
90 [&tc](cir::CXXAssignAttr aa) { return tc.isLegal(aa.getType()); })
91
92 // Collection attributes are legal if ALL of the attributes in them are
93 // also legal.
94 .Case<mlir::ArrayAttr>([&tc](mlir::ArrayAttr array) {
95 return llvm::all_of(array.getValue(), [&tc](mlir::Attribute attr) {
96 return isCXXABIAttributeLegal(tc, attr);
97 });
98 })
99 .Case<mlir::DictionaryAttr>([&tc](mlir::DictionaryAttr dict) {
100 return llvm::all_of(dict.getValue(), [&tc](mlir::NamedAttribute na) {
101 return isCXXABIAttributeLegal(tc, na.getValue());
102 });
103 })
104 // These attributes have sub-attributes that we should check for legality.
105 .Case<cir::ConstArrayAttr>([&tc](cir::ConstArrayAttr array) {
106 return tc.isLegal(array.getType()) &&
107 isCXXABIAttributeLegal(tc, array.getElts());
108 })
109 .Case<cir::GlobalViewAttr>([&tc](cir::GlobalViewAttr gva) {
110 return tc.isLegal(gva.getType()) &&
111 isCXXABIAttributeLegal(tc, gva.getIndices());
112 })
113 .Case<cir::VTableAttr>([&tc](cir::VTableAttr vta) {
114 return tc.isLegal(vta.getType()) &&
115 isCXXABIAttributeLegal(tc, vta.getData());
116 })
117 .Case<cir::TypeInfoAttr>([&tc](cir::TypeInfoAttr tia) {
118 return tc.isLegal(tia.getType()) &&
119 isCXXABIAttributeLegal(tc, tia.getData());
120 })
121 .Case<cir::DynamicCastInfoAttr>([&tc](cir::DynamicCastInfoAttr dcia) {
122 return isCXXABIAttributeLegal(tc, dcia.getSrcRtti()) &&
123 isCXXABIAttributeLegal(tc, dcia.getDestRtti()) &&
124 isCXXABIAttributeLegal(tc, dcia.getRuntimeFunc()) &&
125 isCXXABIAttributeLegal(tc, dcia.getBadCastFunc());
126 })
127 .Case<cir::ConstRecordAttr>([&tc](cir::ConstRecordAttr cra) {
128 return tc.isLegal(cra.getType()) &&
129 isCXXABIAttributeLegal(tc, cra.getMembers());
130 })
131 // We did an audit of all of our attributes (both in OpenACC and CIR), so
132 // it shouldn't be dangerous to consider everything we haven't considered
133 // 'illegal'. Any 'new' attributes will end up asserting in
134 // 'rewriteAttribute' to make sure we consider them here. Otherwise, we
135 // wouldn't discover a problematic new attribute until it contains a
136 // member/method.
137 .Default(false);
138}
139
140mlir::Attribute rewriteAttribute(const mlir::TypeConverter &tc,
141 mlir::MLIRContext *ctx, mlir::Attribute attr) {
142 // If the attribute is legal, there is no reason to rewrite it. This also
143 // filters out 'null' attributes.
144 if (isCXXABIAttributeLegal(tc, attr))
145 return attr;
146
147 // This switch needs to be kept in sync with the potentially-legal type switch
148 // from isCXXABIAttributeLegal. IF we miss any, this will end up causing
149 // verification/transformation issues later, often in the form of
150 // unrealized-conversion-casts.
151
152 return llvm::TypeSwitch<mlir::Attribute, mlir::Attribute>(attr)
153 // These attributes just have a type, so convert just the type.
154 .Case<cir::ZeroAttr>([&tc](cir::ZeroAttr za) {
155 return cir::ZeroAttr::get(tc.convertType(za.getType()));
156 })
157 .Case<cir::PoisonAttr>([&tc](cir::PoisonAttr pa) {
158 return cir::PoisonAttr::get(tc.convertType(pa.getType()));
159 })
160 .Case<cir::UndefAttr>([&tc](cir::UndefAttr uda) {
161 return cir::UndefAttr::get(tc.convertType(uda.getType()));
162 })
163 .Case<mlir::TypeAttr>([&tc](mlir::TypeAttr ta) {
164 return mlir::TypeAttr::get(tc.convertType(ta.getValue()));
165 })
166 .Case<cir::ConstPtrAttr>([&tc](cir::ConstPtrAttr cpa) {
167 return cir::ConstPtrAttr::get(tc.convertType(cpa.getType()),
168 cpa.getValue());
169 })
170 .Case<cir::CXXCtorAttr>([&tc](cir::CXXCtorAttr ca) {
171 return cir::CXXCtorAttr::get(tc.convertType(ca.getType()),
172 ca.getCtorKind(), ca.getIsTrivial());
173 })
174 .Case<cir::CXXDtorAttr>([&tc](cir::CXXDtorAttr da) {
175 return cir::CXXDtorAttr::get(tc.convertType(da.getType()),
176 da.getIsTrivial());
177 })
178 .Case<cir::CXXAssignAttr>([&tc](cir::CXXAssignAttr aa) {
179 return cir::CXXAssignAttr::get(tc.convertType(aa.getType()),
180 aa.getAssignKind(), aa.getIsTrivial());
181 })
182 // Collection attributes need to transform all of the attributes inside of
183 // them.
184 .Case<mlir::ArrayAttr>([&tc, ctx](mlir::ArrayAttr array) {
186 for (mlir::Attribute a : array.getValue())
187 elts.push_back(rewriteAttribute(tc, ctx, a));
188 return mlir::ArrayAttr::get(ctx, elts);
189 })
190 .Case<mlir::DictionaryAttr>([&tc, ctx](mlir::DictionaryAttr dict) {
192 for (mlir::NamedAttribute na : dict.getValue())
193 elts.emplace_back(na.getName(),
194 rewriteAttribute(tc, ctx, na.getValue()));
195
196 return mlir::DictionaryAttr::get(ctx, elts);
197 })
198 // These attributes have sub-attributes that need converting too.
199 .Case<cir::ConstArrayAttr>([&tc, ctx](cir::ConstArrayAttr array) {
200 return cir::ConstArrayAttr::get(
201 ctx, tc.convertType(array.getType()),
202 rewriteAttribute(tc, ctx, array.getElts()),
203 array.getTrailingZerosNum());
204 })
205 .Case<cir::GlobalViewAttr>([&tc, ctx](cir::GlobalViewAttr gva) {
206 return cir::GlobalViewAttr::get(
207 tc.convertType(gva.getType()), gva.getSymbol(),
208 mlir::cast<mlir::ArrayAttr>(
209 rewriteAttribute(tc, ctx, gva.getIndices())));
210 })
211 .Case<cir::VTableAttr>([&tc, ctx](cir::VTableAttr vta) {
212 return cir::VTableAttr::get(
213 tc.convertType(vta.getType()),
214 mlir::cast<mlir::ArrayAttr>(
215 rewriteAttribute(tc, ctx, vta.getData())));
216 })
217 .Case<cir::TypeInfoAttr>([&tc, ctx](cir::TypeInfoAttr tia) {
218 return cir::TypeInfoAttr::get(
219 tc.convertType(tia.getType()),
220 mlir::cast<mlir::ArrayAttr>(
221 rewriteAttribute(tc, ctx, tia.getData())));
222 })
223 .Case<cir::DynamicCastInfoAttr>([&tc,
224 ctx](cir::DynamicCastInfoAttr dcia) {
225 return cir::DynamicCastInfoAttr::get(
226 mlir::cast<cir::GlobalViewAttr>(
227 rewriteAttribute(tc, ctx, dcia.getSrcRtti())),
228 mlir::cast<cir::GlobalViewAttr>(
229 rewriteAttribute(tc, ctx, dcia.getDestRtti())),
230 dcia.getRuntimeFunc(), dcia.getBadCastFunc(), dcia.getOffsetHint());
231 })
232 .Case<cir::ConstRecordAttr>([&tc, ctx](cir::ConstRecordAttr cra) {
233 return cir::ConstRecordAttr::get(
234 ctx, tc.convertType(cra.getType()),
235 mlir::cast<mlir::ArrayAttr>(
236 rewriteAttribute(tc, ctx, cra.getMembers())));
237 })
238 .DefaultUnreachable("unrewritten illegal attribute kind");
239}
240
241#define GET_ABI_LOWERING_PATTERNS
242#include "clang/CIR/Dialect/IR/CIRLowering.inc"
243#undef GET_ABI_LOWERING_PATTERNS
244
245struct CXXABILoweringPass
246 : public impl::CXXABILoweringBase<CXXABILoweringPass> {
247 CXXABILoweringPass() = default;
248 void runOnOperation() override;
249};
250
251/// A generic ABI lowering rewrite pattern. This conversion pattern matches any
252/// CIR dialect operations with at least one operand or result of an
253/// ABI-dependent type. This conversion pattern rewrites the matched operation
254/// by replacing all its ABI-dependent operands and results with their
255/// lowered counterparts.
256class CIRGenericCXXABILoweringPattern : public mlir::ConversionPattern {
257public:
258 CIRGenericCXXABILoweringPattern(mlir::MLIRContext *context,
259 const mlir::TypeConverter &typeConverter)
260 : mlir::ConversionPattern(typeConverter, MatchAnyOpTypeTag(),
261 /*benefit=*/1, context) {}
262
263 mlir::LogicalResult
264 matchAndRewrite(mlir::Operation *op, llvm::ArrayRef<mlir::Value> operands,
265 mlir::ConversionPatternRewriter &rewriter) const override {
266 // Do not match on operations that have dedicated ABI lowering rewrite rules
267 if (llvm::isa<cir::AllocaOp, cir::BaseDataMemberOp, cir::BaseMethodOp,
268 cir::CastOp, cir::CmpOp, cir::ConstantOp, cir::DeleteArrayOp,
269 cir::DerivedDataMemberOp, cir::DerivedMethodOp, cir::FuncOp,
270 cir::GetMethodOp, cir::GetRuntimeMemberOp, cir::GlobalOp>(op))
271 return mlir::failure();
272
273 const mlir::TypeConverter *typeConverter = getTypeConverter();
274 assert(typeConverter &&
275 "CIRGenericCXXABILoweringPattern requires a type converter");
276 bool operandsAndResultsLegal = typeConverter->isLegal(op);
277 bool regionsLegal =
278 std::all_of(op->getRegions().begin(), op->getRegions().end(),
279 [typeConverter](mlir::Region &region) {
280 return typeConverter->isLegal(&region);
281 });
282 bool attrsLegal =
283 llvm::all_of(op->getAttrs(), [typeConverter](mlir::NamedAttribute na) {
284 return isCXXABIAttributeLegal(*typeConverter, na.getValue());
285 });
286
287 if (operandsAndResultsLegal && regionsLegal && attrsLegal) {
288 // The operation does not have any CXXABI-dependent operands or results,
289 // the match fails.
290 return mlir::failure();
291 }
292
293 mlir::OperationState loweredOpState(op->getLoc(), op->getName());
294 loweredOpState.addOperands(operands);
295 loweredOpState.addSuccessors(op->getSuccessors());
296
297 // Lower all attributes.
298 llvm::SmallVector<mlir::NamedAttribute> attrs;
299 for (const mlir::NamedAttribute &na : op->getAttrs())
300 attrs.push_back(
301 {na.getName(),
302 rewriteAttribute(*typeConverter, op->getContext(), na.getValue())});
303 loweredOpState.addAttributes(attrs);
304
305 // Lower all result types
306 llvm::SmallVector<mlir::Type> loweredResultTypes;
307 loweredResultTypes.reserve(op->getNumResults());
308 for (mlir::Type result : op->getResultTypes())
309 loweredResultTypes.push_back(typeConverter->convertType(result));
310 loweredOpState.addTypes(loweredResultTypes);
311
312 // Lower all regions
313 for (mlir::Region &region : op->getRegions()) {
314 mlir::Region *loweredRegion = loweredOpState.addRegion();
315 rewriter.inlineRegionBefore(region, *loweredRegion, loweredRegion->end());
316 if (mlir::failed(
317 rewriter.convertRegionTypes(loweredRegion, *getTypeConverter())))
318 return mlir::failure();
319 }
320
321 // Clone the operation with lowered operand types and result types
322 mlir::Operation *loweredOp = rewriter.create(loweredOpState);
323
324 rewriter.replaceOp(op, loweredOp);
325 return mlir::success();
326 }
327};
328
329} // namespace
330
331mlir::LogicalResult CIRAllocaOpABILowering::matchAndRewrite(
332 cir::AllocaOp op, OpAdaptor adaptor,
333 mlir::ConversionPatternRewriter &rewriter) const {
334 mlir::Type allocaPtrTy = op.getType();
335 mlir::Type loweredAllocaPtrTy = getTypeConverter()->convertType(allocaPtrTy);
336
337 cir::AllocaOp loweredOp = cir::AllocaOp::create(
338 rewriter, op.getLoc(), loweredAllocaPtrTy, op.getName(),
339 op.getAlignmentAttr(), /*dynAllocSize=*/adaptor.getDynAllocSize());
340 loweredOp.setInit(op.getInit());
341 loweredOp.setConstant(op.getConstant());
342 loweredOp.setAnnotationsAttr(op.getAnnotationsAttr());
343
344 rewriter.replaceOp(op, loweredOp);
345 return mlir::success();
346}
347
348mlir::LogicalResult CIRCastOpABILowering::matchAndRewrite(
349 cir::CastOp op, OpAdaptor adaptor,
350 mlir::ConversionPatternRewriter &rewriter) const {
351 mlir::Type srcTy = op.getSrc().getType();
352
353 if (mlir::isa<cir::DataMemberType, cir::MethodType>(srcTy)) {
354 switch (op.getKind()) {
355 case cir::CastKind::bitcast: {
356 mlir::Type destTy = getTypeConverter()->convertType(op.getType());
357 mlir::Value loweredResult;
358 if (mlir::isa<cir::DataMemberType>(srcTy))
359 loweredResult = lowerModule->getCXXABI().lowerDataMemberBitcast(
360 op, destTy, adaptor.getSrc(), rewriter);
361 else
362 loweredResult = lowerModule->getCXXABI().lowerMethodBitcast(
363 op, destTy, adaptor.getSrc(), rewriter);
364 rewriter.replaceOp(op, loweredResult);
365 return mlir::success();
366 }
367 case cir::CastKind::member_ptr_to_bool: {
368 mlir::Value loweredResult;
369 if (mlir::isa<cir::DataMemberType>(srcTy))
370 loweredResult = lowerModule->getCXXABI().lowerDataMemberToBoolCast(
371 op, adaptor.getSrc(), rewriter);
372 else
373 loweredResult = lowerModule->getCXXABI().lowerMethodToBoolCast(
374 op, adaptor.getSrc(), rewriter);
375 rewriter.replaceOp(op, loweredResult);
376 return mlir::success();
377 }
378 default:
379 break;
380 }
381 }
382
383 mlir::Value loweredResult = cir::CastOp::create(
384 rewriter, op.getLoc(), getTypeConverter()->convertType(op.getType()),
385 adaptor.getKind(), adaptor.getSrc());
386 rewriter.replaceOp(op, loweredResult);
387 return mlir::success();
388}
389
390// Helper function to lower a value for things like an initializer.
391static mlir::TypedAttr lowerInitialValue(const LowerModule *lowerModule,
392 const mlir::DataLayout &layout,
393 const mlir::TypeConverter &tc,
394 mlir::Type ty,
395 mlir::Attribute initVal) {
396 if (mlir::isa<cir::DataMemberType>(ty)) {
397 auto dataMemberVal = mlir::cast_if_present<cir::DataMemberAttr>(initVal);
398 return lowerModule->getCXXABI().lowerDataMemberConstant(dataMemberVal,
399 layout, tc);
400 }
401 if (mlir::isa<cir::MethodType>(ty)) {
402 auto methodVal = mlir::cast_if_present<cir::MethodAttr>(initVal);
403 return lowerModule->getCXXABI().lowerMethodConstant(methodVal, layout, tc);
404 }
405
406 if (auto arrTy = mlir::dyn_cast<cir::ArrayType>(ty)) {
407 auto loweredArrTy = mlir::cast<cir::ArrayType>(tc.convertType(arrTy));
408
409 if (!initVal)
410 return {};
411
412 if (auto zeroVal = mlir::dyn_cast_if_present<cir::ZeroAttr>(initVal))
413 return cir::ZeroAttr::get(loweredArrTy);
414
415 auto arrayVal = mlir::cast<cir::ConstArrayAttr>(initVal);
416
417 // String-literal arrays store their bytes as a StringAttr in `elts`. The
418 // backing i8 element type is never rewritten by the CXX ABI type
419 // converter, so the attribute is already legal and can be passed through
420 // unchanged.
421 if (mlir::isa<mlir::StringAttr>(arrayVal.getElts())) {
422 assert(loweredArrTy == arrTy &&
423 "string-literal array type should not change under CXX ABI");
424 return arrayVal;
425 }
426
427 auto arrayElts = mlir::cast<ArrayAttr>(arrayVal.getElts());
428 SmallVector<mlir::Attribute> loweredElements;
429 loweredElements.reserve(arrTy.getSize());
430 for (const mlir::Attribute &attr : arrayElts) {
431 auto typedAttr = cast<mlir::TypedAttr>(attr);
432 loweredElements.push_back(lowerInitialValue(
433 lowerModule, layout, tc, typedAttr.getType(), typedAttr));
434 }
435
436 return cir::ConstArrayAttr::get(
437 loweredArrTy, mlir::ArrayAttr::get(ty.getContext(), loweredElements),
438 arrayVal.getTrailingZerosNum());
439 }
440
441 if (auto recordTy = mlir::dyn_cast<cir::RecordType>(ty)) {
442 auto convertedTy =
443 mlir::dyn_cast<cir::RecordType>(tc.convertType(recordTy));
444 if (!convertedTy)
445 return {};
446
447 if (auto recVal = mlir::dyn_cast_if_present<cir::ZeroAttr>(initVal))
448 return cir::ZeroAttr::get(convertedTy);
449
450 if (auto undefVal = mlir::dyn_cast_if_present<cir::UndefAttr>(initVal))
451 return cir::UndefAttr::get(convertedTy);
452
453 // This might not be possible from Clang directly, but we can get here with
454 // hand-written IR.
455 if (auto poisonVal = mlir::dyn_cast_if_present<cir::PoisonAttr>(initVal))
456 return cir::PoisonAttr::get(convertedTy);
457
458 if (auto recVal =
459 mlir::dyn_cast_if_present<cir::ConstRecordAttr>(initVal)) {
460 auto recordMembers = mlir::cast<ArrayAttr>(recVal.getMembers());
461
462 SmallVector<mlir::Attribute> loweredMembers;
463 loweredMembers.reserve(recordMembers.size());
464
465 for (const mlir::Attribute &attr : recordMembers) {
466 auto typedAttr = cast<mlir::TypedAttr>(attr);
467 loweredMembers.push_back(lowerInitialValue(
468 lowerModule, layout, tc, typedAttr.getType(), typedAttr));
469 }
470
471 return cir::ConstRecordAttr::get(
472 convertedTy, mlir::ArrayAttr::get(ty.getContext(), loweredMembers));
473 }
474
475 assert(!initVal && "Record init val type not handled");
476 return {};
477 }
478
479 // Pointers can contain record types, which can change.
480 if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty)) {
481 auto convertedTy = mlir::cast<cir::PointerType>(tc.convertType(ptrTy));
482 // pointers don't change other than their types.
483
484 if (auto gva = mlir::dyn_cast_if_present<cir::GlobalViewAttr>(initVal))
485 return cir::GlobalViewAttr::get(convertedTy, gva.getSymbol(),
486 gva.getIndices());
487
488 if (auto blockAddr =
489 mlir::dyn_cast_if_present<cir::BlockAddrInfoAttr>(initVal)) {
490 assert(convertedTy == ptrTy && "BlockAddrInfo type should not change");
491 return blockAddr;
492 }
493
494 auto constPtr = mlir::cast_if_present<cir::ConstPtrAttr>(initVal);
495 if (!constPtr)
496 return {};
497 return cir::ConstPtrAttr::get(convertedTy, constPtr.getValue());
498 }
499
500 assert(ty == tc.convertType(ty) &&
501 "cir.global or constant operand is not an CXXABI-dependent type");
502
503 // Every other type can be left alone.
504 return cast<mlir::TypedAttr>(initVal);
505}
506
507mlir::LogicalResult CIRConstantOpABILowering::matchAndRewrite(
508 cir::ConstantOp op, OpAdaptor adaptor,
509 mlir::ConversionPatternRewriter &rewriter) const {
510
511 mlir::DataLayout layout(op->getParentOfType<mlir::ModuleOp>());
512 mlir::TypedAttr newValue = lowerInitialValue(
513 lowerModule, layout, *getTypeConverter(), op.getType(), op.getValue());
514 rewriter.replaceOpWithNewOp<ConstantOp>(op, newValue);
515 return mlir::success();
516}
517
518mlir::LogicalResult CIRCmpOpABILowering::matchAndRewrite(
519 cir::CmpOp op, OpAdaptor adaptor,
520 mlir::ConversionPatternRewriter &rewriter) const {
521 mlir::Type type = op.getLhs().getType();
522
523 mlir::Value loweredResult;
524 if (mlir::isa<cir::DataMemberType>(type))
525 loweredResult = lowerModule->getCXXABI().lowerDataMemberCmp(
526 op, adaptor.getLhs(), adaptor.getRhs(), rewriter);
527 else if (mlir::isa<cir::MethodType>(type))
528 loweredResult = lowerModule->getCXXABI().lowerMethodCmp(
529 op, adaptor.getLhs(), adaptor.getRhs(), rewriter);
530 else
531 loweredResult = cir::CmpOp::create(
532 rewriter, op.getLoc(), getTypeConverter()->convertType(op.getType()),
533 adaptor.getKind(), adaptor.getLhs(), adaptor.getRhs());
534
535 rewriter.replaceOp(op, loweredResult);
536 return mlir::success();
537}
538
539mlir::LogicalResult CIRFuncOpABILowering::matchAndRewrite(
540 cir::FuncOp op, OpAdaptor adaptor,
541 mlir::ConversionPatternRewriter &rewriter) const {
542 cir::FuncType opFuncType = op.getFunctionType();
543 mlir::TypeConverter::SignatureConversion signatureConversion(
544 opFuncType.getNumInputs());
545
546 for (const auto &[i, argType] : llvm::enumerate(opFuncType.getInputs())) {
547 mlir::Type loweredArgType = getTypeConverter()->convertType(argType);
548 if (!loweredArgType)
549 return mlir::failure();
550 signatureConversion.addInputs(i, loweredArgType);
551 }
552
553 mlir::Type loweredResultType =
554 getTypeConverter()->convertType(opFuncType.getReturnType());
555 if (!loweredResultType)
556 return mlir::failure();
557
558 auto loweredFuncType =
559 cir::FuncType::get(signatureConversion.getConvertedTypes(),
560 loweredResultType, /*isVarArg=*/opFuncType.isVarArg());
561
562 // Create a new cir.func operation for the CXXABI-lowered function.
563 cir::FuncOp loweredFuncOp = rewriter.cloneWithoutRegions(op);
564 loweredFuncOp.setFunctionType(loweredFuncType);
565
567 for (const mlir::NamedAttribute &na : op->getAttrs())
568 attrs.push_back(
569 {na.getName(), rewriteAttribute(*getTypeConverter(), op->getContext(),
570 na.getValue())});
571
572 loweredFuncOp->setAttrs(attrs);
573
574 rewriter.inlineRegionBefore(op.getBody(), loweredFuncOp.getBody(),
575 loweredFuncOp.end());
576 if (mlir::failed(rewriter.convertRegionTypes(
577 &loweredFuncOp.getBody(), *getTypeConverter(), &signatureConversion)))
578 return mlir::failure();
579
580 rewriter.eraseOp(op);
581 return mlir::success();
582}
583
584mlir::LogicalResult CIRGlobalOpABILowering::matchAndRewrite(
585 cir::GlobalOp op, OpAdaptor adaptor,
586 mlir::ConversionPatternRewriter &rewriter) const {
587 mlir::Type ty = op.getSymType();
588 mlir::Type loweredTy = getTypeConverter()->convertType(ty);
589 if (!loweredTy)
590 return mlir::failure();
591
592 mlir::DataLayout layout(op->getParentOfType<mlir::ModuleOp>());
593
594 mlir::Attribute loweredInit = lowerInitialValue(
595 lowerModule, layout, *getTypeConverter(), ty, op.getInitialValueAttr());
596
597 auto newOp = mlir::cast<cir::GlobalOp>(rewriter.clone(*op.getOperation()));
598 newOp.setInitialValueAttr(loweredInit);
599 newOp.setSymType(loweredTy);
600 rewriter.replaceOp(op, newOp);
601 return mlir::success();
602}
603
604mlir::LogicalResult CIRBaseDataMemberOpABILowering::matchAndRewrite(
605 cir::BaseDataMemberOp op, OpAdaptor adaptor,
606 mlir::ConversionPatternRewriter &rewriter) const {
607 mlir::Value loweredResult = lowerModule->getCXXABI().lowerBaseDataMember(
608 op, adaptor.getSrc(), rewriter);
609 rewriter.replaceOp(op, loweredResult);
610 return mlir::success();
611}
612
613mlir::LogicalResult CIRBaseMethodOpABILowering::matchAndRewrite(
614 cir::BaseMethodOp op, OpAdaptor adaptor,
615 mlir::ConversionPatternRewriter &rewriter) const {
616 mlir::Value loweredResult =
617 lowerModule->getCXXABI().lowerBaseMethod(op, adaptor.getSrc(), rewriter);
618 rewriter.replaceOp(op, loweredResult);
619 return mlir::success();
620}
621
622mlir::LogicalResult CIRDeleteArrayOpABILowering::matchAndRewrite(
623 cir::DeleteArrayOp op, OpAdaptor adaptor,
624 mlir::ConversionPatternRewriter &rewriter) const {
625 mlir::FlatSymbolRefAttr deleteFn = op.getDeleteFnAttr();
626 mlir::Location loc = op->getLoc();
627 mlir::Value loweredAddress = adaptor.getAddress();
628
629 cir::UsualDeleteParamsAttr deleteParams = op.getDeleteParams();
630 bool cookieRequired = deleteParams.getSize() || op.getElementDtorAttr();
631
632 if (deleteParams.getTypeAwareDelete() || deleteParams.getDestroyingDelete() ||
633 deleteParams.getAlignment())
634 return rewriter.notifyMatchFailure(
635 op, "type-aware, destroying, or aligned delete not yet supported");
636
637 const CIRCXXABI &cxxABI = lowerModule->getCXXABI();
638 CIRBaseBuilderTy cirBuilder(rewriter);
639
640 // Read the array cookie (or compute the void* pointer for the
641 // non-cookie case) before creating the cleanup scope. The cookie read
642 // produces values that are needed by both the destruction loop in the
643 // body region (numElements for the array.dtor) and the operator
644 // delete[] call in the cleanup region (deletePtr / numElements for the
645 // total-size computation), so it must dominate both regions.
646 mlir::Value deletePtr;
647 mlir::Value numElements;
648 cir::PointerType ptrTy;
649 clang::CharUnits cookieSize;
650 mlir::DataLayout dl(op->getParentOfType<mlir::ModuleOp>());
651 unsigned ptrWidth =
652 lowerModule->getTarget().getPointerWidth(clang::LangAS::Default);
653 cir::IntType sizeTy = cirBuilder.getUIntNTy(ptrWidth);
654
655 if (cookieRequired) {
656 ptrTy = mlir::cast<cir::PointerType>(loweredAddress.getType());
657 cxxABI.readArrayCookie(loc, loweredAddress, dl, cirBuilder, numElements,
658 deletePtr, cookieSize);
659 } else {
660 deletePtr = cir::CastOp::create(rewriter, loc, cirBuilder.getVoidPtrTy(),
661 cir::CastKind::bitcast, loweredAddress);
662 }
663
664 // Create a cleanup scope to wrap the ArrayDtor operation (if needed) and
665 // call the array delete operator from the cleanup region. If no exceptions
666 // are thrown during the array dtor, the normal control flow will call the
667 // delete operator. The ArrayDtor operation will get its own cleanup region
668 // when it is expanded during LoweringPrepare. If an exception is thrown, the
669 // exception handling flow will be connected to the cleanup region here to
670 // call the delete operator on the exception path.
671 mlir::FlatSymbolRefAttr dtorFn = op.getElementDtorAttr();
672 cir::CleanupKind cleanupKind =
673 op.getDtorMayThrow() ? cir::CleanupKind::All : cir::CleanupKind::Normal;
674 cir::CleanupScopeOp::create(
675 rewriter, loc, cleanupKind,
676 /*bodyBuilder=*/
677 [&](mlir::OpBuilder &b, mlir::Location l) {
678 if (dtorFn) {
679 auto eltPtrTy = cir::PointerType::get(ptrTy.getPointee());
680 auto arrayDtor = cir::ArrayDtor::create(
681 b, l, loweredAddress, numElements,
682 [&](mlir::OpBuilder &bb, mlir::Location ll) {
683 mlir::Value arg =
684 bb.getInsertionBlock()->addArgument(eltPtrTy, ll);
685 auto dtorCall = cir::CallOp::create(
686 bb, ll, dtorFn, cir::VoidType(), mlir::ValueRange{arg});
687 if (!op.getDtorMayThrow())
688 dtorCall.setNothrowAttr(bb.getUnitAttr());
689 cir::YieldOp::create(bb, ll);
690 });
691 if (op.getDtorMayThrow())
692 arrayDtor.setDtorMayThrow(true);
693 }
694 cir::YieldOp::create(b, l);
695 },
696 /*cleanupBuilder=*/
697 [&](mlir::OpBuilder &b, mlir::Location l) {
699 callArgs.push_back(deletePtr);
700 if (deleteParams.getSize()) {
701 uint64_t eltSizeBytes = dl.getTypeSizeInBits(ptrTy.getPointee()) / 8;
702 auto eltSizeVal = cir::ConstantOp::create(
703 b, l, cir::IntAttr::get(sizeTy, eltSizeBytes));
704 mlir::Value allocSize =
705 cir::MulOp::create(b, l, sizeTy, eltSizeVal, numElements);
706 auto cookieSizeVal = cir::ConstantOp::create(
707 b, l, cir::IntAttr::get(sizeTy, cookieSize.getQuantity()));
708 allocSize =
709 cir::AddOp::create(b, l, sizeTy, allocSize, cookieSizeVal);
710 callArgs.push_back(allocSize);
711 }
712 auto deleteCall =
713 cir::CallOp::create(b, l, deleteFn, cir::VoidType(), callArgs);
714 // operator delete[] is implicitly nothrow per [basic.stc.dynamic],
715 // matching classic CodeGen's `nounwind` attribute on the call.
716 deleteCall.setNothrowAttr(b.getUnitAttr());
717 cir::YieldOp::create(b, l);
718 });
719
720 rewriter.eraseOp(op);
721 return mlir::success();
722}
723
724mlir::LogicalResult CIRDerivedDataMemberOpABILowering::matchAndRewrite(
725 cir::DerivedDataMemberOp op, OpAdaptor adaptor,
726 mlir::ConversionPatternRewriter &rewriter) const {
727 mlir::Value loweredResult = lowerModule->getCXXABI().lowerDerivedDataMember(
728 op, adaptor.getSrc(), rewriter);
729 rewriter.replaceOp(op, loweredResult);
730 return mlir::success();
731}
732
733mlir::LogicalResult CIRDerivedMethodOpABILowering::matchAndRewrite(
734 cir::DerivedMethodOp op, OpAdaptor adaptor,
735 mlir::ConversionPatternRewriter &rewriter) const {
736 mlir::Value loweredResult = lowerModule->getCXXABI().lowerDerivedMethod(
737 op, adaptor.getSrc(), rewriter);
738 rewriter.replaceOp(op, loweredResult);
739 return mlir::success();
740}
741
742mlir::LogicalResult CIRDynamicCastOpABILowering::matchAndRewrite(
743 cir::DynamicCastOp op, OpAdaptor adaptor,
744 mlir::ConversionPatternRewriter &rewriter) const {
745 mlir::Value loweredResult =
746 lowerModule->getCXXABI().lowerDynamicCast(op, rewriter);
747 rewriter.replaceOp(op, loweredResult);
748 return mlir::success();
749}
750
751mlir::LogicalResult CIRGetMethodOpABILowering::matchAndRewrite(
752 cir::GetMethodOp op, OpAdaptor adaptor,
753 mlir::ConversionPatternRewriter &rewriter) const {
754 mlir::Value callee;
755 mlir::Value thisArg;
756 lowerModule->getCXXABI().lowerGetMethod(
757 op, callee, thisArg, adaptor.getMethod(), adaptor.getObject(), rewriter);
758 rewriter.replaceOp(op, {callee, thisArg});
759 return mlir::success();
760}
761
762mlir::LogicalResult CIRGetRuntimeMemberOpABILowering::matchAndRewrite(
763 cir::GetRuntimeMemberOp op, OpAdaptor adaptor,
764 mlir::ConversionPatternRewriter &rewriter) const {
765 mlir::Type resTy = getTypeConverter()->convertType(op.getType());
766 mlir::Operation *newOp = lowerModule->getCXXABI().lowerGetRuntimeMember(
767 op, resTy, adaptor.getAddr(), adaptor.getMember(), rewriter);
768 rewriter.replaceOp(op, newOp);
769 return mlir::success();
770}
771
772mlir::LogicalResult CIRVTableGetTypeInfoOpABILowering::matchAndRewrite(
773 cir::VTableGetTypeInfoOp op, OpAdaptor adaptor,
774 mlir::ConversionPatternRewriter &rewriter) const {
775 mlir::Value loweredResult =
776 lowerModule->getCXXABI().lowerVTableGetTypeInfo(op, rewriter);
777 rewriter.replaceOp(op, loweredResult);
778 return mlir::success();
779}
780
781namespace {
782// A small type to handle type conversion for the the CXXABILoweringPass.
783// Even though this is a CIR-to-CIR pass, we are eliminating some CIR types.
784// Most importantly, this pass solves recursive type conversion problems by
785// keeping a call stack.
786class CIRABITypeConverter : public mlir::TypeConverter {
787
788 mlir::MLIRContext &context;
789
790 // Recursive structure detection.
791 // We store one entry per thread here, and rely on locking. This works the
792 // same way as the LLVM-IR lowering does it, which has a similar problem.
793 DenseMap<uint64_t, std::unique_ptr<SmallVector<cir::RecordType>>>
794 conversionCallStack;
795 llvm::sys::SmartRWMutex<true> callStackMutex;
796
797 // In order to let us 'change the names' back after the fact, we collect them
798 // along the way. They should only be added/accessed via the thread-safe
799 // functions below.
800 llvm::SmallVector<cir::RecordType> convertedRecordTypes;
801 llvm::sys::SmartRWMutex<true> recordTypeMutex;
802
803 // This provides a stack for the RecordTypes being processed on the current
804 // thread, which lets us solve recursive conversions. This implementation is
805 // cribbed from the LLVMTypeConverter which solves a similar but not identical
806 // problem.
807 SmallVector<cir::RecordType> &getCurrentThreadRecursiveStack() {
808 {
809 // Most of the time, the entry already exists in the map.
810 std::shared_lock<decltype(callStackMutex)> lock(callStackMutex,
811 std::defer_lock);
812 if (context.isMultithreadingEnabled())
813 lock.lock();
814 auto recursiveStack = conversionCallStack.find(llvm::get_threadid());
815 if (recursiveStack != conversionCallStack.end())
816 return *recursiveStack->second;
817 }
818
819 // First time this thread gets here, we have to get an exclusive access to
820 // insert in the map
821 std::unique_lock<decltype(callStackMutex)> lock(callStackMutex);
822 auto recursiveStackInserted = conversionCallStack.insert(
823 std::make_pair(llvm::get_threadid(),
824 std::make_unique<SmallVector<cir::RecordType>>()));
825 return *recursiveStackInserted.first->second;
826 }
827
828 void addConvertedRecordType(cir::RecordType rt) {
829 std::unique_lock<decltype(recordTypeMutex)> lock(recordTypeMutex);
830 convertedRecordTypes.push_back(rt);
831 }
832
833 llvm::SmallVector<mlir::Type> convertRecordMemberTypes(cir::RecordType type) {
834 llvm::SmallVector<mlir::Type> loweredMemberTypes;
835 loweredMemberTypes.reserve(type.getNumElements());
836
837 if (mlir::failed(convertTypes(type.getMembers(), loweredMemberTypes)))
838 return {};
839
840 return loweredMemberTypes;
841 }
842
843 cir::RecordType convertRecordType(cir::RecordType type) {
844 // Unnamed record types can't be referred to recursively, so we can just
845 // convert this one. It also doesn't have uniqueness problems, so we can
846 // just do a conversion on it.
847 if (!type.getName()) {
848 llvm::SmallVector<mlir::Type> converted = convertRecordMemberTypes(type);
849 if (auto u = mlir::dyn_cast<cir::UnionType>(type)) {
850 mlir::Type loweredPadding;
851 if (mlir::Type pad = u.getPadding())
852 loweredPadding = convertType(pad);
853 return cir::UnionType::get(type.getContext(), converted,
854 type.getPacked(), loweredPadding);
855 }
856 auto s = mlir::cast<cir::StructType>(type);
857 return cir::StructType::get(type.getContext(), converted,
858 type.getPacked(), type.getPadded(),
859 s.getIsClass());
860 }
861
862 assert(!type.isIncomplete() || type.getMembers().empty());
863
864 // If the type has already been converted, we can just return, since there
865 // is nothing to do. Also, if it is incomplete, it can't have invalid
866 // members! So we can skip transforming it.
867 if (type.isIncomplete() || type.isABIConvertedRecord())
868 return type;
869
870 SmallVectorImpl<cir::RecordType> &recursiveStack =
871 getCurrentThreadRecursiveStack();
872
873 cir::RecordType convertedType;
874 if (mlir::isa<cir::UnionType>(type))
875 convertedType =
876 cir::UnionType::get(type.getContext(), type.getABIConvertedName());
877 else
878 convertedType =
879 cir::StructType::get(type.getContext(), type.getABIConvertedName(),
880 mlir::cast<cir::StructType>(type).getIsClass());
881
882 // This type has already been converted, just return it.
883 if (convertedType.isComplete())
884 return convertedType;
885
886 // We put the existing 'type' into the vector if we're in the process of
887 // converting it (and pop it when we're done). To prevent recursion,
888 // just return the 'incomplete' version, and the 'top level' version of this
889 // call will call 'complete' on it.
890 if (llvm::is_contained(recursiveStack, type))
891 return convertedType;
892
893 recursiveStack.push_back(type);
894 llvm::scope_exit popConvertingType(
895 [&recursiveStack]() { recursiveStack.pop_back(); });
896
897 SmallVector<mlir::Type> convertedMembers = convertRecordMemberTypes(type);
898
899 mlir::Type loweredPadding;
900 if (auto u = mlir::dyn_cast<cir::UnionType>(type))
901 if (mlir::Type pad = u.getPadding())
902 loweredPadding = convertType(pad);
903 convertedType.complete(convertedMembers, type.getPacked(), type.getPadded(),
904 loweredPadding);
905 addConvertedRecordType(convertedType);
906 return convertedType;
907 }
908
909public:
910 CIRABITypeConverter(mlir::MLIRContext &ctx, mlir::DataLayout &dataLayout,
911 cir::LowerModule &lowerModule)
912 : context(ctx) {
913 addConversion([&](mlir::Type type) -> mlir::Type { return type; });
914 // This is necessary in order to convert CIR pointer types that are
915 // pointing to CIR types that we are lowering in this pass.
916 addConversion([&](cir::PointerType type) -> mlir::Type {
917 mlir::Type loweredPointeeType = convertType(type.getPointee());
918 if (!loweredPointeeType)
919 return {};
920 return cir::PointerType::get(type.getContext(), loweredPointeeType,
921 type.getAddrSpace());
922 });
923 addConversion([&](cir::ArrayType type) -> mlir::Type {
924 mlir::Type loweredElementType = convertType(type.getElementType());
925 if (!loweredElementType)
926 return {};
927 return cir::ArrayType::get(loweredElementType, type.getSize());
928 });
929
930 addConversion([&](cir::DataMemberType type) -> mlir::Type {
931 mlir::Type abiType =
932 lowerModule.getCXXABI().lowerDataMemberType(type, *this);
933 return convertType(abiType);
934 });
935 addConversion([&](cir::MethodType type) -> mlir::Type {
936 mlir::Type abiType = lowerModule.getCXXABI().lowerMethodType(type, *this);
937 return convertType(abiType);
938 });
939 // This is necessary in order to convert CIR function types that have
940 // argument or return types that use CIR types that we are lowering in
941 // this pass.
942 addConversion([&](cir::FuncType type) -> mlir::Type {
943 llvm::SmallVector<mlir::Type> loweredInputTypes;
944 loweredInputTypes.reserve(type.getNumInputs());
945 if (mlir::failed(convertTypes(type.getInputs(), loweredInputTypes)))
946 return {};
947
948 mlir::Type loweredReturnType = convertType(type.getReturnType());
949 if (!loweredReturnType)
950 return {};
951
952 return cir::FuncType::get(loweredInputTypes, loweredReturnType,
953 /*isVarArg=*/type.getVarArg());
954 });
955 addConversion([&](cir::StructType type) -> mlir::Type {
956 return convertRecordType(type);
957 });
958 addConversion([&](cir::UnionType type) -> mlir::Type {
959 return convertRecordType(type);
960 });
961 }
962
963 void restoreRecordTypeNames() {
964 std::unique_lock<decltype(recordTypeMutex)> lock(recordTypeMutex);
965
966 for (auto rt : convertedRecordTypes)
968 }
969};
970} // namespace
971
972static void
973populateCXXABIConversionTarget(mlir::ConversionTarget &target,
974 const mlir::TypeConverter &typeConverter) {
975 target.addLegalOp<mlir::ModuleOp>();
976
977 // The ABI lowering pass is interested in CIR operations with operands or
978 // results of CXXABI-dependent types, or CIR operations with regions whose
979 // block arguments are of CXXABI-dependent types.
980 target.addDynamicallyLegalDialect<cir::CIRDialect>(
981 [&typeConverter](mlir::Operation *op) {
982 if (!typeConverter.isLegal(op))
983 return false;
984
985 bool attrs = llvm::all_of(
986 op->getAttrs(), [&typeConverter](const mlir::NamedAttribute &a) {
987 return isCXXABIAttributeLegal(typeConverter, a.getValue());
988 });
989
990 return attrs &&
991 std::all_of(op->getRegions().begin(), op->getRegions().end(),
992 [&typeConverter](mlir::Region &region) {
993 return typeConverter.isLegal(&region);
994 });
995 });
996
997 target.addDynamicallyLegalDialect<mlir::acc::OpenACCDialect>(
998 [&typeConverter](mlir::Operation *op) {
999 if (!typeConverter.isLegal(op))
1000 return false;
1001
1002 bool attrs = llvm::all_of(
1003 op->getAttrs(), [&typeConverter](const mlir::NamedAttribute &a) {
1004 return isCXXABIAttributeLegal(typeConverter, a.getValue());
1005 });
1006
1007 return attrs &&
1008 std::all_of(op->getRegions().begin(), op->getRegions().end(),
1009 [&typeConverter](mlir::Region &region) {
1010 return typeConverter.isLegal(&region);
1011 });
1012 });
1013
1014 // Some CIR ops needs special checking for legality
1015 target.addDynamicallyLegalOp<cir::FuncOp>([&typeConverter](cir::FuncOp op) {
1016 bool attrs = llvm::all_of(
1017 op->getAttrs(), [&typeConverter](const mlir::NamedAttribute &a) {
1018 return isCXXABIAttributeLegal(typeConverter, a.getValue());
1019 });
1020
1021 return attrs && typeConverter.isLegal(op.getFunctionType());
1022 });
1023 target.addDynamicallyLegalOp<cir::GlobalOp>(
1024 [&typeConverter](cir::GlobalOp op) {
1025 return typeConverter.isLegal(op.getSymType());
1026 });
1027 // Operations that do not use any special types must be explicitly marked as
1028 // illegal to trigger processing here.
1029 target.addIllegalOp<cir::DeleteArrayOp>();
1030 target.addIllegalOp<cir::DynamicCastOp>();
1031 target.addIllegalOp<cir::VTableGetTypeInfoOp>();
1032}
1033
1034//===----------------------------------------------------------------------===//
1035// The Pass
1036//===----------------------------------------------------------------------===//
1037
1038void CXXABILoweringPass::runOnOperation() {
1039 auto mod = mlir::cast<mlir::ModuleOp>(getOperation());
1040 mlir::MLIRContext *ctx = mod.getContext();
1041
1042 std::unique_ptr<cir::LowerModule> lowerModule = cir::createLowerModule(mod);
1043 // If lower module is not available, skip the ABI lowering pass.
1044 if (!lowerModule) {
1045 mod.emitWarning("Cannot create a CIR lower module, skipping the ")
1046 << getName() << " pass";
1047 return;
1048 }
1049
1050 mlir::DataLayout dataLayout(mod);
1051 CIRABITypeConverter typeConverter(*ctx, dataLayout, *lowerModule);
1052
1053 mlir::RewritePatternSet patterns(ctx);
1054 patterns.add<CIRGenericCXXABILoweringPattern>(patterns.getContext(),
1055 typeConverter);
1056 patterns.add<
1057#define GET_ABI_LOWERING_PATTERNS_LIST
1058#include "clang/CIR/Dialect/IR/CIRLowering.inc"
1059#undef GET_ABI_LOWERING_PATTERNS_LIST
1060 >(patterns.getContext(), typeConverter, dataLayout, *lowerModule);
1061
1062 mlir::ConversionTarget target(*ctx);
1063 populateCXXABIConversionTarget(target, typeConverter);
1064
1065 llvm::SmallVector<mlir::Operation *> ops;
1066 ops.push_back(mod);
1067 cir::collectUnreachable(mod, ops);
1068
1069 if (failed(mlir::applyPartialConversion(ops, target, std::move(patterns))))
1070 signalPassFailure();
1071
1072 typeConverter.restoreRecordTypeNames();
1073}
1074
1075std::unique_ptr<Pass> mlir::createCXXABILoweringPass() {
1076 return std::make_unique<CXXABILoweringPass>();
1077}
#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.
void readArrayCookie(mlir::Location loc, mlir::Value elementPtr, 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.
Definition CIRCXXABI.cpp:25
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 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.
CIRCXXABI & getCXXABI() const
Definition LowerModule.h:46
bool isComplete() const
Definition CIRTypes.h:112
void removeABIConversionNamePrefix()
Definition CIRTypes.cpp:573
void complete(llvm::ArrayRef< mlir::Type > members, bool packed, bool padded, mlir::Type padding={})
Definition CIRTypes.cpp:535
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
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)
Definition HeaderFile.h:38
RangeSelector callArgs(std::string ID)
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ Default
Set to the current date and time.
std::unique_ptr< Pass > createCXXABILoweringPass()
__DEVICE__ _Tp arg(const std::complex< _Tp > &__c)