clang 24.0.0git
LoweringHelpers.h
Go to the documentation of this file.
1//====- LoweringHelpers.h - 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 declares helper functions for lowering from CIR to LLVM or MLIR.
10//
11//===----------------------------------------------------------------------===//
12#ifndef LLVM_CLANG_CIR_LOWERINGHELPERS_H
13#define LLVM_CLANG_CIR_LOWERINGHELPERS_H
14
15#include "mlir/IR/BuiltinAttributes.h"
16#include "mlir/Interfaces/DataLayoutInterfaces.h"
17#include "mlir/Transforms/DialectConversion.h"
19#include "llvm/ADT/StringMap.h"
20
21// CIR type of each symbol before lowering, keyed by symbol name. Built once
22// before conversion: lookups after a replacement find the LLVM symbol, whose
23// type may have been adjusted during lowering (split rows, flexible array),
24// and use this instead, so the answer does not depend on lowering order.
25// Symbols absent from the map were never CIR and keep the converted-type
26// view.
27using CIRPreConversionTypeMap = llvm::StringMap<mlir::Type>;
28
29mlir::DenseElementsAttr
30convertStringAttrToDenseElementsAttr(cir::ConstArrayAttr attr, mlir::Type type);
31
32/// Return a zero with the lowered element type's width or float semantics.
33template <typename StorageTy> StorageTy getZeroInitFromType(mlir::Type ty);
34template <> mlir::APInt getZeroInitFromType(mlir::Type ty);
35template <> mlir::APFloat getZeroInitFromType(mlir::Type ty);
36
37template <typename AttrTy, typename StorageTy>
38std::optional<mlir::DenseElementsAttr>
39convertToDenseElementsAttr(cir::ConstArrayAttr attr,
41 mlir::Type convertedElementType);
42
43std::optional<mlir::Attribute> lowerConstArrayAttr(
44 cir::ConstArrayAttr constArr, mlir::SymbolTableCollection &symbolTables,
45 const mlir::TypeConverter *converter,
46 const CIRPreConversionTypeMap &preConversionTypes, mlir::ModuleOp moduleOp);
47
48std::optional<mlir::Attribute> lowerConstRecordAttr(
49 cir::ConstRecordAttr constRecord, mlir::SymbolTableCollection &symbolTables,
50 const mlir::TypeConverter *converter,
51 const CIRPreConversionTypeMap &preConversionTypes, mlir::ModuleOp moduleOp);
52
53/// Return \p attr with the trailing zeros of any array that
54/// adjustGlobalTypeForInit would split off written out as explicit zero
55/// elements, so that it lowers to its own type. For constants that, unlike
56/// global initializers, cannot change type.
57mlir::Attribute spellOutSplitTrailingZeros(mlir::Attribute attr);
58
59/// Adjust \p llvmType (the converted type of \p init) to the concrete LLVM type
60/// a global constant initialized with \p init actually lowers to. This differs
61/// from a plain type conversion for flexible-array-member and union
62/// initializers, and the adjustment recurses through nested aggregates. Returns
63/// \p llvmType unchanged when no adjustment is needed. This is the single
64/// source of truth for the shape of a lowered record constant; the
65/// value-producing paths (the insertvalue visitor and lowerConstRecordAttr)
66/// conform to it. Returns null if the input type or an initializer type cannot
67/// be lowered. If provided, \p failedType receives the CIR initializer type
68/// whose conversion failed, including in nested aggregates.
69mlir::Type adjustGlobalTypeForInit(mlir::Type llvmType, mlir::Attribute init,
70 const mlir::TypeConverter &converter,
71 const mlir::DataLayout &dataLayout,
72 mlir::Type *failedType = nullptr);
73
74// A version of adjustGlobalTypeForInit which records where additional padding
75// was added in the middle, so we can properly adjust field indexes.
76mlir::Type
77adjustGlobalTypeForInit(mlir::Type llvmType, mlir::Attribute init,
78 const mlir::TypeConverter &converter,
79 const mlir::DataLayout &dataLayout,
80 llvm::SmallVectorImpl<unsigned> &paddingAddedIndexes,
81 mlir::Type *failedType = nullptr);
82
83mlir::Value getConstAPInt(mlir::OpBuilder &bld, mlir::Location loc,
84 mlir::Type typ, const llvm::APInt &val);
85
86mlir::Value getConst(mlir::OpBuilder &bld, mlir::Location loc, mlir::Type typ,
87 unsigned val);
88
89mlir::Value createShL(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs);
90
91mlir::Value createAShR(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs);
92
93mlir::Value createAnd(mlir::OpBuilder &bld, mlir::Value lhs,
94 const llvm::APInt &rhs);
95
96mlir::Value createLShR(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs);
97
98mlir::Type convertTypeForMemory(const mlir::TypeConverter &converter,
99 mlir::DataLayout const &dataLayout,
100 mlir::Type type, bool isForHLSL = false);
101
102/// The type of a load/store's *value*, as opposed to convertTypeForMemory's
103/// type of the memory it lives in. The two are effectively identical except
104/// with split-storage bit-int.
105mlir::Type convertTypeForLoadStore(const mlir::TypeConverter &converter,
106 mlir::DataLayout const &dataLayout,
107 mlir::Type type);
108
109// Convert a bit-int value to its llvm value, which can be either an array, or
110// just a large-power-of-2 integer.
111mlir::Attribute getBitIntStorageAttr(mlir::ConversionPatternRewriter &rewriter,
112 cir::IntAttr attr,
113 const mlir::DataLayout &dataLayout);
114
115#endif
mlir::Attribute spellOutSplitTrailingZeros(mlir::Attribute attr)
Return attr with the trailing zeros of any array that adjustGlobalTypeForInit would split off written...
mlir::DenseElementsAttr convertStringAttrToDenseElementsAttr(cir::ConstArrayAttr attr, mlir::Type type)
std::optional< mlir::DenseElementsAttr > convertToDenseElementsAttr(cir::ConstArrayAttr attr, const llvm::SmallVectorImpl< int64_t > &dims, mlir::Type convertedElementType)
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)
mlir::Value createAShR(mlir::OpBuilder &bld, mlir::Value lhs, unsigned rhs)
StorageTy getZeroInitFromType(mlir::Type ty)
Return a zero with the lowered element type's width or float semantics.
mlir::Value createAnd(mlir::OpBuilder &bld, mlir::Value lhs, const llvm::APInt &rhs)
mlir::Type convertTypeForLoadStore(const mlir::TypeConverter &converter, mlir::DataLayout const &dataLayout, mlir::Type type)
The type of a load/store's value, as opposed to convertTypeForMemory's type of the memory it lives in...
mlir::Type adjustGlobalTypeForInit(mlir::Type llvmType, mlir::Attribute init, const mlir::TypeConverter &converter, const mlir::DataLayout &dataLayout, mlir::Type *failedType=nullptr)
Adjust llvmType (the converted type of init) to the concrete LLVM type a global constant initialized ...
llvm::StringMap< mlir::Type > CIRPreConversionTypeMap
std::optional< mlir::Attribute > lowerConstArrayAttr(cir::ConstArrayAttr constArr, mlir::SymbolTableCollection &symbolTables, const mlir::TypeConverter *converter, const CIRPreConversionTypeMap &preConversionTypes, mlir::ModuleOp moduleOp)
std::optional< mlir::Attribute > lowerConstRecordAttr(cir::ConstRecordAttr constRecord, mlir::SymbolTableCollection &symbolTables, const mlir::TypeConverter *converter, const CIRPreConversionTypeMap &preConversionTypes, mlir::ModuleOp moduleOp)
mlir::Type convertTypeForMemory(const mlir::TypeConverter &converter, mlir::DataLayout const &dataLayout, mlir::Type type, bool isForHLSL=false)
mlir::Attribute getBitIntStorageAttr(mlir::ConversionPatternRewriter &rewriter, cir::IntAttr attr, const mlir::DataLayout &dataLayout)