[clang] [CIR] Call code gen; create empty cir.func op (PR #113483)
David Olsen via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 1 11:25:42 PDT 2024
================
@@ -24,9 +27,135 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &context,
clang::ASTContext &astctx,
const clang::CodeGenOptions &cgo,
DiagnosticsEngine &diags)
- : astCtx(astctx), langOpts(astctx.getLangOpts()),
- theModule{mlir::ModuleOp::create(mlir::UnknownLoc())},
- target(astCtx.getTargetInfo()) {}
+ : builder(&context), astCtx(astctx), langOpts(astctx.getLangOpts()),
+ theModule{mlir::ModuleOp::create(mlir::UnknownLoc::get(&context))},
+ diags(diags), target(astCtx.getTargetInfo()) {}
+
+mlir::Location CIRGenModule::getLoc(SourceLocation cLoc) {
+ assert(cLoc.isValid() && "expected valid source location");
+ const SourceManager &sm = astCtx.getSourceManager();
+ PresumedLoc pLoc = sm.getPresumedLoc(cLoc);
+ StringRef filename = pLoc.getFilename();
+ return mlir::FileLineColLoc::get(builder.getStringAttr(filename),
+ pLoc.getLine(), pLoc.getColumn());
+}
+
+mlir::Location CIRGenModule::getLoc(SourceRange cRange) {
+ assert(cRange.isValid() && "expected a valid source range");
+ mlir::Location begin = getLoc(cRange.getBegin());
+ mlir::Location end = getLoc(cRange.getEnd());
+ mlir::Attribute metadata;
+ return mlir::FusedLoc::get({begin, end}, metadata, builder.getContext());
+}
+
+void CIRGenModule::buildGlobal(clang::GlobalDecl gd) {
+ const auto *global = cast<ValueDecl>(gd.getDecl());
+
+ if (const auto *fd = dyn_cast<FunctionDecl>(global)) {
+ // Update deferred annotations with the latest declaration if the function
+ // was already used or defined.
+ if (fd->hasAttr<AnnotateAttr>()) {
+ errorNYI(fd->getSourceRange(), "defferedAnnotations");
+ }
+ if (!fd->doesThisDeclarationHaveABody()) {
+ if (!fd->doesDeclarationForceExternallyVisibleDefinition())
+ return;
+
+ errorNYI(fd->getSourceRange(),
+ "function declaration that forces code gen");
+ return;
+ }
+ } else {
+ errorNYI(global->getSourceRange(), "global variable declaration");
+ }
+
+ // TODO(CIR): Defer emitting some global definitions until later
+ buildGlobalDefinition(gd);
+}
+
+void CIRGenModule::buildGlobalFunctionDefinition(clang::GlobalDecl gd,
+ mlir::Operation *op) {
+ auto const *funcDecl = cast<FunctionDecl>(gd.getDecl());
+ auto funcOp = builder.create<mlir::cir::FuncOp>(
+ getLoc(funcDecl->getSourceRange()), funcDecl->getIdentifier()->getName());
----------------
dkolsen-pgi wrote:
This is placeholder code, as Erich said the bare minimum to not choke on function definitions. There are large amounts of code in the ClangIR incubator that will be upstreamed later to handle function definitions and their names. Ignoring the alias issue for the moment, the name of the `cir.func` op should be the mangled name of the function, not the simple name. But the necessary ABI classes that do the mangling haven't been upstreamed yet.
If there are existing Clang tests for edge cases (such as the `alias` attribute), then the feature won't be forgotten during ClangIR development. Eventually every Clang test will have to be handled correctly by the ClangIR compilation path, so missing features will be noticed. I strongly object to expecting me to create a stub test in the LLVM project; that would be a lot of effort for no real benefit. I appreciate that that is no longer the request. I would prefer to not worry about things like this at all. But a compromise I would be fine with is for the PR owner to check that either (1) the feature is implemented and tested in the ClangIR incubator project, or (2) there is an issue to implement the feature in the ClangIR incubator project.
https://github.com/llvm/llvm-project/pull/113483
More information about the cfe-commits
mailing list