clang 24.0.0git
LowerToLLVM.h
Go to the documentation of this file.
1//====- LowerToLLVM.h- Lowering from CIR to LLVM --------------------------===//
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 an interface for converting CIR modules to LLVM IR.
10//
11//===----------------------------------------------------------------------===//
12#ifndef CLANG_CIR_LOWERTOLLVM_H
13#define CLANG_CIR_LOWERTOLLVM_H
14
15#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
16#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
17#include "mlir/Transforms/DialectConversion.h"
21
22namespace cir {
23
24namespace direct {
25
27
28/// Convert a CIR attribute to an LLVM value, or return null on failure.
29/// May use the datalayout for lowering attributes to-be-stored in memory. When
30/// the attribute may contain block address attributes, `blockInfoAddr` is used
31/// to resolve them.
32mlir::Value
33lowerCirAttrAsValue(mlir::Location parentLoc, mlir::ModuleOp parentModule,
34 mlir::Attribute attr,
35 mlir::ConversionPatternRewriter &rewriter,
36 mlir::SymbolTableCollection &symbolTables,
37 const mlir::TypeConverter *converter,
38 const CIRPreConversionTypeMap &preConversionTypes,
39 LLVMBlockAddressInfo *blockInfoAddr = nullptr);
40
41mlir::LLVM::Linkage convertLinkage(cir::GlobalLinkageKind linkage);
42
43/// Declare `fnName` with type `fnTy` before the function enclosing `srcOp`,
44/// unless the module already has a symbol of that name.
45void createLLVMFuncOpIfNotExist(mlir::ConversionPatternRewriter &rewriter,
46 mlir::SymbolTableCollection &symbolTables,
47 mlir::Operation *srcOp, llvm::StringRef fnName,
48 mlir::Type fnTy,
49 mlir::ArrayAttr argAttrs = nullptr,
50 mlir::ArrayAttr resAttrs = nullptr);
51
53 // Get the next tag index
54 uint32_t getTagIndex() { return blockTagOpIndex++; }
55
56 void mapBlockTag(cir::BlockAddrInfoAttr info, mlir::LLVM::BlockTagOp tagOp) {
57 [[maybe_unused]] auto result = blockInfoToTagOp.try_emplace(info, tagOp);
58 assert(result.second &&
59 "attempting to map a BlockTag operation that is already mapped");
60 }
61
62 // Lookup a BlockTagOp, may return nullptr if not yet registered.
63 mlir::LLVM::BlockTagOp lookupBlockTag(cir::BlockAddrInfoAttr info) const {
64 return blockInfoToTagOp.lookup(info);
65 }
66
67 // Record an unresolved BlockAddressOp that needs patching later.
68 void addUnresolvedBlockAddress(mlir::LLVM::BlockAddressOp op,
69 cir::BlockAddrInfoAttr info) {
70 unresolvedBlockAddressOp.try_emplace(op, info);
71 }
72
73 void clearUnresolvedMap() { unresolvedBlockAddressOp.clear(); }
74
75 llvm::DenseMap<mlir::LLVM::BlockAddressOp, cir::BlockAddrInfoAttr> &
77 return unresolvedBlockAddressOp;
78 }
79
80private:
81 // Maps a (function name, label name) pair to the corresponding BlockTagOp.
82 // Used to resolve CIR LabelOps into their LLVM BlockTagOp.
83 llvm::DenseMap<cir::BlockAddrInfoAttr, mlir::LLVM::BlockTagOp>
84 blockInfoToTagOp;
85 // Tracks BlockAddressOps that could not yet be fully resolved because
86 // their BlockTagOp was not available at the time of lowering. The map
87 // stores the unresolved BlockAddressOp along with its (function name, label
88 // name) pair so it can be patched later.
89 llvm::DenseMap<mlir::LLVM::BlockAddressOp, cir::BlockAddrInfoAttr>
90 unresolvedBlockAddressOp;
91 int32_t blockTagOpIndex = 0;
92};
93
94mlir::LogicalResult lowerToConstrainedFPIntrinsic(
95 mlir::Operation *op, mlir::ValueRange operands, cir::FenvAttr fenv,
96 mlir::Type llvmResTy, mlir::ConversionPatternRewriter &rewriter,
97 llvm::StringRef constrainedMnemonic, bool hasRoundingMode,
98 mlir::LLVM::FastmathFlags fastmathFlags = {});
99
100template <typename LLVMOp>
101mlir::LogicalResult lowerConstrainableFPOp(
102 mlir::Operation *op, mlir::ValueRange operands, cir::FenvAttr fenv,
103 cir::FastMathFlagsAttr fastmath, const mlir::TypeConverter &typeConverter,
104 mlir::ConversionPatternRewriter &rewriter,
105 llvm::StringRef constrainedMnemonic, bool hasRoundingMode);
106
107#define GET_LLVM_LOWERING_PATTERNS
108#include "clang/CIR/Dialect/IR/CIRLowering.inc"
109#undef GET_LLVM_LOWERING_PATTERNS
110
111} // namespace direct
112} // namespace cir
113
114#endif // CLANG_CIR_LOWERTOLLVM_H
llvm::StringMap< mlir::Type > CIRPreConversionTypeMap
mlir::Value lowerCirAttrAsValue(mlir::Location parentLoc, mlir::ModuleOp parentModule, const mlir::Attribute attr, mlir::ConversionPatternRewriter &rewriter, mlir::SymbolTableCollection &symbolTables, const mlir::TypeConverter *converter, const CIRPreConversionTypeMap &preConversionTypes, LLVMBlockAddressInfo *blockInfoAddr)
Switches on the type of attribute and calls the appropriate conversion.
mlir::LogicalResult lowerToConstrainedFPIntrinsic(mlir::Operation *op, mlir::ValueRange operands, cir::FenvAttr fenv, mlir::Type llvmResTy, mlir::ConversionPatternRewriter &rewriter, llvm::StringRef constrainedMnemonic, bool hasRoundingMode, mlir::LLVM::FastmathFlags fastmathFlags)
void createLLVMFuncOpIfNotExist(mlir::ConversionPatternRewriter &rewriter, mlir::SymbolTableCollection &symbolTables, mlir::Operation *srcOp, llvm::StringRef fnName, mlir::Type fnTy, mlir::ArrayAttr argAttrs, mlir::ArrayAttr resAttrs)
Declare fnName with type fnTy before the function enclosing srcOp, unless the module already has a sy...
mlir::LogicalResult lowerConstrainableFPOp(mlir::Operation *op, mlir::ValueRange operands, cir::FenvAttr fenv, cir::FastMathFlagsAttr fastmath, const mlir::TypeConverter &typeConverter, mlir::ConversionPatternRewriter &rewriter, llvm::StringRef constrainedMnemonic, bool hasRoundingMode)
mlir::LLVM::Linkage convertLinkage(cir::GlobalLinkageKind linkage)
__builtin_bitreverse32 uint32_t __builtin_riscv_aaddu_u32 __builtin_riscv_asubu_u32 __builtin_elementwise_add_sat __builtin_elementwise_sub_sat int32_t
mlir::LLVM::BlockTagOp lookupBlockTag(cir::BlockAddrInfoAttr info) const
Definition LowerToLLVM.h:63
llvm::DenseMap< mlir::LLVM::BlockAddressOp, cir::BlockAddrInfoAttr > & getUnresolvedBlockAddress()
Definition LowerToLLVM.h:76
void mapBlockTag(cir::BlockAddrInfoAttr info, mlir::LLVM::BlockTagOp tagOp)
Definition LowerToLLVM.h:56
void addUnresolvedBlockAddress(mlir::LLVM::BlockAddressOp op, cir::BlockAddrInfoAttr info)
Definition LowerToLLVM.h:68