Skip to content

Commit be23965

Browse files
topperctstellar
authored andcommitted
[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 6cfa40e commit be23965

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
@@ -67,6 +67,7 @@
6767
#include "llvm/Support/CommandLine.h"
6868
#include "llvm/Support/ConvertUTF.h"
6969
#include "llvm/Support/ErrorHandling.h"
70+
#include "llvm/Support/RISCVISAInfo.h"
7071
#include "llvm/Support/TimeProfiler.h"
7172
#include "llvm/Support/xxhash.h"
7273
#include "llvm/TargetParser/Triple.h"
@@ -1059,6 +1060,19 @@ void CodeGenModule::Release() {
10591060
llvm::LLVMContext &Ctx = TheModule.getContext();
10601061
getModule().addModuleFlag(llvm::Module::Error, "target-abi",
10611062
llvm::MDString::get(Ctx, ABIStr));
1063+
1064+
// Add the canonical ISA string as metadata so the backend can set the ELF
1065+
// attributes correctly. We use AppendUnique so LTO will keep all of the
1066+
// unique ISA strings that were linked together.
1067+
const std::vector<std::string> &Features =
1068+
getTarget().getTargetOpts().Features;
1069+
auto ParseResult = llvm::RISCVISAInfo::parseFeatures(
1070+
Arch == llvm::Triple::riscv64 ? 64 : 32, Features);
1071+
if (!errorToBool(ParseResult.takeError()))
1072+
getModule().addModuleFlag(
1073+
llvm::Module::AppendUnique, "riscv-isa",
1074+
llvm::MDNode::get(
1075+
Ctx, llvm::MDString::get(Ctx, (*ParseResult)->toString())));
10621076
}
10631077

10641078
if (CodeGenOpts.SanitizeCfiCrossDso) {

0 commit comments

Comments
 (0)