clang 24.0.0git
LoweringHelpers.cpp File Reference
#include "clang/CIR/LoweringHelpers.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/SymbolTable.h"
#include "mlir/Interfaces/DataLayoutInterfaces.h"
#include "clang/CIR/MissingFeatures.h"

Go to the source code of this file.

Functions

mlir::Type convertTypeForMemory (const mlir::TypeConverter &converter, mlir::DataLayout const &dataLayout, mlir::Type type)
static unsigned getIntOrBoolBitWidth (mlir::Type ty)
mlir::DenseElementsAttr convertStringAttrToDenseElementsAttr (cir::ConstArrayAttr attr, mlir::Type type)
template<>
mlir::APInt getZeroInitFromType (mlir::Type ty)
template<>
mlir::APFloat getZeroInitFromType (mlir::Type ty)
template<typename AttrTy, typename StorageTy>
void convertToDenseElementsAttrImpl (cir::ConstArrayAttr attr, llvm::SmallVectorImpl< StorageTy > &values, const llvm::SmallVectorImpl< int64_t > &currentDims, int64_t dimIndex, int64_t currentIndex)
template<typename AttrTy, typename StorageTy>
mlir::DenseElementsAttr convertToDenseElementsAttr (cir::ConstArrayAttr attr, const llvm::SmallVectorImpl< int64_t > &dims, mlir::Type elementType, mlir::Type convertedElementType)
static bool globalViewMatchesPointerLeaf (cir::GlobalViewAttr gv, mlir::ModuleOp moduleOp, mlir::SymbolTableCollection &symbolTables, const mlir::TypeConverter *converter)
 Return true when gv can be lowered to a FlatSymbolRefAttr leaf without addrspacecast or bitcast (mirrors CIRAttrToValue::visitCirAttr).
static std::optional< mlir::Attribute > lowerPointerElementAttr (mlir::Attribute elt, mlir::MLIRContext *ctx, mlir::ModuleOp moduleOp, mlir::SymbolTableCollection &symbolTables, const mlir::TypeConverter *converter)
 Lower a single pointer-element of a cir.const_array to an LLVM-dialect constant leaf suitable for a bulk llvm.mlir.constant.
static bool containsPoison (mlir::Attribute attr)
static bool containsBlockAddress (mlir::Attribute attr)
 Block-address attributes (address-of-label and label differences) are lowered to relocation expressions that cannot be materialized as part of a dense/aggregate constant attribute; they require the per-element insertvalue region lowering.
static std::optional< mlir::Attribute > lowerConstRecordMemberAttr (mlir::Attribute attr, mlir::SymbolTableCollection &symbolTables, 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 aggregate) to an LLVM-dialect attribute that can be attached directly to an llvm.mlir.global, avoiding an insertvalue initializer region.
std::optional< mlir::Attribute > lowerConstArrayAttr (cir::ConstArrayAttr constArr, mlir::SymbolTableCollection &symbolTables, const mlir::TypeConverter *converter, mlir::ModuleOp moduleOp)
static bool shouldPackFAMStruct (const mlir::DataLayout &dataLayout, llvm::ArrayRef< mlir::Type > members)
static mlir::Type adjustGlobalStructTypeForInit (mlir::LLVM::LLVMStructType structTy, cir::ConstRecordAttr constRecord, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout, llvm::SmallVectorImpl< unsigned > &paddingAddedIndexes)
static mlir::Type adjustGlobalUnionTypeForInit (mlir::LLVM::LLVMStructType structTy, cir::ConstRecordAttr constRecord, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout)
static mlir::Type adjustGlobalArrayTypeForInit (mlir::LLVM::LLVMArrayType arrayTy, cir::ConstArrayAttr arrayInit, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout)
mlir::Type adjustGlobalTypeForInit (mlir::Type llvmType, mlir::Attribute init, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout, llvm::SmallVectorImpl< unsigned > &paddingAddedIndexes)
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 with init actually lowers to.
std::optional< mlir::Attribute > lowerConstRecordAttr (cir::ConstRecordAttr constRecord, mlir::SymbolTableCollection &symbolTables, const mlir::TypeConverter *converter, mlir::ModuleOp moduleOp)
mlir::Value getConstAPInt (mlir::OpBuilder &bld, mlir::Location loc, mlir::Type typ, const llvm::APInt &val)
mlir::Value getConst (mlir::OpBuilder &bld, mlir::Location loc, mlir::Type typ, unsigned val)
mlir::Value createShL (mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs)
mlir::Value createAShR (mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs)
mlir::Value createAnd (mlir::OpBuilder &bld, mlir::Value lhs, const llvm::APInt &rhs)
mlir::Value createLShR (mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs)

Function Documentation

◆ adjustGlobalArrayTypeForInit()

mlir::Type adjustGlobalArrayTypeForInit ( mlir::LLVM::LLVMArrayType arrayTy,
cir::ConstArrayAttr arrayInit,
const mlir::TypeConverter & converter,
const mlir::DataLayout & dataLayout )
static

Definition at line 630 of file LoweringHelpers.cpp.

References adjustGlobalTypeForInit().

Referenced by adjustGlobalTypeForInit().

◆ adjustGlobalStructTypeForInit()

mlir::Type adjustGlobalStructTypeForInit ( mlir::LLVM::LLVMStructType structTy,
cir::ConstRecordAttr constRecord,
const mlir::TypeConverter & converter,
const mlir::DataLayout & dataLayout,
llvm::SmallVectorImpl< unsigned > & paddingAddedIndexes )
static

Definition at line 478 of file LoweringHelpers.cpp.

References adjustGlobalTypeForInit(), and shouldPackFAMStruct().

Referenced by adjustGlobalTypeForInit().

◆ adjustGlobalTypeForInit() [1/2]

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 with init actually lowers to.

This differs from a plain type conversion for flexible-array-member and union initializers, and the adjustment recurses through nested aggregates. Returns llvmType unchanged when no adjustment is needed. This is the single source of truth for the shape of a lowered record constant; the value-producing paths (the insertvalue visitor and lowerConstRecordAttr) conform to it.

Definition at line 703 of file LoweringHelpers.cpp.

References adjustGlobalTypeForInit().

◆ adjustGlobalTypeForInit() [2/2]

mlir::Type adjustGlobalTypeForInit ( mlir::Type llvmType,
mlir::Attribute init,
const mlir::TypeConverter & converter,
const mlir::DataLayout & dataLayout,
llvm::SmallVectorImpl< unsigned > & paddingAddedIndexes )

◆ adjustGlobalUnionTypeForInit()

mlir::Type adjustGlobalUnionTypeForInit ( mlir::LLVM::LLVMStructType structTy,
cir::ConstRecordAttr constRecord,
const mlir::TypeConverter & converter,
const mlir::DataLayout & dataLayout )
static

◆ containsBlockAddress()

bool containsBlockAddress ( mlir::Attribute attr)
static

Block-address attributes (address-of-label and label differences) are lowered to relocation expressions that cannot be materialized as part of a dense/aggregate constant attribute; they require the per-element insertvalue region lowering.

Return true if attr contains any such element.

Definition at line 278 of file LoweringHelpers.cpp.

References containsBlockAddress().

Referenced by containsBlockAddress(), and lowerConstArrayAttr().

◆ containsPoison()

bool containsPoison ( mlir::Attribute attr)
static

Definition at line 259 of file LoweringHelpers.cpp.

References containsPoison().

Referenced by containsPoison(), and lowerConstArrayAttr().

◆ convertStringAttrToDenseElementsAttr()

mlir::DenseElementsAttr convertStringAttrToDenseElementsAttr ( cir::ConstArrayAttr attr,
mlir::Type type )

Definition at line 83 of file LoweringHelpers.cpp.

References getIntOrBoolBitWidth().

Referenced by lowerConstArrayAttr().

◆ convertToDenseElementsAttr()

template<typename AttrTy, typename StorageTy>
mlir::DenseElementsAttr convertToDenseElementsAttr ( cir::ConstArrayAttr attr,
const llvm::SmallVectorImpl< int64_t > & dims,
mlir::Type elementType,
mlir::Type convertedElementType )

Definition at line 179 of file LoweringHelpers.cpp.

References convertToDenseElementsAttrImpl(), and getZeroInitFromType().

Referenced by lowerConstArrayAttr().

◆ convertToDenseElementsAttrImpl()

template<typename AttrTy, typename StorageTy>
void convertToDenseElementsAttrImpl ( cir::ConstArrayAttr attr,
llvm::SmallVectorImpl< StorageTy > & values,
const llvm::SmallVectorImpl< int64_t > & currentDims,
int64_t dimIndex,
int64_t currentIndex )
Parameters
attrthe ConstArrayAttr to convert
valuesthe output parameter, the values array to fill
currentDimsthe shpae of tensor we're going to convert to
dimIndexthe current dimension we're processing
currentIndexthe current index in the values array

Definition at line 127 of file LoweringHelpers.cpp.

References convertToDenseElementsAttrImpl().

Referenced by convertToDenseElementsAttr(), and convertToDenseElementsAttrImpl().

◆ convertTypeForMemory()

mlir::Type convertTypeForMemory ( const mlir::TypeConverter & converter,
mlir::DataLayout const & dataLayout,
mlir::Type type )

◆ createAnd()

mlir::Value createAnd ( mlir::OpBuilder & bld,
mlir::Value lhs,
const llvm::APInt & rhs )

Definition at line 790 of file LoweringHelpers.cpp.

References getConstAPInt().

◆ createAShR()

mlir::Value createAShR ( mlir::OpBuilder & bld,
mlir::Value lhs,
unsigned rhs )

Definition at line 783 of file LoweringHelpers.cpp.

References getConst().

◆ createLShR()

mlir::Value createLShR ( mlir::OpBuilder & bld,
mlir::Value lhs,
unsigned rhs )

Definition at line 796 of file LoweringHelpers.cpp.

References getConst().

◆ createShL()

mlir::Value createShL ( mlir::OpBuilder & bld,
mlir::Value lhs,
unsigned rhs )

Definition at line 776 of file LoweringHelpers.cpp.

References getConst().

◆ getConst()

mlir::Value getConst ( mlir::OpBuilder & bld,
mlir::Location loc,
mlir::Type typ,
unsigned val )

Definition at line 771 of file LoweringHelpers.cpp.

Referenced by createAShR(), createLShR(), and createShL().

◆ getConstAPInt()

mlir::Value getConstAPInt ( mlir::OpBuilder & bld,
mlir::Location loc,
mlir::Type typ,
const llvm::APInt & val )

Definition at line 766 of file LoweringHelpers.cpp.

Referenced by createAnd().

◆ getIntOrBoolBitWidth()

unsigned getIntOrBoolBitWidth ( mlir::Type ty)
static

Definition at line 74 of file LoweringHelpers.cpp.

Referenced by convertStringAttrToDenseElementsAttr().

◆ getZeroInitFromType() [1/2]

template<>
mlir::APInt getZeroInitFromType ( mlir::Type ty)

Definition at line 109 of file LoweringHelpers.cpp.

◆ getZeroInitFromType() [2/2]

template<>
mlir::APFloat getZeroInitFromType ( mlir::Type ty)

Definition at line 116 of file LoweringHelpers.cpp.

◆ globalViewMatchesPointerLeaf()

bool globalViewMatchesPointerLeaf ( cir::GlobalViewAttr gv,
mlir::ModuleOp moduleOp,
mlir::SymbolTableCollection & symbolTables,
const mlir::TypeConverter * converter )
static

Return true when gv can be lowered to a FlatSymbolRefAttr leaf without addrspacecast or bitcast (mirrors CIRAttrToValue::visitCirAttr).

Definition at line 197 of file LoweringHelpers.cpp.

Referenced by lowerPointerElementAttr().

◆ lowerConstArrayAttr()

std::optional< mlir::Attribute > lowerConstArrayAttr ( cir::ConstArrayAttr constArr,
mlir::SymbolTableCollection & symbolTables,
const mlir::TypeConverter * converter,
mlir::ModuleOp moduleOp )

◆ lowerConstRecordAttr()

std::optional< mlir::Attribute > lowerConstRecordAttr ( cir::ConstRecordAttr constRecord,
mlir::SymbolTableCollection & symbolTables,
const mlir::TypeConverter * converter,
mlir::ModuleOp moduleOp )

◆ lowerConstRecordMemberAttr()

std::optional< mlir::Attribute > lowerConstRecordMemberAttr ( mlir::Attribute attr,
mlir::SymbolTableCollection & symbolTables,
const mlir::TypeConverter * converter,
mlir::ModuleOp moduleOp )
static

Lower a constant attribute that initializes a single member of a record (or a leaf of a nested aggregate) to an LLVM-dialect attribute that can be attached directly to an llvm.mlir.global, avoiding an insertvalue initializer region.

Returns std::nullopt when the attribute cannot be represented as a single constant attribute (e.g. an indexed GlobalViewAttr), in which case the caller falls back to the region-based lowering.

Definition at line 398 of file LoweringHelpers.cpp.

References lowerConstArrayAttr(), lowerConstRecordAttr(), and lowerPointerElementAttr().

Referenced by lowerConstArrayAttr(), and lowerConstRecordAttr().

◆ lowerPointerElementAttr()

std::optional< mlir::Attribute > lowerPointerElementAttr ( mlir::Attribute elt,
mlir::MLIRContext * ctx,
mlir::ModuleOp moduleOp,
mlir::SymbolTableCollection & symbolTables,
const mlir::TypeConverter * converter )
static

Lower a single pointer-element of a cir.const_array to an LLVM-dialect constant leaf suitable for a bulk llvm.mlir.constant.

Only handles address-of-global without indices and null pointers; indexed global views must use the per-element llvm.insertvalue fallback.

Definition at line 241 of file LoweringHelpers.cpp.

References globalViewMatchesPointerLeaf().

Referenced by lowerConstArrayAttr(), and lowerConstRecordMemberAttr().

◆ shouldPackFAMStruct()

bool shouldPackFAMStruct ( const mlir::DataLayout & dataLayout,
llvm::ArrayRef< mlir::Type > members )
static

Definition at line 446 of file LoweringHelpers.cpp.

Referenced by adjustGlobalStructTypeForInit().