Skip to content

Commit 655d68f

Browse files
nwfresistor
authored andcommitted
[cheriot] CSetBoundsRoundDown instruction and intrinsic
1 parent d2d0bea commit 655d68f

File tree

8 files changed

+64
-0
lines changed

8 files changed

+64
-0
lines changed

clang/include/clang/Basic/Builtins.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,7 @@ BUILTIN(__builtin_cheri_address_set, "v*mvC*mz", "nct")
17261726
BUILTIN(__builtin_cheri_base_get, "zvC*m", "nct")
17271727
BUILTIN(__builtin_cheri_bounds_set, "v*mvC*mz", "nct")
17281728
BUILTIN(__builtin_cheri_bounds_set_exact, "v*mvC*mz", "nct")
1729+
BUILTIN(__builtin_cheri_bounds_set_round_down, "v*mvC*mz", "nct")
17291730
BUILTIN(__builtin_cheri_equal_exact, "bvC*mvC*m", "nct")
17301731
BUILTIN(__builtin_cheri_flags_set, "v*mvC*mz", "nct")
17311732
BUILTIN(__builtin_cheri_flags_get, "zvC*m", "nct")

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5505,6 +5505,15 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
55055505
{SizeTy}, {Cap, Length}),
55065506
Cap->getType()));
55075507
}
5508+
case Builtin::BI__builtin_cheri_bounds_set_round_down: {
5509+
Value *Cap = EmitScalarExpr(E->getArg(0));
5510+
Value *Length = EmitScalarExpr(E->getArg(1));
5511+
return RValue::get(Builder.CreateBitCast(
5512+
Builder.CreateIntrinsic(
5513+
llvm::Intrinsic::cheri_cap_bounds_set_round_down, {SizeTy},
5514+
{Cap, Length}),
5515+
Cap->getType()));
5516+
}
55085517
case Builtin::BI__builtin_cheri_flags_get:
55095518
return RValue::get(
55105519
Builder.CreateIntrinsic(llvm::Intrinsic::cheri_cap_flags_get, {SizeTy},
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 2
2+
// RUN: %clang_cc1 %s -o - "-triple" "riscv32cheriot-unknown-cheriotrtos" "-emit-llvm" "-mframe-pointer=none" "-mcmodel=small" "-target-abi" "cheriot" "-Oz" "-Werror" -std=c2x | FileCheck %s
3+
4+
// CHECK-LABEL: define dso_local ptr addrspace(200) @foo
5+
// CHECK-SAME: (ptr addrspace(200) noundef readnone [[CAP:%.*]], i32 noundef [[B:%.*]]) local_unnamed_addr addrspace(200) #[[ATTR0:[0-9]+]] {
6+
// CHECK-NEXT: entry:
7+
// CHECK-NEXT: [[TMP0:%.*]] = tail call ptr addrspace(200) @llvm.cheri.cap.bounds.set.round.down.i32(ptr addrspace(200) [[CAP]], i32 [[B]])
8+
// CHECK-NEXT: ret ptr addrspace(200) [[TMP0]]
9+
//
10+
void *foo(void* cap, int b) {
11+
return __builtin_cheri_bounds_set_round_down(cap, b);
12+
}

llvm/include/llvm/IR/IntrinsicsCHERICap.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ def int_cheri_cap_bounds_set_exact :
5757
Intrinsic<[llvm_cap_ty],
5858
[llvm_cap_ty, llvm_anyint_ty],
5959
[IntrNoMem, IntrWillReturn]>;
60+
def int_cheri_cap_bounds_set_round_down :
61+
Intrinsic<[llvm_cap_ty],
62+
[llvm_cap_ty, llvm_anyint_ty],
63+
[IntrNoMem, IntrWillReturn]>;
6064
def int_cheri_cap_type_get :
6165
Intrinsic<[llvm_anyint_ty],
6266
[llvm_cap_ty],

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,7 @@ bool RISCVInstrInfo::isSetBoundsInstr(const MachineInstr &I,
17431743
case RISCV::CSetBounds:
17441744
case RISCV::CSetBoundsExact:
17451745
case RISCV::CSetBoundsImm:
1746+
case RISCV::CSetBoundsRoundDown:
17461747
Base = &I.getOperand(1);
17471748
Size = &I.getOperand(2);
17481749
return true;

llvm/lib/Target/RISCV/RISCVInstrInfoXCheri.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ let mayTrap = 1 in {
414414
def CSetBounds : Cheri_rr<0x8, "csetbounds">;
415415
def CSetBoundsExact : Cheri_rr<0x9, "csetboundsexact">;
416416
def CSetBoundsImm : Cheri_ri<0x2, "csetbounds", 0>;
417+
def CSetBoundsRoundDown : Cheri_rr<0xA, "csetboundsrounddown">;
417418
} // mayTrap = 1
418419
} // let Constraints = "@traps_if_sealed $rs1"
419420
def CClearTag : Cheri_r<0xb, "ccleartag", GPCR>;
@@ -1300,6 +1301,7 @@ def : PatGpcrGpr<cptradd, CIncOffset>;
13001301
def : PatGpcrSimm12<cptradd, CIncOffsetImm>;
13011302
def : PatGpcrGpr<int_cheri_cap_bounds_set, CSetBounds>;
13021303
def : PatGpcrGpr<int_cheri_cap_bounds_set_exact, CSetBoundsExact>;
1304+
def : PatGpcrGpr<int_cheri_cap_bounds_set_round_down, CSetBoundsRoundDown>;
13031305
def : PatGpcrGpr<riscv_cap_bounds_set, CSetBounds>;
13041306
def : PatGpcrUimm12<riscv_cap_bounds_set, CSetBoundsImm>;
13051307
def : PatGpcrUimm12<int_cheri_cap_bounds_set, CSetBoundsImm>;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 2
2+
; RUN: llc --filetype=asm --mcpu=cheriot --mtriple=riscv32cheriot-unknown-cheriotrtos -target-abi cheriot %s -mattr=+xcheri,+cap-mode -o - | FileCheck %s
3+
target datalayout = "e-m:e-pf200:64:64:64:32-p:32:32-i64:64-n32-S128-A200-P200-G200"
4+
target triple = "riscv32cheriot-unknown-cheriotrtos"
5+
6+
define ptr addrspace(200) @foo(ptr addrspace(200) %cap, i32 noundef %b) addrspace(200) {
7+
; CHECK-LABEL: foo:
8+
; CHECK: # %bb.0: # %entry
9+
; CHECK-NEXT: csetboundsrounddown ca0, ca0, a1
10+
; CHECK-NEXT: cret
11+
entry:
12+
%0 = tail call ptr addrspace(200) @llvm.cheri.cap.bounds.set.round.down.i32(ptr addrspace(200) %cap, i32 %b)
13+
ret ptr addrspace(200) %0
14+
}
15+
16+
declare ptr addrspace(200) @llvm.cheri.cap.bounds.set.round.down.i32(ptr addrspace(200), i32) addrspace(200)

llvm/test/MC/RISCV/cheri/cheriot.s

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# RUN: llvm-mc %s -triple=riscv32cheriot -mcpu=cheriot -mattr=+xcheri -riscv-no-aliases -show-encoding \
2+
# RUN: | FileCheck %s
3+
4+
csetboundsrounddown cra, cra, zero
5+
# CHECK: encoding: [0xdb,0x80,0x00,0x14]
6+
csetboundsrounddown cra, ca5, zero
7+
# CHECK: encoding: [0xdb,0x80,0x07,0x14]
8+
csetboundsrounddown cra, cra, a5
9+
# CHECK: encoding: [0xdb,0x80,0xf0,0x14]
10+
csetboundsrounddown cra, ca5, a5
11+
# CHECK: encoding: [0xdb,0x80,0xf7,0x14]
12+
csetboundsrounddown ca5, cra, zero
13+
# CHECK: [0xdb,0x87,0x00,0x14]
14+
csetboundsrounddown ca5, ca5, zero
15+
# CHECK: [0xdb,0x87,0x07,0x14]
16+
csetboundsrounddown ca5, cra, a5
17+
# CHECK: [0xdb,0x87,0xf0,0x14]
18+
csetboundsrounddown ca5, ca5, a5
19+
# CHECK: [0xdb,0x87,0xf7,0x14]

0 commit comments

Comments
 (0)