1//===--- LowerModule.h - Abstracts CIR's module lowering --------*- C++ -*-===//
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 partially mimics clang/lib/CodeGen/CodeGenModule.h. The queries are
10// adapted to operate on the CIR dialect, however.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_LOWERMODULE_H
15#define CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_LOWERMODULE_H
16
17#include "CIRCXXABI.h"
18#include "TargetLoweringInfo.h"
19#include "mlir/IR/BuiltinOps.h"
20#include "clang/Basic/CodeGenOptions.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/CIR/Dialect/IR/CIRDialect.h"
24#include "clang/CIR/MissingFeatures.h"
25#include "llvm/ADT/StringRef.h"
26#include <memory>
27
28namespace cir {
29
30class LowerModule {
31 mlir::ModuleOp module;
32 const clang::LangOptions langOpts;
33 const std::unique_ptr<clang::TargetInfo> target;
34 std::unique_ptr<TargetLoweringInfo> targetLoweringInfo;
35 std::unique_ptr<CIRCXXABI> abi;
36
37public:
38 LowerModule(clang::LangOptions langOpts, clang::CodeGenOptions codeGenOpts,
39 mlir::ModuleOp &module,
40 std::unique_ptr<clang::TargetInfo> target);
41 ~LowerModule() = default;
42
43 // The lowering-relevant LangOptions, populated by createLowerModule() from
44 // the module's #cir.lowering_options attribute when present (so a
45 // reloaded .cir lowers the same way without a live clang::LangOptions),
46 // otherwise left at their defaults.
47 const clang::LangOptions &getLangOpts() const { return langOpts; }
48
49 clang::TargetCXXABI::Kind getCXXABIKind() const {
50 if (langOpts.CXXABI)
51 return *langOpts.CXXABI;
52 assert(target &&
53 "No C++ ABI kind without a target (missing/unsupported cir.triple)");
54 return target->getCXXABI().getKind();
55 }
56 CIRCXXABI &getCXXABI() const {
57 assert(abi &&
58 "No C++ ABI without a target (missing/unsupported cir.triple)");
59 return *abi;
60 }
61
62 bool hasTarget() const {
63 // Null target means module had no cir.triple, or clang has no TargetInfo
64 // for it; but when cir.triple becomes mandatory we need to figure out how
65 // to handle cir.triple's without a corresponding TargetInfo / potentially
66 // removing this function.
67 assert(!cir::MissingFeatures::makeTripleAlwaysPresent());
68 return target != nullptr;
69 }
70 const clang::TargetInfo &getTarget() const {
71 assert(target &&
72 "LowerModule has no target (missing/unsupported cir.triple)");
73 return *target;
74 }
75
76 // FIXME remove this once all ABIs are supported.
77 bool hasCXXABI() const { return abi != nullptr; }
78
79 mlir::MLIRContext *getMLIRContext() { return module.getContext(); }
80
81 mlir::ModuleOp getModule() const { return module; }
82
83 const TargetLoweringInfo &getTargetLoweringInfo();
84};
85
86std::unique_ptr<LowerModule> createLowerModule(mlir::ModuleOp module);
87
88} // namespace cir
89
90#endif // CLANG_LIB_CIR_DIALECT_TRANSFORMS_TARGETLOWERING_LOWERMODULE_H
91