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