| 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 |
#include "CIRGenBuilder.h" |
| 10 |
#include "mlir/IR/BuiltinAttributes.h" |
| 11 |
#include "clang/CIR/MissingFeatures.h" |
| 12 |
#include "llvm/ADT/ArrayRef.h" |
| 13 |
#include "llvm/ADT/TypeSwitch.h" |
| 14 |
|
| 15 |
using namespace clang::CIRGen; |
| 16 |
|
| 17 |
mlir::Value CIRGenBuilderTy::maybeBuildArrayDecay(mlir::Location loc, |
| 18 |
mlir::Value arrayPtr, |
| 19 |
mlir::Type eltTy) { |
| 20 |
const auto arrayPtrTy = mlir::cast<cir::PointerType>(arrayPtr.getType()); |
| 21 |
const auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee()); |
| 22 |
|
| 23 |
if (arrayTy) { |
| 24 |
const cir::PointerType flatPtrTy = getPointerTo(arrayTy.getElementType()); |
| 25 |
return cir::CastOp::create(*this, loc, flatPtrTy, |
| 26 |
cir::CastKind::array_to_ptrdecay, arrayPtr); |
| 27 |
} |
| 28 |
|
| 29 |
assert(arrayPtrTy.getPointee() == eltTy && |
| 30 |
"flat pointee type must match original array element type"); |
| 31 |
return arrayPtr; |
| 32 |
} |
| 33 |
|
| 34 |
mlir::Value CIRGenBuilderTy::getArrayElement(mlir::Location arrayLocBegin, |
| 35 |
mlir::Location arrayLocEnd, |
| 36 |
mlir::Value arrayPtr, |
| 37 |
mlir::Type eltTy, mlir::Value idx, |
| 38 |
bool shouldDecay) { |
| 39 |
auto arrayPtrTy = mlir::dyn_cast<cir::PointerType>(arrayPtr.getType()); |
| 40 |
assert(arrayPtrTy && "expected pointer type"); |
| 41 |
// If the array pointer is not decayed, emit a GetElementOp. |
| 42 |
auto arrayTy = mlir::dyn_cast<cir::ArrayType>(arrayPtrTy.getPointee()); |
| 43 |
|
| 44 |
if (shouldDecay && arrayTy && arrayTy == eltTy) { |
| 45 |
auto eltPtrTy = |
| 46 |
getPointerTo(arrayTy.getElementType(), arrayPtrTy.getAddrSpace()); |
| 47 |
return cir::GetElementOp::create(*this, arrayLocEnd, eltPtrTy, arrayPtr, |
| 48 |
idx); |
| 49 |
} |
| 50 |
|
| 51 |
// If we don't have sufficient type information, emit a PtrStrideOp. |
| 52 |
mlir::Value basePtr = arrayPtr; |
| 53 |
if (shouldDecay) |
| 54 |
basePtr = maybeBuildArrayDecay(arrayLocBegin, arrayPtr, eltTy); |
| 55 |
const mlir::Type flatPtrTy = basePtr.getType(); |
| 56 |
return cir::PtrStrideOp::create(*this, arrayLocEnd, flatPtrTy, basePtr, idx); |
| 57 |
} |
| 58 |
|
| 59 |
cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, |
| 60 |
llvm::APSInt intVal) { |
| 61 |
bool isSigned = intVal.isSigned(); |
| 62 |
unsigned width = intVal.getBitWidth(); |
| 63 |
cir::IntType t = isSigned ? getSIntNTy(width) : getUIntNTy(width); |
| 64 |
return getConstInt(loc, t, |
| 65 |
isSigned ? intVal.getSExtValue() : intVal.getZExtValue()); |
| 66 |
} |
| 67 |
|
| 68 |
cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, |
| 69 |
llvm::APInt intVal, |
| 70 |
bool isUnsigned) { |
| 71 |
return getConstInt(loc, llvm::APSInt(intVal, isUnsigned)); |
| 72 |
} |
| 73 |
|
| 74 |
cir::ConstantOp CIRGenBuilderTy::getConstInt(mlir::Location loc, mlir::Type t, |
| 75 |
uint64_t c) { |
| 76 |
assert(mlir::isa<cir::IntType>(t) && "expected cir::IntType"); |
| 77 |
return cir::ConstantOp::create(*this, loc, cir::IntAttr::get(t, c)); |
| 78 |
} |
| 79 |
|
| 80 |
cir::ConstantOp |
| 81 |
clang::CIRGen::CIRGenBuilderTy::getConstFP(mlir::Location loc, mlir::Type t, |
| 82 |
llvm::APFloat fpVal) { |
| 83 |
assert(mlir::isa<cir::FPTypeInterface>(t) && "expected floating point type"); |
| 84 |
return cir::ConstantOp::create(*this, loc, cir::FPAttr::get(t, fpVal)); |
| 85 |
} |
| 86 |
|
| 87 |
void CIRGenBuilderTy::computeGlobalViewIndicesFromFlatOffset( |
| 88 |
int64_t offset, mlir::Type ty, cir::CIRDataLayout layout, |
| 89 |
llvm::SmallVectorImpl<int64_t> &indices) { |
| 90 |
if (!offset) |
| 91 |
return; |
| 92 |
|
| 93 |
auto getIndexAndNewOffset = |
| 94 |
[](int64_t offset, int64_t eltSize) -> std::pair<int64_t, int64_t> { |
| 95 |
int64_t divRet = offset / eltSize; |
| 96 |
if (divRet < 0) |
| 97 |
divRet -= 1; // make sure offset is positive |
| 98 |
int64_t modRet = offset - (divRet * eltSize); |
| 99 |
return {divRet, modRet}; |
| 100 |
}; |
| 101 |
|
| 102 |
mlir::Type subType = |
| 103 |
llvm::TypeSwitch<mlir::Type, mlir::Type>(ty) |
| 104 |
.Case<cir::ArrayType>([&](auto arrayTy) { |
| 105 |
int64_t eltSize = layout.getTypeAllocSize(arrayTy.getElementType()); |
| 106 |
const auto [index, newOffset] = |
| 107 |
getIndexAndNewOffset(offset, eltSize); |
| 108 |
indices.push_back(index); |
| 109 |
offset = newOffset; |
| 110 |
return arrayTy.getElementType(); |
| 111 |
}) |
| 112 |
.Case<cir::RecordType>([&](auto recordTy) { |
| 113 |
ArrayRef<mlir::Type> elts = recordTy.getMembers(); |
| 114 |
int64_t pos = 0; |
| 115 |
for (size_t i = 0; i < elts.size(); ++i) { |
| 116 |
int64_t eltSize = |
| 117 |
(int64_t)layout.getTypeAllocSize(elts[i]).getFixedValue(); |
| 118 |
unsigned alignMask = layout.getABITypeAlign(elts[i]).value() - 1; |
| 119 |
if (recordTy.getPacked()) |
| 120 |
alignMask = 0; |
| 121 |
// Union's fields have the same offset, so no need to change pos |
| 122 |
// here, we just need to find eltSize that is greater then the |
| 123 |
// required offset. The same is true for the similar union type |
| 124 |
// check below |
| 125 |
if (!recordTy.isUnion()) |
| 126 |
pos = (pos + alignMask) & ~alignMask; |
| 127 |
assert(offset >= 0); |
| 128 |
if (offset < pos + eltSize) { |
| 129 |
indices.push_back(i); |
| 130 |
offset -= pos; |
| 131 |
return elts[i]; |
| 132 |
} |
| 133 |
// No need to update pos here, see the comment above. |
| 134 |
if (!recordTy.isUnion()) |
| 135 |
pos += eltSize; |
| 136 |
} |
| 137 |
llvm_unreachable("offset was not found within the record"); |
| 138 |
}) |
| 139 |
.Default([](mlir::Type otherTy) { |
| 140 |
llvm_unreachable("unexpected type"); |
| 141 |
return otherTy; // Even though this is unreachable, we need to |
| 142 |
// return a type to satisfy the return type of the |
| 143 |
// lambda. |
| 144 |
}); |
| 145 |
|
| 146 |
assert(subType); |
| 147 |
computeGlobalViewIndicesFromFlatOffset(offset, subType, layout, indices); |
| 148 |
} |
| 149 |
|
| 150 |
cir::RecordType clang::CIRGen::CIRGenBuilderTy::getCompleteRecordType( |
| 151 |
mlir::ArrayAttr fields, bool packed, bool padded, llvm::StringRef name) { |
| 152 |
assert(!cir::MissingFeatures::astRecordDeclAttr()); |
| 153 |
llvm::SmallVector<mlir::Type> members; |
| 154 |
members.reserve(fields.size()); |
| 155 |
llvm::transform(fields, std::back_inserter(members), |
| 156 |
[](mlir::Attribute attr) { |
| 157 |
return mlir::cast<mlir::TypedAttr>(attr).getType(); |
| 158 |
}); |
| 159 |
|
| 160 |
if (name.empty()) |
| 161 |
return getAnonRecordTy(members, packed, padded); |
| 162 |
|
| 163 |
return getCompleteNamedRecordType(members, packed, padded, name); |
| 164 |
} |
| 165 |
|
| 166 |
mlir::Attribute clang::CIRGen::CIRGenBuilderTy::getConstRecordOrZeroAttr( |
| 167 |
mlir::ArrayAttr arrayAttr, bool packed, bool padded, mlir::Type type) { |
| 168 |
auto recordTy = mlir::cast_or_null<cir::RecordType>(type); |
| 169 |
|
| 170 |
// Record type not specified: create anon record type from members. |
| 171 |
if (!recordTy) { |
| 172 |
recordTy = getCompleteRecordType(arrayAttr, packed, padded); |
| 173 |
} |
| 174 |
|
| 175 |
// Return zero or anonymous constant record. |
| 176 |
const bool isZero = llvm::all_of( |
| 177 |
arrayAttr, [&](mlir::Attribute a) { return isNullValue(a); }); |
| 178 |
if (isZero) |
| 179 |
return cir::ZeroAttr::get(recordTy); |
| 180 |
return cir::ConstRecordAttr::get(recordTy, arrayAttr); |
| 181 |
} |
| 182 |
|
| 183 |
// This can't be defined in Address.h because that file is included by |
| 184 |
// CIRGenBuilder.h |
| 185 |
Address Address::withElementType(CIRGenBuilderTy &builder, |
| 186 |
mlir::Type elemTy) const { |
| 187 |
assert(!cir::MissingFeatures::addressOffset()); |
| 188 |
assert(!cir::MissingFeatures::addressIsKnownNonNull()); |
| 189 |
assert(!cir::MissingFeatures::addressPointerAuthInfo()); |
| 190 |
|
| 191 |
return Address(builder.createPtrBitcast(getBasePointer(), elemTy), elemTy, |
| 192 |
getAlignment()); |
| 193 |
} |