Skip to content

Commit a6a9b13

Browse files
committed
[llvm][clang] Add intrinsic support for cbo.zero instruction (Zicboz ISA extension)
1 parent 0591297 commit a6a9b13

File tree

8 files changed

+77
-2
lines changed

8 files changed

+77
-2
lines changed

clang/include/clang/Basic/BuiltinsRISCV.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,13 @@ def ntl_store : RISCVBuiltin<"void(...)">;
153153
let Features = "zihintpause", Attributes = [NoThrow] in
154154
def pause : RISCVBuiltin<"void()">;
155155

156+
//===----------------------------------------------------------------------===//
157+
// Zicboz extension.
158+
//===----------------------------------------------------------------------===//
159+
let Features = "zicboz" in {
160+
def cbo_zero : RISCVBuiltin<"void(void *)">;
161+
} // Features = "zicboz"
162+
156163
//===----------------------------------------------------------------------===//
157164
// XCV extensions.
158165
//===----------------------------------------------------------------------===//

clang/lib/CodeGen/TargetBuiltins/RISCV.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,12 @@ Value *CodeGenFunction::EmitRISCVBuiltinExpr(unsigned BuiltinID,
12961296
return Builder.CreateCall(Fn, {});
12971297
}
12981298

1299+
// Zicboz
1300+
case RISCV::BI__builtin_riscv_cbo_zero:
1301+
ID = Intrinsic::riscv_cbo_zero;
1302+
IntrinsicTypes = {Int8PtrTy};
1303+
break;
1304+
12991305
// XCValu
13001306
case RISCV::BI__builtin_riscv_cv_alu_addN:
13011307
ID = Intrinsic::riscv_cv_alu_addN;

clang/lib/Headers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ set(ppc_htm_files
125125

126126
set(riscv_files
127127
riscv_bitmanip.h
128+
riscv_cmo.h
128129
riscv_corev_alu.h
129130
riscv_crypto.h
130131
riscv_nds.h

clang/lib/Headers/riscv_cmo.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*===---- riscv_cmo.h - RISC-V CMO intrinsics ----------------------------===
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+
10+
#ifndef __RISCV_CMO_H
11+
#define __RISCV_CMO_H
12+
13+
#if defined(__cplusplus)
14+
extern "C" {
15+
#endif
16+
17+
#if defined(__riscv_zicboz)
18+
static __inline__ void __attribute__((__always_inline__, __nodebug__))
19+
__riscv_cbo_zero(void *__x) {
20+
return __builtin_riscv_cbo_zero(__x);
21+
}
22+
#endif // defined(__riscv_zicboz)
23+
24+
#if defined(__cplusplus)
25+
}
26+
#endif
27+
28+
#endif // define __RISCV_CMO_H
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// REQUIRES: riscv-registered-target
2+
// RUN: %clang_cc1 -triple riscv64 -target-feature +zicboz \
3+
// RUN: -Wall -Wno-unused -Werror -fsyntax-only -verify %s
4+
5+
#include <riscv_cmo.h>
6+
7+
void test(void *x) {
8+
__riscv_cbo_zero(x, 0); // expected-error{{too many arguments to function call, expected single argument '__x', have 2 arguments}} expected-note@riscv_cmo.h:* {{'__riscv_cbo_zero' declared here}}
9+
int res = __riscv_cbo_zero(x); // expected-error{{initializing 'int' with an expression of incompatible type 'void'}}
10+
__riscv_cbo_zero(42); // expected-error{{incompatible integer to pointer conversion passing 'int' to parameter of type 'void *'}} expected-note@riscv_cmo.h:* {{passing argument to parameter '__x' here}}
11+
}
12+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
// REQUIRES: riscv-registered-target
3+
// RUN: %clang_cc1 -triple riscv64 -target-feature +zicboz -O2 -S -o - %s | FileCheck %s
4+
5+
#include <riscv_cmo.h>
6+
7+
void test(void *x) {
8+
// CHECK-LABEL: test:
9+
// CHECK: cbo.zero (a0)
10+
// CHECK: ret
11+
__riscv_cbo_zero(x);
12+
}
13+

llvm/include/llvm/IR/IntrinsicsRISCV.td

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,13 +1953,20 @@ let TargetPrefix = "riscv" in {
19531953
} // TargetPrefix = "riscv"
19541954

19551955

1956-
// Zihintpause extensions
19571956
//===----------------------------------------------------------------------===//
1957+
// Zihintpause extensions
19581958
let TargetPrefix = "riscv" in
19591959
def int_riscv_pause : DefaultAttrsIntrinsic<[], [], [IntrNoMem, IntrHasSideEffects]>;
19601960

1961-
// Vendor extensions
19621961
//===----------------------------------------------------------------------===//
1962+
// Cache Management
1963+
let TargetPrefix = "riscv" in {
1964+
// Zicboz
1965+
def int_riscv_cbo_zero : Intrinsic<[], [llvm_anyptr_ty], [IntrWriteMem]>;
1966+
} // TargetPrefix = "riscv"
1967+
1968+
//===----------------------------------------------------------------------===//
1969+
// Vendor extensions
19631970
include "llvm/IR/IntrinsicsRISCVXTHead.td"
19641971
include "llvm/IR/IntrinsicsRISCVXsf.td"
19651972
include "llvm/IR/IntrinsicsRISCVXCV.td"

llvm/lib/Target/RISCV/RISCVInstrInfoZicbo.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def CBO_INVAL : CBO_r<0b000000000000, "cbo.inval">, Sched<[]>;
5656

5757
let Predicates = [HasStdExtZicboz] in {
5858
def CBO_ZERO : CBO_r<0b000000000100, "cbo.zero">, Sched<[]>;
59+
def : Pat<(int_riscv_cbo_zero GPR:$rs1), (CBO_ZERO GPR:$rs1)>;
5960
} // Predicates = [HasStdExtZicboz]
6061

6162
let Predicates = [HasStdExtZicbop, NoVendorXMIPSCBOP] in {

0 commit comments

Comments
 (0)