clang 24.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
20#include "clang/AST/DeclGroup.h"
25#include "llvm/IR/DataLayout.h"
26
27using namespace cir;
28using namespace clang;
29
30void CIRGenerator::anchor() {}
31
34 const CodeGenOptions &cgo)
35 : diags(diags), fs(std::move(vfs)), codeGenOpts{cgo},
36 handlingTopLevelDecls{0} {}
38 // There should normally not be any leftover inline method definitions.
39 assert(deferredInlineMemberFuncDefs.empty() || diags.hasErrorOccurred());
40}
41
43 using namespace llvm;
44
45 this->astContext = &astContext;
46
47 mlirContext = std::make_unique<mlir::MLIRContext>();
48 // MLIR multithreading stays enabled; the CIRDiagnosticHandler is only ever
49 // invoked from a single thread.
51 mlirContext->loadDialect<mlir::DLTIDialect, cir::CIRDialect>();
52 mlirContext->getOrLoadDialect<mlir::acc::OpenACCDialect>();
53 mlirContext->getOrLoadDialect<mlir::omp::OpenMPDialect>();
54
55 cgm = std::make_unique<clang::CIRGen::CIRGenModule>(
56 *mlirContext.get(), astContext, codeGenOpts, diags);
57 mlir::ModuleOp mod = cgm->getModule();
58 llvm::DataLayout layout =
59 llvm::DataLayout(astContext.getTargetInfo().getDataLayoutString());
60 cir::setMLIRDataLayout(mod, layout);
61}
62
63bool CIRGenerator::verifyModule() const { return cgm->verifyModule(); }
64
65mlir::ModuleOp CIRGenerator::getModule() const { return cgm->getModule(); }
66
68 if (diags.hasUnrecoverableErrorOccurred())
69 return true;
70
71 HandlingTopLevelDeclRAII handlingDecl(*this);
72
73 for (Decl *decl : group)
74 cgm->emitTopLevelDecl(decl);
75
76 return true;
77}
78
80 // Release the Builder when there is no error.
81 if (!diags.hasErrorOccurred() && cgm)
82 cgm->release();
83
84 // If there are errors before or when releasing the cgm, reset the module to
85 // stop here before invoking the backend.
87}
88
90 if (diags.hasErrorOccurred())
91 return;
92
94
95 // We may want to emit this definition. However, that decision might be
96 // based on computing the linkage, and we have to defer that in case we are
97 // inside of something that will chagne the method's final linkage, e.g.
98 // typedef struct {
99 // void bar();
100 // void foo() { bar(); }
101 // } A;
102 deferredInlineMemberFuncDefs.push_back(d);
103
104 // Provide some coverage mapping even for methods that aren't emitted.
105 // Don't do this for templated classes though, as they may not be
106 // instantiable.
108}
109
111 if (deferredInlineMemberFuncDefs.empty())
112 return;
113
114 // Emit any deferred inline method definitions. Note that more deferred
115 // methods may be added during this loop, since ASTConsumer callbacks can be
116 // invoked if AST inspection results in declarations being added. Therefore,
117 // we use an index to loop over the deferredInlineMemberFuncDefs rather than
118 // a range.
119 HandlingTopLevelDeclRAII handlingDecls(*this);
120 for (unsigned i = 0; i != deferredInlineMemberFuncDefs.size(); ++i)
121 cgm->emitTopLevelDecl(deferredInlineMemberFuncDefs[i]);
122 deferredInlineMemberFuncDefs.clear();
123}
124
125/// HandleTagDeclDefinition - This callback is invoked each time a TagDecl to
126/// (e.g. struct, union, enum, class) is completed. This allows the client to
127/// hack on the type, which can occur at any point in the file (because these
128/// can be defined in declspecs).
130 if (diags.hasErrorOccurred())
131 return;
132
133 // Don't allow re-entrant calls to CIRGen triggered by PCH deserialization to
134 // emit deferred decls.
135 HandlingTopLevelDeclRAII handlingDecl(*this, /*EmitDeferred=*/false);
136
137 cgm->updateCompletedType(d);
138
139 // For MSVC compatibility, treat declarations of static data members with
140 // inline initializers as definitions.
141 if (astContext->getTargetInfo().getCXXABI().isMicrosoft())
142 cgm->errorNYI(d->getSourceRange(), "HandleTagDeclDefinition: MSABI");
143
144 // For OpenMP emit declare reduction functions or declare mapper, if
145 // required.
146 if (astContext->getLangOpts().OpenMP) {
147 for (Decl *member : d->decls()) {
148 if (auto *drd = dyn_cast<OMPDeclareReductionDecl>(member)) {
149 if (astContext->DeclMustBeEmitted(drd))
150 cgm->errorNYI(d->getSourceRange(),
151 "HandleTagDeclDefinition: OMPDeclareReductionDecl");
152 } else if (auto *dmd = dyn_cast<OMPDeclareMapperDecl>(member)) {
153 if (astContext->DeclMustBeEmitted(dmd))
154 cgm->errorNYI(d->getSourceRange(),
155 "HandleTagDeclDefinition: OMPDeclareMapperDecl");
156 }
157 }
158 }
159}
160
162 if (diags.hasErrorOccurred())
163 return;
164
166}
167
169 if (diags.hasErrorOccurred())
170 return;
171
172 cgm->handleCXXStaticMemberVarInstantiation(D);
173}
174
176 const OpenACCRoutineDecl *RD) {
177 llvm::StringRef mangledName = cgm->getMangledName(FD);
178 cir::FuncOp entry =
179 mlir::dyn_cast_if_present<cir::FuncOp>(cgm->getGlobalValue(mangledName));
180
181 // if this wasn't generated, don't force it to be.
182 if (!entry)
183 return;
184 cgm->emitOpenACCRoutineDecl(FD, entry, RD->getBeginLoc(), RD->clauses());
185}
186
188 if (diags.hasErrorOccurred())
189 return;
190
191 cgm->emitTentativeDefinition(d);
192}
193
195 if (diags.hasErrorOccurred())
196 return;
197
198 cgm->emitVTable(rd);
199}
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:2403
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:234
Represents a function declaration or definition.
Definition Decl.h:2058
bool doesThisDeclarationHaveABody() const
Returns whether this specific declaration of the function has a body.
Definition Decl.h:2439
ArrayRef< const OpenACCClause * > clauses() const
Definition DeclOpenACC.h:62
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3851
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:4956
Represents a variable declaration or definition.
Definition Decl.h:932
void registerAllDialects(mlir::DialectRegistry &registry)
void setMLIRDataLayout(mlir::ModuleOp mod, const llvm::DataLayout &dl)
Translate dl into a DLTI data-layout spec and attach it to mod.
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
Top level wrappers for InstallAPI frontend operations.
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
static bool coverageMapping()
static bool cleanupAfterErrorDiags()
static bool generateDebugInfo()