| 1 | //===--- FunctionUtils.cpp - Shared function emission queries -------------===// |
| 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 | #include "clang/CodeGenUtils/FunctionUtils.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/AST/Attr.h" |
| 12 | #include "clang/AST/Expr.h" |
| 13 | #include "clang/Basic/Builtins.h" |
| 14 | #include "clang/Basic/DiagnosticFrontend.h" |
| 15 | #include "clang/Basic/TargetBuiltins.h" |
| 16 | #include "clang/Basic/TargetInfo.h" |
| 17 | #include "llvm/ADT/StringMap.h" |
| 18 | |
| 19 | namespace clang::CodeGenUtils { |
| 20 | |
| 21 | bool shouldEmitLifetimeMarkers(const CodeGenOptions &CGOpts, |
| 22 | const LangOptions &LangOpts) { |
| 23 | if (CGOpts.DisableLifetimeMarkers) |
| 24 | return false; |
| 25 | |
| 26 | // Sanitizers may use markers. |
| 27 | if (CGOpts.SanitizeAddressUseAfterScope || |
| 28 | LangOpts.Sanitize.has(K: SanitizerKind::HWAddress) || |
| 29 | LangOpts.Sanitize.has(K: SanitizerKind::Memory) || |
| 30 | LangOpts.Sanitize.has(K: SanitizerKind::MemtagStack)) |
| 31 | return true; |
| 32 | |
| 33 | // For now, only in optimized builds. |
| 34 | return CGOpts.OptimizationLevel != 0; |
| 35 | } |
| 36 | |
| 37 | // Emits an error if we don't have a valid set of target features for the |
| 38 | // called function. |
| 39 | void checkTargetFeatures(ASTContext &Ctx, DiagnosticsEngine &Diags, |
| 40 | const LangOptions &LangOpts, const CallExpr *E, |
| 41 | const FunctionDecl *Caller, |
| 42 | const FunctionDecl *TargetDecl) { |
| 43 | // SemaChecking cannot handle these x86 builtins because they have different |
| 44 | // parameter ranges depending on the caller's TargetAttribute. |
| 45 | if (Ctx.getTargetInfo().getTriple().isX86()) { |
| 46 | unsigned BuiltinID = TargetDecl->getBuiltinID(); |
| 47 | if (BuiltinID == X86::BI__builtin_ia32_cmpps || |
| 48 | BuiltinID == X86::BI__builtin_ia32_cmpss || |
| 49 | BuiltinID == X86::BI__builtin_ia32_cmppd || |
| 50 | BuiltinID == X86::BI__builtin_ia32_cmpsd) { |
| 51 | llvm::StringMap<bool> TargetFeatureMap; |
| 52 | Ctx.getFunctionFeatureMap(FeatureMap&: TargetFeatureMap, Caller); |
| 53 | llvm::APSInt Result = *(E->getArg(Arg: 2)->getIntegerConstantExpr(Ctx)); |
| 54 | if (Result.getSExtValue() > 7 && !TargetFeatureMap.lookup(Key: "avx" )) |
| 55 | Diags.Report(Loc: E->getBeginLoc(), DiagID: diag::err_builtin_needs_feature) |
| 56 | << TargetDecl->getDeclName() << "avx" ; |
| 57 | } |
| 58 | } |
| 59 | checkTargetFeatures(Ctx, Diags, LangOpts, Loc: E->getBeginLoc(), Caller, |
| 60 | TargetDecl); |
| 61 | } |
| 62 | |
| 63 | // Emits an error if we don't have a valid set of target features for the |
| 64 | // called function. |
| 65 | void checkTargetFeatures(ASTContext &Ctx, DiagnosticsEngine &Diags, |
| 66 | const LangOptions &LangOpts, SourceLocation Loc, |
| 67 | const FunctionDecl *Caller, |
| 68 | const FunctionDecl *TargetDecl) { |
| 69 | if (!TargetDecl || !Caller) |
| 70 | return; |
| 71 | |
| 72 | bool IsAlwaysInline = TargetDecl->hasAttr<AlwaysInlineAttr>(); |
| 73 | bool IsFlatten = Caller->hasAttr<FlattenAttr>(); |
| 74 | |
| 75 | unsigned BuiltinID = TargetDecl->getBuiltinID(); |
| 76 | std::string MissingFeature; |
| 77 | llvm::StringMap<bool> CallerFeatureMap; |
| 78 | Ctx.getFunctionFeatureMap(FeatureMap&: CallerFeatureMap, Caller); |
| 79 | // When compiling in HipStdPar mode we have to be conservative in rejecting |
| 80 | // target specific features in the FE, and defer the possible error to the |
| 81 | // AcceleratorCodeSelection pass, wherein iff an unsupported target builtin is |
| 82 | // referenced by an accelerator executable function, we emit an error. |
| 83 | bool IsHipStdPar = LangOpts.HIPStdPar && LangOpts.CUDAIsDevice; |
| 84 | if (BuiltinID) { |
| 85 | StringRef FeatureList(Ctx.BuiltinInfo.getRequiredFeatures(ID: BuiltinID)); |
| 86 | if (!Builtin::evaluateRequiredTargetFeatures(RequiredFatures: FeatureList, |
| 87 | TargetFetureMap: CallerFeatureMap) && |
| 88 | !IsHipStdPar) |
| 89 | Diags.Report(Loc, DiagID: diag::err_builtin_needs_feature) |
| 90 | << TargetDecl->getDeclName() << FeatureList; |
| 91 | } else if (!TargetDecl->isMultiVersion() && |
| 92 | TargetDecl->hasAttr<TargetAttr>()) { |
| 93 | // Get the required features for the callee. |
| 94 | const TargetAttr *TD = TargetDecl->getAttr<TargetAttr>(); |
| 95 | ParsedTargetAttr ParsedAttr = Ctx.filterFunctionTargetAttrs(TD); |
| 96 | |
| 97 | SmallVector<StringRef, 1> ReqFeatures; |
| 98 | llvm::StringMap<bool> CalleeFeatureMap; |
| 99 | Ctx.getFunctionFeatureMap(FeatureMap&: CalleeFeatureMap, TargetDecl); |
| 100 | |
| 101 | for (const auto &F : ParsedAttr.Features) { |
| 102 | if (F[0] == '+' && CalleeFeatureMap.lookup(Key: F.substr(pos: 1))) |
| 103 | ReqFeatures.push_back(Elt: StringRef(F).substr(Start: 1)); |
| 104 | } |
| 105 | for (const auto &F : CalleeFeatureMap) { |
| 106 | if (F.getValue()) |
| 107 | ReqFeatures.push_back(Elt: F.getKey()); |
| 108 | } |
| 109 | if (!llvm::all_of(Range&: ReqFeatures, |
| 110 | P: [&](StringRef Feature) { |
| 111 | if (!CallerFeatureMap.lookup(Key: Feature)) { |
| 112 | MissingFeature = Feature.str(); |
| 113 | return false; |
| 114 | } |
| 115 | return true; |
| 116 | }) && |
| 117 | !IsHipStdPar) { |
| 118 | if (IsAlwaysInline) |
| 119 | Diags.Report(Loc, DiagID: diag::err_function_needs_feature) |
| 120 | << Caller->getDeclName() << TargetDecl->getDeclName() |
| 121 | << MissingFeature; |
| 122 | else if (IsFlatten) |
| 123 | Diags.Report(Loc, DiagID: diag::err_flatten_function_needs_feature) |
| 124 | << Caller->getDeclName() << TargetDecl->getDeclName() |
| 125 | << MissingFeature; |
| 126 | } |
| 127 | } else if (!Caller->isMultiVersion() && Caller->hasAttr<TargetAttr>()) { |
| 128 | llvm::StringMap<bool> CalleeFeatureMap; |
| 129 | Ctx.getFunctionFeatureMap(FeatureMap&: CalleeFeatureMap, TargetDecl); |
| 130 | |
| 131 | for (const auto &F : CalleeFeatureMap) { |
| 132 | if (F.getValue() && |
| 133 | (!CallerFeatureMap.lookup(Key: F.getKey()) || |
| 134 | !CallerFeatureMap.find(Key: F.getKey())->getValue()) && |
| 135 | !IsHipStdPar) { |
| 136 | if (IsAlwaysInline) |
| 137 | Diags.Report(Loc, DiagID: diag::err_function_needs_feature) |
| 138 | << Caller->getDeclName() << TargetDecl->getDeclName() |
| 139 | << F.getKey(); |
| 140 | else if (IsFlatten) |
| 141 | Diags.Report(Loc, DiagID: diag::err_flatten_function_needs_feature) |
| 142 | << Caller->getDeclName() << TargetDecl->getDeclName() |
| 143 | << F.getKey(); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | } // namespace clang::CodeGenUtils |
| 150 | |