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