Skip to content

Commit 982a586

Browse files
author
Yeting Kuo
committed
[RISCV] Emit .variant_cc directives for vector function calls.
The patch is splitted from D103435. The patch emits .variant_cc [0] for those function calls that have vector arguments or vector return values. [0]: riscv-non-isa/riscv-elf-psabi-doc#190 Initial authored by: HsiangKai Reviewed By: reames Differential Revision: https://reviews.llvm.org/D139414
1 parent 12c55eb commit 982a586

File tree

6 files changed

+84
-1
lines changed

6 files changed

+84
-1
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "RISCVTargetStreamer.h"
1414
#include "RISCVBaseInfo.h"
1515
#include "RISCVMCTargetDesc.h"
16+
#include "llvm/MC/MCSymbol.h"
1617
#include "llvm/Support/FormattedStream.h"
1718
#include "llvm/Support/RISCVAttributes.h"
1819
#include "llvm/Support/RISCVISAInfo.h"
@@ -98,6 +99,10 @@ void RISCVTargetAsmStreamer::emitDirectiveOptionNoRelax() {
9899
OS << "\t.option\tnorelax\n";
99100
}
100101

102+
void RISCVTargetAsmStreamer::emitDirectiveVariantCC(MCSymbol &Symbol) {
103+
OS << "\t.variant_cc\t" << Symbol.getName() << "\n";
104+
}
105+
101106
void RISCVTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
102107
OS << "\t.attribute\t" << Attribute << ", " << Twine(Value) << "\n";
103108
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class RISCVTargetAsmStreamer : public RISCVTargetStreamer {
6666
void emitDirectiveOptionNoRVC() override;
6767
void emitDirectiveOptionRelax() override;
6868
void emitDirectiveOptionNoRelax() override;
69+
void emitDirectiveVariantCC(MCSymbol &Symbol) override;
6970
};
7071

7172
}

llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "MCTargetDesc/RISCVMCExpr.h"
1616
#include "MCTargetDesc/RISCVTargetStreamer.h"
1717
#include "RISCV.h"
18+
#include "RISCVMachineFunctionInfo.h"
1819
#include "RISCVTargetMachine.h"
1920
#include "TargetInfo/RISCVTargetInfo.h"
2021
#include "llvm/ADT/Statistic.h"
@@ -81,6 +82,8 @@ class RISCVAsmPrinter : public AsmPrinter {
8182
void emitStartOfAsmFile(Module &M) override;
8283
void emitEndOfAsmFile(Module &M) override;
8384

85+
void emitFunctionEntryLabel() override;
86+
8487
private:
8588
void emitAttributes();
8689
};
@@ -225,6 +228,16 @@ void RISCVAsmPrinter::emitAttributes() {
225228
RTS.emitTargetAttributes(*MCSTI);
226229
}
227230

231+
void RISCVAsmPrinter::emitFunctionEntryLabel() {
232+
const auto *RMFI = MF->getInfo<RISCVMachineFunctionInfo>();
233+
if (RMFI->isVectorCall()) {
234+
auto &RTS =
235+
static_cast<RISCVTargetStreamer &>(*OutStreamer->getTargetStreamer());
236+
RTS.emitDirectiveVariantCC(*CurrentFnSym);
237+
}
238+
return AsmPrinter::emitFunctionEntryLabel();
239+
}
240+
228241
// Force static initialization.
229242
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVAsmPrinter() {
230243
RegisterAsmPrinter<RISCVAsmPrinter> X(getTheRISCV32Target());

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -12070,6 +12070,10 @@ SDValue RISCVTargetLowering::LowerFormalArguments(
1207012070
InVals.push_back(ArgValue);
1207112071
}
1207212072

12073+
if (any_of(ArgLocs,
12074+
[](CCValAssign &VA) { return VA.getLocVT().isScalableVector(); }))
12075+
MF.getInfo<RISCVMachineFunctionInfo>()->setIsVectorCall();
12076+
1207312077
if (IsVarArg) {
1207412078
ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs);
1207512079
unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs);
@@ -12540,7 +12544,7 @@ RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1254012544
const SmallVectorImpl<ISD::OutputArg> &Outs,
1254112545
const SmallVectorImpl<SDValue> &OutVals,
1254212546
const SDLoc &DL, SelectionDAG &DAG) const {
12543-
const MachineFunction &MF = DAG.getMachineFunction();
12547+
MachineFunction &MF = DAG.getMachineFunction();
1254412548
const RISCVSubtarget &STI = MF.getSubtarget<RISCVSubtarget>();
1254512549

1254612550
// Stores the assignment of the return value to a location.
@@ -12611,6 +12615,10 @@ RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1261112615
RetOps.push_back(Glue);
1261212616
}
1261312617

12618+
if (any_of(RVLocs,
12619+
[](CCValAssign &VA) { return VA.getLocVT().isScalableVector(); }))
12620+
MF.getInfo<RISCVMachineFunctionInfo>()->setIsVectorCall();
12621+
1261412622
unsigned RetOpc = RISCVISD::RET_FLAG;
1261512623
// Interrupt service routines use different return instructions.
1261612624
const Function &Func = DAG.getMachineFunction().getFunction();

llvm/lib/Target/RISCV/RISCVMachineFunctionInfo.h

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class RISCVMachineFunctionInfo : public MachineFunctionInfo {
6565
uint64_t RVVPadding = 0;
6666
/// Size of stack frame to save callee saved registers
6767
unsigned CalleeSavedStackSize = 0;
68+
/// Is there any vector argument or return?
69+
bool IsVectorCall = false;
6870

6971
/// Registers that have been sign extended from i32.
7072
SmallVector<Register, 8> SExt32Registers;
@@ -124,6 +126,9 @@ class RISCVMachineFunctionInfo : public MachineFunctionInfo {
124126

125127
void addSExt32Register(Register Reg);
126128
bool isSExt32Register(Register Reg) const;
129+
130+
bool isVectorCall() const { return IsVectorCall; }
131+
void setIsVectorCall() { IsVectorCall = true; }
127132
};
128133

129134
} // end namespace llvm
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; RUN: llc -mtriple=riscv64 -mattr=+v -o - %s | FileCheck %s --check-prefix=CHECK-ASM
2+
; RUN: llc -mtriple=riscv64 -mattr=+v -filetype=obj -o - %s \
3+
; RUN: | llvm-readobj --symbols - | FileCheck %s --check-prefix=CHECK-OBJ
4+
5+
define i32 @base_cc() {
6+
; CHECK-ASM-LABEL: base_cc:
7+
; CHECK-ASM-NOT: .variant_cc
8+
; CHECK-OBJ-LABEL: Name: base_cc
9+
; CHECK-OBJ: Other: 0
10+
ret i32 42
11+
}
12+
13+
define <4 x i32> @fixed_vector_cc_1(<4 x i32> %arg) {
14+
; CHECK-ASM: .variant_cc fixed_vector_cc_1
15+
; CHECK-ASM-NEXT: fixed_vector_cc_1:
16+
; CHECK-OBJ-LABEL: Name: fixed_vector_cc_1
17+
; CHECK-OBJ: Other [ (0x80)
18+
ret <4 x i32> %arg
19+
}
20+
21+
define <vscale x 4 x i32> @rvv_vector_cc_1() {
22+
; CHECK-ASM: .variant_cc rvv_vector_cc_1
23+
; CHECK-ASM-NEXT: rvv_vector_cc_1:
24+
; CHECK-OBJ-LABEL: Name: rvv_vector_cc_1
25+
; CHECK-OBJ: Other [ (0x80)
26+
ret <vscale x 4 x i32> undef
27+
}
28+
29+
define <vscale x 4 x i1> @rvv_vector_cc_2() {
30+
; CHECK-ASM: .variant_cc rvv_vector_cc_2
31+
; CHECK-ASM-NEXT: rvv_vector_cc_2:
32+
; CHECK-OBJ-LABEL: Name: rvv_vector_cc_2
33+
; CHECK-OBJ: Other [ (0x80)
34+
ret <vscale x 4 x i1> undef
35+
}
36+
37+
define void @rvv_vector_cc_3(<vscale x 4 x i32> %arg) {
38+
; CHECK-ASM: .variant_cc rvv_vector_cc_3
39+
; CHECK-ASM-NEXT: rvv_vector_cc_3:
40+
; CHECK-OBJ-LABEL: Name: rvv_vector_cc_3
41+
; CHECK-OBJ: Other [ (0x80)
42+
ret void
43+
}
44+
45+
define void @rvv_vector_cc_4(<vscale x 4 x i1> %arg) {
46+
; CHECK-ASM: .variant_cc rvv_vector_cc_4
47+
; CHECK-ASM-NEXT: rvv_vector_cc_4:
48+
; CHECK-OBJ-LABEL: Name: rvv_vector_cc_4
49+
; CHECK-OBJ: Other [ (0x80)
50+
ret void
51+
}

0 commit comments

Comments
 (0)