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