10#include "mlir/IR/Attributes.h"
11#include "mlir/IR/IRMapping.h"
26#include "llvm/ADT/TypeSwitch.h"
27#include "llvm/Support/Path.h"
35#define GEN_PASS_DEF_LOWERINGPREPARE
36#include "clang/CIR/Dialect/Passes.h.inc"
40 SmallString<128> fileName;
42 if (mlirModule.getSymName())
43 fileName = llvm::sys::path::filename(mlirModule.getSymName()->str());
48 for (
size_t i = 0; i < fileName.size(); ++i) {
60 mlir::SymbolRefAttr sym = llvm::dyn_cast_if_present<mlir::SymbolRefAttr>(
61 callOp.getCallableForCallee());
64 return dyn_cast_or_null<cir::FuncOp>(
65 mlir::SymbolTable::lookupNearestSymbolFrom(callOp, sym));
69struct LoweringPreparePass
70 :
public impl::LoweringPrepareBase<LoweringPreparePass> {
71 LoweringPreparePass() =
default;
72 void runOnOperation()
override;
74 void runOnOp(mlir::Operation *op);
75 void lowerCastOp(cir::CastOp op);
76 void lowerComplexDivOp(cir::ComplexDivOp op);
77 void lowerComplexMulOp(cir::ComplexMulOp op);
78 void lowerUnaryOp(cir::UnaryOpInterface op);
79 void lowerGlobalOp(cir::GlobalOp op);
80 void lowerThreeWayCmpOp(cir::CmpThreeWayOp op);
81 void lowerArrayDtor(cir::ArrayDtor op);
82 void lowerArrayCtor(cir::ArrayCtor op);
83 void lowerTrivialCopyCall(cir::CallOp op);
84 void lowerStoreOfConstAggregate(cir::StoreOp op);
87 cir::FuncOp buildCXXGlobalVarDeclInitFunc(cir::GlobalOp op);
90 cir::FuncOp getOrCreateDtorFunc(CIRBaseBuilderTy &builder, cir::GlobalOp op,
91 mlir::Region &dtorRegion,
92 cir::CallOp &dtorCall);
95 void buildCXXGlobalInitFunc();
98 void buildGlobalCtorDtorList();
100 cir::FuncOp buildRuntimeFunction(
101 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
103 cir::GlobalLinkageKind linkage = cir::GlobalLinkageKind::ExternalLinkage);
105 cir::GlobalOp buildRuntimeVariable(
106 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
108 cir::GlobalLinkageKind linkage = cir::GlobalLinkageKind::ExternalLinkage,
109 cir::VisibilityKind visibility = cir::VisibilityKind::Default);
112 void handleStaticLocal(cir::GlobalOp globalOp, cir::GetGlobalOp getGlobalOp);
121 cir::GlobalOp createGuardGlobalOp(CIRBaseBuilderTy &builder,
122 mlir::Location loc, llvm::StringRef name,
123 cir::IntType guardTy,
124 cir::GlobalLinkageKind linkage);
127 cir::GlobalOp getStaticLocalDeclGuardAddress(llvm::StringRef globalSymName) {
128 auto it = staticLocalDeclGuardMap.find(globalSymName);
129 if (it != staticLocalDeclGuardMap.end())
135 void setStaticLocalDeclGuardAddress(llvm::StringRef globalSymName,
136 cir::GlobalOp guard) {
137 staticLocalDeclGuardMap[globalSymName] = guard;
141 cir::GlobalOp getOrCreateStaticLocalDeclGuardAddress(
142 CIRBaseBuilderTy &builder, cir::GlobalOp globalOp,
143 cir::ASTVarDeclInterface varDecl, cir::IntType guardTy,
144 clang::CharUnits guardAlignment) {
145 llvm::StringRef globalSymName = globalOp.getSymName();
146 cir::GlobalOp guard = getStaticLocalDeclGuardAddress(globalSymName);
149 llvm::StringRef guardName =
150 globalOp.getStaticLocalGuard()->getName().getValue();
153 guard = createGuardGlobalOp(builder, globalOp->getLoc(), guardName,
154 guardTy, globalOp.getLinkage());
155 guard.setInitialValueAttr(cir::IntAttr::get(guardTy, 0));
156 guard.setDSOLocal(globalOp.getDsoLocal());
157 guard.setAlignment(guardAlignment.
getAsAlign().value());
163 bool hasComdat = globalOp.getComdat();
164 const llvm::Triple &triple = astCtx->getTargetInfo().getTriple();
165 if (!
varDecl.isLocalVarDecl() && hasComdat &&
166 (triple.isOSBinFormatELF() || triple.isOSBinFormatWasm())) {
167 globalOp->emitError(
"NYI: guard COMDAT for non-local variables");
169 }
else if (hasComdat && globalOp.isWeakForLinker()) {
170 globalOp->emitError(
"NYI: guard COMDAT for weak linkage");
174 setStaticLocalDeclGuardAddress(globalSymName, guard);
183 clang::ASTContext *astCtx;
186 mlir::ModuleOp mlirModule;
189 llvm::StringMap<uint32_t> dynamicInitializerNames;
190 llvm::SmallVector<cir::FuncOp> dynamicInitializers;
193 llvm::StringMap<cir::GlobalOp> staticLocalDeclGuardMap;
196 llvm::SmallVector<std::pair<std::string, uint32_t>, 4> globalCtorList;
198 llvm::SmallVector<std::pair<std::string, uint32_t>, 4> globalDtorList;
202 bool useARMGuardVarABI()
const {
203 switch (astCtx->getCXXABIKind()) {
204 case clang::TargetCXXABI::GenericARM:
205 case clang::TargetCXXABI::iOS:
206 case clang::TargetCXXABI::WatchOS:
207 case clang::TargetCXXABI::GenericAArch64:
208 case clang::TargetCXXABI::WebAssembly:
218 void emitCXXGuardedInitIf(CIRBaseBuilderTy &builder, cir::GlobalOp globalOp,
219 cir::ASTVarDeclInterface varDecl,
220 mlir::Value guardPtr, cir::PointerType guardPtrTy,
222 auto loc = globalOp->getLoc();
245 mlir::Value acquireResult = acquireCall.getResult();
248 loc, mlir::cast<cir::IntType>(acquireResult.getType()), 0);
249 auto shouldInit = builder.
createCompare(loc, cir::CmpOpKind::ne,
250 acquireResult, acquireZero);
255 cir::IfOp::create(builder, loc, shouldInit,
false,
256 [](mlir::OpBuilder &, mlir::Location) {});
257 mlir::OpBuilder::InsertionGuard insertGuard(builder);
258 builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
265 auto &ctorRegion = globalOp.getCtorRegion();
266 assert(!ctorRegion.empty() &&
"This should never be empty here.");
267 if (!ctorRegion.hasOneBlock())
268 llvm_unreachable(
"Multiple blocks NYI");
269 mlir::Block &block = ctorRegion.front();
270 mlir::Block *insertBlock = builder.getInsertionBlock();
271 insertBlock->getOperations().splice(insertBlock->end(),
272 block.getOperations(), block.begin(),
273 std::prev(block.end()));
274 builder.setInsertionPointToEnd(insertBlock);
275 ctorRegion.getBlocks().clear();
283 mlir::ValueRange{guardPtr});
286 }
else if (!
varDecl.isLocalVarDecl()) {
292 globalOp->emitError(
"NYI: non-threadsafe init for non-local variables");
298 globalOp->emitError(
"NYI: non-threadsafe init for local variables");
305 void setASTContext(clang::ASTContext *
c) { astCtx =
c; }
310cir::GlobalOp LoweringPreparePass::buildRuntimeVariable(
311 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
312 mlir::Type type, cir::GlobalLinkageKind linkage,
313 cir::VisibilityKind visibility) {
314 cir::GlobalOp g = dyn_cast_or_null<cir::GlobalOp>(
315 mlir::SymbolTable::lookupNearestSymbolFrom(
316 mlirModule, mlir::StringAttr::get(mlirModule->getContext(), name)));
318 g = cir::GlobalOp::create(builder, loc, name, type);
320 cir::GlobalLinkageKindAttr::get(builder.getContext(), linkage));
321 mlir::SymbolTable::setSymbolVisibility(
322 g, mlir::SymbolTable::Visibility::Private);
323 g.setGlobalVisibilityAttr(
324 cir::VisibilityAttr::get(builder.getContext(), visibility));
329cir::FuncOp LoweringPreparePass::buildRuntimeFunction(
330 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
331 cir::FuncType type, cir::GlobalLinkageKind linkage) {
332 cir::FuncOp f = dyn_cast_or_null<FuncOp>(SymbolTable::lookupNearestSymbolFrom(
333 mlirModule, StringAttr::get(mlirModule->getContext(), name)));
335 f = cir::FuncOp::create(builder, loc, name, type);
337 cir::GlobalLinkageKindAttr::get(builder.getContext(), linkage));
338 mlir::SymbolTable::setSymbolVisibility(
339 f, mlir::SymbolTable::Visibility::Private);
349 builder.setInsertionPoint(op);
351 mlir::Value src = op.getSrc();
352 mlir::Value imag = builder.
getNullValue(src.getType(), op.getLoc());
358 cir::CastKind elemToBoolKind) {
360 builder.setInsertionPoint(op);
362 mlir::Value src = op.getSrc();
363 if (!mlir::isa<cir::BoolType>(op.getType()))
370 cir::BoolType boolTy = builder.
getBoolTy();
371 mlir::Value srcRealToBool =
372 builder.
createCast(op.getLoc(), elemToBoolKind, srcReal, boolTy);
373 mlir::Value srcImagToBool =
374 builder.
createCast(op.getLoc(), elemToBoolKind, srcImag, boolTy);
375 return builder.
createLogicalOr(op.getLoc(), srcRealToBool, srcImagToBool);
380 cir::CastKind scalarCastKind) {
382 builder.setInsertionPoint(op);
384 mlir::Value src = op.getSrc();
385 auto dstComplexElemTy =
386 mlir::cast<cir::ComplexType>(op.getType()).getElementType();
391 mlir::Value dstReal = builder.
createCast(op.getLoc(), scalarCastKind, srcReal,
393 mlir::Value dstImag = builder.
createCast(op.getLoc(), scalarCastKind, srcImag,
398void LoweringPreparePass::lowerCastOp(cir::CastOp op) {
399 mlir::MLIRContext &ctx = getContext();
400 mlir::Value loweredValue = [&]() -> mlir::Value {
401 switch (op.getKind()) {
402 case cir::CastKind::float_to_complex:
403 case cir::CastKind::int_to_complex:
405 case cir::CastKind::float_complex_to_real:
406 case cir::CastKind::int_complex_to_real:
408 case cir::CastKind::float_complex_to_bool:
410 case cir::CastKind::int_complex_to_bool:
412 case cir::CastKind::float_complex:
414 case cir::CastKind::float_complex_to_int_complex:
416 case cir::CastKind::int_complex:
418 case cir::CastKind::int_complex_to_float_complex:
426 op.replaceAllUsesWith(loweredValue);
433 llvm::StringRef (*libFuncNameGetter)(llvm::APFloat::Semantics),
434 mlir::Location loc, cir::ComplexType ty, mlir::Value lhsReal,
435 mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag) {
436 cir::FPTypeInterface elementTy =
437 mlir::cast<cir::FPTypeInterface>(ty.getElementType());
439 llvm::StringRef libFuncName = libFuncNameGetter(
440 llvm::APFloat::SemanticsToEnum(elementTy.getFloatSemantics()));
443 cir::FuncType libFuncTy = cir::FuncType::get(libFuncInputTypes, ty);
449 mlir::OpBuilder::InsertionGuard ipGuard{builder};
450 builder.setInsertionPointToStart(pass.mlirModule.getBody());
451 libFunc = pass.buildRuntimeFunction(builder, libFuncName, loc, libFuncTy);
455 builder.
createCallOp(loc, libFunc, {lhsReal, lhsImag, rhsReal, rhsImag});
456 return call.getResult();
459static llvm::StringRef
462 case llvm::APFloat::S_IEEEhalf:
464 case llvm::APFloat::S_IEEEsingle:
466 case llvm::APFloat::S_IEEEdouble:
468 case llvm::APFloat::S_PPCDoubleDouble:
470 case llvm::APFloat::S_x87DoubleExtended:
472 case llvm::APFloat::S_IEEEquad:
475 llvm_unreachable(
"unsupported floating point type");
481 mlir::Value lhsReal, mlir::Value lhsImag,
482 mlir::Value rhsReal, mlir::Value rhsImag) {
484 mlir::Value &a = lhsReal;
485 mlir::Value &
b = lhsImag;
486 mlir::Value &
c = rhsReal;
487 mlir::Value &d = rhsImag;
489 mlir::Value ac = builder.
createMul(loc, a,
c);
490 mlir::Value bd = builder.
createMul(loc,
b, d);
492 mlir::Value dd = builder.
createMul(loc, d, d);
493 mlir::Value acbd = builder.
createAdd(loc, ac, bd);
494 mlir::Value ccdd = builder.
createAdd(loc, cc, dd);
495 mlir::Value resultReal = builder.
createDiv(loc, acbd, ccdd);
498 mlir::Value ad = builder.
createMul(loc, a, d);
499 mlir::Value bcad = builder.
createSub(loc, bc, ad);
500 mlir::Value resultImag = builder.
createDiv(loc, bcad, ccdd);
506 mlir::Value lhsReal, mlir::Value lhsImag,
507 mlir::Value rhsReal, mlir::Value rhsImag) {
528 mlir::Value &a = lhsReal;
529 mlir::Value &
b = lhsImag;
530 mlir::Value &
c = rhsReal;
531 mlir::Value &d = rhsImag;
533 auto trueBranchBuilder = [&](mlir::OpBuilder &, mlir::Location) {
535 mlir::Value rd = builder.
createMul(loc, r, d);
536 mlir::Value tmp = builder.
createAdd(loc,
c, rd);
538 mlir::Value br = builder.
createMul(loc,
b, r);
539 mlir::Value abr = builder.
createAdd(loc, a, br);
540 mlir::Value e = builder.
createDiv(loc, abr, tmp);
542 mlir::Value ar = builder.
createMul(loc, a, r);
543 mlir::Value bar = builder.
createSub(loc,
b, ar);
544 mlir::Value f = builder.
createDiv(loc, bar, tmp);
550 auto falseBranchBuilder = [&](mlir::OpBuilder &, mlir::Location) {
552 mlir::Value rc = builder.
createMul(loc, r,
c);
553 mlir::Value tmp = builder.
createAdd(loc, d, rc);
555 mlir::Value ar = builder.
createMul(loc, a, r);
556 mlir::Value arb = builder.
createAdd(loc, ar,
b);
557 mlir::Value e = builder.
createDiv(loc, arb, tmp);
559 mlir::Value br = builder.
createMul(loc,
b, r);
560 mlir::Value bra = builder.
createSub(loc, br, a);
561 mlir::Value f = builder.
createDiv(loc, bra, tmp);
567 auto cFabs = cir::FAbsOp::create(builder, loc,
c);
568 auto dFabs = cir::FAbsOp::create(builder, loc, d);
569 cir::CmpOp cmpResult =
570 builder.
createCompare(loc, cir::CmpOpKind::ge, cFabs, dFabs);
571 auto ternary = cir::TernaryOp::create(builder, loc, cmpResult,
572 trueBranchBuilder, falseBranchBuilder);
574 return ternary.getResult();
581 auto getHigherPrecisionFPType = [&context](mlir::Type type) -> mlir::Type {
582 if (mlir::isa<cir::FP16Type>(type))
583 return cir::SingleType::get(&context);
585 if (mlir::isa<cir::SingleType>(type) || mlir::isa<cir::BF16Type>(type))
586 return cir::DoubleType::get(&context);
588 if (mlir::isa<cir::DoubleType>(type))
589 return cir::LongDoubleType::get(&context, type);
594 auto getFloatTypeSemantics =
595 [&cc](mlir::Type type) ->
const llvm::fltSemantics & {
597 if (mlir::isa<cir::FP16Type>(type))
600 if (mlir::isa<cir::BF16Type>(type))
603 if (mlir::isa<cir::SingleType>(type))
606 if (mlir::isa<cir::DoubleType>(type))
609 if (mlir::isa<cir::LongDoubleType>(type)) {
611 llvm_unreachable(
"NYI Float type semantics with OpenMP");
615 if (mlir::isa<cir::FP128Type>(type)) {
617 llvm_unreachable(
"NYI Float type semantics with OpenMP");
621 llvm_unreachable(
"Unsupported float type semantics");
624 const mlir::Type higherElementType = getHigherPrecisionFPType(elementType);
625 const llvm::fltSemantics &elementTypeSemantics =
626 getFloatTypeSemantics(elementType);
627 const llvm::fltSemantics &higherElementTypeSemantics =
628 getFloatTypeSemantics(higherElementType);
637 if (llvm::APFloat::semanticsMaxExponent(elementTypeSemantics) * 2 + 1 <=
638 llvm::APFloat::semanticsMaxExponent(higherElementTypeSemantics)) {
639 return higherElementType;
649 mlir::Location loc, cir::ComplexDivOp op, mlir::Value lhsReal,
650 mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag,
652 cir::ComplexType complexTy = op.getType();
653 if (mlir::isa<cir::FPTypeInterface>(complexTy.getElementType())) {
654 cir::ComplexRangeKind range = op.getRange();
655 if (range == cir::ComplexRangeKind::Improved)
659 if (range == cir::ComplexRangeKind::Full)
661 loc, complexTy, lhsReal, lhsImag, rhsReal,
664 if (range == cir::ComplexRangeKind::Promoted) {
665 mlir::Type originalElementType = complexTy.getElementType();
666 mlir::Type higherPrecisionElementType =
668 originalElementType);
670 if (!higherPrecisionElementType)
674 cir::CastKind floatingCastKind = cir::CastKind::floating;
675 lhsReal = builder.
createCast(floatingCastKind, lhsReal,
676 higherPrecisionElementType);
677 lhsImag = builder.
createCast(floatingCastKind, lhsImag,
678 higherPrecisionElementType);
679 rhsReal = builder.
createCast(floatingCastKind, rhsReal,
680 higherPrecisionElementType);
681 rhsImag = builder.
createCast(floatingCastKind, rhsImag,
682 higherPrecisionElementType);
685 builder, loc, lhsReal, lhsImag, rhsReal, rhsImag);
690 mlir::Value finalReal =
691 builder.
createCast(floatingCastKind, resultReal, originalElementType);
692 mlir::Value finalImag =
693 builder.
createCast(floatingCastKind, resultImag, originalElementType);
702void LoweringPreparePass::lowerComplexDivOp(cir::ComplexDivOp op) {
703 cir::CIRBaseBuilderTy builder(getContext());
704 builder.setInsertionPointAfter(op);
705 mlir::Location loc = op.getLoc();
706 mlir::TypedValue<cir::ComplexType> lhs = op.getLhs();
707 mlir::TypedValue<cir::ComplexType> rhs = op.getRhs();
713 mlir::Value loweredResult =
715 rhsImag, getContext(), *astCtx);
716 op.replaceAllUsesWith(loweredResult);
720static llvm::StringRef
723 case llvm::APFloat::S_IEEEhalf:
725 case llvm::APFloat::S_IEEEsingle:
727 case llvm::APFloat::S_IEEEdouble:
729 case llvm::APFloat::S_PPCDoubleDouble:
731 case llvm::APFloat::S_x87DoubleExtended:
733 case llvm::APFloat::S_IEEEquad:
736 llvm_unreachable(
"unsupported floating point type");
742 mlir::Location loc, cir::ComplexMulOp op,
743 mlir::Value lhsReal, mlir::Value lhsImag,
744 mlir::Value rhsReal, mlir::Value rhsImag) {
746 mlir::Value resultRealLhs = builder.
createMul(loc, lhsReal, rhsReal);
747 mlir::Value resultRealRhs = builder.
createMul(loc, lhsImag, rhsImag);
748 mlir::Value resultImagLhs = builder.
createMul(loc, lhsReal, rhsImag);
749 mlir::Value resultImagRhs = builder.
createMul(loc, lhsImag, rhsReal);
750 mlir::Value resultReal = builder.
createSub(loc, resultRealLhs, resultRealRhs);
751 mlir::Value resultImag = builder.
createAdd(loc, resultImagLhs, resultImagRhs);
752 mlir::Value algebraicResult =
755 cir::ComplexType complexTy = op.getType();
756 cir::ComplexRangeKind rangeKind = op.getRange();
757 if (mlir::isa<cir::IntType>(complexTy.getElementType()) ||
758 rangeKind == cir::ComplexRangeKind::Basic ||
759 rangeKind == cir::ComplexRangeKind::Improved ||
760 rangeKind == cir::ComplexRangeKind::Promoted)
761 return algebraicResult;
768 mlir::Value resultRealIsNaN = builder.
createIsNaN(loc, resultReal);
769 mlir::Value resultImagIsNaN = builder.
createIsNaN(loc, resultImag);
770 mlir::Value resultRealAndImagAreNaN =
773 return cir::TernaryOp::create(
774 builder, loc, resultRealAndImagAreNaN,
775 [&](mlir::OpBuilder &, mlir::Location) {
778 lhsReal, lhsImag, rhsReal, rhsImag);
781 [&](mlir::OpBuilder &, mlir::Location) {
787void LoweringPreparePass::lowerComplexMulOp(cir::ComplexMulOp op) {
788 cir::CIRBaseBuilderTy builder(getContext());
789 builder.setInsertionPointAfter(op);
790 mlir::Location loc = op.getLoc();
791 mlir::TypedValue<cir::ComplexType> lhs = op.getLhs();
792 mlir::TypedValue<cir::ComplexType> rhs = op.getRhs();
797 mlir::Value loweredResult =
lowerComplexMul(*
this, builder, loc, op, lhsReal,
798 lhsImag, rhsReal, rhsImag);
799 op.replaceAllUsesWith(loweredResult);
803void LoweringPreparePass::lowerUnaryOp(cir::UnaryOpInterface op) {
804 if (!mlir::isa<cir::ComplexType>(op.getResult().getType()))
807 mlir::Location loc = op->getLoc();
808 CIRBaseBuilderTy builder(getContext());
809 builder.setInsertionPointAfter(op);
811 mlir::Value operand = op.getInput();
815 mlir::Value resultReal = operandReal;
816 mlir::Value resultImag = operandImag;
818 llvm::TypeSwitch<mlir::Operation *>(op)
820 [&](
auto) { resultReal = builder.
createInc(loc, operandReal); })
822 [&](
auto) { resultReal = builder.
createDec(loc, operandReal); })
823 .Case<cir::MinusOp>([&](
auto) {
824 resultReal = builder.
createMinus(loc, operandReal);
825 resultImag = builder.
createMinus(loc, operandImag);
828 [&](
auto) { resultImag = builder.
createMinus(loc, operandImag); })
829 .
Default([](
auto) { llvm_unreachable(
"unhandled unary complex op"); });
832 op->replaceAllUsesWith(mlir::ValueRange{result});
836cir::FuncOp LoweringPreparePass::getOrCreateDtorFunc(CIRBaseBuilderTy &builder,
838 mlir::Region &dtorRegion,
839 cir::CallOp &dtorCall) {
840 mlir::OpBuilder::InsertionGuard guard(builder);
844 cir::VoidType voidTy = builder.
getVoidTy();
845 auto voidPtrTy = cir::PointerType::get(voidTy);
848 mlir::Block &dtorBlock = dtorRegion.front();
852 auto opIt = dtorBlock.getOperations().begin();
853 cir::GetGlobalOp ggop = mlir::cast<cir::GetGlobalOp>(*opIt);
864 if (dtorBlock.getOperations().size() == 3) {
865 auto callOp = mlir::dyn_cast<cir::CallOp>(&*(++opIt));
866 auto yieldOp = mlir::dyn_cast<cir::YieldOp>(&*(++opIt));
867 if (yieldOp && callOp && callOp.getNumOperands() == 1 &&
868 callOp.getArgOperand(0) == ggop) {
877 builder.setInsertionPointAfter(op);
878 SmallString<256> fnName(
"__cxx_global_array_dtor");
879 uint32_t cnt = dynamicInitializerNames[fnName]++;
881 fnName +=
"." + std::to_string(cnt);
884 auto fnType = cir::FuncType::get({voidPtrTy}, voidTy);
885 cir::FuncOp dtorFunc =
886 buildRuntimeFunction(builder, fnName, op.getLoc(), fnType,
887 cir::GlobalLinkageKind::InternalLinkage);
888 mlir::Block *entryBB = dtorFunc.addEntryBlock();
891 entryBB->getOperations().splice(entryBB->begin(), dtorBlock.getOperations(),
892 dtorBlock.begin(), dtorBlock.end());
895 cir::GetGlobalOp dtorGGop =
896 mlir::cast<cir::GetGlobalOp>(entryBB->getOperations().front());
897 builder.setInsertionPointToStart(&dtorBlock);
898 builder.clone(*dtorGGop.getOperation());
902 mlir::Value dtorArg = entryBB->getArgument(0);
903 dtorGGop.replaceAllUsesWith(dtorArg);
907 mlir::Block &finalBlock = dtorFunc.getBody().back();
908 auto yieldOp = cast<cir::YieldOp>(finalBlock.getTerminator());
909 builder.setInsertionPoint(yieldOp);
910 cir::ReturnOp::create(builder, yieldOp->getLoc());
915 cir::GetGlobalOp origGGop =
916 mlir::cast<cir::GetGlobalOp>(dtorBlock.getOperations().front());
917 builder.setInsertionPointAfter(origGGop);
918 mlir::Value ggopResult = origGGop.getResult();
919 dtorCall = builder.
createCallOp(op.getLoc(), dtorFunc, ggopResult);
922 auto finalYield = cir::YieldOp::create(builder, op.getLoc());
925 dtorBlock.getOperations().erase(std::next(mlir::Block::iterator(finalYield)),
927 dtorRegion.getBlocks().erase(std::next(dtorRegion.begin()), dtorRegion.end());
933LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(cir::GlobalOp op) {
936 SmallString<256> fnName(
"__cxx_global_var_init");
938 uint32_t cnt = dynamicInitializerNames[fnName]++;
940 fnName +=
"." + std::to_string(cnt);
943 CIRBaseBuilderTy builder(getContext());
944 builder.setInsertionPointAfter(op);
945 cir::VoidType voidTy = builder.
getVoidTy();
946 auto fnType = cir::FuncType::get({}, voidTy);
947 FuncOp f = buildRuntimeFunction(builder, fnName, op.getLoc(), fnType,
948 cir::GlobalLinkageKind::InternalLinkage);
951 mlir::Block *entryBB = f.addEntryBlock();
952 if (!op.getCtorRegion().empty()) {
953 mlir::Block &block = op.getCtorRegion().front();
954 entryBB->getOperations().splice(entryBB->begin(), block.getOperations(),
955 block.begin(), std::prev(block.end()));
959 mlir::Region &dtorRegion = op.getDtorRegion();
960 if (!dtorRegion.empty()) {
965 builder.setInsertionPointToStart(&mlirModule.getBodyRegion().front());
966 cir::GlobalOp handle = buildRuntimeVariable(
967 builder,
"__dso_handle", op.getLoc(), builder.getI8Type(),
968 cir::GlobalLinkageKind::ExternalLinkage, cir::VisibilityKind::Hidden);
974 cir::CallOp dtorCall;
975 cir::FuncOp dtorFunc =
976 getOrCreateDtorFunc(builder, op, dtorRegion, dtorCall);
980 auto voidPtrTy = cir::PointerType::get(voidTy);
981 auto voidFnTy = cir::FuncType::get({voidPtrTy}, voidTy);
982 auto voidFnPtrTy = cir::PointerType::get(voidFnTy);
983 auto handlePtrTy = cir::PointerType::get(handle.getSymType());
985 cir::FuncType::get({voidFnPtrTy, voidPtrTy, handlePtrTy}, voidTy);
986 const char *nameAtExit =
"__cxa_atexit";
987 cir::FuncOp fnAtExit =
988 buildRuntimeFunction(builder, nameAtExit, op.getLoc(), fnAtExitType);
992 builder.setInsertionPointAfter(dtorCall);
994 auto dtorPtrTy = cir::PointerType::get(dtorFunc.getFunctionType());
996 args[0] = cir::GetGlobalOp::create(builder, dtorCall.getLoc(), dtorPtrTy,
997 dtorFunc.getSymName());
998 args[0] = cir::CastOp::create(builder, dtorCall.getLoc(), voidFnPtrTy,
999 cir::CastKind::bitcast, args[0]);
1001 cir::CastOp::create(builder, dtorCall.getLoc(), voidPtrTy,
1002 cir::CastKind::bitcast, dtorCall.getArgOperand(0));
1003 args[2] = cir::GetGlobalOp::create(builder, handle.getLoc(), handlePtrTy,
1004 handle.getSymName());
1005 builder.
createCallOp(dtorCall.getLoc(), fnAtExit, args);
1007 mlir::Block &dtorBlock = dtorRegion.front();
1008 entryBB->getOperations().splice(entryBB->end(), dtorBlock.getOperations(),
1010 std::prev(dtorBlock.end()));
1014 builder.setInsertionPointToEnd(entryBB);
1015 mlir::Operation *yieldOp =
nullptr;
1016 if (!op.getCtorRegion().empty()) {
1017 mlir::Block &block = op.getCtorRegion().front();
1018 yieldOp = &block.getOperations().back();
1020 assert(!dtorRegion.empty());
1021 mlir::Block &block = dtorRegion.front();
1022 yieldOp = &block.getOperations().back();
1025 assert(isa<cir::YieldOp>(*yieldOp));
1026 cir::ReturnOp::create(builder, yieldOp->getLoc());
1031LoweringPreparePass::getGuardAcquireFn(cir::PointerType guardPtrTy) {
1033 CIRBaseBuilderTy builder(getContext());
1034 mlir::OpBuilder::InsertionGuard ipGuard{builder};
1035 builder.setInsertionPointToStart(mlirModule.getBody());
1036 mlir::Location loc = mlirModule.getLoc();
1037 cir::IntType intTy = cir::IntType::get(&getContext(), 32,
true);
1038 auto fnType = cir::FuncType::get({guardPtrTy}, intTy);
1039 return buildRuntimeFunction(builder,
"__cxa_guard_acquire", loc, fnType);
1043LoweringPreparePass::getGuardReleaseFn(cir::PointerType guardPtrTy) {
1045 CIRBaseBuilderTy builder(getContext());
1046 mlir::OpBuilder::InsertionGuard ipGuard{builder};
1047 builder.setInsertionPointToStart(mlirModule.getBody());
1048 mlir::Location loc = mlirModule.getLoc();
1049 cir::VoidType voidTy = cir::VoidType::get(&getContext());
1050 auto fnType = cir::FuncType::get({guardPtrTy}, voidTy);
1051 return buildRuntimeFunction(builder,
"__cxa_guard_release", loc, fnType);
1054cir::GlobalOp LoweringPreparePass::createGuardGlobalOp(
1055 CIRBaseBuilderTy &builder, mlir::Location loc, llvm::StringRef name,
1056 cir::IntType guardTy, cir::GlobalLinkageKind linkage) {
1057 mlir::OpBuilder::InsertionGuard guard(builder);
1058 builder.setInsertionPointToStart(mlirModule.getBody());
1059 cir::GlobalOp g = cir::GlobalOp::create(builder, loc, name, guardTy);
1061 cir::GlobalLinkageKindAttr::get(builder.getContext(), linkage));
1062 mlir::SymbolTable::setSymbolVisibility(
1063 g, mlir::SymbolTable::Visibility::Private);
1067void LoweringPreparePass::handleStaticLocal(cir::GlobalOp globalOp,
1068 cir::GetGlobalOp getGlobalOp) {
1069 CIRBaseBuilderTy builder(getContext());
1071 std::optional<cir::ASTVarDeclInterface> astOption = globalOp.getAst();
1072 assert(astOption.has_value());
1073 cir::ASTVarDeclInterface
varDecl = astOption.value();
1075 builder.setInsertionPointAfter(getGlobalOp);
1076 mlir::Block *getGlobalOpBlock = builder.getInsertionBlock();
1079 mlir::Operation *ret = getGlobalOpBlock->getTerminator();
1081 builder.setInsertionPointAfter(getGlobalOp);
1085 bool nonTemplateInline =
1091 if (nonTemplateInline) {
1092 globalOp->emitError(
1093 "NYI: guarded initialization for inline namespace-scope variables");
1100 bool threadsafe = astCtx->
getLangOpts().ThreadsafeStatics &&
1101 (
varDecl.isLocalVarDecl() || nonTemplateInline) &&
1106 globalOp->emitError(
"NYI: guarded initialization for thread-local statics");
1112 bool useInt8GuardVariable = !threadsafe && globalOp.hasInternalLinkage();
1113 if (useInt8GuardVariable) {
1114 globalOp->emitError(
"NYI: int8 guard variables for non-threadsafe statics");
1120 if (useARMGuardVarABI()) {
1121 globalOp->emitError(
"NYI: ARM-style guard variables for static locals");
1124 cir::IntType guardTy =
1125 cir::IntType::get(&getContext(), 64,
true);
1126 cir::CIRDataLayout dataLayout(mlirModule);
1127 clang::CharUnits guardAlignment =
1129 auto guardPtrTy = cir::PointerType::get(guardTy);
1132 cir::GlobalOp guard = getOrCreateStaticLocalDeclGuardAddress(
1133 builder, globalOp, varDecl, guardTy, guardAlignment);
1136 getGlobalOpBlock->push_back(ret);
1162 unsigned maxInlineWidthInBits =
1165 if (!threadsafe || maxInlineWidthInBits) {
1167 auto bytePtrTy = cir::PointerType::get(builder.
getSIntNTy(8));
1168 mlir::Value bytePtr = builder.
createBitcast(guardPtr, bytePtrTy);
1170 getGlobalOp.getLoc(), bytePtr, guardAlignment.
getAsAlign().value());
1179 auto loadOp = mlir::cast<cir::LoadOp>(guardLoad.getDefiningOp());
1180 loadOp.setMemOrder(cir::MemOrder::Acquire);
1181 loadOp.setSyncScope(cir::SyncScopeKind::System);
1204 if (useARMGuardVarABI()) {
1205 globalOp->emitError(
1206 "NYI: ARM-style guard variable check (bit 0 only) for static locals");
1212 getGlobalOp.getLoc(), mlir::cast<cir::IntType>(guardLoad.getType()), 0);
1213 auto needsInit = builder.
createCompare(getGlobalOp.getLoc(),
1214 cir::CmpOpKind::eq, guardLoad, zero);
1217 cir::IfOp::create(builder, globalOp.getLoc(), needsInit,
1219 [&](mlir::OpBuilder &, mlir::Location) {
1220 emitCXXGuardedInitIf(builder, globalOp, varDecl,
1221 guardPtr, guardPtrTy, threadsafe);
1226 globalOp->emitError(
"NYI: guarded init without inline atomics support");
1231 builder.getInsertionBlock()->push_back(ret);
1234void LoweringPreparePass::lowerGlobalOp(GlobalOp op) {
1236 if (op.getStaticLocalGuard())
1239 mlir::Region &ctorRegion = op.getCtorRegion();
1240 mlir::Region &dtorRegion = op.getDtorRegion();
1242 if (!ctorRegion.empty() || !dtorRegion.empty()) {
1245 cir::FuncOp f = buildCXXGlobalVarDeclInitFunc(op);
1248 ctorRegion.getBlocks().clear();
1249 dtorRegion.getBlocks().clear();
1252 dynamicInitializers.push_back(f);
1258void LoweringPreparePass::lowerThreeWayCmpOp(CmpThreeWayOp op) {
1259 CIRBaseBuilderTy builder(getContext());
1260 builder.setInsertionPointAfter(op);
1262 mlir::Location loc = op->getLoc();
1263 cir::CmpThreeWayInfoAttr cmpInfo = op.getInfo();
1272 mlir::Value transformedResult;
1273 if (cmpInfo.getOrdering() != CmpOrdering::Partial) {
1276 builder.
createCompare(loc, CmpOpKind::lt, op.getLhs(), op.getRhs());
1277 mlir::Value selectOnLt = builder.
createSelect(loc, lt, ltRes, gtRes);
1279 builder.
createCompare(loc, CmpOpKind::eq, op.getLhs(), op.getRhs());
1280 transformedResult = builder.
createSelect(loc, eq, eqRes, selectOnLt);
1284 loc, op.getType(), cmpInfo.getUnordered().value());
1287 builder.
createCompare(loc, CmpOpKind::eq, op.getLhs(), op.getRhs());
1288 mlir::Value selectOnEq = builder.
createSelect(loc, eq, eqRes, unorderedRes);
1290 builder.
createCompare(loc, CmpOpKind::gt, op.getLhs(), op.getRhs());
1291 mlir::Value selectOnGt = builder.
createSelect(loc, gt, gtRes, selectOnEq);
1293 builder.
createCompare(loc, CmpOpKind::lt, op.getLhs(), op.getRhs());
1294 transformedResult = builder.
createSelect(loc, lt, ltRes, selectOnGt);
1297 op.replaceAllUsesWith(transformedResult);
1301template <
typename AttributeTy>
1302static llvm::SmallVector<mlir::Attribute>
1306 for (
const auto &[name, priority] : list)
1307 attrs.push_back(AttributeTy::get(context, name, priority));
1311void LoweringPreparePass::buildGlobalCtorDtorList() {
1312 if (!globalCtorList.empty()) {
1313 llvm::SmallVector<mlir::Attribute> globalCtors =
1317 mlirModule->setAttr(cir::CIRDialect::getGlobalCtorsAttrName(),
1318 mlir::ArrayAttr::get(&getContext(), globalCtors));
1321 if (!globalDtorList.empty()) {
1322 llvm::SmallVector<mlir::Attribute> globalDtors =
1325 mlirModule->setAttr(cir::CIRDialect::getGlobalDtorsAttrName(),
1326 mlir::ArrayAttr::get(&getContext(), globalDtors));
1330void LoweringPreparePass::buildCXXGlobalInitFunc() {
1331 if (dynamicInitializers.empty())
1338 SmallString<256> fnName;
1346 llvm::raw_svector_ostream
out(fnName);
1347 std::unique_ptr<clang::MangleContext> mangleCtx(
1349 cast<clang::ItaniumMangleContext>(*mangleCtx)
1352 fnName +=
"_GLOBAL__sub_I_";
1356 CIRBaseBuilderTy builder(getContext());
1357 builder.setInsertionPointToEnd(&mlirModule.getBodyRegion().back());
1358 auto fnType = cir::FuncType::get({}, builder.
getVoidTy());
1360 buildRuntimeFunction(builder, fnName, mlirModule.getLoc(), fnType,
1361 cir::GlobalLinkageKind::ExternalLinkage);
1362 builder.setInsertionPointToStart(f.addEntryBlock());
1363 for (cir::FuncOp &f : dynamicInitializers)
1367 globalCtorList.emplace_back(fnName,
1368 cir::GlobalCtorAttr::getDefaultPriority());
1370 cir::ReturnOp::create(builder, f.getLoc());
1375 mlir::Operation *op, mlir::Type eltTy,
1377 mlir::Value numElements,
1378 uint64_t arrayLen,
bool isCtor) {
1380 mlir::Location loc = op->getLoc();
1381 bool isDynamic = numElements !=
nullptr;
1385 const unsigned sizeTypeSize =
1388 mlir::Value begin, end;
1390 assert(!isCtor &&
"Unexpected dynamic ctor loop");
1392 mlir::Value endOffsetVal = builder.
createSub(loc, numElements, one);
1394 end = cir::PtrStrideOp::create(builder, loc, eltTy, begin, endOffsetVal);
1398 uint64_t endOffset = isCtor ? arrayLen : arrayLen - 1;
1399 mlir::Value endOffsetVal =
1401 begin = cir::CastOp::create(builder, loc, eltTy,
1402 cir::CastKind::array_to_ptrdecay, addr);
1403 end = cir::PtrStrideOp::create(builder, loc, eltTy, begin, endOffsetVal);
1406 mlir::Value start = isCtor ? begin : end;
1407 mlir::Value stop = isCtor ? end : begin;
1413 mlir::Value isEmpty =
1414 cir::CmpOp::create(builder, loc, cir::CmpOpKind::ne, start, stop);
1415 ifOp = cir::IfOp::create(builder, loc, isEmpty,
1417 [&](mlir::OpBuilder &, mlir::Location) {});
1418 builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
1429 [&](mlir::OpBuilder &
b, mlir::Location loc) {
1430 auto currentElement = cir::LoadOp::create(
b, loc, eltTy, tmpAddr);
1431 auto cmp = cir::CmpOp::create(builder, loc, cir::CmpOpKind::ne,
1432 currentElement, stop);
1436 [&](mlir::OpBuilder &
b, mlir::Location loc) {
1437 auto currentElement = cir::LoadOp::create(
b, loc, eltTy, tmpAddr);
1442 mlir::Block *oldBlock = &op->getRegion(0).front();
1443 mlir::BlockArgument oldArg = oldBlock->getArgument(0);
1444 mlir::IRMapping map;
1445 map.map(oldArg, currentElement);
1446 for (mlir::Operation ®ionOp : *oldBlock) {
1447 if (!mlir::isa<cir::YieldOp>(®ionOp))
1448 builder.clone(regionOp, map);
1458 auto nextElement = cir::PtrStrideOp::create(builder, loc, eltTy,
1459 currentElement, stride);
1467 cir::YieldOp::create(builder, loc);
1472void LoweringPreparePass::lowerArrayDtor(cir::ArrayDtor op) {
1473 CIRBaseBuilderTy builder(getContext());
1474 builder.setInsertionPointAfter(op.getOperation());
1476 mlir::Type eltTy = op->getRegion(0).getArgument(0).getType();
1478 if (op.getNumElements()) {
1480 op.getNumElements(), 0,
1487 mlir::cast<cir::ArrayType>(op.getAddr().getType().getPointee()).getSize();
1493void LoweringPreparePass::lowerArrayCtor(cir::ArrayCtor op) {
1494 cir::CIRBaseBuilderTy builder(getContext());
1495 builder.setInsertionPointAfter(op.getOperation());
1497 mlir::Type eltTy = op->getRegion(0).getArgument(0).getType();
1500 mlir::cast<cir::ArrayType>(op.getAddr().getType().getPointee()).getSize();
1506void LoweringPreparePass::lowerTrivialCopyCall(cir::CallOp op) {
1511 std::optional<cir::CtorKind> ctorKind = funcOp.getCxxConstructorKind();
1512 if (ctorKind && *ctorKind == cir::CtorKind::Copy &&
1513 funcOp.isCxxTrivialMemberFunction()) {
1515 CIRBaseBuilderTy builder(getContext());
1516 mlir::ValueRange operands = op.getOperands();
1517 mlir::Value dest = operands[0];
1518 mlir::Value src = operands[1];
1519 builder.setInsertionPoint(op);
1525void LoweringPreparePass::lowerStoreOfConstAggregate(cir::StoreOp op) {
1527 auto constOp = op.getValue().getDefiningOp<cir::ConstantOp>();
1531 mlir::Type ty = constOp.getType();
1532 if (!mlir::isa<cir::ArrayType, cir::RecordType>(ty))
1538 auto alloca = op.getAddr().getDefiningOp<cir::AllocaOp>();
1542 mlir::TypedAttr constant = constOp.getValue();
1553 auto func = op->getParentOfType<cir::FuncOp>();
1556 llvm::StringRef funcName = func.getSymName();
1559 llvm::StringRef varName = alloca.getName();
1562 std::string
name = (
"__const." + funcName +
"." + varName).str();
1565 CIRBaseBuilderTy builder(getContext());
1568 builder.setInsertionPointToStart(mlirModule.getBody());
1572 if (!mlir::SymbolTable::lookupSymbolIn(
1573 mlirModule, mlir::StringAttr::get(&getContext(), name))) {
1574 auto gv = cir::GlobalOp::create(
1575 builder, op.getLoc(), name, ty,
1577 cir::LangAddressSpaceAttr::get(&getContext(),
1578 cir::LangAddressSpace::Default),
1579 cir::GlobalLinkageKind::PrivateLinkage);
1580 mlir::SymbolTable::setSymbolVisibility(
1581 gv, mlir::SymbolTable::Visibility::Private);
1582 gv.setInitialValueAttr(constant);
1586 builder.setInsertionPoint(op);
1588 auto ptrTy = cir::PointerType::get(ty);
1589 mlir::Value globalPtr =
1590 cir::GetGlobalOp::create(builder, op.getLoc(), ptrTy, name);
1599 if (constOp.use_empty())
1603void LoweringPreparePass::runOnOp(mlir::Operation *op) {
1604 if (
auto arrayCtor = dyn_cast<cir::ArrayCtor>(op)) {
1605 lowerArrayCtor(arrayCtor);
1606 }
else if (
auto arrayDtor = dyn_cast<cir::ArrayDtor>(op)) {
1607 lowerArrayDtor(arrayDtor);
1608 }
else if (
auto cast = mlir::dyn_cast<cir::CastOp>(op)) {
1610 }
else if (
auto complexDiv = mlir::dyn_cast<cir::ComplexDivOp>(op)) {
1611 lowerComplexDivOp(complexDiv);
1612 }
else if (
auto complexMul = mlir::dyn_cast<cir::ComplexMulOp>(op)) {
1613 lowerComplexMulOp(complexMul);
1614 }
else if (
auto glob = mlir::dyn_cast<cir::GlobalOp>(op)) {
1615 lowerGlobalOp(glob);
1616 }
else if (
auto getGlobal = mlir::dyn_cast<cir::GetGlobalOp>(op)) {
1620 if (getGlobal.getStaticLocal() &&
1621 getGlobal->getParentOfType<cir::FuncOp>()) {
1622 auto globalOp = mlir::dyn_cast_or_null<cir::GlobalOp>(
1623 mlir::SymbolTable::lookupNearestSymbolFrom(getGlobal,
1624 getGlobal.getNameAttr()));
1629 if (globalOp && globalOp.getStaticLocalGuard() &&
1630 !globalOp.getCtorRegion().empty())
1631 handleStaticLocal(globalOp, getGlobal);
1633 }
else if (
auto unaryOp = mlir::dyn_cast<cir::UnaryOpInterface>(op)) {
1634 lowerUnaryOp(unaryOp);
1635 }
else if (
auto callOp = dyn_cast<cir::CallOp>(op)) {
1636 lowerTrivialCopyCall(callOp);
1637 }
else if (
auto storeOp = dyn_cast<cir::StoreOp>(op)) {
1638 lowerStoreOfConstAggregate(storeOp);
1639 }
else if (
auto fnOp = dyn_cast<cir::FuncOp>(op)) {
1640 if (
auto globalCtor = fnOp.getGlobalCtorPriority())
1641 globalCtorList.emplace_back(fnOp.getName(), globalCtor.value());
1642 else if (
auto globalDtor = fnOp.getGlobalDtorPriority())
1643 globalDtorList.emplace_back(fnOp.getName(), globalDtor.value());
1644 }
else if (
auto threeWayCmp = dyn_cast<cir::CmpThreeWayOp>(op)) {
1645 lowerThreeWayCmpOp(threeWayCmp);
1649void LoweringPreparePass::runOnOperation() {
1650 mlir::Operation *op = getOperation();
1651 if (isa<::mlir::ModuleOp>(op))
1652 mlirModule = cast<::mlir::ModuleOp>(op);
1654 llvm::SmallVector<mlir::Operation *> opsToTransform;
1656 op->walk([&](mlir::Operation *op) {
1657 if (mlir::isa<cir::ArrayCtor, cir::ArrayDtor, cir::CastOp,
1658 cir::ComplexMulOp, cir::ComplexDivOp, cir::DynamicCastOp,
1659 cir::FuncOp, cir::CallOp, cir::GetGlobalOp, cir::GlobalOp,
1660 cir::StoreOp, cir::CmpThreeWayOp, cir::IncOp, cir::DecOp,
1661 cir::MinusOp, cir::NotOp>(op))
1662 opsToTransform.push_back(op);
1665 for (mlir::Operation *o : opsToTransform)
1668 buildCXXGlobalInitFunc();
1669 buildGlobalCtorDtorList();
1673 return std::make_unique<LoweringPreparePass>();
1676std::unique_ptr<Pass>
1678 auto pass = std::make_unique<LoweringPreparePass>();
1679 pass->setASTContext(astCtx);
1680 return std::move(pass);
Defines the clang::ASTContext interface.
static llvm::FunctionCallee getGuardReleaseFn(CodeGenModule &CGM, llvm::PointerType *GuardPtrTy)
static llvm::FunctionCallee getGuardAcquireFn(CodeGenModule &CGM, llvm::PointerType *GuardPtrTy)
static mlir::Value buildRangeReductionComplexDiv(CIRBaseBuilderTy &builder, mlir::Location loc, mlir::Value lhsReal, mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag)
static llvm::StringRef getComplexDivLibCallName(llvm::APFloat::Semantics semantics)
static llvm::SmallVector< mlir::Attribute > prepareCtorDtorAttrList(mlir::MLIRContext *context, llvm::ArrayRef< std::pair< std::string, uint32_t > > list)
static llvm::StringRef getComplexMulLibCallName(llvm::APFloat::Semantics semantics)
static mlir::Value buildComplexBinOpLibCall(LoweringPreparePass &pass, CIRBaseBuilderTy &builder, llvm::StringRef(*libFuncNameGetter)(llvm::APFloat::Semantics), mlir::Location loc, cir::ComplexType ty, mlir::Value lhsReal, mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag)
static mlir::Value lowerComplexMul(LoweringPreparePass &pass, CIRBaseBuilderTy &builder, mlir::Location loc, cir::ComplexMulOp op, mlir::Value lhsReal, mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag)
static SmallString< 128 > getTransformedFileName(mlir::ModuleOp mlirModule)
static mlir::Value lowerComplexToComplexCast(mlir::MLIRContext &ctx, cir::CastOp op, cir::CastKind scalarCastKind)
static void lowerArrayDtorCtorIntoLoop(cir::CIRBaseBuilderTy &builder, clang::ASTContext *astCtx, mlir::Operation *op, mlir::Type eltTy, mlir::Value addr, mlir::Value numElements, uint64_t arrayLen, bool isCtor)
static mlir::Value lowerComplexToScalarCast(mlir::MLIRContext &ctx, cir::CastOp op, cir::CastKind elemToBoolKind)
static mlir::Value buildAlgebraicComplexDiv(CIRBaseBuilderTy &builder, mlir::Location loc, mlir::Value lhsReal, mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag)
static cir::FuncOp getCalledFunction(cir::CallOp callOp)
Return the FuncOp called by callOp.
static mlir::Type higherPrecisionElementTypeForComplexArithmetic(mlir::MLIRContext &context, clang::ASTContext &cc, CIRBaseBuilderTy &builder, mlir::Type elementType)
static mlir::Value lowerScalarToComplexCast(mlir::MLIRContext &ctx, cir::CastOp op)
static mlir::Value lowerComplexDiv(LoweringPreparePass &pass, CIRBaseBuilderTy &builder, mlir::Location loc, cir::ComplexDivOp op, mlir::Value lhsReal, mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag, mlir::MLIRContext &mlirCx, clang::ASTContext &cc)
Defines the clang::Module class, which describes a module in the source code.
Defines various enumerations that describe declaration and type specifiers.
Defines the TargetCXXABI class, which abstracts details of the C++ ABI that we're targeting.
__device__ __2f16 float c
mlir::Value createDiv(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createDec(mlir::Location loc, mlir::Value input, bool nsw=false)
mlir::Value createLogicalOr(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowBehavior ob=OverflowBehavior::None)
cir::ConditionOp createCondition(mlir::Value condition)
Create a loop condition.
mlir::Value createInc(mlir::Location loc, mlir::Value input, bool nsw=false)
cir::VoidType getVoidTy()
cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc)
mlir::Value createCast(mlir::Location loc, cir::CastKind kind, mlir::Value src, mlir::Type newTy)
mlir::Value createAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowBehavior ob=OverflowBehavior::None)
cir::PointerType getPointerTo(mlir::Type ty)
mlir::Value createComplexImag(mlir::Location loc, mlir::Value operand)
cir::DoWhileOp createDoWhile(mlir::Location loc, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> condBuilder, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> bodyBuilder)
Create a do-while operation.
cir::CopyOp createCopy(mlir::Value dst, mlir::Value src, bool isVolatile=false)
Create a copy with inferred length.
mlir::Value getSignedInt(mlir::Location loc, int64_t val, unsigned numBits)
mlir::Value createBitcast(mlir::Value src, mlir::Type newTy)
mlir::Value createGetGlobal(mlir::Location loc, cir::GlobalOp global, bool threadLocal=false)
cir::CmpOp createCompare(mlir::Location loc, cir::CmpOpKind kind, mlir::Value lhs, mlir::Value rhs)
mlir::IntegerAttr getAlignmentAttr(clang::CharUnits alignment)
mlir::Value createSelect(mlir::Location loc, mlir::Value condition, mlir::Value trueValue, mlir::Value falseValue)
mlir::Value createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowBehavior ob=OverflowBehavior::None)
mlir::Value createMinus(mlir::Location loc, mlir::Value input, bool nsw=false)
cir::ConstantOp getConstantInt(mlir::Location loc, mlir::Type ty, int64_t value)
mlir::Value createComplexCreate(mlir::Location loc, mlir::Value real, mlir::Value imag)
mlir::Value createIsNaN(mlir::Location loc, mlir::Value operand)
cir::IntType getSIntNTy(int n)
mlir::Value createAlignedLoad(mlir::Location loc, mlir::Value ptr, uint64_t alignment)
cir::CallOp createCallOp(mlir::Location loc, mlir::SymbolRefAttr callee, mlir::Type returnType, mlir::ValueRange operands, llvm::ArrayRef< mlir::NamedAttribute > attrs={}, llvm::ArrayRef< mlir::NamedAttrList > argAttrs={}, llvm::ArrayRef< mlir::NamedAttribute > resAttrs={})
cir::StoreOp createStore(mlir::Location loc, mlir::Value val, mlir::Value dst, bool isVolatile=false, mlir::IntegerAttr align={}, cir::SyncScopeKindAttr scope={}, cir::MemOrderAttr order={})
cir::YieldOp createYield(mlir::Location loc, mlir::ValueRange value={})
Create a yield operation.
mlir::Value createLogicalAnd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType, mlir::Type type, llvm::StringRef name, mlir::IntegerAttr alignment, mlir::Value dynAllocSize)
cir::BoolType getBoolTy()
mlir::Value getUnsignedInt(mlir::Location loc, uint64_t val, unsigned numBits)
mlir::Value createComplexReal(mlir::Location loc, mlir::Value operand)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
MangleContext * createMangleContext(const TargetInfo *T=nullptr)
If T is null pointer, assume the target in ASTContext.
const LangOptions & getLangOpts() const
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
const TargetInfo & getTargetInfo() const
QualType getSignedSizeType() const
Return the unique signed counterpart of the integer type corresponding to size_t.
Module * getCurrentNamedModule() const
Get module under construction, nullptr if this is not a C++20 module.
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
bool isModuleImplementation() const
Is this a module implementation.
Exposes information about the current target.
unsigned getMaxAtomicInlineWidth() const
Return the maximum width lock-free atomic operation which can be inlined given the supported features...
const llvm::fltSemantics & getDoubleFormat() const
const llvm::fltSemantics & getHalfFormat() const
const llvm::fltSemantics & getBFloat16Format() const
const llvm::fltSemantics & getLongDoubleFormat() const
const llvm::fltSemantics & getFloatFormat() const
const llvm::fltSemantics & getFloat128Format() const
Defines the clang::TargetInfo interface.
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
bool isTemplateInstantiation(TemplateSpecializationKind Kind)
Determine whether this template specialization kind refers to an instantiation of an entity (as oppos...
LLVM_READONLY bool isPreprocessingNumberBody(unsigned char c)
Return true if this is the body character of a C preprocessing number, which is [a-zA-Z0-9_.
std::unique_ptr< Pass > createLoweringPreparePass()
static bool opGlobalThreadLocal()
static bool guardAbortOnException()
static bool opGlobalAnnotations()
static bool opGlobalCtorPriority()
static bool shouldSplitConstantStore()
static bool shouldUseMemSetToInitialize()
static bool opFuncExtraAttrs()
static bool shouldUseBZeroPlusStoresToInitialize()
static bool fastMathFlags()
static bool astVarDeclInterface()