Skip to content

Commit

Permalink
[RISCV] Emit .variant_cc directives for vector function calls.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yetingk committed Dec 16, 2022
1 parent 12c55eb commit 982a586
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
5 changes: 5 additions & 0 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.cpp
Expand Up @@ -13,6 +13,7 @@
#include "RISCVTargetStreamer.h"
#include "RISCVBaseInfo.h"
#include "RISCVMCTargetDesc.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/RISCVAttributes.h"
#include "llvm/Support/RISCVISAInfo.h"
Expand Down Expand Up @@ -98,6 +99,10 @@ void RISCVTargetAsmStreamer::emitDirectiveOptionNoRelax() {
OS << "\t.option\tnorelax\n";
}

void RISCVTargetAsmStreamer::emitDirectiveVariantCC(MCSymbol &Symbol) {
OS << "\t.variant_cc\t" << Symbol.getName() << "\n";
}

void RISCVTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
OS << "\t.attribute\t" << Attribute << ", " << Twine(Value) << "\n";
}
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVTargetStreamer.h
Expand Up @@ -66,6 +66,7 @@ class RISCVTargetAsmStreamer : public RISCVTargetStreamer {
void emitDirectiveOptionNoRVC() override;
void emitDirectiveOptionRelax() override;
void emitDirectiveOptionNoRelax() override;
void emitDirectiveVariantCC(MCSymbol &Symbol) override;
};

}
Expand Down
13 changes: 13 additions & 0 deletions llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp
Expand Up @@ -15,6 +15,7 @@
#include "MCTargetDesc/RISCVMCExpr.h"
#include "MCTargetDesc/RISCVTargetStreamer.h"
#include "RISCV.h"
#include "RISCVMachineFunctionInfo.h"
#include "RISCVTargetMachine.h"
#include "TargetInfo/RISCVTargetInfo.h"
#include "llvm/ADT/Statistic.h"
Expand Down Expand Up @@ -81,6 +82,8 @@ class RISCVAsmPrinter : public AsmPrinter {
void emitStartOfAsmFile(Module &M) override;
void emitEndOfAsmFile(Module &M) override;

void emitFunctionEntryLabel() override;

private:
void emitAttributes();
};
Expand Down Expand Up @@ -225,6 +228,16 @@ void RISCVAsmPrinter::emitAttributes() {
RTS.emitTargetAttributes(*MCSTI);
}

void RISCVAsmPrinter::emitFunctionEntryLabel() {
const auto *RMFI = MF->getInfo<RISCVMachineFunctionInfo>();
if (RMFI->isVectorCall()) {
auto &RTS =
static_cast<RISCVTargetStreamer &>(*OutStreamer->getTargetStreamer());
RTS.emitDirectiveVariantCC(*CurrentFnSym);
}
return AsmPrinter::emitFunctionEntryLabel();
}

// Force static initialization.
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVAsmPrinter() {
RegisterAsmPrinter<RISCVAsmPrinter> X(getTheRISCV32Target());
Expand Down
10 changes: 9 additions & 1 deletion llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Expand Up @@ -12070,6 +12070,10 @@ SDValue RISCVTargetLowering::LowerFormalArguments(
InVals.push_back(ArgValue);
}

if (any_of(ArgLocs,
[](CCValAssign &VA) { return VA.getLocVT().isScalableVector(); }))
MF.getInfo<RISCVMachineFunctionInfo>()->setIsVectorCall();

if (IsVarArg) {
ArrayRef<MCPhysReg> ArgRegs = makeArrayRef(ArgGPRs);
unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs);
Expand Down Expand Up @@ -12540,7 +12544,7 @@ RISCVTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
const SmallVectorImpl<ISD::OutputArg> &Outs,
const SmallVectorImpl<SDValue> &OutVals,
const SDLoc &DL, SelectionDAG &DAG) const {
const MachineFunction &MF = DAG.getMachineFunction();
MachineFunction &MF = DAG.getMachineFunction();
const RISCVSubtarget &STI = MF.getSubtarget<RISCVSubtarget>();

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

if (any_of(RVLocs,
[](CCValAssign &VA) { return VA.getLocVT().isScalableVector(); }))
MF.getInfo<RISCVMachineFunctionInfo>()->setIsVectorCall();

unsigned RetOpc = RISCVISD::RET_FLAG;
// Interrupt service routines use different return instructions.
const Function &Func = DAG.getMachineFunction().getFunction();
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/Target/RISCV/RISCVMachineFunctionInfo.h
Expand Up @@ -65,6 +65,8 @@ class RISCVMachineFunctionInfo : public MachineFunctionInfo {
uint64_t RVVPadding = 0;
/// Size of stack frame to save callee saved registers
unsigned CalleeSavedStackSize = 0;
/// Is there any vector argument or return?
bool IsVectorCall = false;

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

void addSExt32Register(Register Reg);
bool isSExt32Register(Register Reg) const;

bool isVectorCall() const { return IsVectorCall; }
void setIsVectorCall() { IsVectorCall = true; }
};

} // end namespace llvm
Expand Down
51 changes: 51 additions & 0 deletions llvm/test/CodeGen/RISCV/rvv/variant-cc.ll
@@ -0,0 +1,51 @@
; RUN: llc -mtriple=riscv64 -mattr=+v -o - %s | FileCheck %s --check-prefix=CHECK-ASM
; RUN: llc -mtriple=riscv64 -mattr=+v -filetype=obj -o - %s \
; RUN: | llvm-readobj --symbols - | FileCheck %s --check-prefix=CHECK-OBJ

define i32 @base_cc() {
; CHECK-ASM-LABEL: base_cc:
; CHECK-ASM-NOT: .variant_cc
; CHECK-OBJ-LABEL: Name: base_cc
; CHECK-OBJ: Other: 0
ret i32 42
}

define <4 x i32> @fixed_vector_cc_1(<4 x i32> %arg) {
; CHECK-ASM: .variant_cc fixed_vector_cc_1
; CHECK-ASM-NEXT: fixed_vector_cc_1:
; CHECK-OBJ-LABEL: Name: fixed_vector_cc_1
; CHECK-OBJ: Other [ (0x80)
ret <4 x i32> %arg
}

define <vscale x 4 x i32> @rvv_vector_cc_1() {
; CHECK-ASM: .variant_cc rvv_vector_cc_1
; CHECK-ASM-NEXT: rvv_vector_cc_1:
; CHECK-OBJ-LABEL: Name: rvv_vector_cc_1
; CHECK-OBJ: Other [ (0x80)
ret <vscale x 4 x i32> undef
}

define <vscale x 4 x i1> @rvv_vector_cc_2() {
; CHECK-ASM: .variant_cc rvv_vector_cc_2
; CHECK-ASM-NEXT: rvv_vector_cc_2:
; CHECK-OBJ-LABEL: Name: rvv_vector_cc_2
; CHECK-OBJ: Other [ (0x80)
ret <vscale x 4 x i1> undef
}

define void @rvv_vector_cc_3(<vscale x 4 x i32> %arg) {
; CHECK-ASM: .variant_cc rvv_vector_cc_3
; CHECK-ASM-NEXT: rvv_vector_cc_3:
; CHECK-OBJ-LABEL: Name: rvv_vector_cc_3
; CHECK-OBJ: Other [ (0x80)
ret void
}

define void @rvv_vector_cc_4(<vscale x 4 x i1> %arg) {
; CHECK-ASM: .variant_cc rvv_vector_cc_4
; CHECK-ASM-NEXT: rvv_vector_cc_4:
; CHECK-OBJ-LABEL: Name: rvv_vector_cc_4
; CHECK-OBJ: Other [ (0x80)
ret void
}

0 comments on commit 982a586

Please sign in to comment.