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