| 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/Dialect/OpenMP/OpenMPDialect.h" |
| 17 |
#include "mlir/IR/MLIRContext.h" |
| 18 |
#include "mlir/Target/LLVMIR/Import.h" |
| 19 |
|
| 20 |
#include "clang/AST/DeclGroup.h" |
| 21 |
#include "clang/CIR/CIRGenerator.h" |
| 22 |
#include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 23 |
#include "clang/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.h" |
| 24 |
#include "llvm/IR/DataLayout.h" |
| 25 |
|
| 26 |
using namespace cir; |
| 27 |
using namespace clang; |
| 28 |
|
| 29 |
void CIRGenerator::anchor() {} |
| 30 |
|
| 31 |
CIRGenerator::CIRGenerator(clang::DiagnosticsEngine &diags, |
| 32 |
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs, |
| 33 |
const CodeGenOptions &cgo) |
| 34 |
: diags(diags), fs(std::move(vfs)), codeGenOpts{cgo}, |
| 35 |
handlingTopLevelDecls{0} {} |
| 36 |
CIRGenerator::~CIRGenerator() { |
| 37 |
// There should normally not be any leftover inline method definitions. |
| 38 |
assert(deferredInlineMemberFuncDefs.empty() || diags.hasErrorOccurred()); |
| 39 |
} |
| 40 |
|
| 41 |
static void setMLIRDataLayout(mlir::ModuleOp &mod, const llvm::DataLayout &dl) { |
| 42 |
mlir::MLIRContext *mlirContext = mod.getContext(); |
| 43 |
mlir::DataLayoutSpecInterface dlSpec = |
| 44 |
mlir::translateDataLayout(dl, mlirContext); |
| 45 |
mod->setAttr(mlir::DLTIDialect::kDataLayoutAttrName, dlSpec); |
| 46 |
} |
| 47 |
|
| 48 |
void CIRGenerator::Initialize(ASTContext &astContext) { |
| 49 |
using namespace llvm; |
| 50 |
|
| 51 |
this->astContext = &astContext; |
| 52 |
|
| 53 |
mlirContext = std::make_unique<mlir::MLIRContext>(); |
| 54 |
mlirContext->loadDialect<mlir::DLTIDialect>(); |
| 55 |
mlirContext->loadDialect<cir::CIRDialect>(); |
| 56 |
mlirContext->getOrLoadDialect<mlir::acc::OpenACCDialect>(); |
| 57 |
mlirContext->getOrLoadDialect<mlir::omp::OpenMPDialect>(); |
| 58 |
|
| 59 |
// Register extensions to integrate CIR types with OpenACC. |
| 60 |
mlir::DialectRegistry registry; |
| 61 |
cir::acc::registerOpenACCExtensions(registry); |
| 62 |
mlirContext->appendDialectRegistry(registry); |
| 63 |
|
| 64 |
cgm = std::make_unique<clang::CIRGen::CIRGenModule>( |
| 65 |
*mlirContext.get(), astContext, codeGenOpts, diags); |
| 66 |
mlir::ModuleOp mod = cgm->getModule(); |
| 67 |
llvm::DataLayout layout = |
| 68 |
llvm::DataLayout(astContext.getTargetInfo().getDataLayoutString()); |
| 69 |
setMLIRDataLayout(mod, layout); |
| 70 |
} |
| 71 |
|
| 72 |
bool CIRGenerator::verifyModule() const { return cgm->verifyModule(); } |
| 73 |
|
| 74 |
mlir::ModuleOp CIRGenerator::getModule() const { return cgm->getModule(); } |
| 75 |
|
| 76 |
bool CIRGenerator::HandleTopLevelDecl(DeclGroupRef group) { |
| 77 |
if (diags.hasUnrecoverableErrorOccurred()) |
| 78 |
return true; |
| 79 |
|
| 80 |
HandlingTopLevelDeclRAII handlingDecl(*this); |
| 81 |
|
| 82 |
for (Decl *decl : group) |
| 83 |
cgm->emitTopLevelDecl(decl); |
| 84 |
|
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
void CIRGenerator::HandleTranslationUnit(ASTContext &astContext) { |
| 89 |
// Release the Builder when there is no error. |
| 90 |
if (!diags.hasErrorOccurred() && cgm) |
| 91 |
cgm->release(); |
| 92 |
|
| 93 |
// If there are errors before or when releasing the cgm, reset the module to |
| 94 |
// stop here before invoking the backend. |
| 95 |
assert(!cir::MissingFeatures::cleanupAfterErrorDiags()); |
| 96 |
} |
| 97 |
|
| 98 |
void CIRGenerator::HandleInlineFunctionDefinition(FunctionDecl *d) { |
| 99 |
if (diags.hasErrorOccurred()) |
| 100 |
return; |
| 101 |
|
| 102 |
assert(d->doesThisDeclarationHaveABody()); |
| 103 |
|
| 104 |
// We may want to emit this definition. However, that decision might be |
| 105 |
// based on computing the linkage, and we have to defer that in case we are |
| 106 |
// inside of something that will chagne the method's final linkage, e.g. |
| 107 |
// typedef struct { |
| 108 |
// void bar(); |
| 109 |
// void foo() { bar(); } |
| 110 |
// } A; |
| 111 |
deferredInlineMemberFuncDefs.push_back(d); |
| 112 |
|
| 113 |
// Provide some coverage mapping even for methods that aren't emitted. |
| 114 |
// Don't do this for templated classes though, as they may not be |
| 115 |
// instantiable. |
| 116 |
assert(!cir::MissingFeatures::coverageMapping()); |
| 117 |
} |
| 118 |
|
| 119 |
void CIRGenerator::emitDeferredDecls() { |
| 120 |
if (deferredInlineMemberFuncDefs.empty()) |
| 121 |
return; |
| 122 |
|
| 123 |
// Emit any deferred inline method definitions. Note that more deferred |
| 124 |
// methods may be added during this loop, since ASTConsumer callbacks can be |
| 125 |
// invoked if AST inspection results in declarations being added. Therefore, |
| 126 |
// we use an index to loop over the deferredInlineMemberFuncDefs rather than |
| 127 |
// a range. |
| 128 |
HandlingTopLevelDeclRAII handlingDecls(*this); |
| 129 |
for (unsigned i = 0; i != deferredInlineMemberFuncDefs.size(); ++i) |
| 130 |
cgm->emitTopLevelDecl(deferredInlineMemberFuncDefs[i]); |
| 131 |
deferredInlineMemberFuncDefs.clear(); |
| 132 |
} |
| 133 |
|
| 134 |
/// HandleTagDeclDefinition - This callback is invoked each time a TagDecl to |
| 135 |
/// (e.g. struct, union, enum, class) is completed. This allows the client to |
| 136 |
/// hack on the type, which can occur at any point in the file (because these |
| 137 |
/// can be defined in declspecs). |
| 138 |
void CIRGenerator::HandleTagDeclDefinition(TagDecl *d) { |
| 139 |
if (diags.hasErrorOccurred()) |
| 140 |
return; |
| 141 |
|
| 142 |
// Don't allow re-entrant calls to CIRGen triggered by PCH deserialization to |
| 143 |
// emit deferred decls. |
| 144 |
HandlingTopLevelDeclRAII handlingDecl(*this, /*EmitDeferred=*/false); |
| 145 |
|
| 146 |
cgm->updateCompletedType(d); |
| 147 |
|
| 148 |
// For MSVC compatibility, treat declarations of static data members with |
| 149 |
// inline initializers as definitions. |
| 150 |
if (astContext->getTargetInfo().getCXXABI().isMicrosoft()) |
| 151 |
cgm->errorNYI(d->getSourceRange(), "HandleTagDeclDefinition: MSABI"); |
| 152 |
// For OpenMP emit declare reduction functions, if required. |
| 153 |
if (astContext->getLangOpts().OpenMP) |
| 154 |
cgm->errorNYI(d->getSourceRange(), "HandleTagDeclDefinition: OpenMP"); |
| 155 |
} |
| 156 |
|
| 157 |
void CIRGenerator::HandleTagDeclRequiredDefinition(const TagDecl *D) { |
| 158 |
if (diags.hasErrorOccurred()) |
| 159 |
return; |
| 160 |
|
| 161 |
assert(!cir::MissingFeatures::generateDebugInfo()); |
| 162 |
} |
| 163 |
|
| 164 |
void CIRGenerator::HandleCXXStaticMemberVarInstantiation(VarDecl *D) { |
| 165 |
if (diags.hasErrorOccurred()) |
| 166 |
return; |
| 167 |
|
| 168 |
cgm->handleCXXStaticMemberVarInstantiation(D); |
| 169 |
} |
| 170 |
|
| 171 |
void CIRGenerator::HandleOpenACCRoutineReference(const FunctionDecl *FD, |
| 172 |
const OpenACCRoutineDecl *RD) { |
| 173 |
llvm::StringRef mangledName = cgm->getMangledName(FD); |
| 174 |
cir::FuncOp entry = |
| 175 |
mlir::dyn_cast_if_present<cir::FuncOp>(cgm->getGlobalValue(mangledName)); |
| 176 |
|
| 177 |
// if this wasn't generated, don't force it to be. |
| 178 |
if (!entry) |
| 179 |
return; |
| 180 |
cgm->emitOpenACCRoutineDecl(FD, entry, RD->getBeginLoc(), RD->clauses()); |
| 181 |
} |
| 182 |
|
| 183 |
void CIRGenerator::CompleteTentativeDefinition(VarDecl *d) { |
| 184 |
if (diags.hasErrorOccurred()) |
| 185 |
return; |
| 186 |
|
| 187 |
cgm->emitTentativeDefinition(d); |
| 188 |
} |
| 189 |
|
| 190 |
void CIRGenerator::HandleVTable(CXXRecordDecl *rd) { |
| 191 |
if (diags.hasErrorOccurred()) |
| 192 |
return; |
| 193 |
|
| 194 |
cgm->emitVTable(rd); |
| 195 |
} |