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