Skip to content

Commit d561d59

Browse files
tclin914circYuan
andauthored
[RISCV] Implement intrinsics for XAndesVPackFPH (#140007)
This patch implements clang intrinsic support for XAndesVPackFPH. The document for the intrinsics can be found at: https://github.com/andestech/andes-vector-intrinsic-doc/blob/ast-v5_4_0-release-v5/auto-generated/andes-v5/intrinsic_funcs.adoc#andes-vector-packed-fp16-extensionxandesvpackfph and with policy variants https://github.com/andestech/andes-vector-intrinsic-doc/blob/ast-v5_4_0-release-v5/auto-generated/andes-v5/policy_funcs/intrinsic_funcs.adoc#andes-vector-packed-fp16-extensionxandesvpackfph Co-authored-by: Tony Chuan-Yue Yuan <yuan593@andestech.com>
1 parent 575f66c commit d561d59

File tree

25 files changed

+2858
-8
lines changed

25 files changed

+2858
-8
lines changed

clang/include/clang/Basic/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,12 @@ clang_tablegen(riscv_sifive_vector_builtin_cg.inc -gen-riscv-sifive-vector-built
201201
clang_tablegen(riscv_sifive_vector_builtin_sema.inc -gen-riscv-sifive-vector-builtin-sema
202202
SOURCE riscv_sifive_vector.td
203203
TARGET ClangRISCVSiFiveVectorBuiltinSema)
204+
clang_tablegen(riscv_andes_vector_builtins.inc -gen-riscv-andes-vector-builtins
205+
SOURCE riscv_andes_vector.td
206+
TARGET ClangRISCVAndesVectorBuiltins)
207+
clang_tablegen(riscv_andes_vector_builtin_cg.inc -gen-riscv-andes-vector-builtin-codegen
208+
SOURCE riscv_andes_vector.td
209+
TARGET ClangRISCVAndesVectorBuiltinCG)
210+
clang_tablegen(riscv_andes_vector_builtin_sema.inc -gen-riscv-andes-vector-builtin-sema
211+
SOURCE riscv_andes_vector.td
212+
TARGET ClangRISCVAndesVectorBuiltinSema)

clang/include/clang/Basic/TargetBuiltins.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ namespace clang {
197197
FirstSiFiveBuiltin,
198198
LastRVVBuiltin = FirstSiFiveBuiltin - 1,
199199
#include "clang/Basic/riscv_sifive_vector_builtins.inc"
200+
FirstAndesBuiltin,
201+
LastSiFiveBuiltin = FirstAndesBuiltin - 1,
202+
#include "clang/Basic/riscv_andes_vector_builtins.inc"
200203
#undef GET_RISCVV_BUILTIN_ENUMERATORS
201204
FirstTSBuiltin,
202205
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//==--- riscv_andes_vector.td - RISC-V Andes Builtin function list --------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines the builtins for RISC-V Andes Vector Extension. See:
10+
//
11+
// https://github.com/andestech/andes-vector-intrinsic-doc
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
include "riscv_vector_common.td"
16+
17+
//===----------------------------------------------------------------------===//
18+
// Instruction definitions
19+
//===----------------------------------------------------------------------===//
20+
21+
// Andes Vector Packed FP16 Extension (XAndesVPackFPH)
22+
23+
multiclass RVVFPMAD {
24+
let Log2LMUL = [-2, -1, 0, 1, 2, 3],
25+
OverloadedName = NAME in {
26+
defm NAME : RVVOutOp1BuiltinSet<NAME, "x", [["vf", "v", "vvf"]]>;
27+
28+
let HasFRMRoundModeOp = true in
29+
defm NAME : RVVOutOp1BuiltinSet<NAME, "x", [["vf", "v", "vvfu"]]>;
30+
}
31+
}
32+
33+
let RequiredFeatures = ["Xandesvpackfph"],
34+
UnMaskedPolicyScheme = HasPassthruOperand in {
35+
let ManualCodegen = [{
36+
{
37+
// LLVM intrinsic
38+
// Unmasked: (passthru, op0, op1, round_mode, vl)
39+
// Masked: (passthru, vector_in, vector_in/scalar_in, mask, frm, vl, policy)
40+
41+
SmallVector<llvm::Value*, 7> Operands;
42+
bool HasMaskedOff = !(
43+
(IsMasked && (PolicyAttrs & RVV_VTA) && (PolicyAttrs & RVV_VMA)) ||
44+
(!IsMasked && PolicyAttrs & RVV_VTA));
45+
bool HasRoundModeOp = IsMasked ?
46+
(HasMaskedOff ? Ops.size() == 6 : Ops.size() == 5) :
47+
(HasMaskedOff ? Ops.size() == 5 : Ops.size() == 4);
48+
49+
unsigned Offset = IsMasked ?
50+
(HasMaskedOff ? 2 : 1) : (HasMaskedOff ? 1 : 0);
51+
52+
if (!HasMaskedOff)
53+
Operands.push_back(llvm::PoisonValue::get(ResultType));
54+
else
55+
Operands.push_back(Ops[IsMasked ? 1 : 0]);
56+
57+
Operands.push_back(Ops[Offset]); // op0
58+
Operands.push_back(Ops[Offset + 1]); // op1
59+
60+
if (IsMasked)
61+
Operands.push_back(Ops[0]); // mask
62+
63+
if (HasRoundModeOp) {
64+
Operands.push_back(Ops[Offset + 2]); // frm
65+
Operands.push_back(Ops[Offset + 3]); // vl
66+
} else {
67+
Operands.push_back(ConstantInt::get(Ops[Offset + 2]->getType(), 7)); // frm
68+
Operands.push_back(Ops[Offset + 2]); // vl
69+
}
70+
71+
if (IsMasked)
72+
Operands.push_back(ConstantInt::get(Ops.back()->getType(), PolicyAttrs));
73+
74+
IntrinsicTypes = {ResultType, Ops[Offset + 1]->getType(),
75+
Operands.back()->getType()};
76+
llvm::Function *F = CGM.getIntrinsic(ID, IntrinsicTypes);
77+
return Builder.CreateCall(F, Operands, "");
78+
}
79+
}] in {
80+
defm nds_vfpmadt : RVVFPMAD;
81+
defm nds_vfpmadb : RVVFPMAD;
82+
}
83+
}

clang/include/clang/Sema/RISCVIntrinsicManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Preprocessor;
2424
namespace sema {
2525
class RISCVIntrinsicManager {
2626
public:
27-
enum class IntrinsicKind : uint8_t { RVV, SIFIVE_VECTOR };
27+
enum class IntrinsicKind : uint8_t { RVV, SIFIVE_VECTOR, ANDES_VECTOR };
2828

2929
virtual ~RISCVIntrinsicManager() = default;
3030

clang/include/clang/Sema/SemaRISCV.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class SemaRISCV : public SemaBase {
5151
/// Indicate RISC-V SiFive vector builtin functions enabled or not.
5252
bool DeclareSiFiveVectorBuiltins = false;
5353

54+
/// Indicate RISC-V Andes vector builtin functions enabled or not.
55+
bool DeclareAndesVectorBuiltins = false;
56+
5457
std::unique_ptr<sema::RISCVIntrinsicManager> IntrinsicManager;
5558
};
5659

clang/include/clang/Support/RISCVVIntrinsicUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ class RVVIntrinsic {
489489
enum RVVRequire {
490490
RVV_REQ_RV64,
491491
RVV_REQ_Zvfhmin,
492+
RVV_REQ_Xandesvpackfph,
492493
RVV_REQ_Xsfvcp,
493494
RVV_REQ_Xsfvfnrclipxfqf,
494495
RVV_REQ_Xsfvfwmaccqqq,

clang/lib/Basic/Targets/RISCV.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,15 @@ void RISCVTargetInfo::getTargetDefines(const LangOptions &Opts,
265265
static constexpr int NumRVVBuiltins =
266266
RISCVVector::FirstSiFiveBuiltin - Builtin::FirstTSBuiltin;
267267
static constexpr int NumRVVSiFiveBuiltins =
268-
RISCVVector::FirstTSBuiltin - RISCVVector::FirstSiFiveBuiltin;
268+
RISCVVector::FirstAndesBuiltin - RISCVVector::FirstSiFiveBuiltin;
269+
static constexpr int NumRVVAndesBuiltins =
270+
RISCVVector::FirstTSBuiltin - RISCVVector::FirstAndesBuiltin;
269271
static constexpr int NumRISCVBuiltins =
270272
RISCV::LastTSBuiltin - RISCVVector::FirstTSBuiltin;
271273
static constexpr int NumBuiltins =
272274
RISCV::LastTSBuiltin - Builtin::FirstTSBuiltin;
273-
static_assert(NumBuiltins ==
274-
(NumRVVBuiltins + NumRVVSiFiveBuiltins + NumRISCVBuiltins));
275+
static_assert(NumBuiltins == (NumRVVBuiltins + NumRVVSiFiveBuiltins +
276+
NumRVVAndesBuiltins + NumRISCVBuiltins));
275277

276278
namespace RVV {
277279
#define GET_RISCVV_BUILTIN_STR_TABLE
@@ -299,6 +301,19 @@ static constexpr std::array<Builtin::Info, NumRVVSiFiveBuiltins> BuiltinInfos =
299301
};
300302
} // namespace RVVSiFive
301303

304+
namespace RVVAndes {
305+
#define GET_RISCVV_BUILTIN_STR_TABLE
306+
#include "clang/Basic/riscv_andes_vector_builtins.inc"
307+
#undef GET_RISCVV_BUILTIN_STR_TABLE
308+
309+
static constexpr std::array<Builtin::Info, NumRVVAndesBuiltins> BuiltinInfos =
310+
{
311+
#define GET_RISCVV_BUILTIN_INFOS
312+
#include "clang/Basic/riscv_andes_vector_builtins.inc"
313+
#undef GET_RISCVV_BUILTIN_INFOS
314+
};
315+
} // namespace RVVAndes
316+
302317
#define GET_BUILTIN_STR_TABLE
303318
#include "clang/Basic/BuiltinsRISCV.inc"
304319
#undef GET_BUILTIN_STR_TABLE
@@ -315,6 +330,7 @@ RISCVTargetInfo::getTargetBuiltins() const {
315330
return {
316331
{&RVV::BuiltinStrings, RVV::BuiltinInfos, "__builtin_rvv_"},
317332
{&RVVSiFive::BuiltinStrings, RVVSiFive::BuiltinInfos, "__builtin_rvv_"},
333+
{&RVVAndes::BuiltinStrings, RVVAndes::BuiltinInfos, "__builtin_rvv_"},
318334
{&BuiltinStrings, BuiltinInfos},
319335
};
320336
}

clang/lib/CodeGen/TargetBuiltins/RISCV.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,9 @@ Value *CodeGenFunction::EmitRISCVBuiltinExpr(unsigned BuiltinID,
418418

419419
// SiFive Vector builtins are handled from here.
420420
#include "clang/Basic/riscv_sifive_vector_builtin_cg.inc"
421+
422+
// Andes Vector builtins are handled from here.
423+
#include "clang/Basic/riscv_andes_vector_builtin_cg.inc"
421424
}
422425

423426
assert(ID != Intrinsic::not_intrinsic);

clang/lib/Headers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ set(riscv_files
127127
riscv_crypto.h
128128
riscv_ntlh.h
129129
sifive_vector.h
130+
andes_vector.h
130131
)
131132

132133
set(systemz_files

clang/lib/Headers/andes_vector.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===----- andes_vector.h - Andes Vector definitions ----------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _ANDES_VECTOR_H_
10+
#define _ANDES_VECTOR_H_
11+
12+
#include "riscv_vector.h"
13+
14+
#pragma clang riscv intrinsic andes_vector
15+
16+
#endif //_ANDES_VECTOR_H_

0 commit comments

Comments
 (0)