clang 22.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/IR/MLIRContext.h"
17#include "mlir/Target/LLVMIR/Import.h"
18
19#include "clang/AST/DeclGroup.h"
23#include "llvm/IR/DataLayout.h"
24
25using namespace cir;
26using namespace clang;
27
28void CIRGenerator::anchor() {}
29
32 const CodeGenOptions &cgo)
33 : diags(diags), fs(std::move(vfs)), codeGenOpts{cgo},
34 handlingTopLevelDecls{0} {}
36 // There should normally not be any leftover inline method definitions.
37 assert(deferredInlineMemberFuncDefs.empty() || diags.hasErrorOccurred());
38}
39
40static void setMLIRDataLayout(mlir::ModuleOp &mod, const llvm::DataLayout &dl) {
41 mlir::MLIRContext *mlirContext = mod.getContext();
42 mlir::DataLayoutSpecInterface dlSpec =
43 mlir::translateDataLayout(dl, mlirContext);
44 mod->setAttr(mlir::DLTIDialect::kDataLayoutAttrName, dlSpec);
45}
46
48 using namespace llvm;
49
50 this->astContext = &astContext;
51
52 mlirContext = std::make_unique<mlir::MLIRContext>();
53 mlirContext->loadDialect<mlir::DLTIDialect>();
54 mlirContext->loadDialect<cir::CIRDialect>();
55 mlirContext->getOrLoadDialect<mlir::acc::OpenACCDialect>();
56
57 // Register extensions to integrate CIR types with OpenACC.
58 mlir::DialectRegistry registry;
60 mlirContext->appendDialectRegistry(registry);
61
62 cgm = std::make_unique<clang::CIRGen::CIRGenModule>(
63 *mlirContext.get(), astContext, codeGenOpts, diags);
64 mlir::ModuleOp mod = cgm->getModule();
65 llvm::DataLayout layout =
66 llvm::DataLayout(astContext.getTargetInfo().getDataLayoutString());
67 setMLIRDataLayout(mod, layout);
68}
69
70bool CIRGenerator::verifyModule() const { return cgm->verifyModule(); }
71
72mlir::ModuleOp CIRGenerator::getModule() const { return cgm->getModule(); }
73
76 return true;
77
78 HandlingTopLevelDeclRAII handlingDecl(*this);
79
80 for (Decl *decl : group)
81 cgm->emitTopLevelDecl(decl);
82
83 return true;
84}
85
87 // Release the Builder when there is no error.
88 if (!diags.hasErrorOccurred() && cgm)
89 cgm->release();
90
91 // If there are errors before or when releasing the cgm, reset the module to
92 // stop here before invoking the backend.
94}
95
97 if (diags.hasErrorOccurred())
98 return;
99
100 assert(d->doesThisDeclarationHaveABody());
101
102 // We may want to emit this definition. However, that decision might be
103 // based on computing the linkage, and we have to defer that in case we are
104 // inside of something that will chagne the method's final linkage, e.g.
105 // typedef struct {
106 // void bar();
107 // void foo() { bar(); }
108 // } A;
109 deferredInlineMemberFuncDefs.push_back(d);
110
111 // Provide some coverage mapping even for methods that aren't emitted.
112 // Don't do this for templated classes though, as they may not be
113 // instantiable.
115}
116
118 if (deferredInlineMemberFuncDefs.empty())
119 return;
120
121 // Emit any deferred inline method definitions. Note that more deferred
122 // methods may be added during this loop, since ASTConsumer callbacks can be
123 // invoked if AST inspection results in declarations being added. Therefore,
124 // we use an index to loop over the deferredInlineMemberFuncDefs rather than
125 // a range.
126 HandlingTopLevelDeclRAII handlingDecls(*this);
127 for (unsigned i = 0; i != deferredInlineMemberFuncDefs.size(); ++i)
128 cgm->emitTopLevelDecl(deferredInlineMemberFuncDefs[i]);
129 deferredInlineMemberFuncDefs.clear();
130}
131
132/// HandleTagDeclDefinition - This callback is invoked each time a TagDecl to
133/// (e.g. struct, union, enum, class) is completed. This allows the client to
134/// hack on the type, which can occur at any point in the file (because these
135/// can be defined in declspecs).
137 if (diags.hasErrorOccurred())
138 return;
139
140 // Don't allow re-entrant calls to CIRGen triggered by PCH deserialization to
141 // emit deferred decls.
142 HandlingTopLevelDeclRAII handlingDecl(*this, /*EmitDeferred=*/false);
143
144 cgm->updateCompletedType(d);
145
146 // For MSVC compatibility, treat declarations of static data members with
147 // inline initializers as definitions.
148 if (astContext->getTargetInfo().getCXXABI().isMicrosoft())
149 cgm->errorNYI(d->getSourceRange(), "HandleTagDeclDefinition: MSABI");
150 // For OpenMP emit declare reduction functions, if required.
151 if (astContext->getLangOpts().OpenMP)
152 cgm->errorNYI(d->getSourceRange(), "HandleTagDeclDefinition: OpenMP");
153}
154
156 if (diags.hasErrorOccurred())
157 return;
158
160}
161
163 if (diags.hasErrorOccurred())
164 return;
165
166 cgm->handleCXXStaticMemberVarInstantiation(D);
167}
168
170 if (diags.hasErrorOccurred())
171 return;
172
173 cgm->emitTentativeDefinition(d);
174}
175
177 if (diags.hasErrorOccurred())
178 return;
179
180 cgm->emitVTable(rd);
181}
static void setMLIRDataLayout(mlir::ModuleOp &mod, const llvm::DataLayout &dl)
const Decl * D
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
Definition: CIRGenerator.h:67
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 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
Definition: CIRGenerator.h:66
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:188
const LangOptions & getLangOpts() const
Definition: ASTContext.h:894
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:859
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 - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:231
bool hasErrorOccurred() const
Definition: Diagnostic.h:871
bool hasUnrecoverableErrorOccurred() const
Determine whether any kind of unrecoverable error has occurred.
Definition: Diagnostic.h:881
Represents a function declaration or definition.
Definition: Decl.h:1999
bool doesThisDeclarationHaveABody() const
Returns whether this specific declaration of the function has a body.
Definition: Decl.h:2325
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3710
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition: Decl.cpp:4830
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
Definition: TargetCXXABI.h:136
const char * getDataLayoutString() const
Definition: TargetInfo.h:1297
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:1360
Represents a variable declaration or definition.
Definition: Decl.h:925
void registerOpenACCExtensions(mlir::DialectRegistry &registry)
Definition: ABIArgInfo.h:22
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()