Skip to content

[RISCV] Implement intrinsics for XAndesVPackFPH #140007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clang/include/clang/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,12 @@ clang_tablegen(riscv_sifive_vector_builtin_cg.inc -gen-riscv-sifive-vector-built
clang_tablegen(riscv_sifive_vector_builtin_sema.inc -gen-riscv-sifive-vector-builtin-sema
SOURCE riscv_sifive_vector.td
TARGET ClangRISCVSiFiveVectorBuiltinSema)
clang_tablegen(riscv_andes_vector_builtins.inc -gen-riscv-andes-vector-builtins
SOURCE riscv_andes_vector.td
TARGET ClangRISCVAndesVectorBuiltins)
clang_tablegen(riscv_andes_vector_builtin_cg.inc -gen-riscv-andes-vector-builtin-codegen
SOURCE riscv_andes_vector.td
TARGET ClangRISCVAndesVectorBuiltinCG)
clang_tablegen(riscv_andes_vector_builtin_sema.inc -gen-riscv-andes-vector-builtin-sema
SOURCE riscv_andes_vector.td
TARGET ClangRISCVAndesVectorBuiltinSema)
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/TargetBuiltins.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ namespace clang {
FirstSiFiveBuiltin,
LastRVVBuiltin = FirstSiFiveBuiltin - 1,
#include "clang/Basic/riscv_sifive_vector_builtins.inc"
FirstAndesBuiltin,
LastSiFiveBuiltin = FirstAndesBuiltin - 1,
#include "clang/Basic/riscv_andes_vector_builtins.inc"
#undef GET_RISCVV_BUILTIN_ENUMERATORS
FirstTSBuiltin,
};
Expand Down
83 changes: 83 additions & 0 deletions clang/include/clang/Basic/riscv_andes_vector.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//==--- riscv_andes_vector.td - RISC-V Andes Builtin function list --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the builtins for RISC-V Andes Vector Extension. See:
//
// https://github.com/andestech/andes-vector-intrinsic-doc
//
//===----------------------------------------------------------------------===//

include "riscv_vector_common.td"

//===----------------------------------------------------------------------===//
// Instruction definitions
//===----------------------------------------------------------------------===//

// Andes Vector Packed FP16 Extension (XAndesVPackFPH)

multiclass RVVFPMAD {
let Log2LMUL = [-2, -1, 0, 1, 2, 3],
OverloadedName = NAME in {
defm NAME : RVVOutOp1BuiltinSet<NAME, "x", [["vf", "v", "vvf"]]>;

let HasFRMRoundModeOp = true in
defm NAME : RVVOutOp1BuiltinSet<NAME, "x", [["vf", "v", "vvfu"]]>;
}
}

let RequiredFeatures = ["Xandesvpackfph"],
UnMaskedPolicyScheme = HasPassthruOperand in {
let ManualCodegen = [{
{
// LLVM intrinsic
// Unmasked: (passthru, op0, op1, round_mode, vl)
// Masked: (passthru, vector_in, vector_in/scalar_in, mask, frm, vl, policy)

SmallVector<llvm::Value*, 7> Operands;
bool HasMaskedOff = !(
(IsMasked && (PolicyAttrs & RVV_VTA) && (PolicyAttrs & RVV_VMA)) ||
(!IsMasked && PolicyAttrs & RVV_VTA));
bool HasRoundModeOp = IsMasked ?
(HasMaskedOff ? Ops.size() == 6 : Ops.size() == 5) :
(HasMaskedOff ? Ops.size() == 5 : Ops.size() == 4);

unsigned Offset = IsMasked ?
(HasMaskedOff ? 2 : 1) : (HasMaskedOff ? 1 : 0);

if (!HasMaskedOff)
Operands.push_back(llvm::PoisonValue::get(ResultType));
else
Operands.push_back(Ops[IsMasked ? 1 : 0]);

Operands.push_back(Ops[Offset]); // op0
Operands.push_back(Ops[Offset + 1]); // op1

if (IsMasked)
Operands.push_back(Ops[0]); // mask

if (HasRoundModeOp) {
Operands.push_back(Ops[Offset + 2]); // frm
Operands.push_back(Ops[Offset + 3]); // vl
} else {
Operands.push_back(ConstantInt::get(Ops[Offset + 2]->getType(), 7)); // frm
Operands.push_back(Ops[Offset + 2]); // vl
}

if (IsMasked)
Operands.push_back(ConstantInt::get(Ops.back()->getType(), PolicyAttrs));

IntrinsicTypes = {ResultType, Ops[Offset + 1]->getType(),
Operands.back()->getType()};
llvm::Function *F = CGM.getIntrinsic(ID, IntrinsicTypes);
return Builder.CreateCall(F, Operands, "");
}
}] in {
defm nds_vfpmadt : RVVFPMAD;
defm nds_vfpmadb : RVVFPMAD;
}
}
2 changes: 1 addition & 1 deletion clang/include/clang/Sema/RISCVIntrinsicManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Preprocessor;
namespace sema {
class RISCVIntrinsicManager {
public:
enum class IntrinsicKind : uint8_t { RVV, SIFIVE_VECTOR };
enum class IntrinsicKind : uint8_t { RVV, SIFIVE_VECTOR, ANDES_VECTOR };

virtual ~RISCVIntrinsicManager() = default;

Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Sema/SemaRISCV.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class SemaRISCV : public SemaBase {
/// Indicate RISC-V SiFive vector builtin functions enabled or not.
bool DeclareSiFiveVectorBuiltins = false;

/// Indicate RISC-V Andes vector builtin functions enabled or not.
bool DeclareAndesVectorBuiltins = false;

std::unique_ptr<sema::RISCVIntrinsicManager> IntrinsicManager;
};

Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Support/RISCVVIntrinsicUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ class RVVIntrinsic {
enum RVVRequire {
RVV_REQ_RV64,
RVV_REQ_Zvfhmin,
RVV_REQ_Xandesvpackfph,
RVV_REQ_Xsfvcp,
RVV_REQ_Xsfvfnrclipxfqf,
RVV_REQ_Xsfvfwmaccqqq,
Expand Down
22 changes: 19 additions & 3 deletions clang/lib/Basic/Targets/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,15 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions &Opts,
static constexpr int NumRVVBuiltins =
RISCVVector::FirstSiFiveBuiltin - Builtin::FirstTSBuiltin;
static constexpr int NumRVVSiFiveBuiltins =
RISCVVector::FirstTSBuiltin - RISCVVector::FirstSiFiveBuiltin;
RISCVVector::FirstAndesBuiltin - RISCVVector::FirstSiFiveBuiltin;
static constexpr int NumRVVAndesBuiltins =
RISCVVector::FirstTSBuiltin - RISCVVector::FirstAndesBuiltin;
static constexpr int NumRISCVBuiltins =
RISCV::LastTSBuiltin - RISCVVector::FirstTSBuiltin;
static constexpr int NumBuiltins =
RISCV::LastTSBuiltin - Builtin::FirstTSBuiltin;
static_assert(NumBuiltins ==
(NumRVVBuiltins + NumRVVSiFiveBuiltins + NumRISCVBuiltins));
static_assert(NumBuiltins == (NumRVVBuiltins + NumRVVSiFiveBuiltins +
NumRVVAndesBuiltins + NumRISCVBuiltins));

namespace RVV {
#define GET_RISCVV_BUILTIN_STR_TABLE
Expand Down Expand Up @@ -280,6 +282,19 @@ static constexpr std::array<Builtin::Info, NumRVVSiFiveBuiltins> BuiltinInfos =
};
} // namespace RVVSiFive

namespace RVVAndes {
#define GET_RISCVV_BUILTIN_STR_TABLE
#include "clang/Basic/riscv_andes_vector_builtins.inc"
#undef GET_RISCVV_BUILTIN_STR_TABLE

static constexpr std::array<Builtin::Info, NumRVVAndesBuiltins> BuiltinInfos =
{
#define GET_RISCVV_BUILTIN_INFOS
#include "clang/Basic/riscv_andes_vector_builtins.inc"
#undef GET_RISCVV_BUILTIN_INFOS
};
} // namespace RVVAndes

#define GET_BUILTIN_STR_TABLE
#include "clang/Basic/BuiltinsRISCV.inc"
#undef GET_BUILTIN_STR_TABLE
Expand All @@ -296,6 +311,7 @@ RISCVTargetInfo::getTargetBuiltins() const {
return {
{&RVV::BuiltinStrings, RVV::BuiltinInfos, "__builtin_rvv_"},
{&RVVSiFive::BuiltinStrings, RVVSiFive::BuiltinInfos, "__builtin_rvv_"},
{&RVVAndes::BuiltinStrings, RVVAndes::BuiltinInfos, "__builtin_rvv_"},
{&BuiltinStrings, BuiltinInfos},
};
}
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CodeGen/TargetBuiltins/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,9 @@ Value *CodeGenFunction::EmitRISCVBuiltinExpr(unsigned BuiltinID,

// SiFive Vector builtins are handled from here.
#include "clang/Basic/riscv_sifive_vector_builtin_cg.inc"

// Andes Vector builtins are handled from here.
#include "clang/Basic/riscv_andes_vector_builtin_cg.inc"
}

assert(ID != Intrinsic::not_intrinsic);
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ set(riscv_files
riscv_crypto.h
riscv_ntlh.h
sifive_vector.h
andes_vector.h
)

set(systemz_files
Expand Down
16 changes: 16 additions & 0 deletions clang/lib/Headers/andes_vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//===----- andes_vector.h - Andes Vector definitions ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _ANDES_VECTOR_H_
#define _ANDES_VECTOR_H_

#include "riscv_vector.h"

#pragma clang riscv intrinsic andes_vector

#endif //_ANDES_VECTOR_H_
8 changes: 6 additions & 2 deletions clang/lib/Parse/ParsePragma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4139,6 +4139,7 @@ void PragmaMaxTokensTotalHandler::HandlePragma(Preprocessor &PP,

// Handle '#pragma clang riscv intrinsic vector'.
// '#pragma clang riscv intrinsic sifive_vector'.
// '#pragma clang riscv intrinsic andes_vector'.
void PragmaRISCVHandler::HandlePragma(Preprocessor &PP,
PragmaIntroducer Introducer,
Token &FirstToken) {
Expand All @@ -4154,10 +4155,11 @@ void PragmaRISCVHandler::HandlePragma(Preprocessor &PP,

PP.Lex(Tok);
II = Tok.getIdentifierInfo();
if (!II || !(II->isStr("vector") || II->isStr("sifive_vector"))) {
if (!II || !(II->isStr("vector") || II->isStr("sifive_vector") ||
II->isStr("andes_vector"))) {
PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_argument)
<< PP.getSpelling(Tok) << "riscv" << /*Expected=*/true
<< "'vector' or 'sifive_vector'";
<< "'vector', 'sifive_vector' or 'andes_vector'";
return;
}

Expand All @@ -4172,4 +4174,6 @@ void PragmaRISCVHandler::HandlePragma(Preprocessor &PP,
Actions.RISCV().DeclareRVVBuiltins = true;
else if (II->isStr("sifive_vector"))
Actions.RISCV().DeclareSiFiveVectorBuiltins = true;
else if (II->isStr("andes_vector"))
Actions.RISCV().DeclareAndesVectorBuiltins = true;
}
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,8 @@ bool Sema::LookupBuiltin(LookupResult &R) {
}
}

if (RISCV().DeclareRVVBuiltins || RISCV().DeclareSiFiveVectorBuiltins) {
if (RISCV().DeclareRVVBuiltins || RISCV().DeclareSiFiveVectorBuiltins ||
RISCV().DeclareAndesVectorBuiltins) {
if (!RISCV().IntrinsicManager)
RISCV().IntrinsicManager = CreateRISCVIntrinsicManager(*this);

Expand Down
23 changes: 23 additions & 0 deletions clang/lib/Sema/SemaRISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ static const PrototypeDescriptor RVSiFiveVectorSignatureTable[] = {
#undef DECL_SIGNATURE_TABLE
};

static const PrototypeDescriptor RVAndesVectorSignatureTable[] = {
#define DECL_SIGNATURE_TABLE
#include "clang/Basic/riscv_andes_vector_builtin_sema.inc"
#undef DECL_SIGNATURE_TABLE
};

static const RVVIntrinsicRecord RVVIntrinsicRecords[] = {
#define DECL_INTRINSIC_RECORDS
#include "clang/Basic/riscv_vector_builtin_sema.inc"
Expand All @@ -81,6 +87,12 @@ static const RVVIntrinsicRecord RVSiFiveVectorIntrinsicRecords[] = {
#undef DECL_INTRINSIC_RECORDS
};

static const RVVIntrinsicRecord RVAndesVectorIntrinsicRecords[] = {
#define DECL_INTRINSIC_RECORDS
#include "clang/Basic/riscv_andes_vector_builtin_sema.inc"
#undef DECL_INTRINSIC_RECORDS
};

// Get subsequence of signature table.
static ArrayRef<PrototypeDescriptor>
ProtoSeq2ArrayRef(IntrinsicKind K, uint16_t Index, uint8_t Length) {
Expand All @@ -89,6 +101,8 @@ ProtoSeq2ArrayRef(IntrinsicKind K, uint16_t Index, uint8_t Length) {
return ArrayRef(&RVVSignatureTable[Index], Length);
case IntrinsicKind::SIFIVE_VECTOR:
return ArrayRef(&RVSiFiveVectorSignatureTable[Index], Length);
case IntrinsicKind::ANDES_VECTOR:
return ArrayRef(&RVAndesVectorSignatureTable[Index], Length);
}
llvm_unreachable("Unhandled IntrinsicKind");
}
Expand Down Expand Up @@ -167,6 +181,7 @@ class RISCVIntrinsicManagerImpl : public sema::RISCVIntrinsicManager {
RVVTypeCache TypeCache;
bool ConstructedRISCVVBuiltins;
bool ConstructedRISCVSiFiveVectorBuiltins;
bool ConstructedRISCVAndesVectorBuiltins;

// List of all RVV intrinsic.
std::vector<RVVIntrinsicDef> IntrinsicList;
Expand All @@ -192,6 +207,7 @@ class RISCVIntrinsicManagerImpl : public sema::RISCVIntrinsicManager {
RISCVIntrinsicManagerImpl(clang::Sema &S) : S(S), Context(S.Context) {
ConstructedRISCVVBuiltins = false;
ConstructedRISCVSiFiveVectorBuiltins = false;
ConstructedRISCVAndesVectorBuiltins = false;
}

// Initialize IntrinsicList
Expand All @@ -209,6 +225,7 @@ void RISCVIntrinsicManagerImpl::ConstructRVVIntrinsics(
const TargetInfo &TI = Context.getTargetInfo();
static const std::pair<const char *, unsigned> FeatureCheckList[] = {
{"64bit", RVV_REQ_RV64},
{"xandesvpackfph", RVV_REQ_Xandesvpackfph},
{"xsfvcp", RVV_REQ_Xsfvcp},
{"xsfvfnrclipxfqf", RVV_REQ_Xsfvfnrclipxfqf},
{"xsfvfwmaccqqq", RVV_REQ_Xsfvfwmaccqqq},
Expand Down Expand Up @@ -358,6 +375,12 @@ void RISCVIntrinsicManagerImpl::InitIntrinsicList() {
ConstructRVVIntrinsics(RVSiFiveVectorIntrinsicRecords,
IntrinsicKind::SIFIVE_VECTOR);
}
if (S.RISCV().DeclareAndesVectorBuiltins &&
!ConstructedRISCVAndesVectorBuiltins) {
ConstructedRISCVAndesVectorBuiltins = true;
ConstructRVVIntrinsics(RVAndesVectorIntrinsicRecords,
IntrinsicKind::ANDES_VECTOR);
}
}

// Compute name and signatures for intrinsic with practical types.
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Support/RISCVVIntrinsicUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, enum RVVRequire Require) {
switch (Require) {
STRINGIFY(RVV_REQ_RV64)
STRINGIFY(RVV_REQ_Zvfhmin)
STRINGIFY(RVV_REQ_Xandesvpackfph)
STRINGIFY(RVV_REQ_Xsfvcp)
STRINGIFY(RVV_REQ_Xsfvfnrclipxfqf)
STRINGIFY(RVV_REQ_Xsfvfwmaccqqq)
Expand Down
Loading
Loading