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>();
49 mlirContext->loadDialect<mlir::DLTIDialect, cir::CIRDialect>();
50 mlirContext->getOrLoadDialect<mlir::acc::OpenACCDialect>();
51 mlirContext->getOrLoadDialect<mlir::omp::OpenMPDialect>();
52
53 cgm = std::make_unique<clang::CIRGen::CIRGenModule>(
54 *mlirContext.get(), astContext, codeGenOpts, diags);
55 mlir::ModuleOp mod = cgm->getModule();
56 llvm::DataLayout layout =
57 llvm::DataLayout(astContext.getTargetInfo().getDataLayoutString());
58 cir::setMLIRDataLayout(mod, layout);
59}
60
61bool CIRGenerator::verifyModule() const { return cgm->verifyModule(); }
62
63mlir::ModuleOp CIRGenerator::getModule() const { return cgm->getModule(); }
64
66 if (diags.hasUnrecoverableErrorOccurred())
67 return true;
68
69 HandlingTopLevelDeclRAII handlingDecl(*this);
70
71 for (Decl *decl : group)
72 cgm->emitTopLevelDecl(decl);
73
74 return true;
75}
76
78 // Release the Builder when there is no error.
79 if (!diags.hasErrorOccurred() && cgm)
80 cgm->release();
81
82 // If there are errors before or when releasing the cgm, reset the module to
83 // stop here before invoking the backend.
85}
86
88 if (diags.hasErrorOccurred())
89 return;
90
92
93 // We may want to emit this definition. However, that decision might be
94 // based on computing the linkage, and we have to defer that in case we are
95 // inside of something that will chagne the method's final linkage, e.g.
96 // typedef struct {
97 // void bar();
98 // void foo() { bar(); }
99 // } A;
100 deferredInlineMemberFuncDefs.push_back(d);
101
102 // Provide some coverage mapping even for methods that aren't emitted.
103 // Don't do this for templated classes though, as they may not be
104 // instantiable.
106}
107
109 if (deferredInlineMemberFuncDefs.empty())
110 return;
111
112 // Emit any deferred inline method definitions. Note that more deferred
113 // methods may be added during this loop, since ASTConsumer callbacks can be
114 // invoked if AST inspection results in declarations being added. Therefore,
115 // we use an index to loop over the deferredInlineMemberFuncDefs rather than
116 // a range.
117 HandlingTopLevelDeclRAII handlingDecls(*this);
118 for (unsigned i = 0; i != deferredInlineMemberFuncDefs.size(); ++i)
119 cgm->emitTopLevelDecl(deferredInlineMemberFuncDefs[i]);
120 deferredInlineMemberFuncDefs.clear();
121}
122
123/// HandleTagDeclDefinition - This callback is invoked each time a TagDecl to
124/// (e.g. struct, union, enum, class) is completed. This allows the client to
125/// hack on the type, which can occur at any point in the file (because these
126/// can be defined in declspecs).
128 if (diags.hasErrorOccurred())
129 return;
130
131 // Don't allow re-entrant calls to CIRGen triggered by PCH deserialization to
132 // emit deferred decls.
133 HandlingTopLevelDeclRAII handlingDecl(*this, /*EmitDeferred=*/false);
134
135 cgm->updateCompletedType(d);
136
137 // For MSVC compatibility, treat declarations of static data members with
138 // inline initializers as definitions.
139 if (astContext->getTargetInfo().getCXXABI().isMicrosoft())
140 cgm->errorNYI(d->getSourceRange(), "HandleTagDeclDefinition: MSABI");
141
142 // For OpenMP emit declare reduction functions or declare mapper, if
143 // required.
144 if (astContext->getLangOpts().OpenMP) {
145 for (Decl *member : d->decls()) {
146 if (auto *drd = dyn_cast<OMPDeclareReductionDecl>(member)) {
147 if (astContext->DeclMustBeEmitted(drd))
148 cgm->errorNYI(d->getSourceRange(),
149 "HandleTagDeclDefinition: OMPDeclareReductionDecl");
150 } else if (auto *dmd = dyn_cast<OMPDeclareMapperDecl>(member)) {
151 if (astContext->DeclMustBeEmitted(dmd))
152 cgm->errorNYI(d->getSourceRange(),
153 "HandleTagDeclDefinition: OMPDeclareMapperDecl");
154 }
155 }
156 }
157}
158
160 if (diags.hasErrorOccurred())
161 return;
162
164}
165
167 if (diags.hasErrorOccurred())
168 return;
169
170 cgm->handleCXXStaticMemberVarInstantiation(D);
171}
172
174 const OpenACCRoutineDecl *RD) {
175 llvm::StringRef mangledName = cgm->getMangledName(FD);
176 cir::FuncOp entry =
177 mlir::dyn_cast_if_present<cir::FuncOp>(cgm->getGlobalValue(mangledName));
178
179 // if this wasn't generated, don't force it to be.
180 if (!entry)
181 return;
182 cgm->emitOpenACCRoutineDecl(FD, entry, RD->getBeginLoc(), RD->clauses());
183}
184
186 if (diags.hasErrorOccurred())
187 return;
188
189 cgm->emitTentativeDefinition(d);
190}
191
193 if (diags.hasErrorOccurred())
194 return;
195
196 cgm->emitVTable(rd);
197}
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:2029
bool doesThisDeclarationHaveABody() const
Returns whether this specific declaration of the function has a body.
Definition Decl.h:2362
ArrayRef< const OpenACCClause * > clauses() const
Definition DeclOpenACC.h:62
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3761
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:4893
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.
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()