Skip to content

Commit 69183f8

Browse files
authored
[NFC][Clang] Address reviews about overrideFunctionFeaturesWithTargetFeatures (#65938)
Addressing remarks after merge of D159257 * Add comment * Remove irrelevant CHECKs from test * Simplify function * Use llvm::sort before setting target-features as it is done in CodeGenModeule
1 parent 3b7dfda commit 69183f8

File tree

2 files changed

+22
-33
lines changed

2 files changed

+22
-33
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,52 +2001,41 @@ static void getTrivialDefaultFunctionAttributes(
20012001
}
20022002
}
20032003

2004+
/// Merges `target-features` from \TargetOpts and \F, and sets the result in
2005+
/// \FuncAttr
2006+
/// * features from \F are always kept
2007+
/// * a feature from \TargetOpts is kept if itself and its opposite are absent
2008+
/// from \F
20042009
static void
20052010
overrideFunctionFeaturesWithTargetFeatures(llvm::AttrBuilder &FuncAttr,
20062011
const llvm::Function &F,
20072012
const TargetOptions &TargetOpts) {
20082013
auto FFeatures = F.getFnAttribute("target-features");
20092014

2010-
llvm::StringSet<> IncompatibleFeatureNames;
2015+
llvm::StringSet<> MergedNames;
20112016
SmallVector<StringRef> MergedFeatures;
20122017
MergedFeatures.reserve(TargetOpts.Features.size());
20132018

2014-
if (FFeatures.isValid()) {
2015-
const auto &TFeatures = TargetOpts.FeatureMap;
2016-
for (StringRef Feature : llvm::split(FFeatures.getValueAsString(), ',')) {
2019+
auto AddUnmergedFeatures = [&](auto &&FeatureRange) {
2020+
for (StringRef Feature : FeatureRange) {
20172021
if (Feature.empty())
20182022
continue;
2019-
2020-
bool EnabledForFunc = Feature.starts_with("+");
2021-
assert(EnabledForFunc || Feature.starts_with("-"));
2022-
2023+
assert(Feature[0] == '+' || Feature[0] == '-');
20232024
StringRef Name = Feature.drop_front(1);
2024-
auto TEntry = TFeatures.find(Name);
2025-
2026-
// Preserves features that are incompatible (either set to something
2027-
// different or missing) from the target features
2028-
bool MissingFromTarget = TEntry == TFeatures.end();
2029-
bool EnabledForTarget = !MissingFromTarget && TEntry->second;
2030-
bool Incompatible = EnabledForTarget != EnabledForFunc;
2031-
if (MissingFromTarget || Incompatible) {
2025+
bool Merged = !MergedNames.insert(Name).second;
2026+
if (!Merged)
20322027
MergedFeatures.push_back(Feature);
2033-
if (Incompatible)
2034-
IncompatibleFeatureNames.insert(Name);
2035-
}
20362028
}
2037-
}
2029+
};
20382030

2039-
for (StringRef Feature : TargetOpts.Features) {
2040-
if (Feature.empty())
2041-
continue;
2042-
StringRef Name = Feature.drop_front(1);
2043-
if (IncompatibleFeatureNames.contains(Name))
2044-
continue;
2045-
MergedFeatures.push_back(Feature);
2046-
}
2031+
if (FFeatures.isValid())
2032+
AddUnmergedFeatures(llvm::split(FFeatures.getValueAsString(), ','));
2033+
AddUnmergedFeatures(TargetOpts.Features);
20472034

2048-
if (!MergedFeatures.empty())
2035+
if (!MergedFeatures.empty()) {
2036+
llvm::sort(MergedFeatures);
20492037
FuncAttr.addAttribute("target-features", llvm::join(MergedFeatures, ","));
2038+
}
20502039
}
20512040

20522041
void CodeGen::mergeDefaultFunctionDefinitionAttributes(

clang/test/CodeGen/link-builtin-bitcode.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int bar() { return no_attr() + attr_in_target() + attr_not_in_target() + attr_in
4343
// CHECK-LABEL: @attr_incompatible
4444
// CHECK-SAME: () #[[ATTR_INCOMPATIBLE:[0-9]+]] {
4545

46-
// CHECK: attributes #[[ATTR_BAR]] = { noinline nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="gfx90a" "target-features"="+16-bit-insts,+atomic-buffer-global-pk-add-f16-insts,+atomic-fadd-rtn-insts,+ci-insts,+dl-insts,+dot1-insts,+dot10-insts,+dot2-insts,+dot3-insts,+dot4-insts,+dot5-insts,+dot6-insts,+dot7-insts,+dpp,+gfx8-insts,+gfx9-insts,+gfx90a-insts,+mai-insts,+s-memrealtime,+s-memtime-inst,+wavefrontsize64" }
47-
// CHECK: attributes #[[ATTR_COMPATIBLE]] = { convergent noinline nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="gfx90a" "target-features"="+16-bit-insts,+atomic-buffer-global-pk-add-f16-insts,+atomic-fadd-rtn-insts,+ci-insts,+dl-insts,+dot1-insts,+dot10-insts,+dot2-insts,+dot3-insts,+dot4-insts,+dot5-insts,+dot6-insts,+dot7-insts,+dpp,+gfx8-insts,+gfx9-insts,+gfx90a-insts,+gws,+image-insts,+mai-insts,+s-memrealtime,+s-memtime-inst,+wavefrontsize64" }
48-
// CHECK: attributes #[[ATTR_EXTEND]] = { convergent noinline nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="gfx90a" "target-features"="+extended-image-insts,+16-bit-insts,+atomic-buffer-global-pk-add-f16-insts,+atomic-fadd-rtn-insts,+ci-insts,+dl-insts,+dot1-insts,+dot10-insts,+dot2-insts,+dot3-insts,+dot4-insts,+dot5-insts,+dot6-insts,+dot7-insts,+dpp,+gfx8-insts,+gfx9-insts,+gfx90a-insts,+gws,+image-insts,+mai-insts,+s-memrealtime,+s-memtime-inst,+wavefrontsize64" }
49-
// CHECK: attributes #[[ATTR_INCOMPATIBLE]] = { convergent noinline nounwind optnone "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="gfx90a" "target-features"="-gfx9-insts,+16-bit-insts,+atomic-buffer-global-pk-add-f16-insts,+atomic-fadd-rtn-insts,+ci-insts,+dl-insts,+dot1-insts,+dot10-insts,+dot2-insts,+dot3-insts,+dot4-insts,+dot5-insts,+dot6-insts,+dot7-insts,+dpp,+gfx8-insts,+gfx90a-insts,+gws,+image-insts,+mai-insts,+s-memrealtime,+s-memtime-inst,+wavefrontsize64" }
46+
// CHECK: attributes #[[ATTR_BAR]] = { {{.*}} "target-cpu"="gfx90a" "target-features"="+16-bit-insts,+atomic-buffer-global-pk-add-f16-insts,+atomic-fadd-rtn-insts,+ci-insts,+dl-insts,+dot1-insts,+dot10-insts,+dot2-insts,+dot3-insts,+dot4-insts,+dot5-insts,+dot6-insts,+dot7-insts,+dpp,+gfx8-insts,+gfx9-insts,+gfx90a-insts,+mai-insts,+s-memrealtime,+s-memtime-inst,+wavefrontsize64" }
47+
// CHECK: attributes #[[ATTR_COMPATIBLE]] = { {{.*}} "target-cpu"="gfx90a" "target-features"="+16-bit-insts,+atomic-buffer-global-pk-add-f16-insts,+atomic-fadd-rtn-insts,+ci-insts,+dl-insts,+dot1-insts,+dot10-insts,+dot2-insts,+dot3-insts,+dot4-insts,+dot5-insts,+dot6-insts,+dot7-insts,+dpp,+gfx8-insts,+gfx9-insts,+gfx90a-insts,+gws,+image-insts,+mai-insts,+s-memrealtime,+s-memtime-inst,+wavefrontsize64" }
48+
// CHECK: attributes #[[ATTR_EXTEND]] = { {{.*}} "target-cpu"="gfx90a" "target-features"="+16-bit-insts,+atomic-buffer-global-pk-add-f16-insts,+atomic-fadd-rtn-insts,+ci-insts,+dl-insts,+dot1-insts,+dot10-insts,+dot2-insts,+dot3-insts,+dot4-insts,+dot5-insts,+dot6-insts,+dot7-insts,+dpp,+extended-image-insts,+gfx8-insts,+gfx9-insts,+gfx90a-insts,+gws,+image-insts,+mai-insts,+s-memrealtime,+s-memtime-inst,+wavefrontsize64" }
49+
// CHECK: attributes #[[ATTR_INCOMPATIBLE]] = { {{.*}} "target-cpu"="gfx90a" "target-features"="+16-bit-insts,+atomic-buffer-global-pk-add-f16-insts,+atomic-fadd-rtn-insts,+ci-insts,+dl-insts,+dot1-insts,+dot10-insts,+dot2-insts,+dot3-insts,+dot4-insts,+dot5-insts,+dot6-insts,+dot7-insts,+dpp,+gfx8-insts,+gfx90a-insts,+gws,+image-insts,+mai-insts,+s-memrealtime,+s-memtime-inst,+wavefrontsize64,-gfx9-insts" }

0 commit comments

Comments
 (0)