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