[lld] [RISCV][LLD] Zcmt RISC-V extension in lld (PR #163142)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 14 11:07:32 PDT 2025
================
@@ -1476,3 +1538,218 @@ void elf::mergeRISCVAttributesSections(Ctx &ctx) {
}
void elf::setRISCVTargetInfo(Ctx &ctx) { ctx.target.reset(new RISCV(ctx)); }
+
+TableJumpSection::TableJumpSection(Ctx &ctx)
+ : SyntheticSection(ctx, ".riscv.jvt", SHT_PROGBITS,
+ SHF_ALLOC | SHF_EXECINSTR, jvtAlign) {}
+
+void TableJumpSection::addCMJTEntryCandidate(const Symbol *symbol,
+ int csReduction) {
+ addEntry(symbol, CMJTEntryCandidates, csReduction);
+}
+
+int TableJumpSection::getCMJTEntryIndex(const Symbol *symbol) {
+ uint32_t index = getIndex(symbol, maxCMJTEntrySize, finalizedCMJTEntries);
+ return index < finalizedCMJTEntries.size() ? (int)(startCMJTEntryIdx + index)
+ : -1;
+}
+
+void TableJumpSection::addCMJALTEntryCandidate(const Symbol *symbol,
+ int csReduction) {
+ addEntry(symbol, CMJALTEntryCandidates, csReduction);
+}
+
+int TableJumpSection::getCMJALTEntryIndex(const Symbol *symbol) {
+ uint32_t index = getIndex(symbol, maxCMJALTEntrySize, finalizedCMJALTEntries);
+ return index < finalizedCMJALTEntries.size()
+ ? (int)(startCMJALTEntryIdx + index)
+ : -1;
+}
+
+void TableJumpSection::addEntry(
+ const Symbol *symbol, llvm::DenseMap<const Symbol *, int> &entriesList,
+ int csReduction) {
+ entriesList[symbol] += csReduction;
+}
+
+uint32_t TableJumpSection::getIndex(
+ const Symbol *symbol, uint32_t maxSize,
+ SmallVector<llvm::detail::DenseMapPair<const Symbol *, int>, 0>
+ &entriesList) {
+ // Find this symbol in the ordered list of entries if it exists.
+ assert(maxSize >= entriesList.size() &&
+ "Finalized vector of entries exceeds maximum");
+ auto idx = std::find_if(
+ entriesList.begin(), entriesList.end(),
+ [symbol](llvm::detail::DenseMapPair<const Symbol *, int> &e) {
+ return e.first == symbol;
+ });
+
+ if (idx == entriesList.end())
+ return entriesList.size();
+ return idx - entriesList.begin();
+}
+
+void TableJumpSection::scanTableJumpEntries(const InputSection &sec) const {
+ for (auto [i, r] : llvm::enumerate(sec.relocations)) {
+ Defined *definedSymbol = dyn_cast<Defined>(r.sym);
+ if (!definedSymbol)
+ continue;
+ if (i + 1 == sec.relocs().size() ||
+ sec.relocs()[i + 1].type != R_RISCV_RELAX)
+ continue;
+ switch (r.type) {
+ case R_RISCV_JAL:
+ case R_RISCV_CALL:
+ case R_RISCV_CALL_PLT: {
+ const uint32_t jalr =
+ read32le(sec.contentMaybeDecompress().data() + r.offset +
+ (r.type == R_RISCV_JAL ? 0 : 4));
+ const uint8_t rd = extractBits(jalr, 11, 7);
+
+ int csReduction = 6;
+ if (sec.relaxAux->relocTypes[i] == R_RISCV_RVC_JUMP)
+ continue;
+ else if (sec.relaxAux->relocTypes[i] == R_RISCV_JAL)
+ csReduction = 2;
+
+ if (rd == 0)
+ ctx.in.riscvTableJumpSection->addCMJTEntryCandidate(r.sym, csReduction);
+ else if (rd == X_RA)
+ ctx.in.riscvTableJumpSection->addCMJALTEntryCandidate(r.sym,
+ csReduction);
+ }
+ }
+ }
+}
+
+void TableJumpSection::finalizeContents() {
+ if (isFinalized)
+ return;
+ isFinalized = true;
+
+ finalizedCMJTEntries = finalizeEntry(CMJTEntryCandidates, maxCMJTEntrySize);
+ CMJTEntryCandidates.clear();
+ int32_t CMJTSizeReduction = getSizeReduction();
+ finalizedCMJALTEntries =
+ finalizeEntry(CMJALTEntryCandidates, maxCMJALTEntrySize);
+ CMJALTEntryCandidates.clear();
+
+ if (finalizedCMJALTEntries.size() > 0 &&
+ getSizeReduction() < CMJTSizeReduction) {
+ // Stop relax to cm.jalt if there will be the code reduction of cm.jalt is
----------------
topperc wrote:
This comment is very hard to read.
https://github.com/llvm/llvm-project/pull/163142
More information about the llvm-commits
mailing list