Skip to content

Commit e9c8e60

Browse files
sstefan1gerekon
authored andcommittedAug 15, 2024·
[RISCV] Support for ESP32-P4 instructions in RISCV backend
1 parent 255e3a3 commit e9c8e60

34 files changed

+29564
-8
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,4 @@ pythonenv*
7373
/clang/utils/analyzer/projects/*/RefScanBuildResults
7474
# automodapi puts generated documentation files here.
7575
/lldb/docs/python_api/
76+
/dbg

‎.gitlab-ci.yml

+25-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ build_and_test:
186186
[ ! -f "${BUILD_DIR}/lld-tests.log" ] || grep -i "internal compiler error\|Killed" ${BUILD_DIR}/lld-tests.log || true
187187
fi
188188
script:
189-
- BUILD_PATH=$PWD/${BUILD_DIR}
189+
- export BUILD_PATH=$PWD/${BUILD_DIR}
190190
- mkdir -p ${BUILD_PATH}
191191
- cmake -G Ninja
192192
-S llvm
@@ -209,6 +209,29 @@ build_and_test:
209209
- runuser -u test_runner -- ninja -C ${BUILD_PATH} check-lld 2>&1 > ${BUILD_PATH}/lld-tests.log;
210210
- chown -R ${CUR_USER} ${BUILD_PATH};
211211

212+
test_xesppie:
213+
stage: test
214+
dependencies:
215+
- pack_x86_64-linux-gnu
216+
when: manual
217+
allow_failure: true
218+
only:
219+
- tags
220+
script:
221+
- cd ${DIST_DIR}/
222+
- ls -l
223+
- DISTRO_PACK_FILE=$(cat dist_name_x86_64-linux-gnu)
224+
- tar -xf ${DISTRO_PACK_FILE}
225+
- ls -l
226+
- cd esp-clang
227+
- ls -l
228+
- pwd
229+
- export CC="$(pwd)/bin/clang"
230+
- export OBJDUMP="$(pwd)/bin/llvm-objdump"
231+
- git clone -q --depth=1 "${GITLAB_SSH_SERVER}/idf/esp-compiler-tests.git"
232+
- cd esp-compiler-tests/build-only/xesppie
233+
- ./test_xesppie.py
234+
212235
.build_linux-gnu_template:
213236
extends: .build_toolchain_template
214237
variables:
@@ -441,6 +464,7 @@ upload_to_github:
441464
- job: pack_x86_64-w64-mingw32
442465
- job: sign_x86_64-apple-darwin
443466
- job: sign_aarch64-apple-darwin
467+
- job: test_xesppie
444468
before_script: []
445469
script:
446470
- ls -l ${DIST_DIR}

‎clang/include/clang/Basic/BuiltinsRISCV.def

+2
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,7 @@ TARGET_BUILTIN(__builtin_riscv_sm3p1, "UiUi", "nc", "zksh")
8989
TARGET_BUILTIN(__builtin_riscv_ntl_load, "v.", "t", "zihintntl")
9090
TARGET_BUILTIN(__builtin_riscv_ntl_store, "v.", "t", "zihintntl")
9191

92+
#include "clang/Basic/BuiltinsRISCVESP32P4.def"
93+
9294
#undef BUILTIN
9395
#undef TARGET_BUILTIN

‎clang/include/clang/Basic/BuiltinsRISCVESP32P4.def

+354
Large diffs are not rendered by default.

‎clang/test/CodeGen/RISCV/riscv-esp32p4.c

+1,027
Large diffs are not rendered by default.

‎clang/test/Misc/target-invalid-cpu-note.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@
8181

8282
// RUN: not %clang_cc1 -triple riscv32 -target-cpu not-a-cpu -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix RISCV32
8383
// RISCV32: error: unknown target CPU 'not-a-cpu'
84-
// RISCV32-NEXT: note: valid target CPU values are: generic-rv32, rocket-rv32, sifive-e20, sifive-e21, sifive-e24, sifive-e31, sifive-e34, sifive-e76, syntacore-scr1-base, syntacore-scr1-max{{$}}
84+
// RISCV32-NEXT: note: valid target CPU values are: esp32p4, generic-rv32, rocket-rv32, sifive-e20, sifive-e21, sifive-e24, sifive-e31, sifive-e34, sifive-e76, syntacore-scr1-base, syntacore-scr1-max{{$}}
8585

8686
// RUN: not %clang_cc1 -triple riscv64 -target-cpu not-a-cpu -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix RISCV64
8787
// RISCV64: error: unknown target CPU 'not-a-cpu'
8888
// RISCV64-NEXT: note: valid target CPU values are: generic-rv64, rocket-rv64, sifive-p450, sifive-p670, sifive-s21, sifive-s51, sifive-s54, sifive-s76, sifive-u54, sifive-u74, sifive-x280, veyron-v1, xiangshan-nanhu{{$}}
8989

9090
// RUN: not %clang_cc1 -triple riscv32 -tune-cpu not-a-cpu -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix TUNE-RISCV32
9191
// TUNE-RISCV32: error: unknown target CPU 'not-a-cpu'
92-
// TUNE-RISCV32-NEXT: note: valid target CPU values are: generic-rv32, rocket-rv32, sifive-e20, sifive-e21, sifive-e24, sifive-e31, sifive-e34, sifive-e76, syntacore-scr1-base, syntacore-scr1-max, generic, rocket, sifive-7-series{{$}}
92+
// TUNE-RISCV32-NEXT: note: valid target CPU values are: esp32p4, generic-rv32, rocket-rv32, sifive-e20, sifive-e21, sifive-e24, sifive-e31, sifive-e34, sifive-e76, syntacore-scr1-base, syntacore-scr1-max, generic, rocket, sifive-7-series{{$}}
9393

9494
// RUN: not %clang_cc1 -triple riscv64 -tune-cpu not-a-cpu -fsyntax-only %s 2>&1 | FileCheck %s --check-prefix TUNE-RISCV64
9595
// TUNE-RISCV64: error: unknown target CPU 'not-a-cpu'

‎llvm/include/llvm/IR/IntrinsicsRISCV.td

+4
Original file line numberDiff line numberDiff line change
@@ -1889,3 +1889,7 @@ let TargetPrefix = "riscv" in {
18891889
include "llvm/IR/IntrinsicsRISCVXTHead.td"
18901890
include "llvm/IR/IntrinsicsRISCVXsf.td"
18911891
include "llvm/IR/IntrinsicsRISCVXCV.td"
1892+
1893+
// Generated code
1894+
// --------------
1895+
include "llvm/IR/IntrinsicsRISCVESP32P4.td"

‎llvm/include/llvm/IR/IntrinsicsRISCVESP32P4.td

+1,065
Large diffs are not rendered by default.

‎llvm/lib/Support/RISCVISAInfo.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static const RISCVSupportedExtension SupportedExtensions[] = {
7171
{"xcvmac", {1, 0}},
7272
{"xcvmem", {1, 0}},
7373
{"xcvsimd", {1, 0}},
74+
{"xesppie", {1, 0}},
7475
{"xsfvcp", {1, 0}},
7576
{"xsfvfnrclipxfqf", {1, 0}},
7677
{"xsfvfwmaccqqq", {1, 0}},

‎llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp

+97
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,14 @@ struct RISCVOperand final : public MCParsedAsmOperand {
442442
}
443443
}
444444

445+
static bool inRange(const MCExpr *Expr, int64_t MinValue, int64_t MaxValue) {
446+
if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
447+
int64_t Value = CE->getValue();
448+
return Value >= MinValue && Value <= MaxValue;
449+
}
450+
return false;
451+
}
452+
445453
bool isToken() const override { return Kind == KindTy::Token; }
446454
bool isReg() const override { return Kind == KindTy::Register; }
447455
bool isV0Reg() const {
@@ -467,6 +475,43 @@ struct RISCVOperand final : public MCParsedAsmOperand {
467475
bool isRlist() const { return Kind == KindTy::Rlist; }
468476
bool isSpimm() const { return Kind == KindTy::Spimm; }
469477

478+
bool isImm(int64_t MinValue, int64_t MaxValue) const {
479+
return Kind == KindTy::Immediate && inRange(getImm(), MinValue, MaxValue);
480+
}
481+
482+
bool isImm8() const {
483+
// The addi instruction maybe expaned to addmi and addi.
484+
return isImm((-32768 - 128), (32512 + 127));
485+
}
486+
487+
bool isSelect_2() const { return isImm(0, 1); }
488+
489+
bool isSelect_4() const { return isImm(0, 3); }
490+
491+
bool isSelect_8() const { return isImm(0, 7); }
492+
493+
bool isSelect_16() const { return isImm(0, 16); }
494+
495+
bool isOffset_16_16() const {
496+
return isImm(-128, 112) &&
497+
((cast<MCConstantExpr>(getImm())->getValue() & 0xf) == 0);
498+
}
499+
500+
bool isOffset_256_8() const {
501+
return isImm(-1024, 1016) &&
502+
((cast<MCConstantExpr>(getImm())->getValue() & 0x7) == 0);
503+
}
504+
505+
bool isOffset_256_16() const {
506+
return isImm(-2048, 2032) &&
507+
((cast<MCConstantExpr>(getImm())->getValue() & 0xf) == 0);
508+
}
509+
510+
bool isOffset_256_4() const {
511+
return isImm(-512, 508) &&
512+
((cast<MCConstantExpr>(getImm())->getValue() & 0x3) == 0);
513+
}
514+
470515
bool isGPR() const {
471516
return Kind == KindTy::Register &&
472517
RISCVMCRegisterClasses[RISCV::GPRRegClassID].contains(Reg.RegNum);
@@ -841,6 +886,54 @@ struct RISCVOperand final : public MCParsedAsmOperand {
841886
VK == RISCVMCExpr::VK_RISCV_None;
842887
}
843888

889+
bool isUImm9() const {
890+
if (!isImm())
891+
return false;
892+
int64_t Imm;
893+
RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
894+
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
895+
return IsConstantImm && isUInt<9>(Imm) &&
896+
VK == RISCVMCExpr::VK_RISCV_None;
897+
}
898+
899+
bool isUImm10() const {
900+
RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
901+
int64_t Imm;
902+
bool IsValid;
903+
if (!isImm())
904+
return false;
905+
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
906+
if (!IsConstantImm)
907+
IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK);
908+
else
909+
IsValid = isUInt<10>(fixImmediateForRV32(Imm, isRV64Imm()));
910+
return IsValid && VK == RISCVMCExpr::VK_RISCV_None;
911+
}
912+
913+
bool isUImm12() const {
914+
if (!isImm())
915+
return false;
916+
int64_t Imm;
917+
RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
918+
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
919+
return IsConstantImm && isUInt<12>(Imm) &&
920+
VK == RISCVMCExpr::VK_RISCV_None;
921+
}
922+
923+
bool isUImm13() const {
924+
RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
925+
int64_t Imm;
926+
bool IsValid;
927+
if (!isImm())
928+
return false;
929+
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
930+
if (!IsConstantImm)
931+
IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK);
932+
else
933+
IsValid = isUInt<13>(fixImmediateForRV32(Imm, isRV64Imm()));
934+
return IsValid && VK == RISCVMCExpr::VK_RISCV_None;
935+
}
936+
844937
bool isUImm10Lsb00NonZero() const {
845938
if (!isImm())
846939
return false;
@@ -1533,6 +1626,10 @@ bool RISCVAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
15331626
return generateImmOutOfRangeError(
15341627
Operands, ErrorInfo, -(1 << 11), (1 << 11) - 32,
15351628
"immediate must be a multiple of 32 bytes in the range");
1629+
case Match_InvalidUImm12:
1630+
return generateImmOutOfRangeError(
1631+
Operands, ErrorInfo, 0, (1 << 12) - 1,
1632+
"immediate must be in the range");
15361633
case Match_InvalidSImm13Lsb0:
15371634
return generateImmOutOfRangeError(
15381635
Operands, ErrorInfo, -(1 << 12), (1 << 12) - 2,

‎llvm/lib/Target/RISCV/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ add_llvm_target(RISCVCodeGen
4343
RISCVInstrInfo.cpp
4444
RISCVISelDAGToDAG.cpp
4545
RISCVISelLowering.cpp
46+
RISCVESP32P4ISelLowering.cpp
4647
RISCVMachineFunctionInfo.cpp
4748
RISCVMergeBaseOffset.cpp
4849
RISCVOptWInstrs.cpp

0 commit comments

Comments
 (0)
Please sign in to comment.