Skip to content

Commit af1934e

Browse files
Ami-zhangsvkeerthy
authored andcommitted
[Clang][LoongArch] Support target attribute for function (#140700)
This adds support under LoongArch for the target("..") attributes. The supported formats are: - "arch=<arch>" strings, that specify the architecture features for a function as per the -march=arch option. - "tune=<cpu>" strings, that specify the tune-cpu cpu for a function as per -mtune. - "<feature>", "no-<feature>" enabled/disables the specific feature.
1 parent cec85f0 commit af1934e

File tree

7 files changed

+216
-0
lines changed

7 files changed

+216
-0
lines changed

clang/lib/Basic/Targets/LoongArch.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,73 @@ bool LoongArchTargetInfo::handleTargetFeatures(
393393
return true;
394394
}
395395

396+
enum class AttrFeatureKind { Arch, Tune, NoFeature, Feature };
397+
398+
static std::pair<AttrFeatureKind, llvm::StringRef>
399+
getAttrFeatureTypeAndValue(llvm::StringRef AttrFeature) {
400+
if (auto Split = AttrFeature.split("="); !Split.second.empty()) {
401+
if (Split.first.trim() == "arch")
402+
return {AttrFeatureKind::Arch, Split.second.trim()};
403+
if (Split.first.trim() == "tune")
404+
return {AttrFeatureKind::Tune, Split.second.trim()};
405+
}
406+
if (AttrFeature.starts_with("no-"))
407+
return {AttrFeatureKind::NoFeature, AttrFeature.drop_front(3)};
408+
return {AttrFeatureKind::Feature, AttrFeature};
409+
}
410+
411+
ParsedTargetAttr
412+
LoongArchTargetInfo::parseTargetAttr(StringRef Features) const {
413+
ParsedTargetAttr Ret;
414+
if (Features == "default")
415+
return Ret;
416+
SmallVector<StringRef, 1> AttrFeatures;
417+
Features.split(AttrFeatures, ",");
418+
419+
for (auto &Feature : AttrFeatures) {
420+
auto [Kind, Value] = getAttrFeatureTypeAndValue(Feature.trim());
421+
422+
switch (Kind) {
423+
case AttrFeatureKind::Arch: {
424+
if (llvm::LoongArch::isValidArchName(Value) || Value == "la64v1.0" ||
425+
Value == "la64v1.1") {
426+
std::vector<llvm::StringRef> ArchFeatures;
427+
if (llvm::LoongArch::getArchFeatures(Value, ArchFeatures)) {
428+
Ret.Features.insert(Ret.Features.end(), ArchFeatures.begin(),
429+
ArchFeatures.end());
430+
}
431+
432+
if (!Ret.CPU.empty())
433+
Ret.Duplicate = "arch=";
434+
else if (Value == "la64v1.0" || Value == "la64v1.1")
435+
Ret.CPU = "loongarch64";
436+
else
437+
Ret.CPU = Value;
438+
} else {
439+
Ret.Features.push_back("!arch=" + Value.str());
440+
}
441+
break;
442+
}
443+
444+
case AttrFeatureKind::Tune:
445+
if (!Ret.Tune.empty())
446+
Ret.Duplicate = "tune=";
447+
else
448+
Ret.Tune = Value;
449+
break;
450+
451+
case AttrFeatureKind::NoFeature:
452+
Ret.Features.push_back("-" + Value.str());
453+
break;
454+
455+
case AttrFeatureKind::Feature:
456+
Ret.Features.push_back("+" + Value.str());
457+
break;
458+
}
459+
}
460+
return Ret;
461+
}
462+
396463
bool LoongArchTargetInfo::isValidCPUName(StringRef Name) const {
397464
return llvm::LoongArch::isValidCPUName(Name);
398465
}
@@ -401,3 +468,7 @@ void LoongArchTargetInfo::fillValidCPUList(
401468
SmallVectorImpl<StringRef> &Values) const {
402469
llvm::LoongArch::fillValidCPUList(Values);
403470
}
471+
472+
bool LoongArchTargetInfo::isValidFeatureName(StringRef Name) const {
473+
return llvm::LoongArch::isValidFeatureName(Name);
474+
}

clang/lib/Basic/Targets/LoongArch.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
101101
bool handleTargetFeatures(std::vector<std::string> &Features,
102102
DiagnosticsEngine &Diags) override;
103103

104+
ParsedTargetAttr parseTargetAttr(StringRef Str) const override;
105+
bool supportsTargetAttributeTune() const override { return true; }
106+
104107
bool
105108
initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
106109
StringRef CPU,
@@ -110,6 +113,7 @@ class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
110113

111114
bool isValidCPUName(StringRef Name) const override;
112115
void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
116+
bool isValidFeatureName(StringRef Name) const override;
113117
};
114118

115119
class LLVM_LIBRARY_VISIBILITY LoongArch32TargetInfo

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3194,6 +3194,17 @@ bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
31943194
}
31953195
}
31963196

3197+
if (Context.getTargetInfo().getTriple().isLoongArch()) {
3198+
for (const auto &Feature : ParsedAttrs.Features) {
3199+
StringRef CurFeature = Feature;
3200+
if (CurFeature.starts_with("!arch=")) {
3201+
StringRef ArchValue = CurFeature.split("=").second.trim();
3202+
return Diag(LiteralLoc, diag::err_attribute_unsupported)
3203+
<< "target(arch=..)" << ArchValue;
3204+
}
3205+
}
3206+
}
3207+
31973208
if (ParsedAttrs.Duplicate != "")
31983209
return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
31993210
<< Duplicate << None << ParsedAttrs.Duplicate << Target;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-globals all --version 5
2+
// RUN: %clang --target=loongarch64-linux-gnu %s -S -emit-llvm -o - \
3+
// RUN: | FileCheck %s
4+
5+
__attribute__((target("div32")))
6+
// CHECK-LABEL: define dso_local void @testdiv32(
7+
// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
8+
// CHECK-NEXT: [[ENTRY:.*:]]
9+
// CHECK-NEXT: ret void
10+
//
11+
void testdiv32() {}
12+
13+
__attribute__((target("arch=loongarch64")))
14+
// CHECK-LABEL: define dso_local void @testLoongarch64(
15+
// CHECK-SAME: ) #[[ATTR1:[0-9]+]] {
16+
// CHECK-NEXT: [[ENTRY:.*:]]
17+
// CHECK-NEXT: ret void
18+
//
19+
void testLoongarch64() {}
20+
21+
__attribute__((target("arch=la64v1.0")))
22+
// CHECK-LABEL: define dso_local void @testLa64v10(
23+
// CHECK-SAME: ) #[[ATTR1]] {
24+
// CHECK-NEXT: [[ENTRY:.*:]]
25+
// CHECK-NEXT: ret void
26+
//
27+
void testLa64v10() {}
28+
29+
__attribute__((target("arch=la64v1.1")))
30+
// CHECK-LABEL: define dso_local void @testLa64v11(
31+
// CHECK-SAME: ) #[[ATTR2:[0-9]+]] {
32+
// CHECK-NEXT: [[ENTRY:.*:]]
33+
// CHECK-NEXT: ret void
34+
//
35+
void testLa64v11() {}
36+
37+
__attribute__((target("arch=la464")))
38+
// CHECK-LABEL: define dso_local void @testLa464(
39+
// CHECK-SAME: ) #[[ATTR3:[0-9]+]] {
40+
// CHECK-NEXT: [[ENTRY:.*:]]
41+
// CHECK-NEXT: ret void
42+
//
43+
void testLa464() {}
44+
45+
__attribute__((target("arch=la664")))
46+
// CHECK-LABEL: define dso_local void @testLa664(
47+
// CHECK-SAME: ) #[[ATTR4:[0-9]+]] {
48+
// CHECK-NEXT: [[ENTRY:.*:]]
49+
// CHECK-NEXT: ret void
50+
//
51+
void testLa664() {}
52+
53+
__attribute__((target("arch=la664, no-div32")))
54+
// CHECK-LABEL: define dso_local void @la664Nodiv32(
55+
// CHECK-SAME: ) #[[ATTR5:[0-9]+]] {
56+
// CHECK-NEXT: [[ENTRY:.*:]]
57+
// CHECK-NEXT: ret void
58+
//
59+
void la664Nodiv32() {}
60+
61+
__attribute__((target("tune=la464")))
62+
// CHECK-LABEL: define dso_local void @tuneLa464(
63+
// CHECK-SAME: ) #[[ATTR6:[0-9]+]] {
64+
// CHECK-NEXT: [[ENTRY:.*:]]
65+
// CHECK-NEXT: ret void
66+
//
67+
void tuneLa464() {}
68+
69+
__attribute__((target("arch=la464, tune=la664")))
70+
// CHECK-LABEL: define dso_local void @archLa464tuneLa664(
71+
// CHECK-SAME: ) #[[ATTR7:[0-9]+]] {
72+
// CHECK-NEXT: [[ENTRY:.*:]]
73+
// CHECK-NEXT: ret void
74+
//
75+
void archLa464tuneLa664() {}
76+
77+
//.
78+
// CHECK: attributes #[[ATTR0]] = { noinline nounwind optnone "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="loongarch64" "target-features"="+64bit,+d,+div32,+f,+lsx,+ual" }
79+
// CHECK: attributes #[[ATTR1]] = { noinline nounwind optnone "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="loongarch64" "target-features"="+64bit,+d,+f,+lsx,+ual" }
80+
// CHECK: attributes #[[ATTR2]] = { noinline nounwind optnone "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="loongarch64" "target-features"="+64bit,+d,+div32,+f,+frecipe,+lam-bh,+lamcas,+ld-seq-sa,+lsx,+scq,+ual" }
81+
// CHECK: attributes #[[ATTR3]] = { noinline nounwind optnone "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="la464" "target-features"="+64bit,+d,+f,+lasx,+lsx,+ual" }
82+
// CHECK: attributes #[[ATTR4]] = { noinline nounwind optnone "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="la664" "target-features"="+64bit,+d,+div32,+f,+frecipe,+lam-bh,+lamcas,+lasx,+ld-seq-sa,+lsx,+scq,+ual" }
83+
// CHECK: attributes #[[ATTR5]] = { noinline nounwind optnone "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="la664" "target-features"="+64bit,+d,+f,+frecipe,+lam-bh,+lamcas,+lasx,+ld-seq-sa,+lsx,+scq,+ual,-div32" }
84+
// CHECK: attributes #[[ATTR6]] = { noinline nounwind optnone "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="loongarch64" "target-features"="+64bit,+d,+f,+lsx,+ual" "tune-cpu"="la464" }
85+
// CHECK: attributes #[[ATTR7]] = { noinline nounwind optnone "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="la464" "target-features"="+64bit,+d,+f,+lasx,+lsx,+ual" "tune-cpu"="la664" }
86+
//.
87+
// CHECK: [[META0:![0-9]+]] = !{i32 1, !"wchar_size", i32 4}
88+
// CHECK: [[META1:![0-9]+]] = !{i32 8, !"PIC Level", i32 2}
89+
// CHECK: [[META2:![0-9]+]] = !{i32 7, !"PIE Level", i32 2}
90+
// CHECK: [[META3:![0-9]+]] = !{i32 7, !"frame-pointer", i32 2}
91+
// CHECK: [[META4:![0-9]+]] = !{!"{{.*}}clang version {{.*}}"}
92+
//.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang_cc1 -triple loongarch64-linux-gnu -fsyntax-only -verify %s
2+
3+
// expected-error@+1 {{function multiversioning is not supported on the current target}}
4+
void __attribute__((target("default"))) bar(void) {}
5+
6+
// expected-error@+1 {{target(arch=..) attribute is not supported on targets missing invalid; specify an appropriate -march= or -mcpu=}}
7+
void __attribute__((target("arch=invalid"))) foo(void) {}
8+
9+
// expected-warning@+1 {{unsupported '+div32' in the 'target' attribute string; 'target' attribute ignored}}
10+
void __attribute__((target("+div32"))) plusfeature(void) {}
11+
12+
// expected-warning@+1 {{unsupported '-div32' in the 'target' attribute string; 'target' attribute ignored}}
13+
void __attribute__((target("-div32"))) minusfeature(void) {}
14+
15+
// expected-warning@+1 {{unsupported 'aaa' in the 'target' attribute string; 'target' attribute ignored}}
16+
int __attribute__((target("aaa"))) test_feature(void) { return 4; }
17+
18+
// expected-warning@+1 {{unsupported 'aaa' in the 'target' attribute string; 'target' attribute ignored}}
19+
int __attribute__((target("no-aaa"))) test_nofeature(void) { return 4; }
20+
21+
// expected-warning@+1 {{duplicate 'arch=' in the 'target' attribute string; 'target' attribute ignored}}
22+
int __attribute__((target("arch=la464,arch=la664"))) test_duplarch(void) { return 4; }
23+
24+
// expected-warning@+1 {{unknown tune CPU 'la64v1.0' in the 'target' attribute string; 'target' attribute ignored}}
25+
int __attribute__((target("tune=la64v1.0"))) test_tune(void) { return 4; }

llvm/include/llvm/TargetParser/LoongArchTargetParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ struct ArchInfo {
8585
};
8686

8787
bool isValidArchName(StringRef Arch);
88+
bool isValidFeatureName(StringRef Feature);
8889
bool getArchFeatures(StringRef Arch, std::vector<StringRef> &Features);
8990
bool isValidCPUName(StringRef TuneCPU);
9091
void fillValidCPUList(SmallVectorImpl<StringRef> &Values);

llvm/lib/TargetParser/LoongArchTargetParser.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ bool LoongArch::isValidArchName(StringRef Arch) {
3434
return false;
3535
}
3636

37+
bool LoongArch::isValidFeatureName(StringRef Feature) {
38+
if (Feature.starts_with("+") || Feature.starts_with("-"))
39+
return false;
40+
for (const auto &F : AllFeatures) {
41+
StringRef CanonicalName =
42+
F.Name.starts_with("+") ? F.Name.drop_front() : F.Name;
43+
if (CanonicalName == Feature)
44+
return true;
45+
}
46+
return false;
47+
}
48+
3749
bool LoongArch::getArchFeatures(StringRef Arch,
3850
std::vector<StringRef> &Features) {
3951
for (const auto A : AllArchs) {

0 commit comments

Comments
 (0)