clang 23.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 const mlir::TypeConverter *converter,
33 LLVMBlockAddressInfo *blockInfoAddr = nullptr);
34
35mlir::LLVM::Linkage convertLinkage(cir::GlobalLinkageKind linkage);
36
37void convertSideEffectForCall(mlir::Operation *callOp, bool isNothrow,
38 cir::SideEffect sideEffect,
39 mlir::LLVM::MemoryEffectsAttr &memoryEffect,
40 bool &noUnwind, bool &willReturn, bool &noReturn);
41
43 // Get the next tag index
44 uint32_t getTagIndex() { return blockTagOpIndex++; }
45
46 void mapBlockTag(cir::BlockAddrInfoAttr info, mlir::LLVM::BlockTagOp tagOp) {
47 [[maybe_unused]] auto result = blockInfoToTagOp.try_emplace(info, tagOp);
48 assert(result.second &&
49 "attempting to map a BlockTag operation that is already mapped");
50 }
51
52 // Lookup a BlockTagOp, may return nullptr if not yet registered.
53 mlir::LLVM::BlockTagOp lookupBlockTag(cir::BlockAddrInfoAttr info) const {
54 return blockInfoToTagOp.lookup(info);
55 }
56
57 // Record an unresolved BlockAddressOp that needs patching later.
58 void addUnresolvedBlockAddress(mlir::LLVM::BlockAddressOp op,
59 cir::BlockAddrInfoAttr info) {
60 unresolvedBlockAddressOp.try_emplace(op, info);
61 }
62
63 void clearUnresolvedMap() { unresolvedBlockAddressOp.clear(); }
64
65 llvm::DenseMap<mlir::LLVM::BlockAddressOp, cir::BlockAddrInfoAttr> &
67 return unresolvedBlockAddressOp;
68 }
69
70private:
71 // Maps a (function name, label name) pair to the corresponding BlockTagOp.
72 // Used to resolve CIR LabelOps into their LLVM BlockTagOp.
73 llvm::DenseMap<cir::BlockAddrInfoAttr, mlir::LLVM::BlockTagOp>
74 blockInfoToTagOp;
75 // Tracks BlockAddressOps that could not yet be fully resolved because
76 // their BlockTagOp was not available at the time of lowering. The map
77 // stores the unresolved BlockAddressOp along with its (function name, label
78 // name) pair so it can be patched later.
79 llvm::DenseMap<mlir::LLVM::BlockAddressOp, cir::BlockAddrInfoAttr>
80 unresolvedBlockAddressOp;
81 int32_t blockTagOpIndex;
82};
83
84#define GET_LLVM_LOWERING_PATTERNS
85#include "clang/CIR/Dialect/IR/CIRLowering.inc"
86#undef GET_LLVM_LOWERING_PATTERNS
87
88} // namespace direct
89} // namespace cir
90
91#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::LLVM::Linkage convertLinkage(cir::GlobalLinkageKind linkage)
mlir::Value lowerCirAttrAsValue(mlir::Operation *parentOp, const mlir::Attribute attr, mlir::ConversionPatternRewriter &rewriter, const mlir::TypeConverter *converter, LLVMBlockAddressInfo *blockInfoAddr)
Switches on the type of attribute and calls the appropriate conversion.
__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:53
llvm::DenseMap< mlir::LLVM::BlockAddressOp, cir::BlockAddrInfoAttr > & getUnresolvedBlockAddress()
Definition LowerToLLVM.h:66
void mapBlockTag(cir::BlockAddrInfoAttr info, mlir::LLVM::BlockTagOp tagOp)
Definition LowerToLLVM.h:46
void addUnresolvedBlockAddress(mlir::LLVM::BlockAddressOp op, cir::BlockAddrInfoAttr info)
Definition LowerToLLVM.h:58