Skip to content

[RISCV] Teach .option arch to support experimental extensions. #89727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions clang/test/Driver/riscv-option-arch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %clang --target=riscv64 -menable-experimental-extensions -c -o /dev/null %s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these clang tests? This is entirely testing llvm/ code. Never mind the weirdness of putting them in Driver/, nor the fact that they’re missing a REQUIRES: riscv-registered-target given they use the backend, they don’t belong in clang/ at all surely?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I am sorry that I didn't find out the obvious mistake. I am going to review my development process.

// RUN: ! %clang --target=riscv64 -c -o /dev/null %s 2>&1 | FileCheck -check-prefixes=CHECK-ERR %s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use not rather than !

Copy link
Contributor Author

@yetingk yetingk May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion. I will fix this if #91324 could not remove the tests.


void foo() {
asm volatile (".option arch, +zicfiss");
// CHECK-ERR: Unexpected experimental extensions.
}
5 changes: 5 additions & 0 deletions clang/test/Driver/riscv-option-arch.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# RUN: %clang --target=riscv64 -menable-experimental-extensions -c -o /dev/null %s
# RUN: ! %clang --target=riscv64 -c -o /dev/null %s 2>&1 | FileCheck -check-prefixes=CHECK-ERR %s

.option arch, +zicfiss
# CHECK-ERR: Unexpected experimental extensions.
23 changes: 14 additions & 9 deletions llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class RISCVAsmParser : public MCTargetAsmParser {
SMLoc getLoc() const { return getParser().getTok().getLoc(); }
bool isRV64() const { return getSTI().hasFeature(RISCV::Feature64Bit); }
bool isRVE() const { return getSTI().hasFeature(RISCV::FeatureStdExtE); }
bool enableExperimentalExtension() const {
return getSTI().hasFeature(RISCV::Experimental);
}

RISCVTargetStreamer &getTargetStreamer() {
assert(getParser().getStreamer().getTargetStreamer() &&
Expand Down Expand Up @@ -2824,17 +2827,19 @@ bool RISCVAsmParser::parseDirectiveOption() {
break;
}

auto Ext = llvm::lower_bound(RISCVFeatureKV, Arch);
if (Ext == std::end(RISCVFeatureKV) || StringRef(Ext->Key) != Arch ||
!RISCVISAInfo::isSupportedExtension(Arch)) {
if (isDigit(Arch.back()))
return Error(
Loc,
"Extension version number parsing not currently implemented");
if (isDigit(Arch.back()))
return Error(
Loc, "Extension version number parsing not currently implemented");

std::string Feature = RISCVISAInfo::getTargetFeatureForExtension(Arch);
if (!enableExperimentalExtension() &&
StringRef(Feature).starts_with("experimental-"))
return Error(Loc, "Unexpected experimental extensions.");
auto Ext = llvm::lower_bound(RISCVFeatureKV, Feature);
if (Ext == std::end(RISCVFeatureKV) || StringRef(Ext->Key) != Feature)
return Error(Loc, "unknown extension feature");
}

Args.emplace_back(Type, Ext->Key);
Args.emplace_back(Type, Arch.str());

if (Type == RISCVOptionArchArgType::Plus) {
FeatureBitset OldFeatureBits = STI->getFeatureBits();
Expand Down
13 changes: 10 additions & 3 deletions llvm/test/MC/RISCV/option-arch.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# RUN: llvm-mc -triple riscv32 -show-encoding < %s \
# RUN: llvm-mc -triple riscv32 -mattr=+experimental -show-encoding < %s \
# RUN: | FileCheck -check-prefixes=CHECK %s
# RUN: llvm-mc -triple riscv32 -filetype=obj < %s \
# RUN: | llvm-objdump --triple=riscv32 --mattr=+c,+m,+a,+f,+zba -d -M no-aliases - \
# RUN: llvm-mc -triple riscv32 -mattr=+experimental -filetype=obj < %s \
# RUN: | llvm-objdump --triple=riscv32 --mattr=+c,+m,+a,+f,+zba,+experimental-zicfiss -d -M no-aliases - \
# RUN: | FileCheck -check-prefixes=CHECK-INST %s

# Test '.option arch, +' and '.option arch, -' directive
Expand Down Expand Up @@ -78,6 +78,13 @@ lr.w t0, (t1)
# CHECK: encoding: [0xb3,0x22,0x73,0x20]
sh1add t0, t1, t2

# Test experimental extension
# CHECK: .option arch, +zicfiss
.option arch, +zicfiss
# CHECK-INST: sspopchk ra
# CHECK: encoding: [0x73,0xc0,0xc0,0xcd]
sspopchk ra

# Test '.option arch, <arch-string>' directive
# CHECK: .option arch, rv32i2p1_m2p0_a2p1_c2p0
.option arch, rv32i2p1_m2p0_a2p1_c2p0
Expand Down