clang 22.0.0git
LoweringPrepare.cpp
Go to the documentation of this file.
1//===- LoweringPrepare.cpp - pareparation work for LLVM lowering ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "PassDetail.h"
12#include "clang/Basic/Module.h"
19#include "llvm/Support/Path.h"
20
21#include <memory>
22
23using namespace mlir;
24using namespace cir;
25
26static SmallString<128> getTransformedFileName(mlir::ModuleOp mlirModule) {
27 SmallString<128> fileName;
28
29 if (mlirModule.getSymName())
30 fileName = llvm::sys::path::filename(mlirModule.getSymName()->str());
31
32 if (fileName.empty())
33 fileName = "<null>";
34
35 for (size_t i = 0; i < fileName.size(); ++i) {
36 // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
37 // to be the set of C preprocessing numbers.
38 if (!clang::isPreprocessingNumberBody(fileName[i]))
39 fileName[i] = '_';
40 }
41
42 return fileName;
43}
44
45/// Return the FuncOp called by `callOp`.
46static cir::FuncOp getCalledFunction(cir::CallOp callOp) {
47 mlir::SymbolRefAttr sym = llvm::dyn_cast_if_present<mlir::SymbolRefAttr>(
48 callOp.getCallableForCallee());
49 if (!sym)
50 return nullptr;
51 return dyn_cast_or_null<cir::FuncOp>(
52 mlir::SymbolTable::lookupNearestSymbolFrom(callOp, sym));
53}
54
55namespace {
56struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
57 LoweringPreparePass() = default;
58 void runOnOperation() override;
59
60 void runOnOp(mlir::Operation *op);
61 void lowerCastOp(cir::CastOp op);
62 void lowerComplexDivOp(cir::ComplexDivOp op);
63 void lowerComplexMulOp(cir::ComplexMulOp op);
64 void lowerUnaryOp(cir::UnaryOp op);
65 void lowerGlobalOp(cir::GlobalOp op);
66 void lowerDynamicCastOp(cir::DynamicCastOp op);
67 void lowerArrayDtor(cir::ArrayDtor op);
68 void lowerArrayCtor(cir::ArrayCtor op);
69
70 /// Build the function that initializes the specified global
71 cir::FuncOp buildCXXGlobalVarDeclInitFunc(cir::GlobalOp op);
72
73 /// Build a module init function that calls all the dynamic initializers.
74 void buildCXXGlobalInitFunc();
75
76 /// Materialize global ctor/dtor list
77 void buildGlobalCtorDtorList();
78
79 cir::FuncOp buildRuntimeFunction(
80 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
81 cir::FuncType type,
82 cir::GlobalLinkageKind linkage = cir::GlobalLinkageKind::ExternalLinkage);
83
84 cir::GlobalOp buildRuntimeVariable(
85 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
86 mlir::Type type,
87 cir::GlobalLinkageKind linkage = cir::GlobalLinkageKind::ExternalLinkage,
88 cir::VisibilityKind visibility = cir::VisibilityKind::Default);
89
90 ///
91 /// AST related
92 /// -----------
93
94 clang::ASTContext *astCtx;
95
96 // Helper for lowering C++ ABI specific operations.
97 std::shared_ptr<cir::LoweringPrepareCXXABI> cxxABI;
98
99 /// Tracks current module.
100 mlir::ModuleOp mlirModule;
101
102 /// Tracks existing dynamic initializers.
103 llvm::StringMap<uint32_t> dynamicInitializerNames;
104 llvm::SmallVector<cir::FuncOp> dynamicInitializers;
105
106 /// List of ctors and their priorities to be called before main()
107 llvm::SmallVector<std::pair<std::string, uint32_t>, 4> globalCtorList;
108 /// List of dtors and their priorities to be called when unloading module.
109 llvm::SmallVector<std::pair<std::string, uint32_t>, 4> globalDtorList;
110
111 void setASTContext(clang::ASTContext *c) {
112 astCtx = c;
113 switch (c->getCXXABIKind()) {
114 case clang::TargetCXXABI::GenericItanium:
115 // We'll need X86-specific support for handling vaargs lowering, but for
116 // now the Itanium ABI will work.
119 break;
120 case clang::TargetCXXABI::GenericAArch64:
121 case clang::TargetCXXABI::AppleARM64:
124 break;
125 default:
126 llvm_unreachable("NYI");
127 }
128 }
129};
130
131} // namespace
132
133cir::GlobalOp LoweringPreparePass::buildRuntimeVariable(
134 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
135 mlir::Type type, cir::GlobalLinkageKind linkage,
136 cir::VisibilityKind visibility) {
137 cir::GlobalOp g = dyn_cast_or_null<cir::GlobalOp>(
138 mlir::SymbolTable::lookupNearestSymbolFrom(
139 mlirModule, mlir::StringAttr::get(mlirModule->getContext(), name)));
140 if (!g) {
141 g = cir::GlobalOp::create(builder, loc, name, type);
142 g.setLinkageAttr(
143 cir::GlobalLinkageKindAttr::get(builder.getContext(), linkage));
144 mlir::SymbolTable::setSymbolVisibility(
145 g, mlir::SymbolTable::Visibility::Private);
146 g.setGlobalVisibilityAttr(
147 cir::VisibilityAttr::get(builder.getContext(), visibility));
148 }
149 return g;
150}
151
152cir::FuncOp LoweringPreparePass::buildRuntimeFunction(
153 mlir::OpBuilder &builder, llvm::StringRef name, mlir::Location loc,
154 cir::FuncType type, cir::GlobalLinkageKind linkage) {
155 cir::FuncOp f = dyn_cast_or_null<FuncOp>(SymbolTable::lookupNearestSymbolFrom(
156 mlirModule, StringAttr::get(mlirModule->getContext(), name)));
157 if (!f) {
158 f = builder.create<cir::FuncOp>(loc, name, type);
159 f.setLinkageAttr(
160 cir::GlobalLinkageKindAttr::get(builder.getContext(), linkage));
161 mlir::SymbolTable::setSymbolVisibility(
162 f, mlir::SymbolTable::Visibility::Private);
163
165 }
166 return f;
167}
168
169static mlir::Value lowerScalarToComplexCast(mlir::MLIRContext &ctx,
170 cir::CastOp op) {
171 cir::CIRBaseBuilderTy builder(ctx);
172 builder.setInsertionPoint(op);
173
174 mlir::Value src = op.getSrc();
175 mlir::Value imag = builder.getNullValue(src.getType(), op.getLoc());
176 return builder.createComplexCreate(op.getLoc(), src, imag);
177}
178
179static mlir::Value lowerComplexToScalarCast(mlir::MLIRContext &ctx,
180 cir::CastOp op,
181 cir::CastKind elemToBoolKind) {
182 cir::CIRBaseBuilderTy builder(ctx);
183 builder.setInsertionPoint(op);
184
185 mlir::Value src = op.getSrc();
186 if (!mlir::isa<cir::BoolType>(op.getType()))
187 return builder.createComplexReal(op.getLoc(), src);
188
189 // Complex cast to bool: (bool)(a+bi) => (bool)a || (bool)b
190 mlir::Value srcReal = builder.createComplexReal(op.getLoc(), src);
191 mlir::Value srcImag = builder.createComplexImag(op.getLoc(), src);
192
193 cir::BoolType boolTy = builder.getBoolTy();
194 mlir::Value srcRealToBool =
195 builder.createCast(op.getLoc(), elemToBoolKind, srcReal, boolTy);
196 mlir::Value srcImagToBool =
197 builder.createCast(op.getLoc(), elemToBoolKind, srcImag, boolTy);
198 return builder.createLogicalOr(op.getLoc(), srcRealToBool, srcImagToBool);
199}
200
201static mlir::Value lowerComplexToComplexCast(mlir::MLIRContext &ctx,
202 cir::CastOp op,
203 cir::CastKind scalarCastKind) {
204 CIRBaseBuilderTy builder(ctx);
205 builder.setInsertionPoint(op);
206
207 mlir::Value src = op.getSrc();
208 auto dstComplexElemTy =
209 mlir::cast<cir::ComplexType>(op.getType()).getElementType();
210
211 mlir::Value srcReal = builder.createComplexReal(op.getLoc(), src);
212 mlir::Value srcImag = builder.createComplexImag(op.getLoc(), src);
213
214 mlir::Value dstReal = builder.createCast(op.getLoc(), scalarCastKind, srcReal,
215 dstComplexElemTy);
216 mlir::Value dstImag = builder.createCast(op.getLoc(), scalarCastKind, srcImag,
217 dstComplexElemTy);
218 return builder.createComplexCreate(op.getLoc(), dstReal, dstImag);
219}
220
221void LoweringPreparePass::lowerCastOp(cir::CastOp op) {
222 mlir::MLIRContext &ctx = getContext();
223 mlir::Value loweredValue = [&]() -> mlir::Value {
224 switch (op.getKind()) {
225 case cir::CastKind::float_to_complex:
226 case cir::CastKind::int_to_complex:
227 return lowerScalarToComplexCast(ctx, op);
228 case cir::CastKind::float_complex_to_real:
229 case cir::CastKind::int_complex_to_real:
230 return lowerComplexToScalarCast(ctx, op, op.getKind());
231 case cir::CastKind::float_complex_to_bool:
232 return lowerComplexToScalarCast(ctx, op, cir::CastKind::float_to_bool);
233 case cir::CastKind::int_complex_to_bool:
234 return lowerComplexToScalarCast(ctx, op, cir::CastKind::int_to_bool);
235 case cir::CastKind::float_complex:
236 return lowerComplexToComplexCast(ctx, op, cir::CastKind::floating);
237 case cir::CastKind::float_complex_to_int_complex:
238 return lowerComplexToComplexCast(ctx, op, cir::CastKind::float_to_int);
239 case cir::CastKind::int_complex:
240 return lowerComplexToComplexCast(ctx, op, cir::CastKind::integral);
241 case cir::CastKind::int_complex_to_float_complex:
242 return lowerComplexToComplexCast(ctx, op, cir::CastKind::int_to_float);
243 default:
244 return nullptr;
245 }
246 }();
247
248 if (loweredValue) {
249 op.replaceAllUsesWith(loweredValue);
250 op.erase();
251 }
252}
253
254static mlir::Value buildComplexBinOpLibCall(
255 LoweringPreparePass &pass, CIRBaseBuilderTy &builder,
256 llvm::StringRef (*libFuncNameGetter)(llvm::APFloat::Semantics),
257 mlir::Location loc, cir::ComplexType ty, mlir::Value lhsReal,
258 mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag) {
259 cir::FPTypeInterface elementTy =
260 mlir::cast<cir::FPTypeInterface>(ty.getElementType());
261
262 llvm::StringRef libFuncName = libFuncNameGetter(
263 llvm::APFloat::SemanticsToEnum(elementTy.getFloatSemantics()));
264 llvm::SmallVector<mlir::Type, 4> libFuncInputTypes(4, elementTy);
265
266 cir::FuncType libFuncTy = cir::FuncType::get(libFuncInputTypes, ty);
267
268 // Insert a declaration for the runtime function to be used in Complex
269 // multiplication and division when needed
270 cir::FuncOp libFunc;
271 {
272 mlir::OpBuilder::InsertionGuard ipGuard{builder};
273 builder.setInsertionPointToStart(pass.mlirModule.getBody());
274 libFunc = pass.buildRuntimeFunction(builder, libFuncName, loc, libFuncTy);
275 }
276
277 cir::CallOp call =
278 builder.createCallOp(loc, libFunc, {lhsReal, lhsImag, rhsReal, rhsImag});
279 return call.getResult();
280}
281
282static llvm::StringRef
283getComplexDivLibCallName(llvm::APFloat::Semantics semantics) {
284 switch (semantics) {
285 case llvm::APFloat::S_IEEEhalf:
286 return "__divhc3";
287 case llvm::APFloat::S_IEEEsingle:
288 return "__divsc3";
289 case llvm::APFloat::S_IEEEdouble:
290 return "__divdc3";
291 case llvm::APFloat::S_PPCDoubleDouble:
292 return "__divtc3";
293 case llvm::APFloat::S_x87DoubleExtended:
294 return "__divxc3";
295 case llvm::APFloat::S_IEEEquad:
296 return "__divtc3";
297 default:
298 llvm_unreachable("unsupported floating point type");
299 }
300}
301
302static mlir::Value
303buildAlgebraicComplexDiv(CIRBaseBuilderTy &builder, mlir::Location loc,
304 mlir::Value lhsReal, mlir::Value lhsImag,
305 mlir::Value rhsReal, mlir::Value rhsImag) {
306 // (a+bi) / (c+di) = ((ac+bd)/(cc+dd)) + ((bc-ad)/(cc+dd))i
307 mlir::Value &a = lhsReal;
308 mlir::Value &b = lhsImag;
309 mlir::Value &c = rhsReal;
310 mlir::Value &d = rhsImag;
311
312 mlir::Value ac = builder.createBinop(loc, a, cir::BinOpKind::Mul, c); // a*c
313 mlir::Value bd = builder.createBinop(loc, b, cir::BinOpKind::Mul, d); // b*d
314 mlir::Value cc = builder.createBinop(loc, c, cir::BinOpKind::Mul, c); // c*c
315 mlir::Value dd = builder.createBinop(loc, d, cir::BinOpKind::Mul, d); // d*d
316 mlir::Value acbd =
317 builder.createBinop(loc, ac, cir::BinOpKind::Add, bd); // ac+bd
318 mlir::Value ccdd =
319 builder.createBinop(loc, cc, cir::BinOpKind::Add, dd); // cc+dd
320 mlir::Value resultReal =
321 builder.createBinop(loc, acbd, cir::BinOpKind::Div, ccdd);
322
323 mlir::Value bc = builder.createBinop(loc, b, cir::BinOpKind::Mul, c); // b*c
324 mlir::Value ad = builder.createBinop(loc, a, cir::BinOpKind::Mul, d); // a*d
325 mlir::Value bcad =
326 builder.createBinop(loc, bc, cir::BinOpKind::Sub, ad); // bc-ad
327 mlir::Value resultImag =
328 builder.createBinop(loc, bcad, cir::BinOpKind::Div, ccdd);
329 return builder.createComplexCreate(loc, resultReal, resultImag);
330}
331
332static mlir::Value
334 mlir::Value lhsReal, mlir::Value lhsImag,
335 mlir::Value rhsReal, mlir::Value rhsImag) {
336 // Implements Smith's algorithm for complex division.
337 // SMITH, R. L. Algorithm 116: Complex division. Commun. ACM 5, 8 (1962).
338
339 // Let:
340 // - lhs := a+bi
341 // - rhs := c+di
342 // - result := lhs / rhs = e+fi
343 //
344 // The algorithm pseudocode looks like follows:
345 // if fabs(c) >= fabs(d):
346 // r := d / c
347 // tmp := c + r*d
348 // e = (a + b*r) / tmp
349 // f = (b - a*r) / tmp
350 // else:
351 // r := c / d
352 // tmp := d + r*c
353 // e = (a*r + b) / tmp
354 // f = (b*r - a) / tmp
355
356 mlir::Value &a = lhsReal;
357 mlir::Value &b = lhsImag;
358 mlir::Value &c = rhsReal;
359 mlir::Value &d = rhsImag;
360
361 auto trueBranchBuilder = [&](mlir::OpBuilder &, mlir::Location) {
362 mlir::Value r = builder.createBinop(loc, d, cir::BinOpKind::Div,
363 c); // r := d / c
364 mlir::Value rd = builder.createBinop(loc, r, cir::BinOpKind::Mul, d); // r*d
365 mlir::Value tmp = builder.createBinop(loc, c, cir::BinOpKind::Add,
366 rd); // tmp := c + r*d
367
368 mlir::Value br = builder.createBinop(loc, b, cir::BinOpKind::Mul, r); // b*r
369 mlir::Value abr =
370 builder.createBinop(loc, a, cir::BinOpKind::Add, br); // a + b*r
371 mlir::Value e = builder.createBinop(loc, abr, cir::BinOpKind::Div, tmp);
372
373 mlir::Value ar = builder.createBinop(loc, a, cir::BinOpKind::Mul, r); // a*r
374 mlir::Value bar =
375 builder.createBinop(loc, b, cir::BinOpKind::Sub, ar); // b - a*r
376 mlir::Value f = builder.createBinop(loc, bar, cir::BinOpKind::Div, tmp);
377
378 mlir::Value result = builder.createComplexCreate(loc, e, f);
379 builder.createYield(loc, result);
380 };
381
382 auto falseBranchBuilder = [&](mlir::OpBuilder &, mlir::Location) {
383 mlir::Value r = builder.createBinop(loc, c, cir::BinOpKind::Div,
384 d); // r := c / d
385 mlir::Value rc = builder.createBinop(loc, r, cir::BinOpKind::Mul, c); // r*c
386 mlir::Value tmp = builder.createBinop(loc, d, cir::BinOpKind::Add,
387 rc); // tmp := d + r*c
388
389 mlir::Value ar = builder.createBinop(loc, a, cir::BinOpKind::Mul, r); // a*r
390 mlir::Value arb =
391 builder.createBinop(loc, ar, cir::BinOpKind::Add, b); // a*r + b
392 mlir::Value e = builder.createBinop(loc, arb, cir::BinOpKind::Div, tmp);
393
394 mlir::Value br = builder.createBinop(loc, b, cir::BinOpKind::Mul, r); // b*r
395 mlir::Value bra =
396 builder.createBinop(loc, br, cir::BinOpKind::Sub, a); // b*r - a
397 mlir::Value f = builder.createBinop(loc, bra, cir::BinOpKind::Div, tmp);
398
399 mlir::Value result = builder.createComplexCreate(loc, e, f);
400 builder.createYield(loc, result);
401 };
402
403 auto cFabs = builder.create<cir::FAbsOp>(loc, c);
404 auto dFabs = builder.create<cir::FAbsOp>(loc, d);
405 cir::CmpOp cmpResult =
406 builder.createCompare(loc, cir::CmpOpKind::ge, cFabs, dFabs);
407 auto ternary = builder.create<cir::TernaryOp>(
408 loc, cmpResult, trueBranchBuilder, falseBranchBuilder);
409
410 return ternary.getResult();
411}
412
414 mlir::MLIRContext &context, clang::ASTContext &cc,
415 CIRBaseBuilderTy &builder, mlir::Type elementType) {
416
417 auto getHigherPrecisionFPType = [&context](mlir::Type type) -> mlir::Type {
418 if (mlir::isa<cir::FP16Type>(type))
419 return cir::SingleType::get(&context);
420
421 if (mlir::isa<cir::SingleType>(type) || mlir::isa<cir::BF16Type>(type))
422 return cir::DoubleType::get(&context);
423
424 if (mlir::isa<cir::DoubleType>(type))
425 return cir::LongDoubleType::get(&context, type);
426
427 return type;
428 };
429
430 auto getFloatTypeSemantics =
431 [&cc](mlir::Type type) -> const llvm::fltSemantics & {
432 const clang::TargetInfo &info = cc.getTargetInfo();
433 if (mlir::isa<cir::FP16Type>(type))
434 return info.getHalfFormat();
435
436 if (mlir::isa<cir::BF16Type>(type))
437 return info.getBFloat16Format();
438
439 if (mlir::isa<cir::SingleType>(type))
440 return info.getFloatFormat();
441
442 if (mlir::isa<cir::DoubleType>(type))
443 return info.getDoubleFormat();
444
445 if (mlir::isa<cir::LongDoubleType>(type)) {
446 if (cc.getLangOpts().OpenMP && cc.getLangOpts().OpenMPIsTargetDevice)
447 llvm_unreachable("NYI Float type semantics with OpenMP");
448 return info.getLongDoubleFormat();
449 }
450
451 if (mlir::isa<cir::FP128Type>(type)) {
452 if (cc.getLangOpts().OpenMP && cc.getLangOpts().OpenMPIsTargetDevice)
453 llvm_unreachable("NYI Float type semantics with OpenMP");
454 return info.getFloat128Format();
455 }
456
457 assert(false && "Unsupported float type semantics");
458 };
459
460 const mlir::Type higherElementType = getHigherPrecisionFPType(elementType);
461 const llvm::fltSemantics &elementTypeSemantics =
462 getFloatTypeSemantics(elementType);
463 const llvm::fltSemantics &higherElementTypeSemantics =
464 getFloatTypeSemantics(higherElementType);
465
466 // Check that the promoted type can handle the intermediate values without
467 // overflowing. This can be interpreted as:
468 // (SmallerType.LargestFiniteVal * SmallerType.LargestFiniteVal) * 2 <=
469 // LargerType.LargestFiniteVal.
470 // In terms of exponent it gives this formula:
471 // (SmallerType.LargestFiniteVal * SmallerType.LargestFiniteVal
472 // doubles the exponent of SmallerType.LargestFiniteVal)
473 if (llvm::APFloat::semanticsMaxExponent(elementTypeSemantics) * 2 + 1 <=
474 llvm::APFloat::semanticsMaxExponent(higherElementTypeSemantics)) {
475 return higherElementType;
476 }
477
478 // The intermediate values can't be represented in the promoted type
479 // without overflowing.
480 return {};
481}
482
483static mlir::Value
484lowerComplexDiv(LoweringPreparePass &pass, CIRBaseBuilderTy &builder,
485 mlir::Location loc, cir::ComplexDivOp op, mlir::Value lhsReal,
486 mlir::Value lhsImag, mlir::Value rhsReal, mlir::Value rhsImag,
487 mlir::MLIRContext &mlirCx, clang::ASTContext &cc) {
488 cir::ComplexType complexTy = op.getType();
489 if (mlir::isa<cir::FPTypeInterface>(complexTy.getElementType())) {
490 cir::ComplexRangeKind range = op.getRange();
491 if (range == cir::ComplexRangeKind::Improved)
492 return buildRangeReductionComplexDiv(builder, loc, lhsReal, lhsImag,
493 rhsReal, rhsImag);
494
495 if (range == cir::ComplexRangeKind::Full)
497 loc, complexTy, lhsReal, lhsImag, rhsReal,
498 rhsImag);
499
500 if (range == cir::ComplexRangeKind::Promoted) {
501 mlir::Type originalElementType = complexTy.getElementType();
502 mlir::Type higherPrecisionElementType =
504 originalElementType);
505
506 if (!higherPrecisionElementType)
507 return buildRangeReductionComplexDiv(builder, loc, lhsReal, lhsImag,
508 rhsReal, rhsImag);
509
510 cir::CastKind floatingCastKind = cir::CastKind::floating;
511 lhsReal = builder.createCast(floatingCastKind, lhsReal,
512 higherPrecisionElementType);
513 lhsImag = builder.createCast(floatingCastKind, lhsImag,
514 higherPrecisionElementType);
515 rhsReal = builder.createCast(floatingCastKind, rhsReal,
516 higherPrecisionElementType);
517 rhsImag = builder.createCast(floatingCastKind, rhsImag,
518 higherPrecisionElementType);
519
520 mlir::Value algebraicResult = buildAlgebraicComplexDiv(
521 builder, loc, lhsReal, lhsImag, rhsReal, rhsImag);
522
523 mlir::Value resultReal = builder.createComplexReal(loc, algebraicResult);
524 mlir::Value resultImag = builder.createComplexImag(loc, algebraicResult);
525
526 mlir::Value finalReal =
527 builder.createCast(floatingCastKind, resultReal, originalElementType);
528 mlir::Value finalImag =
529 builder.createCast(floatingCastKind, resultImag, originalElementType);
530 return builder.createComplexCreate(loc, finalReal, finalImag);
531 }
532 }
533
534 return buildAlgebraicComplexDiv(builder, loc, lhsReal, lhsImag, rhsReal,
535 rhsImag);
536}
537
538void LoweringPreparePass::lowerComplexDivOp(cir::ComplexDivOp op) {
539 cir::CIRBaseBuilderTy builder(getContext());
540 builder.setInsertionPointAfter(op);
541 mlir::Location loc = op.getLoc();
542 mlir::TypedValue<cir::ComplexType> lhs = op.getLhs();
543 mlir::TypedValue<cir::ComplexType> rhs = op.getRhs();
544 mlir::Value lhsReal = builder.createComplexReal(loc, lhs);
545 mlir::Value lhsImag = builder.createComplexImag(loc, lhs);
546 mlir::Value rhsReal = builder.createComplexReal(loc, rhs);
547 mlir::Value rhsImag = builder.createComplexImag(loc, rhs);
548
549 mlir::Value loweredResult =
550 lowerComplexDiv(*this, builder, loc, op, lhsReal, lhsImag, rhsReal,
551 rhsImag, getContext(), *astCtx);
552 op.replaceAllUsesWith(loweredResult);
553 op.erase();
554}
555
556static llvm::StringRef
557getComplexMulLibCallName(llvm::APFloat::Semantics semantics) {
558 switch (semantics) {
559 case llvm::APFloat::S_IEEEhalf:
560 return "__mulhc3";
561 case llvm::APFloat::S_IEEEsingle:
562 return "__mulsc3";
563 case llvm::APFloat::S_IEEEdouble:
564 return "__muldc3";
565 case llvm::APFloat::S_PPCDoubleDouble:
566 return "__multc3";
567 case llvm::APFloat::S_x87DoubleExtended:
568 return "__mulxc3";
569 case llvm::APFloat::S_IEEEquad:
570 return "__multc3";
571 default:
572 llvm_unreachable("unsupported floating point type");
573 }
574}
575
576static mlir::Value lowerComplexMul(LoweringPreparePass &pass,
577 CIRBaseBuilderTy &builder,
578 mlir::Location loc, cir::ComplexMulOp op,
579 mlir::Value lhsReal, mlir::Value lhsImag,
580 mlir::Value rhsReal, mlir::Value rhsImag) {
581 // (a+bi) * (c+di) = (ac-bd) + (ad+bc)i
582 mlir::Value resultRealLhs =
583 builder.createBinop(loc, lhsReal, cir::BinOpKind::Mul, rhsReal);
584 mlir::Value resultRealRhs =
585 builder.createBinop(loc, lhsImag, cir::BinOpKind::Mul, rhsImag);
586 mlir::Value resultImagLhs =
587 builder.createBinop(loc, lhsReal, cir::BinOpKind::Mul, rhsImag);
588 mlir::Value resultImagRhs =
589 builder.createBinop(loc, lhsImag, cir::BinOpKind::Mul, rhsReal);
590 mlir::Value resultReal = builder.createBinop(
591 loc, resultRealLhs, cir::BinOpKind::Sub, resultRealRhs);
592 mlir::Value resultImag = builder.createBinop(
593 loc, resultImagLhs, cir::BinOpKind::Add, resultImagRhs);
594 mlir::Value algebraicResult =
595 builder.createComplexCreate(loc, resultReal, resultImag);
596
597 cir::ComplexType complexTy = op.getType();
598 cir::ComplexRangeKind rangeKind = op.getRange();
599 if (mlir::isa<cir::IntType>(complexTy.getElementType()) ||
600 rangeKind == cir::ComplexRangeKind::Basic ||
601 rangeKind == cir::ComplexRangeKind::Improved ||
602 rangeKind == cir::ComplexRangeKind::Promoted)
603 return algebraicResult;
604
606
607 // Check whether the real part and the imaginary part of the result are both
608 // NaN. If so, emit a library call to compute the multiplication instead.
609 // We check a value against NaN by comparing the value against itself.
610 mlir::Value resultRealIsNaN = builder.createIsNaN(loc, resultReal);
611 mlir::Value resultImagIsNaN = builder.createIsNaN(loc, resultImag);
612 mlir::Value resultRealAndImagAreNaN =
613 builder.createLogicalAnd(loc, resultRealIsNaN, resultImagIsNaN);
614
615 return builder
616 .create<cir::TernaryOp>(
617 loc, resultRealAndImagAreNaN,
618 [&](mlir::OpBuilder &, mlir::Location) {
619 mlir::Value libCallResult = buildComplexBinOpLibCall(
620 pass, builder, &getComplexMulLibCallName, loc, complexTy,
621 lhsReal, lhsImag, rhsReal, rhsImag);
622 builder.createYield(loc, libCallResult);
623 },
624 [&](mlir::OpBuilder &, mlir::Location) {
625 builder.createYield(loc, algebraicResult);
626 })
627 .getResult();
628}
629
630void LoweringPreparePass::lowerComplexMulOp(cir::ComplexMulOp op) {
631 cir::CIRBaseBuilderTy builder(getContext());
632 builder.setInsertionPointAfter(op);
633 mlir::Location loc = op.getLoc();
634 mlir::TypedValue<cir::ComplexType> lhs = op.getLhs();
635 mlir::TypedValue<cir::ComplexType> rhs = op.getRhs();
636 mlir::Value lhsReal = builder.createComplexReal(loc, lhs);
637 mlir::Value lhsImag = builder.createComplexImag(loc, lhs);
638 mlir::Value rhsReal = builder.createComplexReal(loc, rhs);
639 mlir::Value rhsImag = builder.createComplexImag(loc, rhs);
640 mlir::Value loweredResult = lowerComplexMul(*this, builder, loc, op, lhsReal,
641 lhsImag, rhsReal, rhsImag);
642 op.replaceAllUsesWith(loweredResult);
643 op.erase();
644}
645
646void LoweringPreparePass::lowerUnaryOp(cir::UnaryOp op) {
647 mlir::Type ty = op.getType();
648 if (!mlir::isa<cir::ComplexType>(ty))
649 return;
650
651 mlir::Location loc = op.getLoc();
652 cir::UnaryOpKind opKind = op.getKind();
653
654 CIRBaseBuilderTy builder(getContext());
655 builder.setInsertionPointAfter(op);
656
657 mlir::Value operand = op.getInput();
658 mlir::Value operandReal = builder.createComplexReal(loc, operand);
659 mlir::Value operandImag = builder.createComplexImag(loc, operand);
660
661 mlir::Value resultReal;
662 mlir::Value resultImag;
663
664 switch (opKind) {
665 case cir::UnaryOpKind::Inc:
666 case cir::UnaryOpKind::Dec:
667 resultReal = builder.createUnaryOp(loc, opKind, operandReal);
668 resultImag = operandImag;
669 break;
670
671 case cir::UnaryOpKind::Plus:
672 case cir::UnaryOpKind::Minus:
673 resultReal = builder.createUnaryOp(loc, opKind, operandReal);
674 resultImag = builder.createUnaryOp(loc, opKind, operandImag);
675 break;
676
677 case cir::UnaryOpKind::Not:
678 resultReal = operandReal;
679 resultImag =
680 builder.createUnaryOp(loc, cir::UnaryOpKind::Minus, operandImag);
681 break;
682 }
683
684 mlir::Value result = builder.createComplexCreate(loc, resultReal, resultImag);
685 op.replaceAllUsesWith(result);
686 op.erase();
687}
688
689cir::FuncOp
690LoweringPreparePass::buildCXXGlobalVarDeclInitFunc(cir::GlobalOp op) {
691 // TODO(cir): Store this in the GlobalOp.
692 // This should come from the MangleContext, but for now I'm hardcoding it.
693 SmallString<256> fnName("__cxx_global_var_init");
694 // Get a unique name
695 uint32_t cnt = dynamicInitializerNames[fnName]++;
696 if (cnt)
697 fnName += "." + llvm::Twine(cnt).str();
698
699 // Create a variable initialization function.
700 CIRBaseBuilderTy builder(getContext());
701 builder.setInsertionPointAfter(op);
702 cir::VoidType voidTy = builder.getVoidTy();
703 auto fnType = cir::FuncType::get({}, voidTy);
704 FuncOp f = buildRuntimeFunction(builder, fnName, op.getLoc(), fnType,
705 cir::GlobalLinkageKind::InternalLinkage);
706
707 // Move over the initialzation code of the ctor region.
708 mlir::Block *entryBB = f.addEntryBlock();
709 if (!op.getCtorRegion().empty()) {
710 mlir::Block &block = op.getCtorRegion().front();
711 entryBB->getOperations().splice(entryBB->begin(), block.getOperations(),
712 block.begin(), std::prev(block.end()));
713 }
714
715 // Register the destructor call with __cxa_atexit
716 mlir::Region &dtorRegion = op.getDtorRegion();
717 if (!dtorRegion.empty()) {
720 // Create a variable that binds the atexit to this shared object.
721 builder.setInsertionPointToStart(&mlirModule.getBodyRegion().front());
722 cir::GlobalOp handle = buildRuntimeVariable(
723 builder, "__dso_handle", op.getLoc(), builder.getI8Type(),
724 cir::GlobalLinkageKind::ExternalLinkage, cir::VisibilityKind::Hidden);
725
726 // Look for the destructor call in dtorBlock
727 mlir::Block &dtorBlock = dtorRegion.front();
728 cir::CallOp dtorCall;
729 for (auto op : reverse(dtorBlock.getOps<cir::CallOp>())) {
730 dtorCall = op;
731 break;
732 }
733 assert(dtorCall && "Expected a dtor call");
734 cir::FuncOp dtorFunc = getCalledFunction(dtorCall);
735 assert(dtorFunc && "Expected a dtor call");
736
737 // Create a runtime helper function:
738 // extern "C" int __cxa_atexit(void (*f)(void *), void *p, void *d);
739 auto voidPtrTy = cir::PointerType::get(voidTy);
740 auto voidFnTy = cir::FuncType::get({voidPtrTy}, voidTy);
741 auto voidFnPtrTy = cir::PointerType::get(voidFnTy);
742 auto handlePtrTy = cir::PointerType::get(handle.getSymType());
743 auto fnAtExitType =
744 cir::FuncType::get({voidFnPtrTy, voidPtrTy, handlePtrTy}, voidTy);
745 const char *nameAtExit = "__cxa_atexit";
746 cir::FuncOp fnAtExit =
747 buildRuntimeFunction(builder, nameAtExit, op.getLoc(), fnAtExitType);
748
749 // Replace the dtor call with a call to __cxa_atexit(&dtor, &var,
750 // &__dso_handle)
751 builder.setInsertionPointAfter(dtorCall);
752 mlir::Value args[3];
753 auto dtorPtrTy = cir::PointerType::get(dtorFunc.getFunctionType());
754 // dtorPtrTy
755 args[0] = cir::GetGlobalOp::create(builder, dtorCall.getLoc(), dtorPtrTy,
756 dtorFunc.getSymName());
757 args[0] = cir::CastOp::create(builder, dtorCall.getLoc(), voidFnPtrTy,
758 cir::CastKind::bitcast, args[0]);
759 args[1] =
760 cir::CastOp::create(builder, dtorCall.getLoc(), voidPtrTy,
761 cir::CastKind::bitcast, dtorCall.getArgOperand(0));
762 args[2] = cir::GetGlobalOp::create(builder, handle.getLoc(), handlePtrTy,
763 handle.getSymName());
764 builder.createCallOp(dtorCall.getLoc(), fnAtExit, args);
765 dtorCall->erase();
766 entryBB->getOperations().splice(entryBB->end(), dtorBlock.getOperations(),
767 dtorBlock.begin(),
768 std::prev(dtorBlock.end()));
769 }
770
771 // Replace cir.yield with cir.return
772 builder.setInsertionPointToEnd(entryBB);
773 mlir::Operation *yieldOp = nullptr;
774 if (!op.getCtorRegion().empty()) {
775 mlir::Block &block = op.getCtorRegion().front();
776 yieldOp = &block.getOperations().back();
777 } else {
778 assert(!dtorRegion.empty());
779 mlir::Block &block = dtorRegion.front();
780 yieldOp = &block.getOperations().back();
781 }
782
783 assert(isa<cir::YieldOp>(*yieldOp));
784 cir::ReturnOp::create(builder, yieldOp->getLoc());
785 return f;
786}
787
788void LoweringPreparePass::lowerGlobalOp(GlobalOp op) {
789 mlir::Region &ctorRegion = op.getCtorRegion();
790 mlir::Region &dtorRegion = op.getDtorRegion();
791
792 if (!ctorRegion.empty() || !dtorRegion.empty()) {
793 // Build a variable initialization function and move the initialzation code
794 // in the ctor region over.
795 cir::FuncOp f = buildCXXGlobalVarDeclInitFunc(op);
796
797 // Clear the ctor and dtor region
798 ctorRegion.getBlocks().clear();
799 dtorRegion.getBlocks().clear();
800
802 dynamicInitializers.push_back(f);
803 }
804
806}
807
808template <typename AttributeTy>
809static llvm::SmallVector<mlir::Attribute>
810prepareCtorDtorAttrList(mlir::MLIRContext *context,
811 llvm::ArrayRef<std::pair<std::string, uint32_t>> list) {
813 for (const auto &[name, priority] : list)
814 attrs.push_back(AttributeTy::get(context, name, priority));
815 return attrs;
816}
817
818void LoweringPreparePass::buildGlobalCtorDtorList() {
819 if (!globalCtorList.empty()) {
820 llvm::SmallVector<mlir::Attribute> globalCtors =
822 globalCtorList);
823
824 mlirModule->setAttr(cir::CIRDialect::getGlobalCtorsAttrName(),
825 mlir::ArrayAttr::get(&getContext(), globalCtors));
826 }
827
828 if (!globalDtorList.empty()) {
829 llvm::SmallVector<mlir::Attribute> globalDtors =
831 globalDtorList);
832 mlirModule->setAttr(cir::CIRDialect::getGlobalDtorsAttrName(),
833 mlir::ArrayAttr::get(&getContext(), globalDtors));
834 }
835}
836
837void LoweringPreparePass::buildCXXGlobalInitFunc() {
838 if (dynamicInitializers.empty())
839 return;
840
841 // TODO: handle globals with a user-specified initialzation priority.
842 // TODO: handle default priority more nicely.
844
845 SmallString<256> fnName;
846 // Include the filename in the symbol name. Including "sub_" matches gcc
847 // and makes sure these symbols appear lexicographically behind the symbols
848 // with priority (TBD). Module implementation units behave the same
849 // way as a non-modular TU with imports.
850 // TODO: check CXX20ModuleInits
851 if (astCtx->getCurrentNamedModule() &&
853 llvm::raw_svector_ostream out(fnName);
854 std::unique_ptr<clang::MangleContext> mangleCtx(
855 astCtx->createMangleContext());
856 cast<clang::ItaniumMangleContext>(*mangleCtx)
857 .mangleModuleInitializer(astCtx->getCurrentNamedModule(), out);
858 } else {
859 fnName += "_GLOBAL__sub_I_";
860 fnName += getTransformedFileName(mlirModule);
861 }
862
863 CIRBaseBuilderTy builder(getContext());
864 builder.setInsertionPointToEnd(&mlirModule.getBodyRegion().back());
865 auto fnType = cir::FuncType::get({}, builder.getVoidTy());
866 cir::FuncOp f =
867 buildRuntimeFunction(builder, fnName, mlirModule.getLoc(), fnType,
868 cir::GlobalLinkageKind::ExternalLinkage);
869 builder.setInsertionPointToStart(f.addEntryBlock());
870 for (cir::FuncOp &f : dynamicInitializers)
871 builder.createCallOp(f.getLoc(), f, {});
872 // Add the global init function (not the individual ctor functions) to the
873 // global ctor list.
874 globalCtorList.emplace_back(fnName,
875 cir::GlobalCtorAttr::getDefaultPriority());
876
877 cir::ReturnOp::create(builder, f.getLoc());
878}
879
880void LoweringPreparePass::lowerDynamicCastOp(DynamicCastOp op) {
881 CIRBaseBuilderTy builder(getContext());
882 builder.setInsertionPointAfter(op);
883
884 assert(astCtx && "AST context is not available during lowering prepare");
885 auto loweredValue = cxxABI->lowerDynamicCast(builder, *astCtx, op);
886
887 op.replaceAllUsesWith(loweredValue);
888 op.erase();
889}
890
892 clang::ASTContext *astCtx,
893 mlir::Operation *op, mlir::Type eltTy,
894 mlir::Value arrayAddr, uint64_t arrayLen,
895 bool isCtor) {
896 // Generate loop to call into ctor/dtor for every element.
897 mlir::Location loc = op->getLoc();
898
899 // TODO: instead of getting the size from the AST context, create alias for
900 // PtrDiffTy and unify with CIRGen stuff.
901 const unsigned sizeTypeSize =
902 astCtx->getTypeSize(astCtx->getSignedSizeType());
903 uint64_t endOffset = isCtor ? arrayLen : arrayLen - 1;
904 mlir::Value endOffsetVal =
905 builder.getUnsignedInt(loc, endOffset, sizeTypeSize);
906
907 auto begin = cir::CastOp::create(builder, loc, eltTy,
908 cir::CastKind::array_to_ptrdecay, arrayAddr);
909 mlir::Value end =
910 cir::PtrStrideOp::create(builder, loc, eltTy, begin, endOffsetVal);
911 mlir::Value start = isCtor ? begin : end;
912 mlir::Value stop = isCtor ? end : begin;
913
914 mlir::Value tmpAddr = builder.createAlloca(
915 loc, /*addr type*/ builder.getPointerTo(eltTy),
916 /*var type*/ eltTy, "__array_idx", builder.getAlignmentAttr(1));
917 builder.createStore(loc, start, tmpAddr);
918
919 cir::DoWhileOp loop = builder.createDoWhile(
920 loc,
921 /*condBuilder=*/
922 [&](mlir::OpBuilder &b, mlir::Location loc) {
923 auto currentElement = b.create<cir::LoadOp>(loc, eltTy, tmpAddr);
924 mlir::Type boolTy = cir::BoolType::get(b.getContext());
925 auto cmp = builder.create<cir::CmpOp>(loc, boolTy, cir::CmpOpKind::ne,
926 currentElement, stop);
927 builder.createCondition(cmp);
928 },
929 /*bodyBuilder=*/
930 [&](mlir::OpBuilder &b, mlir::Location loc) {
931 auto currentElement = b.create<cir::LoadOp>(loc, eltTy, tmpAddr);
932
933 cir::CallOp ctorCall;
934 op->walk([&](cir::CallOp c) { ctorCall = c; });
935 assert(ctorCall && "expected ctor call");
936
937 // Array elements get constructed in order but destructed in reverse.
938 mlir::Value stride;
939 if (isCtor)
940 stride = builder.getUnsignedInt(loc, 1, sizeTypeSize);
941 else
942 stride = builder.getSignedInt(loc, -1, sizeTypeSize);
943
944 ctorCall->moveBefore(stride.getDefiningOp());
945 ctorCall->setOperand(0, currentElement);
946 auto nextElement = cir::PtrStrideOp::create(builder, loc, eltTy,
947 currentElement, stride);
948
949 // Store the element pointer to the temporary variable
950 builder.createStore(loc, nextElement, tmpAddr);
951 builder.createYield(loc);
952 });
953
954 op->replaceAllUsesWith(loop);
955 op->erase();
956}
957
958void LoweringPreparePass::lowerArrayDtor(cir::ArrayDtor op) {
959 CIRBaseBuilderTy builder(getContext());
960 builder.setInsertionPointAfter(op.getOperation());
961
962 mlir::Type eltTy = op->getRegion(0).getArgument(0).getType();
964 auto arrayLen =
965 mlir::cast<cir::ArrayType>(op.getAddr().getType().getPointee()).getSize();
966 lowerArrayDtorCtorIntoLoop(builder, astCtx, op, eltTy, op.getAddr(), arrayLen,
967 false);
968}
969
970void LoweringPreparePass::lowerArrayCtor(cir::ArrayCtor op) {
971 cir::CIRBaseBuilderTy builder(getContext());
972 builder.setInsertionPointAfter(op.getOperation());
973
974 mlir::Type eltTy = op->getRegion(0).getArgument(0).getType();
976 auto arrayLen =
977 mlir::cast<cir::ArrayType>(op.getAddr().getType().getPointee()).getSize();
978 lowerArrayDtorCtorIntoLoop(builder, astCtx, op, eltTy, op.getAddr(), arrayLen,
979 true);
980}
981
982void LoweringPreparePass::runOnOp(mlir::Operation *op) {
983 if (auto arrayCtor = dyn_cast<cir::ArrayCtor>(op)) {
984 lowerArrayCtor(arrayCtor);
985 } else if (auto arrayDtor = dyn_cast<cir::ArrayDtor>(op)) {
986 lowerArrayDtor(arrayDtor);
987 } else if (auto cast = mlir::dyn_cast<cir::CastOp>(op)) {
988 lowerCastOp(cast);
989 } else if (auto complexDiv = mlir::dyn_cast<cir::ComplexDivOp>(op)) {
990 lowerComplexDivOp(complexDiv);
991 } else if (auto complexMul = mlir::dyn_cast<cir::ComplexMulOp>(op)) {
992 lowerComplexMulOp(complexMul);
993 } else if (auto glob = mlir::dyn_cast<cir::GlobalOp>(op)) {
994 lowerGlobalOp(glob);
995 } else if (auto dynamicCast = mlir::dyn_cast<cir::DynamicCastOp>(op)) {
996 lowerDynamicCastOp(dynamicCast);
997 } else if (auto unary = mlir::dyn_cast<cir::UnaryOp>(op)) {
998 lowerUnaryOp(unary);
999 } else if (auto fnOp = dyn_cast<cir::FuncOp>(op)) {
1000 if (auto globalCtor = fnOp.getGlobalCtorPriority())
1001 globalCtorList.emplace_back(fnOp.getName(), globalCtor.value());
1002 else if (auto globalDtor = fnOp.getGlobalDtorPriority())
1003 globalDtorList.emplace_back(fnOp.getName(), globalDtor.value());
1004 }
1005}
1006
1007void LoweringPreparePass::runOnOperation() {
1008 mlir::Operation *op = getOperation();
1009 if (isa<::mlir::ModuleOp>(op))
1010 mlirModule = cast<::mlir::ModuleOp>(op);
1011
1012 llvm::SmallVector<mlir::Operation *> opsToTransform;
1013
1014 op->walk([&](mlir::Operation *op) {
1015 if (mlir::isa<cir::ArrayCtor, cir::ArrayDtor, cir::CastOp,
1016 cir::ComplexMulOp, cir::ComplexDivOp, cir::DynamicCastOp,
1017 cir::FuncOp, cir::GlobalOp, cir::UnaryOp>(op))
1018 opsToTransform.push_back(op);
1019 });
1020
1021 for (mlir::Operation *o : opsToTransform)
1022 runOnOp(o);
1023
1024 buildCXXGlobalInitFunc();
1025 buildGlobalCtorDtorList();
1026}
1027
1028std::unique_ptr<Pass> mlir::createLoweringPreparePass() {
1029 return std::make_unique<LoweringPreparePass>();
1030}
1031
1032std::unique_ptr<Pass>
1034 auto pass = std::make_unique<LoweringPreparePass>();
1035 pass->setASTContext(astCtx);
1036 return std::move(pass);
1037}
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 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.
__device__ __2f16 b
__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)
static LoweringPrepareCXXABI * createItaniumABI()
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
MangleContext * createMangleContext(const TargetInfo *T=nullptr)
If T is null pointer, assume the target in ASTContext.
const LangOptions & getLangOpts() const
Definition ASTContext.h:926
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:891
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.
Definition Module.h:664
Exposes information about the current target.
Definition TargetInfo.h:226
const llvm::fltSemantics & getDoubleFormat() const
Definition TargetInfo.h:798
const llvm::fltSemantics & getHalfFormat() const
Definition TargetInfo.h:783
const llvm::fltSemantics & getBFloat16Format() const
Definition TargetInfo.h:793
const llvm::fltSemantics & getLongDoubleFormat() const
Definition TargetInfo.h:804
const llvm::fltSemantics & getFloatFormat() const
Definition TargetInfo.h:788
const llvm::fltSemantics & getFloat128Format() const
Definition TargetInfo.h:812
Defines the clang::TargetInfo interface.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr, CxxCtorInitializer, and TypeLoc) selects th...
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_.
Definition CharInfo.h:168
unsigned int uint32_t
std::unique_ptr< Pass > createLoweringPreparePass()
static bool opGlobalThreadLocal()
static bool opGlobalAnnotations()
static bool opGlobalCtorPriority()
static bool loweringPrepareX86CXXABI()
static bool opFuncExtraAttrs()
static bool fastMathFlags()
static bool loweringPrepareAArch64XXABI()
static bool astVarDeclInterface()