Skip to content

Commit

Permalink
[RISCV] Don't expose any constructors of RISCVISAInfo publicly. (#98249)
Browse files Browse the repository at this point in the history
lld was using RISCVISAInfo(unsigned XLen,
RISCVISAUtils::OrderedExtensionMap &Exts). This required a call to
RISCVISAInfo::postProcessAndChecking to validate the RISCVISAInfo that
was created. This exposes too much about RISCVISAInfo to lld.

Replace with a new RISCVISAInfo::createFromExtMap that is responsible
for creating the object and calling postProcessAndChecking.
  • Loading branch information
topperc authored Jul 10, 2024
1 parent 7b96d13 commit 6647011
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
5 changes: 2 additions & 3 deletions lld/ELF/Arch/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1251,9 +1251,8 @@ mergeAttributesSection(const SmallVector<InputSectionBase *, 0> &sections) {
}
}

if (hasArch) {
if (auto result = RISCVISAInfo::postProcessAndChecking(
std::make_unique<RISCVISAInfo>(xlen, exts))) {
if (hasArch && xlen != 0) {
if (auto result = RISCVISAInfo::createFromExtMap(xlen, exts)) {
merged.strAttr.try_emplace(RISCVAttrs::ARCH,
saver().save((*result)->toString()));
} else {
Expand Down
12 changes: 7 additions & 5 deletions llvm/include/llvm/TargetParser/RISCVISAInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class RISCVISAInfo {
RISCVISAInfo(const RISCVISAInfo &) = delete;
RISCVISAInfo &operator=(const RISCVISAInfo &) = delete;

RISCVISAInfo(unsigned XLen, RISCVISAUtils::OrderedExtensionMap &Exts)
: XLen(XLen), FLen(0), MinVLen(0), MaxELen(0), MaxELenFp(0), Exts(Exts) {}

/// Parse RISC-V ISA info from arch string.
/// If IgnoreUnknown is set, any unrecognised extension names or
/// extensions with unrecognised versions will be silently dropped, except
Expand All @@ -48,6 +45,10 @@ class RISCVISAInfo {
static llvm::Expected<std::unique_ptr<RISCVISAInfo>>
parseFeatures(unsigned XLen, const std::vector<std::string> &Features);

static llvm::Expected<std::unique_ptr<RISCVISAInfo>>
createFromExtMap(unsigned XLen,
const RISCVISAUtils::OrderedExtensionMap &Exts);

/// Convert RISC-V ISA info to a feature vector.
std::vector<std::string> toFeatures(bool AddAllExtensions = false,
bool IgnoreUnknown = true) const;
Expand All @@ -72,8 +73,6 @@ class RISCVISAInfo {
static bool isSupportedExtensionWithVersion(StringRef Ext);
static bool isSupportedExtension(StringRef Ext, unsigned MajorVersion,
unsigned MinorVersion);
static llvm::Expected<std::unique_ptr<RISCVISAInfo>>
postProcessAndChecking(std::unique_ptr<RISCVISAInfo> &&ISAInfo);
static std::string getTargetFeatureForExtension(StringRef Ext);

private:
Expand All @@ -93,6 +92,9 @@ class RISCVISAInfo {

/// Update FLen, MinVLen, MaxELen, and MaxELenFp.
void updateImpliedLengths();

static llvm::Expected<std::unique_ptr<RISCVISAInfo>>
postProcessAndChecking(std::unique_ptr<RISCVISAInfo> &&ISAInfo);
};

} // namespace llvm
Expand Down
11 changes: 11 additions & 0 deletions llvm/lib/TargetParser/RISCVISAInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,17 @@ static Error getExtensionVersion(StringRef Ext, StringRef In, unsigned &Major,
return getError(Error);
}

llvm::Expected<std::unique_ptr<RISCVISAInfo>>
RISCVISAInfo::createFromExtMap(unsigned XLen,
const RISCVISAUtils::OrderedExtensionMap &Exts) {
assert(XLen == 32 || XLen == 64);
std::unique_ptr<RISCVISAInfo> ISAInfo(new RISCVISAInfo(XLen));

ISAInfo->Exts = Exts;

return RISCVISAInfo::postProcessAndChecking(std::move(ISAInfo));
}

llvm::Expected<std::unique_ptr<RISCVISAInfo>>
RISCVISAInfo::parseFeatures(unsigned XLen,
const std::vector<std::string> &Features) {
Expand Down

0 comments on commit 6647011

Please sign in to comment.