10#include "mlir/IR/Attributes.h"
25#include "llvm/Support/Path.h"
33#define GEN_PASS_DEF_LOWERINGPREPARE
34#include "clang/CIR/Dialect/Passes.h.inc"
38 SmallString<128> fileName;
40 if (mlirModule.getSymName())
41 fileName = llvm::sys::path::filename(mlirModule.getSymName()->str());
46 for (
size_t i = 0; i < fileName.size(); ++i) {
58 mlir::SymbolRefAttr sym = llvm::dyn_cast_if_present<mlir::SymbolRefAttr>(
59 callOp.getCallableForCallee());
62 return dyn_cast_or_null<cir::FuncOp>(
63 mlir::SymbolTable::lookupNearestSymbolFrom(callOp, sym));
67struct LoweringPreparePass
68 :
public impl::LoweringPrepareBase<LoweringPreparePass> {
69 LoweringPreparePass() =
default;
70 void runOnOperation()
override;
72 void runOnOp(mlir::Operation *op);
73 void lowerCastOp(cir::CastOp op);
74 void lowerComplexDivOp(cir::ComplexDivOp op);
75 void lowerComplexMulOp(cir::ComplexMulOp op);
76 void lowerUnaryOp(cir::UnaryOp op);
77 void lowerGlobalOp(cir::GlobalOp op);
78 void lowerArrayDtor(cir::ArrayDtor op);
79 void lowerArrayCtor(cir::ArrayCtor op);
80 void lowerTrivialCopyCall(cir::CallOp op);
81 void lowerStoreOfConstAggregate(cir::StoreOp op);
84 cir::FuncOp buildCXXGlobalVarDeclInitFunc(cir::GlobalOp op);
87 cir::FuncOp getOrCreateDtorFunc(CIRBaseBuilderTy &builder, cir::GlobalOp op,
88 mlir::Region &dtorRegion,
89 cir::CallOp &dtorCall);
92 void buildCXXGlobalInitFunc();
95 void buildGlobalCtorDtorList();
97 cir::FuncOp buildRuntimeFunction(
98 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
100 cir::GlobalLinkageKind linkage = cir::GlobalLinkageKind::ExternalLinkage);
102 cir::GlobalOp buildRuntimeVariable(
103 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
105 cir::GlobalLinkageKind linkage = cir::GlobalLinkageKind::ExternalLinkage,
106 cir::VisibilityKind visibility = cir::VisibilityKind::Default);
109 void handleStaticLocal(cir::GlobalOp globalOp, cir::GetGlobalOp getGlobalOp);
118 cir::GlobalOp createGuardGlobalOp(CIRBaseBuilderTy &builder,
119 mlir::Location loc, llvm::StringRef name,
120 cir::IntType guardTy,
121 cir::GlobalLinkageKind linkage);
124 cir::GlobalOp getStaticLocalDeclGuardAddress(llvm::StringRef globalSymName) {
125 auto it = staticLocalDeclGuardMap.find(globalSymName);
126 if (it != staticLocalDeclGuardMap.end())
132 void setStaticLocalDeclGuardAddress(llvm::StringRef globalSymName,
133 cir::GlobalOp guard) {
134 staticLocalDeclGuardMap[globalSymName] = guard;
138 cir::GlobalOp getOrCreateStaticLocalDeclGuardAddress(
139 CIRBaseBuilderTy &builder, cir::GlobalOp globalOp,
140 cir::ASTVarDeclInterface varDecl, cir::IntType guardTy,
141 clang::CharUnits guardAlignment) {
142 llvm::StringRef globalSymName = globalOp.getSymName();
143 cir::GlobalOp guard = getStaticLocalDeclGuardAddress(globalSymName);
146 llvm::StringRef guardName =
147 globalOp.getStaticLocalGuard()->getName().getValue();
150 guard = createGuardGlobalOp(builder, globalOp->getLoc(), guardName,
151 guardTy, globalOp.getLinkage());
152 guard.setInitialValueAttr(cir::IntAttr::get(guardTy, 0));
153 guard.setDSOLocal(globalOp.getDsoLocal());
154 guard.setAlignment(guardAlignment.
getAsAlign().value());
160 bool hasComdat = globalOp.getComdat();
161 const llvm::Triple &triple = astCtx->getTargetInfo().getTriple();
162 if (!
varDecl.isLocalVarDecl() && hasComdat &&
163 (triple.isOSBinFormatELF() || triple.isOSBinFormatWasm())) {
164 globalOp->emitError(
"NYI: guard COMDAT for non-local variables");
166 }
else if (hasComdat && globalOp.isWeakForLinker()) {
167 globalOp->emitError(
"NYI: guard COMDAT for weak linkage");
171 setStaticLocalDeclGuardAddress(globalSymName, guard);
180 clang::ASTContext *astCtx;
183 mlir::ModuleOp mlirModule;
186 llvm::StringMap<uint32_t> dynamicInitializerNames;
187 llvm::SmallVector<cir::FuncOp> dynamicInitializers;
190 llvm::StringMap<cir::GlobalOp> staticLocalDeclGuardMap;
193 llvm::SmallVector<std::pair<std::string, uint32_t>, 4> globalCtorList;
195 llvm::SmallVector<std::pair<std::string, uint32_t>, 4> globalDtorList;
199 bool useARMGuardVarABI()
const {
200 switch (astCtx->getCXXABIKind()) {
201 case clang::TargetCXXABI::GenericARM:
202 case clang::TargetCXXABI::iOS:
203 case clang::TargetCXXABI::WatchOS:
204 case clang::TargetCXXABI::GenericAArch64:
205 case clang::TargetCXXABI::WebAssembly:
215 void emitCXXGuardedInitIf(CIRBaseBuilderTy &builder, cir::GlobalOp globalOp,
216 cir::ASTVarDeclInterface varDecl,
217 mlir::Value guardPtr, cir::PointerType guardPtrTy,
219 auto loc = globalOp->getLoc();
242 mlir::Value acquireResult = acquireCall.getResult();
245 loc, mlir::cast<cir::IntType>(acquireResult.getType()), 0);
246 auto shouldInit = builder.
createCompare(loc, cir::CmpOpKind::ne,
247 acquireResult, acquireZero);
252 cir::IfOp::create(builder, loc, shouldInit,
false,
253 [](mlir::OpBuilder &, mlir::Location) {});
254 mlir::OpBuilder::InsertionGuard insertGuard(builder);
255 builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
262 auto &ctorRegion = globalOp.getCtorRegion();
263 assert(!ctorRegion.empty() &&
"This should never be empty here.");
264 if (!ctorRegion.hasOneBlock())
265 llvm_unreachable(
"Multiple blocks NYI");
266 mlir::Block &block = ctorRegion.front();
267 mlir::Block *insertBlock = builder.getInsertionBlock();
268 insertBlock->getOperations().splice(insertBlock->end(),
269 block.getOperations(), block.begin(),
270 std::prev(block.end()));
271 builder.setInsertionPointToEnd(insertBlock);
272 ctorRegion.getBlocks().clear();
280 mlir::ValueRange{guardPtr});
283 }
else if (!
varDecl.isLocalVarDecl()) {
289 globalOp->emitError(
"NYI: non-threadsafe init for non-local variables");
295 globalOp->emitError(
"NYI: non-threadsafe init for local variables");
302 void setASTContext(clang::ASTContext *
c) { astCtx =
c; }
307cir::GlobalOp LoweringPreparePass::buildRuntimeVariable(
308 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
309 mlir::Type type, cir::GlobalLinkageKind linkage,
310 cir::VisibilityKind visibility) {
311 cir::GlobalOp g = dyn_cast_or_null<cir::GlobalOp>(
312 mlir::SymbolTable::lookupNearestSymbolFrom(
313 mlirModule, mlir::StringAttr::get(mlirModule->getContext(), name)));
315 g = cir::GlobalOp::create(builder, loc, name, type);
317 cir::GlobalLinkageKindAttr::get(builder.getContext(), linkage));
318 mlir::SymbolTable::setSymbolVisibility(
319 g, mlir::SymbolTable::Visibility::Private);
320 g.setGlobalVisibilityAttr(
321 cir::VisibilityAttr::get(builder.getContext(), visibility));
326cir::FuncOp LoweringPreparePass::buildRuntimeFunction(
327 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
328 cir::FuncType type, cir::GlobalLinkageKind linkage) {
329 cir::FuncOp f = dyn_cast_or_null<FuncOp>(SymbolTable::lookupNearestSymbolFrom(
330 mlirModule, StringAttr::get(mlirModule->getContext(), name)));
332 f = cir::FuncOp::create(builder, loc, name, type);
334 cir::GlobalLinkageKindAttr::get(builder.getContext(), linkage));
335 mlir::SymbolTable::setSymbolVisibility(
336 f, mlir::SymbolTable::Visibility::Private);
346 builder.setInsertionPoint(op);
348 mlir::Value src = op.getSrc();
349 mlir::Value imag = builder.
getNullValue(src.getType(), op.getLoc());
355 cir::CastKind elemToBoolKind) {
357 builder.setInsertionPoint(op);
359 mlir::Value src = op.getSrc();
360 if (!mlir::isa<cir::BoolType>(op.getType()))
367 cir::BoolType boolTy = builder.
getBoolTy();
368 mlir::Value srcRealToBool =
369 builder.
createCast(op.getLoc(), elemToBoolKind, srcReal, boolTy);
370 mlir::Value srcImagToBool =
371 builder.
createCast(op.getLoc(), elemToBoolKind, srcImag, boolTy);
372 return builder.
createLogicalOr(op.getLoc(), srcRealToBool, srcImagToBool);
377 cir::CastKind scalarCastKind) {
379 builder.setInsertionPoint(op);
381 mlir::Value src = op.getSrc();
382 auto dstComplexElemTy =
383 mlir::cast<cir::ComplexType>(op.getType()).getElementType();
388 mlir::Value dstReal = builder.
createCast(op.getLoc(), scalarCastKind, srcReal,
390 mlir::Value dstImag = builder.
createCast(op.getLoc(), scalarCastKind, srcImag,
395void LoweringPreparePass::lowerCastOp(cir::CastOp op) {
396 mlir::MLIRContext &ctx = getContext();
397 mlir::Value loweredValue = [&]() -> mlir::Value {
398 switch (op.getKind()) {
399 case cir::CastKind::float_to_complex:
400 case cir::CastKind::int_to_complex:
402 case cir::CastKind::float_complex_to_real:
403 case cir::CastKind::int_complex_to_real:
405 case cir::CastKind::float_complex_to_bool:
407 case cir::CastKind::int_complex_to_bool:
409 case cir::CastKind::float_complex:
411 case cir::CastKind::float_complex_to_int_complex:
413 case cir::CastKind::int_complex:
415 case cir::CastKind::int_complex_to_float_complex:
423 op.replaceAllUsesWith(loweredValue);
430 llvm::StringRef (*libFuncNameGetter)(llvm::APFloat::Semantics),
431 mlir::Location loc, cir::ComplexType ty, mlir::Value lhsReal,
432 mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag) {
433 cir::FPTypeInterface elementTy =
434 mlir::cast<cir::FPTypeInterface>(ty.getElementType());
436 llvm::StringRef libFuncName = libFuncNameGetter(
437 llvm::APFloat::SemanticsToEnum(elementTy.getFloatSemantics()));
440 cir::FuncType libFuncTy = cir::FuncType::get(libFuncInputTypes, ty);
446 mlir::OpBuilder::InsertionGuard ipGuard{builder};
447 builder.setInsertionPointToStart(pass.mlirModule.getBody());
448 libFunc = pass.buildRuntimeFunction(builder, libFuncName, loc, libFuncTy);
452 builder.
createCallOp(loc, libFunc, {lhsReal, lhsImag, rhsReal, rhsImag});
453 return call.getResult();
456static llvm::StringRef
459 case llvm::APFloat::S_IEEEhalf:
461 case llvm::APFloat::S_IEEEsingle:
463 case llvm::APFloat::S_IEEEdouble:
465 case llvm::APFloat::S_PPCDoubleDouble:
467 case llvm::APFloat::S_x87DoubleExtended:
469 case llvm::APFloat::S_IEEEquad:
472 llvm_unreachable(
"unsupported floating point type");
478 mlir::Value lhsReal, mlir::Value lhsImag,
479 mlir::Value rhsReal, mlir::Value rhsImag) {
481 mlir::Value &a = lhsReal;
482 mlir::Value &
b = lhsImag;
483 mlir::Value &
c = rhsReal;
484 mlir::Value &d = rhsImag;
486 mlir::Value ac = builder.
createMul(loc, a,
c);
487 mlir::Value bd = builder.
createMul(loc,
b, d);
489 mlir::Value dd = builder.
createMul(loc, d, d);
490 mlir::Value acbd = builder.
createAdd(loc, ac, bd);
491 mlir::Value ccdd = builder.
createAdd(loc, cc, dd);
492 mlir::Value resultReal = builder.
createDiv(loc, acbd, ccdd);
495 mlir::Value ad = builder.
createMul(loc, a, d);
496 mlir::Value bcad = builder.
createSub(loc, bc, ad);
497 mlir::Value resultImag = builder.
createDiv(loc, bcad, ccdd);
503 mlir::Value lhsReal, mlir::Value lhsImag,
504 mlir::Value rhsReal, mlir::Value rhsImag) {
525 mlir::Value &a = lhsReal;
526 mlir::Value &
b = lhsImag;
527 mlir::Value &
c = rhsReal;
528 mlir::Value &d = rhsImag;
530 auto trueBranchBuilder = [&](mlir::OpBuilder &, mlir::Location) {
532 mlir::Value rd = builder.
createMul(loc, r, d);
533 mlir::Value tmp = builder.
createAdd(loc,
c, rd);
535 mlir::Value br = builder.
createMul(loc,
b, r);
536 mlir::Value abr = builder.
createAdd(loc, a, br);
537 mlir::Value e = builder.
createDiv(loc, abr, tmp);
539 mlir::Value ar = builder.
createMul(loc, a, r);
540 mlir::Value bar = builder.
createSub(loc,
b, ar);
541 mlir::Value f = builder.
createDiv(loc, bar, tmp);
547 auto falseBranchBuilder = [&](mlir::OpBuilder &, mlir::Location) {
549 mlir::Value rc = builder.
createMul(loc, r,
c);
550 mlir::Value tmp = builder.
createAdd(loc, d, rc);
552 mlir::Value ar = builder.
createMul(loc, a, r);
553 mlir::Value arb = builder.
createAdd(loc, ar,
b);
554 mlir::Value e = builder.
createDiv(loc, arb, tmp);
556 mlir::Value br = builder.
createMul(loc,
b, r);
557 mlir::Value bra = builder.
createSub(loc, br, a);
558 mlir::Value f = builder.
createDiv(loc, bra, tmp);
564 auto cFabs = cir::FAbsOp::create(builder, loc,
c);
565 auto dFabs = cir::FAbsOp::create(builder, loc, d);
566 cir::CmpOp cmpResult =
567 builder.
createCompare(loc, cir::CmpOpKind::ge, cFabs, dFabs);
568 auto ternary = cir::TernaryOp::create(builder, loc, cmpResult,
569 trueBranchBuilder, falseBranchBuilder);
571 return ternary.getResult();
578 auto getHigherPrecisionFPType = [&context](mlir::Type type) -> mlir::Type {
579 if (mlir::isa<cir::FP16Type>(type))
580 return cir::SingleType::get(&context);
582 if (mlir::isa<cir::SingleType>(type) || mlir::isa<cir::BF16Type>(type))
583 return cir::DoubleType::get(&context);
585 if (mlir::isa<cir::DoubleType>(type))
586 return cir::LongDoubleType::get(&context, type);
591 auto getFloatTypeSemantics =
592 [&cc](mlir::Type type) ->
const llvm::fltSemantics & {
594 if (mlir::isa<cir::FP16Type>(type))
597 if (mlir::isa<cir::BF16Type>(type))
600 if (mlir::isa<cir::SingleType>(type))
603 if (mlir::isa<cir::DoubleType>(type))
606 if (mlir::isa<cir::LongDoubleType>(type)) {
608 llvm_unreachable(
"NYI Float type semantics with OpenMP");
612 if (mlir::isa<cir::FP128Type>(type)) {
614 llvm_unreachable(
"NYI Float type semantics with OpenMP");
618 llvm_unreachable(
"Unsupported float type semantics");
621 const mlir::Type higherElementType = getHigherPrecisionFPType(elementType);
622 const llvm::fltSemantics &elementTypeSemantics =
623 getFloatTypeSemantics(elementType);
624 const llvm::fltSemantics &higherElementTypeSemantics =
625 getFloatTypeSemantics(higherElementType);
634 if (llvm::APFloat::semanticsMaxExponent(elementTypeSemantics) * 2 + 1 <=
635 llvm::APFloat::semanticsMaxExponent(higherElementTypeSemantics)) {
636 return higherElementType;
646 mlir::Location loc, cir::ComplexDivOp op, mlir::Value lhsReal,
647 mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag,
649 cir::ComplexType complexTy = op.getType();
650 if (mlir::isa<cir::FPTypeInterface>(complexTy.getElementType())) {
651 cir::ComplexRangeKind range = op.getRange();
652 if (range == cir::ComplexRangeKind::Improved)
656 if (range == cir::ComplexRangeKind::Full)
658 loc, complexTy, lhsReal, lhsImag, rhsReal,
661 if (range == cir::ComplexRangeKind::Promoted) {
662 mlir::Type originalElementType = complexTy.getElementType();
663 mlir::Type higherPrecisionElementType =
665 originalElementType);
667 if (!higherPrecisionElementType)
671 cir::CastKind floatingCastKind = cir::CastKind::floating;
672 lhsReal = builder.
createCast(floatingCastKind, lhsReal,
673 higherPrecisionElementType);
674 lhsImag = builder.
createCast(floatingCastKind, lhsImag,
675 higherPrecisionElementType);
676 rhsReal = builder.
createCast(floatingCastKind, rhsReal,
677 higherPrecisionElementType);
678 rhsImag = builder.
createCast(floatingCastKind, rhsImag,
679 higherPrecisionElementType);
682 builder, loc, lhsReal, lhsImag, rhsReal, rhsImag);
687 mlir::Value finalReal =
688 builder.
createCast(floatingCastKind, resultReal, originalElementType);
689 mlir::Value finalImag =
690 builder.
createCast(floatingCastKind, resultImag, originalElementType);
699void LoweringPreparePass::lowerComplexDivOp(cir::ComplexDivOp op) {
700 cir::CIRBaseBuilderTy builder(getContext());
701 builder.setInsertionPointAfter(op);
702 mlir::Location loc = op.getLoc();
703 mlir::TypedValue<cir::ComplexType> lhs = op.getLhs();
704 mlir::TypedValue<cir::ComplexType> rhs = op.getRhs();
710 mlir::Value loweredResult =
712 rhsImag, getContext(), *astCtx);
713 op.replaceAllUsesWith(loweredResult);
717static llvm::StringRef
720 case llvm::APFloat::S_IEEEhalf:
722 case llvm::APFloat::S_IEEEsingle:
724 case llvm::APFloat::S_IEEEdouble:
726 case llvm::APFloat::S_PPCDoubleDouble:
728 case llvm::APFloat::S_x87DoubleExtended:
730 case llvm::APFloat::S_IEEEquad:
733 llvm_unreachable(
"unsupported floating point type");
739 mlir::Location loc, cir::ComplexMulOp op,
740 mlir::Value lhsReal, mlir::Value lhsImag,
741 mlir::Value rhsReal, mlir::Value rhsImag) {
743 mlir::Value resultRealLhs = builder.
createMul(loc, lhsReal, rhsReal);
744 mlir::Value resultRealRhs = builder.
createMul(loc, lhsImag, rhsImag);
745 mlir::Value resultImagLhs = builder.
createMul(loc, lhsReal, rhsImag);
746 mlir::Value resultImagRhs = builder.
createMul(loc, lhsImag, rhsReal);
747 mlir::Value resultReal = builder.
createSub(loc, resultRealLhs, resultRealRhs);
748 mlir::Value resultImag = builder.
createAdd(loc, resultImagLhs, resultImagRhs);
749 mlir::Value algebraicResult =
752 cir::ComplexType complexTy = op.getType();
753 cir::ComplexRangeKind rangeKind = op.getRange();
754 if (mlir::isa<cir::IntType>(complexTy.getElementType()) ||
755 rangeKind == cir::ComplexRangeKind::Basic ||
756 rangeKind == cir::ComplexRangeKind::Improved ||
757 rangeKind == cir::ComplexRangeKind::Promoted)
758 return algebraicResult;
765 mlir::Value resultRealIsNaN = builder.
createIsNaN(loc, resultReal);
766 mlir::Value resultImagIsNaN = builder.
createIsNaN(loc, resultImag);
767 mlir::Value resultRealAndImagAreNaN =
770 return cir::TernaryOp::create(
771 builder, loc, resultRealAndImagAreNaN,
772 [&](mlir::OpBuilder &, mlir::Location) {
775 lhsReal, lhsImag, rhsReal, rhsImag);
778 [&](mlir::OpBuilder &, mlir::Location) {
784void LoweringPreparePass::lowerComplexMulOp(cir::ComplexMulOp op) {
785 cir::CIRBaseBuilderTy builder(getContext());
786 builder.setInsertionPointAfter(op);
787 mlir::Location loc = op.getLoc();
788 mlir::TypedValue<cir::ComplexType> lhs = op.getLhs();
789 mlir::TypedValue<cir::ComplexType> rhs = op.getRhs();
794 mlir::Value loweredResult =
lowerComplexMul(*
this, builder, loc, op, lhsReal,
795 lhsImag, rhsReal, rhsImag);
796 op.replaceAllUsesWith(loweredResult);
800void LoweringPreparePass::lowerUnaryOp(cir::UnaryOp op) {
801 mlir::Type ty = op.getType();
802 if (!mlir::isa<cir::ComplexType>(ty))
805 mlir::Location loc = op.getLoc();
806 cir::UnaryOpKind opKind = op.getKind();
808 CIRBaseBuilderTy builder(getContext());
809 builder.setInsertionPointAfter(op);
811 mlir::Value operand = op.getInput();
815 mlir::Value resultReal;
816 mlir::Value resultImag;
819 case cir::UnaryOpKind::Inc:
820 case cir::UnaryOpKind::Dec:
821 resultReal = builder.
createUnaryOp(loc, opKind, operandReal);
822 resultImag = operandImag;
825 case cir::UnaryOpKind::Plus:
826 case cir::UnaryOpKind::Minus:
827 resultReal = builder.
createUnaryOp(loc, opKind, operandReal);
828 resultImag = builder.
createUnaryOp(loc, opKind, operandImag);
831 case cir::UnaryOpKind::Not:
832 resultReal = operandReal;
834 builder.
createUnaryOp(loc, cir::UnaryOpKind::Minus, operandImag);
839 op.replaceAllUsesWith(result);
843cir::FuncOp LoweringPreparePass::getOrCreateDtorFunc(CIRBaseBuilderTy &builder,
845 mlir::Region &dtorRegion,
846 cir::CallOp &dtorCall) {
847 mlir::OpBuilder::InsertionGuard guard(builder);
851 cir::VoidType voidTy = builder.
getVoidTy();
852 auto voidPtrTy = cir::PointerType::get(voidTy);
855 mlir::Block &dtorBlock = dtorRegion.front();
859 auto opIt = dtorBlock.getOperations().begin();
860 cir::GetGlobalOp ggop = mlir::cast<cir::GetGlobalOp>(*opIt);
871 if (dtorBlock.getOperations().size() == 3) {
872 auto callOp = mlir::dyn_cast<cir::CallOp>(&*(++opIt));
873 auto yieldOp = mlir::dyn_cast<cir::YieldOp>(&*(++opIt));
874 if (yieldOp && callOp && callOp.getNumOperands() == 1 &&
875 callOp.getArgOperand(0) == ggop) {
884 builder.setInsertionPointAfter(op);
885 SmallString<256> fnName(
"__cxx_global_array_dtor");
886 uint32_t cnt = dynamicInitializerNames[fnName]++;
888 fnName +=
"." + std::to_string(cnt);
891 auto fnType = cir::FuncType::get({voidPtrTy}, voidTy);
892 cir::FuncOp dtorFunc =
893 buildRuntimeFunction(builder, fnName, op.getLoc(), fnType,
894 cir::GlobalLinkageKind::InternalLinkage);
895 mlir::Block *entryBB = dtorFunc.addEntryBlock();
898 entryBB->getOperations().splice(entryBB->begin(), dtorBlock.getOperations(),
899 dtorBlock.begin(), dtorBlock.end());
902 cir::GetGlobalOp dtorGGop =
903 mlir::cast<cir::GetGlobalOp>(entryBB->getOperations().front());
904 builder.setInsertionPointToStart(&dtorBlock);
905 builder.clone(*dtorGGop.getOperation());
909 mlir::Value dtorArg = entryBB->getArgument(0);
910 dtorGGop.replaceAllUsesWith(dtorArg);
914 mlir::Block &finalBlock = dtorFunc.getBody().back();
915 auto yieldOp = cast<cir::YieldOp>(finalBlock.getTerminator());
916 builder.setInsertionPoint(yieldOp);
917 cir::ReturnOp::create(builder, yieldOp->getLoc());
922 cir::GetGlobalOp origGGop =
923 mlir::cast<cir::GetGlobalOp>(dtorBlock.getOperations().front());
924 builder.setInsertionPointAfter(origGGop);
925 mlir::Value ggopResult = origGGop.getResult();
926 dtorCall = builder.
createCallOp(op.getLoc(), dtorFunc, ggopResult);
929 auto finalYield = cir::YieldOp::create(builder, op.getLoc());
932 dtorBlock.getOperations().erase(std::next(mlir::Block::iterator(finalYield)),
934 dtorRegion.getBlocks().erase(std::next(dtorRegion.begin()), dtorRegion.end());
940LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(cir::GlobalOp op) {
943 SmallString<256> fnName(
"__cxx_global_var_init");
945 uint32_t cnt = dynamicInitializerNames[fnName]++;
947 fnName +=
"." + std::to_string(cnt);
950 CIRBaseBuilderTy builder(getContext());
951 builder.setInsertionPointAfter(op);
952 cir::VoidType voidTy = builder.
getVoidTy();
953 auto fnType = cir::FuncType::get({}, voidTy);
954 FuncOp f = buildRuntimeFunction(builder, fnName, op.getLoc(), fnType,
955 cir::GlobalLinkageKind::InternalLinkage);
958 mlir::Block *entryBB = f.addEntryBlock();
959 if (!op.getCtorRegion().empty()) {
960 mlir::Block &block = op.getCtorRegion().front();
961 entryBB->getOperations().splice(entryBB->begin(), block.getOperations(),
962 block.begin(), std::prev(block.end()));
966 mlir::Region &dtorRegion = op.getDtorRegion();
967 if (!dtorRegion.empty()) {
972 builder.setInsertionPointToStart(&mlirModule.getBodyRegion().front());
973 cir::GlobalOp handle = buildRuntimeVariable(
974 builder,
"__dso_handle", op.getLoc(), builder.getI8Type(),
975 cir::GlobalLinkageKind::ExternalLinkage, cir::VisibilityKind::Hidden);
981 cir::CallOp dtorCall;
982 cir::FuncOp dtorFunc =
983 getOrCreateDtorFunc(builder, op, dtorRegion, dtorCall);
987 auto voidPtrTy = cir::PointerType::get(voidTy);
988 auto voidFnTy = cir::FuncType::get({voidPtrTy}, voidTy);
989 auto voidFnPtrTy = cir::PointerType::get(voidFnTy);
990 auto handlePtrTy = cir::PointerType::get(handle.getSymType());
992 cir::FuncType::get({voidFnPtrTy, voidPtrTy, handlePtrTy}, voidTy);
993 const char *nameAtExit =
"__cxa_atexit";
994 cir::FuncOp fnAtExit =
995 buildRuntimeFunction(builder, nameAtExit, op.getLoc(), fnAtExitType);
999 builder.setInsertionPointAfter(dtorCall);
1000 mlir::Value args[3];
1001 auto dtorPtrTy = cir::PointerType::get(dtorFunc.getFunctionType());
1003 args[0] = cir::GetGlobalOp::create(builder, dtorCall.getLoc(), dtorPtrTy,
1004 dtorFunc.getSymName());
1005 args[0] = cir::CastOp::create(builder, dtorCall.getLoc(), voidFnPtrTy,
1006 cir::CastKind::bitcast, args[0]);
1008 cir::CastOp::create(builder, dtorCall.getLoc(), voidPtrTy,
1009 cir::CastKind::bitcast, dtorCall.getArgOperand(0));
1010 args[2] = cir::GetGlobalOp::create(builder, handle.getLoc(), handlePtrTy,
1011 handle.getSymName());
1012 builder.
createCallOp(dtorCall.getLoc(), fnAtExit, args);
1014 mlir::Block &dtorBlock = dtorRegion.front();
1015 entryBB->getOperations().splice(entryBB->end(), dtorBlock.getOperations(),
1017 std::prev(dtorBlock.end()));
1021 builder.setInsertionPointToEnd(entryBB);
1022 mlir::Operation *yieldOp =
nullptr;
1023 if (!op.getCtorRegion().empty()) {
1024 mlir::Block &block = op.getCtorRegion().front();
1025 yieldOp = &block.getOperations().back();
1027 assert(!dtorRegion.empty());
1028 mlir::Block &block = dtorRegion.front();
1029 yieldOp = &block.getOperations().back();
1032 assert(isa<cir::YieldOp>(*yieldOp));
1033 cir::ReturnOp::create(builder, yieldOp->getLoc());
1038LoweringPreparePass::getGuardAcquireFn(cir::PointerType guardPtrTy) {
1040 CIRBaseBuilderTy builder(getContext());
1041 mlir::OpBuilder::InsertionGuard ipGuard{builder};
1042 builder.setInsertionPointToStart(mlirModule.getBody());
1043 mlir::Location loc = mlirModule.getLoc();
1044 cir::IntType intTy = cir::IntType::get(&getContext(), 32,
true);
1045 auto fnType = cir::FuncType::get({guardPtrTy}, intTy);
1046 return buildRuntimeFunction(builder,
"__cxa_guard_acquire", loc, fnType);
1050LoweringPreparePass::getGuardReleaseFn(cir::PointerType guardPtrTy) {
1052 CIRBaseBuilderTy builder(getContext());
1053 mlir::OpBuilder::InsertionGuard ipGuard{builder};
1054 builder.setInsertionPointToStart(mlirModule.getBody());
1055 mlir::Location loc = mlirModule.getLoc();
1056 cir::VoidType voidTy = cir::VoidType::get(&getContext());
1057 auto fnType = cir::FuncType::get({guardPtrTy}, voidTy);
1058 return buildRuntimeFunction(builder,
"__cxa_guard_release", loc, fnType);
1061cir::GlobalOp LoweringPreparePass::createGuardGlobalOp(
1062 CIRBaseBuilderTy &builder, mlir::Location loc, llvm::StringRef name,
1063 cir::IntType guardTy, cir::GlobalLinkageKind linkage) {
1064 mlir::OpBuilder::InsertionGuard guard(builder);
1065 builder.setInsertionPointToStart(mlirModule.getBody());
1066 cir::GlobalOp g = cir::GlobalOp::create(builder, loc, name, guardTy);
1068 cir::GlobalLinkageKindAttr::get(builder.getContext(), linkage));
1069 mlir::SymbolTable::setSymbolVisibility(
1070 g, mlir::SymbolTable::Visibility::Private);
1074void LoweringPreparePass::handleStaticLocal(cir::GlobalOp globalOp,
1075 cir::GetGlobalOp getGlobalOp) {
1076 CIRBaseBuilderTy builder(getContext());
1078 std::optional<cir::ASTVarDeclInterface> astOption = globalOp.getAst();
1079 assert(astOption.has_value());
1080 cir::ASTVarDeclInterface
varDecl = astOption.value();
1082 builder.setInsertionPointAfter(getGlobalOp);
1083 mlir::Block *getGlobalOpBlock = builder.getInsertionBlock();
1086 mlir::Operation *ret = getGlobalOpBlock->getTerminator();
1088 builder.setInsertionPointAfter(getGlobalOp);
1092 bool nonTemplateInline =
1098 if (nonTemplateInline) {
1099 globalOp->emitError(
1100 "NYI: guarded initialization for inline namespace-scope variables");
1107 bool threadsafe = astCtx->
getLangOpts().ThreadsafeStatics &&
1108 (
varDecl.isLocalVarDecl() || nonTemplateInline) &&
1113 globalOp->emitError(
"NYI: guarded initialization for thread-local statics");
1119 bool useInt8GuardVariable = !threadsafe && globalOp.hasInternalLinkage();
1120 if (useInt8GuardVariable) {
1121 globalOp->emitError(
"NYI: int8 guard variables for non-threadsafe statics");
1127 if (useARMGuardVarABI()) {
1128 globalOp->emitError(
"NYI: ARM-style guard variables for static locals");
1131 cir::IntType guardTy =
1132 cir::IntType::get(&getContext(), 64,
true);
1133 cir::CIRDataLayout dataLayout(mlirModule);
1134 clang::CharUnits guardAlignment =
1136 auto guardPtrTy = cir::PointerType::get(guardTy);
1139 cir::GlobalOp guard = getOrCreateStaticLocalDeclGuardAddress(
1140 builder, globalOp, varDecl, guardTy, guardAlignment);
1143 getGlobalOpBlock->push_back(ret);
1169 unsigned maxInlineWidthInBits =
1172 if (!threadsafe || maxInlineWidthInBits) {
1174 auto bytePtrTy = cir::PointerType::get(builder.
getSIntNTy(8));
1175 mlir::Value bytePtr = builder.
createBitcast(guardPtr, bytePtrTy);
1177 getGlobalOp.getLoc(), bytePtr, guardAlignment.
getAsAlign().value());
1186 auto loadOp = mlir::cast<cir::LoadOp>(guardLoad.getDefiningOp());
1187 loadOp.setMemOrder(cir::MemOrder::Acquire);
1188 loadOp.setSyncScope(cir::SyncScopeKind::System);
1211 if (useARMGuardVarABI()) {
1212 globalOp->emitError(
1213 "NYI: ARM-style guard variable check (bit 0 only) for static locals");
1219 getGlobalOp.getLoc(), mlir::cast<cir::IntType>(guardLoad.getType()), 0);
1220 auto needsInit = builder.
createCompare(getGlobalOp.getLoc(),
1221 cir::CmpOpKind::eq, guardLoad, zero);
1224 cir::IfOp::create(builder, globalOp.getLoc(), needsInit,
1226 [&](mlir::OpBuilder &, mlir::Location) {
1227 emitCXXGuardedInitIf(builder, globalOp, varDecl,
1228 guardPtr, guardPtrTy, threadsafe);
1233 globalOp->emitError(
"NYI: guarded init without inline atomics support");
1238 builder.getInsertionBlock()->push_back(ret);
1241void LoweringPreparePass::lowerGlobalOp(GlobalOp op) {
1243 if (op.getStaticLocalGuard())
1246 mlir::Region &ctorRegion = op.getCtorRegion();
1247 mlir::Region &dtorRegion = op.getDtorRegion();
1249 if (!ctorRegion.empty() || !dtorRegion.empty()) {
1252 cir::FuncOp f = buildCXXGlobalVarDeclInitFunc(op);
1255 ctorRegion.getBlocks().clear();
1256 dtorRegion.getBlocks().clear();
1259 dynamicInitializers.push_back(f);
1265template <
typename AttributeTy>
1266static llvm::SmallVector<mlir::Attribute>
1270 for (
const auto &[name, priority] : list)
1271 attrs.push_back(AttributeTy::get(context, name, priority));
1275void LoweringPreparePass::buildGlobalCtorDtorList() {
1276 if (!globalCtorList.empty()) {
1277 llvm::SmallVector<mlir::Attribute> globalCtors =
1281 mlirModule->setAttr(cir::CIRDialect::getGlobalCtorsAttrName(),
1282 mlir::ArrayAttr::get(&getContext(), globalCtors));
1285 if (!globalDtorList.empty()) {
1286 llvm::SmallVector<mlir::Attribute> globalDtors =
1289 mlirModule->setAttr(cir::CIRDialect::getGlobalDtorsAttrName(),
1290 mlir::ArrayAttr::get(&getContext(), globalDtors));
1294void LoweringPreparePass::buildCXXGlobalInitFunc() {
1295 if (dynamicInitializers.empty())
1302 SmallString<256> fnName;
1310 llvm::raw_svector_ostream
out(fnName);
1311 std::unique_ptr<clang::MangleContext> mangleCtx(
1313 cast<clang::ItaniumMangleContext>(*mangleCtx)
1316 fnName +=
"_GLOBAL__sub_I_";
1320 CIRBaseBuilderTy builder(getContext());
1321 builder.setInsertionPointToEnd(&mlirModule.getBodyRegion().back());
1322 auto fnType = cir::FuncType::get({}, builder.
getVoidTy());
1324 buildRuntimeFunction(builder, fnName, mlirModule.getLoc(), fnType,
1325 cir::GlobalLinkageKind::ExternalLinkage);
1326 builder.setInsertionPointToStart(f.addEntryBlock());
1327 for (cir::FuncOp &f : dynamicInitializers)
1331 globalCtorList.emplace_back(fnName,
1332 cir::GlobalCtorAttr::getDefaultPriority());
1334 cir::ReturnOp::create(builder, f.getLoc());
1339 mlir::Operation *op, mlir::Type eltTy,
1340 mlir::Value arrayAddr, uint64_t arrayLen,
1343 mlir::Location loc = op->getLoc();
1347 const unsigned sizeTypeSize =
1349 uint64_t endOffset = isCtor ? arrayLen : arrayLen - 1;
1350 mlir::Value endOffsetVal =
1353 auto begin = cir::CastOp::create(builder, loc, eltTy,
1354 cir::CastKind::array_to_ptrdecay, arrayAddr);
1356 cir::PtrStrideOp::create(builder, loc, eltTy, begin, endOffsetVal);
1357 mlir::Value start = isCtor ? begin : end;
1358 mlir::Value stop = isCtor ? end : begin;
1368 [&](mlir::OpBuilder &
b, mlir::Location loc) {
1369 auto currentElement = cir::LoadOp::create(
b, loc, eltTy, tmpAddr);
1370 auto cmp = cir::CmpOp::create(builder, loc, cir::CmpOpKind::ne,
1371 currentElement, stop);
1375 [&](mlir::OpBuilder &
b, mlir::Location loc) {
1376 auto currentElement = cir::LoadOp::create(
b, loc, eltTy, tmpAddr);
1378 cir::CallOp ctorCall;
1379 op->walk([&](cir::CallOp
c) { ctorCall =
c; });
1380 assert(ctorCall &&
"expected ctor call");
1389 ctorCall->moveBefore(stride.getDefiningOp());
1390 ctorCall->setOperand(0, currentElement);
1391 auto nextElement = cir::PtrStrideOp::create(builder, loc, eltTy,
1392 currentElement, stride);
1399 op->replaceAllUsesWith(loop);
1403void LoweringPreparePass::lowerArrayDtor(cir::ArrayDtor op) {
1404 CIRBaseBuilderTy builder(getContext());
1405 builder.setInsertionPointAfter(op.getOperation());
1407 mlir::Type eltTy = op->getRegion(0).getArgument(0).getType();
1410 mlir::cast<cir::ArrayType>(op.getAddr().getType().getPointee()).getSize();
1415void LoweringPreparePass::lowerArrayCtor(cir::ArrayCtor op) {
1416 cir::CIRBaseBuilderTy builder(getContext());
1417 builder.setInsertionPointAfter(op.getOperation());
1419 mlir::Type eltTy = op->getRegion(0).getArgument(0).getType();
1422 mlir::cast<cir::ArrayType>(op.getAddr().getType().getPointee()).getSize();
1427void LoweringPreparePass::lowerTrivialCopyCall(cir::CallOp op) {
1432 std::optional<cir::CtorKind> ctorKind = funcOp.getCxxConstructorKind();
1433 if (ctorKind && *ctorKind == cir::CtorKind::Copy &&
1434 funcOp.isCxxTrivialMemberFunction()) {
1436 CIRBaseBuilderTy builder(getContext());
1437 mlir::ValueRange operands = op.getOperands();
1438 mlir::Value dest = operands[0];
1439 mlir::Value src = operands[1];
1440 builder.setInsertionPoint(op);
1446void LoweringPreparePass::lowerStoreOfConstAggregate(cir::StoreOp op) {
1448 auto constOp = op.getValue().getDefiningOp<cir::ConstantOp>();
1452 mlir::Type ty = constOp.getType();
1453 if (!mlir::isa<cir::ArrayType, cir::RecordType>(ty))
1459 auto alloca = op.getAddr().getDefiningOp<cir::AllocaOp>();
1463 mlir::TypedAttr constant = constOp.getValue();
1474 auto func = op->getParentOfType<cir::FuncOp>();
1477 llvm::StringRef funcName = func.getSymName();
1480 llvm::StringRef varName = alloca.getName();
1483 std::string
name = (
"__const." + funcName +
"." + varName).str();
1486 CIRBaseBuilderTy builder(getContext());
1489 builder.setInsertionPointToStart(mlirModule.getBody());
1493 if (!mlir::SymbolTable::lookupSymbolIn(
1494 mlirModule, mlir::StringAttr::get(&getContext(), name))) {
1495 auto gv = cir::GlobalOp::create(builder, op.getLoc(), name, ty,
1497 cir::GlobalLinkageKind::PrivateLinkage);
1498 mlir::SymbolTable::setSymbolVisibility(
1499 gv, mlir::SymbolTable::Visibility::Private);
1500 gv.setInitialValueAttr(constant);
1504 builder.setInsertionPoint(op);
1506 auto ptrTy = cir::PointerType::get(ty);
1507 mlir::Value globalPtr =
1508 cir::GetGlobalOp::create(builder, op.getLoc(), ptrTy, name);
1517 if (constOp.use_empty())
1521void LoweringPreparePass::runOnOp(mlir::Operation *op) {
1522 if (
auto arrayCtor = dyn_cast<cir::ArrayCtor>(op)) {
1523 lowerArrayCtor(arrayCtor);
1524 }
else if (
auto arrayDtor = dyn_cast<cir::ArrayDtor>(op)) {
1525 lowerArrayDtor(arrayDtor);
1526 }
else if (
auto cast = mlir::dyn_cast<cir::CastOp>(op)) {
1528 }
else if (
auto complexDiv = mlir::dyn_cast<cir::ComplexDivOp>(op)) {
1529 lowerComplexDivOp(complexDiv);
1530 }
else if (
auto complexMul = mlir::dyn_cast<cir::ComplexMulOp>(op)) {
1531 lowerComplexMulOp(complexMul);
1532 }
else if (
auto glob = mlir::dyn_cast<cir::GlobalOp>(op)) {
1533 lowerGlobalOp(glob);
1534 }
else if (
auto getGlobal = mlir::dyn_cast<cir::GetGlobalOp>(op)) {
1538 if (getGlobal.getStaticLocal() &&
1539 getGlobal->getParentOfType<cir::FuncOp>()) {
1540 auto globalOp = mlir::dyn_cast_or_null<cir::GlobalOp>(
1541 mlir::SymbolTable::lookupNearestSymbolFrom(getGlobal,
1542 getGlobal.getNameAttr()));
1547 if (globalOp && globalOp.getStaticLocalGuard() &&
1548 !globalOp.getCtorRegion().empty())
1549 handleStaticLocal(globalOp, getGlobal);
1551 }
else if (
auto unary = mlir::dyn_cast<cir::UnaryOp>(op)) {
1552 lowerUnaryOp(unary);
1553 }
else if (
auto callOp = dyn_cast<cir::CallOp>(op)) {
1554 lowerTrivialCopyCall(callOp);
1555 }
else if (
auto storeOp = dyn_cast<cir::StoreOp>(op)) {
1556 lowerStoreOfConstAggregate(storeOp);
1557 }
else if (
auto fnOp = dyn_cast<cir::FuncOp>(op)) {
1558 if (
auto globalCtor = fnOp.getGlobalCtorPriority())
1559 globalCtorList.emplace_back(fnOp.getName(), globalCtor.value());
1560 else if (
auto globalDtor = fnOp.getGlobalDtorPriority())
1561 globalDtorList.emplace_back(fnOp.getName(), globalDtor.value());
1565void LoweringPreparePass::runOnOperation() {
1566 mlir::Operation *op = getOperation();
1567 if (isa<::mlir::ModuleOp>(op))
1568 mlirModule = cast<::mlir::ModuleOp>(op);
1570 llvm::SmallVector<mlir::Operation *> opsToTransform;
1572 op->walk([&](mlir::Operation *op) {
1573 if (mlir::isa<cir::ArrayCtor, cir::ArrayDtor, cir::CastOp,
1574 cir::ComplexMulOp, cir::ComplexDivOp, cir::DynamicCastOp,
1575 cir::FuncOp, cir::CallOp, cir::GetGlobalOp, cir::GlobalOp,
1576 cir::StoreOp, cir::UnaryOp>(op))
1577 opsToTransform.push_back(op);
1580 for (mlir::Operation *o : opsToTransform)
1583 buildCXXGlobalInitFunc();
1584 buildGlobalCtorDtorList();
1588 return std::make_unique<LoweringPreparePass>();
1591std::unique_ptr<Pass>
1593 auto pass = std::make_unique<LoweringPreparePass>();
1594 pass->setASTContext(astCtx);
1595 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 void lowerArrayDtorCtorIntoLoop(cir::CIRBaseBuilderTy &builder, clang::ASTContext *astCtx, mlir::Operation *op, mlir::Type eltTy, mlir::Value arrayAddr, uint64_t arrayLen, bool isCtor)
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 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 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.
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 createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowBehavior ob=OverflowBehavior::None)
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 createUnaryOp(mlir::Location loc, cir::UnaryOpKind kind, mlir::Value operand)
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()