18#include "llvm/Support/Path.h"
26 SmallString<128> fileName;
28 if (mlirModule.getSymName())
29 fileName = llvm::sys::path::filename(mlirModule.getSymName()->str());
34 for (
size_t i = 0; i < fileName.size(); ++i) {
45struct LoweringPreparePass :
public LoweringPrepareBase<LoweringPreparePass> {
46 LoweringPreparePass() =
default;
47 void runOnOperation()
override;
49 void runOnOp(mlir::Operation *op);
50 void lowerCastOp(cir::CastOp op);
51 void lowerComplexDivOp(cir::ComplexDivOp op);
52 void lowerComplexMulOp(cir::ComplexMulOp op);
53 void lowerUnaryOp(cir::UnaryOp op);
54 void lowerGlobalOp(cir::GlobalOp op);
55 void lowerArrayDtor(cir::ArrayDtor op);
56 void lowerArrayCtor(cir::ArrayCtor op);
59 cir::FuncOp buildCXXGlobalVarDeclInitFunc(cir::GlobalOp op);
62 void buildCXXGlobalInitFunc();
65 void buildGlobalCtorDtorList();
67 cir::FuncOp buildRuntimeFunction(
68 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
70 cir::GlobalLinkageKind linkage = cir::GlobalLinkageKind::ExternalLinkage);
76 clang::ASTContext *astCtx;
79 mlir::ModuleOp mlirModule;
82 llvm::StringMap<uint32_t> dynamicInitializerNames;
83 llvm::SmallVector<cir::FuncOp> dynamicInitializers;
86 llvm::SmallVector<std::pair<std::string, uint32_t>, 4> globalCtorList;
88 void setASTContext(clang::ASTContext *
c) { astCtx =
c; }
93cir::FuncOp LoweringPreparePass::buildRuntimeFunction(
94 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
95 cir::FuncType type, cir::GlobalLinkageKind linkage) {
96 cir::FuncOp f = dyn_cast_or_null<FuncOp>(SymbolTable::lookupNearestSymbolFrom(
97 mlirModule, StringAttr::get(mlirModule->getContext(), name)));
99 f = builder.create<cir::FuncOp>(loc,
name,
type);
101 cir::GlobalLinkageKindAttr::get(builder.getContext(), linkage));
102 mlir::SymbolTable::setSymbolVisibility(
103 f, mlir::SymbolTable::Visibility::Private);
113 builder.setInsertionPoint(op);
115 mlir::Value src = op.getSrc();
116 mlir::Value imag = builder.
getNullValue(src.getType(), op.getLoc());
122 cir::CastKind elemToBoolKind) {
124 builder.setInsertionPoint(op);
126 mlir::Value src = op.getSrc();
127 if (!mlir::isa<cir::BoolType>(op.getType()))
134 cir::BoolType boolTy = builder.
getBoolTy();
135 mlir::Value srcRealToBool =
136 builder.
createCast(op.getLoc(), elemToBoolKind, srcReal, boolTy);
137 mlir::Value srcImagToBool =
138 builder.
createCast(op.getLoc(), elemToBoolKind, srcImag, boolTy);
139 return builder.
createLogicalOr(op.getLoc(), srcRealToBool, srcImagToBool);
144 cir::CastKind scalarCastKind) {
146 builder.setInsertionPoint(op);
148 mlir::Value src = op.getSrc();
149 auto dstComplexElemTy =
150 mlir::cast<cir::ComplexType>(op.getType()).getElementType();
155 mlir::Value dstReal = builder.
createCast(op.getLoc(), scalarCastKind, srcReal,
157 mlir::Value dstImag = builder.
createCast(op.getLoc(), scalarCastKind, srcImag,
162void LoweringPreparePass::lowerCastOp(cir::CastOp op) {
163 mlir::MLIRContext &ctx = getContext();
164 mlir::Value loweredValue = [&]() -> mlir::Value {
165 switch (op.getKind()) {
166 case cir::CastKind::float_to_complex:
167 case cir::CastKind::int_to_complex:
169 case cir::CastKind::float_complex_to_real:
170 case cir::CastKind::int_complex_to_real:
172 case cir::CastKind::float_complex_to_bool:
174 case cir::CastKind::int_complex_to_bool:
176 case cir::CastKind::float_complex:
178 case cir::CastKind::float_complex_to_int_complex:
180 case cir::CastKind::int_complex:
182 case cir::CastKind::int_complex_to_float_complex:
190 op.replaceAllUsesWith(loweredValue);
197 llvm::StringRef (*libFuncNameGetter)(llvm::APFloat::Semantics),
198 mlir::Location loc, cir::ComplexType ty, mlir::Value lhsReal,
199 mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag) {
200 cir::FPTypeInterface elementTy =
201 mlir::cast<cir::FPTypeInterface>(ty.getElementType());
203 llvm::StringRef libFuncName = libFuncNameGetter(
204 llvm::APFloat::SemanticsToEnum(elementTy.getFloatSemantics()));
207 cir::FuncType libFuncTy = cir::FuncType::get(libFuncInputTypes, ty);
213 mlir::OpBuilder::InsertionGuard ipGuard{builder};
214 builder.setInsertionPointToStart(pass.mlirModule.getBody());
215 libFunc = pass.buildRuntimeFunction(builder, libFuncName, loc, libFuncTy);
219 builder.
createCallOp(loc, libFunc, {lhsReal, lhsImag, rhsReal, rhsImag});
220 return call.getResult();
223static llvm::StringRef
226 case llvm::APFloat::S_IEEEhalf:
228 case llvm::APFloat::S_IEEEsingle:
230 case llvm::APFloat::S_IEEEdouble:
232 case llvm::APFloat::S_PPCDoubleDouble:
234 case llvm::APFloat::S_x87DoubleExtended:
236 case llvm::APFloat::S_IEEEquad:
239 llvm_unreachable(
"unsupported floating point type");
245 mlir::Value lhsReal, mlir::Value lhsImag,
246 mlir::Value rhsReal, mlir::Value rhsImag) {
248 mlir::Value &a = lhsReal;
249 mlir::Value &
b = lhsImag;
250 mlir::Value &
c = rhsReal;
251 mlir::Value &d = rhsImag;
253 mlir::Value ac = builder.
createBinop(loc, a, cir::BinOpKind::Mul,
c);
254 mlir::Value bd = builder.
createBinop(loc,
b, cir::BinOpKind::Mul, d);
255 mlir::Value cc = builder.
createBinop(loc,
c, cir::BinOpKind::Mul,
c);
256 mlir::Value dd = builder.
createBinop(loc, d, cir::BinOpKind::Mul, d);
258 builder.
createBinop(loc, ac, cir::BinOpKind::Add, bd);
260 builder.
createBinop(loc, cc, cir::BinOpKind::Add, dd);
261 mlir::Value resultReal =
262 builder.
createBinop(loc, acbd, cir::BinOpKind::Div, ccdd);
264 mlir::Value bc = builder.
createBinop(loc,
b, cir::BinOpKind::Mul,
c);
265 mlir::Value ad = builder.
createBinop(loc, a, cir::BinOpKind::Mul, d);
267 builder.
createBinop(loc, bc, cir::BinOpKind::Sub, ad);
268 mlir::Value resultImag =
269 builder.
createBinop(loc, bcad, cir::BinOpKind::Div, ccdd);
275 mlir::Value lhsReal, mlir::Value lhsImag,
276 mlir::Value rhsReal, mlir::Value rhsImag) {
297 mlir::Value &a = lhsReal;
298 mlir::Value &
b = lhsImag;
299 mlir::Value &
c = rhsReal;
300 mlir::Value &d = rhsImag;
302 auto trueBranchBuilder = [&](mlir::OpBuilder &, mlir::Location) {
303 mlir::Value r = builder.
createBinop(loc, d, cir::BinOpKind::Div,
305 mlir::Value rd = builder.
createBinop(loc, r, cir::BinOpKind::Mul, d);
306 mlir::Value tmp = builder.
createBinop(loc,
c, cir::BinOpKind::Add,
309 mlir::Value br = builder.
createBinop(loc,
b, cir::BinOpKind::Mul, r);
311 builder.
createBinop(loc, a, cir::BinOpKind::Add, br);
312 mlir::Value e = builder.
createBinop(loc, abr, cir::BinOpKind::Div, tmp);
314 mlir::Value ar = builder.
createBinop(loc, a, cir::BinOpKind::Mul, r);
317 mlir::Value f = builder.
createBinop(loc, bar, cir::BinOpKind::Div, tmp);
323 auto falseBranchBuilder = [&](mlir::OpBuilder &, mlir::Location) {
324 mlir::Value r = builder.
createBinop(loc,
c, cir::BinOpKind::Div,
326 mlir::Value rc = builder.
createBinop(loc, r, cir::BinOpKind::Mul,
c);
327 mlir::Value tmp = builder.
createBinop(loc, d, cir::BinOpKind::Add,
330 mlir::Value ar = builder.
createBinop(loc, a, cir::BinOpKind::Mul, r);
333 mlir::Value e = builder.
createBinop(loc, arb, cir::BinOpKind::Div, tmp);
335 mlir::Value br = builder.
createBinop(loc,
b, cir::BinOpKind::Mul, r);
337 builder.
createBinop(loc, br, cir::BinOpKind::Sub, a);
338 mlir::Value f = builder.
createBinop(loc, bra, cir::BinOpKind::Div, tmp);
344 auto cFabs = builder.create<cir::FAbsOp>(loc,
c);
345 auto dFabs = builder.create<cir::FAbsOp>(loc, d);
346 cir::CmpOp cmpResult =
347 builder.
createCompare(loc, cir::CmpOpKind::ge, cFabs, dFabs);
348 auto ternary = builder.create<cir::TernaryOp>(
349 loc, cmpResult, trueBranchBuilder, falseBranchBuilder);
351 return ternary.getResult();
358 auto getHigherPrecisionFPType = [&context](mlir::Type type) -> mlir::Type {
359 if (mlir::isa<cir::FP16Type>(type))
360 return cir::SingleType::get(&context);
362 if (mlir::isa<cir::SingleType>(type) || mlir::isa<cir::BF16Type>(type))
363 return cir::DoubleType::get(&context);
365 if (mlir::isa<cir::DoubleType>(type))
366 return cir::LongDoubleType::get(&context, type);
371 auto getFloatTypeSemantics =
372 [&cc](mlir::Type type) ->
const llvm::fltSemantics & {
374 if (mlir::isa<cir::FP16Type>(type))
377 if (mlir::isa<cir::BF16Type>(type))
380 if (mlir::isa<cir::SingleType>(type))
383 if (mlir::isa<cir::DoubleType>(type))
386 if (mlir::isa<cir::LongDoubleType>(type)) {
388 llvm_unreachable(
"NYI Float type semantics with OpenMP");
392 if (mlir::isa<cir::FP128Type>(type)) {
394 llvm_unreachable(
"NYI Float type semantics with OpenMP");
398 assert(
false &&
"Unsupported float type semantics");
401 const mlir::Type higherElementType = getHigherPrecisionFPType(elementType);
402 const llvm::fltSemantics &elementTypeSemantics =
403 getFloatTypeSemantics(elementType);
404 const llvm::fltSemantics &higherElementTypeSemantics =
405 getFloatTypeSemantics(higherElementType);
414 if (llvm::APFloat::semanticsMaxExponent(elementTypeSemantics) * 2 + 1 <=
415 llvm::APFloat::semanticsMaxExponent(higherElementTypeSemantics)) {
416 return higherElementType;
426 mlir::Location loc, cir::ComplexDivOp op, mlir::Value lhsReal,
427 mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag,
429 cir::ComplexType complexTy = op.getType();
430 if (mlir::isa<cir::FPTypeInterface>(complexTy.getElementType())) {
431 cir::ComplexRangeKind range = op.getRange();
432 if (range == cir::ComplexRangeKind::Improved)
436 if (range == cir::ComplexRangeKind::Full)
438 loc, complexTy, lhsReal, lhsImag, rhsReal,
441 if (range == cir::ComplexRangeKind::Promoted) {
442 mlir::Type originalElementType = complexTy.getElementType();
443 mlir::Type higherPrecisionElementType =
445 originalElementType);
447 if (!higherPrecisionElementType)
451 cir::CastKind floatingCastKind = cir::CastKind::floating;
452 lhsReal = builder.
createCast(floatingCastKind, lhsReal,
453 higherPrecisionElementType);
454 lhsImag = builder.
createCast(floatingCastKind, lhsImag,
455 higherPrecisionElementType);
456 rhsReal = builder.
createCast(floatingCastKind, rhsReal,
457 higherPrecisionElementType);
458 rhsImag = builder.
createCast(floatingCastKind, rhsImag,
459 higherPrecisionElementType);
462 builder, loc, lhsReal, lhsImag, rhsReal, rhsImag);
467 mlir::Value finalReal =
468 builder.
createCast(floatingCastKind, resultReal, originalElementType);
469 mlir::Value finalImag =
470 builder.
createCast(floatingCastKind, resultImag, originalElementType);
479void LoweringPreparePass::lowerComplexDivOp(cir::ComplexDivOp op) {
480 cir::CIRBaseBuilderTy builder(getContext());
481 builder.setInsertionPointAfter(op);
482 mlir::Location loc = op.getLoc();
483 mlir::TypedValue<cir::ComplexType> lhs = op.getLhs();
484 mlir::TypedValue<cir::ComplexType> rhs = op.getRhs();
490 mlir::Value loweredResult =
492 rhsImag, getContext(), *astCtx);
493 op.replaceAllUsesWith(loweredResult);
497static llvm::StringRef
500 case llvm::APFloat::S_IEEEhalf:
502 case llvm::APFloat::S_IEEEsingle:
504 case llvm::APFloat::S_IEEEdouble:
506 case llvm::APFloat::S_PPCDoubleDouble:
508 case llvm::APFloat::S_x87DoubleExtended:
510 case llvm::APFloat::S_IEEEquad:
513 llvm_unreachable(
"unsupported floating point type");
519 mlir::Location loc, cir::ComplexMulOp op,
520 mlir::Value lhsReal, mlir::Value lhsImag,
521 mlir::Value rhsReal, mlir::Value rhsImag) {
523 mlir::Value resultRealLhs =
524 builder.
createBinop(loc, lhsReal, cir::BinOpKind::Mul, rhsReal);
525 mlir::Value resultRealRhs =
526 builder.
createBinop(loc, lhsImag, cir::BinOpKind::Mul, rhsImag);
527 mlir::Value resultImagLhs =
528 builder.
createBinop(loc, lhsReal, cir::BinOpKind::Mul, rhsImag);
529 mlir::Value resultImagRhs =
530 builder.
createBinop(loc, lhsImag, cir::BinOpKind::Mul, rhsReal);
532 loc, resultRealLhs, cir::BinOpKind::Sub, resultRealRhs);
534 loc, resultImagLhs, cir::BinOpKind::Add, resultImagRhs);
535 mlir::Value algebraicResult =
538 cir::ComplexType complexTy = op.getType();
539 cir::ComplexRangeKind rangeKind = op.getRange();
540 if (mlir::isa<cir::IntType>(complexTy.getElementType()) ||
541 rangeKind == cir::ComplexRangeKind::Basic ||
542 rangeKind == cir::ComplexRangeKind::Improved ||
543 rangeKind == cir::ComplexRangeKind::Promoted)
544 return algebraicResult;
551 mlir::Value resultRealIsNaN = builder.
createIsNaN(loc, resultReal);
552 mlir::Value resultImagIsNaN = builder.
createIsNaN(loc, resultImag);
553 mlir::Value resultRealAndImagAreNaN =
557 .create<cir::TernaryOp>(
558 loc, resultRealAndImagAreNaN,
559 [&](mlir::OpBuilder &, mlir::Location) {
562 lhsReal, lhsImag, rhsReal, rhsImag);
565 [&](mlir::OpBuilder &, mlir::Location) {
571void LoweringPreparePass::lowerComplexMulOp(cir::ComplexMulOp op) {
572 cir::CIRBaseBuilderTy builder(getContext());
573 builder.setInsertionPointAfter(op);
574 mlir::Location loc = op.getLoc();
575 mlir::TypedValue<cir::ComplexType> lhs = op.getLhs();
576 mlir::TypedValue<cir::ComplexType> rhs = op.getRhs();
581 mlir::Value loweredResult =
lowerComplexMul(*
this, builder, loc, op, lhsReal,
582 lhsImag, rhsReal, rhsImag);
583 op.replaceAllUsesWith(loweredResult);
587void LoweringPreparePass::lowerUnaryOp(cir::UnaryOp op) {
588 mlir::Type ty = op.getType();
589 if (!mlir::isa<cir::ComplexType>(ty))
592 mlir::Location loc = op.getLoc();
593 cir::UnaryOpKind opKind = op.getKind();
595 CIRBaseBuilderTy builder(getContext());
596 builder.setInsertionPointAfter(op);
598 mlir::Value operand = op.getInput();
602 mlir::Value resultReal;
603 mlir::Value resultImag;
606 case cir::UnaryOpKind::Inc:
607 case cir::UnaryOpKind::Dec:
608 resultReal = builder.
createUnaryOp(loc, opKind, operandReal);
609 resultImag = operandImag;
612 case cir::UnaryOpKind::Plus:
613 case cir::UnaryOpKind::Minus:
614 resultReal = builder.
createUnaryOp(loc, opKind, operandReal);
615 resultImag = builder.
createUnaryOp(loc, opKind, operandImag);
618 case cir::UnaryOpKind::Not:
619 resultReal = operandReal;
621 builder.
createUnaryOp(loc, cir::UnaryOpKind::Minus, operandImag);
626 op.replaceAllUsesWith(result);
631LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(cir::GlobalOp op) {
634 SmallString<256> fnName(
"__cxx_global_var_init");
636 uint32_t cnt = dynamicInitializerNames[fnName]++;
638 fnName +=
"." + llvm::Twine(cnt).str();
641 CIRBaseBuilderTy builder(getContext());
642 builder.setInsertionPointAfter(op);
643 auto fnType = cir::FuncType::get({}, builder.
getVoidTy());
644 FuncOp f = buildRuntimeFunction(builder, fnName, op.getLoc(), fnType,
645 cir::GlobalLinkageKind::InternalLinkage);
648 mlir::Block *entryBB = f.addEntryBlock();
649 if (!op.getCtorRegion().empty()) {
650 mlir::Block &block = op.getCtorRegion().front();
651 entryBB->getOperations().splice(entryBB->begin(), block.getOperations(),
652 block.begin(), std::prev(block.end()));
656 mlir::Region &dtorRegion = op.getDtorRegion();
657 if (!dtorRegion.empty()) {
659 llvm_unreachable(
"dtor region lowering is NYI");
663 builder.setInsertionPointToEnd(entryBB);
664 mlir::Operation *yieldOp =
nullptr;
665 if (!op.getCtorRegion().empty()) {
666 mlir::Block &block = op.getCtorRegion().front();
667 yieldOp = &block.getOperations().back();
670 llvm_unreachable(
"dtor region lowering is NYI");
673 assert(isa<YieldOp>(*yieldOp));
674 cir::ReturnOp::create(builder, yieldOp->getLoc());
678void LoweringPreparePass::lowerGlobalOp(GlobalOp op) {
679 mlir::Region &ctorRegion = op.getCtorRegion();
680 mlir::Region &dtorRegion = op.getDtorRegion();
682 if (!ctorRegion.empty() || !dtorRegion.empty()) {
685 cir::FuncOp f = buildCXXGlobalVarDeclInitFunc(op);
688 ctorRegion.getBlocks().clear();
689 dtorRegion.getBlocks().clear();
692 dynamicInitializers.push_back(f);
698template <
typename AttributeTy>
699static llvm::SmallVector<mlir::Attribute>
703 for (
const auto &[name, priority] : list)
704 attrs.push_back(AttributeTy::get(context, name, priority));
708void LoweringPreparePass::buildGlobalCtorDtorList() {
709 if (!globalCtorList.empty()) {
710 llvm::SmallVector<mlir::Attribute> globalCtors =
714 mlirModule->setAttr(cir::CIRDialect::getGlobalCtorsAttrName(),
715 mlir::ArrayAttr::get(&getContext(), globalCtors));
721void LoweringPreparePass::buildCXXGlobalInitFunc() {
722 if (dynamicInitializers.empty())
729 SmallString<256> fnName;
737 llvm::raw_svector_ostream
out(fnName);
738 std::unique_ptr<clang::MangleContext> mangleCtx(
740 cast<clang::ItaniumMangleContext>(*mangleCtx)
743 fnName +=
"_GLOBAL__sub_I_";
747 CIRBaseBuilderTy builder(getContext());
748 builder.setInsertionPointToEnd(&mlirModule.getBodyRegion().back());
749 auto fnType = cir::FuncType::get({}, builder.
getVoidTy());
751 buildRuntimeFunction(builder, fnName, mlirModule.getLoc(), fnType,
752 cir::GlobalLinkageKind::ExternalLinkage);
753 builder.setInsertionPointToStart(f.addEntryBlock());
754 for (cir::FuncOp &f : dynamicInitializers)
758 globalCtorList.emplace_back(fnName,
759 cir::GlobalCtorAttr::getDefaultPriority());
761 cir::ReturnOp::create(builder, f.getLoc());
766 mlir::Operation *op, mlir::Type eltTy,
767 mlir::Value arrayAddr, uint64_t arrayLen,
770 mlir::Location loc = op->getLoc();
774 const unsigned sizeTypeSize =
776 uint64_t endOffset = isCtor ? arrayLen : arrayLen - 1;
777 mlir::Value endOffsetVal =
780 auto begin = cir::CastOp::create(builder, loc, eltTy,
781 cir::CastKind::array_to_ptrdecay, arrayAddr);
783 cir::PtrStrideOp::create(builder, loc, eltTy, begin, endOffsetVal);
784 mlir::Value start = isCtor ? begin : end;
785 mlir::Value stop = isCtor ? end : begin;
795 [&](mlir::OpBuilder &
b, mlir::Location loc) {
796 auto currentElement =
b.create<cir::LoadOp>(loc, eltTy, tmpAddr);
797 mlir::Type boolTy = cir::BoolType::get(
b.getContext());
798 auto cmp = builder.create<cir::CmpOp>(loc, boolTy, cir::CmpOpKind::ne,
799 currentElement, stop);
803 [&](mlir::OpBuilder &
b, mlir::Location loc) {
804 auto currentElement =
b.create<cir::LoadOp>(loc, eltTy, tmpAddr);
806 cir::CallOp ctorCall;
807 op->walk([&](cir::CallOp
c) { ctorCall =
c; });
808 assert(ctorCall &&
"expected ctor call");
817 ctorCall->moveBefore(stride.getDefiningOp());
818 ctorCall->setOperand(0, currentElement);
819 auto nextElement = cir::PtrStrideOp::create(builder, loc, eltTy,
820 currentElement, stride);
827 op->replaceAllUsesWith(loop);
831void LoweringPreparePass::lowerArrayDtor(cir::ArrayDtor op) {
832 CIRBaseBuilderTy builder(getContext());
833 builder.setInsertionPointAfter(op.getOperation());
835 mlir::Type eltTy = op->getRegion(0).getArgument(0).getType();
838 mlir::cast<cir::ArrayType>(op.getAddr().getType().getPointee()).getSize();
843void LoweringPreparePass::lowerArrayCtor(cir::ArrayCtor op) {
844 cir::CIRBaseBuilderTy builder(getContext());
845 builder.setInsertionPointAfter(op.getOperation());
847 mlir::Type eltTy = op->getRegion(0).getArgument(0).getType();
850 mlir::cast<cir::ArrayType>(op.getAddr().getType().getPointee()).getSize();
855void LoweringPreparePass::runOnOp(mlir::Operation *op) {
856 if (
auto arrayCtor = dyn_cast<ArrayCtor>(op))
857 lowerArrayCtor(arrayCtor);
858 else if (
auto arrayDtor = dyn_cast<cir::ArrayDtor>(op))
859 lowerArrayDtor(arrayDtor);
860 else if (
auto cast = mlir::dyn_cast<cir::CastOp>(op))
862 else if (
auto complexDiv = mlir::dyn_cast<cir::ComplexDivOp>(op))
863 lowerComplexDivOp(complexDiv);
864 else if (
auto complexMul = mlir::dyn_cast<cir::ComplexMulOp>(op))
865 lowerComplexMulOp(complexMul);
866 else if (
auto glob = mlir::dyn_cast<cir::GlobalOp>(op))
868 else if (
auto unary = mlir::dyn_cast<cir::UnaryOp>(op))
872void LoweringPreparePass::runOnOperation() {
873 mlir::Operation *op = getOperation();
874 if (isa<::mlir::ModuleOp>(op))
875 mlirModule = cast<::mlir::ModuleOp>(op);
877 llvm::SmallVector<mlir::Operation *> opsToTransform;
879 op->walk([&](mlir::Operation *op) {
880 if (mlir::isa<cir::ArrayCtor, cir::ArrayDtor, cir::CastOp,
881 cir::ComplexMulOp, cir::ComplexDivOp, cir::GlobalOp,
883 opsToTransform.push_back(op);
886 for (mlir::Operation *o : opsToTransform)
889 buildCXXGlobalInitFunc();
890 buildGlobalCtorDtorList();
894 return std::make_unique<LoweringPreparePass>();
899 auto pass = std::make_unique<LoweringPreparePass>();
900 pass->setASTContext(astCtx);
901 return std::move(pass);
Defines the clang::ASTContext interface.
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 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.
__device__ __2f16 float c
mlir::Value createLogicalOr(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
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)
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::CallOp createCallOp(mlir::Location loc, mlir::SymbolRefAttr callee, mlir::Type returnType, mlir::ValueRange operands, llvm::ArrayRef< mlir::NamedAttribute > attrs={})
mlir::Value getSignedInt(mlir::Location loc, int64_t val, unsigned numBits)
cir::StoreOp createStore(mlir::Location loc, mlir::Value val, mlir::Value dst, bool isVolatile=false, mlir::IntegerAttr align={}, cir::MemOrderAttr order={})
cir::CmpOp createCompare(mlir::Location loc, cir::CmpOpKind kind, mlir::Value lhs, mlir::Value rhs)
mlir::IntegerAttr getAlignmentAttr(clang::CharUnits alignment)
mlir::Value createBinop(mlir::Location loc, mlir::Value lhs, cir::BinOpKind kind, mlir::Value rhs)
mlir::Value createComplexCreate(mlir::Location loc, mlir::Value real, mlir::Value imag)
mlir::Value createIsNaN(mlir::Location loc, mlir::Value operand)
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.
bool isModuleImplementation() const
Is this a module implementation.
Exposes information about the current target.
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::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
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 opGlobalAnnotations()
static bool opGlobalCtorPriority()
static bool opFuncExtraAttrs()
static bool fastMathFlags()
static bool astVarDeclInterface()
static bool opGlobalDtorLowering()