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
39 // Get the next tag index
40 uint32_t getTagIndex() { return blockTagOpIndex++; }
41
42 void mapBlockTag(cir::BlockAddrInfoAttr info, mlir::LLVM::BlockTagOp tagOp) {
43 [[maybe_unused]] auto result = blockInfoToTagOp.try_emplace(info, tagOp);
44 assert(result.second &&
45 "attempting to map a BlockTag operation that is already mapped");
46 }
47
48 // Lookup a BlockTagOp, may return nullptr if not yet registered.
49 mlir::LLVM::BlockTagOp lookupBlockTag(cir::BlockAddrInfoAttr info) const {
50 return blockInfoToTagOp.lookup(info);
51 }
52
53 // Record an unresolved BlockAddressOp that needs patching later.
54 void addUnresolvedBlockAddress(mlir::LLVM::BlockAddressOp op,
55 cir::BlockAddrInfoAttr info) {
56 unresolvedBlockAddressOp.try_emplace(op, info);
57 }
58
59 void clearUnresolvedMap() { unresolvedBlockAddressOp.clear(); }
60
61 llvm::DenseMap<mlir::LLVM::BlockAddressOp, cir::BlockAddrInfoAttr> &
63 return unresolvedBlockAddressOp;
64 }
65
66private:
67 // Maps a (function name, label name) pair to the corresponding BlockTagOp.
68 // Used to resolve CIR LabelOps into their LLVM BlockTagOp.
69 llvm::DenseMap<cir::BlockAddrInfoAttr, mlir::LLVM::BlockTagOp>
70 blockInfoToTagOp;
71 // Tracks BlockAddressOps that could not yet be fully resolved because
72 // their BlockTagOp was not available at the time of lowering. The map
73 // stores the unresolved BlockAddressOp along with its (function name, label
74 // name) pair so it can be patched later.
75 llvm::DenseMap<mlir::LLVM::BlockAddressOp, cir::BlockAddrInfoAttr>
76 unresolvedBlockAddressOp;
77 int32_t blockTagOpIndex;
78};
79
80mlir::LogicalResult lowerToConstrainedFPIntrinsic(
81 mlir::Operation *op, mlir::ValueRange operands, cir::FenvAttr fenv,
82 mlir::Type llvmResTy, mlir::ConversionPatternRewriter &rewriter,
83 llvm::StringRef constrainedMnemonic, bool hasRoundingMode,
84 mlir::LLVM::FastmathFlags fastmathFlags = {});
85
86template <typename LLVMOp>
87mlir::LogicalResult lowerConstrainableFPOp(
88 mlir::Operation *op, mlir::ValueRange operands, cir::FenvAttr fenv,
89 const mlir::TypeConverter &typeConverter,
90 mlir::ConversionPatternRewriter &rewriter,
91 llvm::StringRef constrainedMnemonic, bool hasRoundingMode);
92
93#define GET_LLVM_LOWERING_PATTERNS
94#include "clang/CIR/Dialect/IR/CIRLowering.inc"
95#undef GET_LLVM_LOWERING_PATTERNS
96
97} // namespace direct
98} // namespace cir
99
100#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.
mlir::LLVM::Linkage convertLinkage(cir::GlobalLinkageKind linkage)
mlir::LogicalResult lowerConstrainableFPOp(mlir::Operation *op, mlir::ValueRange operands, cir::FenvAttr fenv, const mlir::TypeConverter &typeConverter, mlir::ConversionPatternRewriter &rewriter, llvm::StringRef constrainedMnemonic, bool hasRoundingMode)
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t
mlir::LLVM::BlockTagOp lookupBlockTag(cir::BlockAddrInfoAttr info) const
Definition LowerToLLVM.h:49
llvm::DenseMap< mlir::LLVM::BlockAddressOp, cir::BlockAddrInfoAttr > & getUnresolvedBlockAddress()
Definition LowerToLLVM.h:62
void mapBlockTag(cir::BlockAddrInfoAttr info, mlir::LLVM::BlockTagOp tagOp)
Definition LowerToLLVM.h:42
void addUnresolvedBlockAddress(mlir::LLVM::BlockAddressOp op, cir::BlockAddrInfoAttr info)
Definition LowerToLLVM.h:54