clang 23.0.0git
CIRGenerator.cpp
Go to the documentation of this file.
1//===--- CIRGenerator.cpp - Emit CIR from ASTs ----------------------------===//
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 builds an AST and converts it to CIR.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CIRGenModule.h"
14
15#include "mlir/Dialect/DLTI/DLTI.h"
16#include "mlir/Dialect/OpenACC/OpenACC.h"
17#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
18#include "mlir/IR/MLIRContext.h"
19#include "mlir/Target/LLVMIR/Import.h"
20
21#include "clang/AST/DeclGroup.h"
24#include "llvm/IR/DataLayout.h"
25
26using namespace cir;
27using namespace clang;
28
29void CIRGenerator::anchor() {}
30
33 const CodeGenOptions &cgo)
34 : diags(diags), fs(std::move(vfs)), codeGenOpts{cgo},
35 handlingTopLevelDecls{0} {}
37 // There should normally not be any leftover inline method definitions.
38 assert(deferredInlineMemberFuncDefs.empty() || diags.hasErrorOccurred());
39}
40
41static void setMLIRDataLayout(mlir::ModuleOp &mod, const llvm::DataLayout &dl) {
42 mlir::MLIRContext *mlirContext = mod.getContext();
43 mlir::DataLayoutSpecInterface dlSpec =
44 mlir::translateDataLayout(dl, mlirContext);
45 mod->setAttr(mlir::DLTIDialect::kDataLayoutAttrName, dlSpec);
46}
47
49 using namespace llvm;
50
51 this->astContext = &astContext;
52
53 mlirContext = std::make_unique<mlir::MLIRContext>();
55 mlirContext->loadDialect<mlir::DLTIDialect, cir::CIRDialect>();
56 mlirContext->getOrLoadDialect<mlir::acc::OpenACCDialect>();
57 mlirContext->getOrLoadDialect<mlir::omp::OpenMPDialect>();
58
59 cgm = std::make_unique<clang::CIRGen::CIRGenModule>(
60 *mlirContext.get(), astContext, codeGenOpts, diags);
61 mlir::ModuleOp mod = cgm->getModule();
62 llvm::DataLayout layout =
63 llvm::DataLayout(astContext.getTargetInfo().getDataLayoutString());
64 setMLIRDataLayout(mod, layout);
65}
66
67bool CIRGenerator::verifyModule() const { return cgm->verifyModule(); }
68
69mlir::ModuleOp CIRGenerator::getModule() const { return cgm->getModule(); }
70
72 if (diags.hasUnrecoverableErrorOccurred())
73 return true;
74
75 HandlingTopLevelDeclRAII handlingDecl(*this);
76
77 for (Decl *decl : group)
78 cgm->emitTopLevelDecl(decl);
79
80 return true;
81}
82
84 // Release the Builder when there is no error.
85 if (!diags.hasErrorOccurred() && cgm)
86 cgm->release();
87
88 // If there are errors before or when releasing the cgm, reset the module to
89 // stop here before invoking the backend.
91}
92
94 if (diags.hasErrorOccurred())
95 return;
96
98
99 // We may want to emit this definition. However, that decision might be
100 // based on computing the linkage, and we have to defer that in case we are
101 // inside of something that will chagne the method's final linkage, e.g.
102 // typedef struct {
103 // void bar();
104 // void foo() { bar(); }
105 // } A;
106 deferredInlineMemberFuncDefs.push_back(d);
107
108 // Provide some coverage mapping even for methods that aren't emitted.
109 // Don't do this for templated classes though, as they may not be
110 // instantiable.
112}
113
115 if (deferredInlineMemberFuncDefs.empty())
116 return;
117
118 // Emit any deferred inline method definitions. Note that more deferred
119 // methods may be added during this loop, since ASTConsumer callbacks can be
120 // invoked if AST inspection results in declarations being added. Therefore,
121 // we use an index to loop over the deferredInlineMemberFuncDefs rather than
122 // a range.
123 HandlingTopLevelDeclRAII handlingDecls(*this);
124 for (unsigned i = 0; i != deferredInlineMemberFuncDefs.size(); ++i)
125 cgm->emitTopLevelDecl(deferredInlineMemberFuncDefs[i]);
126 deferredInlineMemberFuncDefs.clear();
127}
128
129/// HandleTagDeclDefinition - This callback is invoked each time a TagDecl to
130/// (e.g. struct, union, enum, class) is completed. This allows the client to
131/// hack on the type, which can occur at any point in the file (because these
132/// can be defined in declspecs).
134 if (diags.hasErrorOccurred())
135 return;
136
137 // Don't allow re-entrant calls to CIRGen triggered by PCH deserialization to
138 // emit deferred decls.
139 HandlingTopLevelDeclRAII handlingDecl(*this, /*EmitDeferred=*/false);
140
141 cgm->updateCompletedType(d);
142
143 // For MSVC compatibility, treat declarations of static data members with
144 // inline initializers as definitions.
145 if (astContext->getTargetInfo().getCXXABI().isMicrosoft())
146 cgm->errorNYI(d->getSourceRange(), "HandleTagDeclDefinition: MSABI");
147
148 // For OpenMP emit declare reduction functions or declare mapper, if
149 // required.
150 if (astContext->getLangOpts().OpenMP) {
151 for (Decl *member : d->decls()) {
152 if (auto *drd = dyn_cast<OMPDeclareReductionDecl>(member)) {
153 if (astContext->DeclMustBeEmitted(drd))
154 cgm->errorNYI(d->getSourceRange(),
155 "HandleTagDeclDefinition: OMPDeclareReductionDecl");
156 } else if (auto *dmd = dyn_cast<OMPDeclareMapperDecl>(member)) {
157 if (astContext->DeclMustBeEmitted(dmd))
158 cgm->errorNYI(d->getSourceRange(),
159 "HandleTagDeclDefinition: OMPDeclareMapperDecl");
160 }
161 }
162 }
163}
164
166 if (diags.hasErrorOccurred())
167 return;
168
170}
171
173 if (diags.hasErrorOccurred())
174 return;
175
176 cgm->handleCXXStaticMemberVarInstantiation(D);
177}
178
180 const OpenACCRoutineDecl *RD) {
181 llvm::StringRef mangledName = cgm->getMangledName(FD);
182 cir::FuncOp entry =
183 mlir::dyn_cast_if_present<cir::FuncOp>(cgm->getGlobalValue(mangledName));
184
185 // if this wasn't generated, don't force it to be.
186 if (!entry)
187 return;
188 cgm->emitOpenACCRoutineDecl(FD, entry, RD->getBeginLoc(), RD->clauses());
189}
190
192 if (diags.hasErrorOccurred())
193 return;
194
195 cgm->emitTentativeDefinition(d);
196}
197
199 if (diags.hasErrorOccurred())
200 return;
201
202 cgm->emitVTable(rd);
203}
static void setMLIRDataLayout(mlir::ModuleOp &mod, const llvm::DataLayout &dl)
void HandleTagDeclDefinition(clang::TagDecl *d) override
HandleTagDeclDefinition - This callback is invoked each time a TagDecl to (e.g.
CIRGenerator(clang::DiagnosticsEngine &diags, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > fs, const clang::CodeGenOptions &cgo)
mlir::ModuleOp getModule() const
std::unique_ptr< clang::CIRGen::CIRGenModule > cgm
void HandleTranslationUnit(clang::ASTContext &astContext) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
bool verifyModule() const
void HandleVTable(clang::CXXRecordDecl *rd) override
Callback involved at the end of a translation unit to notify the consumer that a vtable for the given...
~CIRGenerator() override
bool HandleTopLevelDecl(clang::DeclGroupRef group) override
HandleTopLevelDecl - Handle the specified top-level declaration.
void CompleteTentativeDefinition(clang::VarDecl *d) override
CompleteTentativeDefinition - Callback invoked at the end of a translation unit to notify the consume...
void HandleTagDeclRequiredDefinition(const clang::TagDecl *D) override
This callback is invoked the first time each TagDecl is required to be complete.
void HandleOpenACCRoutineReference(const clang::FunctionDecl *FD, const clang::OpenACCRoutineDecl *RD) override
Callback to handle the end-of-translation unit attachment of OpenACC routine declaration information.
void HandleInlineFunctionDefinition(clang::FunctionDecl *d) override
This callback is invoked each time an inline (method or friend) function definition in a class is com...
void HandleCXXStaticMemberVarInstantiation(clang::VarDecl *D) override
HandleCXXStaticMemberVarInstantiation - Tell the consumer that this.
std::unique_ptr< mlir::MLIRContext > mlirContext
void Initialize(clang::ASTContext &astContext) override
Initialize - This is called to initialize the consumer, providing the ASTContext.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition DeclBase.h:2386
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
SourceLocation getBeginLoc() const LLVM_READONLY
Definition DeclBase.h:439
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:233
Represents a function declaration or definition.
Definition Decl.h:2018
bool doesThisDeclarationHaveABody() const
Returns whether this specific declaration of the function has a body.
Definition Decl.h:2344
ArrayRef< const OpenACCClause * > clauses() const
Definition DeclOpenACC.h:62
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3739
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:4889
Represents a variable declaration or definition.
Definition Decl.h:924
void registerAllDialects(mlir::DialectRegistry &registry)
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
The JSON file list parser is used to communicate input to InstallAPI.
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
static bool coverageMapping()
static bool cleanupAfterErrorDiags()
static bool generateDebugInfo()