Skip to content

Commit 3e7eab0

Browse files
committed
[RISCV] RISCVELFTargetObjectFile: use 2-byte alignment for .text if RVC
For the "C" Standard Extension/Zca, D45560 enabled 2-byte alignment for assembly output (e.g. `clang -S a.c`) and D102052 enabled 2-byte alignment for assembly input and object file output (e.g. `clang -c a.s`). This patch ports the behavior for code generation and object file output by adding RISCVELFTargetObjectFile::getTextSectionAlignment (e.g. `clang -c a.c`). Reviewed By: craig.topper Differential Revision: https://reviews.llvm.org/D150240
1 parent 2895c4c commit 3e7eab0

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717

1818
using namespace llvm;
1919

20+
unsigned
21+
RISCVMCObjectFileInfo::getTextSectionAlignment(const MCSubtargetInfo &STI) {
22+
bool RVC = STI.hasFeature(RISCV::FeatureStdExtC) ||
23+
STI.hasFeature(RISCV::FeatureStdExtZca);
24+
return RVC ? 2 : 4;
25+
}
26+
2027
unsigned RISCVMCObjectFileInfo::getTextSectionAlignment() const {
21-
const MCSubtargetInfo *STI = getContext().getSubtargetInfo();
22-
return (STI->hasFeature(RISCV::FeatureStdExtC) ||
23-
STI->hasFeature(RISCV::FeatureStdExtZca))
24-
? 2
25-
: 4;
28+
return getTextSectionAlignment(*getContext().getSubtargetInfo());
2629
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
#define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVMCOBJECTFILEINFO_H
1515

1616
#include "llvm/MC/MCObjectFileInfo.h"
17+
#include "llvm/MC/MCSubtargetInfo.h"
1718

1819
namespace llvm {
1920

2021
class RISCVMCObjectFileInfo : public MCObjectFileInfo {
2122
public:
23+
static unsigned getTextSectionAlignment(const MCSubtargetInfo &STI);
2224
unsigned getTextSectionAlignment() const override;
2325
};
2426

llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "RISCVTargetObjectFile.h"
10+
#include "MCTargetDesc/RISCVMCObjectFileInfo.h"
1011
#include "RISCVTargetMachine.h"
1112
#include "llvm/BinaryFormat/ELF.h"
1213
#include "llvm/MC/MCContext.h"
1314
#include "llvm/MC/MCSectionELF.h"
1415

1516
using namespace llvm;
1617

18+
unsigned RISCVELFTargetObjectFile::getTextSectionAlignment() const {
19+
return RISCVMCObjectFileInfo::getTextSectionAlignment(
20+
*getContext().getSubtargetInfo());
21+
}
22+
1723
void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
1824
const TargetMachine &TM) {
1925
TargetLoweringObjectFileELF::Initialize(Ctx, TM);

llvm/lib/Target/RISCV/RISCVTargetObjectFile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class RISCVELFTargetObjectFile : public TargetLoweringObjectFileELF {
2020
unsigned SSThreshold = 8;
2121

2222
public:
23+
unsigned getTextSectionAlignment() const override;
24+
2325
void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
2426

2527
/// Return true if this global address should be placed into small data/bss

llvm/test/CodeGen/RISCV/align.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
; RUN: | FileCheck %s -check-prefix=RV32I
33
; RUN: llc -mtriple=riscv32 -mattr=+c -verify-machineinstrs < %s \
44
; RUN: | FileCheck %s -check-prefix=RV32C
5+
; RUN: llc -filetype=obj -mtriple=riscv32 < %s -o %t
6+
; RUN: llvm-readelf -S %t | FileCheck %s --check-prefixes=SEC,SEC-I
7+
; RUN: llc -filetype=obj -mtriple=riscv32 -mattr=+c < %s -o %t
8+
; RUN: llvm-readelf -S %t | FileCheck %s --check-prefixes=SEC,SEC-C
9+
10+
; SEC: Name Type Address Off Size ES Flg Lk Inf Al
11+
; SEC-I: .text PROGBITS 00000000 [[#%x,]] [[#%x,]] 00 AX 0 0 4
12+
; SEC-C: .text PROGBITS 00000000 [[#%x,]] [[#%x,]] 00 AX 0 0 2
513

614
define void @foo() {
715
;RV32I: .p2align 2

0 commit comments

Comments
 (0)