Skip to content

Commit d037f53

Browse files
committed
[Loongarch] Refactor target attribute parsing
Introduce AttrFeatureKind enum to classify target attributes, and implement getAttrFeatureTypeAndValue() helper for centralized parsing.
1 parent 1f521b6 commit d037f53

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

clang/lib/Basic/Targets/LoongArch.cpp

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,21 @@ bool LoongArchTargetInfo::handleTargetFeatures(
388388
return true;
389389
}
390390

391+
enum class AttrFeatureKind { Arch, Tune, NoFeature, Feature };
392+
393+
static std::pair<AttrFeatureKind, llvm::StringRef>
394+
getAttrFeatureTypeAndValue(llvm::StringRef AttrFeature) {
395+
if (auto Split = AttrFeature.split("="); !Split.second.empty()) {
396+
if (Split.first.trim() == "arch")
397+
return {AttrFeatureKind::Arch, Split.second.trim()};
398+
if (Split.first.trim() == "tune")
399+
return {AttrFeatureKind::Tune, Split.second.trim()};
400+
}
401+
if (AttrFeature.starts_with("no-"))
402+
return {AttrFeatureKind::NoFeature, AttrFeature.drop_front(3)};
403+
return {AttrFeatureKind::Feature, AttrFeature};
404+
}
405+
391406
ParsedTargetAttr
392407
LoongArchTargetInfo::parseTargetAttr(StringRef Features) const {
393408
ParsedTargetAttr Ret;
@@ -397,37 +412,44 @@ LoongArchTargetInfo::parseTargetAttr(StringRef Features) const {
397412
Features.split(AttrFeatures, ",");
398413

399414
for (auto &Feature : AttrFeatures) {
400-
Feature = Feature.trim();
415+
auto [Kind, Value] = getAttrFeatureTypeAndValue(Feature.trim());
401416

402-
if (Feature.starts_with("arch=")) {
403-
StringRef ArchValue = Feature.split("=").second.trim();
404-
405-
if (llvm::LoongArch::isValidArchName(ArchValue) ||
406-
ArchValue == "la64v1.0" || ArchValue == "la64v1.1") {
417+
switch (Kind) {
418+
case AttrFeatureKind::Arch: {
419+
if (llvm::LoongArch::isValidArchName(Value) || Value == "la64v1.0" ||
420+
Value == "la64v1.1") {
407421
std::vector<llvm::StringRef> ArchFeatures;
408-
if (llvm::LoongArch::getArchFeatures(ArchValue, ArchFeatures)) {
422+
if (llvm::LoongArch::getArchFeatures(Value, ArchFeatures)) {
409423
Ret.Features.insert(Ret.Features.end(), ArchFeatures.begin(),
410424
ArchFeatures.end());
411425
}
412426

413427
if (!Ret.CPU.empty())
414428
Ret.Duplicate = "arch=";
415-
else if (ArchValue == "la64v1.0" || ArchValue == "la64v1.1")
429+
else if (Value == "la64v1.0" || Value == "la64v1.1")
416430
Ret.CPU = "loongarch64";
417431
else
418-
Ret.CPU = ArchValue;
432+
Ret.CPU = Value;
419433
} else {
420-
Ret.Features.push_back("!arch=" + ArchValue.str());
434+
Ret.Features.push_back("!arch=" + Value.str());
421435
}
422-
} else if (Feature.starts_with("tune=")) {
436+
break;
437+
}
438+
439+
case AttrFeatureKind::Tune:
423440
if (!Ret.Tune.empty())
424441
Ret.Duplicate = "tune=";
425442
else
426-
Ret.Tune = Feature.split("=").second.trim();
427-
} else if (Feature.starts_with("no-")) {
428-
Ret.Features.push_back("-" + Feature.split("-").second.str());
429-
} else {
430-
Ret.Features.push_back("+" + Feature.str());
443+
Ret.Tune = Value;
444+
break;
445+
446+
case AttrFeatureKind::NoFeature:
447+
Ret.Features.push_back("-" + Value.str());
448+
break;
449+
450+
case AttrFeatureKind::Feature:
451+
Ret.Features.push_back("+" + Value.str());
452+
break;
431453
}
432454
}
433455
return Ret;

clang/test/CodeGen/LoongArch/targetattr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ __attribute__((target("arch=la664, no-div32")))
5959
void la664Nodiv32() {}
6060

6161
__attribute__((target("tune=la464")))
62-
// CHECK-LABEL: define dso_local void @tuneLa664(
62+
// CHECK-LABEL: define dso_local void @tuneLa464(
6363
// CHECK-SAME: ) #[[ATTR6:[0-9]+]] {
6464
// CHECK-NEXT: [[ENTRY:.*:]]
6565
// CHECK-NEXT: ret void
6666
//
67-
void tuneLa664() {}
67+
void tuneLa464() {}
6868

6969
__attribute__((target("arch=la464, tune=la664")))
7070
// CHECK-LABEL: define dso_local void @archLa464tuneLa664(

0 commit comments

Comments
 (0)