| 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" |
| 23 | #include "clang/AST/DeclOpenACC.h" |
| 24 | #include "clang/AST/GlobalDecl.h" |
| 25 | #include "clang/CIR/CIRDataLayoutSpec.h" |
| 26 | #include "clang/CIR/CIRGenerator.h" |
| 27 | #include "clang/CIR/InitAllDialects.h" |
| 28 | #include "clang/CIR/MissingFeatures.h" |
| 29 | #include "llvm/IR/DataLayout.h" |
| 30 | |
| 31 | using namespace cir; |
| 32 | using namespace clang; |
| 33 | |
| 34 | void CIRGenerator::anchor() {} |
| 35 | |
| 36 | CIRGenerator::CIRGenerator(clang::DiagnosticsEngine &diags, |
| 37 | llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs, |
| 38 | const CodeGenOptions &cgo) |
| 39 | : diags(diags), fs(std::move(vfs)), codeGenOpts{cgo}, |
| 40 | handlingTopLevelDecls{0} {} |
| 41 | CIRGenerator::~CIRGenerator() { |
| 42 | // There should normally not be any leftover inline method definitions. |
| 43 | assert(deferredInlineMemberFuncDefs.empty() || diags.hasErrorOccurred()); |
| 44 | } |
| 45 | |
| 46 | void CIRGenerator::Initialize(ASTContext &astContext) { |
| 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. |
| 54 | cir::registerAllDialects(context&: *mlirContext); |
| 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) |
| 60 | mlirContext |
| 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 | args&: *mlirContext.get(), args&: astContext, args: codeGenOpts, args&: diags); |
| 67 | mlir::ModuleOp mod = cgm->getModule(); |
| 68 | llvm::DataLayout layout = |
| 69 | llvm::DataLayout(astContext.getTargetInfo().getDataLayoutString()); |
| 70 | cir::setMLIRDataLayout(mod, dl: layout, |
| 71 | bitIntMaxAlign: astContext.getTargetInfo().getBitIntMaxAlign(), |
| 72 | maxVectorAlign: astContext.getTargetInfo().getMaxVectorAlign()); |
| 73 | } |
| 74 | |
| 75 | bool CIRGenerator::verifyModule() const { return cgm->verifyModule(); } |
| 76 | |
| 77 | mlir::ModuleOp CIRGenerator::getModule() const { return cgm->getModule(); } |
| 78 | |
| 79 | const Decl *CIRGenerator::getDeclForMangledName(llvm::StringRef mangledName) { |
| 80 | GlobalDecl result; |
| 81 | if (!cgm->lookupRepresentativeDecl(mangledName, gd&: result)) |
| 82 | return nullptr; |
| 83 | const Decl *decl = result.getCanonicalDecl().getDecl(); |
| 84 | if (auto *fd = dyn_cast<FunctionDecl>(Val: decl)) { |
| 85 | if (fd->hasBody(Definition&: fd)) |
| 86 | return fd; |
| 87 | } else if (auto *td = dyn_cast<TagDecl>(Val: decl)) { |
| 88 | if (auto *def = td->getDefinition()) |
| 89 | return def; |
| 90 | } |
| 91 | return decl; |
| 92 | } |
| 93 | |
| 94 | bool CIRGenerator::HandleTopLevelDecl(DeclGroupRef group) { |
| 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 | |
| 106 | void CIRGenerator::HandleTranslationUnit(ASTContext &astContext) { |
| 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. |
| 113 | assert(!cir::MissingFeatures::cleanupAfterErrorDiags()); |
| 114 | } |
| 115 | |
| 116 | void CIRGenerator::HandleInlineFunctionDefinition(FunctionDecl *d) { |
| 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(Elt: 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. |
| 134 | assert(!cir::MissingFeatures::coverageMapping()); |
| 135 | } |
| 136 | |
| 137 | void CIRGenerator::emitDeferredDecls() { |
| 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(decl: 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). |
| 156 | void CIRGenerator::HandleTagDeclDefinition(TagDecl *d) { |
| 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(td: 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>(Val: member)) { |
| 176 | if (astContext->DeclMustBeEmitted(D: drd)) |
| 177 | cgm->errorNYI(d->getSourceRange(), |
| 178 | "HandleTagDeclDefinition: OMPDeclareReductionDecl" ); |
| 179 | } else if (auto *dmd = dyn_cast<OMPDeclareMapperDecl>(Val: member)) { |
| 180 | if (astContext->DeclMustBeEmitted(D: dmd)) |
| 181 | cgm->errorNYI(d->getSourceRange(), |
| 182 | "HandleTagDeclDefinition: OMPDeclareMapperDecl" ); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void CIRGenerator::HandleTagDeclRequiredDefinition(const TagDecl *D) { |
| 189 | if (diags.hasErrorOccurred()) |
| 190 | return; |
| 191 | |
| 192 | assert(!cir::MissingFeatures::generateDebugInfo()); |
| 193 | } |
| 194 | |
| 195 | void CIRGenerator::HandleCXXStaticMemberVarInstantiation(VarDecl *D) { |
| 196 | if (diags.hasErrorOccurred()) |
| 197 | return; |
| 198 | |
| 199 | cgm->handleCXXStaticMemberVarInstantiation(vd: D); |
| 200 | } |
| 201 | |
| 202 | void CIRGenerator::HandleOpenACCRoutineReference(const FunctionDecl *FD, |
| 203 | const OpenACCRoutineDecl *RD) { |
| 204 | llvm::StringRef mangledName = cgm->getMangledName(gd: FD); |
| 205 | cir::FuncOp entry = |
| 206 | mlir::dyn_cast_if_present<cir::FuncOp>(Val: cgm->getGlobalValue(ref: mangledName)); |
| 207 | |
| 208 | // if this wasn't generated, don't force it to be. |
| 209 | if (!entry) |
| 210 | return; |
| 211 | cgm->emitOpenACCRoutineDecl(funcDecl: FD, func: entry, pragmaLoc: RD->getBeginLoc(), clauses: RD->clauses()); |
| 212 | } |
| 213 | |
| 214 | void CIRGenerator::CompleteTentativeDefinition(VarDecl *d) { |
| 215 | if (diags.hasErrorOccurred()) |
| 216 | return; |
| 217 | |
| 218 | cgm->emitTentativeDefinition(d); |
| 219 | } |
| 220 | |
| 221 | void CIRGenerator::HandleVTable(CXXRecordDecl *rd) { |
| 222 | if (diags.hasErrorOccurred()) |
| 223 | return; |
| 224 | |
| 225 | cgm->emitVTable(rd); |
| 226 | } |
| 227 | |