clang 22.0.0git
CIRBaseBuilder.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_CIR_DIALECT_BUILDER_CIRBASEBUILDER_H
10#define LLVM_CLANG_CIR_DIALECT_BUILDER_CIRBASEBUILDER_H
11
12#include "clang/AST/CharUnits.h"
17#include "llvm/ADT/STLForwardCompat.h"
18#include "llvm/Support/ErrorHandling.h"
19
20#include "mlir/IR/Builders.h"
21#include "mlir/IR/BuiltinAttributes.h"
22#include "mlir/IR/Location.h"
23#include "mlir/IR/Types.h"
24
25namespace cir {
26
27enum class OverflowBehavior {
28 None = 0,
29 NoSignedWrap = 1 << 0,
31 Saturated = 1 << 2,
32};
33
35 return static_cast<OverflowBehavior>(llvm::to_underlying(a) |
36 llvm::to_underlying(b));
37}
38
40 return static_cast<OverflowBehavior>(llvm::to_underlying(a) &
41 llvm::to_underlying(b));
42}
43
46 a = a | b;
47 return a;
48}
49
52 a = a & b;
53 return a;
54}
55
56class CIRBaseBuilderTy : public mlir::OpBuilder {
57
58public:
59 CIRBaseBuilderTy(mlir::MLIRContext &mlirContext)
60 : mlir::OpBuilder(&mlirContext) {}
61 CIRBaseBuilderTy(mlir::OpBuilder &builder) : mlir::OpBuilder(builder) {}
62
63 mlir::Value getConstAPInt(mlir::Location loc, mlir::Type typ,
64 const llvm::APInt &val) {
65 return cir::ConstantOp::create(*this, loc, cir::IntAttr::get(typ, val));
66 }
67
68 cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr) {
69 return cir::ConstantOp::create(*this, loc, attr);
70 }
71
72 cir::ConstantOp getConstantInt(mlir::Location loc, mlir::Type ty,
73 int64_t value) {
74 return getConstant(loc, cir::IntAttr::get(ty, value));
75 }
76
77 mlir::Value getSignedInt(mlir::Location loc, int64_t val, unsigned numBits) {
78 auto type = cir::IntType::get(getContext(), numBits, /*isSigned=*/true);
79 return getConstAPInt(loc, type,
80 llvm::APInt(numBits, val, /*isSigned=*/true));
81 }
82
83 mlir::Value getUnsignedInt(mlir::Location loc, uint64_t val,
84 unsigned numBits) {
85 auto type = cir::IntType::get(getContext(), numBits, /*isSigned=*/false);
86 return getConstAPInt(loc, type, llvm::APInt(numBits, val));
87 }
88
89 // Creates constant null value for integral type ty.
90 cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc) {
91 return getConstant(loc, getZeroInitAttr(ty));
92 }
93
94 mlir::TypedAttr getConstNullPtrAttr(mlir::Type t) {
95 assert(mlir::isa<cir::PointerType>(t) && "expected cir.ptr");
96 return getConstPtrAttr(t, 0);
97 }
98
99 mlir::TypedAttr getZeroInitAttr(mlir::Type ty) {
100 if (mlir::isa<cir::IntType>(ty))
101 return cir::IntAttr::get(ty, 0);
102 if (cir::isAnyFloatingPointType(ty))
103 return cir::FPAttr::getZero(ty);
104 if (auto complexType = mlir::dyn_cast<cir::ComplexType>(ty))
105 return cir::ZeroAttr::get(complexType);
106 if (auto arrTy = mlir::dyn_cast<cir::ArrayType>(ty))
107 return cir::ZeroAttr::get(arrTy);
108 if (auto vecTy = mlir::dyn_cast<cir::VectorType>(ty))
109 return cir::ZeroAttr::get(vecTy);
110 if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty))
111 return getConstNullPtrAttr(ptrTy);
112 if (auto recordTy = mlir::dyn_cast<cir::RecordType>(ty))
113 return cir::ZeroAttr::get(recordTy);
114 if (mlir::isa<cir::BoolType>(ty)) {
115 return getFalseAttr();
116 }
117 llvm_unreachable("Zero initializer for given type is NYI");
118 }
119
120 cir::ConstantOp getBool(bool state, mlir::Location loc) {
121 return cir::ConstantOp::create(*this, loc, getCIRBoolAttr(state));
122 }
123 cir::ConstantOp getFalse(mlir::Location loc) { return getBool(false, loc); }
124 cir::ConstantOp getTrue(mlir::Location loc) { return getBool(true, loc); }
125
126 cir::BoolType getBoolTy() { return cir::BoolType::get(getContext()); }
127
128 cir::PointerType getPointerTo(mlir::Type ty) {
129 return cir::PointerType::get(ty);
130 }
131
132 cir::PointerType getVoidPtrTy() {
133 return getPointerTo(cir::VoidType::get(getContext()));
134 }
135
136 cir::BoolAttr getCIRBoolAttr(bool state) {
137 return cir::BoolAttr::get(getContext(), state);
138 }
139
140 cir::BoolAttr getTrueAttr() { return getCIRBoolAttr(true); }
141 cir::BoolAttr getFalseAttr() { return getCIRBoolAttr(false); }
142
143 mlir::Value createComplexCreate(mlir::Location loc, mlir::Value real,
144 mlir::Value imag) {
145 auto resultComplexTy = cir::ComplexType::get(real.getType());
146 return cir::ComplexCreateOp::create(*this, loc, resultComplexTy, real,
147 imag);
148 }
149
150 mlir::Value createComplexReal(mlir::Location loc, mlir::Value operand) {
151 auto operandTy = mlir::cast<cir::ComplexType>(operand.getType());
152 return cir::ComplexRealOp::create(*this, loc, operandTy.getElementType(),
153 operand);
154 }
155
156 mlir::Value createComplexImag(mlir::Location loc, mlir::Value operand) {
157 auto operandTy = mlir::cast<cir::ComplexType>(operand.getType());
158 return cir::ComplexImagOp::create(*this, loc, operandTy.getElementType(),
159 operand);
160 }
161
162 cir::LoadOp createLoad(mlir::Location loc, mlir::Value ptr,
163 bool isVolatile = false, uint64_t alignment = 0) {
164 mlir::IntegerAttr alignmentAttr = getAlignmentAttr(alignment);
165 return cir::LoadOp::create(*this, loc, ptr, /*isDeref=*/false, isVolatile,
166 alignmentAttr, cir::MemOrderAttr{});
167 }
168
169 mlir::Value createAlignedLoad(mlir::Location loc, mlir::Value ptr,
170 uint64_t alignment) {
171 return createLoad(loc, ptr, /*isVolatile=*/false, alignment);
172 }
173
174 mlir::Value createNot(mlir::Value value) {
175 return cir::UnaryOp::create(*this, value.getLoc(), value.getType(),
176 cir::UnaryOpKind::Not, value);
177 }
178
179 /// Create a do-while operation.
180 cir::DoWhileOp createDoWhile(
181 mlir::Location loc,
182 llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> condBuilder,
183 llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> bodyBuilder) {
184 return cir::DoWhileOp::create(*this, loc, condBuilder, bodyBuilder);
185 }
186
187 /// Create a while operation.
188 cir::WhileOp createWhile(
189 mlir::Location loc,
190 llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> condBuilder,
191 llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> bodyBuilder) {
192 return cir::WhileOp::create(*this, loc, condBuilder, bodyBuilder);
193 }
194
195 /// Create a for operation.
196 cir::ForOp createFor(
197 mlir::Location loc,
198 llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> condBuilder,
199 llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> bodyBuilder,
200 llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> stepBuilder) {
201 return cir::ForOp::create(*this, loc, condBuilder, bodyBuilder,
202 stepBuilder);
203 }
204
205 /// Create a break operation.
206 cir::BreakOp createBreak(mlir::Location loc) {
207 return cir::BreakOp::create(*this, loc);
208 }
209
210 /// Create a continue operation.
211 cir::ContinueOp createContinue(mlir::Location loc) {
212 return cir::ContinueOp::create(*this, loc);
213 }
214
215 mlir::Value createUnaryOp(mlir::Location loc, cir::UnaryOpKind kind,
216 mlir::Value operand) {
217 return cir::UnaryOp::create(*this, loc, kind, operand);
218 }
219
220 mlir::TypedAttr getConstPtrAttr(mlir::Type type, int64_t value) {
221 return cir::ConstPtrAttr::get(type, getI64IntegerAttr(value));
222 }
223
224 mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType,
225 mlir::Type type, llvm::StringRef name,
226 mlir::IntegerAttr alignment,
227 mlir::Value dynAllocSize) {
228 return cir::AllocaOp::create(*this, loc, addrType, type, name, alignment,
229 dynAllocSize);
230 }
231
232 mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType,
233 mlir::Type type, llvm::StringRef name,
234 clang::CharUnits alignment,
235 mlir::Value dynAllocSize) {
236 mlir::IntegerAttr alignmentAttr = getAlignmentAttr(alignment);
237 return createAlloca(loc, addrType, type, name, alignmentAttr, dynAllocSize);
238 }
239
240 mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType,
241 mlir::Type type, llvm::StringRef name,
242 mlir::IntegerAttr alignment) {
243 return cir::AllocaOp::create(*this, loc, addrType, type, name, alignment);
244 }
245
246 /// Get constant address of a global variable as an MLIR attribute.
247 /// This wrapper infers the attribute type through the global op.
248 cir::GlobalViewAttr getGlobalViewAttr(cir::GlobalOp globalOp,
249 mlir::ArrayAttr indices = {}) {
250 cir::PointerType type = getPointerTo(globalOp.getSymType());
251 return getGlobalViewAttr(type, globalOp, indices);
252 }
253
254 /// Get constant address of a global variable as an MLIR attribute.
255 cir::GlobalViewAttr getGlobalViewAttr(cir::PointerType type,
256 cir::GlobalOp globalOp,
257 mlir::ArrayAttr indices = {}) {
258 auto symbol = mlir::FlatSymbolRefAttr::get(globalOp.getSymNameAttr());
259 return cir::GlobalViewAttr::get(type, symbol, indices);
260 }
261
262 mlir::Value createGetGlobal(mlir::Location loc, cir::GlobalOp global) {
264 return cir::GetGlobalOp::create(
265 *this, loc, getPointerTo(global.getSymType()), global.getSymName());
266 }
267
268 mlir::Value createGetGlobal(cir::GlobalOp global) {
269 return createGetGlobal(global.getLoc(), global);
270 }
271
272 /// Create a copy with inferred length.
273 cir::CopyOp createCopy(mlir::Value dst, mlir::Value src) {
274 return cir::CopyOp::create(*this, dst.getLoc(), dst, src);
275 }
276
277 cir::StoreOp createStore(mlir::Location loc, mlir::Value val, mlir::Value dst,
278 bool isVolatile = false,
279 mlir::IntegerAttr align = {},
280 cir::MemOrderAttr order = {}) {
281 return cir::StoreOp::create(*this, loc, val, dst, isVolatile, align, order);
282 }
283
284 [[nodiscard]] cir::GlobalOp createGlobal(mlir::ModuleOp mlirModule,
285 mlir::Location loc,
286 mlir::StringRef name,
287 mlir::Type type, bool isConstant,
288 cir::GlobalLinkageKind linkage) {
289 mlir::OpBuilder::InsertionGuard guard(*this);
290 setInsertionPointToStart(mlirModule.getBody());
291 return cir::GlobalOp::create(*this, loc, name, type, isConstant, linkage);
292 }
293
294 cir::GetMemberOp createGetMember(mlir::Location loc, mlir::Type resultTy,
295 mlir::Value base, llvm::StringRef name,
296 unsigned index) {
297 return cir::GetMemberOp::create(*this, loc, resultTy, base, name, index);
298 }
299
300 mlir::Value createDummyValue(mlir::Location loc, mlir::Type type,
301 clang::CharUnits alignment) {
302 mlir::IntegerAttr alignmentAttr = getAlignmentAttr(alignment);
303 auto addr = createAlloca(loc, getPointerTo(type), type, {}, alignmentAttr);
304 return cir::LoadOp::create(*this, loc, addr, /*isDeref=*/false,
305 /*isVolatile=*/false, alignmentAttr,
306 /*mem_order=*/{});
307 }
308
309 cir::PtrStrideOp createPtrStride(mlir::Location loc, mlir::Value base,
310 mlir::Value stride) {
311 return cir::PtrStrideOp::create(*this, loc, base.getType(), base, stride);
312 }
313
314 //===--------------------------------------------------------------------===//
315 // Call operators
316 //===--------------------------------------------------------------------===//
317
318 cir::CallOp createCallOp(mlir::Location loc, mlir::SymbolRefAttr callee,
319 mlir::Type returnType, mlir::ValueRange operands,
321 auto op = cir::CallOp::create(*this, loc, callee, returnType, operands);
322 op->setAttrs(attrs);
323 return op;
324 }
325
326 cir::CallOp createCallOp(mlir::Location loc, cir::FuncOp callee,
327 mlir::ValueRange operands,
329 return createCallOp(loc, mlir::SymbolRefAttr::get(callee),
330 callee.getFunctionType().getReturnType(), operands,
331 attrs);
332 }
333
334 cir::CallOp
335 createIndirectCallOp(mlir::Location loc, mlir::Value indirectTarget,
336 cir::FuncType funcType, mlir::ValueRange operands,
338 llvm::SmallVector<mlir::Value> resOperands{indirectTarget};
339 resOperands.append(operands.begin(), operands.end());
340 return createCallOp(loc, mlir::SymbolRefAttr(), funcType.getReturnType(),
341 resOperands, attrs);
342 }
343
344 cir::CallOp createTryCallOp(
345 mlir::Location loc, mlir::SymbolRefAttr callee = mlir::SymbolRefAttr(),
346 mlir::Type returnType = cir::VoidType(),
347 mlir::ValueRange operands = mlir::ValueRange(),
348 [[maybe_unused]] cir::SideEffect sideEffect = cir::SideEffect::All) {
351 return createCallOp(loc, callee, returnType, operands);
352 }
353
354 cir::CallOp createTryCallOp(
355 mlir::Location loc, cir::FuncOp callee, mlir::ValueRange operands,
356 [[maybe_unused]] cir::SideEffect sideEffect = cir::SideEffect::All) {
359 return createTryCallOp(loc, mlir::SymbolRefAttr::get(callee),
360 callee.getFunctionType().getReturnType(), operands);
361 }
362
363 //===--------------------------------------------------------------------===//
364 // Cast/Conversion Operators
365 //===--------------------------------------------------------------------===//
366
367 mlir::Value createCast(mlir::Location loc, cir::CastKind kind,
368 mlir::Value src, mlir::Type newTy) {
369 if (newTy == src.getType())
370 return src;
371 return cir::CastOp::create(*this, loc, newTy, kind, src);
372 }
373
374 mlir::Value createCast(cir::CastKind kind, mlir::Value src,
375 mlir::Type newTy) {
376 if (newTy == src.getType())
377 return src;
378 return createCast(src.getLoc(), kind, src, newTy);
379 }
380
381 mlir::Value createIntCast(mlir::Value src, mlir::Type newTy) {
382 return createCast(cir::CastKind::integral, src, newTy);
383 }
384
385 mlir::Value createIntToPtr(mlir::Value src, mlir::Type newTy) {
386 return createCast(cir::CastKind::int_to_ptr, src, newTy);
387 }
388
389 mlir::Value createPtrToInt(mlir::Value src, mlir::Type newTy) {
390 return createCast(cir::CastKind::ptr_to_int, src, newTy);
391 }
392
393 mlir::Value createPtrToBoolCast(mlir::Value v) {
394 return createCast(cir::CastKind::ptr_to_bool, v, getBoolTy());
395 }
396
397 mlir::Value createBoolToInt(mlir::Value src, mlir::Type newTy) {
398 return createCast(cir::CastKind::bool_to_int, src, newTy);
399 }
400
401 mlir::Value createBitcast(mlir::Value src, mlir::Type newTy) {
402 return createCast(cir::CastKind::bitcast, src, newTy);
403 }
404
405 mlir::Value createBitcast(mlir::Location loc, mlir::Value src,
406 mlir::Type newTy) {
407 return createCast(loc, cir::CastKind::bitcast, src, newTy);
408 }
409
410 mlir::Value createPtrBitcast(mlir::Value src, mlir::Type newPointeeTy) {
411 assert(mlir::isa<cir::PointerType>(src.getType()) && "expected ptr src");
412 return createBitcast(src, getPointerTo(newPointeeTy));
413 }
414
415 //===--------------------------------------------------------------------===//
416 // Binary Operators
417 //===--------------------------------------------------------------------===//
418
419 mlir::Value createBinop(mlir::Location loc, mlir::Value lhs,
420 cir::BinOpKind kind, mlir::Value rhs) {
421 return cir::BinOp::create(*this, loc, lhs.getType(), kind, lhs, rhs);
422 }
423
424 mlir::Value createLowBitsSet(mlir::Location loc, unsigned size,
425 unsigned bits) {
426 llvm::APInt val = llvm::APInt::getLowBitsSet(size, bits);
427 auto type = cir::IntType::get(getContext(), size, /*isSigned=*/false);
428 return getConstAPInt(loc, type, val);
429 }
430
431 mlir::Value createAnd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
432 return createBinop(loc, lhs, cir::BinOpKind::And, rhs);
433 }
434
435 mlir::Value createOr(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
436 return createBinop(loc, lhs, cir::BinOpKind::Or, rhs);
437 }
438
439 mlir::Value createSelect(mlir::Location loc, mlir::Value condition,
440 mlir::Value trueValue, mlir::Value falseValue) {
441 assert(trueValue.getType() == falseValue.getType() &&
442 "trueValue and falseValue should have the same type");
443 return cir::SelectOp::create(*this, loc, trueValue.getType(), condition,
444 trueValue, falseValue);
445 }
446
447 mlir::Value createLogicalAnd(mlir::Location loc, mlir::Value lhs,
448 mlir::Value rhs) {
449 return createSelect(loc, lhs, rhs, getBool(false, loc));
450 }
451
452 mlir::Value createLogicalOr(mlir::Location loc, mlir::Value lhs,
453 mlir::Value rhs) {
454 return createSelect(loc, lhs, getBool(true, loc), rhs);
455 }
456
457 mlir::Value createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
459 auto op = cir::BinOp::create(*this, loc, lhs.getType(), cir::BinOpKind::Mul,
460 lhs, rhs);
461 op.setNoUnsignedWrap(
462 llvm::to_underlying(ob & OverflowBehavior::NoUnsignedWrap));
463 op.setNoSignedWrap(
464 llvm::to_underlying(ob & OverflowBehavior::NoSignedWrap));
465 return op;
466 }
467 mlir::Value createNSWMul(mlir::Location loc, mlir::Value lhs,
468 mlir::Value rhs) {
469 return createMul(loc, lhs, rhs, OverflowBehavior::NoSignedWrap);
470 }
471 mlir::Value createNUWAMul(mlir::Location loc, mlir::Value lhs,
472 mlir::Value rhs) {
473 return createMul(loc, lhs, rhs, OverflowBehavior::NoUnsignedWrap);
474 }
475
476 mlir::Value createSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
478 auto op = cir::BinOp::create(*this, loc, lhs.getType(), cir::BinOpKind::Sub,
479 lhs, rhs);
480 op.setNoUnsignedWrap(
481 llvm::to_underlying(ob & OverflowBehavior::NoUnsignedWrap));
482 op.setNoSignedWrap(
483 llvm::to_underlying(ob & OverflowBehavior::NoSignedWrap));
484 op.setSaturated(llvm::to_underlying(ob & OverflowBehavior::Saturated));
485 return op;
486 }
487
488 mlir::Value createNSWSub(mlir::Location loc, mlir::Value lhs,
489 mlir::Value rhs) {
490 return createSub(loc, lhs, rhs, OverflowBehavior::NoSignedWrap);
491 }
492
493 mlir::Value createNUWSub(mlir::Location loc, mlir::Value lhs,
494 mlir::Value rhs) {
495 return createSub(loc, lhs, rhs, OverflowBehavior::NoUnsignedWrap);
496 }
497
498 mlir::Value createAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
500 auto op = cir::BinOp::create(*this, loc, lhs.getType(), cir::BinOpKind::Add,
501 lhs, rhs);
502 op.setNoUnsignedWrap(
503 llvm::to_underlying(ob & OverflowBehavior::NoUnsignedWrap));
504 op.setNoSignedWrap(
505 llvm::to_underlying(ob & OverflowBehavior::NoSignedWrap));
506 op.setSaturated(llvm::to_underlying(ob & OverflowBehavior::Saturated));
507 return op;
508 }
509
510 mlir::Value createNSWAdd(mlir::Location loc, mlir::Value lhs,
511 mlir::Value rhs) {
512 return createAdd(loc, lhs, rhs, OverflowBehavior::NoSignedWrap);
513 }
514
515 mlir::Value createNUWAdd(mlir::Location loc, mlir::Value lhs,
516 mlir::Value rhs) {
517 return createAdd(loc, lhs, rhs, OverflowBehavior::NoUnsignedWrap);
518 }
519
520 cir::CmpOp createCompare(mlir::Location loc, cir::CmpOpKind kind,
521 mlir::Value lhs, mlir::Value rhs) {
522 return cir::CmpOp::create(*this, loc, getBoolTy(), kind, lhs, rhs);
523 }
524
525 mlir::Value createIsNaN(mlir::Location loc, mlir::Value operand) {
526 return createCompare(loc, cir::CmpOpKind::ne, operand, operand);
527 }
528
529 mlir::Value createShift(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
530 bool isShiftLeft) {
531 return cir::ShiftOp::create(*this, loc, lhs.getType(), lhs, rhs,
532 isShiftLeft);
533 }
534
535 mlir::Value createShift(mlir::Location loc, mlir::Value lhs,
536 const llvm::APInt &rhs, bool isShiftLeft) {
537 return createShift(loc, lhs, getConstAPInt(loc, lhs.getType(), rhs),
538 isShiftLeft);
539 }
540
541 mlir::Value createShift(mlir::Location loc, mlir::Value lhs, unsigned bits,
542 bool isShiftLeft) {
543 auto width = mlir::dyn_cast<cir::IntType>(lhs.getType()).getWidth();
544 auto shift = llvm::APInt(width, bits);
545 return createShift(loc, lhs, shift, isShiftLeft);
546 }
547
548 mlir::Value createShiftLeft(mlir::Location loc, mlir::Value lhs,
549 unsigned bits) {
550 return createShift(loc, lhs, bits, true);
551 }
552
553 mlir::Value createShiftRight(mlir::Location loc, mlir::Value lhs,
554 unsigned bits) {
555 return createShift(loc, lhs, bits, false);
556 }
557
558 mlir::Value createShiftLeft(mlir::Location loc, mlir::Value lhs,
559 mlir::Value rhs) {
560 return createShift(loc, lhs, rhs, true);
561 }
562
563 mlir::Value createShiftRight(mlir::Location loc, mlir::Value lhs,
564 mlir::Value rhs) {
565 return createShift(loc, lhs, rhs, false);
566 }
567
568 //
569 // Block handling helpers
570 // ----------------------
571 //
572 static OpBuilder::InsertPoint getBestAllocaInsertPoint(mlir::Block *block) {
573 auto last =
574 std::find_if(block->rbegin(), block->rend(), [](mlir::Operation &op) {
575 return mlir::isa<cir::AllocaOp, cir::LabelOp>(&op);
576 });
577
578 if (last != block->rend())
579 return OpBuilder::InsertPoint(block, ++mlir::Block::iterator(&*last));
580 return OpBuilder::InsertPoint(block, block->begin());
581 };
582
583 //
584 // Alignment and size helpers
585 //
586
587 // Note that mlir::IntegerType is used instead of cir::IntType here because we
588 // don't need sign information for these to be useful, so keep it simple.
589
590 // For 0 alignment, any overload of `getAlignmentAttr` returns an empty
591 // attribute.
592 mlir::IntegerAttr getAlignmentAttr(clang::CharUnits alignment) {
593 return getAlignmentAttr(alignment.getQuantity());
594 }
595
596 mlir::IntegerAttr getAlignmentAttr(llvm::Align alignment) {
597 return getAlignmentAttr(alignment.value());
598 }
599
600 mlir::IntegerAttr getAlignmentAttr(int64_t alignment) {
601 return alignment ? getI64IntegerAttr(alignment) : mlir::IntegerAttr();
602 }
603
604 mlir::IntegerAttr getSizeFromCharUnits(clang::CharUnits size) {
605 return getI64IntegerAttr(size.getQuantity());
606 }
607
608 /// Create a loop condition.
609 cir::ConditionOp createCondition(mlir::Value condition) {
610 return cir::ConditionOp::create(*this, condition.getLoc(), condition);
611 }
612
613 /// Create a yield operation.
614 cir::YieldOp createYield(mlir::Location loc, mlir::ValueRange value = {}) {
615 return cir::YieldOp::create(*this, loc, value);
616 }
617};
618
619} // namespace cir
620
621#endif
__device__ __2f16 b
mlir::Value createNSWSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::ConstantOp getBool(bool state, mlir::Location loc)
mlir::Value createShift(mlir::Location loc, mlir::Value lhs, unsigned bits, bool isShiftLeft)
cir::WhileOp createWhile(mlir::Location loc, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> condBuilder, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> bodyBuilder)
Create a while operation.
cir::BreakOp createBreak(mlir::Location loc)
Create a break operation.
mlir::TypedAttr getConstNullPtrAttr(mlir::Type t)
mlir::Value createGetGlobal(mlir::Location loc, cir::GlobalOp global)
mlir::IntegerAttr getAlignmentAttr(int64_t alignment)
mlir::Value createShift(mlir::Location loc, mlir::Value lhs, const llvm::APInt &rhs, bool isShiftLeft)
mlir::Value getConstAPInt(mlir::Location loc, mlir::Type typ, const llvm::APInt &val)
cir::GlobalViewAttr getGlobalViewAttr(cir::PointerType type, cir::GlobalOp globalOp, mlir::ArrayAttr indices={})
Get constant address of a global variable as an MLIR attribute.
mlir::Value createCast(cir::CastKind kind, mlir::Value src, mlir::Type newTy)
mlir::Value createLogicalOr(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createShift(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, bool isShiftLeft)
cir::ConditionOp createCondition(mlir::Value condition)
Create a loop condition.
mlir::Value createLowBitsSet(mlir::Location loc, unsigned size, unsigned bits)
mlir::Value createNSWAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::GlobalViewAttr getGlobalViewAttr(cir::GlobalOp globalOp, mlir::ArrayAttr indices={})
Get constant address of a global variable as an MLIR attribute.
cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc)
cir::BoolAttr getCIRBoolAttr(bool state)
mlir::Value createBoolToInt(mlir::Value src, mlir::Type newTy)
cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr)
mlir::Value createNUWAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createOr(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createShiftLeft(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createCast(mlir::Location loc, cir::CastKind kind, mlir::Value src, mlir::Type newTy)
mlir::IntegerAttr getSizeFromCharUnits(clang::CharUnits size)
cir::PtrStrideOp createPtrStride(mlir::Location loc, mlir::Value base, mlir::Value stride)
mlir::Value createIntToPtr(mlir::Value src, mlir::Type newTy)
cir::CallOp createCallOp(mlir::Location loc, cir::FuncOp callee, mlir::ValueRange operands, llvm::ArrayRef< mlir::NamedAttribute > attrs={})
cir::ForOp createFor(mlir::Location loc, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> condBuilder, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> bodyBuilder, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> stepBuilder)
Create a for operation.
static OpBuilder::InsertPoint getBestAllocaInsertPoint(mlir::Block *block)
mlir::Value createPtrToInt(mlir::Value src, mlir::Type newTy)
mlir::Value createNUWSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::ConstantOp getFalse(mlir::Location loc)
mlir::Value createAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowBehavior ob=OverflowBehavior::None)
cir::GetMemberOp createGetMember(mlir::Location loc, mlir::Type resultTy, mlir::Value base, llvm::StringRef name, unsigned index)
cir::PointerType getPointerTo(mlir::Type ty)
mlir::Value createNot(mlir::Value value)
mlir::Value createComplexImag(mlir::Location loc, mlir::Value operand)
cir::ConstantOp getTrue(mlir::Location loc)
mlir::Value createNSWMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createGetGlobal(cir::GlobalOp global)
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.
mlir::Value createNUWAMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::CallOp createCallOp(mlir::Location loc, mlir::SymbolRefAttr callee, mlir::Type returnType, mlir::ValueRange operands, llvm::ArrayRef< mlir::NamedAttribute > attrs={})
mlir::Value createPtrBitcast(mlir::Value src, mlir::Type newPointeeTy)
cir::CallOp createTryCallOp(mlir::Location loc, mlir::SymbolRefAttr callee=mlir::SymbolRefAttr(), mlir::Type returnType=cir::VoidType(), mlir::ValueRange operands=mlir::ValueRange(), cir::SideEffect sideEffect=cir::SideEffect::All)
mlir::Value createShiftLeft(mlir::Location loc, mlir::Value lhs, unsigned bits)
mlir::Value createSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowBehavior ob=OverflowBehavior::Saturated)
mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType, mlir::Type type, llvm::StringRef name, clang::CharUnits alignment, mlir::Value dynAllocSize)
mlir::Value getSignedInt(mlir::Location loc, int64_t val, unsigned numBits)
mlir::Value createAnd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createIntCast(mlir::Value src, mlir::Type newTy)
cir::CallOp createTryCallOp(mlir::Location loc, cir::FuncOp callee, mlir::ValueRange operands, cir::SideEffect sideEffect=cir::SideEffect::All)
mlir::Value createBitcast(mlir::Value src, mlir::Type newTy)
CIRBaseBuilderTy(mlir::MLIRContext &mlirContext)
mlir::Value createBitcast(mlir::Location loc, mlir::Value src, mlir::Type newTy)
cir::GlobalOp createGlobal(mlir::ModuleOp mlirModule, mlir::Location loc, mlir::StringRef name, mlir::Type type, bool isConstant, cir::GlobalLinkageKind linkage)
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 createAlloca(mlir::Location loc, cir::PointerType addrType, mlir::Type type, llvm::StringRef name, mlir::IntegerAttr alignment)
mlir::Value createSelect(mlir::Location loc, mlir::Value condition, mlir::Value trueValue, mlir::Value falseValue)
cir::ContinueOp createContinue(mlir::Location loc)
Create a continue operation.
mlir::Value createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowBehavior ob=OverflowBehavior::None)
mlir::TypedAttr getZeroInitAttr(mlir::Type ty)
cir::LoadOp createLoad(mlir::Location loc, mlir::Value ptr, bool isVolatile=false, uint64_t alignment=0)
cir::CallOp createIndirectCallOp(mlir::Location loc, mlir::Value indirectTarget, cir::FuncType funcType, mlir::ValueRange operands, llvm::ArrayRef< mlir::NamedAttribute > attrs={})
CIRBaseBuilderTy(mlir::OpBuilder &builder)
cir::ConstantOp getConstantInt(mlir::Location loc, mlir::Type ty, int64_t value)
mlir::Value createComplexCreate(mlir::Location loc, mlir::Value real, mlir::Value imag)
cir::CopyOp createCopy(mlir::Value dst, mlir::Value src)
Create a copy with inferred length.
mlir::Value createPtrToBoolCast(mlir::Value v)
cir::BoolAttr getTrueAttr()
mlir::Value createShiftRight(mlir::Location loc, mlir::Value lhs, unsigned bits)
mlir::Value createIsNaN(mlir::Location loc, mlir::Value operand)
mlir::Value createAlignedLoad(mlir::Location loc, mlir::Value ptr, uint64_t alignment)
mlir::TypedAttr getConstPtrAttr(mlir::Type type, int64_t value)
mlir::Value createDummyValue(mlir::Location loc, mlir::Type type, clang::CharUnits alignment)
cir::BoolAttr getFalseAttr()
cir::PointerType getVoidPtrTy()
mlir::Value createShiftRight(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
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::IntegerAttr getAlignmentAttr(llvm::Align alignment)
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)
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
constexpr OverflowBehavior operator|(OverflowBehavior a, OverflowBehavior b)
constexpr OverflowBehavior operator&(OverflowBehavior a, OverflowBehavior b)
constexpr OverflowBehavior & operator|=(OverflowBehavior &a, OverflowBehavior b)
constexpr OverflowBehavior & operator&=(OverflowBehavior &a, OverflowBehavior b)
OverflowBehavior
static bool addressSpace()
static bool opCallSideEffect()
static bool opCallCallConv()