| 1 |
#include "clang/CIR/Dialect/IR/CIRDataLayout.h" |
| 2 |
#include "clang/CIR/Dialect/IR/CIRTypes.h" |
| 3 |
#include "clang/CIR/MissingFeatures.h" |
| 4 |
|
| 5 |
using namespace cir; |
| 6 |
|
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// DataLayout Class Implementation |
| 9 |
//===----------------------------------------------------------------------===// |
| 10 |
|
| 11 |
CIRDataLayout::CIRDataLayout(mlir::ModuleOp modOp) : layout(modOp) { |
| 12 |
reset(modOp.getDataLayoutSpec()); |
| 13 |
} |
| 14 |
|
| 15 |
void CIRDataLayout::reset(mlir::DataLayoutSpecInterface spec) { |
| 16 |
bigEndian = false; |
| 17 |
if (spec) { |
| 18 |
mlir::StringAttr key = mlir::StringAttr::get( |
| 19 |
spec.getContext(), mlir::DLTIDialect::kDataLayoutEndiannessKey); |
| 20 |
if (mlir::DataLayoutEntryInterface entry = spec.getSpecForIdentifier(key)) |
| 21 |
if (auto str = llvm::dyn_cast<mlir::StringAttr>(entry.getValue())) |
| 22 |
bigEndian = str == mlir::DLTIDialect::kDataLayoutEndiannessBig; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
llvm::Align CIRDataLayout::getAlignment(mlir::Type ty, bool useABIAlign) const { |
| 27 |
// FIXME(cir): This does not account for differnt address spaces, and relies |
| 28 |
// on CIR's data layout to give the proper alignment. |
| 29 |
assert(!cir::MissingFeatures::addressSpace()); |
| 30 |
|
| 31 |
// Fetch type alignment from MLIR's data layout. |
| 32 |
unsigned align = useABIAlign ? layout.getTypeABIAlignment(ty) |
| 33 |
: layout.getTypePreferredAlignment(ty); |
| 34 |
return llvm::Align(align); |
| 35 |
} |
| 36 |
|
| 37 |
// The implementation of this method is provided inline as it is particularly |
| 38 |
// well suited to constant folding when called on a specific Type subclass. |
| 39 |
llvm::TypeSize CIRDataLayout::getTypeSizeInBits(mlir::Type ty) const { |
| 40 |
assert(cir::isSized(ty) && "Cannot getTypeInfo() on a type that is unsized!"); |
| 41 |
|
| 42 |
if (auto recordTy = llvm::dyn_cast<cir::RecordType>(ty)) { |
| 43 |
// FIXME(cir): CIR record's data layout implementation doesn't do a good job |
| 44 |
// of handling unions particularities. We should have a separate union type. |
| 45 |
return recordTy.getTypeSizeInBits(layout, {}); |
| 46 |
} |
| 47 |
|
| 48 |
// FIXME(cir): This does not account for different address spaces, and relies |
| 49 |
// on CIR's data layout to give the proper ABI-specific type width. |
| 50 |
assert(!cir::MissingFeatures::addressSpace()); |
| 51 |
|
| 52 |
// This is calling mlir::DataLayout::getTypeSizeInBits(). |
| 53 |
return llvm::TypeSize::getFixed(layout.getTypeSizeInBits(ty)); |
| 54 |
} |