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/Arith/IR/Arith.h"
16#include "mlir/Dialect/DLTI/DLTI.h"
17#include "mlir/Dialect/LLVMIR/LLVMDialectDecl.h"
18#include "mlir/Dialect/OpenACC/OpenACC.h"
19#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
20#include "mlir/IR/MLIRContext.h"
21
22#include "clang/AST/DeclGroup.h"
29#include "llvm/IR/DataLayout.h"
30
31using namespace cir;
32using namespace clang;
33
34void CIRGenerator::anchor() {}
35
38 const CodeGenOptions &cgo)
39 : diags(diags), fs(std::move(vfs)), codeGenOpts{cgo},
40 handlingTopLevelDecls{0} {}
42 // There should normally not be any leftover inline method definitions.
43 assert(deferredInlineMemberFuncDefs.empty() || diags.hasErrorOccurred());
44}
45
47 using namespace llvm;
48
49 this->astContext = &astContext;
50
51 mlirContext = std::make_unique<mlir::MLIRContext>();
52 // MLIR multithreading stays enabled; the CIRDiagnosticHandler is only ever
53 // invoked from a single thread.
55 mlirContext->loadDialect<mlir::DLTIDialect, mlir::LLVM::LLVMDialect,
56 cir::CIRDialect>();
57 const LangOptions &langOpts = astContext.getLangOpts();
58 // OpenACC lowering emits Arith constants for bounds and clause operands.
59 if (langOpts.OpenACC)
61 ->loadDialect<mlir::acc::OpenACCDialect, mlir::arith::ArithDialect>();
62 if (langOpts.OpenMP)
63 mlirContext->getOrLoadDialect<mlir::omp::OpenMPDialect>();
64
65 cgm = std::make_unique<clang::CIRGen::CIRGenModule>(
66 *mlirContext.get(), astContext, codeGenOpts, diags);
67 mlir::ModuleOp mod = cgm->getModule();
68 llvm::DataLayout layout =
69 llvm::DataLayout(astContext.getTargetInfo().getDataLayoutString());
70 cir::setMLIRDataLayout(mod, layout,
71 astContext.getTargetInfo().getBitIntMaxAlign(),
72 astContext.getTargetInfo().getMaxVectorAlign());
73}
74
75bool CIRGenerator::verifyModule() const { return cgm->verifyModule(); }
76
77mlir::ModuleOp CIRGenerator::getModule() const { return cgm->getModule(); }
78
79const Decl *CIRGenerator::getDeclForMangledName(llvm::StringRef mangledName) {
80 GlobalDecl result;
81 if (!cgm->lookupRepresentativeDecl(mangledName, result))
82 return nullptr;
83 const Decl *decl = result.getCanonicalDecl().getDecl();
84 if (auto *fd = dyn_cast<FunctionDecl>(decl)) {
85 if (fd->hasBody(fd))
86 return fd;
87 } else if (auto *td = dyn_cast<TagDecl>(decl)) {
88 if (auto *def = td->getDefinition())
89 return def;
90 }
91 return decl;
92}
93
95 if (diags.hasUnrecoverableErrorOccurred())
96 return true;
97
98 HandlingTopLevelDeclRAII handlingDecl(*this);
99
100 for (Decl *decl : group)
101 cgm->emitTopLevelDecl(decl);
102
103 return true;
104}
105
107 // Release the Builder when there is no error.
108 if (!diags.hasErrorOccurred() && cgm)
109 cgm->release();
110
111 // If there are errors before or when releasing the cgm, reset the module to
112 // stop here before invoking the backend.
114}
115
117 if (diags.hasErrorOccurred())
118 return;
119
120 assert(d->doesThisDeclarationHaveABody());
121
122 // We may want to emit this definition. However, that decision might be
123 // based on computing the linkage, and we have to defer that in case we are
124 // inside of something that will chagne the method's final linkage, e.g.
125 // typedef struct {
126 // void bar();
127 // void foo() { bar(); }
128 // } A;
129 deferredInlineMemberFuncDefs.push_back(d);
130
131 // Provide some coverage mapping even for methods that aren't emitted.
132 // Don't do this for templated classes though, as they may not be
133 // instantiable.
135}
136
138 if (deferredInlineMemberFuncDefs.empty())
139 return;
140
141 // Emit any deferred inline method definitions. Note that more deferred
142 // methods may be added during this loop, since ASTConsumer callbacks can be
143 // invoked if AST inspection results in declarations being added. Therefore,
144 // we use an index to loop over the deferredInlineMemberFuncDefs rather than
145 // a range.
146 HandlingTopLevelDeclRAII handlingDecls(*this);
147 for (unsigned i = 0; i != deferredInlineMemberFuncDefs.size(); ++i)
148 cgm->emitTopLevelDecl(deferredInlineMemberFuncDefs[i]);
149 deferredInlineMemberFuncDefs.clear();
150}
151
152/// HandleTagDeclDefinition - This callback is invoked each time a TagDecl to
153/// (e.g. struct, union, enum, class) is completed. This allows the client to
154/// hack on the type, which can occur at any point in the file (because these
155/// can be defined in declspecs).
157 if (diags.hasErrorOccurred())
158 return;
159
160 // Don't allow re-entrant calls to CIRGen triggered by PCH deserialization to
161 // emit deferred decls.
162 HandlingTopLevelDeclRAII handlingDecl(*this, /*EmitDeferred=*/false);
163
164 cgm->updateCompletedType(d);
165
166 // For MSVC compatibility, treat declarations of static data members with
167 // inline initializers as definitions.
168 if (astContext->getTargetInfo().getCXXABI().isMicrosoft())
169 cgm->errorNYI(d->getSourceRange(), "HandleTagDeclDefinition: MSABI");
170
171 // For OpenMP emit declare reduction functions or declare mapper, if
172 // required.
173 if (astContext->getLangOpts().OpenMP) {
174 for (Decl *member : d->decls()) {
175 if (auto *drd = dyn_cast<OMPDeclareReductionDecl>(member)) {
176 if (astContext->DeclMustBeEmitted(drd))
177 cgm->errorNYI(d->getSourceRange(),
178 "HandleTagDeclDefinition: OMPDeclareReductionDecl");
179 } else if (auto *dmd = dyn_cast<OMPDeclareMapperDecl>(member)) {
180 if (astContext->DeclMustBeEmitted(dmd))
181 cgm->errorNYI(d->getSourceRange(),
182 "HandleTagDeclDefinition: OMPDeclareMapperDecl");
183 }
184 }
185 }
186}
187
189 if (diags.hasErrorOccurred())
190 return;
191
193}
194
196 if (diags.hasErrorOccurred())
197 return;
198
199 cgm->handleCXXStaticMemberVarInstantiation(D);
200}
201
203 const OpenACCRoutineDecl *RD) {
204 llvm::StringRef mangledName = cgm->getMangledName(FD);
205 cir::FuncOp entry =
206 mlir::dyn_cast_if_present<cir::FuncOp>(cgm->getGlobalValue(mangledName));
207
208 // if this wasn't generated, don't force it to be.
209 if (!entry)
210 return;
211 cgm->emitOpenACCRoutineDecl(FD, entry, RD->getBeginLoc(), RD->clauses());
212}
213
215 if (diags.hasErrorOccurred())
216 return;
217
218 cgm->emitTentativeDefinition(d);
219}
220
222 if (diags.hasErrorOccurred())
223 return;
224
225 cgm->emitVTable(rd);
226}
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.
const clang::Decl * getDeclForMangledName(llvm::StringRef mangledName)
Return the Decl that generated the given mangled name, if known.
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:245
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:2407
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
SourceLocation getBeginLoc() const LLVM_READONLY
Definition DeclBase.h:440
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:240
Represents a function declaration or definition.
Definition Decl.h:2082
bool doesThisDeclarationHaveABody() const
Returns whether this specific declaration of the function has a body.
Definition Decl.h:2482
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:60
GlobalDecl getCanonicalDecl() const
Definition GlobalDecl.h:106
const Decl * getDecl() const
Definition GlobalDecl.h:115
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
ArrayRef< const OpenACCClause * > clauses() const
Definition DeclOpenACC.h:62
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3898
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:5004
Represents a variable declaration or definition.
Definition Decl.h:934
void registerAllDialects(mlir::DialectRegistry &registry)
void setMLIRDataLayout(mlir::ModuleOp mod, const llvm::DataLayout &dl, unsigned bitIntMaxAlign, unsigned maxVectorAlign)
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()