| 1 |
//===----------------------------------------------------------------------===// |
| 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 contains code dealing with C++ code generation. |
| 10 |
// |
| 11 |
//===----------------------------------------------------------------------===// |
| 12 |
|
| 13 |
#include "CIRGenCXXABI.h" |
| 14 |
#include "CIRGenFunction.h" |
| 15 |
#include "CIRGenModule.h" |
| 16 |
|
| 17 |
#include "clang/AST/GlobalDecl.h" |
| 18 |
#include "clang/CIR/MissingFeatures.h" |
| 19 |
#include "llvm/Support/SaveAndRestore.h" |
| 20 |
|
| 21 |
using namespace clang; |
| 22 |
using namespace clang::CIRGen; |
| 23 |
|
| 24 |
/// Emit code to cause the variable at the given address to be considered as |
| 25 |
/// constant from this point onwards. |
| 26 |
static void emitDeclInvariant(CIRGenFunction &cgf, const VarDecl *d) { |
| 27 |
mlir::Value addr = cgf.cgm.getAddrOfGlobalVar(d); |
| 28 |
cgf.emitInvariantStart(cgf.getContext().getTypeSizeInChars(d->getType()), |
| 29 |
addr, cgf.getLoc(d->getSourceRange())); |
| 30 |
} |
| 31 |
|
| 32 |
void CIRGenFunction::emitInvariantStart(CharUnits size, mlir::Value addr, |
| 33 |
mlir::Location loc) { |
| 34 |
// Do not emit the intrinsic if we're not optimizing. |
| 35 |
if (!cgm.getCodeGenOpts().OptimizationLevel) |
| 36 |
return; |
| 37 |
|
| 38 |
CIRGenBuilderTy &builder = getBuilder(); |
| 39 |
|
| 40 |
// Create the size constant as i64 |
| 41 |
uint64_t width = size.getQuantity(); |
| 42 |
mlir::Value sizeValue = builder.getConstInt(loc, builder.getSInt64Ty(), |
| 43 |
static_cast<int64_t>(width)); |
| 44 |
|
| 45 |
// Create the intrinsic call. The llvm.invariant.start intrinsic returns a |
| 46 |
// token, but we don't need to capture it. The address space will be |
| 47 |
// automatically handled when the intrinsic is lowered to LLVM IR. |
| 48 |
cir::LLVMIntrinsicCallOp::create( |
| 49 |
builder, loc, builder.getStringAttr("invariant.start"), addr.getType(), |
| 50 |
mlir::ValueRange{sizeValue, addr}); |
| 51 |
} |
| 52 |
|
| 53 |
static void emitDeclInit(CIRGenFunction &cgf, const VarDecl *varDecl, |
| 54 |
cir::GlobalOp globalOp) { |
| 55 |
assert((varDecl->hasGlobalStorage() || |
| 56 |
(varDecl->hasLocalStorage() && |
| 57 |
cgf.getContext().getLangOpts().OpenCLCPlusPlus)) && |
| 58 |
"VarDecl must have global or local (in the case of OpenCL) storage!"); |
| 59 |
assert(!varDecl->getType()->isReferenceType() && |
| 60 |
"Should not call emitDeclInit on a reference!"); |
| 61 |
|
| 62 |
CIRGenBuilderTy &builder = cgf.getBuilder(); |
| 63 |
|
| 64 |
// Set up the ctor region. |
| 65 |
mlir::OpBuilder::InsertionGuard guard(builder); |
| 66 |
mlir::Block *block = builder.createBlock(&globalOp.getCtorRegion()); |
| 67 |
CIRGenFunction::LexicalScope lexScope{cgf, globalOp.getLoc(), |
| 68 |
builder.getInsertionBlock()}; |
| 69 |
lexScope.setAsGlobalInit(); |
| 70 |
builder.setInsertionPointToStart(block); |
| 71 |
|
| 72 |
Address declAddr(cgf.cgm.getAddrOfGlobalVar(varDecl), |
| 73 |
cgf.cgm.getASTContext().getDeclAlign(varDecl)); |
| 74 |
|
| 75 |
QualType type = varDecl->getType(); |
| 76 |
LValue lv = cgf.makeAddrLValue(declAddr, type); |
| 77 |
|
| 78 |
const Expr *init = varDecl->getInit(); |
| 79 |
switch (CIRGenFunction::getEvaluationKind(type)) { |
| 80 |
case cir::TEK_Scalar: |
| 81 |
assert(!cir::MissingFeatures::objCGC()); |
| 82 |
cgf.emitScalarInit(init, cgf.getLoc(varDecl->getLocation()), lv, false); |
| 83 |
break; |
| 84 |
case cir::TEK_Complex: |
| 85 |
cgf.emitComplexExprIntoLValue(init, lv, /*isInit=*/true); |
| 86 |
break; |
| 87 |
case cir::TEK_Aggregate: |
| 88 |
assert(!cir::MissingFeatures::aggValueSlotGC()); |
| 89 |
cgf.emitAggExpr(init, |
| 90 |
AggValueSlot::forLValue(lv, AggValueSlot::IsDestructed, |
| 91 |
AggValueSlot::IsNotAliased, |
| 92 |
AggValueSlot::DoesNotOverlap)); |
| 93 |
break; |
| 94 |
} |
| 95 |
|
| 96 |
// Finish the ctor region. |
| 97 |
builder.setInsertionPointToEnd(block); |
| 98 |
cir::YieldOp::create(builder, globalOp.getLoc()); |
| 99 |
} |
| 100 |
|
| 101 |
static void emitDeclDestroy(CIRGenFunction &cgf, const VarDecl *vd, |
| 102 |
cir::GlobalOp addr) { |
| 103 |
// Honor __attribute__((no_destroy)) and bail instead of attempting |
| 104 |
// to emit a reference to a possibly nonexistent destructor, which |
| 105 |
// in turn can cause a crash. This will result in a global constructor |
| 106 |
// that isn't balanced out by a destructor call as intended by the |
| 107 |
// attribute. This also checks for -fno-c++-static-destructors and |
| 108 |
// bails even if the attribute is not present. |
| 109 |
QualType::DestructionKind dtorKind = vd->needsDestruction(cgf.getContext()); |
| 110 |
|
| 111 |
// FIXME: __attribute__((cleanup)) ? |
| 112 |
|
| 113 |
switch (dtorKind) { |
| 114 |
case QualType::DK_none: |
| 115 |
return; |
| 116 |
|
| 117 |
case QualType::DK_cxx_destructor: |
| 118 |
break; |
| 119 |
|
| 120 |
case QualType::DK_objc_strong_lifetime: |
| 121 |
case QualType::DK_objc_weak_lifetime: |
| 122 |
case QualType::DK_nontrivial_c_struct: |
| 123 |
// We don't care about releasing objects during process teardown. |
| 124 |
assert(!vd->getTLSKind() && "should have rejected this"); |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
// If not constant storage we'll emit this regardless of NeedsDtor value. |
| 129 |
CIRGenBuilderTy &builder = cgf.getBuilder(); |
| 130 |
|
| 131 |
// Prepare the dtor region. |
| 132 |
mlir::OpBuilder::InsertionGuard guard(builder); |
| 133 |
mlir::Block *block = builder.createBlock(&addr.getDtorRegion()); |
| 134 |
CIRGenFunction::LexicalScope lexScope{cgf, addr.getLoc(), |
| 135 |
builder.getInsertionBlock()}; |
| 136 |
lexScope.setAsGlobalInit(); |
| 137 |
builder.setInsertionPointToStart(block); |
| 138 |
|
| 139 |
CIRGenModule &cgm = cgf.cgm; |
| 140 |
QualType type = vd->getType(); |
| 141 |
|
| 142 |
// Special-case non-array C++ destructors, if they have the right signature. |
| 143 |
// Under some ABIs, destructors return this instead of void, and cannot be |
| 144 |
// passed directly to __cxa_atexit if the target does not allow this |
| 145 |
// mismatch. |
| 146 |
const CXXRecordDecl *record = type->getAsCXXRecordDecl(); |
| 147 |
bool canRegisterDestructor = |
| 148 |
record && (!cgm.getCXXABI().hasThisReturn( |
| 149 |
GlobalDecl(record->getDestructor(), Dtor_Complete)) || |
| 150 |
cgm.getCXXABI().canCallMismatchedFunctionType()); |
| 151 |
|
| 152 |
// If __cxa_atexit is disabled via a flag, a different helper function is |
| 153 |
// generated elsewhere which uses atexit instead, and it takes the destructor |
| 154 |
// directly. |
| 155 |
cir::FuncOp fnOp; |
| 156 |
if (record && (canRegisterDestructor || cgm.getCodeGenOpts().CXAAtExit)) { |
| 157 |
if (vd->getTLSKind()) |
| 158 |
cgm.errorNYI(vd->getSourceRange(), "TLS destructor"); |
| 159 |
assert(!record->hasTrivialDestructor()); |
| 160 |
assert(!cir::MissingFeatures::openCL()); |
| 161 |
CXXDestructorDecl *dtor = record->getDestructor(); |
| 162 |
// In LLVM OG codegen this is done in registerGlobalDtor, but CIRGen |
| 163 |
// relies on LoweringPrepare for further decoupling, so build the |
| 164 |
// call right here. |
| 165 |
auto gd = GlobalDecl(dtor, Dtor_Complete); |
| 166 |
fnOp = cgm.getAddrAndTypeOfCXXStructor(gd).second; |
| 167 |
builder.createCallOp(cgf.getLoc(vd->getSourceRange()), |
| 168 |
mlir::FlatSymbolRefAttr::get(fnOp.getSymNameAttr()), |
| 169 |
mlir::ValueRange{cgm.getAddrOfGlobalVar(vd)}); |
| 170 |
assert(fnOp && "expected cir.func"); |
| 171 |
// TODO(cir): This doesn't do anything but check for unhandled conditions. |
| 172 |
// What it is meant to do should really be happening in LoweringPrepare. |
| 173 |
cgm.getCXXABI().registerGlobalDtor(vd, fnOp, nullptr); |
| 174 |
} else { |
| 175 |
// Otherwise, a custom destroyed is needed. Classic codegen creates a helper |
| 176 |
// function here and emits the destroy into the helper function, which is |
| 177 |
// called from __cxa_atexit. |
| 178 |
// In CIR, we just emit the destroy into the dtor region. It will be moved |
| 179 |
// into a separate function during the LoweringPrepare pass. |
| 180 |
// FIXME(cir): We should create a new operation here to explicitly get the |
| 181 |
// address of the global into whose dtor region we are emiiting the destroy. |
| 182 |
// The same applies to code above where it is calling getAddrOfGlobalVar. |
| 183 |
mlir::Value globalVal = builder.createGetGlobal(addr); |
| 184 |
CharUnits alignment = cgf.getContext().getDeclAlign(vd); |
| 185 |
Address globalAddr{globalVal, cgf.convertTypeForMem(type), alignment}; |
| 186 |
cgf.emitDestroy(globalAddr, type, cgf.getDestroyer(dtorKind)); |
| 187 |
} |
| 188 |
|
| 189 |
builder.setInsertionPointToEnd(block); |
| 190 |
if (block->empty()) { |
| 191 |
block->erase(); |
| 192 |
// Don't confuse lexical cleanup. |
| 193 |
builder.clearInsertionPoint(); |
| 194 |
} else { |
| 195 |
cir::YieldOp::create(builder, addr.getLoc()); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
cir::FuncOp CIRGenModule::codegenCXXStructor(GlobalDecl gd) { |
| 200 |
const CIRGenFunctionInfo &fnInfo = |
| 201 |
getTypes().arrangeCXXStructorDeclaration(gd); |
| 202 |
cir::FuncType funcType = getTypes().getFunctionType(fnInfo); |
| 203 |
cir::FuncOp fn = getAddrOfCXXStructor(gd, &fnInfo, /*FnType=*/nullptr, |
| 204 |
/*DontDefer=*/true, ForDefinition); |
| 205 |
setFunctionLinkage(gd, fn); |
| 206 |
CIRGenFunction cgf{*this, builder}; |
| 207 |
curCGF = &cgf; |
| 208 |
{ |
| 209 |
mlir::OpBuilder::InsertionGuard guard(builder); |
| 210 |
cgf.generateCode(gd, fn, funcType); |
| 211 |
} |
| 212 |
curCGF = nullptr; |
| 213 |
|
| 214 |
setNonAliasAttributes(gd, fn); |
| 215 |
setCIRFunctionAttributesForDefinition(mlir::cast<FunctionDecl>(gd.getDecl()), |
| 216 |
fn); |
| 217 |
return fn; |
| 218 |
} |
| 219 |
|
| 220 |
// Global variables requiring non-trivial initialization are handled |
| 221 |
// differently in CIR than in classic codegen. Classic codegen emits |
| 222 |
// a global init function (__cxx_global_var_init) and inserts |
| 223 |
// initialization for each global there. In CIR, we attach a ctor |
| 224 |
// region to the global variable and insert the initialization code |
| 225 |
// into the ctor region. This will be moved into the |
| 226 |
// __cxx_global_var_init function during the LoweringPrepare pass. |
| 227 |
void CIRGenModule::emitCXXGlobalVarDeclInit(const VarDecl *varDecl, |
| 228 |
cir::GlobalOp addr, |
| 229 |
bool performInit) { |
| 230 |
QualType ty = varDecl->getType(); |
| 231 |
|
| 232 |
// TODO: handle address space |
| 233 |
// The address space of a static local variable (addr) may be different |
| 234 |
// from the address space of the "this" argument of the constructor. In that |
| 235 |
// case, we need an addrspacecast before calling the constructor. |
| 236 |
// |
| 237 |
// struct StructWithCtor { |
| 238 |
// __device__ StructWithCtor() {...} |
| 239 |
// }; |
| 240 |
// __device__ void foo() { |
| 241 |
// __shared__ StructWithCtor s; |
| 242 |
// ... |
| 243 |
// } |
| 244 |
// |
| 245 |
// For example, in the above CUDA code, the static local variable s has a |
| 246 |
// "shared" address space qualifier, but the constructor of StructWithCtor |
| 247 |
// expects "this" in the "generic" address space. |
| 248 |
assert(!cir::MissingFeatures::addressSpace()); |
| 249 |
|
| 250 |
// Create a CIRGenFunction to emit the initializer. While this isn't a true |
| 251 |
// function, the handling works the same way. |
| 252 |
CIRGenFunction cgf{*this, builder, true}; |
| 253 |
llvm::SaveAndRestore<CIRGenFunction *> savedCGF(curCGF, &cgf); |
| 254 |
curCGF->curFn = addr; |
| 255 |
|
| 256 |
CIRGenFunction::SourceLocRAIIObject fnLoc{cgf, |
| 257 |
getLoc(varDecl->getLocation())}; |
| 258 |
|
| 259 |
assert(!cir::MissingFeatures::astVarDeclInterface()); |
| 260 |
|
| 261 |
if (!ty->isReferenceType()) { |
| 262 |
assert(!cir::MissingFeatures::openMP()); |
| 263 |
|
| 264 |
bool needsDtor = varDecl->needsDestruction(getASTContext()) == |
| 265 |
QualType::DK_cxx_destructor; |
| 266 |
bool isConstantStorage = |
| 267 |
varDecl->getType().isConstantStorage(getASTContext(), true, !needsDtor); |
| 268 |
// PerformInit, constant store invariant / destroy handled below. |
| 269 |
if (performInit) { |
| 270 |
emitDeclInit(cgf, varDecl, addr); |
| 271 |
// For constant storage, emit invariant.start in the ctor region after |
| 272 |
// initialization but before the yield. |
| 273 |
if (isConstantStorage) { |
| 274 |
CIRGenBuilderTy &builder = cgf.getBuilder(); |
| 275 |
mlir::OpBuilder::InsertionGuard guard(builder); |
| 276 |
// Set insertion point to end of ctor region (before yield) |
| 277 |
if (!addr.getCtorRegion().empty()) { |
| 278 |
mlir::Block *block = &addr.getCtorRegion().back(); |
| 279 |
// Find the yield op and insert before it |
| 280 |
mlir::Operation *yieldOp = block->getTerminator(); |
| 281 |
if (yieldOp) { |
| 282 |
builder.setInsertionPoint(yieldOp); |
| 283 |
emitDeclInvariant(cgf, varDecl); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
} else if (isConstantStorage) { |
| 288 |
emitDeclInvariant(cgf, varDecl); |
| 289 |
} |
| 290 |
|
| 291 |
if (!isConstantStorage) |
| 292 |
emitDeclDestroy(cgf, varDecl, addr); |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
errorNYI(varDecl->getSourceRange(), "global with reference type"); |
| 297 |
} |