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
25/// Convert a CIR attribute to an LLVM attribute. May use the datalayout for
26/// lowering attributes to-be-stored in memory.
27mlir::Value lowerCirAttrAsValue(mlir::Operation *parentOp, mlir::Attribute attr,
28 mlir::ConversionPatternRewriter &rewriter,
29 const mlir::TypeConverter *converter);
30
31mlir::LLVM::Linkage convertLinkage(cir::GlobalLinkageKind linkage);
32
33void convertSideEffectForCall(mlir::Operation *callOp, bool isNothrow,
34 cir::SideEffect sideEffect,
35 mlir::LLVM::MemoryEffectsAttr &memoryEffect,
36 bool &noUnwind, bool &willReturn, bool &noReturn);
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 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
80#define GET_LLVM_LOWERING_PATTERNS
81#include "clang/CIR/Dialect/IR/CIRLowering.inc"
82#undef GET_LLVM_LOWERING_PATTERNS
83
84} // namespace direct
85} // namespace cir
86
87#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::Value lowerCirAttrAsValue(mlir::Operation *parentOp, const mlir::Attribute attr, mlir::ConversionPatternRewriter &rewriter, const mlir::TypeConverter *converter)
Switches on the type of attribute and calls the appropriate conversion.
mlir::LLVM::Linkage convertLinkage(cir::GlobalLinkageKind linkage)
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