clang: lib/CIR/CodeGen/CIRGenModule.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
14
20
21#include "mlir/IR/BuiltinOps.h"
22#include "mlir/IR/Location.h"
23#include "mlir/IR/MLIRContext.h"
24
25using namespace clang;
27
28CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
32 : builder(mlirContext, *this), astContext(astContext),
33 langOpts(astContext.getLangOpts()),
34 theModule{mlir::ModuleOp::create(mlir::UnknownLoc::get(&mlirContext))},
35 diags(diags), target(astContext.getTargetInfo()), genTypes(*this) {
36
37
55}
56
58 assert(cLoc.isValid() && "expected valid source location");
62 return mlir::FileLineColLoc::get(builder.getStringAttr(filename),
64}
65
67 assert(cRange.isValid() && "expected a valid source range");
70 mlir::Attribute metadata;
71 return mlir::FusedLoc::get({begin, end}, metadata, builder.getContext());
72}
73
75 const auto *global = cast(gd.getDecl());
76
77 if (const auto *fd = dyn_cast(global)) {
78
79
80 if (fd->hasAttr())
81 errorNYI(fd->getSourceRange(), "deferredAnnotations");
82 if (!fd->doesThisDeclarationHaveABody()) {
83 if (!fd->doesDeclarationForceExternallyVisibleDefinition())
84 return;
85
86 errorNYI(fd->getSourceRange(),
87 "function declaration that forces code gen");
88 return;
89 }
90 } else {
91 assert(cast(global)->isFileVarDecl() &&
92 "Cannot emit local var decl as global");
93 }
94
95
97}
98
100 mlir::Operation *op) {
101 auto const *funcDecl = cast(gd.getDecl());
103 auto funcOp = builder.createcir::FuncOp(
104 getLoc(funcDecl->getSourceRange()), identifier->getName());
105 theModule.push_back(funcOp);
106 } else {
107 errorNYI(funcDecl->getSourceRange().getBegin(),
108 "function definition with a non-identifier for a name");
109 }
110}
111
113 bool isTentative) {
117 identifier->getName(), type);
118
119
120
121
124 if (initExpr) {
125 mlir::Attribute initializer;
126 if (APValue *value = initDecl->evaluateValue()) {
127 switch (value->getKind()) {
129 initializer = builder.getAttrcir::IntAttr(type, value->getInt());
130 break;
131 }
133 initializer = builder.getAttrcir::FPAttr(type, value->getFloat());
134 break;
135 }
137 if (value->getLValueBase()) {
139 "non-null pointer initialization");
140 } else {
141 if (auto ptrType = mlir::dyn_castcir::PointerType(type)) {
143 ptrType, value->getLValueOffset().getQuantity());
144 } else {
145 llvm_unreachable(
146 "non-pointer variable initialized with a pointer");
147 }
148 }
149 break;
150 }
151 default:
153 break;
154 }
155 } else {
157 }
158 varOp.setInitialValueAttr(initializer);
159 }
160 theModule.push_back(varOp);
161 } else {
163 "variable definition with a non-identifier for a name");
164 }
165}
166
168 mlir::Operation *op) {
169 const auto *decl = cast(gd.getDecl());
170 if (const auto *fd = dyn_cast(decl)) {
171
172
173
174 if (const auto *method = dyn_cast(decl)) {
175
176
177 (void)method;
178 errorNYI(method->getSourceRange(), "member function");
179 return;
180 }
181
182 if (fd->isMultiVersion())
183 errorNYI(fd->getSourceRange(), "multiversion functions");
185 return;
186 }
187
188 if (const auto *vd = dyn_cast(decl))
190
191 llvm_unreachable("Invalid argument to CIRGenModule::emitGlobalDefinition");
192}
193
194
196
197
198 if (decl->isTemplated())
199 return;
200
201 switch (decl->getKind()) {
202 default:
203 errorNYI(decl->getBeginLoc(), "declaration of kind",
204 decl->getDeclKindName());
205 break;
206
207 case Decl::Function: {
208 auto *fd = cast(decl);
209
210 if (!fd->isConsteval())
212 break;
213 }
214
215 case Decl::Var: {
216 auto *vd = cast(decl);
218 break;
219 }
220 }
221}
222
224 llvm::StringRef feature) {
227 return diags.Report(loc, diagID) << feature;
228}
229
231 llvm::StringRef feature) {
233}
Defines the clang::ASTContext interface.
Defines the SourceManager interface.
mlir::TypedAttr getConstPtrAttr(mlir::Type type, int64_t value)
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
SourceManager & getSourceManager()
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
void emitTopLevelDecl(clang::Decl *decl)
void emitGlobalDefinition(clang::GlobalDecl gd, mlir::Operation *op=nullptr)
void emitGlobalVarDefinition(const clang::VarDecl *vd, bool isTentative=false)
mlir::Location getLoc(clang::SourceLocation cLoc)
Helpers to convert the presumed location of Clang's SourceLocation to an MLIR Location.
mlir::MLIRContext & getMLIRContext()
void emitGlobal(clang::GlobalDecl gd)
Emit code for a single global function or variable declaration.
void emitGlobalFunctionDefinition(clang::GlobalDecl gd, mlir::Operation *op)
mlir::Type convertType(clang::QualType type)
Convert a Clang type into a mlir::Type.
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.
A little helper class used to produce diagnostics.
Concrete class used by the front-end to report problems and issues.
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
unsigned getCustomDiagID(Level L, const char(&FormatString)[N])
Return an ID for a diagnostic with the specified format string and level.
This represents one expression.
GlobalDecl - represents a global declaration.
const Decl * getDecl() const
One of these records is kept for each identifier that is lexed.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Represents an unpacked "presumed" location which can be presented to the user.
unsigned getColumn() const
Return the presumed column number of this location.
const char * getFilename() const
Return the presumed filename of this location.
unsigned getLine() const
Return the presumed line number of this location.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Represents a variable declaration or definition.
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
The JSON file list parser is used to communicate input to InstallAPI.