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