| 1 | //===--- CIRGenCall.cpp - Encapsulate calling convention details ----------===// |
| 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 | // These classes wrap the information about a call or function definition used |
| 10 | // to handle ABI compliancy. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CIRGenCall.h" |
| 15 | #include "CIRGenCXXABI.h" |
| 16 | #include "CIRGenCleanup.h" |
| 17 | #include "CIRGenFunction.h" |
| 18 | #include "CIRGenFunctionInfo.h" |
| 19 | #include "mlir/Dialect/LLVMIR/LLVMDialectDecl.h" |
| 20 | #include "mlir/IR/Attributes.h" |
| 21 | #include "clang/Basic/DiagnosticFrontend.h" |
| 22 | #include "clang/CIR/ABIArgInfo.h" |
| 23 | #include "clang/CIR/MissingFeatures.h" |
| 24 | #include "clang/CodeGenUtils/CallUtils.h" |
| 25 | #include "llvm/ADT/FloatingPointMode.h" |
| 26 | #include "llvm/ADT/StringSet.h" |
| 27 | #include "llvm/Support/TypeSize.h" |
| 28 | |
| 29 | using namespace clang; |
| 30 | using namespace clang::CIRGen; |
| 31 | |
| 32 | CIRGenFunctionInfo * |
| 33 | CIRGenFunctionInfo::create(cir::CallingConv cirCC, FunctionType::ExtInfo info, |
| 34 | bool isInstanceMethod, CanQualType resultType, |
| 35 | llvm::ArrayRef<CanQualType> argTypes, |
| 36 | RequiredArgs required) { |
| 37 | // The first slot allocated for arg type slot is for the return value. |
| 38 | void *buffer = operator new( |
| 39 | totalSizeToAlloc<CanQualType>(Counts: argTypes.size() + 1)); |
| 40 | |
| 41 | assert(!cir::MissingFeatures::opCallCIRGenFuncInfoParamInfo()); |
| 42 | |
| 43 | CIRGenFunctionInfo *fi = new (buffer) CIRGenFunctionInfo(); |
| 44 | |
| 45 | fi->callingConvention = llvm::to_underlying(E: cirCC); |
| 46 | fi->astCallingConvention = info.getCC(); |
| 47 | fi->noReturn = info.getNoReturn(); |
| 48 | fi->instanceMethod = isInstanceMethod; |
| 49 | |
| 50 | fi->required = required; |
| 51 | fi->numArgs = argTypes.size(); |
| 52 | |
| 53 | // requiredArguments() reads getNumRequiredArgs() entries out of the |
| 54 | // trailing-object array, so a signature that claims more required arguments |
| 55 | // than we have types for reads past the end and hands a null QualType to |
| 56 | // convertType. |
| 57 | assert((!required.allowsOptionalArgs() || |
| 58 | required.getNumRequiredArgs() <= fi->numArgs) && |
| 59 | "more required arguments than argument types" ); |
| 60 | |
| 61 | fi->getArgTypes()[0] = resultType; |
| 62 | std::copy(first: argTypes.begin(), last: argTypes.end(), result: fi->argTypesBegin()); |
| 63 | assert(!cir::MissingFeatures::opCallCIRGenFuncInfoExtParamInfo()); |
| 64 | |
| 65 | return fi; |
| 66 | } |
| 67 | |
| 68 | cir::FuncType CIRGenTypes::getFunctionType(GlobalDecl gd) { |
| 69 | const CIRGenFunctionInfo &fi = arrangeGlobalDeclaration(gd); |
| 70 | return getFunctionType(info: fi); |
| 71 | } |
| 72 | |
| 73 | cir::FuncType CIRGenTypes::getFunctionType(const CIRGenFunctionInfo &info) { |
| 74 | mlir::Type resultType = convertType(type: info.getReturnType()); |
| 75 | SmallVector<mlir::Type, 8> argTypes; |
| 76 | argTypes.reserve(N: info.getNumRequiredArgs()); |
| 77 | |
| 78 | for (const CanQualType &argType : info.requiredArguments()) |
| 79 | argTypes.push_back(Elt: convertType(type: argType)); |
| 80 | |
| 81 | return cir::FuncType::get(inputs: argTypes, |
| 82 | returnType: (resultType ? resultType : builder.getVoidTy()), |
| 83 | isVarArg: info.isVariadic()); |
| 84 | } |
| 85 | |
| 86 | CIRGenCallee CIRGenCallee::prepareConcreteCallee(CIRGenFunction &cgf) const { |
| 87 | if (isVirtual()) { |
| 88 | const CallExpr *ce = getVirtualCallExpr(); |
| 89 | return cgf.cgm.getCXXABI().getVirtualFunctionPointer( |
| 90 | cgf, gd: getVirtualMethodDecl(), thisAddr: getThisAddress(), ty: getVirtualFunctionType(), |
| 91 | loc: ce ? ce->getBeginLoc() : SourceLocation()); |
| 92 | } |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | void CIRGenFunction::emitAggregateStore(mlir::Value value, Address dest) { |
| 97 | // In classic codegen: |
| 98 | // Function to store a first-class aggregate into memory. We prefer to |
| 99 | // store the elements rather than the aggregate to be more friendly to |
| 100 | // fast-isel. |
| 101 | // In CIR codegen: |
| 102 | // Emit the most simple cir.store possible (e.g. a store for a whole |
| 103 | // record), which can later be broken down in other CIR levels (or prior |
| 104 | // to dialect codegen). |
| 105 | |
| 106 | // Stored result for the callers of this function expected to be in the same |
| 107 | // scope as the value, don't make assumptions about current insertion point. |
| 108 | mlir::OpBuilder::InsertionGuard guard(builder); |
| 109 | builder.setInsertionPointAfter(value.getDefiningOp()); |
| 110 | builder.createStore(loc: getLoc(srcLoc: *currSrcLoc), val: value, dst: dest); |
| 111 | } |
| 112 | |
| 113 | static void addAttributesFromFunctionProtoType(CIRGenBuilderTy &builder, |
| 114 | mlir::NamedAttrList &attrs, |
| 115 | const FunctionProtoType *fpt) { |
| 116 | if (!fpt) |
| 117 | return; |
| 118 | |
| 119 | if (!isUnresolvedExceptionSpec(ESpecType: fpt->getExceptionSpecType()) && |
| 120 | fpt->isNothrow()) |
| 121 | attrs.set(name: cir::CIRDialect::getNoThrowAttrName(), |
| 122 | value: mlir::UnitAttr::get(context: builder.getContext())); |
| 123 | } |
| 124 | |
| 125 | static void addNoBuiltinAttributes(mlir::MLIRContext &ctx, |
| 126 | mlir::NamedAttrList &attrs, |
| 127 | const LangOptions &langOpts, |
| 128 | const NoBuiltinAttr *nba = nullptr) { |
| 129 | // First, handle the language options passed through -fno-builtin. |
| 130 | // or, if there is a wildcard in the builtin names specified through the |
| 131 | // attribute, disable them all. |
| 132 | if (langOpts.NoBuiltin || |
| 133 | (nba && llvm::is_contained(Range: nba->builtinNames(), Element: "*" ))) { |
| 134 | // -fno-builtin disables them all. |
| 135 | // Empty attribute means 'all'. |
| 136 | attrs.set(name: cir::CIRDialect::getNoBuiltinsAttrName(), |
| 137 | value: mlir::ArrayAttr::get(context: &ctx, value: {})); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | llvm::SetVector<mlir::Attribute> nbFuncs; |
| 142 | auto addNoBuiltinAttr = [&ctx, &nbFuncs](StringRef builtinName) { |
| 143 | nbFuncs.insert(X: mlir::StringAttr::get(context: &ctx, bytes: builtinName)); |
| 144 | }; |
| 145 | |
| 146 | // Then, add attributes for builtins specified through -fno-builtin-<name>. |
| 147 | llvm::for_each(Range: langOpts.NoBuiltinFuncs, F: addNoBuiltinAttr); |
| 148 | |
| 149 | // Now, let's check the __attribute__((no_builtin("...")) attribute added to |
| 150 | // the source. |
| 151 | if (nba) |
| 152 | llvm::for_each(Range: nba->builtinNames(), F: addNoBuiltinAttr); |
| 153 | |
| 154 | if (!nbFuncs.empty()) |
| 155 | attrs.set(name: cir::CIRDialect::getNoBuiltinsAttrName(), |
| 156 | value: mlir::ArrayAttr::get(context: &ctx, value: nbFuncs.getArrayRef())); |
| 157 | } |
| 158 | |
| 159 | /// Add denormal-fp-math and denormal-fp-math-f32 as appropriate for the |
| 160 | /// requested denormal behavior, accounting for the overriding behavior of the |
| 161 | /// -f32 case. |
| 162 | static void addDenormalModeAttrs(llvm::DenormalMode fpDenormalMode, |
| 163 | llvm::DenormalMode fp32DenormalMode, |
| 164 | mlir::NamedAttrList &attrs) { |
| 165 | // TODO(cir): Classic-codegen sets the denormal modes here. There are two |
| 166 | // values, both with a string, but it seems that perhaps we could combine |
| 167 | // these into a single attribute? It seems a little silly to have two so |
| 168 | // similar named attributes that do the same thing. |
| 169 | } |
| 170 | |
| 171 | /// Add default attributes to a function, which have merge semantics under |
| 172 | /// -mlink-builtin-bitcode and should not simply overwrite any existing |
| 173 | /// attributes in the linked library. |
| 174 | static void |
| 175 | addMergeableDefaultFunctionAttributes(const CodeGenOptions &codeGenOpts, |
| 176 | mlir::NamedAttrList &attrs) { |
| 177 | addDenormalModeAttrs(fpDenormalMode: codeGenOpts.FPDenormalMode, fp32DenormalMode: codeGenOpts.FP32DenormalMode, |
| 178 | attrs); |
| 179 | } |
| 180 | |
| 181 | static llvm::StringLiteral |
| 182 | getZeroCallUsedRegsKindStr(llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind k) { |
| 183 | switch (k) { |
| 184 | case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Skip: |
| 185 | llvm_unreachable("No string value, shouldn't be able to get here" ); |
| 186 | case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPRArg: |
| 187 | return "used-gpr-arg" ; |
| 188 | case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedGPR: |
| 189 | return "used-gpr" ; |
| 190 | case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::UsedArg: |
| 191 | return "used-arg" ; |
| 192 | case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Used: |
| 193 | return "used" ; |
| 194 | case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPRArg: |
| 195 | return "all-gpr-arg" ; |
| 196 | case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllGPR: |
| 197 | return "all-gpr" ; |
| 198 | case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::AllArg: |
| 199 | return "all-arg" ; |
| 200 | case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::All: |
| 201 | return "all" ; |
| 202 | } |
| 203 | |
| 204 | llvm_unreachable("Unknown kind?" ); |
| 205 | } |
| 206 | |
| 207 | std::optional<cir::FramePointerKind> |
| 208 | CIRGenModule::getFramePointerKind(CodeGenOptions::FramePointerKind kind) { |
| 209 | switch (kind) { |
| 210 | case CodeGenOptions::FramePointerKind::None: |
| 211 | return std::nullopt; |
| 212 | case CodeGenOptions::FramePointerKind::Reserved: |
| 213 | return cir::FramePointerKind::Reserved; |
| 214 | case CodeGenOptions::FramePointerKind::NonLeafNoReserve: |
| 215 | return cir::FramePointerKind::NonLeafNoReserve; |
| 216 | case CodeGenOptions::FramePointerKind::NonLeaf: |
| 217 | return cir::FramePointerKind::NonLeaf; |
| 218 | case CodeGenOptions::FramePointerKind::All: |
| 219 | return cir::FramePointerKind::All; |
| 220 | } |
| 221 | llvm_unreachable("Unknown frame pointer kind" ); |
| 222 | } |
| 223 | |
| 224 | /// Add default attributes to a function, which have merge semantics under |
| 225 | /// -mlink-builtin-bitcode and should not simply overwrite any existing |
| 226 | /// attributes in the linked library. |
| 227 | static void addTrivialDefaultFunctionAttributes( |
| 228 | mlir::MLIRContext *mlirCtx, StringRef name, bool hasOptNoneAttr, |
| 229 | const CodeGenOptions &codeGenOpts, const LangOptions &langOpts, |
| 230 | bool attrOnCallSite, mlir::NamedAttrList &attrs) { |
| 231 | // TODO(cir): Handle optimize attribute flag here. |
| 232 | // OptimizeNoneAttr takes precedence over -Os or -Oz. No warning needed. |
| 233 | if (!hasOptNoneAttr) { |
| 234 | if (codeGenOpts.OptimizeSize) |
| 235 | attrs.set(name: cir::CIRDialect::getOptimizeForSizeAttrName(), |
| 236 | value: mlir::UnitAttr::get(context: mlirCtx)); |
| 237 | if (codeGenOpts.OptimizeSize == 2) |
| 238 | attrs.set(name: cir::CIRDialect::getMinSizeAttrName(), |
| 239 | value: mlir::UnitAttr::get(context: mlirCtx)); |
| 240 | } |
| 241 | |
| 242 | // TODO(cir): Classic codegen adds 'DisableRedZone', 'indirect-tls-seg-refs' |
| 243 | // and 'NoImplicitFloat' here. |
| 244 | |
| 245 | if (attrOnCallSite) { |
| 246 | // Add the 'nobuiltin' tag, which is different from 'no-builtins'. |
| 247 | if (!codeGenOpts.SimplifyLibCalls || langOpts.isNoBuiltinFunc(Name: name)) |
| 248 | attrs.set(name: cir::CIRDialect::getNoBuiltinAttrName(), |
| 249 | value: mlir::UnitAttr::get(context: mlirCtx)); |
| 250 | |
| 251 | if (!codeGenOpts.TrapFuncName.empty()) |
| 252 | attrs.set(name: cir::CIRDialect::getTrapFuncNameAttrName(), |
| 253 | value: mlir::StringAttr::get(context: mlirCtx, bytes: codeGenOpts.TrapFuncName)); |
| 254 | } else { |
| 255 | if (std::optional<cir::FramePointerKind> kind = |
| 256 | CIRGenModule::getFramePointerKind(kind: codeGenOpts.getFramePointer())) |
| 257 | attrs.set(name: cir::CIRDialect::getFramePointerAttrName(), |
| 258 | value: cir::FramePointerKindAttr::get(context: mlirCtx, value: *kind)); |
| 259 | |
| 260 | // TODO(cir): a number of other attribute 1-offs based on codegen/lang opts |
| 261 | // should be done here: less-recise-fpmad null-pointer-is-valid |
| 262 | // various inf/nan/nsz/etc work here. |
| 263 | // |
| 264 | // no-trapping-math and stack-protector-buffer-size are TU-wide, so they |
| 265 | // live in #cir.lowering_options and LowerToLLVM adds them to every |
| 266 | // function not marked as a runtime function. |
| 267 | |
| 268 | // TODO(cir): other attributes here: |
| 269 | // reciprocal estimates, prefer-vector-width, stackrealign, backchain, |
| 270 | // split-stack, speculative-load-hardening. |
| 271 | |
| 272 | if (codeGenOpts.getZeroCallUsedRegs() == |
| 273 | llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Skip) |
| 274 | attrs.erase(name: cir::CIRDialect::getZeroCallUsedRegsAttrName()); |
| 275 | else |
| 276 | attrs.set(name: cir::CIRDialect::getZeroCallUsedRegsAttrName(), |
| 277 | value: mlir::StringAttr::get(context: mlirCtx, |
| 278 | bytes: getZeroCallUsedRegsKindStr( |
| 279 | k: codeGenOpts.getZeroCallUsedRegs()))); |
| 280 | } |
| 281 | |
| 282 | if (langOpts.assumeFunctionsAreConvergent()) { |
| 283 | // Conservatively, mark all functions and calls in CUDA and OpenCL as |
| 284 | // convergent (meaning, they may call an intrinsically convergent op, such |
| 285 | // as __syncthreads() / barrier(), and so can't have certain optimizations |
| 286 | // applied around them). LLVM will remove this attribute where it safely |
| 287 | // can. |
| 288 | attrs.set(name: cir::CIRDialect::getConvergentAttrName(), |
| 289 | value: mlir::UnitAttr::get(context: mlirCtx)); |
| 290 | } |
| 291 | |
| 292 | // Device code cannot unwind. 'nothrow' keeps calls from getting an unwind |
| 293 | // edge, so 'nounwind' becomes LLVM's nounwind. |
| 294 | // TODO: OpenMP offload is not covered. |
| 295 | if ((langOpts.CUDA && langOpts.CUDAIsDevice) || langOpts.OpenCL || |
| 296 | langOpts.SYCLIsDevice) { |
| 297 | attrs.set(name: cir::CIRDialect::getNoThrowAttrName(), |
| 298 | value: mlir::UnitAttr::get(context: mlirCtx)); |
| 299 | attrs.set(name: cir::CIRDialect::getNoUnwindAttrName(), |
| 300 | value: mlir::UnitAttr::get(context: mlirCtx)); |
| 301 | } |
| 302 | |
| 303 | if (codeGenOpts.SaveRegParams && !attrOnCallSite) |
| 304 | attrs.set(name: cir::CIRDialect::getSaveRegParamsAttrName(), |
| 305 | value: mlir::UnitAttr::get(context: mlirCtx)); |
| 306 | |
| 307 | // These come in the form of an optional equality sign, so make sure we pass |
| 308 | // these on correctly. These will eventually just be passed through to |
| 309 | // LLVM-IR, but we want to put them all in 1 array to simplify the |
| 310 | // LLVM-MLIR dialect. |
| 311 | SmallVector<mlir::NamedAttribute> defaultFuncAttrs; |
| 312 | llvm::transform( |
| 313 | Range: codeGenOpts.DefaultFunctionAttrs, d_first: std::back_inserter(x&: defaultFuncAttrs), |
| 314 | F: [mlirCtx](llvm::StringRef arg) { |
| 315 | auto [var, value] = arg.split(Separator: '='); |
| 316 | auto valueAttr = |
| 317 | value.empty() |
| 318 | ? cast<mlir::Attribute>(Val: mlir::UnitAttr::get(context: mlirCtx)) |
| 319 | : cast<mlir::Attribute>(Val: mlir::StringAttr::get(context: mlirCtx, bytes: value)); |
| 320 | return mlir::NamedAttribute(var, valueAttr); |
| 321 | }); |
| 322 | |
| 323 | if (!defaultFuncAttrs.empty()) |
| 324 | attrs.set(name: cir::CIRDialect::getDefaultFuncAttrsAttrName(), |
| 325 | value: mlir::DictionaryAttr::get(context: mlirCtx, value: defaultFuncAttrs)); |
| 326 | |
| 327 | // TODO(cir): Do branch protection attributes here. |
| 328 | } |
| 329 | |
| 330 | /// This function matches the behavior of 'getDefaultFunctionAttributes' from |
| 331 | /// classic codegen, despite the similarity of its name to |
| 332 | /// 'addDefaultFunctionDefinitionAttributes', which is a caller of this |
| 333 | /// function. |
| 334 | void CIRGenModule::addDefaultFunctionAttributes(StringRef name, |
| 335 | bool hasOptNoneAttr, |
| 336 | bool attrOnCallSite, |
| 337 | mlir::NamedAttrList &attrs) { |
| 338 | |
| 339 | addTrivialDefaultFunctionAttributes(mlirCtx: &getMLIRContext(), name, hasOptNoneAttr, |
| 340 | codeGenOpts, langOpts, attrOnCallSite, |
| 341 | attrs); |
| 342 | |
| 343 | if (!attrOnCallSite) { |
| 344 | // TODO(cir): Classic codegen adds pointer-auth attributes here, by calling |
| 345 | // into TargetCodeGenInfo. At the moment, we've not looked into this as it |
| 346 | // is somewhat less used. |
| 347 | addMergeableDefaultFunctionAttributes(codeGenOpts, attrs); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /// Construct the CIR attribute list of a function or call. |
| 352 | void CIRGenModule::constructAttributeList( |
| 353 | llvm::StringRef name, const CIRGenFunctionInfo &info, |
| 354 | CIRGenCalleeInfo calleeInfo, mlir::NamedAttrList &attrs, |
| 355 | llvm::MutableArrayRef<mlir::NamedAttrList> argAttrs, |
| 356 | mlir::NamedAttrList &retAttrs, cir::CallingConv &callingConv, |
| 357 | bool attrOnCallSite, bool isThunk) { |
| 358 | callingConv = info.getCallingConvention(); |
| 359 | |
| 360 | auto addUnitAttr = [&](llvm::StringRef name) { |
| 361 | attrs.set(name, value: mlir::UnitAttr::get(context: &getMLIRContext())); |
| 362 | }; |
| 363 | |
| 364 | if (info.isNoReturn()) |
| 365 | addUnitAttr(cir::CIRDialect::getNoReturnAttrName()); |
| 366 | |
| 367 | // TODO(cir): Implement/check the CSME Nonsecure call attribute here. This |
| 368 | // requires being in CSME mode. |
| 369 | |
| 370 | addAttributesFromFunctionProtoType(builder&: getBuilder(), attrs, |
| 371 | fpt: calleeInfo.getCalleeFunctionProtoType()); |
| 372 | |
| 373 | GlobalDecl targetGlobalDecl = calleeInfo.getCalleeDecl(); |
| 374 | const Decl *targetDecl = targetGlobalDecl.getDecl(); |
| 375 | |
| 376 | // TODO(cir): OMP Assume Attributes should be here. |
| 377 | |
| 378 | const NoBuiltinAttr *nba = nullptr; |
| 379 | |
| 380 | // TODO(cir): Some work for arg memory effects can be done here, as it is in |
| 381 | // classic codegen. |
| 382 | |
| 383 | if (targetDecl) { |
| 384 | if (targetDecl->hasAttr<NoThrowAttr>()) |
| 385 | addUnitAttr(cir::CIRDialect::getNoThrowAttrName()); |
| 386 | // TODO(cir): This is actually only possible if targetDecl isn't a |
| 387 | // declarator, which ObjCMethodDecl seems to be the only way to get this to |
| 388 | // happen. We're including it here for completeness, but we should add a |
| 389 | // test for this when we start generating ObjectiveC. |
| 390 | if (targetDecl->hasAttr<NoReturnAttr>()) |
| 391 | addUnitAttr(cir::CIRDialect::getNoReturnAttrName()); |
| 392 | if (targetDecl->hasAttr<ReturnsTwiceAttr>()) |
| 393 | addUnitAttr(cir::CIRDialect::getReturnsTwiceAttrName()); |
| 394 | if (targetDecl->hasAttr<ColdAttr>()) |
| 395 | addUnitAttr(cir::CIRDialect::getColdAttrName()); |
| 396 | if (targetDecl->hasAttr<HotAttr>()) |
| 397 | addUnitAttr(cir::CIRDialect::getHotAttrName()); |
| 398 | if (targetDecl->hasAttr<NoDuplicateAttr>()) |
| 399 | addUnitAttr(cir::CIRDialect::getNoDuplicatesAttrName()); |
| 400 | if (targetDecl->hasAttr<ConvergentAttr>()) |
| 401 | addUnitAttr(cir::CIRDialect::getConvergentAttrName()); |
| 402 | |
| 403 | if (const FunctionDecl *func = dyn_cast<FunctionDecl>(Val: targetDecl)) { |
| 404 | addAttributesFromFunctionProtoType( |
| 405 | builder&: getBuilder(), attrs, fpt: func->getType()->getAs<FunctionProtoType>()); |
| 406 | |
| 407 | // A sane operator new returns a non-aliasing pointer. |
| 408 | if (attrOnCallSite && func->isReplaceableGlobalAllocationFunction() && |
| 409 | codeGenOpts.AssumeSaneOperatorNew && |
| 410 | func->getDeclName().isAnyOperatorNew()) { |
| 411 | retAttrs.set(name: mlir::LLVM::LLVMDialect::getNoAliasAttrName(), |
| 412 | value: mlir::UnitAttr::get(context: &getMLIRContext())); |
| 413 | } |
| 414 | |
| 415 | assert(!cir::MissingFeatures::opCallAttrs()); |
| 416 | |
| 417 | const CXXMethodDecl *md = dyn_cast<CXXMethodDecl>(Val: func); |
| 418 | bool isVirtualCall = md && md->isVirtual(); |
| 419 | |
| 420 | // Don't use [[noreturn]], _Noreturn or [[no_builtin]] for a call to a |
| 421 | // virtual function. These attributes are not inherited by overloads. |
| 422 | if (!(attrOnCallSite && isVirtualCall)) { |
| 423 | if (func->isNoReturn()) |
| 424 | addUnitAttr(cir::CIRDialect::getNoReturnAttrName()); |
| 425 | nba = func->getAttr<NoBuiltinAttr>(); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | assert(!cir::MissingFeatures::opCallAttrs()); |
| 430 | |
| 431 | auto setMemoryEffects = [&](cir::MemoryEffectsAttr effects) { |
| 432 | attrs.set(name: cir::CIRDialect::getMemoryEffectsAttrName(), value: effects); |
| 433 | }; |
| 434 | |
| 435 | // 'const', 'pure' and 'noalias' attributed functions are also nounwind. |
| 436 | if (targetDecl->hasAttr<ConstAttr>()) { |
| 437 | setMemoryEffects(cir::MemoryEffectsAttr::none(ctx: &getMLIRContext())); |
| 438 | addUnitAttr(cir::CIRDialect::getNoUnwindAttrName()); |
| 439 | // gcc specifies that 'const' functions have greater restrictions than |
| 440 | // 'pure' functions, so they also cannot have infinite loops. |
| 441 | addUnitAttr(cir::CIRDialect::getWillReturnAttrName()); |
| 442 | } else if (targetDecl->hasAttr<PureAttr>()) { |
| 443 | setMemoryEffects(cir::MemoryEffectsAttr::readOnly(ctx: &getMLIRContext())); |
| 444 | addUnitAttr(cir::CIRDialect::getNoUnwindAttrName()); |
| 445 | // gcc specifies that 'pure' functions cannot have infinite loops. |
| 446 | addUnitAttr(cir::CIRDialect::getWillReturnAttrName()); |
| 447 | } else if (targetDecl->hasAttr<NoAliasAttr>()) { |
| 448 | setMemoryEffects( |
| 449 | cir::MemoryEffectsAttr::inaccessibleOrArgMemOnly(ctx: &getMLIRContext())); |
| 450 | addUnitAttr(cir::CIRDialect::getNoUnwindAttrName()); |
| 451 | } |
| 452 | |
| 453 | // Both __attribute__((malloc)) and __declspec(restrict) are represented by |
| 454 | // RestrictAttr. Only forms without a deallocator imply a noalias return. |
| 455 | if (const auto *restrictAttr = targetDecl->getAttr<RestrictAttr>(); |
| 456 | restrictAttr && restrictAttr->getDeallocator() == nullptr) |
| 457 | retAttrs.set(name: mlir::LLVM::LLVMDialect::getNoAliasAttrName(), |
| 458 | value: mlir::UnitAttr::get(context: &getMLIRContext())); |
| 459 | |
| 460 | if (targetDecl->hasAttr<ReturnsNonNullAttr>() && |
| 461 | !codeGenOpts.NullPointerIsValid) |
| 462 | retAttrs.set(name: mlir::LLVM::LLVMDialect::getNonNullAttrName(), |
| 463 | value: mlir::UnitAttr::get(context: &getMLIRContext())); |
| 464 | if (targetDecl->hasAttr<AnyX86NoCallerSavedRegistersAttr>()) |
| 465 | addUnitAttr(cir::CIRDialect::getNoCallerSavedRegsAttrName()); |
| 466 | // TODO(cir): Implement 'NoCFCheck' attribute here. This requires |
| 467 | // fcf-protection mode. |
| 468 | if (targetDecl->hasAttr<LeafAttr>()) |
| 469 | addUnitAttr(cir::CIRDialect::getNoCallbackAttrName()); |
| 470 | // TODO(cir): Implement 'BPFFastCall' attribute here. This requires C, and |
| 471 | // the BPF target. |
| 472 | |
| 473 | if (auto *allocSizeAttr = targetDecl->getAttr<AllocSizeAttr>()) { |
| 474 | unsigned size = allocSizeAttr->getElemSizeParam().getLLVMIndex(); |
| 475 | |
| 476 | if (allocSizeAttr->getNumElemsParam().isValid()) { |
| 477 | unsigned numElts = allocSizeAttr->getNumElemsParam().getLLVMIndex(); |
| 478 | attrs.set(name: cir::CIRDialect::getAllocSizeAttrName(), |
| 479 | value: builder.getDenseI32ArrayAttr( |
| 480 | values: {static_cast<int>(size), static_cast<int>(numElts)})); |
| 481 | } else { |
| 482 | attrs.set(name: cir::CIRDialect::getAllocSizeAttrName(), |
| 483 | value: builder.getDenseI32ArrayAttr(values: {static_cast<int>(size)})); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | // TODO(cir): Quite a few CUDA and OpenCL attributes are added here. |
| 488 | |
| 489 | // -cl-uniform-work-group-size / -foffload-uniform-block: work groups are |
| 490 | // uniform (global work-size is a multiple of work-group size). |
| 491 | if (langOpts.OffloadUniformBlock) |
| 492 | addUnitAttr(cir::CIRDialect::getUniformWorkGroupSizeAttrName()); |
| 493 | |
| 494 | // TODO(cir): we should also do 'aarch64_pstate_sm_body' here. |
| 495 | |
| 496 | if (auto *modularFormat = targetDecl->getAttr<ModularFormatAttr>()) { |
| 497 | FormatAttr *format = targetDecl->getAttr<FormatAttr>(); |
| 498 | StringRef type = format->getType()->getName(); |
| 499 | std::string formatIdx = std::to_string(val: format->getFormatIdx()); |
| 500 | std::string firstArg = std::to_string(val: format->getFirstArg()); |
| 501 | SmallVector<StringRef> args = { |
| 502 | type, formatIdx, firstArg, |
| 503 | modularFormat->getModularImplFn()->getName(), |
| 504 | modularFormat->getImplName()}; |
| 505 | llvm::append_range(C&: args, R: modularFormat->aspects()); |
| 506 | attrs.set(name: cir::CIRDialect::getModularFormatAttrName(), |
| 507 | value: builder.getStringAttr(bytes: llvm::join(R&: args, Separator: "," ))); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | addNoBuiltinAttributes(ctx&: getMLIRContext(), attrs, langOpts: getLangOpts(), nba); |
| 512 | |
| 513 | bool hasOptNoneAttr = targetDecl && targetDecl->hasAttr<OptimizeNoneAttr>(); |
| 514 | addDefaultFunctionAttributes(name, hasOptNoneAttr, attrOnCallSite, attrs); |
| 515 | if (targetDecl) { |
| 516 | // TODO(cir): There is another region of `if (targetDecl)` that handles |
| 517 | // removing some attributes that are necessary modifications of the |
| 518 | // default-function attrs. Including: |
| 519 | // NoSpeculativeLoadHardening |
| 520 | // SpeculativeLoadHardening |
| 521 | // NoSplitStack |
| 522 | // Non-lazy-bind |
| 523 | // 'sample-profile-suffix-elision-policy'. |
| 524 | |
| 525 | if (targetDecl->hasAttr<ZeroCallUsedRegsAttr>()) { |
| 526 | // A function "__attribute__((...))" overrides the command-line flag. |
| 527 | auto kind = |
| 528 | targetDecl->getAttr<ZeroCallUsedRegsAttr>()->getZeroCallUsedRegs(); |
| 529 | attrs.set( |
| 530 | name: cir::CIRDialect::getZeroCallUsedRegsAttrName(), |
| 531 | value: mlir::StringAttr::get( |
| 532 | context: &getMLIRContext(), |
| 533 | bytes: ZeroCallUsedRegsAttr::ConvertZeroCallUsedRegsKindToStr(Val: kind))); |
| 534 | } |
| 535 | |
| 536 | if (targetDecl->hasAttr<NoConvergentAttr>()) |
| 537 | attrs.erase(name: cir::CIRDialect::getConvergentAttrName()); |
| 538 | } |
| 539 | |
| 540 | // Collect non-call-site function IR attributes from declaration-specific |
| 541 | // information. |
| 542 | if (!attrOnCallSite) { |
| 543 | // These functions require the returns_twice attribute for correct |
| 544 | // codegen, but the attribute may not be added if -fno-builtin is |
| 545 | // specified. We explicitly add that attribute here. |
| 546 | static const llvm::StringSet<> returnsTwiceFn{ |
| 547 | "_setjmpex" , "setjmp" , "_setjmp" , "vfork" , |
| 548 | "sigsetjmp" , "__sigsetjmp" , "savectx" , "getcontext" }; |
| 549 | if (returnsTwiceFn.contains(key: name)) |
| 550 | addUnitAttr(cir::CIRDialect::getReturnsTwiceAttrName()); |
| 551 | |
| 552 | llvm::StringMap<std::string> cpuAndFeatures; |
| 553 | if (getCPUAndFeaturesAttributes(gd: targetGlobalDecl, attrs&: cpuAndFeatures)) { |
| 554 | for (const auto &[key, val] : cpuAndFeatures) |
| 555 | attrs.set(name: key, value: builder.getStringAttr(bytes: val)); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | // TODO(cir): A bunch of non-call-site function IR attributes from |
| 560 | // declaration-specific information, including tail calls, |
| 561 | // cmse_nonsecure_entry, and hotpatch support. |
| 562 | |
| 563 | // TODO(cir): Add loader-replaceable attribute here. |
| 564 | |
| 565 | constructFunctionReturnAttributes(info, targetDecl, isThunk, retAttrs); |
| 566 | constructFunctionArgumentAttributes(info, calleeDecl: targetGlobalDecl, isThunk, |
| 567 | attrOnCallSite, argAttrs); |
| 568 | } |
| 569 | |
| 570 | bool CIRGenModule::hasStrictReturn(QualType retTy, const Decl *targetDecl) { |
| 571 | // As-is msan can not tolerate noundef mismatch between caller and |
| 572 | // implementation. Mismatch is possible for e.g. indirect calls from C-caller |
| 573 | // into C++. Such mismatches lead to confusing false reports. To avoid |
| 574 | // expensive workaround on msan we enforce initialization event in uncommon |
| 575 | // cases where it's allowed. |
| 576 | if (getLangOpts().Sanitize.has(K: SanitizerKind::Memory)) |
| 577 | return true; |
| 578 | // C++ explicitly makes returning undefined values UB. C's rule only applies |
| 579 | // to used values, so we never mark them noundef for now. |
| 580 | if (!getLangOpts().CPlusPlus) |
| 581 | return false; |
| 582 | if (targetDecl) { |
| 583 | if (const FunctionDecl *func = dyn_cast<FunctionDecl>(Val: targetDecl)) { |
| 584 | if (func->isExternC()) |
| 585 | return false; |
| 586 | } else if (const VarDecl *var = dyn_cast<VarDecl>(Val: targetDecl)) { |
| 587 | // Function pointer. |
| 588 | if (var->isExternC()) |
| 589 | return false; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | // We don't want to be too aggressive with the return checking, unless |
| 594 | // it's explicit in the code opts or we're using an appropriate sanitizer. |
| 595 | // Try to respect what the programmer intended. |
| 596 | return getCodeGenOpts().StrictReturn || |
| 597 | !mayDropFunctionReturn(context: getASTContext(), retTy) || |
| 598 | getLangOpts().Sanitize.has(K: SanitizerKind::Return); |
| 599 | } |
| 600 | |
| 601 | bool CIRGenModule::mayDropFunctionReturn(const ASTContext &context, |
| 602 | QualType retTy) { |
| 603 | // We can't just discard the return value for a record type with a |
| 604 | // complex destructor or a non-trivially copyable type. |
| 605 | if (const RecordType *recTy = |
| 606 | retTy.getCanonicalType()->getAsCanonical<RecordType>()) { |
| 607 | if (const auto *record = dyn_cast<CXXRecordDecl>(Val: recTy->getDecl())) |
| 608 | return record->hasTrivialDestructor(); |
| 609 | } |
| 610 | return retTy.isTriviallyCopyableType(Context: context); |
| 611 | } |
| 612 | |
| 613 | static bool determineNoUndef(QualType clangTy, CIRGenTypes &types, |
| 614 | const cir::CIRDataLayout &layout, |
| 615 | const cir::ABIArgInfo &argInfo) { |
| 616 | mlir::Type ty = types.convertTypeForMem(clangTy); |
| 617 | assert(!cir::MissingFeatures::abiArgInfo()); |
| 618 | if (argInfo.isIndirect() || argInfo.isIndirectAliased()) |
| 619 | return true; |
| 620 | if (argInfo.isExtend() && !argInfo.isNoExt()) |
| 621 | return true; |
| 622 | |
| 623 | if (cir::isSized(ty) && !layout.typeSizeEqualsStoreSize(ty)) |
| 624 | // TODO: This will result in a modest amount of values not marked noundef |
| 625 | // when they could be. We care about values that *invisibly* contain undef |
| 626 | // bits from the perspective of LLVM IR. |
| 627 | return false; |
| 628 | |
| 629 | // Classic CodeGen stores a packed bool vector as an integer with one bit per |
| 630 | // element, at least a byte wide, so the check above drops noundef when that |
| 631 | // integer is not a whole number of bytes. The CIR vector type is sized to a |
| 632 | // power of two of at least a byte, which always passes that check, so test |
| 633 | // the storage integer's width directly. |
| 634 | if (clangTy->isPackedVectorBoolType(ctx: types.getASTContext()) && |
| 635 | mlir::cast<cir::VectorType>(Val&: ty).getBoolStorageWidth() % 8) |
| 636 | return false; |
| 637 | |
| 638 | assert(!cir::MissingFeatures::opCallCallConv()); |
| 639 | // The coerced type is not known until CallConvLowering, which drops noundef |
| 640 | // when it is wider than the value's memory type, the check classic |
| 641 | // DetermineNoUndef makes at this point. |
| 642 | if (clangTy->isBitIntType()) |
| 643 | return true; |
| 644 | if (clangTy->isReferenceType()) |
| 645 | return true; |
| 646 | if (clangTy->isNullPtrType()) |
| 647 | return false; |
| 648 | if (clangTy->isMemberPointerType()) |
| 649 | // TODO: Some member pointers are `noundef`, but it depends on the ABI. For |
| 650 | // now, never mark them. |
| 651 | return false; |
| 652 | if (clangTy->isScalarType()) { |
| 653 | if (const ComplexType *Complex = dyn_cast<ComplexType>(Val&: clangTy)) |
| 654 | return determineNoUndef(clangTy: Complex->getElementType(), types, layout, |
| 655 | argInfo); |
| 656 | return true; |
| 657 | } |
| 658 | if (const VectorType *Vector = dyn_cast<VectorType>(Val&: clangTy)) |
| 659 | return determineNoUndef(clangTy: Vector->getElementType(), types, layout, argInfo); |
| 660 | if (const MatrixType *Matrix = dyn_cast<MatrixType>(Val&: clangTy)) |
| 661 | return determineNoUndef(clangTy: Matrix->getElementType(), types, layout, argInfo); |
| 662 | if (const ArrayType *Array = dyn_cast<ArrayType>(Val&: clangTy)) |
| 663 | return determineNoUndef(clangTy: Array->getElementType(), types, layout, argInfo); |
| 664 | |
| 665 | // TODO: Some structs may be `noundef`, in specific situations. |
| 666 | return false; |
| 667 | } |
| 668 | |
| 669 | void CIRGenModule::constructFunctionReturnAttributes( |
| 670 | const CIRGenFunctionInfo &info, const Decl *targetDecl, bool isThunk, |
| 671 | mlir::NamedAttrList &retAttrs) { |
| 672 | // Collect attributes from arguments and return values. |
| 673 | QualType retTy = info.getReturnType(); |
| 674 | const cir::ABIArgInfo retInfo = info.getReturnInfo(); |
| 675 | const cir::CIRDataLayout &layout = getDataLayout(); |
| 676 | |
| 677 | if (codeGenOpts.EnableNoundefAttrs && hasStrictReturn(retTy, targetDecl) && |
| 678 | !retTy->isVoidType() && |
| 679 | determineNoUndef(clangTy: retTy, types&: getTypes(), layout, argInfo: retInfo)) |
| 680 | retAttrs.set(name: mlir::LLVM::LLVMDialect::getNoUndefAttrName(), |
| 681 | value: mlir::UnitAttr::get(context: &getMLIRContext())); |
| 682 | |
| 683 | if (retTy->hasFloatingRepresentation()) |
| 684 | if (llvm::FPClassTest mask = |
| 685 | CodeGenUtils::getNoFPClassTestMask(LangOpts: getLangOpts())) |
| 686 | retAttrs.set(name: mlir::LLVM::LLVMDialect::getNoFPClassAttrName(), |
| 687 | value: builder.getI64IntegerAttr(value: mask)); |
| 688 | |
| 689 | if (!isThunk) { |
| 690 | // TODO(cir): following comment taken from classic codegen, so if anything |
| 691 | // happens there, we should reflect it here. |
| 692 | // FIXME: fix this properly, https://reviews.llvm.org/D100388 |
| 693 | if (const auto *refTy = retTy->getAs<ReferenceType>()) { |
| 694 | QualType pointeeTy = refTy->getPointeeType(); |
| 695 | if (!pointeeTy->isIncompleteType() && pointeeTy->isConstantSizeType()) |
| 696 | retAttrs.set(name: mlir::LLVM::LLVMDialect::getDereferenceableAttrName(), |
| 697 | value: builder.getI64IntegerAttr( |
| 698 | value: getMinimumObjectSize(ty: pointeeTy).getQuantity())); |
| 699 | |
| 700 | if (getTypes().getTargetAddressSpace(ty: pointeeTy) == 0 && |
| 701 | !codeGenOpts.NullPointerIsValid) |
| 702 | retAttrs.set(name: mlir::LLVM::LLVMDialect::getNonNullAttrName(), |
| 703 | value: mlir::UnitAttr::get(context: &getMLIRContext())); |
| 704 | |
| 705 | if (pointeeTy->isObjectType()) |
| 706 | retAttrs.set(name: mlir::LLVM::LLVMDialect::getAlignAttrName(), |
| 707 | value: builder.getI64IntegerAttr( |
| 708 | value: getNaturalPointeeTypeAlignment(t: retTy).getQuantity())); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | void CIRGenModule::constructFunctionArgumentAttributes( |
| 714 | const CIRGenFunctionInfo &info, GlobalDecl calleeDecl, bool isThunk, |
| 715 | bool attrOnCallSite, llvm::MutableArrayRef<mlir::NamedAttrList> argAttrs) { |
| 716 | assert(!cir::MissingFeatures::abiArgInfo()); |
| 717 | const Decl *targetDecl = calleeDecl.getDecl(); |
| 718 | // TODO(cir): classic codegen does a lot of work here based on the ABIArgInfo |
| 719 | // to set things based on calling convention. |
| 720 | |
| 721 | if (info.isInstanceMethod() && !isThunk) { |
| 722 | QualType thisPtrTy = info.arguments()[0]; |
| 723 | // Member allocation functions are instance methods, but setting attributes |
| 724 | // on them is nonsensical and not correct. Make sure we skip that here. |
| 725 | if (!thisPtrTy->isVoidPointerType()) { |
| 726 | QualType thisTy = thisPtrTy->getPointeeType(); |
| 727 | |
| 728 | if (!codeGenOpts.NullPointerIsValid && |
| 729 | getTypes().getTargetAddressSpace(ty: thisPtrTy) == 0) { |
| 730 | argAttrs[0].set(name: mlir::LLVM::LLVMDialect::getDereferenceableAttrName(), |
| 731 | value: builder.getI64IntegerAttr( |
| 732 | value: getMinimumObjectSize(ty: thisTy).getQuantity())); |
| 733 | argAttrs[0].set(name: mlir::LLVM::LLVMDialect::getNonNullAttrName(), |
| 734 | value: mlir::UnitAttr::get(context: &getMLIRContext())); |
| 735 | } else { |
| 736 | uint64_t bytes = getMinimumObjectSize(ty: thisTy).getQuantity(); |
| 737 | |
| 738 | if (bytes != 0) |
| 739 | argAttrs[0].set( |
| 740 | name: mlir::LLVM::LLVMDialect::getDereferenceableOrNullAttrName(), |
| 741 | value: builder.getI64IntegerAttr(value: bytes)); |
| 742 | } |
| 743 | |
| 744 | argAttrs[0].set( |
| 745 | name: mlir::LLVM::LLVMDialect::getAlignAttrName(), |
| 746 | value: builder.getI64IntegerAttr( |
| 747 | value: getNaturalPointeeTypeAlignment(t: thisPtrTy).getQuantity())); |
| 748 | |
| 749 | // Do not annotate vector deleting destructors with dead_on_return as the |
| 750 | // this pointer in that case points to an array which we cannot |
| 751 | // statically know the size of. Also do not mark deleting destructors |
| 752 | // dead_on_return as then we might delete stores inside of a user-defined |
| 753 | // operator delete implementation if it gets inlined, which would be |
| 754 | // incorrect as the object's lifetime has already ended and the operator |
| 755 | // delete implementation is allowed to manipulate the underlying storage. |
| 756 | const auto *dd = dyn_cast_if_present<CXXDestructorDecl>(Val: targetDecl); |
| 757 | if (dd && codeGenOpts.StrictLifetimes && |
| 758 | calleeDecl.getDtorType() != CXXDtorType::Dtor_VectorDeleting && |
| 759 | calleeDecl.getDtorType() != CXXDtorType::Dtor_Deleting) { |
| 760 | const CXXRecordDecl *classDecl = dd->getParent(); |
| 761 | uint64_t dataSize = astContext.getASTRecordLayout(D: classDecl) |
| 762 | .getDataSize() |
| 763 | .getQuantity(); |
| 764 | // We cannot add dead_on_return if we have virtual base classes because |
| 765 | // they will generally still be live after the base object destructor. |
| 766 | |
| 767 | if (classDecl->getNumVBases() == 0 && dataSize != 0) |
| 768 | argAttrs[0].set(name: mlir::LLVM::LLVMDialect::getDeadOnReturnAttrName(), |
| 769 | value: builder.getI64IntegerAttr(value: dataSize)); |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | // TODO(cir): the logic between 'this', return, and normal arguments hsould |
| 775 | // probably be merged at one point, however the logic is unfortunately mildly |
| 776 | // different between each in classic codegen, so trying to do anything like |
| 777 | // that seems risky at the moment. At one point we should evaluate if at least |
| 778 | // dereferenceable, nonnull, and align can be combined. |
| 779 | const cir::CIRDataLayout &layout = getDataLayout(); |
| 780 | const auto *fd = dyn_cast_or_null<FunctionDecl>(Val: targetDecl); |
| 781 | |
| 782 | // Build a parallel array of ParmVarDecls aligned with argAttrs so we can |
| 783 | // access parameter-level attributes (e.g. restrict, nonnull) without manual |
| 784 | // index arithmetic in the loop. |
| 785 | SmallVector<const ParmVarDecl *> parmDecls; |
| 786 | parmDecls.reserve(N: argAttrs.size()); |
| 787 | if (fd) { |
| 788 | if (info.isInstanceMethod()) |
| 789 | parmDecls.push_back(Elt: nullptr); |
| 790 | parmDecls.insert(I: parmDecls.end(), From: fd->param_begin(), To: fd->param_end()); |
| 791 | } |
| 792 | parmDecls.resize(N: argAttrs.size(), NV: nullptr); |
| 793 | |
| 794 | for (const auto &[argAttrList, argCanType, pvd] : |
| 795 | llvm::zip_equal(t&: argAttrs, u: info.arguments(), args&: parmDecls)) { |
| 796 | assert(!cir::MissingFeatures::abiArgInfo()); |
| 797 | QualType argType = argCanType; |
| 798 | const cir::ABIArgInfo argInfo = cir::ABIArgInfo::getDirect(); |
| 799 | |
| 800 | if (codeGenOpts.EnableNoundefAttrs && |
| 801 | determineNoUndef(clangTy: argType, types&: getTypes(), layout, argInfo)) |
| 802 | argAttrList.set(name: mlir::LLVM::LLVMDialect::getNoUndefAttrName(), |
| 803 | value: mlir::UnitAttr::get(context: &getMLIRContext())); |
| 804 | |
| 805 | assert(!cir::MissingFeatures::abiArgInfo()); |
| 806 | // TODO(cir): there is plenty of other attributes here added due to ABI |
| 807 | // decisions. While these probably won't end up here, we note that the |
| 808 | // classic codegen does it here and perhaps we should pay attention to that. |
| 809 | |
| 810 | if (const auto *refTy = argType->getAs<ReferenceType>()) { |
| 811 | QualType pointeeTy = refTy->getPointeeType(); |
| 812 | if (!pointeeTy->isIncompleteType() && pointeeTy->isConstantSizeType()) |
| 813 | argAttrList.set(name: mlir::LLVM::LLVMDialect::getDereferenceableAttrName(), |
| 814 | value: builder.getI64IntegerAttr( |
| 815 | value: getMinimumObjectSize(ty: pointeeTy).getQuantity())); |
| 816 | if (getTypes().getTargetAddressSpace(ty: pointeeTy) == 0 && |
| 817 | !codeGenOpts.NullPointerIsValid) |
| 818 | argAttrList.set(name: mlir::LLVM::LLVMDialect::getNonNullAttrName(), |
| 819 | value: mlir::UnitAttr::get(context: &getMLIRContext())); |
| 820 | if (pointeeTy->isObjectType()) |
| 821 | argAttrList.set( |
| 822 | name: mlir::LLVM::LLVMDialect::getAlignAttrName(), |
| 823 | value: builder.getI64IntegerAttr( |
| 824 | value: getNaturalPointeeTypeAlignment(t: argType).getQuantity())); |
| 825 | } |
| 826 | |
| 827 | if (argType->hasFloatingRepresentation()) |
| 828 | if (llvm::FPClassTest mask = |
| 829 | CodeGenUtils::getNoFPClassTestMask(LangOpts: getLangOpts())) |
| 830 | argAttrList.set(name: mlir::LLVM::LLVMDialect::getNoFPClassAttrName(), |
| 831 | value: builder.getI64IntegerAttr(value: mask)); |
| 832 | |
| 833 | // restrict -> noalias on definitions only (not call sites). Skip |
| 834 | // builtins: OGCG applies restrict->noalias in EmitFunctionProlog. |
| 835 | if (!attrOnCallSite && pvd && pvd->getType()->isPointerType() && |
| 836 | pvd->getType().isRestrictQualified() && !fd->getBuiltinID()) |
| 837 | argAttrList.set(name: mlir::LLVM::LLVMDialect::getNoAliasAttrName(), |
| 838 | value: mlir::UnitAttr::get(context: &getMLIRContext())); |
| 839 | |
| 840 | // __attribute__((nonnull)) on pointer parameters. Checks both |
| 841 | // per-parameter and function-level nonnull attributes. |
| 842 | if (pvd && argType->isAnyPointerType() && !codeGenOpts.NullPointerIsValid) { |
| 843 | unsigned srcIdx = pvd->getFunctionScopeIndex(); |
| 844 | if (pvd->hasAttr<NonNullAttr>() || |
| 845 | (fd->getAttr<NonNullAttr>() && |
| 846 | fd->getAttr<NonNullAttr>()->isNonNull(IdxAST: srcIdx))) |
| 847 | argAttrList.set(name: mlir::LLVM::LLVMDialect::getNonNullAttrName(), |
| 848 | value: mlir::UnitAttr::get(context: &getMLIRContext())); |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | /// Adds the formal parameters in FPT to the given prefix. If any parameter in |
| 854 | /// FPT has pass_object_size attrs, then we'll add parameters for those, too. |
| 855 | /// TODO(cir): this should be shared with LLVM codegen |
| 856 | static void appendParameterTypes(const CIRGenTypes &cgt, |
| 857 | SmallVectorImpl<CanQualType> &prefix, |
| 858 | CanQual<FunctionProtoType> fpt) { |
| 859 | // Fast path: don't touch param info if we don't need to. |
| 860 | if (!fpt->hasExtParameterInfos()) { |
| 861 | prefix.append(in_start: fpt->param_type_begin(), in_end: fpt->param_type_end()); |
| 862 | return; |
| 863 | } |
| 864 | |
| 865 | // In the vast majority of cases, we'll have precisely fpt->getNumParams() |
| 866 | // parameters; the only thing that can change this is the presence of |
| 867 | // pass_object_size. So, we preallocate for the common case. |
| 868 | prefix.reserve(N: prefix.size() + fpt->getNumParams()); |
| 869 | ArrayRef<FunctionProtoType::ExtParameterInfo> extInfos = |
| 870 | fpt->getExtParameterInfos(); |
| 871 | assert(extInfos.size() == fpt->getNumParams()); |
| 872 | for (auto [paramType, extInfo] : llvm::zip_equal( |
| 873 | t: llvm::make_range(x: fpt->param_type_begin(), y: fpt->param_type_end()), |
| 874 | u&: extInfos)) { |
| 875 | prefix.push_back(Elt: paramType); |
| 876 | if (extInfo.hasPassObjectSize()) |
| 877 | prefix.push_back(Elt: cgt.getASTContext().getCanonicalSizeType()); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | const CIRGenFunctionInfo & |
| 882 | CIRGenTypes::arrangeCXXStructorDeclaration(GlobalDecl gd) { |
| 883 | auto *md = cast<CXXMethodDecl>(Val: gd.getDecl()); |
| 884 | |
| 885 | llvm::SmallVector<CanQualType, 16> argTypes; |
| 886 | argTypes.push_back(Elt: deriveThisType(rd: md->getParent(), md)); |
| 887 | |
| 888 | bool passParams = true; |
| 889 | |
| 890 | if (auto *cd = dyn_cast<CXXConstructorDecl>(Val: md)) { |
| 891 | // A base class inheriting constructor doesn't get forwarded arguments |
| 892 | // needed to construct a virtual base (or base class thereof) |
| 893 | if (auto inherited = cd->getInheritedConstructor()) |
| 894 | passParams = inheritingCtorHasParams(inherited, type: gd.getCtorType()); |
| 895 | } |
| 896 | |
| 897 | CanQual<FunctionProtoType> fpt = CodeGenUtils::getFormalType(MD: md); |
| 898 | |
| 899 | if (passParams) |
| 900 | appendParameterTypes(cgt: *this, prefix&: argTypes, fpt); |
| 901 | |
| 902 | // The structor signature may include implicit parameters. |
| 903 | [[maybe_unused]] CIRGenCXXABI::AddedStructorArgCounts addedArgs = |
| 904 | theCXXABI.buildStructorSignature(gd, argTys&: argTypes); |
| 905 | assert(!cir::MissingFeatures::opCallCIRGenFuncInfoExtParamInfo()); |
| 906 | |
| 907 | RequiredArgs required = |
| 908 | (passParams && md->isVariadic() ? RequiredArgs(argTypes.size()) |
| 909 | : RequiredArgs::All); |
| 910 | |
| 911 | CanQualType resultType = theCXXABI.hasThisReturn(gd) ? argTypes.front() |
| 912 | : theCXXABI.hasMostDerivedReturn(gd) |
| 913 | ? astContext.VoidPtrTy |
| 914 | : astContext.VoidTy; |
| 915 | |
| 916 | assert(!cir::MissingFeatures::opCallCIRGenFuncInfoExtParamInfo()); |
| 917 | assert(!cir::MissingFeatures::opCallFnInfoOpts()); |
| 918 | |
| 919 | return arrangeCIRFunctionInfo(returnType: resultType, /*isInstanceMethod=*/true, argTypes, |
| 920 | info: fpt->getExtInfo(), required); |
| 921 | } |
| 922 | |
| 923 | /// Derives the 'this' type for CIRGen purposes, i.e. ignoring method CVR |
| 924 | /// qualification. Either or both of `rd` and `md` may be null. A null `rd` |
| 925 | /// indicates that there is no meaningful 'this' type, and a null `md` can occur |
| 926 | /// when calling a method pointer. |
| 927 | CanQualType CIRGenTypes::deriveThisType(const CXXRecordDecl *rd, |
| 928 | const CXXMethodDecl *md) { |
| 929 | CanQualType recTy; |
| 930 | if (rd) { |
| 931 | recTy = getASTContext().getCanonicalTagType(TD: rd); |
| 932 | } else { |
| 933 | // This can happen with the MS ABI. It shouldn't need anything more than |
| 934 | // setting recTy to VoidTy here, but we're flagging it for now because we |
| 935 | // don't have the full handling implemented. |
| 936 | cgm.errorNYI(feature: "deriveThisType: no record decl" ); |
| 937 | recTy = getASTContext().VoidTy; |
| 938 | } |
| 939 | |
| 940 | if (md) |
| 941 | recTy = CanQualType::CreateUnsafe(Other: getASTContext().getAddrSpaceQualType( |
| 942 | T: recTy, AddressSpace: md->getMethodQualifiers().getAddressSpace())); |
| 943 | return getASTContext().getPointerType(T: recTy); |
| 944 | } |
| 945 | |
| 946 | /// Arrange the CIR function layout for a value of the given function type, on |
| 947 | /// top of any implicit parameters already stored. |
| 948 | static const CIRGenFunctionInfo & |
| 949 | arrangeCIRFunctionInfo(CIRGenTypes &cgt, bool instanceMethod, |
| 950 | SmallVectorImpl<CanQualType> &prefix, |
| 951 | CanQual<FunctionProtoType> fpt) { |
| 952 | assert(!cir::MissingFeatures::opCallFnInfoOpts()); |
| 953 | RequiredArgs required = |
| 954 | RequiredArgs::getFromProtoWithExtraSlots(prototype: fpt, additional: prefix.size()); |
| 955 | assert(!cir::MissingFeatures::opCallExtParameterInfo()); |
| 956 | appendParameterTypes(cgt, prefix, fpt); |
| 957 | CanQualType resultType = fpt->getReturnType().getUnqualifiedType(); |
| 958 | return cgt.arrangeCIRFunctionInfo(returnType: resultType, isInstanceMethod: instanceMethod, argTypes: prefix, |
| 959 | info: fpt->getExtInfo(), required); |
| 960 | } |
| 961 | |
| 962 | void CIRGenFunction::emitDelegateCallArg(CallArgList &args, |
| 963 | const VarDecl *param, |
| 964 | SourceLocation loc) { |
| 965 | // StartFunction converted the ABI-lowered parameter(s) into a local alloca. |
| 966 | // We need to turn that into an r-value suitable for emitCall |
| 967 | Address local = getAddrOfLocalVar(vd: param); |
| 968 | |
| 969 | QualType type = param->getType(); |
| 970 | |
| 971 | // GetAddrOfLocalVar returns a pointer-to-pointer for references, but the |
| 972 | // argument needs to be the original pointer. |
| 973 | if (type->isReferenceType()) { |
| 974 | args.add( |
| 975 | rvalue: RValue::get(v: builder.createLoad(loc: getLoc(srcLoc: param->getSourceRange()), addr: local)), |
| 976 | type); |
| 977 | } else if (getLangOpts().ObjCAutoRefCount) { |
| 978 | cgm.errorNYI(param->getSourceRange(), |
| 979 | "emitDelegateCallArg: ObjCAutoRefCount" ); |
| 980 | // For the most part, we just need to load the alloca, except that aggregate |
| 981 | // r-values are actually pointers to temporaries. |
| 982 | } else { |
| 983 | args.add(rvalue: convertTempToRValue(addr: local, type, loc), type); |
| 984 | } |
| 985 | |
| 986 | // Deactivate the cleanup for the callee-destructed param that was pushed. |
| 987 | assert(!cir::MissingFeatures::thunks()); |
| 988 | if (type->isRecordType() && |
| 989 | type->castAsRecordDecl()->isParamDestroyedInCallee() && |
| 990 | param->needsDestruction(Ctx: getContext())) { |
| 991 | cgm.errorNYI(param->getSourceRange(), |
| 992 | "emitDelegateCallArg: callee-destructed param" ); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | static const CIRGenFunctionInfo & |
| 997 | arrangeFreeFunctionLikeCall(CIRGenTypes &cgt, CIRGenModule &cgm, |
| 998 | const CallArgList &args, |
| 999 | const FunctionType *fnType) { |
| 1000 | |
| 1001 | RequiredArgs required = RequiredArgs::All; |
| 1002 | |
| 1003 | if (const auto *proto = dyn_cast<FunctionProtoType>(Val: fnType)) { |
| 1004 | // A free function call has no extra prefix arguments. Note that |
| 1005 | // getFromProtoWithExtraSlots already accounts for the prototype's |
| 1006 | // pass_object_size parameters; adding them here too would double-count |
| 1007 | // them and make the signature claim more required arguments than `args` |
| 1008 | // actually holds. |
| 1009 | if (proto->isVariadic()) |
| 1010 | required = RequiredArgs::getFromProtoWithExtraSlots(prototype: proto, additional: 0); |
| 1011 | } else if (cgm.getTargetCIRGenInfo().isNoProtoCallVariadic( |
| 1012 | fnType: cast<FunctionNoProtoType>(Val: fnType))) |
| 1013 | cgm.errorNYI(feature: "call to function without a prototype" ); |
| 1014 | |
| 1015 | SmallVector<CanQualType, 16> argTypes; |
| 1016 | for (const CallArg &arg : args) |
| 1017 | argTypes.push_back(Elt: cgt.getASTContext().getCanonicalParamType(T: arg.ty)); |
| 1018 | |
| 1019 | CanQualType retType = fnType->getReturnType()->getCanonicalTypeUnqualified(); |
| 1020 | |
| 1021 | assert(!cir::MissingFeatures::opCallFnInfoOpts()); |
| 1022 | return cgt.arrangeCIRFunctionInfo(returnType: retType, /*isInstanceMethod=*/false, |
| 1023 | argTypes, info: fnType->getExtInfo(), required); |
| 1024 | } |
| 1025 | |
| 1026 | /// Arrange a call to a C++ method, passing the given arguments. |
| 1027 | /// |
| 1028 | /// extraPrefixArgs is the number of ABI-specific args passed after the `this` |
| 1029 | /// parameter. |
| 1030 | /// passProtoArgs indicates whether `args` has args for the parameters in the |
| 1031 | /// given CXXConstructorDecl. |
| 1032 | const CIRGenFunctionInfo &CIRGenTypes::arrangeCXXConstructorCall( |
| 1033 | const CallArgList &args, const CXXConstructorDecl *d, CXXCtorType ctorKind, |
| 1034 | unsigned , unsigned , bool passProtoArgs) { |
| 1035 | |
| 1036 | // FIXME: Kill copy. |
| 1037 | llvm::SmallVector<CanQualType, 16> argTypes; |
| 1038 | for (const auto &arg : args) |
| 1039 | argTypes.push_back(Elt: astContext.getCanonicalParamType(T: arg.ty)); |
| 1040 | |
| 1041 | // +1 for implicit this, which should always be args[0] |
| 1042 | unsigned totalPrefixArgs = 1 + extraPrefixArgs; |
| 1043 | |
| 1044 | CanQual<FunctionProtoType> fpt = CodeGenUtils::getFormalType(MD: d); |
| 1045 | RequiredArgs required = passProtoArgs |
| 1046 | ? RequiredArgs::getFromProtoWithExtraSlots( |
| 1047 | prototype: fpt, additional: totalPrefixArgs + extraSuffixArgs) |
| 1048 | : RequiredArgs::All; |
| 1049 | |
| 1050 | GlobalDecl gd(d, ctorKind); |
| 1051 | |
| 1052 | // TODO: hasMostDerivedReturn isn't implemented or tested yet, but the below |
| 1053 | // init of 'resultType' should be enough once it is thoroughly |
| 1054 | // tested/implemented everywhere else. |
| 1055 | if (theCXXABI.hasMostDerivedReturn(gd)) |
| 1056 | cgm.errorNYI(d->getSourceRange(), |
| 1057 | "arrangeCXXConstructorCall: hasMostDerivedReturn" ); |
| 1058 | |
| 1059 | CanQualType resultType = theCXXABI.hasThisReturn(gd) ? argTypes.front() |
| 1060 | : theCXXABI.hasMostDerivedReturn(gd) |
| 1061 | ? astContext.VoidPtrTy |
| 1062 | : astContext.VoidTy; |
| 1063 | |
| 1064 | assert(!cir::MissingFeatures::opCallFnInfoOpts()); |
| 1065 | assert(!cir::MissingFeatures::opCallCIRGenFuncInfoExtParamInfo()); |
| 1066 | |
| 1067 | return arrangeCIRFunctionInfo(returnType: resultType, /*isInstanceMethod=*/true, argTypes, |
| 1068 | info: fpt->getExtInfo(), required); |
| 1069 | } |
| 1070 | |
| 1071 | /// Arrange a call to a C++ method, passing the given arguments. |
| 1072 | /// |
| 1073 | /// numPrefixArgs is the number of the ABI-specific prefix arguments we have. It |
| 1074 | /// does not count `this`. |
| 1075 | const CIRGenFunctionInfo &CIRGenTypes::arrangeCXXMethodCall( |
| 1076 | const CallArgList &args, const FunctionProtoType *proto, |
| 1077 | RequiredArgs required, unsigned numPrefixArgs) { |
| 1078 | assert(!cir::MissingFeatures::opCallExtParameterInfo()); |
| 1079 | assert(numPrefixArgs + 1 <= args.size() && |
| 1080 | "Emitting a call with less args than the required prefix?" ); |
| 1081 | |
| 1082 | // FIXME: Kill copy. |
| 1083 | llvm::SmallVector<CanQualType, 16> argTypes; |
| 1084 | for (const CallArg &arg : args) |
| 1085 | argTypes.push_back(Elt: astContext.getCanonicalParamType(T: arg.ty)); |
| 1086 | |
| 1087 | assert(!cir::MissingFeatures::opCallFnInfoOpts()); |
| 1088 | return arrangeCIRFunctionInfo( |
| 1089 | returnType: proto->getReturnType()->getCanonicalTypeUnqualified(), |
| 1090 | /*isInstanceMethod=*/true, argTypes, info: proto->getExtInfo(), required); |
| 1091 | } |
| 1092 | |
| 1093 | const CIRGenFunctionInfo & |
| 1094 | CIRGenTypes::arrangeFreeFunctionCall(const CallArgList &args, |
| 1095 | const FunctionType *fnType) { |
| 1096 | return arrangeFreeFunctionLikeCall(cgt&: *this, cgm, args, fnType); |
| 1097 | } |
| 1098 | |
| 1099 | const CIRGenFunctionInfo & |
| 1100 | CIRGenTypes::arrangeBuiltinFunctionCall(QualType resultType, |
| 1101 | const CallArgList &args) { |
| 1102 | llvm::SmallVector<CanQualType, 16> argTypes; |
| 1103 | for (const CallArg &arg : args) |
| 1104 | argTypes.push_back(Elt: astContext.getCanonicalParamType(T: arg.ty)); |
| 1105 | |
| 1106 | CanQualType retType = resultType->getCanonicalTypeUnqualified(); |
| 1107 | return arrangeCIRFunctionInfo(returnType: retType, /*isInstanceMethod=*/false, argTypes, |
| 1108 | info: FunctionType::ExtInfo(), required: RequiredArgs::All); |
| 1109 | } |
| 1110 | |
| 1111 | /// Set calling convention for CUDA/HIP kernel. |
| 1112 | static void setCUDAKernelCallingConvention(CanQualType &funcTy, |
| 1113 | CIRGenModule &cgm, |
| 1114 | const FunctionDecl *fd) { |
| 1115 | if (fd->hasAttr<CUDAGlobalAttr>()) { |
| 1116 | const FunctionType *ft = funcTy->getAs<FunctionType>(); |
| 1117 | cgm.getTargetCIRGenInfo().setCUDAKernelCallingConvention(ft); |
| 1118 | funcTy = ft->getCanonicalTypeUnqualified(); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | /// Arrange the argument and result information for a declaration or definition |
| 1123 | /// of the given C++ non-static member function. The member function must be an |
| 1124 | /// ordinary function, i.e. not a constructor or destructor. |
| 1125 | const CIRGenFunctionInfo & |
| 1126 | CIRGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *md) { |
| 1127 | assert(!isa<CXXConstructorDecl>(md) && "wrong method for constructors!" ); |
| 1128 | assert(!isa<CXXDestructorDecl>(md) && "wrong method for destructors!" ); |
| 1129 | |
| 1130 | CanQualType funcTy = md->getType()->getCanonicalTypeUnqualified(); |
| 1131 | setCUDAKernelCallingConvention(funcTy, cgm, fd: md); |
| 1132 | auto prototype = funcTy.getAs<FunctionProtoType>(); |
| 1133 | |
| 1134 | // Mirrors classic CodeGen's check at CGCall.cpp. C++23 explicit-object |
| 1135 | // member functions (P0847R7, `void f(this Self&&)`) do not receive an |
| 1136 | // implicit `this`; the explicit object parameter takes its place at the |
| 1137 | // AST level and appears as the first parameter of the FunctionProtoType. |
| 1138 | // Arrange them as free functions so we don't prepend a stale implicit |
| 1139 | // `this` to the parameter list, which would produce a CIRGenFunctionInfo |
| 1140 | // with one more argument than the matching cir.func type and trip the |
| 1141 | // assertion in setArgAttrs. |
| 1142 | if (md->isImplicitObjectMemberFunction()) { |
| 1143 | // The abstract case is perfectly fine. |
| 1144 | auto *thisType = theCXXABI.getThisArgumentTypeForMethod(md); |
| 1145 | return arrangeCXXMethodType(rd: thisType, ftp: prototype.getTypePtr(), md); |
| 1146 | } |
| 1147 | |
| 1148 | return arrangeFreeFunctionType(fpt: prototype); |
| 1149 | } |
| 1150 | |
| 1151 | /// Arrange the argument and result information for a call to an unknown C++ |
| 1152 | /// non-static member function of the given abstract type. (A null RD means we |
| 1153 | /// don't have any meaningful "this" argument type, so fall back to a generic |
| 1154 | /// pointer type). The member fucntion must be an ordinary function, i.e. not a |
| 1155 | /// constructor or destructor. |
| 1156 | const CIRGenFunctionInfo & |
| 1157 | CIRGenTypes::arrangeCXXMethodType(const CXXRecordDecl *rd, |
| 1158 | const FunctionProtoType *fpt, |
| 1159 | const CXXMethodDecl *md) { |
| 1160 | llvm::SmallVector<CanQualType, 16> argTypes; |
| 1161 | |
| 1162 | // Add the 'this' pointer. |
| 1163 | argTypes.push_back(Elt: deriveThisType(rd, md)); |
| 1164 | |
| 1165 | assert(!cir::MissingFeatures::opCallFnInfoOpts()); |
| 1166 | return ::arrangeCIRFunctionInfo( |
| 1167 | cgt&: *this, /*isInstanceMethod=*/instanceMethod: true, prefix&: argTypes, |
| 1168 | fpt: fpt->getCanonicalTypeUnqualified().getAs<FunctionProtoType>()); |
| 1169 | } |
| 1170 | |
| 1171 | /// Arrange the argument and result information for the declaration or |
| 1172 | /// definition of the given function. |
| 1173 | const CIRGenFunctionInfo & |
| 1174 | CIRGenTypes::arrangeFunctionDeclaration(const FunctionDecl *fd) { |
| 1175 | if (const auto *md = dyn_cast<CXXMethodDecl>(Val: fd)) |
| 1176 | if (md->isInstance()) |
| 1177 | return arrangeCXXMethodDeclaration(md); |
| 1178 | |
| 1179 | CanQualType funcTy = fd->getType()->getCanonicalTypeUnqualified(); |
| 1180 | |
| 1181 | assert(isa<FunctionType>(funcTy)); |
| 1182 | setCUDAKernelCallingConvention(funcTy, cgm, fd); |
| 1183 | |
| 1184 | // When declaring a function without a prototype, always use a non-variadic |
| 1185 | // type. |
| 1186 | if (CanQual<FunctionNoProtoType> noProto = |
| 1187 | funcTy.getAs<FunctionNoProtoType>()) { |
| 1188 | assert(!cir::MissingFeatures::opCallCIRGenFuncInfoExtParamInfo()); |
| 1189 | assert(!cir::MissingFeatures::opCallFnInfoOpts()); |
| 1190 | return arrangeCIRFunctionInfo(returnType: noProto->getReturnType(), |
| 1191 | /*isInstanceMethod=*/false, argTypes: {}, |
| 1192 | info: noProto->getExtInfo(), required: RequiredArgs::All); |
| 1193 | } |
| 1194 | |
| 1195 | return arrangeFreeFunctionType(fpt: funcTy.castAs<FunctionProtoType>()); |
| 1196 | } |
| 1197 | |
| 1198 | RValue CallArg::getRValue(CIRGenFunction &cgf, mlir::Location loc) const { |
| 1199 | if (!hasLV) |
| 1200 | return rv; |
| 1201 | LValue copy = cgf.makeAddrLValue(addr: cgf.createMemTemp(t: ty, loc), ty); |
| 1202 | cgf.emitAggregateCopy(dest: copy, src: lv, eltTy: ty, mayOverlap: AggValueSlot::DoesNotOverlap, |
| 1203 | isVolatile: lv.isVolatile()); |
| 1204 | isUsed = true; |
| 1205 | return RValue::getAggregate(addr: copy.getAddress()); |
| 1206 | } |
| 1207 | |
| 1208 | void CIRGenFunction::emitNonNullArgCheck(RValue rv, QualType argType, |
| 1209 | SourceLocation argLoc, |
| 1210 | AbstractCallee ac, unsigned paramNum) { |
| 1211 | if (!ac.getDecl() || !(sanOpts.has(K: SanitizerKind::NonnullAttribute) || |
| 1212 | sanOpts.has(K: SanitizerKind::NullabilityArg))) |
| 1213 | return; |
| 1214 | cgm.errorNYI(feature: "non-null arg check is NYI" ); |
| 1215 | } |
| 1216 | |
| 1217 | static cir::CIRCallOpInterface emitCallLikeOp( |
| 1218 | CIRGenFunction &cgf, mlir::Location callLoc, cir::FuncType indirectFuncTy, |
| 1219 | mlir::Value indirectFuncVal, cir::FuncOp directFuncOp, |
| 1220 | const SmallVectorImpl<mlir::Value> &cirCallArgs, bool isInvoke, |
| 1221 | const mlir::NamedAttrList &attrs, |
| 1222 | llvm::ArrayRef<mlir::NamedAttrList> argAttrs, |
| 1223 | const mlir::NamedAttrList &retAttrs, cir::CallingConv callingConv) { |
| 1224 | CIRGenBuilderTy &builder = cgf.getBuilder(); |
| 1225 | |
| 1226 | assert(!cir::MissingFeatures::opCallSurroundingTry()); |
| 1227 | |
| 1228 | assert(builder.getInsertionBlock() && "expected valid basic block" ); |
| 1229 | |
| 1230 | cir::CallOp op; |
| 1231 | if (indirectFuncTy) { |
| 1232 | op = builder.createIndirectCallOp(loc: callLoc, indirectTarget: indirectFuncVal, funcType: indirectFuncTy, |
| 1233 | operands: cirCallArgs, attrs, argAttrs, resAttrs: retAttrs, |
| 1234 | callingConv); |
| 1235 | } else { |
| 1236 | op = builder.createCallOp(loc: callLoc, callee: directFuncOp, operands: cirCallArgs, attrs, |
| 1237 | argAttrs, resAttrs: retAttrs, callingConv); |
| 1238 | } |
| 1239 | |
| 1240 | return op; |
| 1241 | } |
| 1242 | |
| 1243 | const CIRGenFunctionInfo & |
| 1244 | CIRGenTypes::arrangeFreeFunctionType(CanQual<FunctionProtoType> fpt) { |
| 1245 | SmallVector<CanQualType, 16> argTypes; |
| 1246 | assert(!cir::MissingFeatures::opCallFnInfoOpts()); |
| 1247 | return ::arrangeCIRFunctionInfo(cgt&: *this, /*isInstanceMethod=*/instanceMethod: false, prefix&: argTypes, |
| 1248 | fpt); |
| 1249 | } |
| 1250 | |
| 1251 | const CIRGenFunctionInfo & |
| 1252 | CIRGenTypes::arrangeFreeFunctionType(CanQual<FunctionNoProtoType> fnpt) { |
| 1253 | CanQualType resultType = fnpt->getReturnType().getUnqualifiedType(); |
| 1254 | assert(!cir::MissingFeatures::opCallFnInfoOpts()); |
| 1255 | return arrangeCIRFunctionInfo(returnType: resultType, /*isInstanceMethod=*/false, argTypes: {}, |
| 1256 | info: fnpt->getExtInfo(), required: RequiredArgs(0)); |
| 1257 | } |
| 1258 | |
| 1259 | RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &funcInfo, |
| 1260 | const CIRGenCallee &callee, |
| 1261 | ReturnValueSlot returnValue, |
| 1262 | const CallArgList &args, |
| 1263 | cir::CIRCallOpInterface *callOp, |
| 1264 | bool isMustTail, SourceRange clangLoc) { |
| 1265 | QualType retTy = funcInfo.getReturnType(); |
| 1266 | cir::FuncType cirFuncTy = getTypes().getFunctionType(info: funcInfo); |
| 1267 | mlir::Location loc = getLoc(srcLoc: clangLoc); |
| 1268 | |
| 1269 | SmallVector<mlir::Value, 16> cirCallArgs(args.size()); |
| 1270 | |
| 1271 | const Decl *targetDecl = callee.getAbstractInfo().getCalleeDecl().getDecl(); |
| 1272 | |
| 1273 | if (const FunctionDecl *fd = dyn_cast_or_null<FunctionDecl>(Val: targetDecl)) { |
| 1274 | // We can only guarantee that a function is called from the correct |
| 1275 | // context/function based on the appropriate target attributes, |
| 1276 | // so only check in the case where we have both always_inline and target |
| 1277 | // since otherwise we could be making a conditional call after a check for |
| 1278 | // the proper cpu features (and it won't cause code generation issues due to |
| 1279 | // function based code generation). |
| 1280 | if ((targetDecl->hasAttr<AlwaysInlineAttr>() && |
| 1281 | (targetDecl->hasAttr<TargetAttr>() || |
| 1282 | (curFuncDecl && curFuncDecl->hasAttr<TargetAttr>()))) || |
| 1283 | (curFuncDecl && curFuncDecl->hasAttr<FlattenAttr>() && |
| 1284 | (curFuncDecl->hasAttr<TargetAttr>() || |
| 1285 | targetDecl->hasAttr<TargetAttr>()))) |
| 1286 | checkTargetFeatures(loc: clangLoc.getBegin(), targetDecl: fd); |
| 1287 | } |
| 1288 | |
| 1289 | const FunctionDecl *callerDecl = dyn_cast_or_null<FunctionDecl>(Val: curCodeDecl); |
| 1290 | const FunctionDecl *calleeDecl = dyn_cast_or_null<FunctionDecl>(Val: targetDecl); |
| 1291 | |
| 1292 | assert(!cir::MissingFeatures::emitLifetimeMarkers()); |
| 1293 | |
| 1294 | // Translate all of the arguments as necessary to match the CIR lowering. |
| 1295 | for (auto [argNo, arg, canQualArgType] : |
| 1296 | llvm::enumerate(First: args, Rest: funcInfo.argTypes())) { |
| 1297 | |
| 1298 | // Insert a padding argument to ensure proper alignment. |
| 1299 | assert(!cir::MissingFeatures::opCallPaddingArgs()); |
| 1300 | |
| 1301 | mlir::Type argType = convertType(t: canQualArgType); |
| 1302 | if (!mlir::isa<cir::RecordType>(Val: argType) && |
| 1303 | !mlir::isa<cir::ComplexType>(Val: argType)) { |
| 1304 | mlir::Value v; |
| 1305 | if (arg.isAggregate()) |
| 1306 | cgm.errorNYI(loc, feature: "emitCall: aggregate call argument" ); |
| 1307 | v = arg.getKnownRValue().getValue(); |
| 1308 | |
| 1309 | // We might have to widen integers, but we should never truncate. |
| 1310 | if (argType != v.getType() && mlir::isa<cir::IntType>(Val: v.getType())) |
| 1311 | cgm.errorNYI(loc, feature: "emitCall: widening integer call argument" ); |
| 1312 | |
| 1313 | // If we have a pointer argument and there's an address space mismatch, |
| 1314 | // insert an address_space cast to match the expected function signature. |
| 1315 | if (argType != v.getType()) { |
| 1316 | auto argPtrTy = mlir::dyn_cast<cir::PointerType>(Val&: argType); |
| 1317 | auto vPtrTy = mlir::dyn_cast<cir::PointerType>(Val: v.getType()); |
| 1318 | if (argPtrTy && vPtrTy && |
| 1319 | argPtrTy.getPointee() == vPtrTy.getPointee() && |
| 1320 | argPtrTy.getAddrSpace() != vPtrTy.getAddrSpace()) { |
| 1321 | v = performAddrSpaceCast(v, destTy: argPtrTy); |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | // If the argument doesn't match, perform a bitcast to coerce it. This |
| 1326 | // can happen due to trivial type mismatches. |
| 1327 | // TODO(cir): When getFunctionType is added, assert that this isn't |
| 1328 | // needed. |
| 1329 | assert(!cir::MissingFeatures::opCallBitcastArg()); |
| 1330 | cirCallArgs[argNo] = v; |
| 1331 | } else { |
| 1332 | Address src = Address::invalid(); |
| 1333 | if (!arg.isAggregate()) { |
| 1334 | src = createMemTemp(t: arg.ty, loc, name: "coerce" ); |
| 1335 | arg.copyInto(cgf&: *this, addr: src, loc); |
| 1336 | } else { |
| 1337 | src = arg.hasLValue() ? arg.getKnownLValue().getAddress() |
| 1338 | : arg.getKnownRValue().getAggregateAddress(); |
| 1339 | } |
| 1340 | |
| 1341 | // Fast-isel and the optimizer generally like scalar values better than |
| 1342 | // FCAs, so we flatten them if this is safe to do for this argument. |
| 1343 | mlir::Type srcTy = src.getElementType(); |
| 1344 | // FIXME(cir): get proper location for each argument. |
| 1345 | mlir::Location argLoc = loc; |
| 1346 | |
| 1347 | // If the source type is smaller than the destination type of the |
| 1348 | // coerce-to logic, copy the source value into a temp alloca the size |
| 1349 | // of the destination type to allow loading all of it. The bits past |
| 1350 | // the source value are left undef. |
| 1351 | // FIXME(cir): add data layout info and compare sizes instead of |
| 1352 | // matching the types. |
| 1353 | // |
| 1354 | // uint64_t SrcSize = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
| 1355 | // uint64_t DstSize = CGM.getDataLayout().getTypeAllocSize(STy); |
| 1356 | // if (SrcSize < DstSize) { |
| 1357 | assert(!cir::MissingFeatures::dataLayoutTypeAllocSize()); |
| 1358 | if (srcTy != argType) { |
| 1359 | cgm.errorNYI(loc, feature: "emitCall: source type does not match argument type" ); |
| 1360 | } else { |
| 1361 | // FIXME(cir): this currently only runs when the types are exactly the |
| 1362 | // same, but should be when alloc sizes are the same, fix this as soon |
| 1363 | // as datalayout gets introduced. |
| 1364 | assert(!cir::MissingFeatures::dataLayoutTypeAllocSize()); |
| 1365 | } |
| 1366 | |
| 1367 | // assert(NumCIRArgs == STy.getMembers().size()); |
| 1368 | // In LLVMGen: Still only pass the struct without any gaps but mark it |
| 1369 | // as such somehow. |
| 1370 | // |
| 1371 | // In CIRGen: Emit a load from the "whole" struct, |
| 1372 | // which shall be broken later by some lowering step into multiple |
| 1373 | // loads. |
| 1374 | assert(!cir::MissingFeatures::lowerAggregateLoadStore()); |
| 1375 | cirCallArgs[argNo] = builder.createLoad(loc: argLoc, addr: src); |
| 1376 | } |
| 1377 | |
| 1378 | if (CodeGenUtils::isArgumentMaybeUndef( |
| 1379 | TargetDecl: targetDecl, NumRequiredArgs: funcInfo.getNumRequiredArgs(), ArgNo: argNo)) |
| 1380 | cirCallArgs[argNo] = |
| 1381 | cir::FreezeOp::create(builder, location: loc, input: cirCallArgs[argNo]); |
| 1382 | } |
| 1383 | |
| 1384 | const CIRGenCallee &concreteCallee = callee.prepareConcreteCallee(cgf&: *this); |
| 1385 | mlir::Operation *calleePtr = concreteCallee.getFunctionPointer(); |
| 1386 | |
| 1387 | assert(!cir::MissingFeatures::opCallInAlloca()); |
| 1388 | |
| 1389 | mlir::NamedAttrList attrs; |
| 1390 | std::vector<mlir::NamedAttrList> argAttrs(funcInfo.arguments().size()); |
| 1391 | mlir::NamedAttrList retAttrs; |
| 1392 | StringRef funcName; |
| 1393 | if (auto calleeFuncOp = dyn_cast<cir::FuncOp>(Val: calleePtr)) |
| 1394 | funcName = calleeFuncOp.getName(); |
| 1395 | |
| 1396 | assert(!cir::MissingFeatures::opCallAttrs()); |
| 1397 | cir::CallingConv callingConv; |
| 1398 | cgm.constructAttributeList(name: funcName, info: funcInfo, calleeInfo: callee.getAbstractInfo(), |
| 1399 | attrs, argAttrs, retAttrs, callingConv, |
| 1400 | /*attrOnCallSite=*/true, /*isThunk=*/false); |
| 1401 | |
| 1402 | // TODO(cir): Classic CodeGen redirects calls to OpenCL kernels to a |
| 1403 | // non-kernel stub (__clang_ocl_kern_imp_*), since kernel calling conventions |
| 1404 | // do not permit calls. |
| 1405 | if (callingConv == cir::CallingConv::SpirKernel || |
| 1406 | callingConv == cir::CallingConv::AMDGPUKernel || |
| 1407 | callingConv == cir::CallingConv::PTXKernel) |
| 1408 | cgm.errorNYI(loc, feature: "emitCall: call to kernel function" ); |
| 1409 | |
| 1410 | auto resolvedFuncOpFromGlobal = [&](mlir::Operation *op) -> cir::FuncOp { |
| 1411 | if (auto fnOp = dyn_cast<cir::FuncOp>(Val: op)) |
| 1412 | return fnOp; |
| 1413 | if (auto getGlobalOp = dyn_cast<cir::GetGlobalOp>(Val: op)) { |
| 1414 | // FIXME(cir): This peephole optimization avoids indirect calls for |
| 1415 | // builtins. This should be fixed in the builtin declaration instead by |
| 1416 | // not emitting an unecessary get_global in the first place. However, |
| 1417 | // this is also used for no-prototype functions. |
| 1418 | mlir::Operation *globalOp = cgm.getGlobalValue(ref: getGlobalOp.getName()); |
| 1419 | assert(globalOp && "undefined global function" ); |
| 1420 | return cast<cir::FuncOp>(Val: globalOp); |
| 1421 | } |
| 1422 | return nullptr; |
| 1423 | }; |
| 1424 | |
| 1425 | cir::FuncType indirectFuncTy; |
| 1426 | mlir::Value indirectFuncVal; |
| 1427 | cir::FuncOp directFuncOp; |
| 1428 | |
| 1429 | // If the callee resolves to a FuncOp whose stored signature differs from |
| 1430 | // this call site's expected signature, the CIR verifier would reject the |
| 1431 | // mismatched types. This happens, for example, when two declarations share a |
| 1432 | // mangled name via __asm__ renaming (glibc's __REDIRECT_NTH pattern) but |
| 1433 | // disagree about a struct argument type. If that happens, we demote the |
| 1434 | // direct call to an indirect call through a function-pointer bitcast typed |
| 1435 | // at the call site. |
| 1436 | if (cir::FuncOp candidate = resolvedFuncOpFromGlobal(calleePtr)) { |
| 1437 | if (candidate.getFunctionType() == cirFuncTy) { |
| 1438 | directFuncOp = candidate; |
| 1439 | } else { |
| 1440 | mlir::Value addr = cir::GetGlobalOp::create( |
| 1441 | builder, location: loc, addr: cir::PointerType::get(pointee: candidate.getFunctionType()), |
| 1442 | name: candidate.getSymName()); |
| 1443 | indirectFuncTy = cirFuncTy; |
| 1444 | indirectFuncVal = |
| 1445 | builder.createBitcast(src: addr, newTy: cir::PointerType::get(pointee: cirFuncTy)); |
| 1446 | } |
| 1447 | } else { |
| 1448 | [[maybe_unused]] mlir::ValueTypeRange<mlir::ResultRange> resultTypes = |
| 1449 | calleePtr->getResultTypes(); |
| 1450 | [[maybe_unused]] auto funcPtrTy = |
| 1451 | mlir::dyn_cast<cir::PointerType>(Val: resultTypes.front()); |
| 1452 | assert(funcPtrTy && mlir::isa<cir::FuncType>(funcPtrTy.getPointee()) && |
| 1453 | "expected pointer to function" ); |
| 1454 | |
| 1455 | indirectFuncTy = cirFuncTy; |
| 1456 | indirectFuncVal = calleePtr->getResult(idx: 0); |
| 1457 | } |
| 1458 | |
| 1459 | assert(!cir::MissingFeatures::msvcCXXPersonality()); |
| 1460 | assert(!cir::MissingFeatures::functionUsesSEHTry()); |
| 1461 | assert(!cir::MissingFeatures::nothrowAttr()); |
| 1462 | |
| 1463 | bool cannotThrow = attrs.getNamed(name: "nothrow" ).has_value(); |
| 1464 | bool isInvoke = !cannotThrow && isCatchOrCleanupRequired(); |
| 1465 | |
| 1466 | mlir::Location callLoc = loc; |
| 1467 | cir::CIRCallOpInterface theCall = emitCallLikeOp( |
| 1468 | cgf&: *this, callLoc: loc, indirectFuncTy, indirectFuncVal, directFuncOp, cirCallArgs, |
| 1469 | isInvoke, attrs, argAttrs, retAttrs, callingConv); |
| 1470 | |
| 1471 | if (callOp) |
| 1472 | *callOp = theCall; |
| 1473 | |
| 1474 | // Add srcloc if we have [[gnu::error/warning]] or ShowInliningChain. |
| 1475 | if (calleeDecl) { |
| 1476 | bool needSrcLoc = calleeDecl->hasAttr<ErrorAttr>(); |
| 1477 | if (!needSrcLoc && cgm.getCodeGenOpts().ShowInliningChain) |
| 1478 | needSrcLoc = calleeDecl->isInlined() || |
| 1479 | calleeDecl->hasAttr<AlwaysInlineAttr>() || |
| 1480 | calleeDecl->getStorageClass() == SC_Static || |
| 1481 | calleeDecl->isInAnonymousNamespace(); |
| 1482 | if (needSrcLoc) |
| 1483 | theCall->setAttr( |
| 1484 | name: cir::CIRDialect::getSrcLocAttrName(), |
| 1485 | value: builder.getI64IntegerAttr(value: clangLoc.getBegin().getRawEncoding())); |
| 1486 | } |
| 1487 | |
| 1488 | // Sema/emitAttributedStmt (see |
| 1489 | // https://github.com/llvm/llvm-project/issues/214764) should one-day enforce |
| 1490 | // that only one of these is valid at a time. For now, we have the same 'bug' |
| 1491 | // as classic codegen where we can end up having BOTH of these. |
| 1492 | if (inNoInlineAttributedStmt) |
| 1493 | theCall.setInlineKind(cir::InlineKind::NoInline); |
| 1494 | if (inAlwaysInlineAttributedStmt && |
| 1495 | !cgm.getTargetCIRGenInfo().wouldInliningViolateFunctionCallABI( |
| 1496 | Caller: callerDecl, Callee: calleeDecl)) |
| 1497 | theCall.setInlineKind(cir::InlineKind::AlwaysInline); |
| 1498 | |
| 1499 | if (isMustTail) { |
| 1500 | // PPC/MIPS have some diagnostics for classic-codegen, but we don't support |
| 1501 | // them yet. |
| 1502 | const llvm::Triple &triple = getTarget().getTriple(); |
| 1503 | if (triple.isPPC() || triple.isMIPS()) { |
| 1504 | cgm.errorNYI(mustTailCall->getBeginLoc(), |
| 1505 | "musttail call target legality checks" ); |
| 1506 | return getUndefRValue(ty: retTy); |
| 1507 | } |
| 1508 | |
| 1509 | // This call replaces the frame that the handler enforcing the exception |
| 1510 | // specification belongs to, taking that handler with it. |
| 1511 | if (ehSpecTryOp) { |
| 1512 | if (!inEHSpecTerminateScope()) { |
| 1513 | // The filter of a dynamic specification has to run while an exception |
| 1514 | // is leaving the function, and the tail call skips over it. |
| 1515 | cgm.errorUnsupported(s: mustTailCall, type: "tail call skipping over cleanups" ); |
| 1516 | return getUndefRValue(ty: retTy); |
| 1517 | } |
| 1518 | |
| 1519 | // Losing a terminate handler is safe when the callee cannot throw, |
| 1520 | // because then the callee's own handler stands in for it. |
| 1521 | if (!cannotThrow) { |
| 1522 | cgm.getDiags().Report(Loc: mustTailCall->getBeginLoc(), |
| 1523 | DiagID: diag::err_musttail_noexcept_mismatch); |
| 1524 | return getUndefRValue(ty: retTy); |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | // Musttail is required to return immediately, so no cleanup can run |
| 1529 | // between the call and the return. Cleanups that the return makes |
| 1530 | // unnecessary are simply skipped. Anything else cannot be expressed. |
| 1531 | assert(!cir::MissingFeatures::fakeUseCleanup()); |
| 1532 | for (EHScopeStack::iterator it = ehStack.begin(), |
| 1533 | end = ehStack.find(savePoint: prologueCleanupDepth); |
| 1534 | it != end; ++it) { |
| 1535 | auto *cleanupScope = dyn_cast<EHCleanupScope>(Val: &*it); |
| 1536 | if (!cleanupScope || |
| 1537 | !cleanupScope->getCleanup()->isRedundantBeforeReturn()) { |
| 1538 | cgm.errorUnsupported(s: mustTailCall, type: "tail call skipping over cleanups" ); |
| 1539 | return getUndefRValue(ty: retTy); |
| 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | theCall->setAttr(name: cir::CIRDialect::getMustTailAttrName(), |
| 1544 | value: builder.getUnitAttr()); |
| 1545 | |
| 1546 | if (isa<cir::VoidType>(Val: convertType(t: retTy))) |
| 1547 | cir::ReturnOp::create(builder, location: loc); |
| 1548 | else |
| 1549 | cir::ReturnOp::create(builder, location: loc, input: theCall->getResult(idx: 0)); |
| 1550 | |
| 1551 | // Musttail must return immediately, so we just do that. All of the below |
| 1552 | // stuff is effectively UB if this is a musttail, so just do a return |
| 1553 | // immediately. |
| 1554 | builder.createBlock(parent: builder.getBlock()->getParent()); |
| 1555 | return getUndefRValue(ty: retTy); |
| 1556 | } |
| 1557 | |
| 1558 | assert(!cir::MissingFeatures::opCallReturn()); |
| 1559 | |
| 1560 | mlir::Type retCIRTy = convertType(t: retTy); |
| 1561 | if (isa<cir::VoidType>(Val: retCIRTy)) |
| 1562 | return getUndefRValue(ty: retTy); |
| 1563 | switch (getEvaluationKind(type: retTy)) { |
| 1564 | case cir::TEK_Aggregate: { |
| 1565 | Address destPtr = returnValue.getValue(); |
| 1566 | |
| 1567 | if (!destPtr.isValid()) |
| 1568 | destPtr = createMemTemp(t: retTy, loc: callLoc, name: getCounterAggTmpAsString()); |
| 1569 | |
| 1570 | mlir::ResultRange results = theCall->getOpResults(); |
| 1571 | assert(results.size() <= 1 && "multiple returns from a call" ); |
| 1572 | |
| 1573 | SourceLocRAIIObject loc{*this, clangLoc}; |
| 1574 | emitAggregateStore(value: results[0], dest: destPtr); |
| 1575 | return RValue::getAggregate(addr: destPtr); |
| 1576 | } |
| 1577 | case cir::TEK_Scalar: { |
| 1578 | mlir::ResultRange results = theCall->getOpResults(); |
| 1579 | assert(results.size() == 1 && "unexpected number of returns" ); |
| 1580 | |
| 1581 | // If the argument doesn't match, perform a bitcast to coerce it. This |
| 1582 | // can happen due to trivial type mismatches. |
| 1583 | if (results[0].getType() != retCIRTy) |
| 1584 | cgm.errorNYI(loc, feature: "bitcast on function return value" ); |
| 1585 | |
| 1586 | mlir::Region *region = builder.getBlock()->getParent(); |
| 1587 | if (region != theCall->getParentRegion()) |
| 1588 | cgm.errorNYI(loc, feature: "function calls with cleanup" ); |
| 1589 | |
| 1590 | return RValue::get(v: results[0]); |
| 1591 | } |
| 1592 | case cir::TEK_Complex: { |
| 1593 | mlir::ResultRange results = theCall->getOpResults(); |
| 1594 | assert(!results.empty() && |
| 1595 | "Expected at least one result for complex rvalue" ); |
| 1596 | return RValue::getComplex(v: results[0]); |
| 1597 | } |
| 1598 | } |
| 1599 | llvm_unreachable("Invalid evaluation kind" ); |
| 1600 | } |
| 1601 | |
| 1602 | void CallArg::copyInto(CIRGenFunction &cgf, Address addr, |
| 1603 | mlir::Location loc) const { |
| 1604 | LValue dst = cgf.makeAddrLValue(addr, ty); |
| 1605 | if (!hasLV && rv.isScalar()) |
| 1606 | cgf.cgm.errorNYI(loc, feature: "copyInto scalar value" ); |
| 1607 | else if (!hasLV && rv.isComplex()) |
| 1608 | cgf.emitStoreOfComplex(loc, v: rv.getComplexValue(), dest: dst, /*isInit=*/true); |
| 1609 | else |
| 1610 | cgf.cgm.errorNYI(loc, feature: "copyInto hasLV" ); |
| 1611 | isUsed = true; |
| 1612 | } |
| 1613 | |
| 1614 | mlir::Value CIRGenFunction::emitRuntimeCall(mlir::Location loc, |
| 1615 | cir::FuncOp callee, |
| 1616 | ArrayRef<mlir::Value> args, |
| 1617 | mlir::NamedAttrList attrs) { |
| 1618 | |
| 1619 | // TODO(cir): set the calling convention to this runtime call. |
| 1620 | assert(!cir::MissingFeatures::opFuncCallingConv()); |
| 1621 | |
| 1622 | cir::CallOp call = builder.createCallOp(loc, callee, operands: args); |
| 1623 | assert(call->getNumResults() <= 1 && |
| 1624 | "runtime functions have at most 1 result" ); |
| 1625 | |
| 1626 | if (!attrs.empty()) |
| 1627 | call->setAttrs(attrs); |
| 1628 | |
| 1629 | if (call->getNumResults() == 0) |
| 1630 | return nullptr; |
| 1631 | |
| 1632 | return call->getResult(idx: 0); |
| 1633 | } |
| 1634 | |
| 1635 | void CIRGenFunction::emitCallArg(CallArgList &args, const clang::Expr *e, |
| 1636 | clang::QualType argType) { |
| 1637 | assert(argType->isReferenceType() == e->isGLValue() && |
| 1638 | "reference binding to unmaterialized r-value!" ); |
| 1639 | |
| 1640 | if (e->isGLValue()) { |
| 1641 | assert(e->getObjectKind() == OK_Ordinary); |
| 1642 | return args.add(rvalue: emitReferenceBindingToExpr(e), type: argType); |
| 1643 | } |
| 1644 | |
| 1645 | bool hasAggregateEvalKind = hasAggregateEvaluationKind(type: argType); |
| 1646 | |
| 1647 | // For callee-destructed parameters (trivial_abi, MS ABI), create an |
| 1648 | // aggregate temp and let the callee destroy it. |
| 1649 | if (argType->isRecordType() && |
| 1650 | argType->castAsRecordDecl()->isParamDestroyedInCallee()) { |
| 1651 | AggValueSlot slot = createAggTemp(ty: argType, loc: getLoc(srcLoc: e->getSourceRange()), |
| 1652 | name: getCounterAggTmpAsString()); |
| 1653 | |
| 1654 | bool destroyedInCallee = true; |
| 1655 | if (const auto *rd = argType->getAsCXXRecordDecl()) |
| 1656 | destroyedInCallee = rd->hasNonTrivialDestructor(); |
| 1657 | |
| 1658 | if (destroyedInCallee) |
| 1659 | slot.setExternallyDestructed(); |
| 1660 | |
| 1661 | emitAggExpr(e, slot); |
| 1662 | RValue rv = slot.asRValue(); |
| 1663 | args.add(rvalue: rv, type: argType); |
| 1664 | |
| 1665 | if (destroyedInCallee && getLangOpts().Exceptions) |
| 1666 | cgm.errorNYI(e->getSourceRange(), |
| 1667 | "callee-destructed param with exceptions" ); |
| 1668 | return; |
| 1669 | } |
| 1670 | |
| 1671 | if (hasAggregateEvalKind && isa<ImplicitCastExpr>(Val: e) && |
| 1672 | cast<CastExpr>(Val: e)->getCastKind() == CK_LValueToRValue) { |
| 1673 | LValue lv = emitLValue(e: cast<CastExpr>(Val: e)->getSubExpr()); |
| 1674 | assert(lv.isSimple()); |
| 1675 | args.addUncopiedAggregate(lvalue: lv, type: argType); |
| 1676 | return; |
| 1677 | } |
| 1678 | |
| 1679 | args.add(rvalue: emitAnyExprToTemp(e), type: argType); |
| 1680 | } |
| 1681 | |
| 1682 | QualType CIRGenFunction::getVarArgType(const Expr *arg) { |
| 1683 | // System headers on Windows define NULL to 0 instead of 0LL on Win64. MSVC |
| 1684 | // implicitly widens null pointer constants that are arguments to varargs |
| 1685 | // functions to pointer-sized ints. |
| 1686 | if (!getTarget().getTriple().isOSWindows()) |
| 1687 | return arg->getType(); |
| 1688 | |
| 1689 | assert(!cir::MissingFeatures::msabi()); |
| 1690 | cgm.errorNYI(arg->getSourceRange(), "getVarArgType: NYI for Windows target" ); |
| 1691 | return arg->getType(); |
| 1692 | } |
| 1693 | |
| 1694 | /// Similar to emitAnyExpr(), however, the result will always be accessible |
| 1695 | /// even if no aggregate location is provided. |
| 1696 | RValue CIRGenFunction::emitAnyExprToTemp(const Expr *e) { |
| 1697 | AggValueSlot aggSlot = AggValueSlot::ignored(); |
| 1698 | |
| 1699 | if (hasAggregateEvaluationKind(type: e->getType())) |
| 1700 | aggSlot = createAggTemp(ty: e->getType(), loc: getLoc(srcLoc: e->getSourceRange()), |
| 1701 | name: getCounterAggTmpAsString()); |
| 1702 | |
| 1703 | return emitAnyExpr(e, aggSlot); |
| 1704 | } |
| 1705 | |
| 1706 | void CIRGenFunction::emitCallArgs( |
| 1707 | CallArgList &args, PrototypeWrapper prototype, |
| 1708 | llvm::iterator_range<clang::CallExpr::const_arg_iterator> argRange, |
| 1709 | AbstractCallee callee, unsigned paramsToSkip, EvaluationOrder order) { |
| 1710 | llvm::SmallVector<QualType, 16> argTypes; |
| 1711 | |
| 1712 | assert(!cir::MissingFeatures::opCallCallConv()); |
| 1713 | |
| 1714 | // First, if a prototype was provided, use those argument types. |
| 1715 | bool isVariadic = false; |
| 1716 | if (prototype.p) { |
| 1717 | assert(!cir::MissingFeatures::opCallObjCMethod()); |
| 1718 | |
| 1719 | const auto *fpt = cast<const FunctionProtoType *>(Val&: prototype.p); |
| 1720 | isVariadic = fpt->isVariadic(); |
| 1721 | assert(!cir::MissingFeatures::opCallCallConv()); |
| 1722 | argTypes.assign(in_start: fpt->param_type_begin() + paramsToSkip, |
| 1723 | in_end: fpt->param_type_end()); |
| 1724 | } |
| 1725 | |
| 1726 | // If we still have any arguments, emit them using the type of the argument. |
| 1727 | for (const clang::Expr *a : llvm::drop_begin(RangeOrContainer&: argRange, N: argTypes.size())) |
| 1728 | argTypes.push_back(Elt: isVariadic ? getVarArgType(arg: a) : a->getType()); |
| 1729 | assert(argTypes.size() == (size_t)(argRange.end() - argRange.begin())); |
| 1730 | |
| 1731 | // We must evaluate arguments from right to left in the MS C++ ABI, because |
| 1732 | // arguments are destroyed left to right in the callee. As a special case, |
| 1733 | // there are certain language constructs that require left-to-right |
| 1734 | // evaluation, and in those cases we consider the evaluation order requirement |
| 1735 | // to trump the "destruction order is reverse construction order" guarantee. |
| 1736 | bool leftToRight = CodeGenUtils::shouldEvaluateArgsLeftToRight( |
| 1737 | Order: order, ABI: getTarget().getCXXABI()); |
| 1738 | |
| 1739 | auto maybeEmitImplicitObjectSize = [&](size_t i, const Expr *arg, |
| 1740 | RValue emittedArg) { |
| 1741 | if (!callee.hasFunctionDecl() || i >= callee.getNumParams()) |
| 1742 | return; |
| 1743 | auto *ps = callee.getParamDecl(I: i)->getAttr<PassObjectSizeAttr>(); |
| 1744 | if (!ps) |
| 1745 | return; |
| 1746 | |
| 1747 | QualType sizeTy = getContext().getSizeType(); |
| 1748 | assert(emittedArg.getValue() && "We emitted nothing for the arg?" ); |
| 1749 | mlir::Value v = evaluateOrEmitBuiltinObjectSize( |
| 1750 | e: arg, type: ps->getType(), resType: cast<cir::IntType>(Val&: cgm.sizeTy), |
| 1751 | emittedE: emittedArg.getValue(), isDynamic: ps->isDynamic()); |
| 1752 | args.add(rvalue: RValue::get(v), type: sizeTy); |
| 1753 | // When emitting right-to-left, the size arg was appended after the |
| 1754 | // pointer arg; swap them so the size follows the pointer in the final |
| 1755 | // argument list after the outer reverse. |
| 1756 | if (!leftToRight) |
| 1757 | std::iter_swap(a: args.rbegin(), b: std::next(x: args.rbegin())); |
| 1758 | }; |
| 1759 | |
| 1760 | // Evaluate each argument in the appropriate order. |
| 1761 | size_t callArgsStart = args.size(); |
| 1762 | for (size_t i = 0; i != argTypes.size(); ++i) { |
| 1763 | size_t idx = leftToRight ? i : argTypes.size() - i - 1; |
| 1764 | CallExpr::const_arg_iterator currentArg = argRange.begin() + idx; |
| 1765 | size_t initialArgSize = args.size(); |
| 1766 | |
| 1767 | emitCallArg(args, e: *currentArg, argType: argTypes[idx]); |
| 1768 | |
| 1769 | // In particular, we depend on it being the last arg in Args, and the |
| 1770 | // objectsize bits depend on there only being one arg if !LeftToRight. |
| 1771 | assert(initialArgSize + 1 == args.size() && |
| 1772 | "The code below depends on only adding one arg per emitCallArg" ); |
| 1773 | (void)initialArgSize; |
| 1774 | |
| 1775 | // Since pointer argument are never emitted as LValue, it is safe to emit |
| 1776 | // non-null argument check for r-value only. |
| 1777 | if (!args.back().hasLValue()) { |
| 1778 | RValue rvArg = args.back().getKnownRValue(); |
| 1779 | assert(!cir::MissingFeatures::sanitizers()); |
| 1780 | maybeEmitImplicitObjectSize(idx, *currentArg, rvArg); |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | if (!leftToRight) { |
| 1785 | // Un-reverse the arguments we just evaluated so they match up with the |
| 1786 | // callee's parameter order. |
| 1787 | std::reverse(first: args.begin() + callArgsStart, last: args.end()); |
| 1788 | assert(!cir::MissingFeatures::writebacks()); |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | // FIXME(cir): This is identical to the version from classic-codegen, we should |
| 1793 | // figure out how to move this to a common location. |
| 1794 | bool CIRGenTypes::inheritingCtorHasParams(const InheritedConstructor &inherited, |
| 1795 | CXXCtorType type) { |
| 1796 | // Parameters are unnecessary if we're constructing a base class subobject |
| 1797 | // and the inherited constructor lives in a virtual base. |
| 1798 | return type == Ctor_Complete || |
| 1799 | !inherited.getShadowDecl()->constructsVirtualBase() || |
| 1800 | !getASTContext().getTargetInfo().getCXXABI().hasConstructorVariants(); |
| 1801 | } |
| 1802 | |