Skip to content

Commit 9e30f3b

Browse files
nwfresistor
authored andcommitted
[cheriot] CSetBoundsRoundDown instruction and intrinsic
1 parent 905c7f6 commit 9e30f3b

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
@@ -1688,6 +1688,7 @@ BUILTIN(__builtin_cheri_address_set, "v*mvC*mz", "nct")
16881688
BUILTIN(__builtin_cheri_base_get, "zvC*m", "nct")
16891689
BUILTIN(__builtin_cheri_bounds_set, "v*mvC*mz", "nct")
16901690
BUILTIN(__builtin_cheri_bounds_set_exact, "v*mvC*mz", "nct")
1691+
BUILTIN(__builtin_cheri_bounds_set_round_down, "v*mvC*mz", "nct")
16911692
BUILTIN(__builtin_cheri_equal_exact, "bvC*mvC*m", "nct")
16921693
BUILTIN(__builtin_cheri_flags_set, "v*mvC*mz", "nct")
16931694
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
@@ -5032,6 +5032,15 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
50325032
{SizeTy}, {Cap, Length}),
50335033
Cap->getType()));
50345034
}
5035+
case Builtin::BI__builtin_cheri_bounds_set_round_down: {
5036+
Value *Cap = EmitScalarExpr(E->getArg(0));
5037+
Value *Length = EmitScalarExpr(E->getArg(1));
5038+
return RValue::get(Builder.CreateBitCast(
5039+
Builder.CreateIntrinsic(
5040+
llvm::Intrinsic::cheri_cap_bounds_set_round_down, {SizeTy},
5041+
{Cap, Length}),
5042+
Cap->getType()));
5043+
}
50355044
case Builtin::BI__builtin_cheri_flags_get:
50365045
return RValue::get(
50375046
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
@@ -1494,6 +1494,7 @@ bool RISCVInstrInfo::isSetBoundsInstr(const MachineInstr &I,
14941494
case RISCV::CSetBounds:
14951495
case RISCV::CSetBoundsExact:
14961496
case RISCV::CSetBoundsImm:
1497+
case RISCV::CSetBoundsRoundDown:
14971498
Base = &I.getOperand(1);
14981499
Size = &I.getOperand(2);
14991500
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>;
@@ -1306,6 +1307,7 @@ def : PatGpcrGpr<cptradd, CIncOffset>;
13061307
def : PatGpcrSimm12<cptradd, CIncOffsetImm>;
13071308
def : PatGpcrGpr<int_cheri_cap_bounds_set, CSetBounds>;
13081309
def : PatGpcrGpr<int_cheri_cap_bounds_set_exact, CSetBoundsExact>;
1310+
def : PatGpcrGpr<int_cheri_cap_bounds_set_round_down, CSetBoundsRoundDown>;
13091311
def : PatGpcrGpr<riscv_cap_bounds_set, CSetBounds>;
13101312
def : PatGpcrUimm12<riscv_cap_bounds_set, CSetBoundsImm>;
13111313
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)