clang 24.0.0git
LoweringHelpers.cpp
Go to the documentation of this file.
1//====- LoweringHelpers.cpp - Lowering helper functions -------------------===//
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// This file contains helper functions for lowering from CIR to LLVM or MLIR.
10//
11//===----------------------------------------------------------------------===//
12
14#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
15#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
16#include "mlir/IR/BuiltinTypes.h"
17#include "mlir/IR/SymbolTable.h"
18#include "mlir/Interfaces/DataLayoutInterfaces.h"
19
20static unsigned getIntOrBoolBitWidth(mlir::Type ty) {
21 if (auto intTy = mlir::dyn_cast<cir::IntType>(ty))
22 return intTy.getWidth();
23 assert(mlir::isa<cir::BoolType>(ty) &&
24 "expected CIR integer or bool element type");
25 return 1;
26}
27
28mlir::DenseElementsAttr
29convertStringAttrToDenseElementsAttr(cir::ConstArrayAttr attr,
30 mlir::Type type) {
31 const auto stringAttr = mlir::cast<mlir::StringAttr>(attr.getElts());
32 const auto arrayTy = mlir::cast<cir::ArrayType>(attr.getType());
33 const unsigned totalSize = arrayTy.getSize();
34 const unsigned trailingZeros = attr.getTrailingZerosNum();
35 assert(stringAttr.size() + trailingZeros == totalSize &&
36 "string const_array size must match explicit elements plus "
37 "trailing_zeros");
38
39 const unsigned bitWidth = getIntOrBoolBitWidth(arrayTy.getElementType());
41 values.reserve(totalSize);
42
43 // String bytes are raw values; interpret each as an unsigned byte so a
44 // high-bit char (>= 0x80) does not sign-extend to a value that overflows
45 // the element bit width when constructing the APInt.
46 for (const char element : stringAttr)
47 values.emplace_back(bitWidth, static_cast<unsigned char>(element));
48
49 values.insert(values.end(), trailingZeros, mlir::APInt::getZero(bitWidth));
50
51 return mlir::DenseElementsAttr::get(
52 mlir::RankedTensorType::get({totalSize}, type), llvm::ArrayRef(values));
53}
54
55template <> mlir::APInt getZeroInitFromType(mlir::Type ty) {
56 if (mlir::isa<cir::BoolType>(ty))
57 return mlir::APInt::getZero(1);
58 const auto intTy = mlir::cast<cir::IntType>(ty);
59 return mlir::APInt::getZero(intTy.getWidth());
60}
61
62template <> mlir::APFloat getZeroInitFromType(mlir::Type ty) {
63 auto fpTy = mlir::cast<cir::FPTypeInterface>(ty);
64 return mlir::APFloat::getZero(fpTy.getFloatSemantics());
65}
66
67/// \param attr the ConstArrayAttr to convert
68/// \param values the output parameter, the values array to fill
69/// \param currentDims the shpae of tensor we're going to convert to
70/// \param dimIndex the current dimension we're processing
71/// \param currentIndex the current index in the values array
72template <typename AttrTy, typename StorageTy>
74 cir::ConstArrayAttr attr, llvm::SmallVectorImpl<StorageTy> &values,
75 const llvm::SmallVectorImpl<int64_t> &currentDims, int64_t dimIndex,
76 int64_t currentIndex) {
77 if (auto stringAttr = mlir::dyn_cast<mlir::StringAttr>(attr.getElts())) {
78 if (auto arrayType = mlir::dyn_cast<cir::ArrayType>(attr.getType())) {
79 for (auto element : stringAttr) {
80 auto intAttr = cir::IntAttr::get(arrayType.getElementType(), element);
81 values[currentIndex++] = mlir::dyn_cast<AttrTy>(intAttr).getValue();
82 }
83 // Remaining slots are trailing zeros; values was zero-initialized.
84 currentIndex += attr.getTrailingZerosNum();
85 return;
86 }
87 }
88
89 dimIndex++;
90 std::size_t elementsSizeInCurrentDim = 1;
91 for (std::size_t i = dimIndex; i < currentDims.size(); i++)
92 elementsSizeInCurrentDim *= currentDims[i];
93
94 auto arrayAttr = mlir::cast<mlir::ArrayAttr>(attr.getElts());
95 for (auto eltAttr : arrayAttr) {
96 if constexpr (std::is_same_v<StorageTy, mlir::APInt>) {
97 if (auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(eltAttr)) {
98 values[currentIndex++] =
99 llvm::APInt(1, static_cast<uint64_t>(boolAttr.getValue()));
100 continue;
101 }
102 }
103 if (auto valueAttr = mlir::dyn_cast<AttrTy>(eltAttr)) {
104 values[currentIndex++] = valueAttr.getValue();
105 continue;
106 }
107
108 if (auto subArrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(eltAttr)) {
109 convertToDenseElementsAttrImpl<AttrTy>(subArrayAttr, values, currentDims,
110 dimIndex, currentIndex);
111 currentIndex += elementsSizeInCurrentDim;
112 continue;
113 }
114
115 if (mlir::isa<cir::ZeroAttr, cir::UndefAttr>(eltAttr)) {
116 currentIndex += elementsSizeInCurrentDim;
117 continue;
118 }
119
120 llvm_unreachable("unknown element in ConstArrayAttr");
121 }
122}
123
124template <typename AttrTy, typename StorageTy>
125mlir::DenseElementsAttr convertToDenseElementsAttr(
126 cir::ConstArrayAttr attr, const llvm::SmallVectorImpl<int64_t> &dims,
127 mlir::Type elementType, mlir::Type convertedElementType) {
128 unsigned vectorSize = 1;
129 for (auto dim : dims)
130 vectorSize *= dim;
132 vectorSize, getZeroInitFromType<StorageTy>(elementType));
133 convertToDenseElementsAttrImpl<AttrTy>(attr, values, dims, /*currentDim=*/0,
134 /*initialIndex=*/0);
135 return mlir::DenseElementsAttr::get(
136 mlir::RankedTensorType::get(dims, convertedElementType),
137 llvm::ArrayRef(values));
138}
139
140/// Return true when \p gv can be lowered to a \c FlatSymbolRefAttr leaf without
141/// addrspacecast or bitcast (mirrors \c CIRAttrToValue::visitCirAttr).
142static bool globalViewMatchesPointerLeaf(cir::GlobalViewAttr gv,
143 mlir::ModuleOp moduleOp,
144 const mlir::TypeConverter *converter) {
145 if (gv.getIndices() || mlir::isa<cir::IntType, cir::VPtrType>(gv.getType()))
146 return false;
147
148 auto ptrTy = mlir::dyn_cast<cir::PointerType>(gv.getType());
149 if (!ptrTy)
150 return false;
151
152 unsigned sourceAddrSpace = 0;
153 mlir::Type sourceType;
154 auto sourceSymbol =
155 mlir::SymbolTable::lookupSymbolIn(moduleOp, gv.getSymbol());
156 if (auto llvmSymbol = mlir::dyn_cast<mlir::LLVM::GlobalOp>(sourceSymbol)) {
157 sourceType = llvmSymbol.getType();
158 sourceAddrSpace = llvmSymbol.getAddrSpace();
159 } else if (auto cirSymbol = mlir::dyn_cast<cir::GlobalOp>(sourceSymbol)) {
160 sourceType = converter->convertType(cirSymbol.getSymType());
161 if (auto targetAS = mlir::dyn_cast_if_present<cir::TargetAddressSpaceAttr>(
162 cirSymbol.getAddrSpaceAttr()))
163 sourceAddrSpace = targetAS.getValue();
164 } else {
165 // cir.func and other symbols not yet lowered to globals cannot be used as
166 // bulk constant leaves; those cases keep the insertvalue fallback.
167 return false;
168 }
169
170 auto llvmDstTy = converter->convertType<mlir::LLVM::LLVMPointerType>(ptrTy);
171 if (llvmDstTy.getAddressSpace() != sourceAddrSpace)
172 return false;
173
174 mlir::Type llvmEltTy = converter->convertType(ptrTy.getPointee());
175 if (llvmEltTy == sourceType)
176 return true;
177 if (auto arrTy = mlir::dyn_cast<mlir::LLVM::LLVMArrayType>(sourceType))
178 return llvmEltTy == arrTy.getElementType();
179 return false;
180}
181
182/// Lower a single pointer-element of a \c cir.const_array to an LLVM-dialect
183/// constant leaf suitable for a bulk \c llvm.mlir.constant. Only handles
184/// address-of-global without indices and null pointers; indexed global views
185/// must use the per-element \c llvm.insertvalue fallback.
186static std::optional<mlir::Attribute>
187lowerPointerElementAttr(mlir::Attribute elt, mlir::MLIRContext *ctx,
188 mlir::ModuleOp moduleOp,
189 const mlir::TypeConverter *converter) {
190 if (auto gv = mlir::dyn_cast<cir::GlobalViewAttr>(elt)) {
191 if (!moduleOp || !globalViewMatchesPointerLeaf(gv, moduleOp, converter))
192 return std::nullopt;
193 return gv.getSymbol();
194 }
195 if (auto nullPtr = mlir::dyn_cast<cir::ConstPtrAttr>(elt)) {
196 if (nullPtr.isNullValue())
197 return mlir::LLVM::ZeroAttr::get(ctx);
198 return std::nullopt;
199 }
200 return std::nullopt;
201}
202
203static bool containsPoison(mlir::Attribute attr) {
204 if (mlir::isa<cir::PoisonAttr>(attr))
205 return true;
206 if (auto elts = mlir::dyn_cast<mlir::ArrayAttr>(attr))
207 return llvm::any_of(elts, containsPoison);
208 if (auto constArr = mlir::dyn_cast<cir::ConstArrayAttr>(attr)) {
209 if (mlir::isa<mlir::StringAttr>(constArr.getElts()))
210 return false;
211 if (auto elts = mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts()))
212 return llvm::any_of(elts, containsPoison);
213 }
214 return false;
215}
216
217std::optional<mlir::Attribute>
218lowerConstArrayAttr(cir::ConstArrayAttr constArr,
219 const mlir::TypeConverter *converter,
220 mlir::ModuleOp moduleOp) {
221 // Ensure ConstArrayAttr has a type.
222 const auto typedConstArr = mlir::cast<mlir::TypedAttr>(constArr);
223
224 // Ensure ConstArrayAttr type is a ArrayType.
225 const auto cirArrayType = mlir::cast<cir::ArrayType>(typedConstArr.getType());
226
227 // Is a ConstArrayAttr with an cir::ArrayType: fetch element type.
228 mlir::Type type = cirArrayType;
229 auto dims = llvm::SmallVector<int64_t, 2>{};
230 while (auto arrayType = mlir::dyn_cast<cir::ArrayType>(type)) {
231 dims.push_back(arrayType.getSize());
232 type = arrayType.getElementType();
233 }
234
235 if (containsPoison(constArr))
236 return std::nullopt;
237
238 if (mlir::isa<mlir::StringAttr>(constArr.getElts()))
240 converter->convertType(type));
241 if (mlir::isa<cir::IntType>(type)) {
242 // A _BitInt array element is stored in a padded integer in memory; the
243 // dense-attribute path here cannot express that widening, so fall back to
244 // the insertvalue region, which widens each element via emitToMemory.
245 if (mlir::cast<cir::IntType>(type).isBitInt())
246 return std::nullopt;
248 constArr, dims, type, converter->convertType(type));
249 }
250
251 if (mlir::isa<cir::BoolType>(type))
253 constArr, dims, type, converter->convertType(type));
254
255 if (mlir::isa<cir::FPTypeInterface>(type))
257 constArr, dims, type, converter->convertType(type));
258
259 if (mlir::isa<cir::PointerType>(type)) {
260 // FIXME: Pointer arrays with trailing_zeros (null-sentinel tables) fall
261 // through to the insertvalue path for now.
262 if (constArr.getTrailingZerosNum() > 0)
263 return std::nullopt;
264 auto eltsArr = mlir::dyn_cast<mlir::ArrayAttr>(constArr.getElts());
265 if (!eltsArr)
266 return std::nullopt;
268 lowered.reserve(eltsArr.size());
269 mlir::MLIRContext *ctx = constArr.getContext();
270 for (mlir::Attribute elt : eltsArr) {
271 std::optional<mlir::Attribute> llvmElt =
272 lowerPointerElementAttr(elt, ctx, moduleOp, converter);
273 if (!llvmElt)
274 return std::nullopt;
275 lowered.push_back(*llvmElt);
276 }
277 return mlir::ArrayAttr::get(ctx, lowered);
278 }
279
280 return std::nullopt;
281}
282
283/// Lower a constant attribute that initializes a single member of a record (or
284/// a leaf of a nested aggregate) to an LLVM-dialect attribute that can be
285/// attached directly to an \c llvm.mlir.global, avoiding an insertvalue
286/// initializer region. Returns \c std::nullopt when the attribute cannot be
287/// represented as a single constant attribute (e.g. an indexed
288/// \c GlobalViewAttr), in which case the caller falls back to the region-based
289/// lowering.
290static std::optional<mlir::Attribute>
291lowerConstRecordMemberAttr(mlir::Attribute attr,
292 const mlir::TypeConverter *converter,
293 mlir::ModuleOp moduleOp) {
294 mlir::MLIRContext *ctx = attr.getContext();
295
296 if (auto arrayAttr = mlir::dyn_cast<cir::ConstArrayAttr>(attr))
297 return lowerConstArrayAttr(arrayAttr, converter, moduleOp);
298
299 if (auto recordAttr = mlir::dyn_cast<cir::ConstRecordAttr>(attr))
300 return lowerConstRecordAttr(recordAttr, converter, moduleOp);
301
302 if (mlir::isa<cir::ZeroAttr>(attr))
303 return mlir::LLVM::ZeroAttr::get(ctx);
304
305 if (mlir::isa<cir::UndefAttr>(attr))
306 return mlir::LLVM::UndefAttr::get(ctx);
307
308 if (auto intAttr = mlir::dyn_cast<cir::IntAttr>(attr)) {
309 // A _BitInt member is stored in a padded integer in memory; defer to the
310 // insertvalue region (CIRAttrToValue), which performs the extension.
311 if (mlir::cast<cir::IntType>(intAttr.getType()).isBitInt())
312 return std::nullopt;
313 return mlir::IntegerAttr::get(converter->convertType(intAttr.getType()),
314 intAttr.getValue());
315 }
316
317 if (auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(attr))
318 return mlir::IntegerAttr::get(converter->convertType(boolAttr.getType()),
319 boolAttr.getValue() ? 1 : 0);
320
321 if (auto fpAttr = mlir::dyn_cast<cir::FPAttr>(attr))
322 return mlir::FloatAttr::get(converter->convertType(fpAttr.getType()),
323 fpAttr.getValue());
324
325 // Null pointers and simple address-of-global references can be represented
326 // as constant attributes; anything more complex uses the region fallback.
327 return lowerPointerElementAttr(attr, ctx, moduleOp, converter);
328}
329
330// Figure out if we want mark the new struct 'packed' if it isn't already. IF
331// it is already, we have to keep that behavior. We pack it with logic similar
332// to classic codegen, though will end up missing cases, since we don't want to
333// change the type other than the FAM.
334// We can do so if:
335// 1- Packing it won't change any of the field offsets.
336// 2- the non-padded struct would add padding beyond the
337// flexible array member. We don't pack if the flexible array member manages
338// to not cause trailing padding.
339static bool shouldPackFAMStruct(const mlir::DataLayout &dataLayout,
341 uint64_t maxAlign = 1;
342 uint64_t totalSize = 0;
343 for (mlir::Type member : members) {
344 uint64_t align = dataLayout.getTypeABIAlignment(member);
345 maxAlign = std::max(maxAlign, align);
346 uint64_t size = dataLayout.getTypeSize(member).getFixedValue();
347
348 if (llvm::alignTo(totalSize, align) != totalSize)
349 return false;
350
351 totalSize += size;
352 }
353 return llvm::alignTo(totalSize, maxAlign) != totalSize;
354}
355
356// CIR supports flexible-array-members in its struct types. That is, a
357// zero-length array as the last element, which can be initialized with an
358// arbitrary number of elements. A ConstRecordAttr can be created with one of
359// these, and our verifier allows it. However, the LLVM implementation does NOT
360// permit this. So we have to replace this type in LLVM with special struct for
361// this value.
362//
363// Additionally, the struct itself could contain a struct with a FAM or a union
364// that needed adjustment, so it recurses to check those. If no such type has
365// been found/no adjustment needed, this returns the type unchanged.
367 mlir::LLVM::LLVMStructType structTy, cir::ConstRecordAttr constRecord,
368 const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout) {
369
371 constRecord.getMembers().getValue();
372 llvm::SmallVector<mlir::Type> newBody{structTy.getBody()};
373 bool changed = false;
374
375 // Recursively adjust each member. A member that is itself a union (or a
376 // struct containing one) lowers to a type that differs from its declared
377 // field type, and this struct has to adopt that adjusted type so the
378 // enclosing insertvalue chain type-checks.
379 for (auto [idx, member] : llvm::enumerate(initMembers)) {
380 if (idx >= newBody.size())
381 break;
382 mlir::Type adjusted =
383 adjustGlobalTypeForInit(newBody[idx], member, converter, dataLayout);
384 if (adjusted != newBody[idx]) {
385 newBody[idx] = adjusted;
386 changed = true;
387 }
388 }
389
390 // CIR supports flexible-array-members in its struct types. That is, a
391 // zero-length array as the last element, which can be initialized with an
392 // arbitrary number of elements. A ConstRecordAttr can be created with one of
393 // these, and our verifier allows it. However, the LLVM implementation does
394 // NOT permit this, so we widen that trailing member to the initializer's
395 // array type (packing the struct if that changes the layout).
396 bool packed = structTy.isPacked();
397 if (auto fam =
398 mlir::dyn_cast<mlir::LLVM::LLVMArrayType>(structTy.getBody().back());
399 fam && fam.getNumElements() == 0) {
400 mlir::Type lastInitType =
401 mlir::cast<mlir::TypedAttr>(initMembers.back()).getType();
402 if (mlir::cast<cir::ArrayType>(lastInitType).getSize() != 0) {
403 newBody.back() = converter.convertType(lastInitType);
404 packed = packed || shouldPackFAMStruct(dataLayout, newBody);
405 changed = true;
406 }
407 }
408
409 if (!changed)
410 return structTy;
411
412 return mlir::LLVM::LLVMStructType::getLiteral(structTy.getContext(), newBody,
413 packed);
414}
415
416// A union constant only initializes its active member, but a union is lowered
417// to its most-aligned member (its 'storage' type), which need not be the
418// initialized one -- e.g. `union { char buf[16]; long cap; }` stores as `long`
419// (higher alignment) but may be initialized through `buf`. So the storage type
420// (and thus structTy) generally can't hold the active member's value. Rebuild
421// an anonymous struct `{ <active member>, [pad x i8] }` that holds the active
422// member followed by enough byte padding to span the union's full allocated
423// size, mirroring classic codegen.
425 mlir::LLVM::LLVMStructType structTy, cir::ConstRecordAttr constRecord,
426 const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout) {
427
428 auto unionTy = mlir::cast<cir::UnionType>(constRecord.getType());
429
430 // Unions can only initialize one field, so this has to be sizeof-one.
431 assert(constRecord.getMembers().size() == 1);
432 mlir::Attribute member = constRecord.getMembers()[0];
433 mlir::Type memberTy =
434 converter.convertType(mlir::cast<mlir::TypedAttr>(member).getType());
435
436 // The active member may itself need adjusting (e.g. it is a nested union, or
437 // a struct containing one), so recurse before using its type below.
438 memberTy = adjustGlobalTypeForInit(memberTy, member, converter, dataLayout);
439
440 // The converted union type is { storage, [padding] }, where storage is the
441 // union's most-aligned member. When the active member IS that storage type,
442 // the converted type already describes this initializer exactly (the pad we
443 // would compute equals the union's declared pad), so there is nothing to do.
444 if (memberTy == structTy.getBody().front())
445 return structTy;
446
447 uint64_t unionSize = dataLayout.getTypeSize(unionTy).getFixedValue();
448 uint64_t initSize = dataLayout.getTypeSize(memberTy).getFixedValue();
449 assert(initSize <= unionSize && "union initializer larger than the union");
450
452 newBody.push_back(memberTy);
453
454 // Fill the rest of the union's allocated size with byte padding.
455 if (initSize < unionSize)
456 newBody.push_back(mlir::LLVM::LLVMArrayType::get(
457 mlir::IntegerType::get(structTy.getContext(), 8),
458 unionSize - initSize));
459
460 return mlir::LLVM::LLVMStructType::getLiteral(structTy.getContext(), newBody,
461 unionTy.getPacked());
462}
463
464// Apply various adjustments required for struct/union types.
465mlir::Type adjustGlobalTypeForInit(mlir::Type llvmType, mlir::Attribute init,
466 const mlir::TypeConverter &converter,
467 const mlir::DataLayout &dataLayout) {
468 // Conversions for both only happen if we have a record init.
469 auto constRecord = mlir::dyn_cast_if_present<cir::ConstRecordAttr>(init);
470 if (!constRecord)
471 return llvmType;
472
473 // If this isn't of struct-type, or doesn't have any members, there is nothing
474 // to do.
475 auto structTy = mlir::dyn_cast<mlir::LLVM::LLVMStructType>(llvmType);
476 if (!structTy || structTy.getBody().empty())
477 return llvmType;
478
479 // Structs can have a flexible array member, adjust that.
480 if (mlir::isa<cir::StructType>(constRecord.getType()))
481 return adjustGlobalStructTypeForInit(structTy, constRecord, converter,
482 dataLayout);
483 if (mlir::isa<cir::UnionType>(constRecord.getType()))
484 return adjustGlobalUnionTypeForInit(structTy, constRecord, converter,
485 dataLayout);
486 return llvmType;
487}
488
489std::optional<mlir::Attribute>
490lowerConstRecordAttr(cir::ConstRecordAttr constRecord,
491 const mlir::TypeConverter *converter,
492 mlir::ModuleOp moduleOp) {
493 // Build one constant attribute per record member. The LLVM dialect global
494 // translation accepts an ArrayAttr (one element per struct field) and emits
495 // an llvm::ConstantStruct, so the whole initializer can be a single
496 // attribute on the global instead of an insertvalue region.
497 mlir::ArrayAttr memberAttrs = constRecord.getMembers();
499 loweredMembers.reserve(memberAttrs.size());
500 for (mlir::Attribute member : memberAttrs) {
501 std::optional<mlir::Attribute> lowered =
502 lowerConstRecordMemberAttr(member, converter, moduleOp);
503 if (!lowered)
504 return std::nullopt;
505 loweredMembers.push_back(*lowered);
506 }
507
508 // The lowered LLVM type may have more fields than the CIR record has members
509 // -- e.g. a union lowers to { active-member, [pad x i8] } (see
510 // adjustGlobalTypeForInit, the single source of truth for the shape). Fill
511 // any such synthesized (padding) fields with undef so this ArrayAttr has
512 // exactly one entry per LLVM field, matching the type the global is declared
513 // with.
514 mlir::Type adjustedTy = adjustGlobalTypeForInit(
515 converter->convertType(constRecord.getType()), constRecord, *converter,
516 mlir::DataLayout(moduleOp));
517 if (auto structTy = mlir::dyn_cast<mlir::LLVM::LLVMStructType>(adjustedTy))
518 while (loweredMembers.size() < structTy.getBody().size())
519 loweredMembers.push_back(
520 mlir::LLVM::UndefAttr::get(constRecord.getContext()));
521
522 return mlir::ArrayAttr::get(constRecord.getContext(), loweredMembers);
523}
524
525mlir::Value getConstAPInt(mlir::OpBuilder &bld, mlir::Location loc,
526 mlir::Type typ, const llvm::APInt &val) {
527 return mlir::LLVM::ConstantOp::create(bld, loc, typ, val);
528}
529
530mlir::Value getConst(mlir::OpBuilder &bld, mlir::Location loc, mlir::Type typ,
531 unsigned val) {
532 return mlir::LLVM::ConstantOp::create(bld, loc, typ, val);
533}
534
535mlir::Value createShL(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs) {
536 if (!rhs)
537 return lhs;
538 mlir::Value rhsVal = getConst(bld, lhs.getLoc(), lhs.getType(), rhs);
539 return mlir::LLVM::ShlOp::create(bld, lhs.getLoc(), lhs, rhsVal);
540}
541
542mlir::Value createAShR(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs) {
543 if (!rhs)
544 return lhs;
545 mlir::Value rhsVal = getConst(bld, lhs.getLoc(), lhs.getType(), rhs);
546 return mlir::LLVM::AShrOp::create(bld, lhs.getLoc(), lhs, rhsVal);
547}
548
549mlir::Value createAnd(mlir::OpBuilder &bld, mlir::Value lhs,
550 const llvm::APInt &rhs) {
551 mlir::Value rhsVal = getConstAPInt(bld, lhs.getLoc(), lhs.getType(), rhs);
552 return mlir::LLVM::AndOp::create(bld, lhs.getLoc(), lhs, rhsVal);
553}
554
555mlir::Value createLShR(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs) {
556 if (!rhs)
557 return lhs;
558 mlir::Value rhsVal = getConst(bld, lhs.getLoc(), lhs.getType(), rhs);
559 return mlir::LLVM::LShrOp::create(bld, lhs.getLoc(), lhs, rhsVal);
560}
TokenType getType() const
Returns the token's type, e.g.
mlir::DenseElementsAttr convertStringAttrToDenseElementsAttr(cir::ConstArrayAttr attr, mlir::Type type)
std::optional< mlir::Attribute > lowerConstArrayAttr(cir::ConstArrayAttr constArr, const mlir::TypeConverter *converter, mlir::ModuleOp moduleOp)
mlir::Value createLShR(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs)
mlir::Value createShL(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs)
mlir::Value getConst(mlir::OpBuilder &bld, mlir::Location loc, mlir::Type typ, unsigned val)
mlir::Value getConstAPInt(mlir::OpBuilder &bld, mlir::Location loc, mlir::Type typ, const llvm::APInt &val)
std::optional< mlir::Attribute > lowerConstRecordAttr(cir::ConstRecordAttr constRecord, const mlir::TypeConverter *converter, mlir::ModuleOp moduleOp)
void convertToDenseElementsAttrImpl(cir::ConstArrayAttr attr, llvm::SmallVectorImpl< StorageTy > &values, const llvm::SmallVectorImpl< int64_t > &currentDims, int64_t dimIndex, int64_t currentIndex)
mlir::Type adjustGlobalTypeForInit(mlir::Type llvmType, mlir::Attribute init, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout)
Adjust llvmType (the converted type of init) to the concrete LLVM type a global constant initialized ...
static mlir::Type adjustGlobalStructTypeForInit(mlir::LLVM::LLVMStructType structTy, cir::ConstRecordAttr constRecord, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout)
static bool containsPoison(mlir::Attribute attr)
static std::optional< mlir::Attribute > lowerConstRecordMemberAttr(mlir::Attribute attr, const mlir::TypeConverter *converter, mlir::ModuleOp moduleOp)
Lower a constant attribute that initializes a single member of a record (or a leaf of a nested aggreg...
static bool globalViewMatchesPointerLeaf(cir::GlobalViewAttr gv, mlir::ModuleOp moduleOp, const mlir::TypeConverter *converter)
Return true when gv can be lowered to a FlatSymbolRefAttr leaf without addrspacecast or bitcast (mirr...
static std::optional< mlir::Attribute > lowerPointerElementAttr(mlir::Attribute elt, mlir::MLIRContext *ctx, mlir::ModuleOp moduleOp, const mlir::TypeConverter *converter)
Lower a single pointer-element of a cir.const_array to an LLVM-dialect constant leaf suitable for a b...
mlir::Value createAShR(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs)
mlir::Value createAnd(mlir::OpBuilder &bld, mlir::Value lhs, const llvm::APInt &rhs)
static bool shouldPackFAMStruct(const mlir::DataLayout &dataLayout, llvm::ArrayRef< mlir::Type > members)
mlir::DenseElementsAttr convertToDenseElementsAttr(cir::ConstArrayAttr attr, const llvm::SmallVectorImpl< int64_t > &dims, mlir::Type elementType, mlir::Type convertedElementType)
static mlir::Type adjustGlobalUnionTypeForInit(mlir::LLVM::LLVMStructType structTy, cir::ConstRecordAttr constRecord, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout)
mlir::APInt getZeroInitFromType(mlir::Type ty)
static unsigned getIntOrBoolBitWidth(mlir::Type ty)
StorageTy getZeroInitFromType(mlir::Type ty)