Skip to content

Commit 6afda56

Browse files
authored
[RISCV] Store RVC and TSO ELF flags explicitly in RISCVTargetStreamer. NFCI (#83344)
Instead of caching STI in the RISCVELFTargetStreamer, store the two flags we need from it. My goal is to allow RISCVAsmPrinter to override these flags using IR module metadata for LTO. So they need to be separated from the STI used to construct the TargetStreamer. This patch should be NFC as long as no one is changing the contents of the STI that was used to construct the TargetStreamer between the constructor and the use of the flags.
1 parent 6c7805d commit 6afda56

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ using namespace llvm;
3131
// This part is for ELF object output.
3232
RISCVTargetELFStreamer::RISCVTargetELFStreamer(MCStreamer &S,
3333
const MCSubtargetInfo &STI)
34-
: RISCVTargetStreamer(S), CurrentVendor("riscv"), STI(STI) {
34+
: RISCVTargetStreamer(S), CurrentVendor("riscv") {
3535
MCAssembler &MCA = getStreamer().getAssembler();
3636
const FeatureBitset &Features = STI.getFeatureBits();
3737
auto &MAB = static_cast<RISCVAsmBackend &>(MCA.getBackend());
3838
setTargetABI(RISCVABI::computeTargetABI(STI.getTargetTriple(), Features,
3939
MAB.getTargetOptions().getABIName()));
40+
setFlagsFromFeatures(STI);
4041
// `j label` in `.option norelax; j label; .option relax; ...; label:` needs a
4142
// relocation to ensure the jump target is correct after linking. This is due
4243
// to a limitation that shouldForceRelocation has to make the decision upfront
@@ -91,10 +92,9 @@ void RISCVTargetELFStreamer::finish() {
9192

9293
unsigned EFlags = MCA.getELFHeaderEFlags();
9394

94-
if (STI.hasFeature(RISCV::FeatureStdExtC) ||
95-
STI.hasFeature(RISCV::FeatureStdExtZca))
95+
if (hasRVC())
9696
EFlags |= ELF::EF_RISCV_RVC;
97-
if (STI.hasFeature(RISCV::FeatureStdExtZtso))
97+
if (hasTSO())
9898
EFlags |= ELF::EF_RISCV_TSO;
9999

100100
switch (ABI) {

llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.h

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class RISCVTargetELFStreamer : public RISCVTargetStreamer {
4646
StringRef CurrentVendor;
4747

4848
MCSection *AttributeSection = nullptr;
49-
const MCSubtargetInfo &STI;
5049

5150
void emitAttribute(unsigned Attribute, unsigned Value) override;
5251
void emitTextAttribute(unsigned Attribute, StringRef String) override;

llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ void RISCVTargetStreamer::setTargetABI(RISCVABI::ABI ABI) {
4848
TargetABI = ABI;
4949
}
5050

51+
void RISCVTargetStreamer::setFlagsFromFeatures(const MCSubtargetInfo &STI) {
52+
HasRVC = STI.hasFeature(RISCV::FeatureStdExtC) ||
53+
STI.hasFeature(RISCV::FeatureStdExtZca);
54+
HasTSO = STI.hasFeature(RISCV::FeatureStdExtZtso);
55+
}
56+
5157
void RISCVTargetStreamer::emitTargetAttributes(const MCSubtargetInfo &STI,
5258
bool EmitStackAlign) {
5359
if (EmitStackAlign) {

llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ struct RISCVOptionArchArg {
3333

3434
class RISCVTargetStreamer : public MCTargetStreamer {
3535
RISCVABI::ABI TargetABI = RISCVABI::ABI_Unknown;
36+
bool HasRVC = false;
37+
bool HasTSO = false;
3638

3739
public:
3840
RISCVTargetStreamer(MCStreamer &S);
@@ -58,6 +60,9 @@ class RISCVTargetStreamer : public MCTargetStreamer {
5860
void emitTargetAttributes(const MCSubtargetInfo &STI, bool EmitStackAlign);
5961
void setTargetABI(RISCVABI::ABI ABI);
6062
RISCVABI::ABI getTargetABI() const { return TargetABI; }
63+
void setFlagsFromFeatures(const MCSubtargetInfo &STI);
64+
bool hasRVC() const { return HasRVC; }
65+
bool hasTSO() const { return HasTSO; }
6166
};
6267

6368
// This part is for ascii assembly output

0 commit comments

Comments
 (0)