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"
18#include "clang/CIR/Dialect/IR/CIRDialect.h"
19#include "clang/CIR/Interfaces/CIROpInterfaces.h"
20#include "clang/CIR/LoweringHelpers.h"
21
22namespace cir {
23
24namespace direct {
25
26struct LLVMBlockAddressInfo;
27struct LLVMAnnotationGlobals;
28
29/// Convert a CIR attribute to an LLVM value, or return null on failure.
30/// May use the datalayout for lowering attributes to-be-stored in memory. When
31/// the attribute may contain block address attributes, `blockInfoAddr` is used
32/// to resolve them.
33mlir::Value
34lowerCirAttrAsValue(mlir::Location parentLoc, mlir::ModuleOp parentModule,
35 mlir::Attribute attr,
36 mlir::ConversionPatternRewriter &rewriter,
37 mlir::SymbolTableCollection &symbolTables,
38 const mlir::TypeConverter *converter,
39 const CIRPreConversionTypeMap &preConversionTypes,
40 LLVMBlockAddressInfo *blockInfoAddr = nullptr);
41
42mlir::LLVM::Linkage convertLinkage(cir::GlobalLinkageKind linkage);
43
44/// Declare `fnName` with type `fnTy` before the function enclosing `srcOp`,
45/// unless the module already has a symbol of that name.
46void createLLVMFuncOpIfNotExist(mlir::ConversionPatternRewriter &rewriter,
47 mlir::SymbolTableCollection &symbolTables,
48 mlir::Operation *srcOp, llvm::StringRef fnName,
49 mlir::Type fnTy,
50 mlir::ArrayAttr argAttrs = nullptr,
51 mlir::ArrayAttr resAttrs = nullptr);
52
53struct LLVMBlockAddressInfo {
54 // Get the next tag index
55 uint32_t getTagIndex() { return blockTagOpIndex++; }
56
57 void mapBlockTag(cir::BlockAddrInfoAttr info, mlir::LLVM::BlockTagOp tagOp) {
58 [[maybe_unused]] auto result = blockInfoToTagOp.try_emplace(Key: info, Args&: tagOp);
59 assert(result.second &&
60 "attempting to map a BlockTag operation that is already mapped");
61 }
62
63 // Lookup a BlockTagOp, may return nullptr if not yet registered.
64 mlir::LLVM::BlockTagOp lookupBlockTag(cir::BlockAddrInfoAttr info) const {
65 return blockInfoToTagOp.lookup(Val: info);
66 }
67
68 // Record an unresolved BlockAddressOp that needs patching later.
69 void addUnresolvedBlockAddress(mlir::LLVM::BlockAddressOp op,
70 cir::BlockAddrInfoAttr info) {
71 unresolvedBlockAddressOp.try_emplace(Key: op, Args&: info);
72 }
73
74 void clearUnresolvedMap() { unresolvedBlockAddressOp.clear(); }
75
76 llvm::DenseMap<mlir::LLVM::BlockAddressOp, cir::BlockAddrInfoAttr> &
77 getUnresolvedBlockAddress() {
78 return unresolvedBlockAddressOp;
79 }
80
81private:
82 // Maps a (function name, label name) pair to the corresponding BlockTagOp.
83 // Used to resolve CIR LabelOps into their LLVM BlockTagOp.
84 llvm::DenseMap<cir::BlockAddrInfoAttr, mlir::LLVM::BlockTagOp>
85 blockInfoToTagOp;
86 // Tracks BlockAddressOps that could not yet be fully resolved because
87 // their BlockTagOp was not available at the time of lowering. The map
88 // stores the unresolved BlockAddressOp along with its (function name, label
89 // name) pair so it can be patched later.
90 llvm::DenseMap<mlir::LLVM::BlockAddressOp, cir::BlockAddrInfoAttr>
91 unresolvedBlockAddressOp;
92 int32_t blockTagOpIndex = 0;
93};
94
95mlir::LogicalResult lowerToConstrainedFPIntrinsic(
96 mlir::Operation *op, mlir::ValueRange operands, cir::FenvAttr fenv,
97 mlir::Type llvmResTy, mlir::ConversionPatternRewriter &rewriter,
98 llvm::StringRef constrainedMnemonic, bool hasRoundingMode,
99 mlir::LLVM::FastmathFlags fastmathFlags = {});
100
101template <typename LLVMOp>
102mlir::LogicalResult lowerFPOp(mlir::Operation *op, mlir::ValueRange operands,
103 cir::FastMathFlagsAttr fastmath,
104 const mlir::TypeConverter &typeConverter,
105 mlir::ConversionPatternRewriter &rewriter);
106
107template <typename LLVMOp>
108mlir::LogicalResult lowerConstrainableFPOp(
109 mlir::Operation *op, mlir::ValueRange operands, cir::FenvAttr fenv,
110 cir::FastMathFlagsAttr fastmath, const mlir::TypeConverter &typeConverter,
111 mlir::ConversionPatternRewriter &rewriter,
112 llvm::StringRef constrainedMnemonic, bool hasRoundingMode);
113
114// Convert the value of a CIR attribute or property to the LLVM property that
115// a generated lowering forwards it to (`llvmOpProps`). The overload is picked
116// by the C++ type of the CIR value.
117mlir::LLVM::IntegerOverflowFlags
118convertToLLVMProperty(mlir::MLIRContext *ctx, cir::OverflowFlags flags);
119mlir::LLVM::FastmathFlagsAttr
120convertToLLVMProperty(mlir::MLIRContext *ctx,
121 std::optional<cir::FastMathFlags> flags);
122mlir::IntegerAttr convertToLLVMProperty(mlir::MLIRContext *ctx,
123 cir::FPClassTest flags);
124mlir::IntegerAttr convertToLLVMProperty(mlir::MLIRContext *ctx, bool flag);
125
126#define GET_LLVM_LOWERING_PATTERNS
127#include "clang/CIR/Dialect/IR/CIRLowering.inc"
128#undef GET_LLVM_LOWERING_PATTERNS
129
130} // namespace direct
131} // namespace cir
132
133#endif // CLANG_CIR_LOWERTOLLVM_H
134