Skip to content

Commit 853d495

Browse files
authored
Merge pull request llvm#515 from AMD-Lightning-Internal/amd/merge/upstream_merge_20250205180955
merge main into amd-staging
2 parents 74744e3 + 4aa3ab7 commit 853d495

29 files changed

+457
-225
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ Removed Compiler Flags
112112
Attribute Changes in Clang
113113
--------------------------
114114

115+
- The ``no_sanitize`` attribute now accepts both ``gnu`` and ``clang`` names.
116+
115117
Improvements to Clang's diagnostics
116118
-----------------------------------
117119

clang/include/clang/AST/OpenMPClause.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9451,7 +9451,8 @@ struct TargetOMPContext final : public llvm::omp::OMPContext {
94519451
TargetOMPContext(ASTContext &ASTCtx,
94529452
std::function<void(StringRef)> &&DiagUnknownTrait,
94539453
const FunctionDecl *CurrentFunctionDecl,
9454-
ArrayRef<llvm::omp::TraitProperty> ConstructTraits);
9454+
ArrayRef<llvm::omp::TraitProperty> ConstructTraits,
9455+
int DeviceNum);
94559456

94569457
virtual ~TargetOMPContext() = default;
94579458

clang/include/clang/Basic/Attr.td

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,13 @@ class Clang<string name, bit allowInC = 1, int version = 1>
380380
bit AllowInC = allowInC;
381381
}
382382

383+
// This spelling combines the spellings of GCC and Clang for cases where the
384+
// spellings are equivalent for compile compatibility.
385+
class ClangGCC<string name, bit allowInC = 1, int version = 1>
386+
: Spelling<name, "ClangGCC", version> {
387+
bit AllowInC = allowInC;
388+
}
389+
383390
// HLSL Annotation spellings
384391
class HLSLAnnotation<string name> : Spelling<name, "HLSLAnnotation">;
385392

@@ -3677,7 +3684,7 @@ def X86ForceAlignArgPointer : InheritableAttr, TargetSpecificAttr<TargetAnyX86>
36773684
}
36783685

36793686
def NoSanitize : InheritableAttr {
3680-
let Spellings = [Clang<"no_sanitize">];
3687+
let Spellings = [ClangGCC<"no_sanitize">];
36813688
let Args = [VariadicStringArgument<"Sanitizers">];
36823689
let Subjects = SubjectList<[Function, ObjCMethod, GlobalVar], ErrorDiag>;
36833690
let Documentation = [NoSanitizeDocs];

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,9 @@ def W_Joined : Joined<["-"], "W">, Group<W_Group>,
934934
def Xanalyzer : Separate<["-"], "Xanalyzer">,
935935
HelpText<"Pass <arg> to the static analyzer">, MetaVarName<"<arg>">,
936936
Group<StaticAnalyzer_Group>;
937-
def Xarch__ : JoinedAndSeparate<["-"], "Xarch_">, Flags<[NoXarchOption]>;
937+
def Xarch__ : JoinedAndSeparate<["-"], "Xarch_">, Flags<[NoXarchOption]>,
938+
HelpText<"Pass <arg> to the compiliation if the target matches <arch>">,
939+
MetaVarName<"<arch> <arg>">;
938940
def Xarch_host : Separate<["-"], "Xarch_host">, Flags<[NoXarchOption]>,
939941
HelpText<"Pass <arg> to the CUDA/HIP host compilation">, MetaVarName<"<arg>">;
940942
def Xarch_device : Separate<["-"], "Xarch_device">, Flags<[NoXarchOption]>,
@@ -1120,8 +1122,8 @@ def fno_convergent_functions : Flag<["-"], "fno-convergent-functions">,
11201122

11211123
// Common offloading options
11221124
let Group = offload_Group in {
1123-
def offload_arch_EQ : Joined<["--"], "offload-arch=">, Flags<[NoXarchOption]>,
1124-
Visibility<[ClangOption, FlangOption]>,
1125+
def offload_arch_EQ : Joined<["--"], "offload-arch=">,
1126+
Visibility<[ClangOption, FlangOption]>, Flags<[NoXarchOption]>,
11251127
HelpText<"Specify an offloading device architecture for CUDA, HIP, or OpenMP. (e.g. sm_35). "
11261128
"If 'native' is used the compiler will detect locally installed architectures. "
11271129
"For HIP offloading, the device architecture can be followed by target ID features "

clang/include/clang/Sema/SemaOpenMP.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,9 @@ class SemaOpenMP : public SemaBase {
849849
ArrayRef<OMPInteropInfo> AppendArgs, SourceLocation AdjustArgsLoc,
850850
SourceLocation AppendArgsLoc, SourceRange SR);
851851

852+
/// Called on device_num selector in context selectors.
853+
void ActOnOpenMPDeviceNum(Expr *DeviceNumExpr);
854+
852855
OMPClause *ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
853856
SourceLocation StartLoc,
854857
SourceLocation LParenLoc,
@@ -1410,6 +1413,13 @@ class SemaOpenMP : public SemaBase {
14101413

14111414
void handleOMPAssumeAttr(Decl *D, const ParsedAttr &AL);
14121415

1416+
/// Setter and getter functions for device_num.
1417+
void setOpenMPDeviceNum(int Num);
1418+
1419+
int getOpenMPDeviceNum() const;
1420+
1421+
void setOpenMPDeviceNumID(StringRef ID);
1422+
14131423
private:
14141424
void *VarDataSharingAttributesStack;
14151425

@@ -1480,6 +1490,12 @@ class SemaOpenMP : public SemaBase {
14801490

14811491
/// All `omp assumes` we encountered so far.
14821492
SmallVector<OMPAssumeAttr *, 4> OMPAssumeGlobal;
1493+
1494+
/// Device number specified by the context selector.
1495+
int DeviceNum = -1;
1496+
1497+
/// Device number identifier specified by the context selector.
1498+
StringRef DeviceNumID;
14831499
};
14841500

14851501
} // namespace clang

clang/lib/AST/OpenMPClause.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,9 +2927,13 @@ llvm::raw_ostream &clang::operator<<(llvm::raw_ostream &OS,
29272927
TargetOMPContext::TargetOMPContext(
29282928
ASTContext &ASTCtx, std::function<void(StringRef)> &&DiagUnknownTrait,
29292929
const FunctionDecl *CurrentFunctionDecl,
2930-
ArrayRef<llvm::omp::TraitProperty> ConstructTraits)
2930+
ArrayRef<llvm::omp::TraitProperty> ConstructTraits, int DeviceNum)
29312931
: OMPContext(ASTCtx.getLangOpts().OpenMPIsTargetDevice,
2932-
ASTCtx.getTargetInfo().getTriple()),
2932+
ASTCtx.getTargetInfo().getTriple(),
2933+
ASTCtx.getLangOpts().OMPTargetTriples.empty()
2934+
? llvm::Triple()
2935+
: ASTCtx.getLangOpts().OMPTargetTriples[0],
2936+
DeviceNum),
29332937
FeatureValidityCheck([&](StringRef FeatureName) {
29342938
return ASTCtx.getTargetInfo().isValidFeatureName(FeatureName);
29352939
}),

clang/lib/Driver/Driver.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3418,7 +3418,9 @@ class OffloadingActionBuilder final {
34183418
// Collect all offload arch parameters, removing duplicates.
34193419
std::set<StringRef> GpuArchs;
34203420
bool Error = false;
3421-
for (Arg *A : Args) {
3421+
const ToolChain &TC = *ToolChains.front();
3422+
for (Arg *A : C.getArgsForToolChain(&TC, /*BoundArch=*/"",
3423+
AssociatedOffloadKind)) {
34223424
if (!(A->getOption().matches(options::OPT_offload_arch_EQ) ||
34233425
A->getOption().matches(options::OPT_no_offload_arch_EQ)))
34243426
continue;
@@ -3429,7 +3431,6 @@ class OffloadingActionBuilder final {
34293431
ArchStr == "all") {
34303432
GpuArchs.clear();
34313433
} else if (ArchStr == "native") {
3432-
const ToolChain &TC = *ToolChains.front();
34333434
auto GPUsOrErr = ToolChains.front()->getSystemGPUArchs(Args);
34343435
if (!GPUsOrErr) {
34353436
TC.getDriver().Diag(diag::err_drv_undetermined_gpu_arch)

clang/lib/Driver/ToolChain.cpp

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,8 @@ void ToolChain::TranslateXarchArgs(
16591659
A->getOption().matches(options::OPT_Xarch_host))
16601660
ValuePos = 0;
16611661

1662-
unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(ValuePos));
1662+
const InputArgList &BaseArgs = Args.getBaseArgs();
1663+
unsigned Index = BaseArgs.MakeIndex(A->getValue(ValuePos));
16631664
unsigned Prev = Index;
16641665
std::unique_ptr<llvm::opt::Arg> XarchArg(Opts.ParseOneArg(Args, Index));
16651666

@@ -1683,8 +1684,31 @@ void ToolChain::TranslateXarchArgs(
16831684
Diags.Report(DiagID) << A->getAsString(Args);
16841685
return;
16851686
}
1687+
16861688
XarchArg->setBaseArg(A);
16871689
A = XarchArg.release();
1690+
1691+
// Linker input arguments require custom handling. The problem is that we
1692+
// have already constructed the phase actions, so we can not treat them as
1693+
// "input arguments".
1694+
if (A->getOption().hasFlag(options::LinkerInput)) {
1695+
// Convert the argument into individual Zlinker_input_args. Need to do this
1696+
// manually to avoid memory leaks with the allocated arguments.
1697+
for (const char *Value : A->getValues()) {
1698+
auto Opt = Opts.getOption(options::OPT_Zlinker_input);
1699+
unsigned Index = BaseArgs.MakeIndex(Opt.getName(), Value);
1700+
auto NewArg =
1701+
new Arg(Opt, BaseArgs.MakeArgString(Opt.getPrefix() + Opt.getName()),
1702+
Index, BaseArgs.getArgString(Index + 1), A);
1703+
1704+
DAL->append(NewArg);
1705+
if (!AllocatedArgs)
1706+
DAL->AddSynthesizedArg(NewArg);
1707+
else
1708+
AllocatedArgs->push_back(NewArg);
1709+
}
1710+
}
1711+
16881712
if (!AllocatedArgs)
16891713
DAL->AddSynthesizedArg(A);
16901714
else
@@ -1708,19 +1732,17 @@ llvm::opt::DerivedArgList *ToolChain::TranslateXarchArgs(
17081732
} else if (A->getOption().matches(options::OPT_Xarch_host)) {
17091733
NeedTrans = !IsDevice;
17101734
Skip = IsDevice;
1711-
} else if (A->getOption().matches(options::OPT_Xarch__) && IsDevice) {
1712-
// Do not translate -Xarch_ options for non CUDA/HIP toolchain since
1713-
// they may need special translation.
1714-
// Skip this argument unless the architecture matches BoundArch
1715-
if (BoundArch.empty() || A->getValue(0) != BoundArch)
1716-
Skip = true;
1717-
else
1718-
NeedTrans = true;
1735+
} else if (A->getOption().matches(options::OPT_Xarch__)) {
1736+
NeedTrans = A->getValue() == getArchName() ||
1737+
(!BoundArch.empty() && A->getValue() == BoundArch);
1738+
Skip = !NeedTrans;
17191739
}
17201740
if (NeedTrans || Skip)
17211741
Modified = true;
1722-
if (NeedTrans)
1742+
if (NeedTrans) {
1743+
A->claim();
17231744
TranslateXarchArgs(Args, A, DAL, AllocatedArgs);
1745+
}
17241746
if (!Skip)
17251747
DAL->append(A);
17261748
}

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,30 +2777,6 @@ DerivedArgList *MachO::TranslateArgs(const DerivedArgList &Args,
27772777
// and try to push it down into tool specific logic.
27782778

27792779
for (Arg *A : Args) {
2780-
if (A->getOption().matches(options::OPT_Xarch__)) {
2781-
// Skip this argument unless the architecture matches either the toolchain
2782-
// triple arch, or the arch being bound.
2783-
StringRef XarchArch = A->getValue(0);
2784-
if (!(XarchArch == getArchName() ||
2785-
(!BoundArch.empty() && XarchArch == BoundArch)))
2786-
continue;
2787-
2788-
Arg *OriginalArg = A;
2789-
TranslateXarchArgs(Args, A, DAL);
2790-
2791-
// Linker input arguments require custom handling. The problem is that we
2792-
// have already constructed the phase actions, so we can not treat them as
2793-
// "input arguments".
2794-
if (A->getOption().hasFlag(options::LinkerInput)) {
2795-
// Convert the argument into individual Zlinker_input_args.
2796-
for (const char *Value : A->getValues()) {
2797-
DAL->AddSeparateArg(
2798-
OriginalArg, Opts.getOption(options::OPT_Zlinker_input), Value);
2799-
}
2800-
continue;
2801-
}
2802-
}
2803-
28042780
// Sob. These is strictly gcc compatible for the time being. Apple
28052781
// gcc translates options twice, which means that self-expanding
28062782
// options add duplicates.

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,18 @@ void Parser::parseOMPTraitPropertyKind(OMPTraitProperty &TIProperty,
888888
TIProperty.Kind = TraitProperty::invalid;
889889

890890
SourceLocation NameLoc = Tok.getLocation();
891-
StringRef Name = getNameFromIdOrString(*this, Tok, CONTEXT_TRAIT_LVL);
891+
StringRef Name;
892+
if (Selector == llvm::omp::TraitSelector::target_device_device_num) {
893+
Name = "number";
894+
TIProperty.Kind = getOpenMPContextTraitPropertyKind(Set, Selector, Name);
895+
ExprResult DeviceNumExprResult = ParseExpression();
896+
if (DeviceNumExprResult.isUsable()) {
897+
Expr *DeviceNumExpr = DeviceNumExprResult.get();
898+
Actions.OpenMP().ActOnOpenMPDeviceNum(DeviceNumExpr);
899+
}
900+
return;
901+
}
902+
Name = getNameFromIdOrString(*this, Tok, CONTEXT_TRAIT_LVL);
892903
if (Name.empty()) {
893904
Diag(Tok.getLocation(), diag::note_omp_declare_variant_ctx_options)
894905
<< CONTEXT_TRAIT_LVL << listOpenMPContextTraitProperties(Set, Selector);
@@ -918,7 +929,8 @@ void Parser::parseOMPTraitPropertyKind(OMPTraitProperty &TIProperty,
918929
<< "(<property-name>)";
919930
return;
920931
}
921-
TraitSelector SelectorForName = getOpenMPContextTraitSelectorKind(Name);
932+
TraitSelector SelectorForName =
933+
getOpenMPContextTraitSelectorKind(Name, SetForName);
922934
if (SelectorForName != TraitSelector::invalid) {
923935
Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a)
924936
<< Name << CONTEXT_SELECTOR_LVL << CONTEXT_TRAIT_LVL;
@@ -935,7 +947,7 @@ void Parser::parseOMPTraitPropertyKind(OMPTraitProperty &TIProperty,
935947
}
936948
for (const auto &PotentialSet :
937949
{TraitSet::construct, TraitSet::user, TraitSet::implementation,
938-
TraitSet::device}) {
950+
TraitSet::device, TraitSet::target_device}) {
939951
TraitProperty PropertyForName =
940952
getOpenMPContextTraitPropertyKind(PotentialSet, Selector, Name);
941953
if (PropertyForName == TraitProperty::invalid)
@@ -1062,7 +1074,7 @@ void Parser::parseOMPTraitSelectorKind(OMPTraitSelector &TISelector,
10621074
return;
10631075
}
10641076

1065-
TISelector.Kind = getOpenMPContextTraitSelectorKind(Name);
1077+
TISelector.Kind = getOpenMPContextTraitSelectorKind(Name, Set);
10661078
if (TISelector.Kind != TraitSelector::invalid) {
10671079
if (checkForDuplicates(*this, Name, NameLoc, Seen, CONTEXT_SELECTOR_LVL))
10681080
TISelector.Kind = TraitSelector::invalid;
@@ -1084,7 +1096,7 @@ void Parser::parseOMPTraitSelectorKind(OMPTraitSelector &TISelector,
10841096
}
10851097
for (const auto &PotentialSet :
10861098
{TraitSet::construct, TraitSet::user, TraitSet::implementation,
1087-
TraitSet::device}) {
1099+
TraitSet::device, TraitSet::target_device}) {
10881100
TraitProperty PropertyForName = getOpenMPContextTraitPropertyKind(
10891101
PotentialSet, TraitSelector::invalid, Name);
10901102
if (PropertyForName == TraitProperty::invalid)
@@ -1259,7 +1271,8 @@ void Parser::parseOMPTraitSetKind(OMPTraitSet &TISet,
12591271
// It follows diagnosis and helping notes.
12601272
Diag(NameLoc, diag::warn_omp_declare_variant_ctx_not_a_set) << Name;
12611273

1262-
TraitSelector SelectorForName = getOpenMPContextTraitSelectorKind(Name);
1274+
TraitSelector SelectorForName =
1275+
getOpenMPContextTraitSelectorKind(Name, TISet.Kind);
12631276
if (SelectorForName != TraitSelector::invalid) {
12641277
Diag(NameLoc, diag::note_omp_declare_variant_ctx_is_a)
12651278
<< Name << CONTEXT_SELECTOR_LVL << CONTEXT_SELECTOR_SET_LVL;
@@ -1276,7 +1289,7 @@ void Parser::parseOMPTraitSetKind(OMPTraitSet &TISet,
12761289
}
12771290
for (const auto &PotentialSet :
12781291
{TraitSet::construct, TraitSet::user, TraitSet::implementation,
1279-
TraitSet::device}) {
1292+
TraitSet::device, TraitSet::target_device}) {
12801293
TraitProperty PropertyForName = getOpenMPContextTraitPropertyKind(
12811294
PotentialSet, TraitSelector::invalid, Name);
12821295
if (PropertyForName == TraitProperty::invalid)
@@ -2253,7 +2266,8 @@ Parser::DeclGroupPtrTy Parser::ParseOpenMPDeclarativeDirectiveWithExtDecl(
22532266
TargetOMPContext OMPCtx(
22542267
ASTCtx, std::move(DiagUnknownTrait),
22552268
/* CurrentFunctionDecl */ nullptr,
2256-
/* ConstructTraits */ ArrayRef<llvm::omp::TraitProperty>());
2269+
/* ConstructTraits */ ArrayRef<llvm::omp::TraitProperty>(),
2270+
Actions.OpenMP().getOpenMPDeviceNum());
22572271

22582272
if (isVariantApplicableInContext(VMI, OMPCtx, /* DeviceSetOnly */ true)) {
22592273
Actions.OpenMP().ActOnOpenMPBeginDeclareVariant(Loc, TI);
@@ -2805,7 +2819,8 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
28052819
};
28062820
TargetOMPContext OMPCtx(ASTContext, std::move(DiagUnknownTrait),
28072821
/* CurrentFunctionDecl */ nullptr,
2808-
ArrayRef<llvm::omp::TraitProperty>());
2822+
ArrayRef<llvm::omp::TraitProperty>(),
2823+
Actions.OpenMP().getOpenMPDeviceNum());
28092824

28102825
// A single match is returned for OpenMP 5.0
28112826
int BestIdx = getBestVariantMatchForContext(VMIs, OMPCtx);

0 commit comments

Comments
 (0)