Skip to content

Commit f45b9d9

Browse files
authored
[RISCV] Add canonical ISA string as Module metadata in IR. (#80760)
In an LTO build, we don't set the ELF attributes to indicate what extensions were compiled with. The target CPU/Attrs in RISCVTargetMachine do not get set for an LTO build. Each function gets a target-cpu/feature attribute, but this isn't usable to set ELF attributs since we wouldn't know what function to use. We can't just once since it might have been compiler with an attribute likes target_verson. This patch adds the ISA as Module metadata so we can retrieve it in the backend. Individual translation units can still be compiled with different strings so we need to collect the unique set when Modules are merged. The backend will need to combine the unique ISA strings to produce a single value for the ELF attributes. This will be done in a separate patch.
1 parent 3647ff1 commit f45b9d9

File tree

3 files changed

+209
-175
lines changed

3 files changed

+209
-175
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include "llvm/Support/CommandLine.h"
6969
#include "llvm/Support/ConvertUTF.h"
7070
#include "llvm/Support/ErrorHandling.h"
71+
#include "llvm/Support/RISCVISAInfo.h"
7172
#include "llvm/Support/TimeProfiler.h"
7273
#include "llvm/Support/xxhash.h"
7374
#include "llvm/TargetParser/Triple.h"
@@ -1057,6 +1058,19 @@ void CodeGenModule::Release() {
10571058
llvm::LLVMContext &Ctx = TheModule.getContext();
10581059
getModule().addModuleFlag(llvm::Module::Error, "target-abi",
10591060
llvm::MDString::get(Ctx, ABIStr));
1061+
1062+
// Add the canonical ISA string as metadata so the backend can set the ELF
1063+
// attributes correctly. We use AppendUnique so LTO will keep all of the
1064+
// unique ISA strings that were linked together.
1065+
const std::vector<std::string> &Features =
1066+
getTarget().getTargetOpts().Features;
1067+
auto ParseResult =
1068+
llvm::RISCVISAInfo::parseFeatures(T.isRISCV64() ? 64 : 32, Features);
1069+
if (!errorToBool(ParseResult.takeError()))
1070+
getModule().addModuleFlag(
1071+
llvm::Module::AppendUnique, "riscv-isa",
1072+
llvm::MDNode::get(
1073+
Ctx, llvm::MDString::get(Ctx, (*ParseResult)->toString())));
10601074
}
10611075

10621076
if (CodeGenOpts.SanitizeCfiCrossDso) {

0 commit comments

Comments
 (0)