[llvm] 3e7eab0 - [RISCV] RISCVELFTargetObjectFile: use 2-byte alignment for .text if RVC

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu May 11 13:44:42 PDT 2023


Author: Fangrui Song
Date: 2023-05-11T13:44:37-07:00
New Revision: 3e7eab099766cc92e5930a7883a014d0de8710d9

URL: https://github.com/llvm/llvm-project/commit/3e7eab099766cc92e5930a7883a014d0de8710d9
DIFF: https://github.com/llvm/llvm-project/commit/3e7eab099766cc92e5930a7883a014d0de8710d9.diff

LOG: [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

Added: 
    

Modified: 
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp
    llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h
    llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
    llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
    llvm/test/CodeGen/RISCV/align.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp
index 6118d0ddb836b..ac7d3b785ab1b 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.cpp
@@ -17,10 +17,13 @@
 
 using namespace llvm;
 
+unsigned
+RISCVMCObjectFileInfo::getTextSectionAlignment(const MCSubtargetInfo &STI) {
+  bool RVC = STI.hasFeature(RISCV::FeatureStdExtC) ||
+             STI.hasFeature(RISCV::FeatureStdExtZca);
+  return RVC ? 2 : 4;
+}
+
 unsigned RISCVMCObjectFileInfo::getTextSectionAlignment() const {
-  const MCSubtargetInfo *STI = getContext().getSubtargetInfo();
-  return (STI->hasFeature(RISCV::FeatureStdExtC) ||
-          STI->hasFeature(RISCV::FeatureStdExtZca))
-             ? 2
-             : 4;
+  return getTextSectionAlignment(*getContext().getSubtargetInfo());
 }

diff  --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h
index ee686ba7917b3..c2ef160c51072 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCObjectFileInfo.h
@@ -14,11 +14,13 @@
 #define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVMCOBJECTFILEINFO_H
 
 #include "llvm/MC/MCObjectFileInfo.h"
+#include "llvm/MC/MCSubtargetInfo.h"
 
 namespace llvm {
 
 class RISCVMCObjectFileInfo : public MCObjectFileInfo {
 public:
+  static unsigned getTextSectionAlignment(const MCSubtargetInfo &STI);
   unsigned getTextSectionAlignment() const override;
 };
 

diff  --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
index 1d80ea4a8a87a..7c9e57e6eef3c 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "RISCVTargetObjectFile.h"
+#include "MCTargetDesc/RISCVMCObjectFileInfo.h"
 #include "RISCVTargetMachine.h"
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/MC/MCContext.h"
@@ -14,6 +15,11 @@
 
 using namespace llvm;
 
+unsigned RISCVELFTargetObjectFile::getTextSectionAlignment() const {
+  return RISCVMCObjectFileInfo::getTextSectionAlignment(
+      *getContext().getSubtargetInfo());
+}
+
 void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx,
                                           const TargetMachine &TM) {
   TargetLoweringObjectFileELF::Initialize(Ctx, TM);

diff  --git a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
index 659ad40f08b1e..890effd07320a 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
+++ b/llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
@@ -20,6 +20,8 @@ class RISCVELFTargetObjectFile : public TargetLoweringObjectFileELF {
   unsigned SSThreshold = 8;
 
 public:
+  unsigned getTextSectionAlignment() const override;
+
   void Initialize(MCContext &Ctx, const TargetMachine &TM) override;
 
   /// Return true if this global address should be placed into small data/bss

diff  --git a/llvm/test/CodeGen/RISCV/align.ll b/llvm/test/CodeGen/RISCV/align.ll
index f044f3bb156a4..5807fc14efc29 100644
--- a/llvm/test/CodeGen/RISCV/align.ll
+++ b/llvm/test/CodeGen/RISCV/align.ll
@@ -2,6 +2,14 @@
 ; RUN:   | FileCheck %s -check-prefix=RV32I
 ; RUN: llc -mtriple=riscv32 -mattr=+c -verify-machineinstrs < %s \
 ; RUN:   | FileCheck %s -check-prefix=RV32C
+; RUN: llc -filetype=obj -mtriple=riscv32 < %s -o %t
+; RUN: llvm-readelf -S %t | FileCheck %s --check-prefixes=SEC,SEC-I
+; RUN: llc -filetype=obj -mtriple=riscv32 -mattr=+c < %s -o %t
+; RUN: llvm-readelf -S %t | FileCheck %s --check-prefixes=SEC,SEC-C
+
+; SEC:   Name   Type     Address  Off      Size     ES Flg Lk Inf Al
+; SEC-I: .text  PROGBITS 00000000 [[#%x,]] [[#%x,]] 00  AX  0   0  4
+; SEC-C: .text  PROGBITS 00000000 [[#%x,]] [[#%x,]] 00  AX  0   0  2
 
 define void @foo() {
 ;RV32I: .p2align 2


        


More information about the llvm-commits mailing list