Thanks to visit codestin.com
Credit goes to clang.llvm.org

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 mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType,
247 mlir::Type type, llvm::StringRef name,
248 clang::CharUnits alignment) {
249 mlir::IntegerAttr alignmentAttr = getAlignmentAttr(alignment);
250 return createAlloca(loc, addrType, type, name, alignmentAttr);
251 }
252
253 /// Get constant address of a global variable as an MLIR attribute.
254 /// This wrapper infers the attribute type through the global op.
255 cir::GlobalViewAttr getGlobalViewAttr(cir::GlobalOp globalOp,
256 mlir::ArrayAttr indices = {}) {
257 cir::PointerType type = getPointerTo(globalOp.getSymType());
258 return getGlobalViewAttr(type, globalOp, indices);
259 }
260
261 /// Get constant address of a global variable as an MLIR attribute.
262 cir::GlobalViewAttr getGlobalViewAttr(cir::PointerType type,
263 cir::GlobalOp globalOp,
264 mlir::ArrayAttr indices = {}) {
265 auto symbol = mlir::FlatSymbolRefAttr::get(globalOp.getSymNameAttr());
266 return cir::GlobalViewAttr::get(type, symbol, indices);
267 }
268
269 mlir::Value createGetGlobal(mlir::Location loc, cir::GlobalOp global) {
271 return cir::GetGlobalOp::create(
272 *this, loc, getPointerTo(global.getSymType()), global.getSymName());
273 }
274
275 mlir::Value createGetGlobal(cir::GlobalOp global) {
276 return createGetGlobal(global.getLoc(), global);
277 }
278
279 /// Create a copy with inferred length.
280 cir::CopyOp createCopy(mlir::Value dst, mlir::Value src) {
281 return cir::CopyOp::create(*this, dst.getLoc(), dst, src);
282 }
283
284 cir::StoreOp createStore(mlir::Location loc, mlir::Value val, mlir::Value dst,
285 bool isVolatile = false,
286 mlir::IntegerAttr align = {},
287 cir::MemOrderAttr order = {}) {
288 return cir::StoreOp::create(*this, loc, val, dst, isVolatile, align, order);
289 }
290
291 [[nodiscard]] cir::GlobalOp createGlobal(mlir::ModuleOp mlirModule,
292 mlir::Location loc,
293 mlir::StringRef name,
294 mlir::Type type, bool isConstant,
295 cir::GlobalLinkageKind linkage) {
296 mlir::OpBuilder::InsertionGuard guard(*this);
297 setInsertionPointToStart(mlirModule.getBody());
298 return cir::GlobalOp::create(*this, loc, name, type, isConstant, linkage);
299 }
300
301 cir::GetMemberOp createGetMember(mlir::Location loc, mlir::Type resultTy,
302 mlir::Value base, llvm::StringRef name,
303 unsigned index) {
304 return cir::GetMemberOp::create(*this, loc, resultTy, base, name, index);
305 }
306
307 mlir::Value createDummyValue(mlir::Location loc, mlir::Type type,
308 clang::CharUnits alignment) {
309 mlir::IntegerAttr alignmentAttr = getAlignmentAttr(alignment);
310 auto addr = createAlloca(loc, getPointerTo(type), type, {}, alignmentAttr);
311 return cir::LoadOp::create(*this, loc, addr, /*isDeref=*/false,
312 /*isVolatile=*/false, alignmentAttr,
313 /*mem_order=*/{});
314 }
315
316 cir::PtrStrideOp createPtrStride(mlir::Location loc, mlir::Value base,
317 mlir::Value stride) {
318 return cir::PtrStrideOp::create(*this, loc, base.getType(), base, stride);
319 }
320
321 //===--------------------------------------------------------------------===//
322 // Call operators
323 //===--------------------------------------------------------------------===//
324
325 cir::CallOp createCallOp(mlir::Location loc, mlir::SymbolRefAttr callee,
326 mlir::Type returnType, mlir::ValueRange operands,
328 auto op = cir::CallOp::create(*this, loc, callee, returnType, operands);
329 op->setAttrs(attrs);
330 return op;
331 }
332
333 cir::CallOp createCallOp(mlir::Location loc, cir::FuncOp callee,
334 mlir::ValueRange operands,
336 return createCallOp(loc, mlir::SymbolRefAttr::get(callee),
337 callee.getFunctionType().getReturnType(), operands,
338 attrs);
339 }
340
341 cir::CallOp
342 createIndirectCallOp(mlir::Location loc, mlir::Value indirectTarget,
343 cir::FuncType funcType, mlir::ValueRange operands,
345 llvm::SmallVector<mlir::Value> resOperands{indirectTarget};
346 resOperands.append(operands.begin(), operands.end());
347 return createCallOp(loc, mlir::SymbolRefAttr(), funcType.getReturnType(),
348 resOperands, attrs);
349 }
350
351 cir::CallOp createTryCallOp(
352 mlir::Location loc, mlir::SymbolRefAttr callee = mlir::SymbolRefAttr(),
353 mlir::Type returnType = cir::VoidType(),
354 mlir::ValueRange operands = mlir::ValueRange(),
355 [[maybe_unused]] cir::SideEffect sideEffect = cir::SideEffect::All) {
358 return createCallOp(loc, callee, returnType, operands);
359 }
360
361 cir::CallOp createTryCallOp(
362 mlir::Location loc, cir::FuncOp callee, mlir::ValueRange operands,
363 [[maybe_unused]] cir::SideEffect sideEffect = cir::SideEffect::All) {
366 return createTryCallOp(loc, mlir::SymbolRefAttr::get(callee),
367 callee.getFunctionType().getReturnType(), operands);
368 }
369
370 //===--------------------------------------------------------------------===//
371 // Cast/Conversion Operators
372 //===--------------------------------------------------------------------===//
373
374 mlir::Value createCast(mlir::Location loc, cir::CastKind kind,
375 mlir::Value src, mlir::Type newTy) {
376 if (newTy == src.getType())
377 return src;
378 return cir::CastOp::create(*this, loc, newTy, kind, src);
379 }
380
381 mlir::Value createCast(cir::CastKind kind, mlir::Value src,
382 mlir::Type newTy) {
383 if (newTy == src.getType())
384 return src;
385 return createCast(src.getLoc(), kind, src, newTy);
386 }
387
388 mlir::Value createIntCast(mlir::Value src, mlir::Type newTy) {
389 return createCast(cir::CastKind::integral, src, newTy);
390 }
391
392 mlir::Value createIntToPtr(mlir::Value src, mlir::Type newTy) {
393 return createCast(cir::CastKind::int_to_ptr, src, newTy);
394 }
395
396 mlir::Value createPtrToInt(mlir::Value src, mlir::Type newTy) {
397 return createCast(cir::CastKind::ptr_to_int, src, newTy);
398 }
399
400 mlir::Value createPtrToBoolCast(mlir::Value v) {
401 return createCast(cir::CastKind::ptr_to_bool, v, getBoolTy());
402 }
403
404 mlir::Value createBoolToInt(mlir::Value src, mlir::Type newTy) {
405 return createCast(cir::CastKind::bool_to_int, src, newTy);
406 }
407
408 mlir::Value createBitcast(mlir::Value src, mlir::Type newTy) {
409 return createCast(cir::CastKind::bitcast, src, newTy);
410 }
411
412 mlir::Value createBitcast(mlir::Location loc, mlir::Value src,
413 mlir::Type newTy) {
414 return createCast(loc, cir::CastKind::bitcast, src, newTy);
415 }
416
417 mlir::Value createPtrBitcast(mlir::Value src, mlir::Type newPointeeTy) {
418 assert(mlir::isa<cir::PointerType>(src.getType()) && "expected ptr src");
419 return createBitcast(src, getPointerTo(newPointeeTy));
420 }
421
422 //===--------------------------------------------------------------------===//
423 // Binary Operators
424 //===--------------------------------------------------------------------===//
425
426 mlir::Value createBinop(mlir::Location loc, mlir::Value lhs,
427 cir::BinOpKind kind, mlir::Value rhs) {
428 return cir::BinOp::create(*this, loc, lhs.getType(), kind, lhs, rhs);
429 }
430
431 mlir::Value createLowBitsSet(mlir::Location loc, unsigned size,
432 unsigned bits) {
433 llvm::APInt val = llvm::APInt::getLowBitsSet(size, bits);
434 auto type = cir::IntType::get(getContext(), size, /*isSigned=*/false);
435 return getConstAPInt(loc, type, val);
436 }
437
438 mlir::Value createAnd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
439 return createBinop(loc, lhs, cir::BinOpKind::And, rhs);
440 }
441
442 mlir::Value createOr(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
443 return createBinop(loc, lhs, cir::BinOpKind::Or, rhs);
444 }
445
446 mlir::Value createSelect(mlir::Location loc, mlir::Value condition,
447 mlir::Value trueValue, mlir::Value falseValue) {
448 assert(trueValue.getType() == falseValue.getType() &&
449 "trueValue and falseValue should have the same type");
450 return cir::SelectOp::create(*this, loc, trueValue.getType(), condition,
451 trueValue, falseValue);
452 }
453
454 mlir::Value createLogicalAnd(mlir::Location loc, mlir::Value lhs,
455 mlir::Value rhs) {
456 return createSelect(loc, lhs, rhs, getBool(false, loc));
457 }
458
459 mlir::Value createLogicalOr(mlir::Location loc, mlir::Value lhs,
460 mlir::Value rhs) {
461 return createSelect(loc, lhs, getBool(true, loc), rhs);
462 }
463
464 mlir::Value createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
466 auto op = cir::BinOp::create(*this, loc, lhs.getType(), cir::BinOpKind::Mul,
467 lhs, rhs);
468 op.setNoUnsignedWrap(
469 llvm::to_underlying(ob & OverflowBehavior::NoUnsignedWrap));
470 op.setNoSignedWrap(
471 llvm::to_underlying(ob & OverflowBehavior::NoSignedWrap));
472 return op;
473 }
474 mlir::Value createNSWMul(mlir::Location loc, mlir::Value lhs,
475 mlir::Value rhs) {
476 return createMul(loc, lhs, rhs, OverflowBehavior::NoSignedWrap);
477 }
478 mlir::Value createNUWAMul(mlir::Location loc, mlir::Value lhs,
479 mlir::Value rhs) {
480 return createMul(loc, lhs, rhs, OverflowBehavior::NoUnsignedWrap);
481 }
482
483 mlir::Value createSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
485 auto op = cir::BinOp::create(*this, loc, lhs.getType(), cir::BinOpKind::Sub,
486 lhs, rhs);
487 op.setNoUnsignedWrap(
488 llvm::to_underlying(ob & OverflowBehavior::NoUnsignedWrap));
489 op.setNoSignedWrap(
490 llvm::to_underlying(ob & OverflowBehavior::NoSignedWrap));
491 op.setSaturated(llvm::to_underlying(ob & OverflowBehavior::Saturated));
492 return op;
493 }
494
495 mlir::Value createNSWSub(mlir::Location loc, mlir::Value lhs,
496 mlir::Value rhs) {
497 return createSub(loc, lhs, rhs, OverflowBehavior::NoSignedWrap);
498 }
499
500 mlir::Value createNUWSub(mlir::Location loc, mlir::Value lhs,
501 mlir::Value rhs) {
502 return createSub(loc, lhs, rhs, OverflowBehavior::NoUnsignedWrap);
503 }
504
505 mlir::Value createAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
507 auto op = cir::BinOp::create(*this, loc, lhs.getType(), cir::BinOpKind::Add,
508 lhs, rhs);
509 op.setNoUnsignedWrap(
510 llvm::to_underlying(ob & OverflowBehavior::NoUnsignedWrap));
511 op.setNoSignedWrap(
512 llvm::to_underlying(ob & OverflowBehavior::NoSignedWrap));
513 op.setSaturated(llvm::to_underlying(ob & OverflowBehavior::Saturated));
514 return op;
515 }
516
517 mlir::Value createNSWAdd(mlir::Location loc, mlir::Value lhs,
518 mlir::Value rhs) {
519 return createAdd(loc, lhs, rhs, OverflowBehavior::NoSignedWrap);
520 }
521
522 mlir::Value createNUWAdd(mlir::Location loc, mlir::Value lhs,
523 mlir::Value rhs) {
524 return createAdd(loc, lhs, rhs, OverflowBehavior::NoUnsignedWrap);
525 }
526
527 cir::CmpOp createCompare(mlir::Location loc, cir::CmpOpKind kind,
528 mlir::Value lhs, mlir::Value rhs) {
529 return cir::CmpOp::create(*this, loc, getBoolTy(), kind, lhs, rhs);
530 }
531
532 mlir::Value createIsNaN(mlir::Location loc, mlir::Value operand) {
533 return createCompare(loc, cir::CmpOpKind::ne, operand, operand);
534 }
535
536 mlir::Value createShift(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
537 bool isShiftLeft) {
538 return cir::ShiftOp::create(*this, loc, lhs.getType(), lhs, rhs,
539 isShiftLeft);
540 }
541
542 mlir::Value createShift(mlir::Location loc, mlir::Value lhs,
543 const llvm::APInt &rhs, bool isShiftLeft) {
544 return createShift(loc, lhs, getConstAPInt(loc, lhs.getType(), rhs),
545 isShiftLeft);
546 }
547
548 mlir::Value createShift(mlir::Location loc, mlir::Value lhs, unsigned bits,
549 bool isShiftLeft) {
550 auto width = mlir::dyn_cast<cir::IntType>(lhs.getType()).getWidth();
551 auto shift = llvm::APInt(width, bits);
552 return createShift(loc, lhs, shift, isShiftLeft);
553 }
554
555 mlir::Value createShiftLeft(mlir::Location loc, mlir::Value lhs,
556 unsigned bits) {
557 return createShift(loc, lhs, bits, true);
558 }
559
560 mlir::Value createShiftRight(mlir::Location loc, mlir::Value lhs,
561 unsigned bits) {
562 return createShift(loc, lhs, bits, false);
563 }
564
565 mlir::Value createShiftLeft(mlir::Location loc, mlir::Value lhs,
566 mlir::Value rhs) {
567 return createShift(loc, lhs, rhs, true);
568 }
569
570 mlir::Value createShiftRight(mlir::Location loc, mlir::Value lhs,
571 mlir::Value rhs) {
572 return createShift(loc, lhs, rhs, false);
573 }
574
575 //
576 // Block handling helpers
577 // ----------------------
578 //
579 static OpBuilder::InsertPoint getBestAllocaInsertPoint(mlir::Block *block) {
580 auto last =
581 std::find_if(block->rbegin(), block->rend(), [](mlir::Operation &op) {
582 return mlir::isa<cir::AllocaOp, cir::LabelOp>(&op);
583 });
584
585 if (last != block->rend())
586 return OpBuilder::InsertPoint(block, ++mlir::Block::iterator(&*last));
587 return OpBuilder::InsertPoint(block, block->begin());
588 };
589
590 //
591 // Alignment and size helpers
592 //
593
594 // Note that mlir::IntegerType is used instead of cir::IntType here because we
595 // don't need sign information for these to be useful, so keep it simple.
596
597 // For 0 alignment, any overload of `getAlignmentAttr` returns an empty
598 // attribute.
599 mlir::IntegerAttr getAlignmentAttr(clang::CharUnits alignment) {
600 return getAlignmentAttr(alignment.getQuantity());
601 }
602
603 mlir::IntegerAttr getAlignmentAttr(llvm::Align alignment) {
604 return getAlignmentAttr(alignment.value());
605 }
606
607 mlir::IntegerAttr getAlignmentAttr(int64_t alignment) {
608 return alignment ? getI64IntegerAttr(alignment) : mlir::IntegerAttr();
609 }
610
611 mlir::IntegerAttr getSizeFromCharUnits(clang::CharUnits size) {
612 return getI64IntegerAttr(size.getQuantity());
613 }
614
615 /// Create a loop condition.
616 cir::ConditionOp createCondition(mlir::Value condition) {
617 return cir::ConditionOp::create(*this, condition.getLoc(), condition);
618 }
619
620 /// Create a yield operation.
621 cir::YieldOp createYield(mlir::Location loc, mlir::ValueRange value = {}) {
622 return cir::YieldOp::create(*this, loc, value);
623 }
624};
625
626} // namespace cir
627
628#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)
mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType, mlir::Type type, llvm::StringRef name, clang::CharUnits alignment)
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()